@btc-vision/bitcoin 6.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +201 -0
  3. package/package.json +95 -0
  4. package/src/address.d.ts +42 -0
  5. package/src/address.js +191 -0
  6. package/src/bip66.d.ts +7 -0
  7. package/src/bip66.js +103 -0
  8. package/src/block.d.ts +30 -0
  9. package/src/block.js +224 -0
  10. package/src/bufferutils.d.ts +54 -0
  11. package/src/bufferutils.js +182 -0
  12. package/src/crypto.d.ts +18 -0
  13. package/src/crypto.js +128 -0
  14. package/src/ecc_lib.d.ts +17 -0
  15. package/src/ecc_lib.js +122 -0
  16. package/src/hooks/AdvancedSignatureManager.d.ts +44 -0
  17. package/src/hooks/AdvancedSignatureManager.js +88 -0
  18. package/src/hooks/HookedSigner.d.ts +4 -0
  19. package/src/hooks/HookedSigner.js +90 -0
  20. package/src/hooks/SignatureManager.d.ts +35 -0
  21. package/src/hooks/SignatureManager.js +72 -0
  22. package/src/index.d.ts +42 -0
  23. package/src/index.js +87 -0
  24. package/src/merkle.d.ts +10 -0
  25. package/src/merkle.js +30 -0
  26. package/src/networks.d.ts +29 -0
  27. package/src/networks.js +71 -0
  28. package/src/ops.d.ts +126 -0
  29. package/src/ops.js +131 -0
  30. package/src/payments/bip341.d.ts +49 -0
  31. package/src/payments/bip341.js +124 -0
  32. package/src/payments/embed.d.ts +9 -0
  33. package/src/payments/embed.js +54 -0
  34. package/src/payments/index.d.ts +48 -0
  35. package/src/payments/index.js +69 -0
  36. package/src/payments/lazy.d.ts +2 -0
  37. package/src/payments/lazy.js +32 -0
  38. package/src/payments/p2ms.d.ts +9 -0
  39. package/src/payments/p2ms.js +158 -0
  40. package/src/payments/p2pk.d.ts +10 -0
  41. package/src/payments/p2pk.js +82 -0
  42. package/src/payments/p2pkh.d.ts +10 -0
  43. package/src/payments/p2pkh.js +143 -0
  44. package/src/payments/p2sh.d.ts +10 -0
  45. package/src/payments/p2sh.js +204 -0
  46. package/src/payments/p2tr.d.ts +10 -0
  47. package/src/payments/p2tr.js +315 -0
  48. package/src/payments/p2wpkh.d.ts +10 -0
  49. package/src/payments/p2wpkh.js +146 -0
  50. package/src/payments/p2wsh.d.ts +10 -0
  51. package/src/payments/p2wsh.js +226 -0
  52. package/src/psbt/bip371.d.ts +42 -0
  53. package/src/psbt/bip371.js +424 -0
  54. package/src/psbt/psbtutils.d.ts +64 -0
  55. package/src/psbt/psbtutils.js +191 -0
  56. package/src/psbt.d.ts +235 -0
  57. package/src/psbt.js +1825 -0
  58. package/src/push_data.d.ts +29 -0
  59. package/src/push_data.js +83 -0
  60. package/src/script.d.ts +42 -0
  61. package/src/script.js +231 -0
  62. package/src/script_number.d.ts +19 -0
  63. package/src/script_number.js +78 -0
  64. package/src/script_signature.d.ts +21 -0
  65. package/src/script_signature.js +79 -0
  66. package/src/transaction.d.ts +60 -0
  67. package/src/transaction.js +571 -0
  68. package/src/types.d.ts +54 -0
  69. package/src/types.js +106 -0
@@ -0,0 +1,204 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.p2sh = void 0;
4
+ const bcrypto = require('../crypto');
5
+ const networks_1 = require('../networks');
6
+ const bscript = require('../script');
7
+ const types_1 = require('../types');
8
+ const lazy = require('./lazy');
9
+ const bs58check = require('bs58check');
10
+ const OPS = bscript.OPS;
11
+ // input: [redeemScriptSig ...] {redeemScript}
12
+ // witness: <?>
13
+ // output: OP_HASH160 {hash160(redeemScript)} OP_EQUAL
14
+ /**
15
+ * Creates a Pay-to-Script-Hash (P2SH) payment object.
16
+ *
17
+ * @param a - The payment object containing the necessary data.
18
+ * @param opts - Optional payment options.
19
+ * @returns The P2SH payment object.
20
+ * @throws {TypeError} If the required data is not provided or if the data is invalid.
21
+ */
22
+ function p2sh(a, opts) {
23
+ if (!a.address && !a.hash && !a.output && !a.redeem && !a.input)
24
+ throw new TypeError('Not enough data');
25
+ opts = Object.assign({ validate: true }, opts || {});
26
+ (0, types_1.typeforce)(
27
+ {
28
+ network: types_1.typeforce.maybe(types_1.typeforce.Object),
29
+ address: types_1.typeforce.maybe(types_1.typeforce.String),
30
+ hash: types_1.typeforce.maybe(types_1.typeforce.BufferN(20)),
31
+ output: types_1.typeforce.maybe(types_1.typeforce.BufferN(23)),
32
+ redeem: types_1.typeforce.maybe({
33
+ network: types_1.typeforce.maybe(types_1.typeforce.Object),
34
+ output: types_1.typeforce.maybe(types_1.typeforce.Buffer),
35
+ input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
36
+ witness: types_1.typeforce.maybe(
37
+ types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
38
+ ),
39
+ }),
40
+ input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
41
+ witness: types_1.typeforce.maybe(
42
+ types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
43
+ ),
44
+ },
45
+ a,
46
+ );
47
+ let network = a.network;
48
+ if (!network) {
49
+ network = (a.redeem && a.redeem.network) || networks_1.bitcoin;
50
+ }
51
+ const o = { network };
52
+ const _address = lazy.value(() => {
53
+ const payload = Buffer.from(bs58check.decode(a.address));
54
+ const version = payload.readUInt8(0);
55
+ const hash = payload.slice(1);
56
+ return { version, hash };
57
+ });
58
+ const _chunks = lazy.value(() => {
59
+ return bscript.decompile(a.input);
60
+ });
61
+ const _redeem = lazy.value(() => {
62
+ const chunks = _chunks();
63
+ const lastChunk = chunks[chunks.length - 1];
64
+ return {
65
+ network,
66
+ output: lastChunk === OPS.OP_FALSE ? Buffer.from([]) : lastChunk,
67
+ input: bscript.compile(chunks.slice(0, -1)),
68
+ witness: a.witness || [],
69
+ };
70
+ });
71
+ // output dependents
72
+ lazy.prop(o, 'address', () => {
73
+ if (!o.hash) return;
74
+ const payload = Buffer.allocUnsafe(21);
75
+ payload.writeUInt8(o.network.scriptHash, 0);
76
+ o.hash.copy(payload, 1);
77
+ return bs58check.encode(payload);
78
+ });
79
+ lazy.prop(o, 'hash', () => {
80
+ // in order of least effort
81
+ if (a.output) return a.output.slice(2, 22);
82
+ if (a.address) return _address().hash;
83
+ if (o.redeem && o.redeem.output)
84
+ return bcrypto.hash160(o.redeem.output);
85
+ });
86
+ lazy.prop(o, 'output', () => {
87
+ if (!o.hash) return;
88
+ return bscript.compile([OPS.OP_HASH160, o.hash, OPS.OP_EQUAL]);
89
+ });
90
+ // input dependents
91
+ lazy.prop(o, 'redeem', () => {
92
+ if (!a.input) return;
93
+ return _redeem();
94
+ });
95
+ lazy.prop(o, 'input', () => {
96
+ if (!a.redeem || !a.redeem.input || !a.redeem.output) return;
97
+ return bscript.compile(
98
+ [].concat(bscript.decompile(a.redeem.input), a.redeem.output),
99
+ );
100
+ });
101
+ lazy.prop(o, 'witness', () => {
102
+ if (o.redeem && o.redeem.witness) return o.redeem.witness;
103
+ if (o.input) return [];
104
+ });
105
+ lazy.prop(o, 'name', () => {
106
+ const nameParts = ['p2sh'];
107
+ if (o.redeem !== undefined && o.redeem.name !== undefined)
108
+ nameParts.push(o.redeem.name);
109
+ return nameParts.join('-');
110
+ });
111
+ if (opts.validate) {
112
+ let hash = Buffer.from([]);
113
+ if (a.address) {
114
+ if (_address().version !== network.scriptHash)
115
+ throw new TypeError('Invalid version or Network mismatch');
116
+ if (_address().hash.length !== 20)
117
+ throw new TypeError('Invalid address');
118
+ hash = _address().hash;
119
+ }
120
+ if (a.hash) {
121
+ if (hash.length > 0 && !hash.equals(a.hash))
122
+ throw new TypeError('Hash mismatch');
123
+ else hash = a.hash;
124
+ }
125
+ if (a.output) {
126
+ if (
127
+ a.output.length !== 23 ||
128
+ a.output[0] !== OPS.OP_HASH160 ||
129
+ a.output[1] !== 0x14 ||
130
+ a.output[22] !== OPS.OP_EQUAL
131
+ )
132
+ throw new TypeError('Output is invalid');
133
+ const hash2 = a.output.slice(2, 22);
134
+ if (hash.length > 0 && !hash.equals(hash2))
135
+ throw new TypeError('Hash mismatch');
136
+ else hash = hash2;
137
+ }
138
+ // inlined to prevent 'no-inner-declarations' failing
139
+ const checkRedeem = redeem => {
140
+ // is the redeem output empty/invalid?
141
+ if (redeem.output) {
142
+ const decompile = bscript.decompile(redeem.output);
143
+ if (!decompile || decompile.length < 1)
144
+ throw new TypeError('Redeem.output too short');
145
+ if (redeem.output.byteLength > 520)
146
+ throw new TypeError(
147
+ 'Redeem.output unspendable if larger than 520 bytes',
148
+ );
149
+ if (bscript.countNonPushOnlyOPs(decompile) > 201)
150
+ throw new TypeError(
151
+ 'Redeem.output unspendable with more than 201 non-push ops',
152
+ );
153
+ // match hash against other sources
154
+ const hash2 = bcrypto.hash160(redeem.output);
155
+ if (hash.length > 0 && !hash.equals(hash2))
156
+ throw new TypeError('Hash mismatch');
157
+ else hash = hash2;
158
+ }
159
+ if (redeem.input) {
160
+ const hasInput = redeem.input.length > 0;
161
+ const hasWitness = redeem.witness && redeem.witness.length > 0;
162
+ if (!hasInput && !hasWitness)
163
+ throw new TypeError('Empty input');
164
+ if (hasInput && hasWitness)
165
+ throw new TypeError('Input and witness provided');
166
+ if (hasInput) {
167
+ const richunks = bscript.decompile(redeem.input);
168
+ if (!bscript.isPushOnly(richunks))
169
+ throw new TypeError('Non push-only scriptSig');
170
+ }
171
+ }
172
+ };
173
+ if (a.input) {
174
+ const chunks = _chunks();
175
+ if (!chunks || chunks.length < 1)
176
+ throw new TypeError('Input too short');
177
+ if (!Buffer.isBuffer(_redeem().output))
178
+ throw new TypeError('Input is invalid');
179
+ checkRedeem(_redeem());
180
+ }
181
+ if (a.redeem) {
182
+ if (a.redeem.network && a.redeem.network !== network)
183
+ throw new TypeError('Network mismatch');
184
+ if (a.input) {
185
+ const redeem = _redeem();
186
+ if (a.redeem.output && !a.redeem.output.equals(redeem.output))
187
+ throw new TypeError('Redeem.output mismatch');
188
+ if (a.redeem.input && !a.redeem.input.equals(redeem.input))
189
+ throw new TypeError('Redeem.input mismatch');
190
+ }
191
+ checkRedeem(a.redeem);
192
+ }
193
+ if (a.witness) {
194
+ if (
195
+ a.redeem &&
196
+ a.redeem.witness &&
197
+ !(0, types_1.stacksEqual)(a.redeem.witness, a.witness)
198
+ )
199
+ throw new TypeError('Witness and redeem.witness mismatch');
200
+ }
201
+ }
202
+ return Object.assign(o, a);
203
+ }
204
+ exports.p2sh = p2sh;
@@ -0,0 +1,10 @@
1
+ import { Payment, PaymentOpts } from './index';
2
+ /**
3
+ * Creates a Pay-to-Taproot (P2TR) payment object.
4
+ *
5
+ * @param a - The payment object containing the necessary data for P2TR.
6
+ * @param opts - Optional payment options.
7
+ * @returns The P2TR payment object.
8
+ * @throws {TypeError} If the provided data is invalid or insufficient.
9
+ */
10
+ export declare function p2tr(a: Payment, opts?: PaymentOpts): Payment;
@@ -0,0 +1,315 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.p2tr = void 0;
4
+ const buffer_1 = require('buffer');
5
+ const networks_1 = require('../networks');
6
+ const bscript = require('../script');
7
+ const types_1 = require('../types');
8
+ const ecc_lib_1 = require('../ecc_lib');
9
+ const bip341_1 = require('./bip341');
10
+ const lazy = require('./lazy');
11
+ const bech32_1 = require('bech32');
12
+ const address_1 = require('../address');
13
+ const OPS = bscript.OPS;
14
+ const TAPROOT_WITNESS_VERSION = 0x01;
15
+ const ANNEX_PREFIX = 0x50;
16
+ /**
17
+ * Creates a Pay-to-Taproot (P2TR) payment object.
18
+ *
19
+ * @param a - The payment object containing the necessary data for P2TR.
20
+ * @param opts - Optional payment options.
21
+ * @returns The P2TR payment object.
22
+ * @throws {TypeError} If the provided data is invalid or insufficient.
23
+ */
24
+ function p2tr(a, opts) {
25
+ if (
26
+ !a.address &&
27
+ !a.output &&
28
+ !a.pubkey &&
29
+ !a.internalPubkey &&
30
+ !(a.witness && a.witness.length > 1)
31
+ )
32
+ throw new TypeError('Not enough data');
33
+ opts = Object.assign({ validate: true }, opts || {});
34
+ (0, types_1.typeforce)(
35
+ {
36
+ address: types_1.typeforce.maybe(types_1.typeforce.String),
37
+ input: types_1.typeforce.maybe(types_1.typeforce.BufferN(0)),
38
+ network: types_1.typeforce.maybe(types_1.typeforce.Object),
39
+ output: types_1.typeforce.maybe(types_1.typeforce.BufferN(34)),
40
+ internalPubkey: types_1.typeforce.maybe(
41
+ types_1.typeforce.BufferN(32),
42
+ ),
43
+ hash: types_1.typeforce.maybe(types_1.typeforce.BufferN(32)),
44
+ pubkey: types_1.typeforce.maybe(types_1.typeforce.BufferN(32)),
45
+ signature: types_1.typeforce.maybe(
46
+ types_1.typeforce.anyOf(
47
+ types_1.typeforce.BufferN(64),
48
+ types_1.typeforce.BufferN(65),
49
+ ),
50
+ ),
51
+ witness: types_1.typeforce.maybe(
52
+ types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
53
+ ),
54
+ scriptTree: types_1.typeforce.maybe(types_1.isTaptree),
55
+ redeem: types_1.typeforce.maybe({
56
+ output: types_1.typeforce.maybe(types_1.typeforce.Buffer),
57
+ redeemVersion: types_1.typeforce.maybe(
58
+ types_1.typeforce.Number,
59
+ ),
60
+ witness: types_1.typeforce.maybe(
61
+ types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
62
+ ),
63
+ }),
64
+ redeemVersion: types_1.typeforce.maybe(types_1.typeforce.Number),
65
+ },
66
+ a,
67
+ );
68
+ const _address = lazy.value(() => {
69
+ return (0, address_1.fromBech32)(a.address);
70
+ });
71
+ // remove annex if present, ignored by taproot
72
+ const _witness = lazy.value(() => {
73
+ if (!a.witness || !a.witness.length) return;
74
+ if (
75
+ a.witness.length >= 2 &&
76
+ a.witness[a.witness.length - 1][0] === ANNEX_PREFIX
77
+ ) {
78
+ return a.witness.slice(0, -1);
79
+ }
80
+ return a.witness.slice();
81
+ });
82
+ const _hashTree = lazy.value(() => {
83
+ if (a.scriptTree) return (0, bip341_1.toHashTree)(a.scriptTree);
84
+ if (a.hash) return { hash: a.hash };
85
+ return;
86
+ });
87
+ const network = a.network || networks_1.bitcoin;
88
+ const o = { name: 'p2tr', network };
89
+ lazy.prop(o, 'address', () => {
90
+ if (!o.pubkey) return;
91
+ const words = bech32_1.bech32m.toWords(o.pubkey);
92
+ words.unshift(TAPROOT_WITNESS_VERSION);
93
+ return bech32_1.bech32m.encode(network.bech32, words);
94
+ });
95
+ lazy.prop(o, 'hash', () => {
96
+ const hashTree = _hashTree();
97
+ if (hashTree) return hashTree.hash;
98
+ const w = _witness();
99
+ if (w && w.length > 1) {
100
+ const controlBlock = w[w.length - 1];
101
+ const leafVersion = controlBlock[0] & types_1.TAPLEAF_VERSION_MASK;
102
+ const script = w[w.length - 2];
103
+ const leafHash = (0, bip341_1.tapleafHash)({
104
+ output: script,
105
+ version: leafVersion,
106
+ });
107
+ return (0, bip341_1.rootHashFromPath)(controlBlock, leafHash);
108
+ }
109
+ return null;
110
+ });
111
+ lazy.prop(o, 'output', () => {
112
+ if (!o.pubkey) return;
113
+ return bscript.compile([OPS.OP_1, o.pubkey]);
114
+ });
115
+ lazy.prop(o, 'redeemVersion', () => {
116
+ if (a.redeemVersion) return a.redeemVersion;
117
+ if (
118
+ a.redeem &&
119
+ a.redeem.redeemVersion !== undefined &&
120
+ a.redeem.redeemVersion !== null
121
+ ) {
122
+ return a.redeem.redeemVersion;
123
+ }
124
+ return bip341_1.LEAF_VERSION_TAPSCRIPT;
125
+ });
126
+ lazy.prop(o, 'redeem', () => {
127
+ const witness = _witness(); // witness without annex
128
+ if (!witness || witness.length < 2) return;
129
+ return {
130
+ output: witness[witness.length - 2],
131
+ witness: witness.slice(0, -2),
132
+ redeemVersion:
133
+ witness[witness.length - 1][0] & types_1.TAPLEAF_VERSION_MASK,
134
+ };
135
+ });
136
+ lazy.prop(o, 'pubkey', () => {
137
+ if (a.pubkey) return a.pubkey;
138
+ if (a.output) return a.output.slice(2);
139
+ if (a.address) return _address().data;
140
+ if (o.internalPubkey) {
141
+ const tweakedKey = (0, bip341_1.tweakKey)(o.internalPubkey, o.hash);
142
+ if (tweakedKey) return tweakedKey.x;
143
+ }
144
+ });
145
+ lazy.prop(o, 'internalPubkey', () => {
146
+ if (a.internalPubkey) return a.internalPubkey;
147
+ const witness = _witness();
148
+ if (witness && witness.length > 1)
149
+ return witness[witness.length - 1].slice(1, 33);
150
+ });
151
+ lazy.prop(o, 'signature', () => {
152
+ if (a.signature) return a.signature;
153
+ const witness = _witness(); // witness without annex
154
+ if (!witness || witness.length !== 1) return;
155
+ return witness[0];
156
+ });
157
+ lazy.prop(o, 'witness', () => {
158
+ if (a.witness) return a.witness;
159
+ const hashTree = _hashTree();
160
+ if (hashTree && a.redeem && a.redeem.output && a.internalPubkey) {
161
+ const leafHash = (0, bip341_1.tapleafHash)({
162
+ output: a.redeem.output,
163
+ version: o.redeemVersion,
164
+ });
165
+ const path = (0, bip341_1.findScriptPath)(hashTree, leafHash);
166
+ if (!path) return;
167
+ const outputKey = (0, bip341_1.tweakKey)(
168
+ a.internalPubkey,
169
+ hashTree.hash,
170
+ );
171
+ if (!outputKey) return;
172
+ const controlBock = buffer_1.Buffer.concat(
173
+ [
174
+ buffer_1.Buffer.from([o.redeemVersion | outputKey.parity]),
175
+ a.internalPubkey,
176
+ ].concat(path),
177
+ );
178
+ return [a.redeem.output, controlBock];
179
+ }
180
+ if (a.signature) return [a.signature];
181
+ });
182
+ // extended validation
183
+ if (opts.validate) {
184
+ let pubkey = buffer_1.Buffer.from([]);
185
+ if (a.address) {
186
+ if (network && network.bech32 !== _address().prefix)
187
+ throw new TypeError('Invalid prefix or Network mismatch');
188
+ if (_address().version !== TAPROOT_WITNESS_VERSION)
189
+ throw new TypeError('Invalid address version');
190
+ if (_address().data.length !== 32)
191
+ throw new TypeError('Invalid address data');
192
+ pubkey = _address().data;
193
+ }
194
+ if (a.pubkey) {
195
+ if (pubkey.length > 0 && !pubkey.equals(a.pubkey))
196
+ throw new TypeError('Pubkey mismatch');
197
+ else pubkey = a.pubkey;
198
+ }
199
+ if (a.output) {
200
+ if (
201
+ a.output.length !== 34 ||
202
+ a.output[0] !== OPS.OP_1 ||
203
+ a.output[1] !== 0x20
204
+ )
205
+ throw new TypeError('Output is invalid');
206
+ if (pubkey.length > 0 && !pubkey.equals(a.output.slice(2)))
207
+ throw new TypeError('Pubkey mismatch');
208
+ else pubkey = a.output.slice(2);
209
+ }
210
+ if (a.internalPubkey) {
211
+ const tweakedKey = (0, bip341_1.tweakKey)(a.internalPubkey, o.hash);
212
+ if (pubkey.length > 0 && !pubkey.equals(tweakedKey.x))
213
+ throw new TypeError('Pubkey mismatch');
214
+ else pubkey = tweakedKey.x;
215
+ }
216
+ /*if (pubkey && pubkey.length) {
217
+ if (!getEccLib().isXOnlyPoint(pubkey))
218
+ throw new TypeError('Invalid pubkey for p2tr');
219
+ }*/
220
+ const hashTree = _hashTree();
221
+ if (a.hash && hashTree) {
222
+ if (!a.hash.equals(hashTree.hash))
223
+ throw new TypeError('Hash mismatch');
224
+ }
225
+ if (a.redeem && a.redeem.output && hashTree) {
226
+ const leafHash = (0, bip341_1.tapleafHash)({
227
+ output: a.redeem.output,
228
+ version: o.redeemVersion,
229
+ });
230
+ if (!(0, bip341_1.findScriptPath)(hashTree, leafHash))
231
+ throw new TypeError('Redeem script not in tree');
232
+ }
233
+ const witness = _witness();
234
+ // compare the provided redeem data with the one computed from witness
235
+ if (a.redeem && o.redeem) {
236
+ if (a.redeem.redeemVersion) {
237
+ if (a.redeem.redeemVersion !== o.redeem.redeemVersion)
238
+ throw new TypeError(
239
+ 'Redeem.redeemVersion and witness mismatch',
240
+ );
241
+ }
242
+ if (a.redeem.output) {
243
+ if (bscript.decompile(a.redeem.output).length === 0)
244
+ throw new TypeError('Redeem.output is invalid');
245
+ // output redeem is constructed from the witness
246
+ if (o.redeem.output && !a.redeem.output.equals(o.redeem.output))
247
+ throw new TypeError('Redeem.output and witness mismatch');
248
+ }
249
+ if (a.redeem.witness) {
250
+ if (
251
+ o.redeem.witness &&
252
+ !(0, types_1.stacksEqual)(
253
+ a.redeem.witness,
254
+ o.redeem.witness,
255
+ )
256
+ )
257
+ throw new TypeError('Redeem.witness and witness mismatch');
258
+ }
259
+ }
260
+ if (witness && witness.length) {
261
+ if (witness.length === 1) {
262
+ // key spending
263
+ if (a.signature && !a.signature.equals(witness[0]))
264
+ throw new TypeError('Signature mismatch');
265
+ } else {
266
+ // script path spending
267
+ const controlBlock = witness[witness.length - 1];
268
+ if (controlBlock.length < 33)
269
+ throw new TypeError(
270
+ `The control-block length is too small. Got ${controlBlock.length}, expected min 33.`,
271
+ );
272
+ if ((controlBlock.length - 33) % 32 !== 0)
273
+ throw new TypeError(
274
+ `The control-block length of ${controlBlock.length} is incorrect!`,
275
+ );
276
+ const m = (controlBlock.length - 33) / 32;
277
+ if (m > 128)
278
+ throw new TypeError(
279
+ `The script path is too long. Got ${m}, expected max 128.`,
280
+ );
281
+ const internalPubkey = controlBlock.slice(1, 33);
282
+ if (
283
+ a.internalPubkey &&
284
+ !a.internalPubkey.equals(internalPubkey)
285
+ )
286
+ throw new TypeError('Internal pubkey mismatch');
287
+ if (!(0, ecc_lib_1.getEccLib)().isXOnlyPoint(internalPubkey))
288
+ throw new TypeError(
289
+ 'Invalid internalPubkey for p2tr witness',
290
+ );
291
+ const leafVersion =
292
+ controlBlock[0] & types_1.TAPLEAF_VERSION_MASK;
293
+ const script = witness[witness.length - 2];
294
+ const leafHash = (0, bip341_1.tapleafHash)({
295
+ output: script,
296
+ version: leafVersion,
297
+ });
298
+ const hash = (0, bip341_1.rootHashFromPath)(
299
+ controlBlock,
300
+ leafHash,
301
+ );
302
+ const outputKey = (0, bip341_1.tweakKey)(internalPubkey, hash);
303
+ if (!outputKey)
304
+ // todo: needs test data
305
+ throw new TypeError('Invalid outputKey for p2tr witness');
306
+ if (pubkey.length && !pubkey.equals(outputKey.x))
307
+ throw new TypeError('Pubkey mismatch for p2tr witness');
308
+ if (outputKey.parity !== (controlBlock[0] & 1))
309
+ throw new Error('Incorrect parity');
310
+ }
311
+ }
312
+ }
313
+ return Object.assign(o, a);
314
+ }
315
+ exports.p2tr = p2tr;
@@ -0,0 +1,10 @@
1
+ import { Payment, PaymentOpts } from './index';
2
+ /**
3
+ * Creates a pay-to-witness-public-key-hash (p2wpkh) payment object.
4
+ *
5
+ * @param a - The payment object containing the necessary data.
6
+ * @param opts - Optional payment options.
7
+ * @returns The p2wpkh payment object.
8
+ * @throws {TypeError} If the required data is missing or invalid.
9
+ */
10
+ export declare function p2wpkh(a: Payment, opts?: PaymentOpts): Payment;
@@ -0,0 +1,146 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.p2wpkh = void 0;
4
+ const bcrypto = require('../crypto');
5
+ const networks_1 = require('../networks');
6
+ const bscript = require('../script');
7
+ const types_1 = require('../types');
8
+ const lazy = require('./lazy');
9
+ const bech32_1 = require('bech32');
10
+ const OPS = bscript.OPS;
11
+ const EMPTY_BUFFER = Buffer.alloc(0);
12
+ // witness: {signature} {pubKey}
13
+ // input: <>
14
+ // output: OP_0 {pubKeyHash}
15
+ /**
16
+ * Creates a pay-to-witness-public-key-hash (p2wpkh) payment object.
17
+ *
18
+ * @param a - The payment object containing the necessary data.
19
+ * @param opts - Optional payment options.
20
+ * @returns The p2wpkh payment object.
21
+ * @throws {TypeError} If the required data is missing or invalid.
22
+ */
23
+ function p2wpkh(a, opts) {
24
+ if (!a.address && !a.hash && !a.output && !a.pubkey && !a.witness)
25
+ throw new TypeError('Not enough data');
26
+ opts = Object.assign({ validate: true }, opts || {});
27
+ (0, types_1.typeforce)(
28
+ {
29
+ address: types_1.typeforce.maybe(types_1.typeforce.String),
30
+ hash: types_1.typeforce.maybe(types_1.typeforce.BufferN(20)),
31
+ input: types_1.typeforce.maybe(types_1.typeforce.BufferN(0)),
32
+ network: types_1.typeforce.maybe(types_1.typeforce.Object),
33
+ output: types_1.typeforce.maybe(types_1.typeforce.BufferN(22)),
34
+ pubkey: types_1.typeforce.maybe(types_1.isPoint),
35
+ signature: types_1.typeforce.maybe(
36
+ bscript.isCanonicalScriptSignature,
37
+ ),
38
+ witness: types_1.typeforce.maybe(
39
+ types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
40
+ ),
41
+ },
42
+ a,
43
+ );
44
+ const _address = lazy.value(() => {
45
+ const result = bech32_1.bech32.decode(a.address);
46
+ const version = result.words.shift();
47
+ const data = bech32_1.bech32.fromWords(result.words);
48
+ return {
49
+ version,
50
+ prefix: result.prefix,
51
+ data: Buffer.from(data),
52
+ };
53
+ });
54
+ const network = a.network || networks_1.bitcoin;
55
+ const o = { name: 'p2wpkh', network };
56
+ lazy.prop(o, 'address', () => {
57
+ if (!o.hash) return;
58
+ const words = bech32_1.bech32.toWords(o.hash);
59
+ words.unshift(0x00);
60
+ return bech32_1.bech32.encode(network.bech32, words);
61
+ });
62
+ lazy.prop(o, 'hash', () => {
63
+ if (a.output) return a.output.slice(2, 22);
64
+ if (a.address) return _address().data;
65
+ if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey || o.pubkey);
66
+ });
67
+ lazy.prop(o, 'output', () => {
68
+ if (!o.hash) return;
69
+ return bscript.compile([OPS.OP_0, o.hash]);
70
+ });
71
+ lazy.prop(o, 'pubkey', () => {
72
+ if (a.pubkey) return a.pubkey;
73
+ if (!a.witness) return;
74
+ return a.witness[1];
75
+ });
76
+ lazy.prop(o, 'signature', () => {
77
+ if (!a.witness) return;
78
+ return a.witness[0];
79
+ });
80
+ lazy.prop(o, 'input', () => {
81
+ if (!o.witness) return;
82
+ return EMPTY_BUFFER;
83
+ });
84
+ lazy.prop(o, 'witness', () => {
85
+ if (!a.pubkey) return;
86
+ if (!a.signature) return;
87
+ return [a.signature, a.pubkey];
88
+ });
89
+ // extended validation
90
+ if (opts.validate) {
91
+ let hash = Buffer.from([]);
92
+ if (a.address) {
93
+ if (network && network.bech32 !== _address().prefix)
94
+ throw new TypeError('Invalid prefix or Network mismatch');
95
+ if (_address().version !== 0x00)
96
+ throw new TypeError('Invalid address version');
97
+ if (_address().data.length !== 20)
98
+ throw new TypeError('Invalid address data');
99
+ hash = _address().data;
100
+ }
101
+ if (a.hash) {
102
+ if (hash.length > 0 && !hash.equals(a.hash))
103
+ throw new TypeError('Hash mismatch');
104
+ else hash = a.hash;
105
+ }
106
+ if (a.output) {
107
+ if (
108
+ a.output.length !== 22 ||
109
+ a.output[0] !== OPS.OP_0 ||
110
+ a.output[1] !== 0x14
111
+ )
112
+ throw new TypeError('Output is invalid');
113
+ if (hash.length > 0 && !hash.equals(a.output.slice(2)))
114
+ throw new TypeError('Hash mismatch');
115
+ else hash = a.output.slice(2);
116
+ }
117
+ if (a.pubkey) {
118
+ const pkh = bcrypto.hash160(a.pubkey);
119
+ if (hash.length > 0 && !hash.equals(pkh))
120
+ throw new TypeError('Hash mismatch');
121
+ else hash = pkh;
122
+ if (!(0, types_1.isPoint)(a.pubkey) || a.pubkey.length !== 33)
123
+ throw new TypeError('Invalid pubkey for p2wpkh');
124
+ }
125
+ if (a.witness) {
126
+ if (a.witness.length !== 2)
127
+ throw new TypeError('Witness is invalid');
128
+ if (!bscript.isCanonicalScriptSignature(a.witness[0]))
129
+ throw new TypeError('Witness has invalid signature');
130
+ if (
131
+ !(0, types_1.isPoint)(a.witness[1]) ||
132
+ a.witness[1].length !== 33
133
+ )
134
+ throw new TypeError('Witness has invalid pubkey');
135
+ if (a.signature && !a.signature.equals(a.witness[0]))
136
+ throw new TypeError('Signature mismatch');
137
+ if (a.pubkey && !a.pubkey.equals(a.witness[1]))
138
+ throw new TypeError('Pubkey mismatch');
139
+ const pkh = bcrypto.hash160(a.witness[1]);
140
+ if (hash.length > 0 && !hash.equals(pkh))
141
+ throw new TypeError('Hash mismatch');
142
+ }
143
+ }
144
+ return Object.assign(o, a);
145
+ }
146
+ exports.p2wpkh = p2wpkh;