Skip to content

Commit

Permalink
Add name attribute to Payments
Browse files Browse the repository at this point in the history
  • Loading branch information
junderw committed Jul 11, 2019
1 parent 8bbe7c7 commit c403757
Show file tree
Hide file tree
Showing 23 changed files with 105 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/payments/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 4 additions & 0 deletions src/payments/p2ms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/payments/p2pk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion src/payments/p2pkh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/payments/p2sh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/payments/p2wpkh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/payments/p2wsh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/p2ms.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/p2pk.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"options": {},
"expected": {
"name": "p2pk",
"pubkey": "030000000000000000000000000000000000000000000000000000000000000001",
"signature": null,
"input": null,
Expand All @@ -19,6 +20,7 @@
"pubkey": "030000000000000000000000000000000000000000000000000000000000000001"
},
"expected": {
"name": "p2pk",
"output": "030000000000000000000000000000000000000000000000000000000000000001 OP_CHECKSIG",
"signature": null,
"input": null,
Expand All @@ -32,6 +34,7 @@
"signature": "300602010002010001"
},
"expected": {
"name": "p2pk",
"pubkey": "030000000000000000000000000000000000000000000000000000000000000001",
"input": "300602010002010001",
"witness": []
Expand All @@ -44,6 +47,7 @@
"signature": "300602010002010001"
},
"expected": {
"name": "p2pk",
"output": "030000000000000000000000000000000000000000000000000000000000000001 OP_CHECKSIG",
"input": "300602010002010001",
"witness": []
Expand All @@ -56,6 +60,7 @@
"input": "300602010002010001"
},
"expected": {
"name": "p2pk",
"pubkey": "030000000000000000000000000000000000000000000000000000000000000001",
"signature": "300602010002010001",
"witness": []
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/p2pkh.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"options": {},
"expected": {
"name": "p2pkh",
"hash": "168b992bcfc44050310b3a94bd0771136d0b28d1",
"output": "OP_DUP OP_HASH160 168b992bcfc44050310b3a94bd0771136d0b28d1 OP_EQUALVERIFY OP_CHECKSIG",
"signature": null,
Expand All @@ -20,6 +21,7 @@
"hash": "168b992bcfc44050310b3a94bd0771136d0b28d1"
},
"expected": {
"name": "p2pkh",
"address": "134D6gYy8DsR5m4416BnmgASuMBqKvogQh",
"output": "OP_DUP OP_HASH160 168b992bcfc44050310b3a94bd0771136d0b28d1 OP_EQUALVERIFY OP_CHECKSIG",
"signature": null,
Expand All @@ -33,6 +35,7 @@
"output": "OP_DUP OP_HASH160 168b992bcfc44050310b3a94bd0771136d0b28d1 OP_EQUALVERIFY OP_CHECKSIG"
},
"expected": {
"name": "p2pkh",
"address": "134D6gYy8DsR5m4416BnmgASuMBqKvogQh",
"hash": "168b992bcfc44050310b3a94bd0771136d0b28d1",
"signature": null,
Expand All @@ -46,6 +49,7 @@
"pubkey": "030000000000000000000000000000000000000000000000000000000000000001"
},
"expected": {
"name": "p2pkh",
"address": "134D6gYy8DsR5m4416BnmgASuMBqKvogQh",
"hash": "168b992bcfc44050310b3a94bd0771136d0b28d1",
"output": "OP_DUP OP_HASH160 168b992bcfc44050310b3a94bd0771136d0b28d1 OP_EQUALVERIFY OP_CHECKSIG",
Expand All @@ -61,6 +65,7 @@
"signature": "300602010002010001"
},
"expected": {
"name": "p2pkh",
"address": "134D6gYy8DsR5m4416BnmgASuMBqKvogQh",
"hash": "168b992bcfc44050310b3a94bd0771136d0b28d1",
"output": "OP_DUP OP_HASH160 168b992bcfc44050310b3a94bd0771136d0b28d1 OP_EQUALVERIFY OP_CHECKSIG",
Expand All @@ -74,6 +79,7 @@
"input": "300602010002010001 030000000000000000000000000000000000000000000000000000000000000001"
},
"expected": {
"name": "p2pkh",
"address": "134D6gYy8DsR5m4416BnmgASuMBqKvogQh",
"hash": "168b992bcfc44050310b3a94bd0771136d0b28d1",
"output": "OP_DUP OP_HASH160 168b992bcfc44050310b3a94bd0771136d0b28d1 OP_EQUALVERIFY OP_CHECKSIG",
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/p2sh.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"options": {},
"expected": {
"name": "p2sh",
"hash": "9f840a5fc02407ef0ad499c2ec0eb0b942fb0086",
"output": "OP_HASH160 9f840a5fc02407ef0ad499c2ec0eb0b942fb0086 OP_EQUAL",
"redeem": null,
Expand All @@ -20,6 +21,7 @@
"hash": "9f840a5fc02407ef0ad499c2ec0eb0b942fb0086"
},
"expected": {
"name": "p2sh",
"address": "3GETYP4cuSesh2zsPEEYVZqnRedwe4FwUT",
"output": "OP_HASH160 9f840a5fc02407ef0ad499c2ec0eb0b942fb0086 OP_EQUAL",
"redeem": null,
Expand All @@ -33,6 +35,7 @@
"output": "OP_HASH160 9f840a5fc02407ef0ad499c2ec0eb0b942fb0086 OP_EQUAL"
},
"expected": {
"name": "p2sh",
"address": "3GETYP4cuSesh2zsPEEYVZqnRedwe4FwUT",
"hash": "9f840a5fc02407ef0ad499c2ec0eb0b942fb0086",
"redeem": null,
Expand All @@ -49,6 +52,7 @@
}
},
"expected": {
"name": "p2sh-p2pkh",
"address": "3GETYP4cuSesh2zsPEEYVZqnRedwe4FwUT",
"hash": "9f840a5fc02407ef0ad499c2ec0eb0b942fb0086",
"output": "OP_HASH160 9f840a5fc02407ef0ad499c2ec0eb0b942fb0086 OP_EQUAL",
Expand All @@ -65,6 +69,7 @@
}
},
"expected": {
"name": "p2sh-p2wpkh",
"address": "325CuTNSYmvurXaBmhNFer5zDkKnDXZggu",
"hash": "0432515d8fe8de31be8207987fc6d67b29d5e7cc",
"output": "OP_HASH160 0432515d8fe8de31be8207987fc6d67b29d5e7cc OP_EQUAL",
Expand All @@ -81,6 +86,7 @@
}
},
"expected": {
"name": "p2sh-p2pk",
"address": "36TibC8RrPB9WrBdPoGXhHqDHJosyFVtVQ",
"hash": "3454c084887afe854e80221c69d6282926f809c4",
"output": "OP_HASH160 3454c084887afe854e80221c69d6282926f809c4 OP_EQUAL",
Expand All @@ -97,6 +103,7 @@
}
},
"expected": {
"name": "p2sh-p2pkh",
"address": "3GETYP4cuSesh2zsPEEYVZqnRedwe4FwUT",
"hash": "9f840a5fc02407ef0ad499c2ec0eb0b942fb0086",
"output": "OP_HASH160 9f840a5fc02407ef0ad499c2ec0eb0b942fb0086 OP_EQUAL",
Expand All @@ -117,6 +124,7 @@
}
},
"expected": {
"name": "p2sh-p2wpkh",
"address": "325CuTNSYmvurXaBmhNFer5zDkKnDXZggu",
"hash": "0432515d8fe8de31be8207987fc6d67b29d5e7cc",
"output": "OP_HASH160 0432515d8fe8de31be8207987fc6d67b29d5e7cc OP_EQUAL",
Expand All @@ -133,6 +141,7 @@
"input": "3045022100e4fce9ec72b609a2df1dc050c20dcf101d27faefb3e686b7a4cb067becdd5e8e022071287fced53806b08cf39b5ad58bbe614775b3776e98a9f8760af0d4d1d47a9501 2103e15819590382a9dd878f01e2f0cbce541564eb415e43b440472d883ecd283058ac"
},
"expected": {
"name": "p2sh-p2pk",
"address": "36TibC8RrPB9WrBdPoGXhHqDHJosyFVtVQ",
"hash": "3454c084887afe854e80221c69d6282926f809c4",
"output": "OP_HASH160 3454c084887afe854e80221c69d6282926f809c4 OP_EQUAL",
Expand All @@ -154,6 +163,7 @@
]
},
"expected": {
"name": "p2sh-p2wpkh",
"address": "325CuTNSYmvurXaBmhNFer5zDkKnDXZggu",
"hash": "0432515d8fe8de31be8207987fc6d67b29d5e7cc",
"output": "OP_HASH160 0432515d8fe8de31be8207987fc6d67b29d5e7cc OP_EQUAL",
Expand All @@ -177,6 +187,7 @@
}
},
"expected": {
"name": "p2sh-p2pkh",
"address": "2N7nfc7zeWuADtpdR4MrR7Wq3dzr7LxTCgS",
"hash": "9f840a5fc02407ef0ad499c2ec0eb0b942fb0086",
"output": "OP_HASH160 9f840a5fc02407ef0ad499c2ec0eb0b942fb0086 OP_EQUAL",
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/p2wpkh.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"options": {},
"expected": {
"name": "p2wpkh",
"hash": "ea6d525c0c955d90d3dbd29a81ef8bfb79003727",
"output": "OP_0 ea6d525c0c955d90d3dbd29a81ef8bfb79003727",
"signature": null,
Expand All @@ -20,6 +21,7 @@
"hash": "ea6d525c0c955d90d3dbd29a81ef8bfb79003727"
},
"expected": {
"name": "p2wpkh",
"address": "bc1qafk4yhqvj4wep57m62dgrmutldusqde8adh20d",
"output": "OP_0 ea6d525c0c955d90d3dbd29a81ef8bfb79003727",
"signature": null,
Expand All @@ -33,6 +35,7 @@
"output": "OP_0 ea6d525c0c955d90d3dbd29a81ef8bfb79003727"
},
"expected": {
"name": "p2wpkh",
"address": "bc1qafk4yhqvj4wep57m62dgrmutldusqde8adh20d",
"hash": "ea6d525c0c955d90d3dbd29a81ef8bfb79003727",
"signature": null,
Expand All @@ -46,6 +49,7 @@
"pubkey": "030000000000000000000000000000000000000000000000000000000000000001"
},
"expected": {
"name": "p2wpkh",
"address": "bc1qz69ej270c3q9qvgt822t6pm3zdksk2x35j2jlm",
"hash": "168b992bcfc44050310b3a94bd0771136d0b28d1",
"output": "OP_0 168b992bcfc44050310b3a94bd0771136d0b28d1",
Expand All @@ -61,6 +65,7 @@
"signature": "300602010002010001"
},
"expected": {
"name": "p2wpkh",
"address": "bc1qz69ej270c3q9qvgt822t6pm3zdksk2x35j2jlm",
"hash": "168b992bcfc44050310b3a94bd0771136d0b28d1",
"output": "OP_0 168b992bcfc44050310b3a94bd0771136d0b28d1",
Expand All @@ -80,6 +85,7 @@
]
},
"expected": {
"name": "p2wpkh",
"address": "bc1qz69ej270c3q9qvgt822t6pm3zdksk2x35j2jlm",
"hash": "168b992bcfc44050310b3a94bd0771136d0b28d1",
"output": "OP_0 168b992bcfc44050310b3a94bd0771136d0b28d1",
Expand Down
Loading

0 comments on commit c403757

Please sign in to comment.