Browse Source

change hd wallet constructor to take bytes

original constructor -> fromSeedString, fromMasterHex -> fromSeedHex

[#60]
hk-custom-address
Wei Lu 11 years ago
parent
commit
44012b47b5
  1. 11
      src/hdwallet.js
  2. 41
      test/hdwallet.js

11
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) {

41
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')

Loading…
Cancel
Save