Detect if scriptSig (input) or scriptPubKey (output) is previously known.
This commit is contained in:
parent
55afeb3eaf
commit
d9047eebf0
|
@ -15,8 +15,6 @@ async.series([
|
||||||
|
|
||||||
var c = 0;
|
var c = 0;
|
||||||
var scripts = [];
|
var scripts = [];
|
||||||
var inputScripts = [];
|
|
||||||
var outputScripts = [];
|
|
||||||
var block = bitcore.Block.fromString(blockData);
|
var block = bitcore.Block.fromString(blockData);
|
||||||
for (var i = 0; i < block.transactions.length; i++) {
|
for (var i = 0; i < block.transactions.length; i++) {
|
||||||
var tx = block.transactions[i];
|
var tx = block.transactions[i];
|
||||||
|
@ -24,14 +22,12 @@ async.series([
|
||||||
var input = tx.inputs[j];
|
var input = tx.inputs[j];
|
||||||
if (input.script) {
|
if (input.script) {
|
||||||
scripts.push(input.script);
|
scripts.push(input.script);
|
||||||
inputScripts.push(input.script);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (var k = 0; k < tx.outputs.length; k++) {
|
for (var k = 0; k < tx.outputs.length; k++) {
|
||||||
var output = tx.outputs[k];
|
var output = tx.outputs[k];
|
||||||
if (output.script) {
|
if (output.script) {
|
||||||
scripts.push(output.script);
|
scripts.push(output.script);
|
||||||
outputScripts.push(output.script);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,22 +48,6 @@ async.series([
|
||||||
c++;
|
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() {
|
function getAddressInfo() {
|
||||||
if (c >= scripts.length) {
|
if (c >= scripts.length) {
|
||||||
c = 0;
|
c = 0;
|
||||||
|
@ -79,8 +59,6 @@ async.series([
|
||||||
var suite = new benchmark.Suite();
|
var suite = new benchmark.Suite();
|
||||||
suite.add('isPublicKeyHashIn', isPublicKeyHashIn, {maxTime: maxTime});
|
suite.add('isPublicKeyHashIn', isPublicKeyHashIn, {maxTime: maxTime});
|
||||||
suite.add('toAddress', toAddress, {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.add('getAddressInfo', getAddressInfo, {maxTime: maxTime});
|
||||||
suite
|
suite
|
||||||
.on('cycle', function(event) {
|
.on('cycle', function(event) {
|
||||||
|
|
|
@ -754,18 +754,25 @@ Script.fromAddress = function(address) {
|
||||||
* @return {Address|boolean}
|
* @return {Address|boolean}
|
||||||
*/
|
*/
|
||||||
Script.prototype.getAddressInfo = function(opts) {
|
Script.prototype.getAddressInfo = function(opts) {
|
||||||
var info = this.getOutputAddressInfo();
|
if (this._isInput) {
|
||||||
if (!info) {
|
return this._getInputAddressInfo();
|
||||||
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
|
* Will return the associated output scriptPubKey address information object
|
||||||
* @return {Address|boolean}
|
* @return {Address|boolean}
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
Script.prototype.getOutputAddressInfo = function() {
|
Script.prototype._getOutputAddressInfo = function() {
|
||||||
var info = {};
|
var info = {};
|
||||||
if (this.isScriptHashOut()) {
|
if (this.isScriptHashOut()) {
|
||||||
info.hashBuffer = this.getData();
|
info.hashBuffer = this.getData();
|
||||||
|
@ -782,8 +789,9 @@ Script.prototype.getOutputAddressInfo = function() {
|
||||||
/**
|
/**
|
||||||
* Will return the associated input scriptSig address information object
|
* Will return the associated input scriptSig address information object
|
||||||
* @return {Address|boolean}
|
* @return {Address|boolean}
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
Script.prototype.getInputAddressInfo = function() {
|
Script.prototype._getInputAddressInfo = function() {
|
||||||
var info = {};
|
var info = {};
|
||||||
if (this.isPublicKeyHashIn()) {
|
if (this.isPublicKeyHashIn()) {
|
||||||
// hash the publickey found in the scriptSig
|
// hash the publickey found in the scriptSig
|
||||||
|
@ -799,38 +807,17 @@ Script.prototype.getInputAddressInfo = function() {
|
||||||
return info;
|
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
|
* @param {Network=} network
|
||||||
* @return {Address|boolean} the associated address for this script if possible, or false
|
* @return {Address|boolean} the associated address for this script if possible, or false
|
||||||
*/
|
*/
|
||||||
Script.prototype.toAddress = function(network) {
|
Script.prototype.toAddress = function(network) {
|
||||||
return this._toAddress(this.getAddressInfo(), network);
|
var info = this.getAddressInfo();
|
||||||
};
|
if (!info) {
|
||||||
|
return false;
|
||||||
/**
|
}
|
||||||
* Will get the associated address for an output scriptPubKey
|
info.network = Networks.get(network) || this._network || Networks.defaultNetwork;
|
||||||
* @param {Network=} network
|
return new Address(info);
|
||||||
* @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);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -34,6 +34,7 @@ Object.defineProperty(Input.prototype, 'script', {
|
||||||
}
|
}
|
||||||
if (!this._script) {
|
if (!this._script) {
|
||||||
this._script = new Script(this._scriptBuffer);
|
this._script = new Script(this._scriptBuffer);
|
||||||
|
this._script._isInput = true;
|
||||||
}
|
}
|
||||||
return this._script;
|
return this._script;
|
||||||
}
|
}
|
||||||
|
@ -116,6 +117,7 @@ Input.prototype.setScript = function(script) {
|
||||||
this._script = null;
|
this._script = null;
|
||||||
if (script instanceof Script) {
|
if (script instanceof Script) {
|
||||||
this._script = script;
|
this._script = script;
|
||||||
|
this._script._isInput = true;
|
||||||
this._scriptBuffer = script.toBuffer();
|
this._scriptBuffer = script.toBuffer();
|
||||||
} else if (JSUtil.isHexa(script)) {
|
} else if (JSUtil.isHexa(script)) {
|
||||||
// hex string script
|
// hex string script
|
||||||
|
@ -123,6 +125,7 @@ Input.prototype.setScript = function(script) {
|
||||||
} else if (_.isString(script)) {
|
} else if (_.isString(script)) {
|
||||||
// human readable string script
|
// human readable string script
|
||||||
this._script = new Script(script);
|
this._script = new Script(script);
|
||||||
|
this._script._isInput = true;
|
||||||
this._scriptBuffer = this._script.toBuffer();
|
this._scriptBuffer = this._script.toBuffer();
|
||||||
} else if (BufferUtil.isBuffer(script)) {
|
} else if (BufferUtil.isBuffer(script)) {
|
||||||
// buffer script
|
// buffer script
|
||||||
|
|
|
@ -113,6 +113,7 @@ Output.prototype.setScriptFromBuffer = function(buffer) {
|
||||||
this._scriptBuffer = buffer;
|
this._scriptBuffer = buffer;
|
||||||
try {
|
try {
|
||||||
this._script = Script.fromBuffer(this._scriptBuffer);
|
this._script = Script.fromBuffer(this._scriptBuffer);
|
||||||
|
this._script._isOutput = true;
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
if (e instanceof errors.Script.InvalidBuffer) {
|
if (e instanceof errors.Script.InvalidBuffer) {
|
||||||
this._script = null;
|
this._script = null;
|
||||||
|
@ -126,9 +127,11 @@ Output.prototype.setScript = function(script) {
|
||||||
if (script instanceof Script) {
|
if (script instanceof Script) {
|
||||||
this._scriptBuffer = script.toBuffer();
|
this._scriptBuffer = script.toBuffer();
|
||||||
this._script = script;
|
this._script = script;
|
||||||
|
this._script._isOutput = true;
|
||||||
} else if (_.isString(script)) {
|
} else if (_.isString(script)) {
|
||||||
this._script = Script.fromString(script);
|
this._script = Script.fromString(script);
|
||||||
this._scriptBuffer = this._script.toBuffer();
|
this._scriptBuffer = this._script.toBuffer();
|
||||||
|
this._script._isOutput = true;
|
||||||
} else if (bufferUtil.isBuffer(script)) {
|
} else if (bufferUtil.isBuffer(script)) {
|
||||||
this.setScriptFromBuffer(script);
|
this.setScriptFromBuffer(script);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -202,7 +202,9 @@ describe('Transaction', function() {
|
||||||
transaction.outputs[1].satoshis.should.equal(40000);
|
transaction.outputs[1].satoshis.should.equal(40000);
|
||||||
transaction.outputs[1].script.toString()
|
transaction.outputs[1].script.toString()
|
||||||
.should.equal(Script.fromAddress(changeAddress).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() {
|
it('accepts a P2SH address for change', function() {
|
||||||
var transaction = new Transaction()
|
var transaction = new Transaction()
|
||||||
|
|
Loading…
Reference in New Issue