diff --git a/lib/wallet/masterkey.js b/lib/wallet/masterkey.js index 94489d04..e9a13d74 100644 --- a/lib/wallet/masterkey.js +++ b/lib/wallet/masterkey.js @@ -279,7 +279,7 @@ MasterKey.prototype.decipher = function decipher(data, iv) { * the timer if there is one. */ -MasterKey.prototype.destroy = function destroy() { +MasterKey.prototype.lock = function lock() { if (!this.encrypted) { assert(this.timer == null); assert(this.key); @@ -299,6 +299,15 @@ MasterKey.prototype.destroy = function destroy() { } }; +/** + * Destroy the key permanently. + */ + +MasterKey.prototype.destroy = function destroy() { + this.lock(); + this.locker.destroy(); +}; + /** * Decrypt the key permanently. * @param {Buffer|String} passphrase - Zero this yourself. @@ -332,7 +341,7 @@ MasterKey.prototype._decrypt = co(function* decrypt(passphrase) { if (!passphrase) return; - this.destroy(); + this.lock(); key = yield this.derive(passphrase); data = crypto.decipher(this.ciphertext, key, this.iv); diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 06414595..148ee504 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -81,7 +81,7 @@ function Wallet(db, options) { this.accountDepth = 0; this.token = constants.ZERO_HASH; this.tokenDepth = 0; - this.master = null; + this.master = new MasterKey(); this.txdb = new TXDB(this); this.account = null; @@ -99,27 +99,27 @@ utils.inherits(Wallet, EventEmitter); */ Wallet.prototype.fromOptions = function fromOptions(options) { - var master = options.master; + var key = options.master; var id, token; - if (!MasterKey.isMasterKey(master)) { - if (!master) - master = HD.fromMnemonic(null, this.network); + if (MasterKey.isMasterKey(key)) { + this.master.fromOptions(key); + } else { + if (!key) + key = HD.fromMnemonic(null, this.network); - if (HD.isExtended(master)) - master = HD.fromBase58(master); + if (HD.isExtended(key)) + key = HD.fromBase58(key); - assert(HD.isPrivate(master), + assert(HD.isPrivate(key), 'Must create wallet with hd private key.'); - assert(master.network === this.network, + assert(key.network === this.network, 'Network mismatch for master key.'); - master = MasterKey.fromKey(master); + this.master.fromKey(key); } - this.master = master; - if (options.wid != null) { assert(utils.isNumber(options.wid)); this.wid = options.wid; @@ -589,7 +589,7 @@ Wallet.prototype.lock = co(function* lock() { var unlock1 = yield this.writeLock.lock(); var unlock2 = yield this.fundLock.lock(); try { - this.master.destroy(); + this.master.lock(); } finally { unlock2(); unlock1();