From cf9a35f59bc6346bf4c6bee9466c58683fd55f2b Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 27 Jul 2018 20:14:11 +1000 Subject: [PATCH 1/6] payments: more const --- src/payments/p2pk.js | 18 +++++++++--------- src/payments/p2pkh.js | 11 ++++++----- src/payments/p2sh.js | 3 ++- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/payments/p2pk.js b/src/payments/p2pk.js index 9cddc81..9e12749 100644 --- a/src/payments/p2pk.js +++ b/src/payments/p2pk.js @@ -1,10 +1,10 @@ -let lazy = require('./lazy') -let typef = require('typeforce') -let OPS = require('bitcoin-ops') -let ecc = require('tiny-secp256k1') +const lazy = require('./lazy') +const typef = require('typeforce') +const OPS = require('bitcoin-ops') +const ecc = require('tiny-secp256k1') -let bscript = require('../script') -let BITCOIN_NETWORK = require('../networks').bitcoin +const bscript = require('../script') +const BITCOIN_NETWORK = require('../networks').bitcoin // input: {signature} // output: {pubKey} OP_CHECKSIG @@ -27,10 +27,10 @@ function p2pk (a, opts) { input: typef.maybe(typef.Buffer) }, a) - let _chunks = lazy.value(function () { return bscript.decompile(a.input) }) + const _chunks = lazy.value(function () { return bscript.decompile(a.input) }) - let network = a.network || BITCOIN_NETWORK - let o = { network } + const network = a.network || BITCOIN_NETWORK + const o = { network } lazy.prop(o, 'output', function () { if (!a.pubkey) return diff --git a/src/payments/p2pkh.js b/src/payments/p2pkh.js index 08a4329..0ab9fa0 100644 --- a/src/payments/p2pkh.js +++ b/src/payments/p2pkh.js @@ -106,18 +106,19 @@ function p2pkh (a, opts) { a.output[23] !== OPS.OP_EQUALVERIFY || a.output[24] !== OPS.OP_CHECKSIG) throw new TypeError('Output is invalid') - if (hash && !hash.equals(a.output.slice(3, 23))) throw new TypeError('Hash mismatch') - else hash = a.output.slice(3, 23) + const hash2 = a.output.slice(3, 23) + if (hash && !hash.equals(hash2)) throw new TypeError('Hash mismatch') + else hash = hash2 } if (a.pubkey) { - let pkh = bcrypto.hash160(a.pubkey) + const pkh = bcrypto.hash160(a.pubkey) if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch') else hash = pkh } if (a.input) { - let chunks = _chunks() + const chunks = _chunks() if (chunks.length !== 2) throw new TypeError('Input is invalid') if (!bscript.isCanonicalScriptSignature(chunks[0])) throw new TypeError('Input has invalid signature') if (!ecc.isPoint(chunks[1])) throw new TypeError('Input has invalid pubkey') @@ -125,7 +126,7 @@ function p2pkh (a, opts) { if (a.signature && !a.signature.equals(chunks[0])) throw new TypeError('Signature mismatch') if (a.pubkey && !a.pubkey.equals(chunks[1])) throw new TypeError('Pubkey mismatch') - let pkh = bcrypto.hash160(chunks[1]) + const pkh = bcrypto.hash160(chunks[1]) if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch') } } diff --git a/src/payments/p2sh.js b/src/payments/p2sh.js index 7b95a45..9c80b09 100644 --- a/src/payments/p2sh.js +++ b/src/payments/p2sh.js @@ -111,7 +111,7 @@ function p2sh (a, opts) { if (a.address) { if (_address().version !== network.scriptHash) throw new TypeError('Invalid version or Network mismatch') if (_address().hash.length !== 20) throw new TypeError('Invalid address') - else hash = _address().hash + hash = _address().hash } if (a.hash) { @@ -125,6 +125,7 @@ function p2sh (a, opts) { a.output[0] !== OPS.OP_HASH160 || a.output[1] !== 0x14 || a.output[22] !== OPS.OP_EQUAL) throw new TypeError('Output is invalid') + const hash2 = a.output.slice(2, 22) if (hash && !hash.equals(hash2)) throw new TypeError('Hash mismatch') else hash = hash2 From 54ec449a751faa55d607d70d02904ad27d6120c0 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 27 Jul 2018 20:20:28 +1000 Subject: [PATCH 2/6] payments/p2sh: try not to rely on o. in validation --- src/payments/p2sh.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/payments/p2sh.js b/src/payments/p2sh.js index 9c80b09..b642154 100644 --- a/src/payments/p2sh.js +++ b/src/payments/p2sh.js @@ -58,7 +58,7 @@ function p2sh (a, opts) { const _redeem = lazy.value(function () { const chunks = _chunks() return { - network: network, + network, output: chunks[chunks.length - 1], input: bscript.compile(chunks.slice(0, -1)), witness: a.witness || [] @@ -166,9 +166,10 @@ function p2sh (a, opts) { if (a.redeem) { if (a.redeem.network && a.redeem.network !== network) throw new TypeError('Network mismatch') - if (o.redeem) { - if (a.redeem.output && !a.redeem.output.equals(o.redeem.output)) throw new TypeError('Redeem.output mismatch') - if (a.redeem.input && !a.redeem.input.equals(o.redeem.input)) throw new TypeError('Redeem.input mismatch') + if (a.input) { + const redeem = _redeem() + if (a.redeem.output && !a.redeem.output.equals(redeem.output)) throw new TypeError('Redeem.output mismatch') + if (a.redeem.input && !a.redeem.input.equals(redeem.input)) throw new TypeError('Redeem.input mismatch') } checkRedeem(a.redeem) From 3ed77c4820d3e5ddf4bb38fe06da6d63ea67b22e Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 27 Jul 2018 20:22:11 +1000 Subject: [PATCH 3/6] payments: cleanup --- src/payments/p2pk.js | 7 ++----- src/payments/p2wpkh.js | 1 - src/payments/p2wsh.js | 2 +- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/payments/p2pk.js b/src/payments/p2pk.js index 9e12749..5f65030 100644 --- a/src/payments/p2pk.js +++ b/src/payments/p2pk.js @@ -58,13 +58,10 @@ function p2pk (a, opts) { // extended validation if (opts.validate) { - if (a.pubkey && a.output) { - if (!a.pubkey.equals(o.pubkey)) throw new TypeError('Pubkey mismatch') - } - if (a.output) { if (a.output[a.output.length - 1] !== OPS.OP_CHECKSIG) throw new TypeError('Output is invalid') if (!ecc.isPoint(o.pubkey)) throw new TypeError('Output pubkey is invalid') + if (a.pubkey && !a.pubkey.equals(o.pubkey)) throw new TypeError('Pubkey mismatch') } if (a.signature) { @@ -73,7 +70,7 @@ function p2pk (a, opts) { if (a.input) { if (_chunks().length !== 1) throw new TypeError('Input is invalid') - if (!bscript.isCanonicalScriptSignature(_chunks()[0])) throw new TypeError('Input has invalid signature') + if (!bscript.isCanonicalScriptSignature(o.signature)) throw new TypeError('Input has invalid signature') } } diff --git a/src/payments/p2wpkh.js b/src/payments/p2wpkh.js index ba42ba1..c47c354 100644 --- a/src/payments/p2wpkh.js +++ b/src/payments/p2wpkh.js @@ -93,7 +93,6 @@ function p2wpkh (a, opts) { if (network && network.bech32 !== _address().prefix) throw new TypeError('Invalid prefix or Network mismatch') if (_address().version !== 0x00) throw new TypeError('Invalid address version') if (_address().data.length !== 20) throw new TypeError('Invalid address data') - // if (hash && !hash.equals(_address().data)) throw new TypeError('Hash mismatch') hash = _address().data } diff --git a/src/payments/p2wsh.js b/src/payments/p2wsh.js index c84d822..8c45022 100644 --- a/src/payments/p2wsh.js +++ b/src/payments/p2wsh.js @@ -122,7 +122,7 @@ function p2wsh (a, opts) { if (_address().prefix !== network.bech32) throw new TypeError('Invalid prefix or Network mismatch') if (_address().version !== 0x00) throw new TypeError('Invalid address version') if (_address().data.length !== 32) throw new TypeError('Invalid address data') - else hash = _address().data + hash = _address().data } if (a.hash) { From 55207e57427b30ae4380d30dd33e867ac6e1b773 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 27 Jul 2018 20:44:02 +1000 Subject: [PATCH 4/6] tests/payments/p2pk*: add signature mismatch tests --- src/payments/p2pk.js | 2 +- test/fixtures/p2pk.json | 13 +++++++++++++ test/fixtures/p2pkh.json | 7 +++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/payments/p2pk.js b/src/payments/p2pk.js index 5f65030..4073d52 100644 --- a/src/payments/p2pk.js +++ b/src/payments/p2pk.js @@ -65,7 +65,7 @@ function p2pk (a, opts) { } if (a.signature) { - if (a.input && !a.input.equals(o.input)) throw new TypeError('Input mismatch') + if (a.input && !a.input.equals(o.input)) throw new TypeError('Signature mismatch') } if (a.input) { diff --git a/test/fixtures/p2pk.json b/test/fixtures/p2pk.json index 7ede5f4..a07c5b8 100644 --- a/test/fixtures/p2pk.json +++ b/test/fixtures/p2pk.json @@ -116,6 +116,19 @@ "pubkey": "030000000000000000000000000000000000000000000000000000000000000001", "input": "ffffffffffffffff" } + }, + { + "exception": "Input has invalid signature", + "arguments": { + "input": "30060201ff0201ff01" + } + }, + { + "exception": "Signature mismatch", + "arguments": { + "signature": "300602010002010001", + "input": "300602010302010301" + } } ], "dynamic": { diff --git a/test/fixtures/p2pkh.json b/test/fixtures/p2pkh.json index d16b181..118111d 100644 --- a/test/fixtures/p2pkh.json +++ b/test/fixtures/p2pkh.json @@ -204,6 +204,13 @@ "hash": "ffffffffffffffffffffffffffffffffffffffff", "input": "300602010002010001 030000000000000000000000000000000000000000000000000000000000000001" } + }, + { + "exception": "Signature mismatch", + "arguments": { + "signature": "300602010002010001", + "input": "300602010302010301 030000000000000000000000000000000000000000000000000000000000000001" + } } ], "dynamic": { From 1051946f0014b3ff701ec0f64e3ef0e579c0635f Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 27 Jul 2018 20:44:20 +1000 Subject: [PATCH 5/6] tests/payments/p2pk: fix wrong fixture names --- test/fixtures/p2pk.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/fixtures/p2pk.json b/test/fixtures/p2pk.json index a07c5b8..a9e1063 100644 --- a/test/fixtures/p2pk.json +++ b/test/fixtures/p2pk.json @@ -7,7 +7,7 @@ }, "expected": { "pubkey": "030000000000000000000000000000000000000000000000000000000000000001", - "signatures": null, + "signature": null, "input": null, "witness": null } @@ -19,7 +19,7 @@ }, "expected": { "output": "030000000000000000000000000000000000000000000000000000000000000001 OP_CHECKSIG", - "signatures": null, + "signature": null, "input": null, "witness": null } From cd9e6d1d5e6bf219a86dc4bd555e1c26733cdcae Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 27 Jul 2018 20:59:13 +1000 Subject: [PATCH 6/6] tests/p2wsh: add secondary stacksEqual test --- test/fixtures/p2wsh.json | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/fixtures/p2wsh.json b/test/fixtures/p2wsh.json index 9579f64..2f7b9cc 100644 --- a/test/fixtures/p2wsh.json +++ b/test/fixtures/p2wsh.json @@ -269,6 +269,20 @@ ] } }, + { + "exception": "Witness and redeem.witness mismatch", + "arguments": { + "redeem": { + "output": "OP_TRUE", + "witness": [ + "04000000ff" + ] + }, + "witness": [ + "04000000ee" + ] + } + }, { "exception": "Ambiguous witness source", "arguments": {