From 2e5df5514a0311006a2f50a54cd6a54388eee597 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 31 May 2016 06:29:25 -0700 Subject: [PATCH] change passphrase. --- lib/bcoin/wallet.js | 59 ++++++++++++++++++++++++++++++++++++++++----- test/wallet-test.js | 31 ++++++++++++------------ 2 files changed, 69 insertions(+), 21 deletions(-) diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 98ef90c2..97b565be 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -179,9 +179,6 @@ Wallet.prototype.init = function init(callback) { self.account = account; - if (Buffer.isBuffer(options.passphrase)) - options.passphrase.fill(0); - options.passphrase = null; return callback(); @@ -264,6 +261,39 @@ Wallet.prototype.removeKey = function removeKey(account, key, callback) { }); }; +/** + * Change or set master key's passphrase. + * @param {(String|Buffer)?} old + * @param {(String|Buffer)?} new_ + * @param {Function} callback + */ + +Wallet.prototype.setPassphrase = function setPassphrase(old, new_, callback) { + if (typeof new_ === 'function') { + callback = new_; + new_ = old; + old = null; + } + + if (old) { + try { + this.master.decrypt(old); + } catch (e) { + return callback(e); + } + } + + if (new_) { + try { + this.master.encrypt(new_); + } catch (e) { + return callback(e); + } + } + + return this.save(callback); +}; + /** * Generate the wallet ID if none was passed in. * It is represented as `m/44'` (public) hashed @@ -300,7 +330,7 @@ Wallet.prototype.createAccount = function createAccount(options, callback) { var master, key; try { - master = this.master.decrypt(options.passphrase); + master = this.master.toKey(options.passphrase); } catch (e) { return callback(e); } @@ -977,7 +1007,7 @@ Wallet.prototype.sign = function sign(tx, options, callback) { return callback(err); try { - master = self.master.decrypt(options.passphrase); + master = self.master.toKey(options.passphrase); } catch (e) { return callback(null, 0); } @@ -2335,7 +2365,7 @@ function MasterKey(options) { this.key = options.key || null; } -MasterKey.prototype.decrypt = function decrypt(passphrase) { +MasterKey.prototype.toKey = function toKey(passphrase) { var xprivkey; if (this.key) @@ -2351,6 +2381,23 @@ MasterKey.prototype.decrypt = function decrypt(passphrase) { return bcoin.hd.PrivateKey.fromRaw(xprivkey); }; +MasterKey.prototype.decrypt = function decrypt(passphrase) { + if (!this.encrypted) + return; + + assert(passphrase, 'Passphrase is required.'); + + this.encrypted = false; + this.xprivkey = utils.decrypt(this.xprivkey, passphrase); + + if (this.phrase) { + this.phrase = utils.decrypt(this.phrase, passphrase); + this.passphrase = utils.decrypt(this.passphrase, passphrase); + } + + this.key = this.toKey(); +}; + MasterKey.prototype.encrypt = function encrypt(passphrase) { if (this.encrypted) return; diff --git a/test/wallet-test.js b/test/wallet-test.js index 80c52da1..7f588d12 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -327,25 +327,26 @@ describe('Wallet', function() { w1.fill(t2, { rate: 10000, round: true }, function(err) { assert.ifError(err); w1.sign(t2, function(err) { - assert.ifError(err); - assert(t2.verify()); + assert.ifError(err); - assert.equal(t2.getInputValue(), 16380); - // If change < dust and is added to outputs: - // assert.equal(t2.getOutputValue(), 6380); - // If change > dust and is added to fee: - assert.equal(t2.getOutputValue(), 5460); - assert.equal(t2.getFee(), 10920); + assert(t2.verify()); - // Create new transaction - var t3 = bcoin.mtx().addOutput(w2, 15000); - w1.fill(t3, { rate: 10000, round: true }, function(err) { - assert(err); - assert.equal(err.requiredFunds, 25000); - cb(); + assert.equal(t2.getInputValue(), 16380); + // If change < dust and is added to outputs: + // assert.equal(t2.getOutputValue(), 6380); + // If change > dust and is added to fee: + assert.equal(t2.getOutputValue(), 5460); + assert.equal(t2.getFee(), 10920); + + // Create new transaction + var t3 = bcoin.mtx().addOutput(w2, 15000); + w1.fill(t3, { rate: 10000, round: true }, function(err) { + assert(err); + assert.equal(err.requiredFunds, 25000); + cb(); + }); }); }); - }); }); }); });