From 6d86c99314116a6c4b97d5977563d4cc209ffd97 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 8 Jul 2015 16:43:49 -0400 Subject: [PATCH] Seperate getAddressInfo for input or output only use. --- benchmark/script.js | 28 ++++++++++++++++++ lib/script/script.js | 67 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 84 insertions(+), 11 deletions(-) diff --git a/benchmark/script.js b/benchmark/script.js index 33229dc..4b59c83 100644 --- a/benchmark/script.js +++ b/benchmark/script.js @@ -15,6 +15,8 @@ async.series([ var c = 0; var scripts = []; + var inputScripts = []; + var outputScripts = []; var block = bitcore.Block.fromString(blockData); for (var i = 0; i < block.transactions.length; i++) { var tx = block.transactions[i]; @@ -22,6 +24,14 @@ async.series([ var input = tx.inputs[j]; if (input.script) { scripts.push(input.script); + inputScripts.push(input.script); + } + } + for (var k = 0; k < tx.outputs.length; k++) { + var output = tx.outputs[k]; + if (output.script) { + scripts.push(output.script); + outputScripts.push(output.script); } } } @@ -42,6 +52,22 @@ async.series([ c++; } + function toOutputAddress() { + if (c >= outputScripts.length) { + c = 0; + } + outputScripts[c].toOutputAddress(); + c++; + } + + function toInputAddress() { + if (c >= inputScripts.length) { + c = 0; + } + inputScripts[c].toInputAddress(); + c++; + } + function getAddressInfo() { if (c >= scripts.length) { c = 0; @@ -53,6 +79,8 @@ async.series([ var suite = new benchmark.Suite(); suite.add('isPublicKeyHashIn', isPublicKeyHashIn, {maxTime: maxTime}); suite.add('toAddress', toAddress, {maxTime: maxTime}); + suite.add('toInputAddress', toInputAddress, {maxTime: maxTime}); + suite.add('toOutputAddress', toOutputAddress, {maxTime: maxTime}); suite.add('getAddressInfo', getAddressInfo, {maxTime: maxTime}); suite .on('cycle', function(event) { diff --git a/lib/script/script.js b/lib/script/script.js index 6bffeb0..0856245 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -750,11 +750,22 @@ Script.fromAddress = function(address) { }; /** - * @param {Network=} network - * @return {Address|boolean} the associated address information object - * for this script if any, or false + * Will return the associated address information object + * @return {Address|boolean} + */ +Script.prototype.getAddressInfo = function(opts) { + var info = this.getOutputAddressInfo(); + if (!info) { + return this.getInputAddressInfo(); + } + return info; +}; + +/** + * Will return the associated output scriptPubKey address information object + * @return {Address|boolean} */ -Script.prototype.getAddressInfo = function() { +Script.prototype.getOutputAddressInfo = function() { var info = {}; if (this.isScriptHashOut()) { info.hashBuffer = this.getData(); @@ -762,7 +773,19 @@ Script.prototype.getAddressInfo = function() { } else if (this.isPublicKeyHashOut()) { info.hashBuffer = this.getData(); info.type = Address.PayToPublicKeyHash; - } else if (this.isPublicKeyHashIn()) { + } else { + return false; + } + return info; +}; + +/** + * Will return the associated input scriptSig address information object + * @return {Address|boolean} + */ +Script.prototype.getInputAddressInfo = function() { + var info = {}; + if (this.isPublicKeyHashIn()) { // hash the publickey found in the scriptSig info.hashBuffer = Hash.sha256ripemd160(this.chunks[1].buf); info.type = Address.PayToPublicKeyHash; @@ -775,12 +798,8 @@ Script.prototype.getAddressInfo = function() { } return info; }; -/** - * @param {Network=} network - * @return {Address|boolean} the associated address for this script if possible, or false - */ -Script.prototype.toAddress = function(network) { - var info = this.getAddressInfo(); + +Script.prototype._toAddress = function(info, network) { if (!info) { return false; } @@ -788,6 +807,32 @@ Script.prototype.toAddress = function(network) { return new Address(info); }; +/** + * @param {Network=} network + * @return {Address|boolean} the associated address for this script if possible, or false + */ +Script.prototype.toAddress = function(network) { + return this._toAddress(this.getAddressInfo(), network); +}; + +/** + * Will get the associated address for an output scriptPubKey + * @param {Network=} network + * @return {Address|boolean} + */ +Script.prototype.toOutputAddress = function(network) { + return this._toAddress(this.getOutputAddressInfo(), network); +}; + +/** + * Will get the associated address for an input scriptSig + * @param {Network=} network + * @return {Address|boolean} + */ +Script.prototype.toInputAddress = function(network) { + return this._toAddress(this.getInputAddressInfo(), network); +}; + /** * Analagous to bitcoind's FindAndDelete. Find and delete equivalent chunks, * typically used with push data chunks. Note that this will find and delete