From dca7a38362010e201b55c5185a204649a698109f Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 29 Sep 2015 18:41:58 +1000 Subject: [PATCH 1/4] tests: remove unnecessary branch, fix bscript.fromHex undefined --- test/script.js | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/test/script.js b/test/script.js index b8e43eb..ccb009f 100644 --- a/test/script.js +++ b/test/script.js @@ -149,19 +149,17 @@ describe('script', function () { if (!(inputFnName in fixtures.invalid)) return fixtures.invalid[inputFnName].forEach(function (f) { - if (inputFn && (f.scriptSig || f.scriptSigHex)) { - it('returns false for ' + f.description + ' (' + (f.scriptSig || f.scriptSigHex) + ')', function () { - var scriptSig + it('returns false for ' + f.description + ' (' + (f.scriptSig || f.scriptSigHex) + ')', function () { + var scriptSig - if (f.scriptSig) { - scriptSig = bscript.fromASM(f.scriptSig) - } else { - scriptSig = bscript.fromHex(f.scriptSigHex) - } + if (f.scriptSig) { + scriptSig = bscript.fromASM(f.scriptSig) + } else { + scriptSig = new Buffer(f.scriptSigHex, 'hex') + } - assert.strictEqual(inputFn(scriptSig), false) - }) - } + assert.strictEqual(inputFn(scriptSig), false) + }) }) }) @@ -181,13 +179,17 @@ describe('script', function () { if (!(outputFnName in fixtures.invalid)) return fixtures.invalid[outputFnName].forEach(function (f) { - if (outputFn && f.scriptPubKey) { - it('returns false for ' + f.description + ' (' + f.scriptPubKey + ')', function () { - var scriptPubKey = bscript.fromASM(f.scriptPubKey) + it('returns false for ' + f.description + ' (' + (f.scriptPubKey || f.scriptPubKeyHex) + ')', function () { + var scriptPubKey - assert.strictEqual(outputFn(scriptPubKey), false) - }) - } + if (f.scriptPubKey) { + scriptPubKey = bscript.fromASM(f.scriptPubKey) + } else { + scriptPubKey = new Buffer(f.scriptPubKeyHex, 'hex') + } + + assert.strictEqual(outputFn(scriptPubKey), false) + }) }) }) }) From 50f381fb5c31250ba1d2efe5bafa75e2e54b2803 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 29 Sep 2015 18:41:32 +1000 Subject: [PATCH 2/4] tests: add non-BIP62 compliant is*Output fixtures --- test/fixtures/script.json | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/fixtures/script.json b/test/fixtures/script.json index 8abdcd1..797186f 100644 --- a/test/fixtures/script.json +++ b/test/fixtures/script.json @@ -200,6 +200,18 @@ "scriptPubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1ffffff OP_CHECKSIG" } ], + "isPubKeyHashOutput": [ + { + "description": "non-minimal encoded isPubKeyHashOutput (non BIP62 compliant)", + "scriptPubKeyHex": "76a94c14aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac" + } + ], + "isScriptHashOutput": [ + { + "description": "non-minimal encoded isScriptHashOutput (non BIP62 compliant)", + "scriptPubKeyHex": "a94c14c286a1af0947f58d1ad787385b1c2c4a976f9e7187" + } + ], "isMultisigOutput": [ { "description": "OP_CHECKMULTISIG not found", From f60cb2e49128f847384693a425e8af367192aee8 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 29 Sep 2015 18:55:33 +1000 Subject: [PATCH 3/4] scripts: ensure isPubKeyHashOutput/isScriptHashOutput adhere to BIP62 --- src/script.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/script.js b/src/script.js index 3239e37..5be3c68 100644 --- a/src/script.js +++ b/src/script.js @@ -156,15 +156,14 @@ function isPubKeyHashInput (script) { } function isPubKeyHashOutput (script) { - var chunks = decompile(script) - - return chunks.length === 5 && - chunks[0] === OPS.OP_DUP && - chunks[1] === OPS.OP_HASH160 && - Buffer.isBuffer(chunks[2]) && - chunks[2].length === 20 && - chunks[3] === OPS.OP_EQUALVERIFY && - chunks[4] === OPS.OP_CHECKSIG + var buffer = compile(script) + + return buffer.length === 25 && + buffer[0] === OPS.OP_DUP && + buffer[1] === OPS.OP_HASH160 && + buffer[2] === 0x14 && + buffer[23] === OPS.OP_EQUALVERIFY && + buffer[24] === OPS.OP_CHECKSIG } function isPubKeyInput (script) { @@ -199,13 +198,12 @@ function isScriptHashInput (script, allowIncomplete) { } function isScriptHashOutput (script) { - var chunks = decompile(script) + var buffer = compile(script) - return chunks.length === 3 && - chunks[0] === OPS.OP_HASH160 && - Buffer.isBuffer(chunks[1]) && - chunks[1].length === 20 && - chunks[2] === OPS.OP_EQUAL + return buffer.length === 23 && + buffer[0] === OPS.OP_HASH160 && + buffer[1] === 0x14 && + buffer[22] === OPS.OP_EQUAL } // allowIncomplete is to account for combining signatures From d48a7ab6820ad06859334988cafc70b9a7edf375 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 30 Sep 2015 10:32:46 +1000 Subject: [PATCH 4/4] address: avoid unnecessary unchunking --- src/address.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/address.js b/src/address.js index d6924b3..e631401 100644 --- a/src/address.js +++ b/src/address.js @@ -18,11 +18,10 @@ function fromBase58Check (address) { function fromOutputScript (scriptPubKey, network) { network = network || networks.bitcoin - var chunks = bscript.decompile(scriptPubKey) - if (bscript.isPubKeyHashOutput(chunks)) return toBase58Check(chunks[2], network.pubKeyHash) - if (bscript.isScriptHashOutput(chunks)) return toBase58Check(chunks[1], network.scriptHash) + if (bscript.isPubKeyHashOutput(scriptPubKey)) return toBase58Check(scriptPubKey.slice(3, 23), network.pubKeyHash) + if (bscript.isScriptHashOutput(scriptPubKey)) return toBase58Check(scriptPubKey.slice(2, 22), network.scriptHash) - throw new Error(bscript.toASM(chunks) + ' has no matching Address') + throw new Error(bscript.toASM(scriptPubKey) + ' has no matching Address') } function toBase58Check (hash, version) {