refactor lib/bitcoind.js.

This commit is contained in:
Christopher Jeffrey 2014-12-08 12:33:59 -08:00
parent 263bc2fa73
commit 0b07a7839b

View File

@ -364,8 +364,8 @@ Bitcoin.prototype.start = function(options, callback) {
}, 1000); }, 1000);
}; };
Bitcoin.prototype.getBlock = function(blockHash, callback) { Bitcoin.prototype.getBlock = function(blockhash, callback) {
return bitcoindjs.getBlock(blockHash, function(err, block) { return bitcoindjs.getBlock(blockhash, function(err, block) {
if (err) return callback(err); if (err) return callback(err);
return callback(null, bitcoin.block(block)); return callback(null, bitcoin.block(block));
}); });
@ -379,35 +379,43 @@ Bitcoin.prototype.getBlockHeight = function(height, callback) {
}; };
Bitcoin.prototype.getTransaction = Bitcoin.prototype.getTransaction =
Bitcoin.prototype.getTx = function(txHash, blockHash, callback) { Bitcoin.prototype.getTx = function(txid, blockhash, callback) {
if (typeof blockHash === 'function') { var traverse = false;
callback = blockHash; if (typeof txid === 'object') {
blockHash = ''; var options = txid;
callback = blockhash;
txid = options.txid || options.tx || options.txhash || options.id || options.hash;
blockhash = options.blockhash || options.block;
traverse = options.traverse;
} }
if (typeof blockHash !== 'string') { if (typeof blockhash === 'function') {
if (blockHash) { callback = blockhash;
blockHash = blockHash.hash blockhash = '';
|| blockHash.blockhash }
|| (blockHash.getHash && blockHash.getHash()) if (typeof blockhash !== 'string') {
if (blockhash) {
blockhash = blockhash.hash
|| blockhash.blockhash
|| (blockhash.getHash && blockhash.getHash())
|| ''; || '';
} else { } else {
blockHash = ''; blockhash = '';
} }
} }
if (blockHash && typeof blockHash === 'string') { if (blockhash && typeof blockhash === 'string') {
return bitcoindjs.getTransaction(txHash, blockHash, function(err, tx) { return bitcoindjs.getTransaction(txid, blockhash, traverse, function(err, tx) {
if (err) return callback(err); if (err) return callback(err);
bitcoin.db.set('tx-block/' + txHash, bitcoin.db.set('tx-block/' + txid,
{ hash: blockHash }, utils.NOOP); { hash: blockhash }, utils.NOOP);
return callback(null, bitcoin.tx(tx)); return callback(null, bitcoin.tx(tx));
}); });
} }
return bitcoin.db.get('tx-block/' + txHash, function(err, block) { return bitcoin.db.get('tx-block/' + txid, function(err, block) {
// Will traverse blockchain if no block - slow: // Will traverse blockchain if no block - slow:
return bitcoinjs.getTransaction(txHash, block ? block.hash : '', function(err, tx) { return bitcoinjs.getTransaction(txid, block ? block.hash : '', traverse, function(err, tx) {
if (err) return callback(err); if (err) return callback(err);
if (!block) { if (!block) {
bitcoin.db.set('tx-block/' + txHash, bitcoin.db.set('tx-block/' + txid,
{ hash: tx.blockhash }, utils.NOOP); { hash: tx.blockhash }, utils.NOOP);
} }
return callback(null, bitcoin.tx(tx)); return callback(null, bitcoin.tx(tx));