diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 321faec7..71bb2516 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -207,17 +207,17 @@ Wallet.prototype.open = co(function* open() { * @param {Function} callback */ -Wallet.prototype.destroy = function destroy() { +Wallet.prototype.destroy = co(function* destroy() { + var unlock1 = yield this.writeLock.lock(); + var unlock2 = yield this.fundLock.lock(); try { this.db.unregister(this); this.master.destroy(); - } catch (e) { - this.emit('error', e); - return Promise.reject(e); + } finally { + unlock2(); + unlock1(); } - - return Promise.resolve(null); -}; +}); /** * Add a public account key to the wallet (multisig). @@ -354,10 +354,10 @@ Wallet.prototype._setPassphrase = co(function* setPassphrase(old, new_) { old = null; } - if (old) + if (old != null) yield this.master.decrypt(old); - if (new_) + if (new_ != null) yield this.master.encrypt(new_); this.start(); @@ -406,9 +406,16 @@ Wallet.prototype._retoken = co(function* retoken(passphrase) { * Lock the wallet, destroy decrypted key. */ -Wallet.prototype.lock = function lock() { - this.master.destroy(); -}; +Wallet.prototype.lock = co(function* lock() { + var unlock1 = yield this.writeLock.lock(); + var unlock2 = yield this.fundLock.lock(); + try { + this.master.destroy(); + } finally { + unlock2(); + unlock1(); + } +}); /** * Unlock the key for `timeout` milliseconds. diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 2fee45ae..ba54de54 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -136,7 +136,7 @@ function WalletDB(options) { // We need one read lock for `get` and `create`. // It will hold locks specific to wallet ids. this.readLock = new bcoin.locker.mapped(this); - this.writeLock = new bcoin.locker.mapped(this); + this.writeLock = new bcoin.locker(this); this.txLock = new bcoin.locker(this); this.walletCache = new bcoin.lru(10000); @@ -530,13 +530,11 @@ WalletDB.prototype.auth = co(function* auth(wid, token) { */ WalletDB.prototype.create = co(function* create(options) { - var unlock; + var unlock = yield this.writeLock.lock(); if (!options) options = {}; - unlock = yield this.writeLock.lock(options.id); - try { return yield this._create(options); } finally {