wallet: refactor variable names.

This commit is contained in:
Christopher Jeffrey 2016-10-03 08:30:02 -07:00
parent f1b4c61a99
commit f5625e76e1
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 124 additions and 116 deletions

View File

@ -6,7 +6,6 @@
'use strict'; 'use strict';
var Network = require('../protocol/network');
var utils = require('../utils/utils'); var utils = require('../utils/utils');
var co = require('../utils/co'); var co = require('../utils/co');
var assert = require('assert'); var assert = require('assert');

View File

@ -235,14 +235,15 @@ Wallet.prototype.destroy = co(function* destroy() {
/** /**
* Add a public account key to the wallet (multisig). * Add a public account key to the wallet (multisig).
* Saves the key in the wallet database. * Saves the key in the wallet database.
* @param {(Number|String)} acct
* @param {HDPublicKey} key * @param {HDPublicKey} key
* @returns {Promise} * @returns {Promise}
*/ */
Wallet.prototype.addKey = co(function* addKey(account, key) { Wallet.prototype.addKey = co(function* addKey(acct, key) {
var unlock = yield this.writeLock.lock(); var unlock = yield this.writeLock.lock();
try { try {
return yield this._addKey(account, key); return yield this._addKey(acct, key);
} finally { } finally {
unlock(); unlock();
} }
@ -251,22 +252,23 @@ Wallet.prototype.addKey = co(function* addKey(account, key) {
/** /**
* Add a public account key to the wallet without a lock. * Add a public account key to the wallet without a lock.
* @private * @private
* @param {(Number|String)} acct
* @param {HDPublicKey} key * @param {HDPublicKey} key
* @returns {Promise} * @returns {Promise}
*/ */
Wallet.prototype._addKey = co(function* addKey(account, key) { Wallet.prototype._addKey = co(function* addKey(acct, key) {
var result; var account, result;
if (!key) { if (!key) {
key = account; key = acct;
account = null; acct = null;
} }
if (account == null) if (acct == null)
account = 0; acct = 0;
account = yield this.getAccount(account); account = yield this.getAccount(acct);
if (!account) if (!account)
throw new Error('Account not found.'); 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). * Remove a public account key from the wallet (multisig).
* @param {(Number|String)} acct
* @param {HDPublicKey} key * @param {HDPublicKey} key
* @returns {Promise} * @returns {Promise}
*/ */
Wallet.prototype.removeKey = co(function* removeKey(account, key) { Wallet.prototype.removeKey = co(function* removeKey(acct, key) {
var unlock = yield this.writeLock.lock(); var unlock = yield this.writeLock.lock();
try { try {
return yield this._removeKey(account, key); return yield this._removeKey(acct, key);
} finally { } finally {
unlock(); unlock();
} }
@ -303,22 +306,23 @@ Wallet.prototype.removeKey = co(function* removeKey(account, key) {
/** /**
* Remove a public account key from the wallet (multisig). * Remove a public account key from the wallet (multisig).
* @private * @private
* @param {(Number|String)} acct
* @param {HDPublicKey} key * @param {HDPublicKey} key
* @returns {Promise} * @returns {Promise}
*/ */
Wallet.prototype._removeKey = co(function* removeKey(account, key) { Wallet.prototype._removeKey = co(function* removeKey(acct, key) {
var result; var account, result;
if (!key) { if (!key) {
key = account; key = acct;
account = null; acct = null;
} }
if (account == null) if (acct == null)
account = 0; acct = 0;
account = yield this.getAccount(account); account = yield this.getAccount(acct);
if (!account) if (!account)
throw new Error('Account not found.'); throw new Error('Account not found.');
@ -501,15 +505,15 @@ Wallet.prototype.rename = co(function* rename(id) {
/** /**
* Rename account. * Rename account.
* @param {(String|Number)?} account * @param {(String|Number)?} acct
* @param {String} name * @param {String} name
* @returns {Promise} * @returns {Promise}
*/ */
Wallet.prototype.renameAccount = co(function* renameAccount(account, name) { Wallet.prototype.renameAccount = co(function* renameAccount(acct, name) {
var unlock = yield this.writeLock.lock(); var unlock = yield this.writeLock.lock();
try { try {
return yield this._renameAccount(account, name); return yield this._renameAccount(acct, name);
} finally { } finally {
unlock(); unlock();
} }
@ -518,15 +522,17 @@ Wallet.prototype.renameAccount = co(function* renameAccount(account, name) {
/** /**
* Rename account without a lock. * Rename account without a lock.
* @private * @private
* @param {(String|Number)?} account * @param {(String|Number)?} acct
* @param {String} name * @param {String} name
* @returns {Promise} * @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.'); assert(utils.isName(name), 'Bad account name.');
account = yield this.getAccount(account); account = yield this.getAccount(acct);
if (!account) if (!account)
throw new Error('Account not found.'); throw new Error('Account not found.');
@ -700,16 +706,16 @@ Wallet.prototype._createAccount = co(function* createAccount(options) {
*/ */
Wallet.prototype.ensureAccount = co(function* ensureAccount(options) { Wallet.prototype.ensureAccount = co(function* ensureAccount(options) {
var account = options.account; var acct = options.account;
var exists; var exists;
if (typeof options.name === 'string') if (typeof options.name === 'string')
account = options.name; acct = options.name;
exists = yield this.hasAccount(account); exists = yield this.hasAccount(acct);
if (exists) if (exists)
return yield this.getAccount(account); return yield this.getAccount(acct);
return this.createAccount(options); return this.createAccount(options);
}); });
@ -734,17 +740,19 @@ Wallet.prototype.getAddressHashes = function getAddressHashes() {
/** /**
* Retrieve an account from the database. * Retrieve an account from the database.
* @param {Number|String} account * @param {Number|String} acct
* @returns {Promise} - Returns {@link Account}. * @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 (this.account) {
if (account === 0 || account === 'default') if (acct === 0 || acct === 'default')
return this.account; return this.account;
} }
account = yield this.db.getAccount(this.wid, account); account = yield this.db.getAccount(this.wid, acct);
if (!account) if (!account)
return; return;
@ -758,55 +766,55 @@ Wallet.prototype.getAccount = co(function* getAccount(account) {
/** /**
* Test whether an account exists. * Test whether an account exists.
* @param {Number|String} account * @param {Number|String} acct
* @returns {Promise} - Returns {@link Boolean}. * @returns {Promise} - Returns {@link Boolean}.
*/ */
Wallet.prototype.hasAccount = function hasAccount(account) { Wallet.prototype.hasAccount = function hasAccount(acct) {
return this.db.hasAccount(this.wid, account); return this.db.hasAccount(this.wid, acct);
}; };
/** /**
* Create a new receiving address (increments receiveDepth). * Create a new receiving address (increments receiveDepth).
* @param {(Number|String)?} account * @param {(Number|String)?} acct
* @returns {Promise} - Returns {@link WalletKey}. * @returns {Promise} - Returns {@link WalletKey}.
*/ */
Wallet.prototype.createReceive = function createReceive(account) { Wallet.prototype.createReceive = function createReceive(acct) {
return this.createKey(account, 0); return this.createKey(acct, 0);
}; };
/** /**
* Create a new change address (increments receiveDepth). * Create a new change address (increments receiveDepth).
* @param {(Number|String)?} account * @param {(Number|String)?} acct
* @returns {Promise} - Returns {@link WalletKey}. * @returns {Promise} - Returns {@link WalletKey}.
*/ */
Wallet.prototype.createChange = function createChange(account) { Wallet.prototype.createChange = function createChange(acct) {
return this.createKey(account, 1); return this.createKey(acct, 1);
}; };
/** /**
* Create a new nested address (increments receiveDepth). * Create a new nested address (increments receiveDepth).
* @param {(Number|String)?} account * @param {(Number|String)?} acct
* @returns {Promise} - Returns {@link WalletKey}. * @returns {Promise} - Returns {@link WalletKey}.
*/ */
Wallet.prototype.createNested = function createNested(account) { Wallet.prototype.createNested = function createNested(acct) {
return this.createKey(account, 2); return this.createKey(acct, 2);
}; };
/** /**
* Create a new address (increments depth). * Create a new address (increments depth).
* @param {(Number|String)?} account * @param {(Number|String)?} acct
* @param {Number} branch * @param {Number} branch
* @returns {Promise} - Returns {@link WalletKey}. * @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(); var unlock = yield this.writeLock.lock();
try { try {
return yield this._createKey(account, branch); return yield this._createKey(acct, branch);
} finally { } finally {
unlock(); unlock();
} }
@ -815,23 +823,23 @@ Wallet.prototype.createKey = co(function* createKey(account, branch) {
/** /**
* Create a new address (increments depth) without a lock. * Create a new address (increments depth) without a lock.
* @private * @private
* @param {(Number|String)?} account * @param {(Number|String)?} acct
* @param {Number} branche * @param {Number} branche
* @returns {Promise} - Returns {@link WalletKey}. * @returns {Promise} - Returns {@link WalletKey}.
*/ */
Wallet.prototype._createKey = co(function* createKey(account, branch) { Wallet.prototype._createKey = co(function* createKey(acct, branch) {
var result; var account, result;
if (branch == null) { if (branch == null) {
branch = account; branch = acct;
account = null; acct = null;
} }
if (account == null) if (acct == null)
account = 0; acct = 0;
account = yield this.getAccount(account); account = yield this.getAccount(acct);
if (!account) if (!account)
throw new Error('Account not found.'); throw new Error('Account not found.');
@ -925,15 +933,15 @@ Wallet.prototype.getPath = co(function* getPath(address) {
/** /**
* Get all wallet paths. * Get all wallet paths.
* @param {(String|Number)?} account * @param {(String|Number)?} acct
* @returns {Promise} - Returns {@link Path}. * @returns {Promise} - Returns {@link Path}.
*/ */
Wallet.prototype.getPaths = co(function* getPaths(account) { Wallet.prototype.getPaths = co(function* getPaths(acct) {
var out = []; 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); paths = yield this.db.getWalletPaths(this.wid);
for (i = 0; i < paths.length; i++) { 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). * Import a keyring (will not exist on derivation chain).
* Rescanning must be invoked manually. * Rescanning must be invoked manually.
* @param {(String|Number)?} account * @param {(String|Number)?} acct
* @param {WalletKey} ring * @param {WalletKey} ring
* @param {(String|Buffer)?} passphrase * @param {(String|Buffer)?} passphrase
* @returns {Promise} * @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(); var unlock = yield this.writeLock.lock();
try { try {
return yield this._importKey(account, ring, passphrase); return yield this._importKey(acct, ring, passphrase);
} finally { } finally {
unlock(); 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. * Import a keyring (will not exist on derivation chain) without a lock.
* @private * @private
* @param {(String|Number)?} account * @param {(String|Number)?} acct
* @param {WalletKey} ring * @param {WalletKey} ring
* @param {(String|Buffer)?} passphrase * @param {(String|Buffer)?} passphrase
* @returns {Promise} * @returns {Promise}
*/ */
Wallet.prototype._importKey = co(function* importKey(account, ring, passphrase) { Wallet.prototype._importKey = co(function* importKey(acct, ring, passphrase) {
var exists, path; var account, exists, path;
if (account && typeof account === 'object') { if (acct && typeof acct === 'object') {
passphrase = ring; passphrase = ring;
ring = account; ring = acct;
account = null; acct = null;
} }
if (account == null) if (acct == null)
account = 0; acct = 0;
if (!this.watchOnly) { if (!this.watchOnly) {
if (!ring.privateKey) if (!ring.privateKey)
@ -999,7 +1007,7 @@ Wallet.prototype._importKey = co(function* importKey(account, ring, passphrase)
if (exists) if (exists)
throw new Error('Key already exists.'); throw new Error('Key already exists.');
account = yield this.getAccount(account); account = yield this.getAccount(acct);
if (!account) if (!account)
throw new Error('Account not found.'); 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). * Import a keyring (will not exist on derivation chain).
* Rescanning must be invoked manually. * Rescanning must be invoked manually.
* @param {(String|Number)?} account * @param {(String|Number)?} acct
* @param {WalletKey} ring * @param {WalletKey} ring
* @param {(String|Buffer)?} passphrase * @param {(String|Buffer)?} passphrase
* @returns {Promise} * @returns {Promise}
*/ */
Wallet.prototype.importAddress = co(function* importAddress(account, address) { Wallet.prototype.importAddress = co(function* importAddress(acct, address) {
var unlock = yield this.writeLock.lock(); var unlock = yield this.writeLock.lock();
try { try {
return yield this._importAddress(account, address); return yield this._importAddress(acct, address);
} finally { } finally {
unlock(); 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. * Import a keyring (will not exist on derivation chain) without a lock.
* @private * @private
* @param {(String|Number)?} account * @param {(String|Number)?} acct
* @param {WalletKey} ring * @param {WalletKey} ring
* @param {(String|Buffer)?} passphrase * @param {(String|Buffer)?} passphrase
* @returns {Promise} * @returns {Promise}
*/ */
Wallet.prototype._importAddress = co(function* importAddress(account, address) { Wallet.prototype._importAddress = co(function* importAddress(acct, address) {
var exists, path; var account, exists, path;
if (!address) { if (!address) {
address = account; address = acct;
account = null; acct = null;
} }
if (account == null) if (acct == null)
account = 0; acct = 0;
if (!this.watchOnly) if (!this.watchOnly)
throw new Error('Cannot import address into non watch-only wallet.'); 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) if (exists)
throw new Error('Address already exists.'); throw new Error('Address already exists.');
account = yield this.getAccount(account); account = yield this.getAccount(acct);
if (!account) if (!account)
throw new Error('Account not found.'); throw new Error('Account not found.');
@ -1696,12 +1704,12 @@ Wallet.prototype.addTX = function addTX(tx) {
/** /**
* Get all transactions in transaction history (accesses db). * Get all transactions in transaction history (accesses db).
* @param {(String|Number)?} account * @param {(String|Number)?} acct
* @returns {Promise} - Returns {@link TX}[]. * @returns {Promise} - Returns {@link TX}[].
*/ */
Wallet.prototype.getHistory = co(function* getHistory(account) { Wallet.prototype.getHistory = co(function* getHistory(acct) {
account = yield this._getIndex(account); var account = yield this._getIndex(acct);
return this.txdb.getHistory(account); return this.txdb.getHistory(account);
}); });
@ -1711,72 +1719,73 @@ Wallet.prototype.getHistory = co(function* getHistory(account) {
* @returns {Promise} - Returns {@link Coin}[]. * @returns {Promise} - Returns {@link Coin}[].
*/ */
Wallet.prototype.getCoins = co(function* getCoins(account) { Wallet.prototype.getCoins = co(function* getCoins(acct) {
account = yield this._getIndex(account); var account = yield this._getIndex(acct);
return yield this.txdb.getCoins(account); return yield this.txdb.getCoins(account);
}); });
/** /**
* Get all pending/unconfirmed transactions (accesses db). * Get all pending/unconfirmed transactions (accesses db).
* @param {(String|Number)?} account * @param {(String|Number)?} acct
* @returns {Promise} - Returns {@link TX}[]. * @returns {Promise} - Returns {@link TX}[].
*/ */
Wallet.prototype.getUnconfirmed = co(function* getUnconfirmed(account) { Wallet.prototype.getUnconfirmed = co(function* getUnconfirmed(acct) {
account = yield this._getIndex(account); var account = yield this._getIndex(acct);
return yield this.txdb.getUnconfirmed(account); return yield this.txdb.getUnconfirmed(account);
}); });
/** /**
* Get wallet balance (accesses db). * Get wallet balance (accesses db).
* @param {(String|Number)?} account * @param {(String|Number)?} acct
* @returns {Promise} - Returns {@link Balance}. * @returns {Promise} - Returns {@link Balance}.
*/ */
Wallet.prototype.getBalance = co(function* getBalance(account) { Wallet.prototype.getBalance = co(function* getBalance(acct) {
account = yield this._getIndex(account); var account = yield this._getIndex(acct);
return yield this.txdb.getBalance(account); return yield this.txdb.getBalance(account);
}); });
/** /**
* Get a range of transactions between two timestamps (accesses db). * Get a range of transactions between two timestamps (accesses db).
* @param {(String|Number)?} account * @param {(String|Number)?} acct
* @param {Object} options * @param {Object} options
* @param {Number} options.start * @param {Number} options.start
* @param {Number} options.end * @param {Number} options.end
* @returns {Promise} - Returns {@link TX}[]. * @returns {Promise} - Returns {@link TX}[].
*/ */
Wallet.prototype.getRange = co(function* getRange(account, options) { Wallet.prototype.getRange = co(function* getRange(acct, options) {
if (account && typeof account === 'object') { var account;
options = account; if (acct && typeof acct === 'object') {
account = null; options = acct;
acct = null;
} }
account = yield this._getIndex(account); account = yield this._getIndex(acct);
return yield this.txdb.getRange(account, options); return yield this.txdb.getRange(account, options);
}); });
/** /**
* Get the last N transactions (accesses db). * Get the last N transactions (accesses db).
* @param {(String|Number)?} account * @param {(String|Number)?} acct
* @param {Number} limit * @param {Number} limit
* @returns {Promise} - Returns {@link TX}[]. * @returns {Promise} - Returns {@link TX}[].
*/ */
Wallet.prototype.getLast = co(function* getLast(account, limit) { Wallet.prototype.getLast = co(function* getLast(acct, limit) {
account = yield this._getIndex(account); var account = yield this._getIndex(acct);
return yield this.txdb.getLast(account, limit); return yield this.txdb.getLast(account, limit);
}); });
/** /**
* Zap stale TXs from wallet (accesses db). * 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). * @param {Number} age - Age threshold (unix time, default=72 hours).
* @returns {Promise} * @returns {Promise}
*/ */
Wallet.prototype.zap = co(function* zap(account, age) { Wallet.prototype.zap = co(function* zap(acct, age) {
account = yield this._getIndex(account); var account = yield this._getIndex(acct);
return yield this.txdb.zap(account, age); return yield this.txdb.zap(account, age);
}); });
@ -1793,18 +1802,18 @@ Wallet.prototype.abandon = function abandon(hash) {
/** /**
* Resolve account index. * Resolve account index.
* @private * @private
* @param {(Number|String)?} account * @param {(Number|String)?} acct
* @param {Function} errback - Returns [Error]. * @param {Function} errback - Returns [Error].
* @returns {Promise} * @returns {Promise}
*/ */
Wallet.prototype._getIndex = co(function* _getIndex(account) { Wallet.prototype._getIndex = co(function* _getIndex(acct) {
var index; var index;
if (account == null) if (acct == null)
return null; return null;
index = yield this.db.getAccountIndex(this.wid, account); index = yield this.db.getAccountIndex(this.wid, acct);
if (index === -1) if (index === -1)
throw new Error('Account not found.'); throw new Error('Account not found.');

View File

@ -721,12 +721,12 @@ WalletDB.prototype.ensure = co(function* ensure(options) {
/** /**
* Get an account from the database. * Get an account from the database.
* @param {WalletID} wid * @param {WalletID} wid
* @param {String|Number} name - Account name/index. * @param {String|Number} acct - Account name/index.
* @returns {Promise} - Returns {@link Wallet}. * @returns {Promise} - Returns {@link Wallet}.
*/ */
WalletDB.prototype.getAccount = co(function* getAccount(wid, name) { WalletDB.prototype.getAccount = co(function* getAccount(wid, acct) {
var index = yield this.getAccountIndex(wid, name); var index = yield this.getAccountIndex(wid, acct);
var account; var account;
if (index === -1) if (index === -1)
@ -906,17 +906,17 @@ WalletDB.prototype.createAccount = co(function* createAccount(options) {
/** /**
* Test for the existence of an account. * Test for the existence of an account.
* @param {WalletID} wid * @param {WalletID} wid
* @param {String|Number} account * @param {String|Number} acct
* @returns {Promise} - Returns Boolean. * @returns {Promise} - Returns Boolean.
*/ */
WalletDB.prototype.hasAccount = co(function* hasAccount(wid, account) { WalletDB.prototype.hasAccount = co(function* hasAccount(wid, acct) {
var index, key; var index, key;
if (!wid) if (!wid)
return false; return false;
index = yield this.getAccountIndex(wid, account); index = yield this.getAccountIndex(wid, acct);
if (index === -1) if (index === -1)
return false; return false;