From df2a66c852546a273f33d7a997c46945202b7155 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 26 Sep 2018 16:02:39 +1000 Subject: [PATCH 1/4] tests/payments: fix redeem fixture mutation --- test/payments.utils.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/payments.utils.js b/test/payments.utils.js index 219d086..485bf03 100644 --- a/test/payments.utils.js +++ b/test/payments.utils.js @@ -1,6 +1,6 @@ const t = require('assert') const bscript = require('../src/script') -const bnetworks = require('../src/networks') +const BNETWORKS = require('../src/networks') function tryHex (x) { if (Buffer.isBuffer(x)) return x.toString('hex') @@ -58,7 +58,7 @@ function equate (a, b, args) { equateBase(a, b, '') if (b.redeem) equateBase(a.redeem, b.redeem, 'redeem.') - if (b.network) t.deepEqual(a.network, b.network, 'Inequal *.network') + if (b.network) t.deepEqual(a.network, BNETWORKS[b.network], 'Inequal *.network') // contextual if (b.signature === null) b.signature = undefined @@ -76,7 +76,7 @@ function equate (a, b, args) { function preform (x) { x = Object.assign({}, x) - if (x.network) x.network = bnetworks[x.network] + if (x.network) x.network = BNETWORKS[x.network] if (typeof x.inputHex === 'string') { x.input = Buffer.from(x.inputHex, 'hex') delete x.inputHex @@ -96,10 +96,11 @@ function preform (x) { if (x.pubkeys) x.pubkeys = x.pubkeys.map(fromHex) if (x.signatures) x.signatures = x.signatures.map(function (y) { return Number.isFinite(y) ? y : Buffer.from(y, 'hex') }) if (x.redeem) { + x.redeem = Object.assign({}, x.redeem) if (typeof x.redeem.input === 'string') x.redeem.input = asmToBuffer(x.redeem.input) if (typeof x.redeem.output === 'string') x.redeem.output = asmToBuffer(x.redeem.output) if (Array.isArray(x.redeem.witness)) x.redeem.witness = x.redeem.witness.map(fromHex) - if (x.redeem.network) x.redeem.network = bnetworks[x.redeem.network] + if (x.redeem.network) x.redeem.network = BNETWORKS[x.redeem.network] } return x From 6927f74b7aafc046bbca80ebe8658186149c598a Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 26 Sep 2018 15:44:55 +1000 Subject: [PATCH 2/4] add inverted Network mismatch tests --- test/fixtures/p2sh.json | 18 ++++++++++++++++++ test/fixtures/p2wsh.json | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/test/fixtures/p2sh.json b/test/fixtures/p2sh.json index e6fe2ae..e447e4a 100644 --- a/test/fixtures/p2sh.json +++ b/test/fixtures/p2sh.json @@ -325,6 +325,24 @@ } } }, + { + "exception": "Network mismatch", + "arguments": { + "network": "bitcoin", + "redeem": { + "network": "testnet" + } + } + }, + { + "exception": "Network mismatch", + "arguments": { + "network": "testnet", + "redeem": { + "network": "bitcoin" + } + } + }, { "exception": "Empty input", "arguments": { diff --git a/test/fixtures/p2wsh.json b/test/fixtures/p2wsh.json index 0870278..2dfec75 100644 --- a/test/fixtures/p2wsh.json +++ b/test/fixtures/p2wsh.json @@ -306,6 +306,15 @@ } } }, + { + "exception": "Network mismatch", + "arguments": { + "network": "testnet", + "redeem": { + "network": "bitcoin" + } + } + }, { "exception": "Invalid prefix or Network mismatch", "arguments": { From 1b4dc48d75c65c854c23fa7abff7976b7814615a Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 26 Sep 2018 16:02:13 +1000 Subject: [PATCH 3/4] add failing tests for redeem.network derivation --- test/fixtures/p2sh.json | 18 ++++++++++++++++++ test/fixtures/p2wsh.json | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/test/fixtures/p2sh.json b/test/fixtures/p2sh.json index e447e4a..595b85e 100644 --- a/test/fixtures/p2sh.json +++ b/test/fixtures/p2sh.json @@ -166,6 +166,24 @@ ] } } + }, + { + "description": "p2sh-p2pkh, out (network derived from redeem)", + "arguments": { + "redeem": { + "address": "this is P2PKH context, unknown and ignored by P2SH", + "output": "OP_DUP OP_HASH160 c30afa58ae0673b00a45b5c17dff4633780f1400 OP_EQUALVERIFY OP_CHECKSIG", + "network": "testnet" + } + }, + "expected": { + "address": "2N7nfc7zeWuADtpdR4MrR7Wq3dzr7LxTCgS", + "hash": "9f840a5fc02407ef0ad499c2ec0eb0b942fb0086", + "output": "OP_HASH160 9f840a5fc02407ef0ad499c2ec0eb0b942fb0086 OP_EQUAL", + "input": null, + "witness": null, + "network": "testnet" + } } ], "invalid": [ diff --git a/test/fixtures/p2wsh.json b/test/fixtures/p2wsh.json index 2dfec75..e5ce0e0 100644 --- a/test/fixtures/p2wsh.json +++ b/test/fixtures/p2wsh.json @@ -179,6 +179,24 @@ ] } } + }, + { + "description": "p2wsh-p2pkh, out (network derived from redeem)", + "arguments": { + "redeem": { + "address": "this is P2PKH context, unknown and ignored by p2wsh", + "output": "OP_DUP OP_HASH160 c30afa58ae0673b00a45b5c17dff4633780f1400 OP_EQUALVERIFY OP_CHECKSIG", + "network": "testnet" + } + }, + "expected": { + "address": "tb1qusxlgq9quu27ucxs7a2fg8nv0pycdzvxsjk9npyupupxw3y892ssaskm8v", + "hash": "e40df400a0e715ee60d0f754941e6c784986898684ac59849c0f026744872aa1", + "output": "OP_0 e40df400a0e715ee60d0f754941e6c784986898684ac59849c0f026744872aa1", + "input": null, + "witness": null, + "network": "testnet" + } } ], "invalid": [ From a976fba27b45c742207b7081565a4e35a3282c9f Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 26 Sep 2018 16:02:52 +1000 Subject: [PATCH 4/4] add network derivation from redeem.network --- src/payments/p2sh.js | 6 +++++- src/payments/p2wsh.js | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/payments/p2sh.js b/src/payments/p2sh.js index 4741936..0729c8b 100644 --- a/src/payments/p2sh.js +++ b/src/payments/p2sh.js @@ -45,7 +45,11 @@ function p2sh (a, opts) { witness: typef.maybe(typef.arrayOf(typef.Buffer)) }, a) - const network = a.network || BITCOIN_NETWORK + let network = a.network + if (!network) { + network = (a.redeem && a.redeem.network) || BITCOIN_NETWORK + } + const o = { network } const _address = lazy.value(function () { diff --git a/src/payments/p2wsh.js b/src/payments/p2wsh.js index a26e706..d8e6e8a 100644 --- a/src/payments/p2wsh.js +++ b/src/payments/p2wsh.js @@ -59,7 +59,11 @@ function p2wsh (a, opts) { }) const _rchunks = lazy.value(function () { return bscript.decompile(a.redeem.input) }) - const network = a.network || BITCOIN_NETWORK + let network = a.network + if (!network) { + network = (a.redeem && a.redeem.network) || BITCOIN_NETWORK + } + const o = { network } lazy.prop(o, 'address', function () {