diff --git a/lib/wallet/account.js b/lib/wallet/account.js index b3ce19e4..4db92269 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -245,6 +245,9 @@ Account.prototype.open = function open() { if (!this.initialized) return Promise.resolve(null); + if (this.receive) + return Promise.resolve(null); + this.receive = this.deriveReceive(this.receiveDepth - 1); this.change = this.deriveChange(this.changeDepth - 1); diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index cb32ca19..317d95d6 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -64,6 +64,7 @@ function Wallet(db, options) { this.db = db; this.network = db.network; this.logger = db.logger; + this.readLock = new Locker.Mapped(); this.writeLock = new Locker(); this.fundLock = new Locker(); @@ -745,14 +746,35 @@ Wallet.prototype.getAddressHashes = function getAddressHashes() { */ Wallet.prototype.getAccount = co(function* getAccount(acct) { - var account; + var index, unlock; if (this.account) { if (acct === 0 || acct === 'default') return this.account; } - account = yield this.db.getAccount(this.wid, acct); + index = yield this.db.getAccountIndex(this.wid, acct); + + if (index === -1) + return; + + unlock = yield this.readLock.lock(index); + + try { + return yield this._getAccount(index); + } finally { + unlock(); + } +}); + +/** + * Retrieve an account from the database without a lock. + * @param {Number} index + * @returns {Promise} - Returns {@link Account}. + */ + +Wallet.prototype._getAccount = co(function* _getAccount(index) { + var account = yield this.db.getAccount(this.wid, index); if (!account) return;