getHash enc.

This commit is contained in:
Christopher Jeffrey 2016-06-12 11:45:55 -07:00
parent 46fbaf9514
commit 408102949c
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
8 changed files with 71 additions and 35 deletions

View File

@ -449,10 +449,15 @@ Address.validate = function validate(address, type) {
* @returns {Hash|null}
*/
Address.getHash = function getHash(data) {
Address.getHash = function getHash(data, enc) {
var hash;
if (data instanceof Address) {
if (utils.isHex(data))
return enc === 'hex' ? data : new Buffer(data, 'hex');
if (Buffer.isBuffer(data)) {
hash = data;
} else if (data instanceof Address) {
hash = data.hash;
} else {
try {
@ -462,7 +467,9 @@ Address.getHash = function getHash(data) {
}
}
return hash.toString('hex');
return enc === 'hex'
? hash.toString('hex')
: hash;
};
/*

View File

@ -178,14 +178,15 @@ Input.prototype.getAddress = function getAddress() {
/**
* Get the address hash.
* @param {String?} enc
* @returns {Hash} hash
*/
Input.prototype.getHash = function getHash() {
Input.prototype.getHash = function getHash(enc) {
var address = this.getAddress();
if (!address)
return;
return address.getHash('hex');
return address.getHash(enc);
};
/**

View File

@ -469,7 +469,7 @@ Mempool.prototype.getCoinsByAddress = function getCoinsByAddress(addresses, call
addresses = utils.uniq(addresses);
utils.forEachSerial(addresses, function(address, next) {
address = bcoin.address.getHash(address);
address = bcoin.address.getHash(address, 'hex');
if (!address)
return next();
@ -513,7 +513,7 @@ Mempool.prototype.getTXByAddress = function getTXByAddress(addresses, callback)
addresses = utils.uniq(addresses);
utils.forEachSerial(addresses, function(address, next) {
address = bcoin.address.getHash(address);
address = bcoin.address.getHash(address, 'hex');
if (!address)
return next();
@ -1594,7 +1594,7 @@ Mempool.prototype._addUnchecked = function _addUnchecked(entry, callback) {
batch.put('m/' + pad32(entry.ts) + '/' + hash, DUMMY);
if (this.options.indexAddress) {
addresses = tx.getHashes();
addresses = tx.getHashes('hex');
for (i = 0; i < addresses.length; i++)
batch.put('T/' + addresses[i] + '/' + hash, DUMMY);
}
@ -1612,7 +1612,7 @@ Mempool.prototype._addUnchecked = function _addUnchecked(entry, callback) {
batch.put('s/' + key, tx.hash());
if (this.options.indexAddress) {
address = input.getHash();
address = input.getHash('hex');
if (address)
batch.del('C/' + address + '/' + key);
}
@ -1630,7 +1630,7 @@ Mempool.prototype._addUnchecked = function _addUnchecked(entry, callback) {
batch.put('c/' + key, coin);
if (this.options.indexAddress) {
address = output.getHash();
address = output.getHash('hex');
if (address)
batch.put('C/' + address + '/' + key, DUMMY);
}
@ -1668,7 +1668,7 @@ Mempool.prototype._removeUnchecked = function _removeUnchecked(hash, limit, call
batch.del('m/' + pad32(entry.ts) + '/' + hash);
if (self.options.indexAddress) {
addresses = tx.getHashes();
addresses = tx.getHashes('hex');
for (i = 0; i < addresses.length; i++)
batch.del('T/' + addresses[i] + '/' + hash);
}
@ -1692,14 +1692,14 @@ Mempool.prototype._removeUnchecked = function _removeUnchecked(hash, limit, call
if (result) {
batch.put('c/' + key, input.coin.toRaw());
if (self.options.indexAddress) {
address = input.getHash();
address = input.getHash('hex');
if (address)
batch.put('C/' + address + '/' + key, DUMMY);
}
} else {
batch.del('c/' + key);
if (self.options.indexAddress) {
address = input.getHash();
address = input.getHash('hex');
if (address)
batch.del('C/' + address + '/' + key);
}
@ -1721,7 +1721,7 @@ Mempool.prototype._removeUnchecked = function _removeUnchecked(hash, limit, call
batch.del('c/' + key);
if (self.options.indexAddress) {
address = output.getHash();
address = output.getHash('hex');
if (address)
batch.del('C/' + address + '/' + key);
}

View File

@ -86,14 +86,15 @@ Output.prototype.getAddress = function getAddress() {
/**
* Get the address hash.
* @param {String?} enc
* @returns {Hash} hash
*/
Output.prototype.getHash = function getHash() {
Output.prototype.getHash = function getHash(enc) {
var address = this.getAddress();
if (!address)
return;
return address.getHash('hex');
return address.getHash(enc);
};
/**
@ -104,7 +105,7 @@ Output.prototype.getHash = function getHash() {
*/
Output.prototype.test = function test(addressMap) {
var hash = this.getHash();
var hash = this.getHash('hex');
if (!hash)
return false;

View File

@ -1242,7 +1242,7 @@ Pool.prototype.updateWatch = function updateWatch() {
*/
Pool.prototype.watchAddress = function watchAddress(address) {
this.watch(bcoin.address.getHash(address), 'hex');
this.watch(bcoin.address.getHash(address));
};
/**

View File

@ -811,8 +811,17 @@ TX.prototype.getAddresses = function getAddresses() {
* @returns {Hash[]} hashes
*/
TX.prototype.getInputHashes = function getInputHashes() {
return Object.keys(this.getInputAddresses().table);
TX.prototype.getInputHashes = function getInputHashes(enc) {
var input = this.getInputAddresses();
var i;
if (enc === 'hex')
return Object.keys(input.table);
for (i = 0; i < input.length; i++)
input[i] = input[i].getHash();
return input;
};
/**
@ -820,8 +829,17 @@ TX.prototype.getInputHashes = function getInputHashes() {
* @returns {Hash[]} hashes
*/
TX.prototype.getOutputHashes = function getOutputHashes() {
return Object.keys(this.getOutputAddresses().table);
TX.prototype.getOutputHashes = function getOutputHashes(enc) {
var output = this.getOutputAddresses();
var i;
if (enc === 'hex')
return Object.keys(output.table);
for (i = 0; i < output.length; i++)
output[i] = output[i].getHash();
return output;
};
/**
@ -829,8 +847,17 @@ TX.prototype.getOutputHashes = function getOutputHashes() {
* @returns {Hash[]} hashes
*/
TX.prototype.getHashes = function getHashes() {
return Object.keys(this.getAddresses().table);
TX.prototype.getHashes = function getHashes(enc) {
var hashes = this.getAddresses();
var i;
if (enc === 'hex')
return Object.keys(hashes.table);
for (i = 0; i < hashes.length; i++)
hashes[i] = hashes[i].getHash();
return hashes;
};
/**

View File

@ -110,8 +110,8 @@ TXDB.prototype._testFilter = function _testFilter(addresses) {
TXDB.prototype.getMap = function getMap(tx, callback) {
var i, input, output, address, addresses, map;
input = tx.getInputHashes();
output = tx.getOutputHashes();
input = tx.getInputHashes('hex');
output = tx.getOutputHashes('hex');
addresses = utils.uniq(input.concat(output));
if (!this._testFilter(addresses))
@ -347,7 +347,7 @@ TXDB.prototype._add = function add(tx, map, callback, force) {
if (tx.isCoinbase())
return next();
address = input.getHash();
address = input.getHash('hex');
// Only add orphans if this input is ours.
if (!address || !map.table[address].length)
@ -445,7 +445,7 @@ TXDB.prototype._add = function add(tx, map, callback, force) {
// Add unspent outputs or resolve orphans
utils.forEachSerial(tx.outputs, function(output, next, i) {
var address = output.getHash();
var address = output.getHash('hex');
var key = hash + '/' + i;
var coin;
@ -747,7 +747,7 @@ TXDB.prototype._confirm = function _confirm(tx, map, callback, force) {
}
utils.forEachSerial(tx.outputs, function(output, next, i) {
var address = output.getHash();
var address = output.getHash('hex');
// Only update coins if this output is ours.
if (!address || !map.table[address].length)
@ -896,7 +896,7 @@ TXDB.prototype._remove = function remove(tx, map, callback, force) {
for (i = 0; i < tx.inputs.length; i++) {
input = tx.inputs[i];
key = input.prevout.hash + '/' + input.prevout.index;
address = input.getHash();
address = input.getHash('hex');
if (tx.isCoinbase())
break;
@ -923,7 +923,7 @@ TXDB.prototype._remove = function remove(tx, map, callback, force) {
for (i = 0; i < tx.outputs.length; i++) {
output = tx.outputs[i];
key = hash + '/' + i;
address = output.getHash();
address = output.getHash('hex');
if (!address || !map.table[address].length)
continue;

View File

@ -729,7 +729,7 @@ Wallet.prototype.getInputPaths = function getInputPaths(tx, callback) {
if (tx instanceof bcoin.input) {
if (!tx.coin)
return callback(new Error('Not all coins available.'));
hashes = [tx.coin.getHash()];
hashes = [tx.coin.getHash('hex')];
return done();
}
@ -740,7 +740,7 @@ Wallet.prototype.getInputPaths = function getInputPaths(tx, callback) {
if (!tx.hasCoins())
return callback(new Error('Not all coins available.'));
hashes = tx.getInputHashes();
hashes = tx.getInputHashes('hex');
done();
});
};
@ -757,9 +757,9 @@ Wallet.prototype.getOutputPaths = function getOutputPaths(tx, callback) {
var hashes;
if (tx instanceof bcoin.output)
hashes = [tx.getHash()];
hashes = [tx.getHash('hex')];
else
hashes = tx.getOutputHashes();
hashes = tx.getOutputHashes('hex');
utils.forEachSerial(hashes, function(hash, next, i) {
self.getPath(hash, function(err, path) {