Skip to content

Commit

Permalink
Merge pull request #1298 from braydonf/ispublickeyout
Browse files Browse the repository at this point in the history
Speed up isPublicKeyOut
  • Loading branch information
pnagurny committed Jul 22, 2015
2 parents 9a7cdc4 + ad9dd2a commit aeac467
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
9 changes: 9 additions & 0 deletions benchmark/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ async.series([
}
}

function isPublicKeyOut() {
if (c >= scripts.length) {
c = 0;
}
scripts[c].isPublicKeyOut();
c++;
}

function isPublicKeyHashIn() {
if (c >= scripts.length) {
c = 0;
Expand All @@ -58,6 +66,7 @@ async.series([

var suite = new benchmark.Suite();
suite.add('isPublicKeyHashIn', isPublicKeyHashIn, {maxTime: maxTime});
suite.add('isPublicKeyOut', isPublicKeyOut, {maxTime: maxTime});
suite.add('toAddress', toAddress, {maxTime: maxTime});
suite.add('getAddressInfo', getAddressInfo, {maxTime: maxTime});
suite
Expand Down
23 changes: 19 additions & 4 deletions lib/script/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,25 @@ Script.prototype.getPublicKeyHash = function() {
* @returns {boolean} if this is a public key output script
*/
Script.prototype.isPublicKeyOut = function() {
return this.chunks.length === 2 &&
BufferUtil.isBuffer(this.chunks[0].buf) &&
PublicKey.isValid(this.chunks[0].buf) &&
this.chunks[1].opcodenum === Opcode.OP_CHECKSIG;
if (this.chunks.length === 2 &&
this.chunks[0].buf &&
this.chunks[0].buf.length &&
this.chunks[1].opcodenum === Opcode.OP_CHECKSIG) {
var pubkeyBuf = this.chunks[0].buf;
var version = pubkeyBuf[0];
var isVersion = false;
if ((version === 0x04 ||
version === 0x06 ||
version === 0x07) && pubkeyBuf.length === 65) {
isVersion = true;
} else if ((version === 0x03 || version === 0x02) && pubkeyBuf.length === 33) {
isVersion = true;
}
if (isVersion) {
return PublicKey.isValid(pubkeyBuf);
}
}
return false;
};

/**
Expand Down

0 comments on commit aeac467

Please sign in to comment.