From f5625e76e1359804e016ae54e3931aec7458b21b Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 3 Oct 2016 08:30:02 -0700 Subject: [PATCH] wallet: refactor variable names. --- lib/wallet/account.js | 1 - lib/wallet/wallet.js | 227 +++++++++++++++++++++-------------------- lib/wallet/walletdb.js | 12 +-- 3 files changed, 124 insertions(+), 116 deletions(-) diff --git a/lib/wallet/account.js b/lib/wallet/account.js index ea64d616..44facf08 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -6,7 +6,6 @@ 'use strict'; -var Network = require('../protocol/network'); var utils = require('../utils/utils'); var co = require('../utils/co'); var assert = require('assert'); diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index d65cc256..f71880dd 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -235,14 +235,15 @@ Wallet.prototype.destroy = co(function* destroy() { /** * Add a public account key to the wallet (multisig). * Saves the key in the wallet database. + * @param {(Number|String)} acct * @param {HDPublicKey} key * @returns {Promise} */ -Wallet.prototype.addKey = co(function* addKey(account, key) { +Wallet.prototype.addKey = co(function* addKey(acct, key) { var unlock = yield this.writeLock.lock(); try { - return yield this._addKey(account, key); + return yield this._addKey(acct, key); } finally { unlock(); } @@ -251,22 +252,23 @@ Wallet.prototype.addKey = co(function* addKey(account, key) { /** * Add a public account key to the wallet without a lock. * @private + * @param {(Number|String)} acct * @param {HDPublicKey} key * @returns {Promise} */ -Wallet.prototype._addKey = co(function* addKey(account, key) { - var result; +Wallet.prototype._addKey = co(function* addKey(acct, key) { + var account, result; if (!key) { - key = account; - account = null; + key = acct; + acct = null; } - if (account == null) - account = 0; + if (acct == null) + acct = 0; - account = yield this.getAccount(account); + account = yield this.getAccount(acct); if (!account) throw new Error('Account not found.'); @@ -287,14 +289,15 @@ Wallet.prototype._addKey = co(function* addKey(account, key) { /** * Remove a public account key from the wallet (multisig). + * @param {(Number|String)} acct * @param {HDPublicKey} key * @returns {Promise} */ -Wallet.prototype.removeKey = co(function* removeKey(account, key) { +Wallet.prototype.removeKey = co(function* removeKey(acct, key) { var unlock = yield this.writeLock.lock(); try { - return yield this._removeKey(account, key); + return yield this._removeKey(acct, key); } finally { unlock(); } @@ -303,22 +306,23 @@ Wallet.prototype.removeKey = co(function* removeKey(account, key) { /** * Remove a public account key from the wallet (multisig). * @private + * @param {(Number|String)} acct * @param {HDPublicKey} key * @returns {Promise} */ -Wallet.prototype._removeKey = co(function* removeKey(account, key) { - var result; +Wallet.prototype._removeKey = co(function* removeKey(acct, key) { + var account, result; if (!key) { - key = account; - account = null; + key = acct; + acct = null; } - if (account == null) - account = 0; + if (acct == null) + acct = 0; - account = yield this.getAccount(account); + account = yield this.getAccount(acct); if (!account) throw new Error('Account not found.'); @@ -501,15 +505,15 @@ Wallet.prototype.rename = co(function* rename(id) { /** * Rename account. - * @param {(String|Number)?} account + * @param {(String|Number)?} acct * @param {String} name * @returns {Promise} */ -Wallet.prototype.renameAccount = co(function* renameAccount(account, name) { +Wallet.prototype.renameAccount = co(function* renameAccount(acct, name) { var unlock = yield this.writeLock.lock(); try { - return yield this._renameAccount(account, name); + return yield this._renameAccount(acct, name); } finally { unlock(); } @@ -518,15 +522,17 @@ Wallet.prototype.renameAccount = co(function* renameAccount(account, name) { /** * Rename account without a lock. * @private - * @param {(String|Number)?} account + * @param {(String|Number)?} acct * @param {String} name * @returns {Promise} */ -Wallet.prototype._renameAccount = co(function* _renameAccount(account, name) { +Wallet.prototype._renameAccount = co(function* _renameAccount(acct, name) { + var account; + assert(utils.isName(name), 'Bad account name.'); - account = yield this.getAccount(account); + account = yield this.getAccount(acct); if (!account) throw new Error('Account not found.'); @@ -700,16 +706,16 @@ Wallet.prototype._createAccount = co(function* createAccount(options) { */ Wallet.prototype.ensureAccount = co(function* ensureAccount(options) { - var account = options.account; + var acct = options.account; var exists; if (typeof options.name === 'string') - account = options.name; + acct = options.name; - exists = yield this.hasAccount(account); + exists = yield this.hasAccount(acct); if (exists) - return yield this.getAccount(account); + return yield this.getAccount(acct); return this.createAccount(options); }); @@ -734,17 +740,19 @@ Wallet.prototype.getAddressHashes = function getAddressHashes() { /** * Retrieve an account from the database. - * @param {Number|String} account + * @param {Number|String} acct * @returns {Promise} - Returns {@link Account}. */ -Wallet.prototype.getAccount = co(function* getAccount(account) { +Wallet.prototype.getAccount = co(function* getAccount(acct) { + var account; + if (this.account) { - if (account === 0 || account === 'default') + if (acct === 0 || acct === 'default') return this.account; } - account = yield this.db.getAccount(this.wid, account); + account = yield this.db.getAccount(this.wid, acct); if (!account) return; @@ -758,55 +766,55 @@ Wallet.prototype.getAccount = co(function* getAccount(account) { /** * Test whether an account exists. - * @param {Number|String} account + * @param {Number|String} acct * @returns {Promise} - Returns {@link Boolean}. */ -Wallet.prototype.hasAccount = function hasAccount(account) { - return this.db.hasAccount(this.wid, account); +Wallet.prototype.hasAccount = function hasAccount(acct) { + return this.db.hasAccount(this.wid, acct); }; /** * Create a new receiving address (increments receiveDepth). - * @param {(Number|String)?} account + * @param {(Number|String)?} acct * @returns {Promise} - Returns {@link WalletKey}. */ -Wallet.prototype.createReceive = function createReceive(account) { - return this.createKey(account, 0); +Wallet.prototype.createReceive = function createReceive(acct) { + return this.createKey(acct, 0); }; /** * Create a new change address (increments receiveDepth). - * @param {(Number|String)?} account + * @param {(Number|String)?} acct * @returns {Promise} - Returns {@link WalletKey}. */ -Wallet.prototype.createChange = function createChange(account) { - return this.createKey(account, 1); +Wallet.prototype.createChange = function createChange(acct) { + return this.createKey(acct, 1); }; /** * Create a new nested address (increments receiveDepth). - * @param {(Number|String)?} account + * @param {(Number|String)?} acct * @returns {Promise} - Returns {@link WalletKey}. */ -Wallet.prototype.createNested = function createNested(account) { - return this.createKey(account, 2); +Wallet.prototype.createNested = function createNested(acct) { + return this.createKey(acct, 2); }; /** * Create a new address (increments depth). - * @param {(Number|String)?} account + * @param {(Number|String)?} acct * @param {Number} branch * @returns {Promise} - Returns {@link WalletKey}. */ -Wallet.prototype.createKey = co(function* createKey(account, branch) { +Wallet.prototype.createKey = co(function* createKey(acct, branch) { var unlock = yield this.writeLock.lock(); try { - return yield this._createKey(account, branch); + return yield this._createKey(acct, branch); } finally { unlock(); } @@ -815,23 +823,23 @@ Wallet.prototype.createKey = co(function* createKey(account, branch) { /** * Create a new address (increments depth) without a lock. * @private - * @param {(Number|String)?} account + * @param {(Number|String)?} acct * @param {Number} branche * @returns {Promise} - Returns {@link WalletKey}. */ -Wallet.prototype._createKey = co(function* createKey(account, branch) { - var result; +Wallet.prototype._createKey = co(function* createKey(acct, branch) { + var account, result; if (branch == null) { - branch = account; - account = null; + branch = acct; + acct = null; } - if (account == null) - account = 0; + if (acct == null) + acct = 0; - account = yield this.getAccount(account); + account = yield this.getAccount(acct); if (!account) throw new Error('Account not found.'); @@ -925,15 +933,15 @@ Wallet.prototype.getPath = co(function* getPath(address) { /** * Get all wallet paths. - * @param {(String|Number)?} account + * @param {(String|Number)?} acct * @returns {Promise} - Returns {@link Path}. */ -Wallet.prototype.getPaths = co(function* getPaths(account) { +Wallet.prototype.getPaths = co(function* getPaths(acct) { var out = []; - var i, paths, path; + var i, account, paths, path; - account = yield this._getIndex(account); + account = yield this._getIndex(acct); paths = yield this.db.getWalletPaths(this.wid); for (i = 0; i < paths.length; i++) { @@ -950,16 +958,16 @@ Wallet.prototype.getPaths = co(function* getPaths(account) { /** * Import a keyring (will not exist on derivation chain). * Rescanning must be invoked manually. - * @param {(String|Number)?} account + * @param {(String|Number)?} acct * @param {WalletKey} ring * @param {(String|Buffer)?} passphrase * @returns {Promise} */ -Wallet.prototype.importKey = co(function* importKey(account, ring, passphrase) { +Wallet.prototype.importKey = co(function* importKey(acct, ring, passphrase) { var unlock = yield this.writeLock.lock(); try { - return yield this._importKey(account, ring, passphrase); + return yield this._importKey(acct, ring, passphrase); } finally { unlock(); } @@ -968,23 +976,23 @@ Wallet.prototype.importKey = co(function* importKey(account, ring, passphrase) { /** * Import a keyring (will not exist on derivation chain) without a lock. * @private - * @param {(String|Number)?} account + * @param {(String|Number)?} acct * @param {WalletKey} ring * @param {(String|Buffer)?} passphrase * @returns {Promise} */ -Wallet.prototype._importKey = co(function* importKey(account, ring, passphrase) { - var exists, path; +Wallet.prototype._importKey = co(function* importKey(acct, ring, passphrase) { + var account, exists, path; - if (account && typeof account === 'object') { + if (acct && typeof acct === 'object') { passphrase = ring; - ring = account; - account = null; + ring = acct; + acct = null; } - if (account == null) - account = 0; + if (acct == null) + acct = 0; if (!this.watchOnly) { if (!ring.privateKey) @@ -999,7 +1007,7 @@ Wallet.prototype._importKey = co(function* importKey(account, ring, passphrase) if (exists) throw new Error('Key already exists.'); - account = yield this.getAccount(account); + account = yield this.getAccount(acct); if (!account) throw new Error('Account not found.'); @@ -1033,16 +1041,16 @@ Wallet.prototype._importKey = co(function* importKey(account, ring, passphrase) /** * Import a keyring (will not exist on derivation chain). * Rescanning must be invoked manually. - * @param {(String|Number)?} account + * @param {(String|Number)?} acct * @param {WalletKey} ring * @param {(String|Buffer)?} passphrase * @returns {Promise} */ -Wallet.prototype.importAddress = co(function* importAddress(account, address) { +Wallet.prototype.importAddress = co(function* importAddress(acct, address) { var unlock = yield this.writeLock.lock(); try { - return yield this._importAddress(account, address); + return yield this._importAddress(acct, address); } finally { unlock(); } @@ -1051,22 +1059,22 @@ Wallet.prototype.importAddress = co(function* importAddress(account, address) { /** * Import a keyring (will not exist on derivation chain) without a lock. * @private - * @param {(String|Number)?} account + * @param {(String|Number)?} acct * @param {WalletKey} ring * @param {(String|Buffer)?} passphrase * @returns {Promise} */ -Wallet.prototype._importAddress = co(function* importAddress(account, address) { - var exists, path; +Wallet.prototype._importAddress = co(function* importAddress(acct, address) { + var account, exists, path; if (!address) { - address = account; - account = null; + address = acct; + acct = null; } - if (account == null) - account = 0; + if (acct == null) + acct = 0; if (!this.watchOnly) throw new Error('Cannot import address into non watch-only wallet.'); @@ -1076,7 +1084,7 @@ Wallet.prototype._importAddress = co(function* importAddress(account, address) { if (exists) throw new Error('Address already exists.'); - account = yield this.getAccount(account); + account = yield this.getAccount(acct); if (!account) throw new Error('Account not found.'); @@ -1696,12 +1704,12 @@ Wallet.prototype.addTX = function addTX(tx) { /** * Get all transactions in transaction history (accesses db). - * @param {(String|Number)?} account + * @param {(String|Number)?} acct * @returns {Promise} - Returns {@link TX}[]. */ -Wallet.prototype.getHistory = co(function* getHistory(account) { - account = yield this._getIndex(account); +Wallet.prototype.getHistory = co(function* getHistory(acct) { + var account = yield this._getIndex(acct); return this.txdb.getHistory(account); }); @@ -1711,72 +1719,73 @@ Wallet.prototype.getHistory = co(function* getHistory(account) { * @returns {Promise} - Returns {@link Coin}[]. */ -Wallet.prototype.getCoins = co(function* getCoins(account) { - account = yield this._getIndex(account); +Wallet.prototype.getCoins = co(function* getCoins(acct) { + var account = yield this._getIndex(acct); return yield this.txdb.getCoins(account); }); /** * Get all pending/unconfirmed transactions (accesses db). - * @param {(String|Number)?} account + * @param {(String|Number)?} acct * @returns {Promise} - Returns {@link TX}[]. */ -Wallet.prototype.getUnconfirmed = co(function* getUnconfirmed(account) { - account = yield this._getIndex(account); +Wallet.prototype.getUnconfirmed = co(function* getUnconfirmed(acct) { + var account = yield this._getIndex(acct); return yield this.txdb.getUnconfirmed(account); }); /** * Get wallet balance (accesses db). - * @param {(String|Number)?} account + * @param {(String|Number)?} acct * @returns {Promise} - Returns {@link Balance}. */ -Wallet.prototype.getBalance = co(function* getBalance(account) { - account = yield this._getIndex(account); +Wallet.prototype.getBalance = co(function* getBalance(acct) { + var account = yield this._getIndex(acct); return yield this.txdb.getBalance(account); }); /** * Get a range of transactions between two timestamps (accesses db). - * @param {(String|Number)?} account + * @param {(String|Number)?} acct * @param {Object} options * @param {Number} options.start * @param {Number} options.end * @returns {Promise} - Returns {@link TX}[]. */ -Wallet.prototype.getRange = co(function* getRange(account, options) { - if (account && typeof account === 'object') { - options = account; - account = null; +Wallet.prototype.getRange = co(function* getRange(acct, options) { + var account; + if (acct && typeof acct === 'object') { + options = acct; + acct = null; } - account = yield this._getIndex(account); + account = yield this._getIndex(acct); return yield this.txdb.getRange(account, options); }); /** * Get the last N transactions (accesses db). - * @param {(String|Number)?} account + * @param {(String|Number)?} acct * @param {Number} limit * @returns {Promise} - Returns {@link TX}[]. */ -Wallet.prototype.getLast = co(function* getLast(account, limit) { - account = yield this._getIndex(account); +Wallet.prototype.getLast = co(function* getLast(acct, limit) { + var account = yield this._getIndex(acct); return yield this.txdb.getLast(account, limit); }); /** * Zap stale TXs from wallet (accesses db). - * @param {(Number|String)?} account + * @param {(Number|String)?} acct * @param {Number} age - Age threshold (unix time, default=72 hours). * @returns {Promise} */ -Wallet.prototype.zap = co(function* zap(account, age) { - account = yield this._getIndex(account); +Wallet.prototype.zap = co(function* zap(acct, age) { + var account = yield this._getIndex(acct); return yield this.txdb.zap(account, age); }); @@ -1793,18 +1802,18 @@ Wallet.prototype.abandon = function abandon(hash) { /** * Resolve account index. * @private - * @param {(Number|String)?} account + * @param {(Number|String)?} acct * @param {Function} errback - Returns [Error]. * @returns {Promise} */ -Wallet.prototype._getIndex = co(function* _getIndex(account) { +Wallet.prototype._getIndex = co(function* _getIndex(acct) { var index; - if (account == null) + if (acct == null) return null; - index = yield this.db.getAccountIndex(this.wid, account); + index = yield this.db.getAccountIndex(this.wid, acct); if (index === -1) throw new Error('Account not found.'); diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 4e6a79f3..2e5d527a 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -721,12 +721,12 @@ WalletDB.prototype.ensure = co(function* ensure(options) { /** * Get an account from the database. * @param {WalletID} wid - * @param {String|Number} name - Account name/index. + * @param {String|Number} acct - Account name/index. * @returns {Promise} - Returns {@link Wallet}. */ -WalletDB.prototype.getAccount = co(function* getAccount(wid, name) { - var index = yield this.getAccountIndex(wid, name); +WalletDB.prototype.getAccount = co(function* getAccount(wid, acct) { + var index = yield this.getAccountIndex(wid, acct); var account; if (index === -1) @@ -906,17 +906,17 @@ WalletDB.prototype.createAccount = co(function* createAccount(options) { /** * Test for the existence of an account. * @param {WalletID} wid - * @param {String|Number} account + * @param {String|Number} acct * @returns {Promise} - Returns Boolean. */ -WalletDB.prototype.hasAccount = co(function* hasAccount(wid, account) { +WalletDB.prototype.hasAccount = co(function* hasAccount(wid, acct) { var index, key; if (!wid) return false; - index = yield this.getAccountIndex(wid, account); + index = yield this.getAccountIndex(wid, acct); if (index === -1) return false;