From 9f4b1a2c26051d3e800a0f81396a02549648ce68 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 13 Jul 2015 18:51:52 -0400 Subject: [PATCH 1/2] Speed up isPublicKeyOut --- benchmark/script.js | 9 +++++++++ lib/script/script.js | 19 +++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/benchmark/script.js b/benchmark/script.js index 7442d1a..1ec27f3 100644 --- a/benchmark/script.js +++ b/benchmark/script.js @@ -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; @@ -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 diff --git a/lib/script/script.js b/lib/script/script.js index 5bcdce6..0fe1435 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -276,10 +276,21 @@ 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]; + 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; }; /** From ad9dd2a41fd79ee706c08dfaef2e2fbd4fa90fd4 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 14 Jul 2015 09:58:05 -0400 Subject: [PATCH 2/2] Check that the public key is valid for outputs. --- lib/script/script.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/script/script.js b/lib/script/script.js index 0fe1435..4893f71 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -282,12 +282,16 @@ Script.prototype.isPublicKeyOut = function() { 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) { - return true; + isVersion = true; } else if ((version === 0x03 || version === 0x02) && pubkeyBuf.length === 33) { - return true; + isVersion = true; + } + if (isVersion) { + return PublicKey.isValid(pubkeyBuf); } } return false;