Seperate getAddressInfo for input or output only use.
This commit is contained in:
parent
e8446d4537
commit
6d86c99314
|
@ -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) {
|
||||
|
|
|
@ -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() {
|
||||
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.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
|
||||
|
|
Loading…
Reference in New Issue