From 35e0956ed9e93ee0767dd6c28993a7f164050cd9 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 3 Jul 2018 22:33:48 +1000 Subject: [PATCH 1/3] payments/p2ms: add const to p2ms --- src/payments/p2ms.js | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/payments/p2ms.js b/src/payments/p2ms.js index a17d422..15de44e 100644 --- a/src/payments/p2ms.js +++ b/src/payments/p2ms.js @@ -1,11 +1,11 @@ -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 -let OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1 +const bscript = require('../script') +const BITCOIN_NETWORK = require('../networks').bitcoin +const OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1 function stacksEqual (a, b) { if (a.length !== b.length) return false @@ -41,8 +41,8 @@ function p2ms (a, opts) { input: typef.maybe(typef.Buffer) }, a) - let network = a.network || BITCOIN_NETWORK - let o = { network } + const network = a.network || BITCOIN_NETWORK + const o = { network } let chunks let decoded = false @@ -50,10 +50,8 @@ function p2ms (a, opts) { if (decoded) return decoded = true chunks = bscript.decompile(output) - let om = chunks[0] - OP_INT_BASE - let on = chunks[chunks.length - 2] - OP_INT_BASE - o.m = om - o.n = on + o.m = chunks[0] - OP_INT_BASE + o.n = chunks[chunks.length - 2] - OP_INT_BASE o.pubkeys = chunks.slice(1, -2) } From 0d9619aeeddaf35c599db4719c2c468bebedce34 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sat, 14 Jul 2018 20:13:50 +1000 Subject: [PATCH 2/3] payments/p2wpkh: fix exception messages --- src/payments/p2wpkh.js | 6 +++--- test/fixtures/p2wpkh.json | 10 ++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/payments/p2wpkh.js b/src/payments/p2wpkh.js index bd010f7..b11a0bb 100644 --- a/src/payments/p2wpkh.js +++ b/src/payments/p2wpkh.js @@ -118,9 +118,9 @@ function p2wpkh (a, opts) { } if (a.witness) { - if (a.witness.length !== 2) throw new TypeError('Input is invalid') - if (!bscript.isCanonicalScriptSignature(a.witness[0])) throw new TypeError('Input has invalid signature') - if (!ecc.isPoint(a.witness[1])) throw new TypeError('Input has invalid pubkey') + if (a.witness.length !== 2) throw new TypeError('Witness is invalid') + if (!bscript.isCanonicalScriptSignature(a.witness[0])) throw new TypeError('Witness has invalid signature') + if (!ecc.isPoint(a.witness[1])) throw new TypeError('Witness has invalid pubkey') if (a.signature && !a.signature.equals(a.witness[0])) throw new TypeError('Signature mismatch') if (a.pubkey && !a.pubkey.equals(a.witness[1])) throw new TypeError('Pubkey mismatch') diff --git a/test/fixtures/p2wpkh.json b/test/fixtures/p2wpkh.json index 84e0c08..3a53525 100644 --- a/test/fixtures/p2wpkh.json +++ b/test/fixtures/p2wpkh.json @@ -148,7 +148,13 @@ } }, { - "exception": "Input has invalid signature", + "exception": "Witness is invalid", + "arguments": { + "witness": [] + } + }, + { + "exception": "Witness has invalid signature", "arguments": { "witness": [ "ffffffffffffffffff", @@ -157,7 +163,7 @@ } }, { - "exception": "Input has invalid pubkey", + "exception": "Witness has invalid pubkey", "arguments": { "witness": [ "300602010002010001", From 7104bb412ed4b765af892f789012410a234cbd7a Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sat, 14 Jul 2018 20:24:11 +1000 Subject: [PATCH 3/3] tests/payments: add missing tests --- src/payments/p2pkh.js | 4 ++-- src/payments/p2sh.js | 2 +- src/payments/p2wpkh.js | 22 +++++++++++----------- test/fixtures/p2pkh.json | 18 ++++++++++++++++++ test/fixtures/p2sh.json | 33 +++++++++++++++++++++++++++++++++ test/fixtures/p2wpkh.json | 19 +++++++++++++++++++ 6 files changed, 84 insertions(+), 14 deletions(-) diff --git a/src/payments/p2pkh.js b/src/payments/p2pkh.js index 140f707..08a4329 100644 --- a/src/payments/p2pkh.js +++ b/src/payments/p2pkh.js @@ -87,9 +87,9 @@ function p2pkh (a, opts) { if (opts.validate) { let hash if (a.address) { - if (_address().version !== network.pubKeyHash) throw new TypeError('Network mismatch') + if (_address().version !== network.pubKeyHash) 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) { diff --git a/src/payments/p2sh.js b/src/payments/p2sh.js index 9b28a3a..7b95a45 100644 --- a/src/payments/p2sh.js +++ b/src/payments/p2sh.js @@ -109,7 +109,7 @@ function p2sh (a, opts) { if (opts.validate) { let hash if (a.address) { - if (_address().version !== network.scriptHash) throw new TypeError('Network mismatch') + 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 } diff --git a/src/payments/p2wpkh.js b/src/payments/p2wpkh.js index b11a0bb..ba42ba1 100644 --- a/src/payments/p2wpkh.js +++ b/src/payments/p2wpkh.js @@ -90,17 +90,11 @@ function p2wpkh (a, opts) { if (opts.validate) { let hash if (a.address) { - if (network && network.bech32 !== _address().prefix) throw new TypeError('Network mismatch') - if (_address().version !== 0x00) throw new TypeError('Invalid version') - if (_address().data.length !== 20) throw new TypeError('Invalid data') - if (hash && !hash.equals(_address().data)) throw new TypeError('Hash mismatch') - else hash = _address().data - } - - if (a.pubkey) { - const pkh = bcrypto.hash160(a.pubkey) - if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch') - else hash = pkh + 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 } if (a.hash) { @@ -117,6 +111,12 @@ function p2wpkh (a, opts) { else hash = a.output.slice(2) } + if (a.pubkey) { + const pkh = bcrypto.hash160(a.pubkey) + if (hash && !hash.equals(pkh)) throw new TypeError('Hash mismatch') + else hash = pkh + } + if (a.witness) { if (a.witness.length !== 2) throw new TypeError('Witness is invalid') if (!bscript.isCanonicalScriptSignature(a.witness[0])) throw new TypeError('Witness has invalid signature') diff --git a/test/fixtures/p2pkh.json b/test/fixtures/p2pkh.json index 7d47152..d16b181 100644 --- a/test/fixtures/p2pkh.json +++ b/test/fixtures/p2pkh.json @@ -121,6 +121,24 @@ "outputHex": "76a94c14aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac" } }, + { + "exception": "Invalid version or Network mismatch", + "arguments": { + "address": "3LRW7jeCvQCRdPF8S3yUCfRAx4eqXFmdcr" + } + }, + { + "exception": "Invalid address", + "arguments": { + "address": "111111111111111111117K4nzc" + } + }, + { + "exception": "Invalid address", + "arguments": { + "address": "111111111111111111111111133izVn" + } + }, { "exception": "Pubkey mismatch", "arguments": { diff --git a/test/fixtures/p2sh.json b/test/fixtures/p2sh.json index c87c8a8..44611a5 100644 --- a/test/fixtures/p2sh.json +++ b/test/fixtures/p2sh.json @@ -227,6 +227,24 @@ } } }, + { + "exception": "Invalid version or Network mismatch", + "arguments": { + "address": "134D6gYy8DsR5m4416BnmgASuMBqKvogQh" + } + }, + { + "exception": "Invalid address", + "arguments": { + "address": "TYPjCiGbKiwP6r12cdkmVjySbQr7mb3r" + } + }, + { + "exception": "Invalid address", + "arguments": { + "address": "EDaBpuERpLssFzbCV1kgy8tKJsHrcwmzY7HDMF2" + } + }, { "exception": "Input too short", "arguments": { @@ -280,6 +298,21 @@ "inputHex": "021000" } }, + { + "exception": "Witness and redeem.witness mismatch", + "arguments": { + "witness": [ + "3045ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "030000000000000000000000000000000000000000000000000000000000000001" + ], + "redeem": { + "witness": [ + "3045dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "030000000000000000000000000000000000000000000000000000000000000001" + ] + } + } + }, { "exception": "Hash mismatch", "arguments": { diff --git a/test/fixtures/p2wpkh.json b/test/fixtures/p2wpkh.json index 3a53525..b0aa4ff 100644 --- a/test/fixtures/p2wpkh.json +++ b/test/fixtures/p2wpkh.json @@ -116,6 +116,18 @@ ] } }, + { + "exception": "Invalid prefix or Network mismatch", + "arguments": { + "address": "tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7" + } + }, + { + "exception": "Invalid address version", + "arguments": { + "address": "bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7k7grplx" + } + }, { "exception": "Hash mismatch", "arguments": { @@ -137,6 +149,13 @@ "hash": "ffffffffffffffffffffffffffffffffffffffff" } }, + { + "exception": "Hash mismatch", + "arguments": { + "hash": "ffffffffffffffffffffffffffffffffffffffff", + "pubkey": "030000000000000000000000000000000000000000000000000000000000000001" + } + }, { "exception": "Hash mismatch", "arguments": {