From 933474a973672eef686fd1207f8870f728531bb8 Mon Sep 17 00:00:00 2001 From: xnova Date: Wed, 26 Feb 2014 20:13:51 -0500 Subject: [PATCH] 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] }