From ff70f661f7ed36298bb63edb724216db878e2c5a Mon Sep 17 00:00:00 2001 From: xnova Date: Thu, 27 Feb 2014 01:18:56 -0500 Subject: [PATCH 1/4] HDWallet passes along network type when it creates the master private key --- src/hdwallet.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hdwallet.js b/src/hdwallet.js index 00a02c0..e653b71 100644 --- a/src/hdwallet.js +++ b/src/hdwallet.js @@ -13,7 +13,8 @@ var HDWallet = module.exports = function(seed, network) { var I = Crypto.HMAC(Crypto.SHA512, seed, 'Bitcoin seed', { asBytes: true }) this.chaincode = I.slice(32) - this.priv = new ECKey(I.slice(0, 32).concat([1]), true) + this.priv = new ECKey(I.slice(0, 32).concat([1]), true, + network == 'Bitcoin' ? Address.address_types.prod : Address.address_types.testnet) this.pub = this.priv.getPub() this.network = network || 'Bitcoin' this.index = 0 From d447e6927976959980e371aa797d759ec8c68f15 Mon Sep 17 00:00:00 2001 From: xnova Date: Thu, 27 Feb 2014 03:46:28 -0500 Subject: [PATCH 2/4] more ECKey version specification in HDWallet --- src/hdwallet.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/hdwallet.js b/src/hdwallet.js index e653b71..d379436 100644 --- a/src/hdwallet.js +++ b/src/hdwallet.js @@ -13,8 +13,8 @@ var HDWallet = module.exports = function(seed, network) { var I = Crypto.HMAC(Crypto.SHA512, seed, 'Bitcoin seed', { asBytes: true }) this.chaincode = I.slice(32) - this.priv = new ECKey(I.slice(0, 32).concat([1]), true, - network == 'Bitcoin' ? Address.address_types.prod : Address.address_types.testnet) + this.keyVersion = network == 'Bitcoin' ? Address.address_types.prod : Address.address_types.testnet + this.priv = new ECKey(I.slice(0, 32).concat([1]), true, this.keyVersion) this.pub = this.priv.getPub() this.network = network || 'Bitcoin' this.index = 0 @@ -107,10 +107,10 @@ HDWallet.fromBytes = function(input) { // 33 bytes: the public key or private key data (0x02 + X or 0x03 + X for // public keys, 0x00 + k for private keys) if (type == 'private') { - hd.priv = new ECKey(input.slice(46, 78).concat([1]), true) + hd.priv = new ECKey(input.slice(46, 78).concat([1]), true, this.keyVersion) hd.pub = hd.priv.getPub() } else { - hd.pub = new ECPubKey(input.slice(45, 78), true) + hd.pub = new ECPubKey(input.slice(45, 78), true, this.keyVersion) } return hd @@ -214,10 +214,11 @@ HDWallet.prototype.derive = function(i) { // ki = IL + kpar (mod n). hd.priv = this.priv.add(new ECKey(IL.concat([1]))) hd.priv.compressed = true + hd.priv.version = this.keyVersion hd.pub = hd.priv.getPub() } else { // Ki = (IL + kpar)*G = IL*G + Kpar - hd.pub = this.pub.add(new ECKey(IL.concat([1])).getPub()) + hd.pub = this.pub.add(new ECKey(IL.concat([1]), true, this.keyVersion)) } // ci = IR. From 0310a890ba084008fc51172c5c4ecc7c454b43d7 Mon Sep 17 00:00:00 2001 From: xnova Date: Thu, 27 Feb 2014 03:48:20 -0500 Subject: [PATCH 3/4] didn't mean to remove getPub() --- src/hdwallet.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hdwallet.js b/src/hdwallet.js index d379436..f40582f 100644 --- a/src/hdwallet.js +++ b/src/hdwallet.js @@ -218,7 +218,7 @@ HDWallet.prototype.derive = function(i) { hd.pub = hd.priv.getPub() } else { // Ki = (IL + kpar)*G = IL*G + Kpar - hd.pub = this.pub.add(new ECKey(IL.concat([1]), true, this.keyVersion)) + hd.pub = this.pub.add(new ECKey(IL.concat([1]), true, this.keyVersion).getPub()) } // ci = IR. From 2ca02c47ce95c48aeacecf1532a29c1898b71a7d Mon Sep 17 00:00:00 2001 From: xnova Date: Thu, 27 Feb 2014 12:25:28 -0500 Subject: [PATCH 4/4] added testcases for new logic --- test/wallet.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/wallet.js b/test/wallet.js index 0f39d02..44f6164 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -1,4 +1,5 @@ var Wallet = require('../src/wallet.js') +var Address = require('../src/address.js') var assert = require('assert') describe('Wallet', function() { @@ -33,4 +34,20 @@ describe('Wallet', function() { assert.equal(wallet.derivationMethod, 'public') }) }) + + describe('networkType', function() { + it('ensures that a mainnet Wallet has mainnet child keys (pub and priv)', function() { + var w = Wallet("foobar", {network: "Bitcoin"}) + assert(w.getMasterKey().priv.version == Address.address_types['prod']) + w.generateAddress() + assert(w.getPrivateKey(0).priv.version == Address.address_types['prod']) + }) + + it('ensures that a testnet Wallet has testnet child keys (pub and priv)', function() { + var w = Wallet("foobar", {network: "BitcoinTest"}) + assert(w.getMasterKey().priv.version == Address.address_types['testnet']) + w.generateAddress() + assert(w.getPrivateKey(0).priv.version == Address.address_types['testnet']) + }) + }) })