From c0006c299ff1ba676cf8193363972962521f8894 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sat, 31 May 2014 20:16:42 +1000 Subject: [PATCH] HDWallet: use new constructor --- src/hdwallet.js | 128 ++++++++++++++++++++---------------- src/wallet.js | 2 +- test/fixtures/hdwallet.json | 24 +++---- test/hdwallet.js | 28 ++++---- 4 files changed, 99 insertions(+), 83 deletions(-) diff --git a/src/hdwallet.js b/src/hdwallet.js index 2888683..22413a8 100644 --- a/src/hdwallet.js +++ b/src/hdwallet.js @@ -5,39 +5,49 @@ var BigInteger = require('bigi') var crypto = require('./crypto') var ECKey = require('./eckey') var ECPubKey = require('./ecpubkey') +var ECPointFp = require('./ec').ECPointFp var networks = require('./networks') var sec = require('./sec') var ecparams = sec("secp256k1") -function HDWallet(seed, network) { - if (seed == undefined) return; // FIXME: Boo, should be stricter - +function HDWallet(K, chainCode, network) { network = network || networks.bitcoin - assert(network.bip32, 'Unknown BIP32 constants for network') - var I = crypto.HmacSHA512(seed, HDWallet.MASTER_SECRET) - var IL = I.slice(0, 32) - var IR = I.slice(32) - - // In case IL is 0 or >= n, the master key is invalid (handled by ECKey.fromBuffer) - var pIL = BigInteger.fromBuffer(IL) - - this.network = network - this.priv = new ECKey(pIL, true) - this.pub = this.priv.pub + assert(Buffer.isBuffer(chainCode), 'Expected Buffer, got ' + chainCode) + assert(network.bip32, 'Unknown BIP32 constants for network') - this.chaincode = IR + this.chainCode = chainCode this.depth = 0 this.index = 0 + this.network = network + + if (K instanceof BigInteger) { + this.priv = new ECKey(K, true) + this.pub = this.priv.pub + } else { + this.pub = new ECPubKey(K, true) + } } HDWallet.MASTER_SECRET = new Buffer('Bitcoin seed') HDWallet.HIGHEST_BIT = 0x80000000 HDWallet.LENGTH = 78 +HDWallet.fromSeedBuffer = function(seed, network) { + var I = crypto.HmacSHA512(seed, HDWallet.MASTER_SECRET) + var IL = I.slice(0, 32) + var IR = I.slice(32) + + // In case IL is 0 or >= n, the master key is invalid + // This is handled by `new ECKey` in the HDWallet constructor + var pIL = BigInteger.fromBuffer(IL) + + return new HDWallet(pIL, IR, network) +} + HDWallet.fromSeedHex = function(hex, network) { - return new HDWallet(new Buffer(hex, 'hex'), network) + return HDWallet.fromSeedBuffer(new Buffer(hex, 'hex'), network) } HDWallet.fromBase58 = function(string) { @@ -53,59 +63,66 @@ HDWallet.fromBase58 = function(string) { return HDWallet.fromBuffer(payload) } -HDWallet.fromBuffer = function(input) { - assert.strictEqual(input.length, HDWallet.LENGTH, 'Invalid buffer length') - - var hd = new HDWallet() +HDWallet.fromBuffer = function(buffer) { + assert.strictEqual(buffer.length, HDWallet.LENGTH, 'Invalid buffer length') // 4 byte: version bytes - var version = input.readUInt32BE(0) + var version = buffer.readUInt32BE(0) - var type + var hdNetwork, isPrivate for (var name in networks) { var network = networks[name] - for (var t in network.bip32) { - if (version != network.bip32[t]) continue + for (var type in network.bip32) { + if (version != network.bip32[type]) continue - type = t - hd.network = network + hdNetwork = network + isPrivate = (type === 'priv') } } - if (!hd.network) { - throw new Error('Could not find version ' + version.toString(16)) - } + assert(hdNetwork, 'Could not find version ' + version.toString(16)) // 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ... - hd.depth = input.readUInt8(4) + var depth = buffer.readUInt8(4) // 4 bytes: the fingerprint of the parent's key (0x00000000 if master key) - hd.parentFingerprint = input.readUInt32BE(5) - if (hd.depth === 0) { - assert.strictEqual(hd.parentFingerprint, 0x00000000, 'Invalid parent fingerprint') + var parentFingerprint = buffer.readUInt32BE(5) + if (depth === 0) { + assert.strictEqual(parentFingerprint, 0x00000000, 'Invalid parent fingerprint') } // 4 bytes: child number. This is the number i in xi = xpar/i, with xi the key being serialized. // This is encoded in MSB order. (0x00000000 if master key) - hd.index = input.readUInt32BE(9) - assert(hd.depth > 0 || hd.index === 0, 'Invalid index') + var index = buffer.readUInt32BE(9) + assert(depth > 0 || index === 0, 'Invalid index') // 32 bytes: the chain code - hd.chaincode = input.slice(13, 45) + var chainCode = buffer.slice(13, 45) // 33 bytes: the public key or private key data (0x02 + X or 0x03 + X for // public keys, 0x00 + k for private keys) - if (type == 'priv') { - assert.equal(input.readUInt8(45), 0, 'Invalid private key') - var D = BigInteger.fromBuffer(input.slice(46, 78)) + var data = buffer.slice(45, 78) + + var hd + if (isPrivate) { + assert.strictEqual(data.readUInt8(0), 0x00, 'Invalid private key') + data = data.slice(1) - hd.priv = new ECKey(D, true) - hd.pub = hd.priv.pub + var D = BigInteger.fromBuffer(data) + hd = new HDWallet(D, chainCode, hdNetwork) } else { - hd.pub = ECPubKey.fromBuffer(input.slice(45, 78), true) + + var decode = ECPointFp.decodeFrom(ecparams.getCurve(), data) + assert.equal(decode.compressed, true, 'Invalid public key') + + hd = new HDWallet(decode.Q, chainCode, hdNetwork) } + hd.depth = depth + hd.index = index + hd.parentFingerprint = parentFingerprint + return hd } @@ -142,7 +159,7 @@ HDWallet.prototype.toBuffer = function(priv) { buffer.writeUInt32BE(this.index, 9) // 32 bytes: the chain code - this.chaincode.copy(buffer, 13) + this.chainCode.copy(buffer, 13) // 33 bytes: the public key or private key data if (priv) { @@ -201,25 +218,28 @@ HDWallet.prototype.derive = function(index) { ]) } - var I = crypto.HmacSHA512(data, this.chaincode) + var I = crypto.HmacSHA512(data, this.chainCode) var IL = I.slice(0, 32) var IR = I.slice(32) - var hd = new HDWallet() var pIL = BigInteger.fromBuffer(IL) + // In case parse256(IL) >= n, proceed with the next value for i + if (pIL.compareTo(ecparams.getN()) >= 0) { + return this.derive(index + 1) + } + // Private parent key -> private child key if (this.priv) { // ki = parse256(IL) + kpar (mod n) var ki = pIL.add(this.priv.D).mod(ecparams.getN()) - // In case parse256(IL) >= n or ki == 0, one should proceed with the next value for i - if (pIL.compareTo(ecparams.getN()) >= 0 || ki.signum() === 0) { + // In case ki == 0, proceed with the next value for i + if (ki.signum() === 0) { return this.derive(index + 1) } - hd.priv = new ECKey(ki, true) - hd.pub = hd.priv.pub + hd = new HDWallet(ki, IR, this.network) // Public parent key -> public child key } else { @@ -227,19 +247,17 @@ HDWallet.prototype.derive = function(index) { // = G*IL + Kpar var Ki = ecparams.getG().multiply(pIL).add(this.pub.Q) - // In case parse256(IL) >= n or Ki is the point at infinity, one should proceed with the next value for i - if (pIL.compareTo(ecparams.getN()) >= 0 || Ki.isInfinity()) { + // In case Ki is the point at infinity, proceed with the next value for i + if (Ki.isInfinity()) { return this.derive(index + 1) } - hd.pub = new ECPubKey(Ki, true) + hd = new HDWallet(Ki, IR, this.network) } - hd.chaincode = IR hd.depth = this.depth + 1 - hd.network = this.network - hd.parentFingerprint = this.getFingerprint().readUInt32BE(0) hd.index = index + hd.parentFingerprint = this.getFingerprint().readUInt32BE(0) return hd } diff --git a/src/wallet.js b/src/wallet.js index 846a5ad..1255228 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -29,7 +29,7 @@ function Wallet(seed, network) { // Make a new master key this.newMasterKey = function(seed) { seed = seed || new Buffer(rng(32)) - masterkey = new HDNode(seed, network) + masterkey = HDNode.fromSeedBuffer(seed, network) // HD first-level child derivation method should be private // See https://bitcointalk.org/index.php?topic=405179.msg4415254#msg4415254 diff --git a/test/fixtures/hdwallet.json b/test/fixtures/hdwallet.json index a983b8f..5ad7005 100644 --- a/test/fixtures/hdwallet.json +++ b/test/fixtures/hdwallet.json @@ -8,7 +8,7 @@ "address": "15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma", "wif": "L52XzL2cMkHxqxBXRyEpnPQZGUs3uKiL3R11XbAdHigRzDozKZeW", "pubKey": "0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2", - "chaincode": "873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508", + "chainCode": "873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508", "hex": "0488b21e000000000000000000873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d5080339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2", "hexPriv": "0488ade4000000000000000000873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d50800e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35", "base58": "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8", @@ -23,7 +23,7 @@ "address": "19Q2WoS5hSS6T8GjhK8KZLMgmWaq4neXrh", "wif": "L5BmPijJjrKbiUfG4zbiFKNqkvuJ8usooJmzuD7Z8dkRoTThYnAT", "pubKey": "035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56", - "chaincode": "47fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141", + "chainCode": "47fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141", "hex": "0488b21e013442193e8000000047fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56", "hexPriv": "0488ade4013442193e8000000047fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae623614100edb2e14f9ee77d26dd93b4ecede8d16ed408ce149b6cd80b0715a2d911a0afea", "base58": "xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw", @@ -37,7 +37,7 @@ "address": "1JQheacLPdM5ySCkrZkV66G2ApAXe1mqLj", "wif": "KyFAjQ5rgrKvhXvNMtFB5PCSKUYD1yyPEe3xr3T34TZSUHycXtMM", "pubKey": "03501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c", - "chaincode": "2a7857631386ba23dacac34180dd1983734e444fdbf774041578e9b6adb37c19", + "chainCode": "2a7857631386ba23dacac34180dd1983734e444fdbf774041578e9b6adb37c19", "hex": "0488b21e025c1bd648000000012a7857631386ba23dacac34180dd1983734e444fdbf774041578e9b6adb37c1903501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c", "hexPriv": "0488ade4025c1bd648000000012a7857631386ba23dacac34180dd1983734e444fdbf774041578e9b6adb37c19003c6cb8d0f6a264c91ea8b5030fadaa8e538b020f0a387421a12de9319dc93368", "base58": "xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ", @@ -51,7 +51,7 @@ "address": "1NjxqbA9aZWnh17q1UW3rB4EPu79wDXj7x", "wif": "L43t3od1Gh7Lj55Bzjj1xDAgJDcL7YFo2nEcNaMGiyRZS1CidBVU", "pubKey": "0357bfe1e341d01c69fe5654309956cbea516822fba8a601743a012a7896ee8dc2", - "chaincode": "04466b9cc8e161e966409ca52986c584f07e9dc81f735db683c3ff6ec7b1503f", + "chainCode": "04466b9cc8e161e966409ca52986c584f07e9dc81f735db683c3ff6ec7b1503f", "hex": "0488b21e03bef5a2f98000000204466b9cc8e161e966409ca52986c584f07e9dc81f735db683c3ff6ec7b1503f0357bfe1e341d01c69fe5654309956cbea516822fba8a601743a012a7896ee8dc2", "hexPriv": "0488ade403bef5a2f98000000204466b9cc8e161e966409ca52986c584f07e9dc81f735db683c3ff6ec7b1503f00cbce0d719ecf7431d88e6a89fa1483e02e35092af60c042b1df2ff59fa424dca", "base58": "xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5", @@ -65,7 +65,7 @@ "address": "1LjmJcdPnDHhNTUgrWyhLGnRDKxQjoxAgt", "wif": "KwjQsVuMjbCP2Zmr3VaFaStav7NvevwjvvkqrWd5Qmh1XVnCteBR", "pubKey": "02e8445082a72f29b75ca48748a914df60622a609cacfce8ed0e35804560741d29", - "chaincode": "cfb71883f01676f587d023cc53a35bc7f88f724b1f8c2892ac1275ac822a3edd", + "chainCode": "cfb71883f01676f587d023cc53a35bc7f88f724b1f8c2892ac1275ac822a3edd", "hex": "0488b21e04ee7ab90c00000002cfb71883f01676f587d023cc53a35bc7f88f724b1f8c2892ac1275ac822a3edd02e8445082a72f29b75ca48748a914df60622a609cacfce8ed0e35804560741d29", "hexPriv": "0488ade404ee7ab90c00000002cfb71883f01676f587d023cc53a35bc7f88f724b1f8c2892ac1275ac822a3edd000f479245fb19a38a1954c5c7c0ebab2f9bdfd96a17563ef28a6a4b1a2a764ef4", "base58": "xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV", @@ -79,7 +79,7 @@ "address": "1LZiqrop2HGR4qrH1ULZPyBpU6AUP49Uam", "wif": "Kybw8izYevo5xMh1TK7aUr7jHFCxXS1zv8p3oqFz3o2zFbhRXHYs", "pubKey": "022a471424da5e657499d1ff51cb43c47481a03b1e77f951fe64cec9f5a48f7011", - "chaincode": "c783e67b921d2beb8f6b389cc646d7263b4145701dadd2161548a8b078e65e9e", + "chainCode": "c783e67b921d2beb8f6b389cc646d7263b4145701dadd2161548a8b078e65e9e", "hex": "0488b21e05d880d7d83b9aca00c783e67b921d2beb8f6b389cc646d7263b4145701dadd2161548a8b078e65e9e022a471424da5e657499d1ff51cb43c47481a03b1e77f951fe64cec9f5a48f7011", "hexPriv": "0488ade405d880d7d83b9aca00c783e67b921d2beb8f6b389cc646d7263b4145701dadd2161548a8b078e65e9e00471b76e389e528d6de6d816857e012c5455051cad6660850e58372a6c3e6e7c8", "base58": "xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy", @@ -95,7 +95,7 @@ "address": "1JEoxevbLLG8cVqeoGKQiAwoWbNYSUyYjg", "wif": "KyjXhyHF9wTphBkfpxjL8hkDXDUSbE3tKANT94kXSyh6vn6nKaoy", "pubKey": "03cbcaa9c98c877a26977d00825c956a238e8dddfbd322cce4f74b0b5bd6ace4a7", - "chaincode": "60499f801b896d83179a4374aeb7822aaeaceaa0db1f85ee3e904c4defbd9689", + "chainCode": "60499f801b896d83179a4374aeb7822aaeaceaa0db1f85ee3e904c4defbd9689", "hex": "0488b21e00000000000000000060499f801b896d83179a4374aeb7822aaeaceaa0db1f85ee3e904c4defbd968903cbcaa9c98c877a26977d00825c956a238e8dddfbd322cce4f74b0b5bd6ace4a7", "hexPriv": "0488ade400000000000000000060499f801b896d83179a4374aeb7822aaeaceaa0db1f85ee3e904c4defbd9689004b03d6fc340455b363f51020ad3ecca4f0850280cf436c70c727923f6db46c3e", "base58": "xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB", @@ -110,7 +110,7 @@ "address": "19EuDJdgfRkwCmRzbzVBHZWQG9QNWhftbZ", "wif": "L2ysLrR6KMSAtx7uPqmYpoTeiRzydXBattRXjXz5GDFPrdfPzKbj", "pubKey": "02fc9e5af0ac8d9b3cecfe2a888e2117ba3d089d8585886c9c826b6b22a98d12ea", - "chaincode": "f0909affaa7ee7abe5dd4e100598d4dc53cd709d5a5c2cac40e7412f232f7c9c", + "chainCode": "f0909affaa7ee7abe5dd4e100598d4dc53cd709d5a5c2cac40e7412f232f7c9c", "hex": "0488b21e01bd16bee500000000f0909affaa7ee7abe5dd4e100598d4dc53cd709d5a5c2cac40e7412f232f7c9c02fc9e5af0ac8d9b3cecfe2a888e2117ba3d089d8585886c9c826b6b22a98d12ea", "hexPriv": "0488ade401bd16bee500000000f0909affaa7ee7abe5dd4e100598d4dc53cd709d5a5c2cac40e7412f232f7c9c00abe74a98f6c7eabee0428f53798f0ab8aa1bd37873999041703c742f15ac7e1e", "base58": "xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH", @@ -124,7 +124,7 @@ "address": "1Lke9bXGhn5VPrBuXgN12uGUphrttUErmk", "wif": "L1m5VpbXmMp57P3knskwhoMTLdhAAaXiHvnGLMribbfwzVRpz2Sr", "pubKey": "03c01e7425647bdefa82b12d9bad5e3e6865bee0502694b94ca58b666abc0a5c3b", - "chaincode": "be17a268474a6bb9c61e1d720cf6215e2a88c5406c4aee7b38547f585c9a37d9", + "chainCode": "be17a268474a6bb9c61e1d720cf6215e2a88c5406c4aee7b38547f585c9a37d9", "hex": "0488b21e025a61ff8effffffffbe17a268474a6bb9c61e1d720cf6215e2a88c5406c4aee7b38547f585c9a37d903c01e7425647bdefa82b12d9bad5e3e6865bee0502694b94ca58b666abc0a5c3b", "hexPriv": "0488ade4025a61ff8effffffffbe17a268474a6bb9c61e1d720cf6215e2a88c5406c4aee7b38547f585c9a37d900877c779ad9687164e9c2f4f0f4ff0340814392330693ce95a58fe18fd52e6e93", "base58": "xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a", @@ -138,7 +138,7 @@ "address": "1BxrAr2pHpeBheusmd6fHDP2tSLAUa3qsW", "wif": "KzyzXnznxSv249b4KuNkBwowaN3akiNeEHy5FWoPCJpStZbEKXN2", "pubKey": "03a7d1d856deb74c508e05031f9895dab54626251b3806e16b4bd12e781a7df5b9", - "chaincode": "f366f48f1ea9f2d1d3fe958c95ca84ea18e4c4ddb9366c336c927eb246fb38cb", + "chainCode": "f366f48f1ea9f2d1d3fe958c95ca84ea18e4c4ddb9366c336c927eb246fb38cb", "hex": "0488b21e03d8ab493700000001f366f48f1ea9f2d1d3fe958c95ca84ea18e4c4ddb9366c336c927eb246fb38cb03a7d1d856deb74c508e05031f9895dab54626251b3806e16b4bd12e781a7df5b9", "hexPriv": "0488ade403d8ab493700000001f366f48f1ea9f2d1d3fe958c95ca84ea18e4c4ddb9366c336c927eb246fb38cb00704addf544a06e5ee4bea37098463c23613da32020d604506da8c0518e1da4b7", "base58": "xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon", @@ -152,7 +152,7 @@ "address": "15XVotxCAV7sRx1PSCkQNsGw3W9jT9A94R", "wif": "L5KhaMvPYRW1ZoFmRjUtxxPypQ94m6BcDrPhqArhggdaTbbAFJEF", "pubKey": "02d2b36900396c9282fa14628566582f206a5dd0bcc8d5e892611806cafb0301f0", - "chaincode": "637807030d55d01f9a0cb3a7839515d796bd07706386a6eddf06cc29a65a0e29", + "chainCode": "637807030d55d01f9a0cb3a7839515d796bd07706386a6eddf06cc29a65a0e29", "hex": "0488b21e0478412e3afffffffe637807030d55d01f9a0cb3a7839515d796bd07706386a6eddf06cc29a65a0e2902d2b36900396c9282fa14628566582f206a5dd0bcc8d5e892611806cafb0301f0", "hexPriv": "0488ade40478412e3afffffffe637807030d55d01f9a0cb3a7839515d796bd07706386a6eddf06cc29a65a0e2900f1c7c871a54a804afe328b4c83a1c33b8e5ff48f5087273f04efa83b247d6a2d", @@ -167,7 +167,7 @@ "address": "14UKfRV9ZPUp6ZC9PLhqbRtxdihW9em3xt", "wif": "L3WAYNAZPxx1fr7KCz7GN9nD5qMBnNiqEJNJMU1z9MMaannAt4aK", "pubKey": "024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c", - "chaincode": "9452b549be8cea3ecb7a84bec10dcfd94afe4d129ebfd3b3cb58eedf394ed271", + "chainCode": "9452b549be8cea3ecb7a84bec10dcfd94afe4d129ebfd3b3cb58eedf394ed271", "hex": "0488b21e0531a507b8000000029452b549be8cea3ecb7a84bec10dcfd94afe4d129ebfd3b3cb58eedf394ed271024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c", "hexPriv": "0488ade40531a507b8000000029452b549be8cea3ecb7a84bec10dcfd94afe4d129ebfd3b3cb58eedf394ed27100bb7d39bdb83ecf58f2fd82b6d918341cbef428661ef01ab97c28a4842125ac23", "base58": "xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt", diff --git a/test/hdwallet.js b/test/hdwallet.js index cebca2d..d39eac3 100644 --- a/test/hdwallet.js +++ b/test/hdwallet.js @@ -52,12 +52,12 @@ describe('HDWallet', function() { }) }) - describe('constructor & seed deserialization', function() { + describe('fromSeedBuffer', function() { var expectedPrivateKey = '0fd71c652e847ba7ea7956e3cf3fc0a0985871846b1b2c23b9c6a29a38cee860' - var seed = new Buffer('6372617a7920686f727365206261747465727920737461706c65', 'hex') + var seedHex = '6372617a7920686f727365206261747465727920737461706c65' - it('creates from binary seed', function() { - var hd = new HDWallet(seed) + it('creates from a binary seed', function() { + var hd = HDWallet.fromSeedBuffer(new Buffer(seedHex, 'hex')) assert.equal(hd.priv.D.toHex(), expectedPrivateKey) assert(hd.pub) @@ -65,7 +65,7 @@ describe('HDWallet', function() { describe('fromSeedHex', function() { it('creates from hex seed', function() { - var hd = HDWallet.fromSeedHex(seed.toString('hex')) + var hd = HDWallet.fromSeedHex(seedHex) assert.equal(hd.priv.D.toHex(), expectedPrivateKey) assert(hd.pub) @@ -80,9 +80,7 @@ describe('HDWallet', function() { assert.equal(hd.getAddress().toString(), v.address) assert.equal(hd.priv.toWIF(), v.wif) assert.equal(hd.pub.toHex(), v.pubKey) - assert.equal(b2h(hd.chaincode), v.chaincode) - assert.equal(hd.toHex(false), v.hex) - assert.equal(hd.toHex(true), v.hexPriv) + assert.equal(b2h(hd.chainCode), v.chainCode) assert.equal(hd.toBase58(false), v.base58) assert.equal(hd.toBase58(true), v.base58Priv) } @@ -128,28 +126,28 @@ describe('HDWallet', function() { }) it('ensure that a bitcoin wallet is the default', function() { - var wallet = new HDWallet(seed) + var hd = HDWallet.fromSeedBuffer(seed) - assert.equal(wallet.network, networks.bitcoin) + assert.equal(hd.network, networks.bitcoin) }) it('ensures that a bitcoin Wallet generates bitcoin addresses', function() { - var wallet = new HDWallet(seed) - var address = wallet.getAddress().toString() + var hd = HDWallet.fromSeedBuffer(seed, networks.bitcoin) + var address = hd.getAddress().toString() assert.equal(address, '17SnB9hyGwJPoKpLb9eVPHjsujyEuBpMAA') }) it('ensures that a testnet Wallet generates testnet addresses', function() { - var wallet = new HDWallet(seed, networks.testnet) - var address = wallet.getAddress().toString() + 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() { - new HDWallet(seed, {}) + HDWallet.fromSeedBuffer(seed, {}) }, /Unknown BIP32 constants for network/) }) })