bitcoinjs-lib/test/hdwallet.js

155 lines
4.8 KiB
JavaScript
Raw Normal View History

var assert = require('assert')
var networks = require('../src/networks')
var HDWallet = require('../src/hdwallet')
var fixtures = require('./fixtures/hdwallet.json')
2014-04-16 22:18:31 +02:00
function b2h(buf) {
assert(Buffer.isBuffer(buf))
return buf.toString('hex')
}
describe('HDWallet', function() {
2014-03-31 05:47:47 +02:00
describe('toBase58', function() {
it('reproduces input', function() {
var input = 'xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5'
var output = HDWallet.fromBase58(input).toBase58(false)
assert.equal(output, input)
2014-03-31 05:47:47 +02:00
input = 'xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334'
output = HDWallet.fromBase58(input).toBase58(true)
assert.equal(output, input)
})
2014-03-31 05:47:47 +02:00
it('fails with priv=true when theres no private key', function() {
var hd = HDWallet.fromBase58('xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon')
2014-05-31 07:24:03 +02:00
assert.throws(function() {
2014-03-31 05:47:47 +02:00
hd.toBase58(true)
2014-05-31 07:24:03 +02:00
}, /Missing private key/)
})
2014-03-31 05:47:47 +02:00
})
describe('fromBase58', function() {
fixtures.invalid.fromBase58.forEach(function(f) {
it('throws on ' + f.string, function() {
assert.throws(function() {
HDWallet.fromBase58(f.string)
}, new RegExp(f.exception))
})
})
})
describe('fromBuffer', function() {
fixtures.invalid.fromBuffer.forEach(function(f) {
it('throws on ' + f.exception, function() {
var buffer = new Buffer(f.hex, 'hex')
assert.throws(function() {
HDWallet.fromBuffer(buffer)
}, new RegExp(f.exception))
})
})
})
2014-05-31 12:16:42 +02:00
describe('fromSeedBuffer', function() {
2014-04-17 11:08:16 +02:00
var expectedPrivateKey = '0fd71c652e847ba7ea7956e3cf3fc0a0985871846b1b2c23b9c6a29a38cee860'
2014-05-31 12:16:42 +02:00
var seedHex = '6372617a7920686f727365206261747465727920737461706c65'
2014-05-31 12:16:42 +02:00
it('creates from a binary seed', function() {
var hd = HDWallet.fromSeedBuffer(new Buffer(seedHex, 'hex'))
assert.equal(hd.priv.D.toHex(), expectedPrivateKey)
2014-03-31 05:47:47 +02:00
assert(hd.pub)
})
2014-03-31 05:47:47 +02:00
describe('fromSeedHex', function() {
it('creates from hex seed', function() {
2014-05-31 12:16:42 +02:00
var hd = HDWallet.fromSeedHex(seedHex)
assert.equal(hd.priv.D.toHex(), expectedPrivateKey)
2014-03-31 05:47:47 +02:00
assert(hd.pub)
})
})
2014-03-31 05:47:47 +02:00
})
2014-03-31 05:47:47 +02:00
describe('Test vectors', function() {
function verifyVector(hd, v) {
assert.equal(b2h(hd.getIdentifier()), v.identifier)
assert.equal(b2h(hd.getFingerprint()), v.fingerprint)
assert.equal(hd.getAddress().toString(), v.address)
assert.equal(hd.priv.toWIF(), v.wif)
assert.equal(hd.pub.toHex(), v.pubKey)
2014-05-31 12:16:42 +02:00
assert.equal(b2h(hd.chainCode), v.chainCode)
assert.equal(hd.toBase58(false), v.base58)
assert.equal(hd.toBase58(true), v.base58Priv)
}
it('matches the test vectors', function() {
fixtures.valid.forEach(function(f) {
var hd = HDWallet.fromSeedHex(f.master.seed)
verifyVector(hd, f.master)
f.children.forEach(function(c) {
// FIXME: c.description could be shown
if (c.mPriv != undefined) {
hd = hd.derivePrivate(c.mPriv)
} else {
hd = hd.derive(c.m)
}
verifyVector(hd, c)
})
})
})
2014-03-31 05:47:47 +02:00
})
2014-05-04 03:40:48 +02:00
describe('derive', function() {
describe('m/0', function() {
var wallet = HDWallet.fromBase58('xpub6CxuB8ifZCMXeS3KbyNkYvrsJEHqxedCSiUhrNwH1nKtb8hcJpxDbDxkdoVCTR2bQ1G8hY4UMv85gef9SEpgFFUftBjt37FUSZxVx4AU9Qh').derive(0)
2014-05-04 03:40:48 +02:00
it('derives the correct public key', function() {
2014-05-04 07:15:22 +02:00
assert.equal(wallet.pub.toHex(), '02df843e6ae2017e0772d0584f76f56b8f2f5181a3045c7a7740a9d86dc7c80ce7')
})
it('derives the correct depth', function() {
2014-05-04 03:40:48 +02:00
assert.equal(wallet.depth, 4)
})
})
})
2014-03-31 05:47:47 +02:00
describe('network types', function() {
var seed
beforeEach(function() {
seed = new Buffer('foobar')
})
it('ensure that a bitcoin wallet is the default', function() {
2014-05-31 12:16:42 +02:00
var hd = HDWallet.fromSeedBuffer(seed)
2014-05-31 12:16:42 +02:00
assert.equal(hd.network, networks.bitcoin)
})
2014-04-16 21:43:34 +02:00
it('ensures that a bitcoin Wallet generates bitcoin addresses', function() {
2014-05-31 12:16:42 +02:00
var hd = HDWallet.fromSeedBuffer(seed, networks.bitcoin)
var address = hd.getAddress().toString()
assert.equal(address, '17SnB9hyGwJPoKpLb9eVPHjsujyEuBpMAA')
2014-03-31 05:47:47 +02:00
})
2014-03-31 05:47:47 +02:00
it('ensures that a testnet Wallet generates testnet addresses', function() {
2014-05-31 12:16:42 +02:00
var hd = HDWallet.fromSeedBuffer(seed, networks.testnet)
var address = hd.getAddress().toString()
assert.equal(address, 'mmxjUCnx5xjeaSHxJicsDCxCmjZwq8KTbv')
})
it('throws an exception when unknown network type is passed in', function() {
assert.throws(function() {
2014-05-31 12:16:42 +02:00
HDWallet.fromSeedBuffer(seed, {})
}, /Unknown BIP32 constants for network/)
})
2014-03-31 05:47:47 +02:00
})
})