From 49c2fc06b5d9ca182ab689a3f0ea88a30d3507e4 Mon Sep 17 00:00:00 2001 From: xnova Date: Wed, 26 Feb 2014 19:56:37 -0500 Subject: [PATCH 1/2] allow Wallet to work with testnet --- src/wallet.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wallet.js b/src/wallet.js index 5710f25..eef851e 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -13,8 +13,8 @@ var HDWallet = require('./hdwallet.js') var SecureRandom = require('./jsbn/rng'); var rng = new SecureRandom(); -var Wallet = function (seed) { - if (!(this instanceof Wallet)) { return new Wallet(seed); } +var Wallet = function (seed, network) { + if (!(this instanceof Wallet)) { return new Wallet(seed, network); } // Stored in a closure to make accidental serialization less likely var keys = []; @@ -28,16 +28,16 @@ var Wallet = function (seed) { this.outputs = {}; // Make a new master key - this.newMasterKey = function(seed) { + this.newMasterKey = function(seed, network) { if (!seed) { var seedBytes = new Array(32); rng.nextBytes(seedBytes); seed = conv.bytesToString(seedBytes) } - masterkey = new HDWallet(seed); + masterkey = new HDWallet(seed, network); keys = [] } - this.newMasterKey(seed) + this.newMasterKey(seed, network) // Add a new address this.generateAddress = function() { From 933474a973672eef686fd1207f8870f728531bb8 Mon Sep 17 00:00:00 2001 From: xnova Date: Wed, 26 Feb 2014 20:13:51 -0500 Subject: [PATCH 2/2] added ability to specify child key derivation method to the Wallet constructor --- src/wallet.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/wallet.js b/src/wallet.js index eef851e..e738f9b 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -2,6 +2,7 @@ var Script = require('./script'); var ECKey = require('./eckey').ECKey; var conv = require('./convert'); var util = require('./util'); +var assert = require('assert'); var BigInteger = require('./jsbn/jsbn'); @@ -13,8 +14,8 @@ var HDWallet = require('./hdwallet.js') var SecureRandom = require('./jsbn/rng'); var rng = new SecureRandom(); -var Wallet = function (seed, network) { - if (!(this instanceof Wallet)) { return new Wallet(seed, network); } +var Wallet = function (seed, network, derivationMethod) { + if (!(this instanceof Wallet)) { return new Wallet(seed, network, derivationMethod); } // Stored in a closure to make accidental serialization less likely var keys = []; @@ -27,6 +28,13 @@ var Wallet = function (seed, network) { // Transaction output data this.outputs = {}; + // HD first-level child derivation method (i.e. public or private child key derivation) + // NB: if not specified, defaults to private child derivation + // Also see https://bitcointalk.org/index.php?topic=405179.msg4415254#msg4415254 + this.derivationMethod = derivationMethod || 'private'; + assert(this.derivationMethod == 'public' || this.derivationMethod == 'private', + "derivationMethod must be either 'public' or 'private'"); + // Make a new master key this.newMasterKey = function(seed, network) { if (!seed) { @@ -41,7 +49,10 @@ var Wallet = function (seed, network) { // Add a new address this.generateAddress = function() { - keys.push(masterkey.derive(keys.length)) + if(this.derivationMethod == 'private') + keys.push(masterkey.derivePrivate(keys.length)); + else + keys.push(masterkey.derive(keys.length)); this.addresses.push(keys[keys.length-1].getBitcoinAddress().toString()) return this.addresses[this.addresses.length - 1] }