diff --git a/benchmark/script.js b/benchmark/script.js index 4b59c83..7442d1a 100644 --- a/benchmark/script.js +++ b/benchmark/script.js @@ -15,8 +15,6 @@ 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]; @@ -24,14 +22,12 @@ 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); } } } @@ -52,22 +48,6 @@ 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; @@ -79,8 +59,6 @@ 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 0856245..5bcdce6 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -754,18 +754,25 @@ Script.fromAddress = function(address) { * @return {Address|boolean} */ Script.prototype.getAddressInfo = function(opts) { - var info = this.getOutputAddressInfo(); - if (!info) { - return this.getInputAddressInfo(); + if (this._isInput) { + return this._getInputAddressInfo(); + } else if (this._isOutput) { + return this._getOutputAddressInfo(); + } else { + var info = this._getOutputAddressInfo(); + if (!info) { + return this._getInputAddressInfo(); + } + return info; } - return info; }; /** * Will return the associated output scriptPubKey address information object * @return {Address|boolean} + * @private */ -Script.prototype.getOutputAddressInfo = function() { +Script.prototype._getOutputAddressInfo = function() { var info = {}; if (this.isScriptHashOut()) { info.hashBuffer = this.getData(); @@ -782,8 +789,9 @@ Script.prototype.getOutputAddressInfo = function() { /** * Will return the associated input scriptSig address information object * @return {Address|boolean} + * @private */ -Script.prototype.getInputAddressInfo = function() { +Script.prototype._getInputAddressInfo = function() { var info = {}; if (this.isPublicKeyHashIn()) { // hash the publickey found in the scriptSig @@ -799,38 +807,17 @@ Script.prototype.getInputAddressInfo = function() { return info; }; -Script.prototype._toAddress = function(info, network) { - if (!info) { - return false; - } - info.network = Networks.get(network) || this._network || Networks.defaultNetwork; - 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); + var info = this.getAddressInfo(); + if (!info) { + return false; + } + info.network = Networks.get(network) || this._network || Networks.defaultNetwork; + return new Address(info); }; /** diff --git a/lib/transaction/input/input.js b/lib/transaction/input/input.js index afa169e..d810890 100644 --- a/lib/transaction/input/input.js +++ b/lib/transaction/input/input.js @@ -34,6 +34,7 @@ Object.defineProperty(Input.prototype, 'script', { } if (!this._script) { this._script = new Script(this._scriptBuffer); + this._script._isInput = true; } return this._script; } @@ -116,6 +117,7 @@ Input.prototype.setScript = function(script) { this._script = null; if (script instanceof Script) { this._script = script; + this._script._isInput = true; this._scriptBuffer = script.toBuffer(); } else if (JSUtil.isHexa(script)) { // hex string script @@ -123,6 +125,7 @@ Input.prototype.setScript = function(script) { } else if (_.isString(script)) { // human readable string script this._script = new Script(script); + this._script._isInput = true; this._scriptBuffer = this._script.toBuffer(); } else if (BufferUtil.isBuffer(script)) { // buffer script diff --git a/lib/transaction/output.js b/lib/transaction/output.js index 684b7d2..62af6b4 100644 --- a/lib/transaction/output.js +++ b/lib/transaction/output.js @@ -113,6 +113,7 @@ Output.prototype.setScriptFromBuffer = function(buffer) { this._scriptBuffer = buffer; try { this._script = Script.fromBuffer(this._scriptBuffer); + this._script._isOutput = true; } catch(e) { if (e instanceof errors.Script.InvalidBuffer) { this._script = null; @@ -126,9 +127,11 @@ Output.prototype.setScript = function(script) { if (script instanceof Script) { this._scriptBuffer = script.toBuffer(); this._script = script; + this._script._isOutput = true; } else if (_.isString(script)) { this._script = Script.fromString(script); this._scriptBuffer = this._script.toBuffer(); + this._script._isOutput = true; } else if (bufferUtil.isBuffer(script)) { this.setScriptFromBuffer(script); } else { diff --git a/test/transaction/transaction.js b/test/transaction/transaction.js index b828ec5..8980969 100644 --- a/test/transaction/transaction.js +++ b/test/transaction/transaction.js @@ -202,7 +202,9 @@ describe('Transaction', function() { transaction.outputs[1].satoshis.should.equal(40000); transaction.outputs[1].script.toString() .should.equal(Script.fromAddress(changeAddress).toString()); - transaction.getChangeOutput().script.should.deep.equal(Script.fromAddress(changeAddress)); + var actual = transaction.getChangeOutput().script.toString(); + var expected = Script.fromAddress(changeAddress).toString(); + actual.should.equal(expected); }); it('accepts a P2SH address for change', function() { var transaction = new Transaction()