Changes existing code to use new base58 API

This commit is contained in:
Daniel Cousens 2014-04-04 05:10:12 +11:00
parent f53b821cc9
commit 6fa0c07763
7 changed files with 83 additions and 52 deletions

View file

@ -1,4 +1,5 @@
var base58 = require('./base58') var base58 = require('./base58')
var base58check = require('./base58check')
var convert = require('./convert') var convert = require('./convert')
var error = require('./util').error var error = require('./util').error
var mainnet = require('./network').mainnet.addressVersion var mainnet = require('./network').mainnet.addressVersion
@ -13,8 +14,19 @@ function Address(bytes, version) {
this.version = bytes.version this.version = bytes.version
} }
else if (typeof bytes === 'string') { else if (typeof bytes === 'string') {
this.hash = stringToHash(bytes) if (bytes.length <= 35) {
this.version = version || this.hash.version || mainnet var decode = base58check.decode(bytes)
this.hash = decode.payload
this.version = decode.version
}
else if (bytes.length <= 40) {
this.hash = convert.hexToBytes(bytes)
this.version = version || mainnet
}
else {
error('invalid or unrecognized input')
}
} }
else { else {
this.hash = bytes this.hash = bytes
@ -22,22 +34,12 @@ function Address(bytes, version) {
} }
} }
function stringToHash(str) {
if (str.length <= 35) {
return base58.checkDecode(str)
}
if (str.length <= 40) {
return convert.hexToBytes(str)
}
error('invalid or unrecognized input')
}
/** /**
* Serialize this object as a standard Bitcoin address. * Serialize this object as a standard Bitcoin address.
* Returns the address as a base58-encoded string in the standardized format. * Returns the address as a base58-encoded string in the standardized format.
*/ */
Address.prototype.toString = function () { Address.prototype.toString = function () {
return base58.checkEncode(this.hash.slice(0), this.version) return base58check.encode(this.hash.slice(0), this.version)
} }
/** /**
@ -53,7 +55,7 @@ Address.getVersion = function (address) {
*/ */
Address.validate = function (address) { Address.validate = function (address) {
try { try {
base58.checkDecode(address) base58check.decode(address)
return true return true
} catch (e) { } catch (e) {
return false return false

View file

@ -8,6 +8,11 @@ function lpad(str, padString, length) {
} }
function bytesToHex(bytes) { function bytesToHex(bytes) {
// FIXME: transitionary fix
if (Buffer.isBuffer(bytes)) {
return bytes.toString('hex')
}
return bytes.map(function(x) { return bytes.map(function(x) {
return lpad(x.toString(16), '0', 2) return lpad(x.toString(16), '0', 2)
}).join('') }).join('')

View file

@ -1,7 +1,7 @@
var Address = require('./address') var Address = require('./address')
var assert = require('assert') var assert = require('assert')
var convert = require('./convert') var convert = require('./convert')
var base58 = require('./base58') var base58check = require('./base58check')
var BigInteger = require('./jsbn/jsbn') var BigInteger = require('./jsbn/jsbn')
var ecdsa = require('./ecdsa') var ecdsa = require('./ecdsa')
var ECPointFp = require('./jsbn/ec').ECPointFp var ECPointFp = require('./jsbn/ec').ECPointFp
@ -32,10 +32,10 @@ ECKey.prototype.import = function (input, compressed) {
: Array.isArray(input) ? fromBin(input.slice(0, 32)) : Array.isArray(input) ? fromBin(input.slice(0, 32))
: typeof input != "string" ? null : typeof input != "string" ? null
: input.length == 44 ? fromBin(convert.base64ToBytes(input)) : input.length == 44 ? fromBin(convert.base64ToBytes(input))
: input.length == 51 && input[0] == '5' ? fromBin(base58.checkDecode(input)) : input.length == 51 && input[0] == '5' ? fromBin(base58check.decode(input).payload)
: input.length == 51 && input[0] == '9' ? fromBin(base58.checkDecode(input)) : input.length == 51 && input[0] == '9' ? fromBin(base58check.decode(input).payload)
: input.length == 52 && has('LK', input[0]) ? fromBin(base58.checkDecode(input).slice(0, 32)) : input.length == 52 && has('LK', input[0]) ? fromBin(base58check.decode(input).payload.slice(0, 32))
: input.length == 52 && input[0] == 'c' ? fromBin(base58.checkDecode(input).slice(0, 32)) : input.length == 52 && input[0] == 'c' ? fromBin(base58check.decode(input).payload.slice(0, 32))
: has([64,65],input.length) ? fromBin(convert.hexToBytes(input.slice(0, 64))) : has([64,65],input.length) ? fromBin(convert.hexToBytes(input.slice(0, 64)))
: null : null
@ -76,7 +76,7 @@ ECKey.version_bytes = {
ECKey.prototype.toWif = function(version) { ECKey.prototype.toWif = function(version) {
version = version || Network.mainnet.addressVersion version = version || Network.mainnet.addressVersion
return base58.checkEncode(this.toBytes(), ECKey.version_bytes[version]) return base58check.encode(this.toBytes(), ECKey.version_bytes[version])
} }
ECKey.prototype.toHex = function() { ECKey.prototype.toHex = function() {
@ -167,7 +167,7 @@ ECPubKey.prototype.toBin = function(compressed) {
ECPubKey.prototype.toWif = function(version) { ECPubKey.prototype.toWif = function(version) {
version = version || Network.mainnet.addressVersion version = version || Network.mainnet.addressVersion
return base58.checkEncode(this.toBytes(), version) return base58check.encode(this.toBytes(), version)
} }
ECPubKey.prototype.toString = ECPubKey.prototype.toHex ECPubKey.prototype.toString = ECPubKey.prototype.toHex

View file

@ -11,7 +11,16 @@ var ECPubKey = require('./eckey.js').ECPubKey
var Address = require('./address.js') var Address = require('./address.js')
var Network = require('./network') var Network = require('./network')
var HDWallet = module.exports = function(seed, network) { var crypto = require('crypto')
function sha256(buf) {
var hash = crypto.createHash('sha256')
hash.update(buf)
return hash.digest()
}
function HDWallet(seed, network) {
if (seed === undefined) return; if (seed === undefined) return;
var seedWords = convert.bytesToWordArray(seed) var seedWords = convert.bytesToWordArray(seed)
@ -35,8 +44,6 @@ function arrayEqual(a, b) {
return !(a < b || a > b) return !(a < b || a > b)
} }
HDWallet.getChecksum = base58.getChecksum
HDWallet.fromSeedHex = function(hex, network) { HDWallet.fromSeedHex = function(hex, network) {
return new HDWallet(convert.hexToBytes(hex), network) return new HDWallet(convert.hexToBytes(hex), network)
} }
@ -45,20 +52,17 @@ HDWallet.fromSeedString = function(string, network) {
return new HDWallet(convert.stringToBytes(string), network) return new HDWallet(convert.stringToBytes(string), network)
} }
HDWallet.fromBase58 = function(input) { HDWallet.fromBase58 = function(string) {
var buffer = base58.decode(input) var buffer = base58.decode(string)
if (buffer.length == HDWallet.LENGTH + 4) { var payload = buffer.slice(0, -4)
var expectedChecksum = buffer.slice(HDWallet.LENGTH, HDWallet.LENGTH + 4) var checksum = buffer.slice(-4)
buffer = buffer.slice(0, HDWallet.LENGTH) var newChecksum = sha256(sha256(payload)).slice(0, 4)
var actualChecksum = HDWallet.getChecksum(buffer)
if (!arrayEqual(expectedChecksum, actualChecksum)) { assert.deepEqual(newChecksum, checksum)
throw new Error('Checksum mismatch') assert.equal(payload.length, HDWallet.LENGTH)
}
}
return HDWallet.fromBytes(buffer) return HDWallet.fromBytes(payload)
} }
HDWallet.fromHex = function(input) { HDWallet.fromHex = function(input) {
@ -71,6 +75,11 @@ HDWallet.fromBytes = function(input) {
throw new Error(format('Invalid input length, %s. Expected %s.', input.length, HDWallet.LENGTH)) throw new Error(format('Invalid input length, %s. Expected %s.', input.length, HDWallet.LENGTH))
} }
// FIXME: transitionary fix
if (Buffer.isBuffer(input)) {
input = Array.prototype.map.bind(input, function(x) { return x })()
}
var hd = new HDWallet() var hd = new HDWallet()
// 4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private // 4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private
@ -182,10 +191,13 @@ HDWallet.prototype.toHex = function(priv) {
} }
HDWallet.prototype.toBase58 = function(priv) { HDWallet.prototype.toBase58 = function(priv) {
var buffer = this.toBytes(priv) var buffer = new Buffer(this.toBytes(priv))
, checksum = HDWallet.getChecksum(buffer) var checksum = sha256(sha256(buffer)).slice(0, 4)
buffer = buffer.concat(checksum)
return base58.encode(buffer) return base58.encode(Buffer.concat([
buffer,
checksum
]))
} }
HDWallet.prototype.derive = function(i) { HDWallet.prototype.derive = function(i) {
@ -252,3 +264,4 @@ function HmacFromBytesToBytes(hasher, message, key) {
return convert.wordArrayToBytes(hmac.finalize()) return convert.wordArrayToBytes(hmac.finalize())
} }
module.exports = HDWallet

View file

@ -1200,6 +1200,11 @@ BigInteger.valueOf = nbv;
* endian notation and ignore leading zeros. * endian notation and ignore leading zeros.
*/ */
BigInteger.fromByteArrayUnsigned = function(ba) { BigInteger.fromByteArrayUnsigned = function(ba) {
// FIXME: BigInteger doesn't yet support Buffers
if (Buffer.isBuffer(ba)) {
ba = Array.prototype.map.bind(ba, function(x) { return x })()
}
if (!ba.length) { if (!ba.length) {
return new BigInteger.valueOf(0); return new BigInteger.valueOf(0);
} else if (ba[0] & 0x80) { } else if (ba[0] & 0x80) {

View file

@ -279,6 +279,11 @@ Script.prototype.writeOp = function(opcode) {
* Add a data chunk to the script. * Add a data chunk to the script.
*/ */
Script.prototype.writeBytes = function(data) { Script.prototype.writeBytes = function(data) {
// FIXME: Script module doesn't support buffers yet
if (Buffer.isBuffer(data)) {
data = Array.prototype.map.bind(data, function(x) { return x })()
}
if (data.length < Opcode.map.OP_PUSHDATA1) { if (data.length < Opcode.map.OP_PUSHDATA1) {
this.buffer.push(data.length) this.buffer.push(data.length)
} else if (data.length <= 0xff) { } else if (data.length <= 0xff) {

View file

@ -1,7 +1,8 @@
var assert = require('assert') var assert = require('assert')
var Address = require('../src/address.js') var Address = require('../src/address')
var network = require('../src/network.js') var network = require('../src/network')
var base58 = require('../src/base58.js') var base58 = require('../src/base58')
var base58check = require('../src/base58check')
var mainnet = network.mainnet.addressVersion var mainnet = network.mainnet.addressVersion
var testnet = network.testnet.addressVersion var testnet = network.testnet.addressVersion
@ -10,10 +11,10 @@ describe('Address', function() {
var testnetP2shAddress, mainnetP2shAddress var testnetP2shAddress, mainnetP2shAddress
beforeEach(function(){ beforeEach(function(){
testnetAddress = 'mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef'
mainnetAddress = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa' mainnetAddress = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'
testnetP2shAddress = '2MxKEf2su6FGAUfCEAHreGFQvEYrfYNHvL7' testnetAddress = 'mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef'
mainnetP2shAddress = '3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt' mainnetP2shAddress = '3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt'
testnetP2shAddress = '2MxKEf2su6FGAUfCEAHreGFQvEYrfYNHvL7'
}) })
describe('parsing', function() { describe('parsing', function() {
@ -36,14 +37,14 @@ describe('Address', function() {
}) })
it('works for byte input', function() { it('works for byte input', function() {
var hash = base58.checkDecode('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa') var hash = base58check.decode(mainnetAddress)
var addr = new Address(hash) var addr = new Address(hash.payload)
assert.equal(addr.hash, hash) assert.equal(addr.hash, hash.payload)
assert.equal(network.mainnet.addressVersion, hash.version) assert.equal(network.mainnet.addressVersion, hash.version)
var hash = base58.checkDecode('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef') var hash = base58check.decode(testnetAddress)
var addr = new Address(hash) var addr = new Address(hash.payload)
assert.equal(addr.hash, hash) assert.equal(addr.hash, hash.payload)
assert.equal(network.testnet.addressVersion, hash.version) assert.equal(network.testnet.addressVersion, hash.version)
}) })
@ -56,8 +57,8 @@ describe('Address', function() {
describe('getVersion', function() { describe('getVersion', function() {
it('returns the proper address version', function() { it('returns the proper address version', function() {
assert.equal(Address.getVersion('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'), network.mainnet.addressVersion) assert.equal(Address.getVersion(mainnetAddress), network.mainnet.addressVersion)
assert.equal(Address.getVersion('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef'), network.testnet.addressVersion) assert.equal(Address.getVersion(testnetAddress), network.testnet.addressVersion)
}) })
}) })