diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index d4bf19aa..cfa96d17 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -787,11 +787,7 @@ Wallet.prototype.getAddressHashes = function getAddressHashes(acct) { */ Wallet.prototype.getAccountHashes = co(function* getAccountHashes(acct) { - var index = yield this.ensureIndex(acct); - - if (index == null) - throw new Error('Account not provided.'); - + var index = yield this.ensureIndex(acct, true); return yield this.db.getAccountHashes(this.wid, index); }); @@ -891,12 +887,17 @@ Wallet.prototype.getAccountIndex = co(function* getAccountIndex(name) { */ Wallet.prototype.getAccountName = co(function* getAccountName(index) { - var account = yield this.getAccount(index); + var account; - if (!account) - return null; + if (typeof index === 'string') + return index; - return account.name; + account = this.accountCache.get(index); + + if (account) + return account.name; + + return yield this.db.getAccountName(this.wid, index); }); /** @@ -1072,6 +1073,28 @@ Wallet.prototype.hasAddress = co(function* hasAddress(address) { */ Wallet.prototype.getPath = co(function* getPath(address) { + var path = yield this.readPath(address); + + if (!path) + return; + + path.name = yield this.getAccountName(path.account); + + assert(path.name); + + this.pathCache.set(path.hash, path); + + return path; +}); + +/** + * Get path by address hash (without account name). + * @private + * @param {Address|Hash} address + * @returns {Promise} - Returns {@link Path}. + */ + +Wallet.prototype.readPath = co(function* readPath(address) { var hash = Address.getHash(address, 'hex'); var path; @@ -1089,11 +1112,6 @@ Wallet.prototype.getPath = co(function* getPath(address) { return; path.id = this.id; - path.name = yield this.getAccountName(path.account); - - assert(path.name); - - this.pathCache.set(hash, path); return path; }); @@ -1153,16 +1171,25 @@ Wallet.prototype.getPaths = co(function* getPaths(acct) { */ Wallet.prototype.getAccountPaths = co(function* getAccountPaths(acct) { - var index = yield this.ensureIndex(acct); + var index = yield this.ensureIndex(acct, true); var hashes = yield this.getAccountHashes(index); + var name = yield this.getAccountName(acct); var result = []; var i, hash, path; + assert(name); + for (i = 0; i < hashes.length; i++) { hash = hashes[i]; - path = yield this.getPath(hash); + path = yield this.readPath(hash); + assert(path); assert(path.account === index); + + path.name = name; + + this.pathCache.set(path.hash, path); + result.push(path); } @@ -2228,11 +2255,14 @@ Wallet.prototype.getLast = co(function* getLast(acct, limit) { * @returns {Promise} */ -Wallet.prototype.ensureIndex = co(function* ensureIndex(acct) { +Wallet.prototype.ensureIndex = co(function* ensureIndex(acct, enforce) { var index; - if (acct == null) + if (acct == null) { + if (enforce) + throw new Error('No account provided.'); return null; + } index = yield this.getAccountIndex(acct); diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index f3ab253d..e8bd982f 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -1071,7 +1071,7 @@ WalletDB.prototype.getAccounts = co(function* getAccounts(wid) { /** * Lookup the corresponding account name's index. * @param {WalletID} wid - * @param {String|Number} name - Account name/index. + * @param {String} name - Account name/index. * @returns {Promise} - Returns Number. */ @@ -1084,6 +1084,22 @@ WalletDB.prototype.getAccountIndex = co(function* getAccountIndex(wid, name) { return index.readUInt32LE(0, true); }); +/** + * Lookup the corresponding account index's name. + * @param {WalletID} wid + * @param {Number} index + * @returns {Promise} - Returns Number. + */ + +WalletDB.prototype.getAccountName = co(function* getAccountName(wid, index) { + var name = yield this.db.get(layout.n(wid, index)); + + if (!name) + return; + + return name.toString('ascii'); +}); + /** * Save an account to the database. * @param {Account} account