From 44012b47b5c817fcebe566c4bfc841f1fb2b49bc Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Tue, 11 Mar 2014 22:41:20 +0800 Subject: [PATCH] change hd wallet constructor to take bytes original constructor -> fromSeedString, fromMasterHex -> fromSeedHex [#60] --- src/hdwallet.js | 11 +++++++---- test/hdwallet.js | 41 ++++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/hdwallet.js b/src/hdwallet.js index 89eb41b..43207a1 100644 --- a/src/hdwallet.js +++ b/src/hdwallet.js @@ -13,7 +13,7 @@ var Network = require('./network') var HDWallet = module.exports = function(seed, network) { if (seed === undefined) return - var seedWords = convert.bytesToWordArray(convert.stringToBytes(seed)) + var seedWords = convert.bytesToWordArray(seed) var I = convert.wordArrayToBytes(HmacSHA512(seedWords, 'Bitcoin seed')) this.chaincode = I.slice(32) this.network = network || 'mainnet' @@ -36,9 +36,12 @@ function arrayEqual(a, b) { HDWallet.getChecksum = base58.getChecksum; -HDWallet.fromMasterHex = function(hex, network) { - var bytes = convert.hexToBytes(hex) - return new HDWallet(convert.bytesToString(bytes), network) +HDWallet.fromSeedHex = function(hex, network) { + return new HDWallet(convert.hexToBytes(hex), network) +} + +HDWallet.fromSeedString = function(string, network) { + return new HDWallet(convert.stringToBytes(string), network) } HDWallet.fromBase58 = function(input) { diff --git a/test/hdwallet.js b/test/hdwallet.js index 85d2ae4..f637535 100644 --- a/test/hdwallet.js +++ b/test/hdwallet.js @@ -32,19 +32,46 @@ describe('HDWallet', function() { }) }) - describe('ctor', function() { - it('creates from seed', function() { - var seed = 'crazy horse battery staple' - , hd = new HDWallet(seed) + describe('constructor & seed deserialization', function() { + var expectedPrivKey, seed; - assert(hd.priv) + beforeEach(function(){ + expectedPrivKey = 'KwkW62Lzm4a7Eo5nPLezrVjWBGFh2KMfpyf4Swz9NmfsVaLoeXv9' + seed = [ + 99, 114, 97, 122, 121, 32, 104, 111, 114, 115, 101, 32, 98, + 97, 116, 116, 101, 114, 121, 32, 115, 116, 97, 112, 108, 101 + ] + }) + + it('creates from binary seed', function() { + var hd = new HDWallet(seed) + + assert.equal(hd.priv, expectedPrivKey) assert(hd.pub) }) + + describe('fromSeedHex', function() { + it('creates from hex seed', function() { + var hd = HDWallet.fromSeedHex(b2h(seed)) + + assert.equal(hd.priv, expectedPrivKey) + assert(hd.pub) + }) + }) + + describe('fromSeedString', function() { + it('creates from string seed', function() { + var hd = HDWallet.fromSeedString(convert.bytesToString(seed)) + + assert.equal(hd.priv, expectedPrivKey) + assert(hd.pub) + }) + }) }) describe('Test vectors', function() { it('Test vector 1', function() { - var hd = HDWallet.fromMasterHex('000102030405060708090a0b0c0d0e0f') + var hd = HDWallet.fromSeedHex('000102030405060708090a0b0c0d0e0f') // m assert.equal(b2h(hd.getIdentifier()), '3442193e1bb70916e914552172cd4e2dbc9df811') @@ -131,7 +158,7 @@ describe('HDWallet', function() { }) it('Test vector 2', function() { - var hd = HDWallet.fromMasterHex('fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542') + var hd = HDWallet.fromSeedHex('fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542') // m assert.equal(b2h(hd.getIdentifier()), 'bd16bee53961a47d6ad888e29545434a89bdfe95')