Detect if scriptSig (input) or scriptPubKey (output) is previously known.

This commit is contained in:
Braydon Fuller 2015-07-09 11:26:09 -04:00
parent 55afeb3eaf
commit d9047eebf0
5 changed files with 29 additions and 56 deletions

View File

@ -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) {

View File

@ -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);
};
/**

View File

@ -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

View File

@ -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 {

View File

@ -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()