address/wallet: refactor usage of Address.getHash.

This commit is contained in:
Christopher Jeffrey 2017-05-25 23:26:33 -07:00
parent e8fd4db0d6
commit 46b34677f1
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
6 changed files with 53 additions and 69 deletions

View File

@ -1120,9 +1120,6 @@ ChainDB.prototype.getCoinsByAddress = co(function* getCoinsByAddress(addresses)
address = addresses[i];
hash = Address.getHash(address);
if (!hash)
throw new Error('Not an address.');
keys = yield this.db.keys({
gte: layout.C(hash, encoding.ZERO_HASH, 0),
lte: layout.C(hash, encoding.MAX_HASH, 0xffffffff),

View File

@ -552,10 +552,6 @@ Mempool.prototype.getCoinsByAddress = function getCoinsByAddress(addresses) {
for (i = 0; i < addresses.length; i++) {
hash = Address.getHash(addresses[i], 'hex');
if (!hash)
throw new Error('Not an address.');
coin = this.coinIndex.get(hash);
for (j = 0; j < coin.length; j++)
@ -580,10 +576,6 @@ Mempool.prototype.getTXByAddress = function getTXByAddress(addresses) {
for (i = 0; i < addresses.length; i++) {
hash = Address.getHash(addresses[i], 'hex');
if (!hash)
throw new Error('Not an address.');
tx = this.txIndex.get(hash);
for (j = 0; j < tx.length; j++)
@ -608,10 +600,6 @@ Mempool.prototype.getMetaByAddress = function getMetaByAddress(addresses) {
for (i = 0; i < addresses.length; i++) {
hash = Address.getHash(addresses[i], 'hex');
if (!hash)
throw new Error('Not an address.');
tx = this.txIndex.getMeta(hash);
for (j = 0; j < tx.length; j++)

View File

@ -3375,7 +3375,6 @@ Pool.prototype.sendFilterLoad = function sendFilterLoad() {
Pool.prototype.watchAddress = function watchAddress(address) {
var hash = Address.getHash(address);
assert(hash, 'Bad address.');
this.watch(hash);
};

View File

@ -816,29 +816,36 @@ Address.prototype.isUnknown = function isUnknown() {
/**
* Get the hash of a base58 address or address-related object.
* @param {Base58Address|Address|Hash} data
* @param {String} enc
* @returns {Hash|null}
* @param {String|Address|Hash} data
* @param {String?} enc
* @param {Network?} network
* @returns {Hash}
*/
Address.getHash = function getHash(data, enc) {
Address.getHash = function getHash(data, enc, network) {
var hash;
if (!data)
throw new Error('Object is not an address.');
if (typeof data === 'string') {
if (data.length === 40 || data.length === 64)
return enc === 'hex' ? data : new Buffer(data, 'hex');
try {
hash = Address.fromString(data).hash;
} catch (e) {
return;
}
hash = Address.fromString(data, network).hash;
} else if (Buffer.isBuffer(data)) {
if (data.length !== 20 && data.length !== 32)
throw new Error('Object is not an address.');
hash = data;
} else if (data instanceof Address) {
hash = data.hash;
if (network) {
network = Network.get(network);
if (data.network !== network)
throw new Error('Network mismatch for address.');
}
} else {
return;
throw new Error('Object is not an address.');
}
return enc === 'hex'

View File

@ -266,9 +266,12 @@ TXDB.prototype.values = function values(options) {
*/
TXDB.prototype.getPath = function getPath(output) {
if (!output)
var addr = output.getAddress();
if (!addr)
return Promise.resolve();
return this.wallet.getPath(output.getAddress());
return this.wallet.getPath(addr);
};
/**
@ -278,9 +281,12 @@ TXDB.prototype.getPath = function getPath(output) {
*/
TXDB.prototype.hasPath = function hasPath(output) {
if (!output)
return Promise.resolve();
return this.wallet.hasPath(output.getAddress());
var addr = output.getAddress();
if (!addr)
return Promise.resolve(false);
return this.wallet.hasPath(addr);
};
/**
@ -2342,7 +2348,11 @@ TXDB.prototype._toDetails = co(function* _toDetails(wtx) {
for (i = 0; i < tx.inputs.length; i++) {
coin = coins[i];
path = yield this.getPath(coin);
path = null;
if (coin)
path = yield this.getPath(coin);
details.setInput(i, path, coin);
}

View File

@ -1060,13 +1060,7 @@ Wallet.prototype.commit = function commit() {
Wallet.prototype.hasAddress = co(function* hasAddress(address) {
var hash = Address.getHash(address, 'hex');
var path;
if (!hash)
return false;
path = yield this.getPath(hash);
var path = yield this.getPath(hash);
return path != null;
});
@ -1100,12 +1094,7 @@ Wallet.prototype.getPath = co(function* getPath(address) {
Wallet.prototype.readPath = co(function* readPath(address) {
var hash = Address.getHash(address, 'hex');
var path;
if (!hash)
return;
path = this.pathCache.get(hash);
var path = this.pathCache.get(hash);
if (path)
return path;
@ -1129,9 +1118,6 @@ Wallet.prototype.readPath = co(function* readPath(address) {
Wallet.prototype.hasPath = co(function* hasPath(address) {
var hash = Address.getHash(address, 'hex');
if (!hash)
return false;
if (this.pathCache.has(hash))
return true;
@ -1456,12 +1442,7 @@ Wallet.prototype._fund = co(function* fund(mtx, options) {
Wallet.prototype.getAccountByAddress = co(function* getAccountByAddress(address) {
var hash = Address.getHash(address, 'hex');
var path;
if (!hash)
return;
path = yield this.getPath(hash);
var path = yield this.getPath(hash);
if (!path)
return;
@ -1478,8 +1459,13 @@ Wallet.prototype.getAccountByAddress = co(function* getAccountByAddress(address)
Wallet.prototype.estimateSize = co(function* estimateSize(prev) {
var scale = consensus.WITNESS_SCALE_FACTOR;
var address = prev.getAddress();
var account = yield this.getAccountByAddress(address);
var size = 0;
var account;
if (!address)
return -1;
account = yield this.getAccountByAddress(address);
if (!account)
return -1;
@ -1666,7 +1652,7 @@ Wallet.prototype._send = co(function* send(options, passphrase) {
Wallet.prototype.increaseFee = co(function* increaseFee(hash, rate, passphrase) {
var wtx = yield this.getTX(hash);
var i, tx, mtx, view, oldFee, fee, path, input, output, change;
var i, tx, mtx, view, oldFee, fee, path, input, output, change, addr;
assert(util.isUInt32(rate), 'Rate must be a number.');
@ -1708,7 +1694,12 @@ Wallet.prototype.increaseFee = co(function* increaseFee(hash, rate, passphrase)
for (i = 0; i < mtx.outputs.length; i++) {
output = mtx.outputs[i];
path = yield this.getPath(output.getAddress());
addr = output.getAddress();
if (!addr)
continue;
path = yield this.getPath(addr);
if (!path)
continue;
@ -1820,12 +1811,8 @@ Wallet.prototype.deriveInputs = co(function* deriveInputs(mtx) {
Wallet.prototype.getKey = co(function* getKey(address) {
var hash = Address.getHash(address, 'hex');
var path, account;
if (!hash)
return;
path = yield this.getPath(hash);
var path = yield this.getPath(hash);
var account;
if (!path)
return;
@ -1848,12 +1835,8 @@ Wallet.prototype.getKey = co(function* getKey(address) {
Wallet.prototype.getPrivateKey = co(function* getPrivateKey(address, passphrase) {
var hash = Address.getHash(address, 'hex');
var path, account, key;
if (!hash)
return;
path = yield this.getPath(hash);
var path = yield this.getPath(hash);
var account, key;
if (!path)
return;