Browse Source

Speed up isPublicKeyOut

patch-2
Braydon Fuller 10 years ago
parent
commit
9f4b1a2c26
  1. 9
      benchmark/script.js
  2. 19
      lib/script/script.js

9
benchmark/script.js

@ -32,6 +32,14 @@ async.series([
} }
} }
function isPublicKeyOut() {
if (c >= scripts.length) {
c = 0;
}
scripts[c].isPublicKeyOut();
c++;
}
function isPublicKeyHashIn() { function isPublicKeyHashIn() {
if (c >= scripts.length) { if (c >= scripts.length) {
c = 0; c = 0;
@ -58,6 +66,7 @@ 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('isPublicKeyOut', isPublicKeyOut, {maxTime: maxTime});
suite.add('toAddress', toAddress, {maxTime: maxTime}); suite.add('toAddress', toAddress, {maxTime: maxTime});
suite.add('getAddressInfo', getAddressInfo, {maxTime: maxTime}); suite.add('getAddressInfo', getAddressInfo, {maxTime: maxTime});
suite suite

19
lib/script/script.js

@ -276,10 +276,21 @@ Script.prototype.getPublicKeyHash = function() {
* @returns {boolean} if this is a public key output script * @returns {boolean} if this is a public key output script
*/ */
Script.prototype.isPublicKeyOut = function() { Script.prototype.isPublicKeyOut = function() {
return this.chunks.length === 2 && if (this.chunks.length === 2 &&
BufferUtil.isBuffer(this.chunks[0].buf) && this.chunks[0].buf &&
PublicKey.isValid(this.chunks[0].buf) && this.chunks[0].buf.length &&
this.chunks[1].opcodenum === Opcode.OP_CHECKSIG; this.chunks[1].opcodenum === Opcode.OP_CHECKSIG) {
var pubkeyBuf = this.chunks[0].buf;
var version = pubkeyBuf[0];
if ((version === 0x04 ||
version === 0x06 ||
version === 0x07) && pubkeyBuf.length === 65) {
return true;
} else if ((version === 0x03 || version === 0x02) && pubkeyBuf.length === 33) {
return true;
}
}
return false;
}; };
/** /**

Loading…
Cancel
Save