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} * @returns {Hash|null}
*/ */
Address.getHash = function getHash(data) { Address.getHash = function getHash(data, enc) {
var hash; 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; hash = data.hash;
} else { } else {
try { 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. * Get the address hash.
* @param {String?} enc
* @returns {Hash} hash * @returns {Hash} hash
*/ */
Input.prototype.getHash = function getHash() { Input.prototype.getHash = function getHash(enc) {
var address = this.getAddress(); var address = this.getAddress();
if (!address) if (!address)
return; 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); addresses = utils.uniq(addresses);
utils.forEachSerial(addresses, function(address, next) { utils.forEachSerial(addresses, function(address, next) {
address = bcoin.address.getHash(address); address = bcoin.address.getHash(address, 'hex');
if (!address) if (!address)
return next(); return next();
@ -513,7 +513,7 @@ Mempool.prototype.getTXByAddress = function getTXByAddress(addresses, callback)
addresses = utils.uniq(addresses); addresses = utils.uniq(addresses);
utils.forEachSerial(addresses, function(address, next) { utils.forEachSerial(addresses, function(address, next) {
address = bcoin.address.getHash(address); address = bcoin.address.getHash(address, 'hex');
if (!address) if (!address)
return next(); return next();
@ -1594,7 +1594,7 @@ Mempool.prototype._addUnchecked = function _addUnchecked(entry, callback) {
batch.put('m/' + pad32(entry.ts) + '/' + hash, DUMMY); batch.put('m/' + pad32(entry.ts) + '/' + hash, DUMMY);
if (this.options.indexAddress) { if (this.options.indexAddress) {
addresses = tx.getHashes(); addresses = tx.getHashes('hex');
for (i = 0; i < addresses.length; i++) for (i = 0; i < addresses.length; i++)
batch.put('T/' + addresses[i] + '/' + hash, DUMMY); batch.put('T/' + addresses[i] + '/' + hash, DUMMY);
} }
@ -1612,7 +1612,7 @@ Mempool.prototype._addUnchecked = function _addUnchecked(entry, callback) {
batch.put('s/' + key, tx.hash()); batch.put('s/' + key, tx.hash());
if (this.options.indexAddress) { if (this.options.indexAddress) {
address = input.getHash(); address = input.getHash('hex');
if (address) if (address)
batch.del('C/' + address + '/' + key); batch.del('C/' + address + '/' + key);
} }
@ -1630,7 +1630,7 @@ Mempool.prototype._addUnchecked = function _addUnchecked(entry, callback) {
batch.put('c/' + key, coin); batch.put('c/' + key, coin);
if (this.options.indexAddress) { if (this.options.indexAddress) {
address = output.getHash(); address = output.getHash('hex');
if (address) if (address)
batch.put('C/' + address + '/' + key, DUMMY); 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); batch.del('m/' + pad32(entry.ts) + '/' + hash);
if (self.options.indexAddress) { if (self.options.indexAddress) {
addresses = tx.getHashes(); addresses = tx.getHashes('hex');
for (i = 0; i < addresses.length; i++) for (i = 0; i < addresses.length; i++)
batch.del('T/' + addresses[i] + '/' + hash); batch.del('T/' + addresses[i] + '/' + hash);
} }
@ -1692,14 +1692,14 @@ Mempool.prototype._removeUnchecked = function _removeUnchecked(hash, limit, call
if (result) { if (result) {
batch.put('c/' + key, input.coin.toRaw()); batch.put('c/' + key, input.coin.toRaw());
if (self.options.indexAddress) { if (self.options.indexAddress) {
address = input.getHash(); address = input.getHash('hex');
if (address) if (address)
batch.put('C/' + address + '/' + key, DUMMY); batch.put('C/' + address + '/' + key, DUMMY);
} }
} else { } else {
batch.del('c/' + key); batch.del('c/' + key);
if (self.options.indexAddress) { if (self.options.indexAddress) {
address = input.getHash(); address = input.getHash('hex');
if (address) if (address)
batch.del('C/' + address + '/' + key); batch.del('C/' + address + '/' + key);
} }
@ -1721,7 +1721,7 @@ Mempool.prototype._removeUnchecked = function _removeUnchecked(hash, limit, call
batch.del('c/' + key); batch.del('c/' + key);
if (self.options.indexAddress) { if (self.options.indexAddress) {
address = output.getHash(); address = output.getHash('hex');
if (address) if (address)
batch.del('C/' + address + '/' + key); batch.del('C/' + address + '/' + key);
} }

View File

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

View File

@ -1242,7 +1242,7 @@ Pool.prototype.updateWatch = function updateWatch() {
*/ */
Pool.prototype.watchAddress = function watchAddress(address) { 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 * @returns {Hash[]} hashes
*/ */
TX.prototype.getInputHashes = function getInputHashes() { TX.prototype.getInputHashes = function getInputHashes(enc) {
return Object.keys(this.getInputAddresses().table); 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 * @returns {Hash[]} hashes
*/ */
TX.prototype.getOutputHashes = function getOutputHashes() { TX.prototype.getOutputHashes = function getOutputHashes(enc) {
return Object.keys(this.getOutputAddresses().table); 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 * @returns {Hash[]} hashes
*/ */
TX.prototype.getHashes = function getHashes() { TX.prototype.getHashes = function getHashes(enc) {
return Object.keys(this.getAddresses().table); 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) { TXDB.prototype.getMap = function getMap(tx, callback) {
var i, input, output, address, addresses, map; var i, input, output, address, addresses, map;
input = tx.getInputHashes(); input = tx.getInputHashes('hex');
output = tx.getOutputHashes(); output = tx.getOutputHashes('hex');
addresses = utils.uniq(input.concat(output)); addresses = utils.uniq(input.concat(output));
if (!this._testFilter(addresses)) if (!this._testFilter(addresses))
@ -347,7 +347,7 @@ TXDB.prototype._add = function add(tx, map, callback, force) {
if (tx.isCoinbase()) if (tx.isCoinbase())
return next(); return next();
address = input.getHash(); address = input.getHash('hex');
// Only add orphans if this input is ours. // Only add orphans if this input is ours.
if (!address || !map.table[address].length) 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 // Add unspent outputs or resolve orphans
utils.forEachSerial(tx.outputs, function(output, next, i) { utils.forEachSerial(tx.outputs, function(output, next, i) {
var address = output.getHash(); var address = output.getHash('hex');
var key = hash + '/' + i; var key = hash + '/' + i;
var coin; var coin;
@ -747,7 +747,7 @@ TXDB.prototype._confirm = function _confirm(tx, map, callback, force) {
} }
utils.forEachSerial(tx.outputs, function(output, next, i) { 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. // Only update coins if this output is ours.
if (!address || !map.table[address].length) 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++) { for (i = 0; i < tx.inputs.length; i++) {
input = tx.inputs[i]; input = tx.inputs[i];
key = input.prevout.hash + '/' + input.prevout.index; key = input.prevout.hash + '/' + input.prevout.index;
address = input.getHash(); address = input.getHash('hex');
if (tx.isCoinbase()) if (tx.isCoinbase())
break; break;
@ -923,7 +923,7 @@ TXDB.prototype._remove = function remove(tx, map, callback, force) {
for (i = 0; i < tx.outputs.length; i++) { for (i = 0; i < tx.outputs.length; i++) {
output = tx.outputs[i]; output = tx.outputs[i];
key = hash + '/' + i; key = hash + '/' + i;
address = output.getHash(); address = output.getHash('hex');
if (!address || !map.table[address].length) if (!address || !map.table[address].length)
continue; continue;

View File

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