@btc-vision/bitcoin 6.4.1 → 6.4.3

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 (55) hide show
  1. package/browser/address.d.ts +1 -0
  2. package/browser/index.js +1 -1
  3. package/browser/payments/embed.d.ts +2 -2
  4. package/browser/payments/index.d.ts +61 -12
  5. package/browser/payments/lazy.d.ts +1 -1
  6. package/browser/payments/p2ms.d.ts +2 -2
  7. package/browser/payments/p2op.d.ts +25 -0
  8. package/browser/payments/p2pk.d.ts +2 -2
  9. package/browser/payments/p2pkh.d.ts +2 -2
  10. package/browser/payments/p2sh.d.ts +2 -2
  11. package/browser/payments/p2tr.d.ts +2 -2
  12. package/browser/payments/p2wpkh.d.ts +2 -2
  13. package/browser/payments/p2wsh.d.ts +2 -2
  14. package/browser/psbt/psbtutils.d.ts +1 -0
  15. package/build/address.js +4 -5
  16. package/build/payments/embed.d.ts +2 -2
  17. package/build/payments/embed.js +6 -2
  18. package/build/payments/index.d.ts +61 -12
  19. package/build/payments/index.js +1 -0
  20. package/build/payments/lazy.d.ts +1 -1
  21. package/build/payments/p2ms.d.ts +2 -2
  22. package/build/payments/p2ms.js +8 -2
  23. package/build/payments/p2op.d.ts +25 -0
  24. package/build/payments/p2op.js +112 -0
  25. package/build/payments/p2pk.d.ts +2 -2
  26. package/build/payments/p2pk.js +5 -1
  27. package/build/payments/p2pkh.d.ts +2 -2
  28. package/build/payments/p2pkh.js +5 -1
  29. package/build/payments/p2sh.d.ts +2 -2
  30. package/build/payments/p2sh.js +5 -1
  31. package/build/payments/p2tr.d.ts +2 -2
  32. package/build/payments/p2tr.js +5 -2
  33. package/build/payments/p2wpkh.d.ts +2 -2
  34. package/build/payments/p2wpkh.js +5 -1
  35. package/build/payments/p2wsh.d.ts +2 -2
  36. package/build/payments/p2wsh.js +5 -1
  37. package/build/psbt/psbtutils.d.ts +1 -0
  38. package/build/psbt/psbtutils.js +2 -0
  39. package/build/psbt.js +3 -1
  40. package/package.json +1 -1
  41. package/src/address.ts +283 -287
  42. package/src/payments/embed.ts +61 -55
  43. package/src/payments/index.ts +110 -13
  44. package/src/payments/lazy.ts +28 -28
  45. package/src/payments/p2ms.ts +11 -5
  46. package/src/payments/p2op.ts +170 -0
  47. package/src/payments/p2pk.ts +93 -85
  48. package/src/payments/p2pkh.ts +214 -210
  49. package/src/payments/p2sh.ts +211 -206
  50. package/src/payments/p2tr.ts +9 -4
  51. package/src/payments/p2wpkh.ts +7 -3
  52. package/src/payments/p2wsh.ts +7 -3
  53. package/src/psbt/psbtutils.ts +322 -320
  54. package/src/psbt.ts +5 -3
  55. package/test/payments.spec.ts +2 -2
@@ -1,206 +1,211 @@
1
- import * as bs58check from 'bs58check';
2
- import * as bcrypto from '../crypto.js';
3
- import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
4
- import * as bscript from '../script.js';
5
- import { stacksEqual, typeforce as typef } from '../types.js';
6
- import { Payment, PaymentFunction, PaymentOpts, Stack, StackFunction } from './index.js';
7
- import * as lazy from './lazy.js';
8
-
9
- const OPS = bscript.OPS;
10
-
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
- export function p2sh(a: Payment, opts?: PaymentOpts): Payment {
23
- if (!a.address && !a.hash && !a.output && !a.redeem && !a.input) {
24
- throw new TypeError('Not enough data');
25
- }
26
- opts = Object.assign({ validate: true }, opts || {});
27
-
28
- typef(
29
- {
30
- network: typef.maybe(typef.Object),
31
-
32
- address: typef.maybe(typef.String),
33
- hash: typef.maybe(typef.BufferN(20)),
34
- output: typef.maybe(typef.BufferN(23)),
35
-
36
- redeem: typef.maybe({
37
- network: typef.maybe(typef.Object),
38
- output: typef.maybe(typef.Buffer),
39
- input: typef.maybe(typef.Buffer),
40
- witness: typef.maybe(typef.arrayOf(typef.Buffer)),
41
- }),
42
- input: typef.maybe(typef.Buffer),
43
- witness: typef.maybe(typef.arrayOf(typef.Buffer)),
44
- },
45
- a,
46
- );
47
-
48
- let network = a.network;
49
- if (!network) {
50
- network = (a.redeem && a.redeem.network) || BITCOIN_NETWORK;
51
- }
52
-
53
- const o: Payment = { network };
54
-
55
- const _address = lazy.value(() => {
56
- const payload = Buffer.from(bs58check.default.decode(a.address!));
57
- const version = payload.readUInt8(0);
58
- const hash = payload.slice(1);
59
- return { version, hash };
60
- });
61
- const _chunks = lazy.value(() => {
62
- return bscript.decompile(a.input!);
63
- }) as StackFunction;
64
- const _redeem = lazy.value((): Payment => {
65
- const chunks = _chunks();
66
- const lastChunk = chunks[chunks.length - 1];
67
- return {
68
- network,
69
- output: lastChunk === OPS.OP_FALSE ? Buffer.from([]) : (lastChunk as Buffer),
70
- input: bscript.compile(chunks.slice(0, -1)),
71
- witness: a.witness || [],
72
- };
73
- }) as PaymentFunction;
74
-
75
- // output dependents
76
- lazy.prop(o, 'address', () => {
77
- if (!o.hash) return;
78
-
79
- const payload = Buffer.allocUnsafe(21);
80
- payload.writeUInt8(o.network!.scriptHash, 0);
81
- o.hash.copy(payload, 1);
82
- return bs58check.default.encode(payload);
83
- });
84
- lazy.prop(o, 'hash', () => {
85
- // in order of least effort
86
- if (a.output) return a.output.slice(2, 22);
87
- if (a.address) return _address().hash;
88
- if (o.redeem && o.redeem.output) return bcrypto.hash160(o.redeem.output);
89
- });
90
- lazy.prop(o, 'output', () => {
91
- if (!o.hash) return;
92
- return bscript.compile([OPS.OP_HASH160, o.hash, OPS.OP_EQUAL]);
93
- });
94
-
95
- // input dependents
96
- lazy.prop(o, 'redeem', () => {
97
- if (!a.input) return;
98
- return _redeem();
99
- });
100
- lazy.prop(o, 'input', () => {
101
- if (!a.redeem || !a.redeem.input || !a.redeem.output) return;
102
- return bscript.compile(
103
- ([] as Stack).concat(bscript.decompile(a.redeem.input) as Stack, a.redeem.output),
104
- );
105
- });
106
- lazy.prop(o, 'witness', () => {
107
- if (o.redeem && o.redeem.witness) return o.redeem.witness;
108
- if (o.input) return [];
109
- });
110
- lazy.prop(o, 'name', () => {
111
- const nameParts = ['p2sh'];
112
- if (o.redeem !== undefined && o.redeem.name !== undefined) nameParts.push(o.redeem.name!);
113
- return nameParts.join('-');
114
- });
115
-
116
- if (opts.validate) {
117
- let hash: Buffer = Buffer.from([]);
118
- if (a.address) {
119
- if (_address().version !== network.scriptHash)
120
- throw new TypeError('Invalid version or Network mismatch');
121
- if (_address().hash.length !== 20) throw new TypeError('Invalid address');
122
- hash = _address().hash;
123
- }
124
-
125
- if (a.hash) {
126
- if (hash.length > 0 && !hash.equals(a.hash)) throw new TypeError('Hash mismatch');
127
- else hash = a.hash;
128
- }
129
-
130
- if (a.output) {
131
- if (
132
- a.output.length !== 23 ||
133
- a.output[0] !== OPS.OP_HASH160 ||
134
- a.output[1] !== 0x14 ||
135
- a.output[22] !== OPS.OP_EQUAL
136
- )
137
- throw new TypeError('Output is invalid');
138
-
139
- const hash2 = a.output.slice(2, 22);
140
- if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch');
141
- else hash = hash2;
142
- }
143
-
144
- // inlined to prevent 'no-inner-declarations' failing
145
- const checkRedeem = (redeem: Payment): void => {
146
- // is the redeem output empty/invalid?
147
- if (redeem.output) {
148
- const decompile = bscript.decompile(redeem.output);
149
- if (!decompile || decompile.length < 1)
150
- throw new TypeError('Redeem.output too short');
151
- if (redeem.output.byteLength > 520)
152
- throw new TypeError('Redeem.output unspendable if larger than 520 bytes');
153
- if (bscript.countNonPushOnlyOPs(decompile) > 201)
154
- throw new TypeError(
155
- 'Redeem.output unspendable with more than 201 non-push ops',
156
- );
157
-
158
- // match hash against other sources
159
- const hash2 = bcrypto.hash160(redeem.output);
160
- if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch');
161
- else hash = hash2;
162
- }
163
-
164
- if (redeem.input) {
165
- const hasInput = redeem.input.length > 0;
166
- const hasWitness = redeem.witness && redeem.witness.length > 0;
167
- if (!hasInput && !hasWitness) throw new TypeError('Empty input');
168
- if (hasInput && hasWitness) throw new TypeError('Input and witness provided');
169
- if (hasInput) {
170
- const richunks = bscript.decompile(redeem.input) as Stack;
171
- if (!bscript.isPushOnly(richunks))
172
- throw new TypeError('Non push-only scriptSig');
173
- }
174
- }
175
- };
176
-
177
- if (a.input) {
178
- const chunks = _chunks();
179
- if (!chunks || chunks.length < 1) throw new TypeError('Input too short');
180
- if (!Buffer.isBuffer(_redeem().output)) throw new TypeError('Input is invalid');
181
-
182
- checkRedeem(_redeem());
183
- }
184
-
185
- if (a.redeem) {
186
- if (a.redeem.network && a.redeem.network !== network)
187
- throw new TypeError('Network mismatch');
188
- if (a.input) {
189
- const redeem = _redeem();
190
- if (a.redeem.output && !a.redeem.output.equals(redeem.output!))
191
- throw new TypeError('Redeem.output mismatch');
192
- if (a.redeem.input && !a.redeem.input.equals(redeem.input!))
193
- throw new TypeError('Redeem.input mismatch');
194
- }
195
-
196
- checkRedeem(a.redeem);
197
- }
198
-
199
- if (a.witness) {
200
- if (a.redeem && a.redeem.witness && !stacksEqual(a.redeem.witness, a.witness))
201
- throw new TypeError('Witness and redeem.witness mismatch');
202
- }
203
- }
204
-
205
- return Object.assign(o, a);
206
- }
1
+ import * as bs58check from 'bs58check';
2
+ import * as bcrypto from '../crypto.js';
3
+ import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
4
+ import * as bscript from '../script.js';
5
+ import { stacksEqual, typeforce as typef } from '../types.js';
6
+ import { P2SHPayment, Payment, PaymentOpts, ScriptRedeem, Stack, StackFunction } from './index.js';
7
+ import * as lazy from './lazy.js';
8
+
9
+ const OPS = bscript.OPS;
10
+
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
+ export function p2sh(a: Omit<P2SHPayment, 'name'>, opts?: PaymentOpts): P2SHPayment {
23
+ if (!a.address && !a.hash && !a.output && !a.redeem && !a.input) {
24
+ throw new TypeError('Not enough data');
25
+ }
26
+ opts = Object.assign({ validate: true }, opts || {});
27
+
28
+ typef(
29
+ {
30
+ network: typef.maybe(typef.Object),
31
+
32
+ address: typef.maybe(typef.String),
33
+ hash: typef.maybe(typef.BufferN(20)),
34
+ output: typef.maybe(typef.BufferN(23)),
35
+
36
+ redeem: typef.maybe({
37
+ network: typef.maybe(typef.Object),
38
+ output: typef.maybe(typef.Buffer),
39
+ input: typef.maybe(typef.Buffer),
40
+ witness: typef.maybe(typef.arrayOf(typef.Buffer)),
41
+ }),
42
+ input: typef.maybe(typef.Buffer),
43
+ witness: typef.maybe(typef.arrayOf(typef.Buffer)),
44
+ },
45
+ a,
46
+ );
47
+
48
+ let network = a.network;
49
+ if (!network) {
50
+ network = (a.redeem && a.redeem.network) || BITCOIN_NETWORK;
51
+ }
52
+
53
+ const o: P2SHPayment = {
54
+ network,
55
+ name: 'p2sh',
56
+ hash: undefined,
57
+ };
58
+
59
+ const _address = lazy.value(() => {
60
+ const payload = Buffer.from(bs58check.default.decode(a.address!));
61
+ const version = payload.readUInt8(0);
62
+ const hash = payload.slice(1);
63
+ return { version, hash };
64
+ });
65
+ const _chunks = lazy.value(() => {
66
+ return bscript.decompile(a.input!);
67
+ }) as StackFunction;
68
+
69
+ const _redeem = lazy.value((): ScriptRedeem => {
70
+ const chunks = _chunks();
71
+ const lastChunk = chunks[chunks.length - 1];
72
+ return {
73
+ network,
74
+ output: lastChunk === OPS.OP_FALSE ? Buffer.from([]) : (lastChunk as Buffer),
75
+ input: bscript.compile(chunks.slice(0, -1)),
76
+ witness: a.witness || [],
77
+ };
78
+ });
79
+
80
+ // output dependents
81
+ lazy.prop(o, 'address', () => {
82
+ if (!o.hash) return;
83
+
84
+ const payload = Buffer.allocUnsafe(21);
85
+ payload.writeUInt8(o.network!.scriptHash, 0);
86
+ o.hash.copy(payload, 1);
87
+ return bs58check.default.encode(payload);
88
+ });
89
+ lazy.prop(o, 'hash', () => {
90
+ // in order of least effort
91
+ if (a.output) return a.output.slice(2, 22);
92
+ if (a.address) return _address().hash;
93
+ if (o.redeem && o.redeem.output) return bcrypto.hash160(o.redeem.output);
94
+ });
95
+ lazy.prop(o, 'output', () => {
96
+ if (!o.hash) return;
97
+ return bscript.compile([OPS.OP_HASH160, o.hash, OPS.OP_EQUAL]);
98
+ });
99
+
100
+ // input dependents
101
+ lazy.prop(o, 'redeem', () => {
102
+ if (!a.input) return;
103
+ return _redeem();
104
+ });
105
+ lazy.prop(o, 'input', () => {
106
+ if (!a.redeem || !a.redeem.input || !a.redeem.output) return;
107
+ return bscript.compile(
108
+ ([] as Stack).concat(bscript.decompile(a.redeem.input) as Stack, a.redeem.output),
109
+ );
110
+ });
111
+ lazy.prop(o, 'witness', () => {
112
+ if (o.redeem && o.redeem.witness) return o.redeem.witness;
113
+ if (o.input) return [];
114
+ });
115
+ lazy.prop(o, 'name', () => {
116
+ const nameParts = ['p2sh'];
117
+ if (o.redeem !== undefined && o.redeem.name !== undefined) nameParts.push(o.redeem.name!);
118
+ return nameParts.join('-');
119
+ });
120
+
121
+ if (opts.validate) {
122
+ let hash: Buffer = Buffer.from([]);
123
+ if (a.address) {
124
+ if (_address().version !== network.scriptHash)
125
+ throw new TypeError('Invalid version or Network mismatch');
126
+ if (_address().hash.length !== 20) throw new TypeError('Invalid address');
127
+ hash = _address().hash;
128
+ }
129
+
130
+ if (a.hash) {
131
+ if (hash.length > 0 && !hash.equals(a.hash)) throw new TypeError('Hash mismatch');
132
+ else hash = a.hash;
133
+ }
134
+
135
+ if (a.output) {
136
+ if (
137
+ a.output.length !== 23 ||
138
+ a.output[0] !== OPS.OP_HASH160 ||
139
+ a.output[1] !== 0x14 ||
140
+ a.output[22] !== OPS.OP_EQUAL
141
+ )
142
+ throw new TypeError('Output is invalid');
143
+
144
+ const hash2 = a.output.slice(2, 22);
145
+ if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch');
146
+ else hash = hash2;
147
+ }
148
+
149
+ // inlined to prevent 'no-inner-declarations' failing
150
+ const checkRedeem = (redeem: Payment): void => {
151
+ // is the redeem output empty/invalid?
152
+ if (redeem.output) {
153
+ const decompile = bscript.decompile(redeem.output);
154
+ if (!decompile || decompile.length < 1)
155
+ throw new TypeError('Redeem.output too short');
156
+ if (redeem.output.byteLength > 520)
157
+ throw new TypeError('Redeem.output unspendable if larger than 520 bytes');
158
+ if (bscript.countNonPushOnlyOPs(decompile) > 201)
159
+ throw new TypeError(
160
+ 'Redeem.output unspendable with more than 201 non-push ops',
161
+ );
162
+
163
+ // match hash against other sources
164
+ const hash2 = bcrypto.hash160(redeem.output);
165
+ if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch');
166
+ else hash = hash2;
167
+ }
168
+
169
+ if (redeem.input) {
170
+ const hasInput = redeem.input.length > 0;
171
+ const hasWitness = redeem.witness && redeem.witness.length > 0;
172
+ if (!hasInput && !hasWitness) throw new TypeError('Empty input');
173
+ if (hasInput && hasWitness) throw new TypeError('Input and witness provided');
174
+ if (hasInput) {
175
+ const richunks = bscript.decompile(redeem.input) as Stack;
176
+ if (!bscript.isPushOnly(richunks))
177
+ throw new TypeError('Non push-only scriptSig');
178
+ }
179
+ }
180
+ };
181
+
182
+ if (a.input) {
183
+ const chunks = _chunks();
184
+ if (!chunks || chunks.length < 1) throw new TypeError('Input too short');
185
+ if (!Buffer.isBuffer(_redeem().output)) throw new TypeError('Input is invalid');
186
+
187
+ checkRedeem(_redeem());
188
+ }
189
+
190
+ if (a.redeem) {
191
+ if (a.redeem.network && a.redeem.network !== network)
192
+ throw new TypeError('Network mismatch');
193
+ if (a.input) {
194
+ const redeem = _redeem();
195
+ if (a.redeem.output && !a.redeem.output.equals(redeem.output!))
196
+ throw new TypeError('Redeem.output mismatch');
197
+ if (a.redeem.input && !a.redeem.input.equals(redeem.input!))
198
+ throw new TypeError('Redeem.input mismatch');
199
+ }
200
+
201
+ checkRedeem(a.redeem);
202
+ }
203
+
204
+ if (a.witness) {
205
+ if (a.redeem && a.redeem.witness && !stacksEqual(a.redeem.witness, a.witness))
206
+ throw new TypeError('Witness and redeem.witness mismatch');
207
+ }
208
+ }
209
+
210
+ return Object.assign(o, a);
211
+ }
@@ -13,7 +13,7 @@ import {
13
13
  toHashTree,
14
14
  tweakKey,
15
15
  } from './bip341.js';
16
- import { Payment, PaymentOpts } from './index.js';
16
+ import { P2TRPayment, PaymentOpts } from './index.js';
17
17
  import * as lazy from './lazy.js';
18
18
 
19
19
  const OPS = bscript.OPS;
@@ -28,7 +28,7 @@ const ANNEX_PREFIX = 0x50;
28
28
  * @returns The P2TR payment object.
29
29
  * @throws {TypeError} If the provided data is invalid or insufficient.
30
30
  */
31
- export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
31
+ export function p2tr(a: Omit<P2TRPayment, 'name'>, opts?: PaymentOpts): P2TRPayment {
32
32
  if (
33
33
  !a.address &&
34
34
  !a.output &&
@@ -82,7 +82,10 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
82
82
  });
83
83
 
84
84
  const network = a.network || BITCOIN_NETWORK;
85
- const o: Payment = { name: 'p2tr', network };
85
+ const o: P2TRPayment = {
86
+ name: 'p2tr',
87
+ network,
88
+ };
86
89
 
87
90
  lazy.prop(o, 'address', () => {
88
91
  if (!o.pubkey) return;
@@ -95,6 +98,7 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
95
98
  lazy.prop(o, 'hash', () => {
96
99
  const hashTree = _hashTree();
97
100
  if (hashTree) return hashTree.hash;
101
+
98
102
  const w = _witness();
99
103
  if (w && w.length > 1) {
100
104
  const controlBlock = w[w.length - 1];
@@ -106,7 +110,8 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
106
110
  });
107
111
  return rootHashFromPath(controlBlock, leafHash);
108
112
  }
109
- return null;
113
+
114
+ return undefined;
110
115
  });
111
116
  lazy.prop(o, 'output', () => {
112
117
  if (!o.pubkey) return;
@@ -3,7 +3,7 @@ import * as bcrypto from '../crypto.js';
3
3
  import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
4
4
  import * as bscript from '../script.js';
5
5
  import { isPoint, typeforce as typef } from '../types.js';
6
- import { Payment, PaymentOpts } from './index.js';
6
+ import { P2WPKHPayment, PaymentOpts } from './index.js';
7
7
  import * as lazy from './lazy.js';
8
8
 
9
9
  const OPS = bscript.OPS;
@@ -21,7 +21,7 @@ const EMPTY_BUFFER = Buffer.alloc(0);
21
21
  * @returns The p2wpkh payment object.
22
22
  * @throws {TypeError} If the required data is missing or invalid.
23
23
  */
24
- export function p2wpkh(a: Payment, opts?: PaymentOpts): Payment {
24
+ export function p2wpkh(a: Omit<P2WPKHPayment, 'name'>, opts?: PaymentOpts): P2WPKHPayment {
25
25
  if (!a.address && !a.hash && !a.output && !a.pubkey && !a.witness)
26
26
  throw new TypeError('Not enough data');
27
27
  opts = Object.assign({ validate: true }, opts || {});
@@ -52,7 +52,11 @@ export function p2wpkh(a: Payment, opts?: PaymentOpts): Payment {
52
52
  });
53
53
 
54
54
  const network = a.network || BITCOIN_NETWORK;
55
- const o: Payment = { name: 'p2wpkh', network };
55
+ const o: P2WPKHPayment = {
56
+ name: 'p2wpkh',
57
+ network,
58
+ hash: undefined,
59
+ };
56
60
 
57
61
  lazy.prop(o, 'address', () => {
58
62
  if (!o.hash) return;
@@ -3,7 +3,7 @@ import * as bcrypto from '../crypto.js';
3
3
  import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
4
4
  import * as bscript from '../script.js';
5
5
  import { isPoint, stacksEqual, typeforce as typef } from '../types.js';
6
- import { Payment, PaymentOpts, StackElement, StackFunction } from './index.js';
6
+ import { P2WSHPayment, PaymentOpts, StackElement, StackFunction } from './index.js';
7
7
  import * as lazy from './lazy.js';
8
8
 
9
9
  const OPS = bscript.OPS;
@@ -29,7 +29,7 @@ function chunkHasUncompressedPubkey(chunk: StackElement): boolean {
29
29
  * @returns The P2WSH payment object.
30
30
  * @throws {TypeError} If the required data is missing or invalid.
31
31
  */
32
- export function p2wsh(a: Payment, opts?: PaymentOpts): Payment {
32
+ export function p2wsh(a: Omit<P2WSHPayment, 'name'>, opts?: PaymentOpts): P2WSHPayment {
33
33
  if (!a.address && !a.hash && !a.output && !a.redeem && !a.witness)
34
34
  throw new TypeError('Not enough data');
35
35
  opts = Object.assign({ validate: true }, opts || {});
@@ -73,7 +73,11 @@ export function p2wsh(a: Payment, opts?: PaymentOpts): Payment {
73
73
  network = (a.redeem && a.redeem.network) || BITCOIN_NETWORK;
74
74
  }
75
75
 
76
- const o: Payment = { network };
76
+ const o: P2WSHPayment = {
77
+ network,
78
+ name: 'p2wsh',
79
+ hash: undefined,
80
+ };
77
81
 
78
82
  lazy.prop(o, 'address', () => {
79
83
  if (!o.hash) return;