From c403757ce84e35cd1b63d87780cde1187ca8630a Mon Sep 17 00:00:00 2001 From: junderw Date: Thu, 11 Jul 2019 14:49:26 +0900 Subject: [PATCH] Add name attribute to Payments --- src/payments/embed.js | 2 +- src/payments/p2ms.js | 4 ++++ src/payments/p2pk.js | 2 +- src/payments/p2pkh.js | 2 +- src/payments/p2sh.js | 5 +++++ src/payments/p2wpkh.js | 2 +- src/payments/p2wsh.js | 5 +++++ test/fixtures/p2ms.json | 7 +++++++ test/fixtures/p2pk.json | 5 +++++ test/fixtures/p2pkh.json | 6 ++++++ test/fixtures/p2sh.json | 11 +++++++++++ test/fixtures/p2wpkh.json | 6 ++++++ test/fixtures/p2wsh.json | 11 +++++++++++ test/payments.js | 21 +++++++++++++++++++++ ts_src/payments/embed.ts | 2 +- ts_src/payments/index.ts | 1 + ts_src/payments/p2ms.ts | 4 ++++ ts_src/payments/p2pk.ts | 2 +- ts_src/payments/p2pkh.ts | 2 +- ts_src/payments/p2sh.ts | 5 +++++ ts_src/payments/p2wpkh.ts | 2 +- ts_src/payments/p2wsh.ts | 5 +++++ types/payments/index.d.ts | 1 + 23 files changed, 105 insertions(+), 8 deletions(-) diff --git a/src/payments/embed.js b/src/payments/embed.js index 3ddceb9..19df35d 100644 --- a/src/payments/embed.js +++ b/src/payments/embed.js @@ -24,7 +24,7 @@ function p2data(a, opts) { a, ); const network = a.network || networks_1.bitcoin; - const o = { network }; + const o = { name: 'embed', network }; lazy.prop(o, 'output', () => { if (!a.data) return; return bscript.compile([OPS.OP_RETURN].concat(a.data)); diff --git a/src/payments/p2ms.js b/src/payments/p2ms.js index 1e7c6ba..9fed788 100644 --- a/src/payments/p2ms.js +++ b/src/payments/p2ms.js @@ -93,6 +93,10 @@ function p2ms(a, opts) { if (!o.input) return; return []; }); + lazy.prop(o, 'name', () => { + if (!o.m || !o.n) return; + return `p2ms(${o.m} of ${o.n})`; + }); // extended validation if (opts.validate) { if (a.output) { diff --git a/src/payments/p2pk.js b/src/payments/p2pk.js index 13356d1..702669e 100644 --- a/src/payments/p2pk.js +++ b/src/payments/p2pk.js @@ -26,7 +26,7 @@ function p2pk(a, opts) { return bscript.decompile(a.input); }); const network = a.network || networks_1.bitcoin; - const o = { network }; + const o = { name: 'p2pk', network }; lazy.prop(o, 'output', () => { if (!a.pubkey) return; return bscript.compile([a.pubkey, OPS.OP_CHECKSIG]); diff --git a/src/payments/p2pkh.js b/src/payments/p2pkh.js index ceb7093..94c801f 100644 --- a/src/payments/p2pkh.js +++ b/src/payments/p2pkh.js @@ -36,7 +36,7 @@ function p2pkh(a, opts) { return bscript.decompile(a.input); }); const network = a.network || networks_1.bitcoin; - const o = { network }; + const o = { name: 'p2pkh', network }; lazy.prop(o, 'address', () => { if (!o.hash) return; const payload = Buffer.allocUnsafe(21); diff --git a/src/payments/p2sh.js b/src/payments/p2sh.js index 5fe660a..42b1e23 100644 --- a/src/payments/p2sh.js +++ b/src/payments/p2sh.js @@ -93,6 +93,11 @@ function p2sh(a, opts) { if (o.redeem && o.redeem.witness) return o.redeem.witness; if (o.input) return []; }); + lazy.prop(o, 'name', () => { + const nameParts = ['p2sh']; + if (o.redeem !== undefined) nameParts.push(o.redeem.name); + return nameParts.join('-'); + }); if (opts.validate) { let hash = Buffer.from([]); if (a.address) { diff --git a/src/payments/p2wpkh.js b/src/payments/p2wpkh.js index 9571e50..b32e808 100644 --- a/src/payments/p2wpkh.js +++ b/src/payments/p2wpkh.js @@ -40,7 +40,7 @@ function p2wpkh(a, opts) { }; }); const network = a.network || networks_1.bitcoin; - const o = { network }; + const o = { name: 'p2wpkh', network }; lazy.prop(o, 'address', () => { if (!o.hash) return; const words = bech32.toWords(o.hash); diff --git a/src/payments/p2wsh.js b/src/payments/p2wsh.js index 9363718..6a4aa24 100644 --- a/src/payments/p2wsh.js +++ b/src/payments/p2wsh.js @@ -103,6 +103,11 @@ function p2wsh(a, opts) { if (!a.redeem.witness) return; return [].concat(a.redeem.witness, a.redeem.output); }); + lazy.prop(o, 'name', () => { + const nameParts = ['p2wsh']; + if (o.redeem !== undefined) nameParts.push(o.redeem.name); + return nameParts.join('-'); + }); // extended validation if (opts.validate) { let hash = Buffer.from([]); diff --git a/test/fixtures/p2ms.json b/test/fixtures/p2ms.json index 2f41270..f84b4d5 100644 --- a/test/fixtures/p2ms.json +++ b/test/fixtures/p2ms.json @@ -9,6 +9,7 @@ "expected": { "m": 2, "n": 2, + "name": "p2ms(2 of 2)", "output": "OP_2 030000000000000000000000000000000000000000000000000000000000000001 030000000000000000000000000000000000000000000000000000000000000002 OP_2 OP_CHECKMULTISIG", "pubkeys": [ "030000000000000000000000000000000000000000000000000000000000000001", @@ -31,6 +32,7 @@ "expected": { "m": 1, "n": 2, + "name": "p2ms(1 of 2)", "output": "OP_1 030000000000000000000000000000000000000000000000000000000000000001 030000000000000000000000000000000000000000000000000000000000000002 OP_2 OP_CHECKMULTISIG", "pubkeys": [ "030000000000000000000000000000000000000000000000000000000000000001", @@ -58,6 +60,7 @@ "expected": { "m": 2, "n": 3, + "name": "p2ms(2 of 3)", "output": "OP_2 030000000000000000000000000000000000000000000000000000000000000001 030000000000000000000000000000000000000000000000000000000000000002 030000000000000000000000000000000000000000000000000000000000000003 OP_3 OP_CHECKMULTISIG", "pubkeys": [ "030000000000000000000000000000000000000000000000000000000000000001", @@ -84,6 +87,7 @@ "expected": { "m": 2, "n": 3, + "name": "p2ms(2 of 3)", "output": "OP_2 030000000000000000000000000000000000000000000000000000000000000001 030000000000000000000000000000000000000000000000000000000000000002 030000000000000000000000000000000000000000000000000000000000000003 OP_3 OP_CHECKMULTISIG", "pubkeys": [ "030000000000000000000000000000000000000000000000000000000000000001", @@ -107,6 +111,7 @@ "expected": { "m": 2, "n": 3, + "name": "p2ms(2 of 3)", "output": "OP_2 030000000000000000000000000000000000000000000000000000000000000001 030000000000000000000000000000000000000000000000000000000000000002 030000000000000000000000000000000000000000000000000000000000000003 OP_3 OP_CHECKMULTISIG", "pubkeys": [ "030000000000000000000000000000000000000000000000000000000000000001", @@ -133,6 +138,7 @@ "expected": { "m": 2, "n": 2, + "name": "p2ms(2 of 2)", "output": "OP_2 030000000000000000000000000000000000000000000000000000000000000001 030000000000000000000000000000000000000000000000000000000000000002 OP_2 OP_CHECKMULTISIG", "pubkeys": [ "030000000000000000000000000000000000000000000000000000000000000001", @@ -161,6 +167,7 @@ "expected": { "m": 2, "n": 2, + "name": "p2ms(2 of 2)", "output": "OP_2 030000000000000000000000000000000000000000000000000000000000000001 030000000000000000000000000000000000000000000000000000000000000002 OP_2 OP_CHECKMULTISIG", "pubkeys": [ "030000000000000000000000000000000000000000000000000000000000000001", diff --git a/test/fixtures/p2pk.json b/test/fixtures/p2pk.json index 58f51cb..f3982d3 100644 --- a/test/fixtures/p2pk.json +++ b/test/fixtures/p2pk.json @@ -7,6 +7,7 @@ }, "options": {}, "expected": { + "name": "p2pk", "pubkey": "030000000000000000000000000000000000000000000000000000000000000001", "signature": null, "input": null, @@ -19,6 +20,7 @@ "pubkey": "030000000000000000000000000000000000000000000000000000000000000001" }, "expected": { + "name": "p2pk", "output": "030000000000000000000000000000000000000000000000000000000000000001 OP_CHECKSIG", "signature": null, "input": null, @@ -32,6 +34,7 @@ "signature": "300602010002010001" }, "expected": { + "name": "p2pk", "pubkey": "030000000000000000000000000000000000000000000000000000000000000001", "input": "300602010002010001", "witness": [] @@ -44,6 +47,7 @@ "signature": "300602010002010001" }, "expected": { + "name": "p2pk", "output": "030000000000000000000000000000000000000000000000000000000000000001 OP_CHECKSIG", "input": "300602010002010001", "witness": [] @@ -56,6 +60,7 @@ "input": "300602010002010001" }, "expected": { + "name": "p2pk", "pubkey": "030000000000000000000000000000000000000000000000000000000000000001", "signature": "300602010002010001", "witness": [] diff --git a/test/fixtures/p2pkh.json b/test/fixtures/p2pkh.json index 44c20fe..4efbb59 100644 --- a/test/fixtures/p2pkh.json +++ b/test/fixtures/p2pkh.json @@ -7,6 +7,7 @@ }, "options": {}, "expected": { + "name": "p2pkh", "hash": "168b992bcfc44050310b3a94bd0771136d0b28d1", "output": "OP_DUP OP_HASH160 168b992bcfc44050310b3a94bd0771136d0b28d1 OP_EQUALVERIFY OP_CHECKSIG", "signature": null, @@ -20,6 +21,7 @@ "hash": "168b992bcfc44050310b3a94bd0771136d0b28d1" }, "expected": { + "name": "p2pkh", "address": "134D6gYy8DsR5m4416BnmgASuMBqKvogQh", "output": "OP_DUP OP_HASH160 168b992bcfc44050310b3a94bd0771136d0b28d1 OP_EQUALVERIFY OP_CHECKSIG", "signature": null, @@ -33,6 +35,7 @@ "output": "OP_DUP OP_HASH160 168b992bcfc44050310b3a94bd0771136d0b28d1 OP_EQUALVERIFY OP_CHECKSIG" }, "expected": { + "name": "p2pkh", "address": "134D6gYy8DsR5m4416BnmgASuMBqKvogQh", "hash": "168b992bcfc44050310b3a94bd0771136d0b28d1", "signature": null, @@ -46,6 +49,7 @@ "pubkey": "030000000000000000000000000000000000000000000000000000000000000001" }, "expected": { + "name": "p2pkh", "address": "134D6gYy8DsR5m4416BnmgASuMBqKvogQh", "hash": "168b992bcfc44050310b3a94bd0771136d0b28d1", "output": "OP_DUP OP_HASH160 168b992bcfc44050310b3a94bd0771136d0b28d1 OP_EQUALVERIFY OP_CHECKSIG", @@ -61,6 +65,7 @@ "signature": "300602010002010001" }, "expected": { + "name": "p2pkh", "address": "134D6gYy8DsR5m4416BnmgASuMBqKvogQh", "hash": "168b992bcfc44050310b3a94bd0771136d0b28d1", "output": "OP_DUP OP_HASH160 168b992bcfc44050310b3a94bd0771136d0b28d1 OP_EQUALVERIFY OP_CHECKSIG", @@ -74,6 +79,7 @@ "input": "300602010002010001 030000000000000000000000000000000000000000000000000000000000000001" }, "expected": { + "name": "p2pkh", "address": "134D6gYy8DsR5m4416BnmgASuMBqKvogQh", "hash": "168b992bcfc44050310b3a94bd0771136d0b28d1", "output": "OP_DUP OP_HASH160 168b992bcfc44050310b3a94bd0771136d0b28d1 OP_EQUALVERIFY OP_CHECKSIG", diff --git a/test/fixtures/p2sh.json b/test/fixtures/p2sh.json index 595b85e..21b63cc 100644 --- a/test/fixtures/p2sh.json +++ b/test/fixtures/p2sh.json @@ -7,6 +7,7 @@ }, "options": {}, "expected": { + "name": "p2sh", "hash": "9f840a5fc02407ef0ad499c2ec0eb0b942fb0086", "output": "OP_HASH160 9f840a5fc02407ef0ad499c2ec0eb0b942fb0086 OP_EQUAL", "redeem": null, @@ -20,6 +21,7 @@ "hash": "9f840a5fc02407ef0ad499c2ec0eb0b942fb0086" }, "expected": { + "name": "p2sh", "address": "3GETYP4cuSesh2zsPEEYVZqnRedwe4FwUT", "output": "OP_HASH160 9f840a5fc02407ef0ad499c2ec0eb0b942fb0086 OP_EQUAL", "redeem": null, @@ -33,6 +35,7 @@ "output": "OP_HASH160 9f840a5fc02407ef0ad499c2ec0eb0b942fb0086 OP_EQUAL" }, "expected": { + "name": "p2sh", "address": "3GETYP4cuSesh2zsPEEYVZqnRedwe4FwUT", "hash": "9f840a5fc02407ef0ad499c2ec0eb0b942fb0086", "redeem": null, @@ -49,6 +52,7 @@ } }, "expected": { + "name": "p2sh-p2pkh", "address": "3GETYP4cuSesh2zsPEEYVZqnRedwe4FwUT", "hash": "9f840a5fc02407ef0ad499c2ec0eb0b942fb0086", "output": "OP_HASH160 9f840a5fc02407ef0ad499c2ec0eb0b942fb0086 OP_EQUAL", @@ -65,6 +69,7 @@ } }, "expected": { + "name": "p2sh-p2wpkh", "address": "325CuTNSYmvurXaBmhNFer5zDkKnDXZggu", "hash": "0432515d8fe8de31be8207987fc6d67b29d5e7cc", "output": "OP_HASH160 0432515d8fe8de31be8207987fc6d67b29d5e7cc OP_EQUAL", @@ -81,6 +86,7 @@ } }, "expected": { + "name": "p2sh-p2pk", "address": "36TibC8RrPB9WrBdPoGXhHqDHJosyFVtVQ", "hash": "3454c084887afe854e80221c69d6282926f809c4", "output": "OP_HASH160 3454c084887afe854e80221c69d6282926f809c4 OP_EQUAL", @@ -97,6 +103,7 @@ } }, "expected": { + "name": "p2sh-p2pkh", "address": "3GETYP4cuSesh2zsPEEYVZqnRedwe4FwUT", "hash": "9f840a5fc02407ef0ad499c2ec0eb0b942fb0086", "output": "OP_HASH160 9f840a5fc02407ef0ad499c2ec0eb0b942fb0086 OP_EQUAL", @@ -117,6 +124,7 @@ } }, "expected": { + "name": "p2sh-p2wpkh", "address": "325CuTNSYmvurXaBmhNFer5zDkKnDXZggu", "hash": "0432515d8fe8de31be8207987fc6d67b29d5e7cc", "output": "OP_HASH160 0432515d8fe8de31be8207987fc6d67b29d5e7cc OP_EQUAL", @@ -133,6 +141,7 @@ "input": "3045022100e4fce9ec72b609a2df1dc050c20dcf101d27faefb3e686b7a4cb067becdd5e8e022071287fced53806b08cf39b5ad58bbe614775b3776e98a9f8760af0d4d1d47a9501 2103e15819590382a9dd878f01e2f0cbce541564eb415e43b440472d883ecd283058ac" }, "expected": { + "name": "p2sh-p2pk", "address": "36TibC8RrPB9WrBdPoGXhHqDHJosyFVtVQ", "hash": "3454c084887afe854e80221c69d6282926f809c4", "output": "OP_HASH160 3454c084887afe854e80221c69d6282926f809c4 OP_EQUAL", @@ -154,6 +163,7 @@ ] }, "expected": { + "name": "p2sh-p2wpkh", "address": "325CuTNSYmvurXaBmhNFer5zDkKnDXZggu", "hash": "0432515d8fe8de31be8207987fc6d67b29d5e7cc", "output": "OP_HASH160 0432515d8fe8de31be8207987fc6d67b29d5e7cc OP_EQUAL", @@ -177,6 +187,7 @@ } }, "expected": { + "name": "p2sh-p2pkh", "address": "2N7nfc7zeWuADtpdR4MrR7Wq3dzr7LxTCgS", "hash": "9f840a5fc02407ef0ad499c2ec0eb0b942fb0086", "output": "OP_HASH160 9f840a5fc02407ef0ad499c2ec0eb0b942fb0086 OP_EQUAL", diff --git a/test/fixtures/p2wpkh.json b/test/fixtures/p2wpkh.json index f667bdc..4d62b60 100644 --- a/test/fixtures/p2wpkh.json +++ b/test/fixtures/p2wpkh.json @@ -7,6 +7,7 @@ }, "options": {}, "expected": { + "name": "p2wpkh", "hash": "ea6d525c0c955d90d3dbd29a81ef8bfb79003727", "output": "OP_0 ea6d525c0c955d90d3dbd29a81ef8bfb79003727", "signature": null, @@ -20,6 +21,7 @@ "hash": "ea6d525c0c955d90d3dbd29a81ef8bfb79003727" }, "expected": { + "name": "p2wpkh", "address": "bc1qafk4yhqvj4wep57m62dgrmutldusqde8adh20d", "output": "OP_0 ea6d525c0c955d90d3dbd29a81ef8bfb79003727", "signature": null, @@ -33,6 +35,7 @@ "output": "OP_0 ea6d525c0c955d90d3dbd29a81ef8bfb79003727" }, "expected": { + "name": "p2wpkh", "address": "bc1qafk4yhqvj4wep57m62dgrmutldusqde8adh20d", "hash": "ea6d525c0c955d90d3dbd29a81ef8bfb79003727", "signature": null, @@ -46,6 +49,7 @@ "pubkey": "030000000000000000000000000000000000000000000000000000000000000001" }, "expected": { + "name": "p2wpkh", "address": "bc1qz69ej270c3q9qvgt822t6pm3zdksk2x35j2jlm", "hash": "168b992bcfc44050310b3a94bd0771136d0b28d1", "output": "OP_0 168b992bcfc44050310b3a94bd0771136d0b28d1", @@ -61,6 +65,7 @@ "signature": "300602010002010001" }, "expected": { + "name": "p2wpkh", "address": "bc1qz69ej270c3q9qvgt822t6pm3zdksk2x35j2jlm", "hash": "168b992bcfc44050310b3a94bd0771136d0b28d1", "output": "OP_0 168b992bcfc44050310b3a94bd0771136d0b28d1", @@ -80,6 +85,7 @@ ] }, "expected": { + "name": "p2wpkh", "address": "bc1qz69ej270c3q9qvgt822t6pm3zdksk2x35j2jlm", "hash": "168b992bcfc44050310b3a94bd0771136d0b28d1", "output": "OP_0 168b992bcfc44050310b3a94bd0771136d0b28d1", diff --git a/test/fixtures/p2wsh.json b/test/fixtures/p2wsh.json index e5ce0e0..0350fe9 100644 --- a/test/fixtures/p2wsh.json +++ b/test/fixtures/p2wsh.json @@ -7,6 +7,7 @@ }, "options": {}, "expected": { + "name": "p2wsh", "hash": "d0d1f8c5b1815bc471aef4f4720c64ecac38dfa501c0aac94f1434a866a02ae0", "output": "OP_0 d0d1f8c5b1815bc471aef4f4720c64ecac38dfa501c0aac94f1434a866a02ae0", "redeem": null, @@ -20,6 +21,7 @@ "hash": "d0d1f8c5b1815bc471aef4f4720c64ecac38dfa501c0aac94f1434a866a02ae0" }, "expected": { + "name": "p2wsh", "address": "bc1q6rgl33d3s9dugudw7n68yrryajkr3ha9q8q24j20zs62se4q9tsqdy0t2q", "output": "OP_0 d0d1f8c5b1815bc471aef4f4720c64ecac38dfa501c0aac94f1434a866a02ae0", "redeem": null, @@ -33,6 +35,7 @@ "output": "OP_0 d0d1f8c5b1815bc471aef4f4720c64ecac38dfa501c0aac94f1434a866a02ae0" }, "expected": { + "name": "p2wsh", "address": "bc1q6rgl33d3s9dugudw7n68yrryajkr3ha9q8q24j20zs62se4q9tsqdy0t2q", "hash": "d0d1f8c5b1815bc471aef4f4720c64ecac38dfa501c0aac94f1434a866a02ae0", "redeem": null, @@ -49,6 +52,7 @@ } }, "expected": { + "name": "p2wsh-p2pkh", "address": "bc1qusxlgq9quu27ucxs7a2fg8nv0pycdzvxsjk9npyupupxw3y892ss2cq5ar", "hash": "e40df400a0e715ee60d0f754941e6c784986898684ac59849c0f026744872aa1", "output": "OP_0 e40df400a0e715ee60d0f754941e6c784986898684ac59849c0f026744872aa1", @@ -65,6 +69,7 @@ } }, "expected": { + "name": "p2wsh-p2wpkh", "address": "bc1qpsl7el8wcx22f3fpdt3lm2wmzug7yyx2q3n8wzgtf37kps9tqy7skc7m3e", "hash": "0c3fecfceec194a4c5216ae3fda9db1711e210ca046677090b4c7d60c0ab013d", "output": "OP_0 0c3fecfceec194a4c5216ae3fda9db1711e210ca046677090b4c7d60c0ab013d", @@ -81,6 +86,7 @@ } }, "expected": { + "name": "p2wsh-p2pk", "address": "bc1q6rgl33d3s9dugudw7n68yrryajkr3ha9q8q24j20zs62se4q9tsqdy0t2q", "hash": "d0d1f8c5b1815bc471aef4f4720c64ecac38dfa501c0aac94f1434a866a02ae0", "output": "OP_0 d0d1f8c5b1815bc471aef4f4720c64ecac38dfa501c0aac94f1434a866a02ae0", @@ -97,6 +103,7 @@ } }, "expected": { + "name": "p2wsh-p2pkh", "address": "bc1qusxlgq9quu27ucxs7a2fg8nv0pycdzvxsjk9npyupupxw3y892ss2cq5ar", "hash": "e40df400a0e715ee60d0f754941e6c784986898684ac59849c0f026744872aa1", "output": "OP_0 e40df400a0e715ee60d0f754941e6c784986898684ac59849c0f026744872aa1", @@ -124,6 +131,7 @@ } }, "expected": { + "name": "p2wsh-p2wpkh", "address": "bc1qpsl7el8wcx22f3fpdt3lm2wmzug7yyx2q3n8wzgtf37kps9tqy7skc7m3e", "hash": "0c3fecfceec194a4c5216ae3fda9db1711e210ca046677090b4c7d60c0ab013d", "output": "OP_0 0c3fecfceec194a4c5216ae3fda9db1711e210ca046677090b4c7d60c0ab013d", @@ -144,6 +152,7 @@ ] }, "expected": { + "name": "p2wsh-p2pk", "address": "bc1q6rgl33d3s9dugudw7n68yrryajkr3ha9q8q24j20zs62se4q9tsqdy0t2q", "hash": "d0d1f8c5b1815bc471aef4f4720c64ecac38dfa501c0aac94f1434a866a02ae0", "output": "OP_0 d0d1f8c5b1815bc471aef4f4720c64ecac38dfa501c0aac94f1434a866a02ae0", @@ -167,6 +176,7 @@ ] }, "expected": { + "name": "p2wsh-p2wpkh", "address": "bc1qpsl7el8wcx22f3fpdt3lm2wmzug7yyx2q3n8wzgtf37kps9tqy7skc7m3e", "hash": "0c3fecfceec194a4c5216ae3fda9db1711e210ca046677090b4c7d60c0ab013d", "output": "OP_0 0c3fecfceec194a4c5216ae3fda9db1711e210ca046677090b4c7d60c0ab013d", @@ -190,6 +200,7 @@ } }, "expected": { + "name": "p2wsh-p2pkh", "address": "tb1qusxlgq9quu27ucxs7a2fg8nv0pycdzvxsjk9npyupupxw3y892ssaskm8v", "hash": "e40df400a0e715ee60d0f754941e6c784986898684ac59849c0f026744872aa1", "output": "OP_0 e40df400a0e715ee60d0f754941e6c784986898684ac59849c0f026744872aa1", diff --git a/test/payments.js b/test/payments.js index 5619cdb..1386ea4 100644 --- a/test/payments.js +++ b/test/payments.js @@ -41,6 +41,27 @@ const u = require('./payments.utils') }) }) + if (p === 'p2sh') { + const p2wsh = require('../src/payments/p2wsh').p2wsh + const p2pk = require('../src/payments/p2pk').p2pk + it('properly assembles nested p2wsh with names', () => { + const actual = fn({ + redeem: p2wsh({ + redeem: p2pk({ + pubkey: Buffer.from( + '03e15819590382a9dd878f01e2f0cbce541564eb415e43b440472d883ecd283058', + 'hex', + ) + }) + }) + }) + assert.strictEqual(actual.address, '3MGbrbye4ttNUXM8WAvBFRKry4fkS9fjuw') + assert.strictEqual(actual.name, 'p2sh-p2wsh-p2pk') + assert.strictEqual(actual.redeem.name, 'p2wsh-p2pk') + assert.strictEqual(actual.redeem.redeem.name, 'p2pk') + }) + } + // cross-verify dynamically too if (!fixtures.dynamic) return const { depends, details } = fixtures.dynamic diff --git a/ts_src/payments/embed.ts b/ts_src/payments/embed.ts index 38a8162..c54e279 100644 --- a/ts_src/payments/embed.ts +++ b/ts_src/payments/embed.ts @@ -29,7 +29,7 @@ export function p2data(a: Payment, opts?: PaymentOpts): Payment { ); const network = a.network || BITCOIN_NETWORK; - const o = { network } as Payment; + const o = { name: 'embed', network } as Payment; lazy.prop(o, 'output', () => { if (!a.data) return; diff --git a/ts_src/payments/index.ts b/ts_src/payments/index.ts index acd6ddb..c48a6f1 100644 --- a/ts_src/payments/index.ts +++ b/ts_src/payments/index.ts @@ -8,6 +8,7 @@ import { p2wpkh } from './p2wpkh'; import { p2wsh } from './p2wsh'; export interface Payment { + name?: string; network?: Network; output?: Buffer; data?: Buffer[]; diff --git a/ts_src/payments/p2ms.ts b/ts_src/payments/p2ms.ts index bac8b83..7cd6f10 100644 --- a/ts_src/payments/p2ms.ts +++ b/ts_src/payments/p2ms.ts @@ -102,6 +102,10 @@ export function p2ms(a: Payment, opts?: PaymentOpts): Payment { if (!o.input) return; return []; }); + lazy.prop(o, 'name', () => { + if (!o.m || !o.n) return; + return `p2ms(${o.m} of ${o.n})`; + }); // extended validation if (opts.validate) { diff --git a/ts_src/payments/p2pk.ts b/ts_src/payments/p2pk.ts index d14aacf..b4fdbc5 100644 --- a/ts_src/payments/p2pk.ts +++ b/ts_src/payments/p2pk.ts @@ -30,7 +30,7 @@ export function p2pk(a: Payment, opts?: PaymentOpts): Payment { }) as StackFunction; const network = a.network || BITCOIN_NETWORK; - const o: Payment = { network }; + const o: Payment = { name: 'p2pk', network }; lazy.prop(o, 'output', () => { if (!a.pubkey) return; diff --git a/ts_src/payments/p2pkh.ts b/ts_src/payments/p2pkh.ts index 12c9473..6503093 100644 --- a/ts_src/payments/p2pkh.ts +++ b/ts_src/payments/p2pkh.ts @@ -41,7 +41,7 @@ export function p2pkh(a: Payment, opts?: PaymentOpts): Payment { }) as StackFunction; const network = a.network || BITCOIN_NETWORK; - const o: Payment = { network }; + const o: Payment = { name: 'p2pkh', network }; lazy.prop(o, 'address', () => { if (!o.hash) return; diff --git a/ts_src/payments/p2sh.ts b/ts_src/payments/p2sh.ts index 46c11cc..8a097bd 100644 --- a/ts_src/payments/p2sh.ts +++ b/ts_src/payments/p2sh.ts @@ -116,6 +116,11 @@ export function p2sh(a: Payment, opts?: PaymentOpts): Payment { if (o.redeem && o.redeem.witness) return o.redeem.witness; if (o.input) return []; }); + lazy.prop(o, 'name', () => { + const nameParts = ['p2sh']; + if (o.redeem !== undefined) nameParts.push(o.redeem.name!); + return nameParts.join('-'); + }); if (opts.validate) { let hash: Buffer = Buffer.from([]); diff --git a/ts_src/payments/p2wpkh.ts b/ts_src/payments/p2wpkh.ts index 7d2748c..fc2a458 100644 --- a/ts_src/payments/p2wpkh.ts +++ b/ts_src/payments/p2wpkh.ts @@ -45,7 +45,7 @@ export function p2wpkh(a: Payment, opts?: PaymentOpts): Payment { }); const network = a.network || BITCOIN_NETWORK; - const o: Payment = { network }; + const o: Payment = { name: 'p2wpkh', network }; lazy.prop(o, 'address', () => { if (!o.hash) return; diff --git a/ts_src/payments/p2wsh.ts b/ts_src/payments/p2wsh.ts index 131de45..132dde4 100644 --- a/ts_src/payments/p2wsh.ts +++ b/ts_src/payments/p2wsh.ts @@ -116,6 +116,11 @@ export function p2wsh(a: Payment, opts?: PaymentOpts): Payment { if (!a.redeem.witness) return; return ([] as Buffer[]).concat(a.redeem.witness, a.redeem.output); }); + lazy.prop(o, 'name', () => { + const nameParts = ['p2wsh']; + if (o.redeem !== undefined) nameParts.push(o.redeem.name!); + return nameParts.join('-'); + }); // extended validation if (opts.validate) { diff --git a/types/payments/index.d.ts b/types/payments/index.d.ts index 102f20a..7f17b79 100644 --- a/types/payments/index.d.ts +++ b/types/payments/index.d.ts @@ -8,6 +8,7 @@ import { p2sh } from './p2sh'; import { p2wpkh } from './p2wpkh'; import { p2wsh } from './p2wsh'; export interface Payment { + name?: string; network?: Network; output?: Buffer; data?: Buffer[];