@btc-vision/bitcoin 6.4.2 → 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.
- package/browser/index.js +1 -1
- package/browser/payments/embed.d.ts +2 -2
- package/browser/payments/index.d.ts +58 -13
- package/browser/payments/lazy.d.ts +1 -1
- package/browser/payments/p2ms.d.ts +2 -2
- package/browser/payments/p2op.d.ts +25 -2
- package/browser/payments/p2pk.d.ts +2 -2
- package/browser/payments/p2pkh.d.ts +2 -2
- package/browser/payments/p2sh.d.ts +2 -2
- package/browser/payments/p2tr.d.ts +2 -2
- package/browser/payments/p2wpkh.d.ts +2 -2
- package/browser/payments/p2wsh.d.ts +2 -2
- package/build/address.js +0 -1
- package/build/payments/embed.d.ts +2 -2
- package/build/payments/embed.js +6 -2
- package/build/payments/index.d.ts +58 -13
- package/build/payments/lazy.d.ts +1 -1
- package/build/payments/p2ms.d.ts +2 -2
- package/build/payments/p2ms.js +8 -2
- package/build/payments/p2op.d.ts +25 -2
- package/build/payments/p2op.js +5 -1
- package/build/payments/p2pk.d.ts +2 -2
- package/build/payments/p2pk.js +5 -1
- package/build/payments/p2pkh.d.ts +2 -2
- package/build/payments/p2pkh.js +5 -1
- package/build/payments/p2sh.d.ts +2 -2
- package/build/payments/p2sh.js +5 -1
- package/build/payments/p2tr.d.ts +2 -2
- package/build/payments/p2tr.js +5 -2
- package/build/payments/p2wpkh.d.ts +2 -2
- package/build/payments/p2wpkh.js +5 -1
- package/build/payments/p2wsh.d.ts +2 -2
- package/build/payments/p2wsh.js +5 -1
- package/build/psbt.js +3 -1
- package/package.json +1 -1
- package/src/address.ts +283 -284
- package/src/payments/embed.ts +61 -55
- package/src/payments/index.ts +159 -68
- package/src/payments/lazy.ts +28 -28
- package/src/payments/p2ms.ts +11 -5
- package/src/payments/p2op.ts +170 -136
- package/src/payments/p2pk.ts +93 -85
- package/src/payments/p2pkh.ts +214 -210
- package/src/payments/p2sh.ts +211 -206
- package/src/payments/p2tr.ts +9 -4
- package/src/payments/p2wpkh.ts +7 -3
- package/src/payments/p2wsh.ts +7 -3
- package/src/psbt.ts +5 -3
- package/test/payments.spec.ts +2 -2
package/src/payments/p2sh.ts
CHANGED
|
@@ -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 {
|
|
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:
|
|
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:
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
if (o.redeem
|
|
113
|
-
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
if (hash.length
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (a.
|
|
131
|
-
if (
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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
|
+
}
|
package/src/payments/p2tr.ts
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
toHashTree,
|
|
14
14
|
tweakKey,
|
|
15
15
|
} from './bip341.js';
|
|
16
|
-
import {
|
|
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:
|
|
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:
|
|
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
|
-
|
|
113
|
+
|
|
114
|
+
return undefined;
|
|
110
115
|
});
|
|
111
116
|
lazy.prop(o, 'output', () => {
|
|
112
117
|
if (!o.pubkey) return;
|
package/src/payments/p2wpkh.ts
CHANGED
|
@@ -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 {
|
|
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:
|
|
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:
|
|
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;
|
package/src/payments/p2wsh.ts
CHANGED
|
@@ -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 {
|
|
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:
|
|
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:
|
|
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;
|
package/src/psbt.ts
CHANGED
|
@@ -23,7 +23,7 @@ import { cloneBuffer, reverseBuffer } from './bufferutils.js';
|
|
|
23
23
|
import { payments } from './index.js';
|
|
24
24
|
import { bitcoin as btcNetwork, Network } from './networks.js';
|
|
25
25
|
import { tapleafHash } from './payments/bip341.js';
|
|
26
|
-
import { Payment, PaymentOpts } from './payments/index.js';
|
|
26
|
+
import { P2SHPayment, Payment, PaymentOpts } from './payments/index.js';
|
|
27
27
|
import {
|
|
28
28
|
checkTaprootInputFields,
|
|
29
29
|
checkTaprootInputForSigs,
|
|
@@ -1284,7 +1284,9 @@ function canFinalize(input: PsbtInput, script: Buffer, scriptType: string): bool
|
|
|
1284
1284
|
case 'witnesspubkeyhash':
|
|
1285
1285
|
return hasSigs(1, input.partialSig);
|
|
1286
1286
|
case 'multisig':
|
|
1287
|
-
const p2ms = payments.p2ms({
|
|
1287
|
+
const p2ms = payments.p2ms({
|
|
1288
|
+
output: script,
|
|
1289
|
+
});
|
|
1288
1290
|
return hasSigs(p2ms.m!, input.partialSig, p2ms.pubkeys);
|
|
1289
1291
|
case 'nonstandard':
|
|
1290
1292
|
return true;
|
|
@@ -1412,7 +1414,7 @@ function scriptCheckerFactory(
|
|
|
1412
1414
|
): void => {
|
|
1413
1415
|
const redeemScriptOutput = payment({
|
|
1414
1416
|
redeem: { output: redeemScript },
|
|
1415
|
-
}).output as Buffer;
|
|
1417
|
+
} as P2SHPayment).output as Buffer;
|
|
1416
1418
|
|
|
1417
1419
|
if (!scriptPubKey.equals(redeemScriptOutput)) {
|
|
1418
1420
|
throw new Error(
|
package/test/payments.spec.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { describe, it } from 'mocha';
|
|
|
4
4
|
import { initEccLib, PaymentCreator } from '../src/index.js';
|
|
5
5
|
import * as u from './payments.utils.js';
|
|
6
6
|
import fs from 'node:fs';
|
|
7
|
-
import { p2pk, p2wsh } from '../src/payments/index.js';
|
|
7
|
+
import { p2pk, P2SHPayment, p2wsh } from '../src/payments/index.js';
|
|
8
8
|
|
|
9
9
|
const require = async (name: string) => {
|
|
10
10
|
const mod = await import(name);
|
|
@@ -68,7 +68,7 @@ const require = async (name: string) => {
|
|
|
68
68
|
),
|
|
69
69
|
}),
|
|
70
70
|
}),
|
|
71
|
-
});
|
|
71
|
+
} as P2SHPayment);
|
|
72
72
|
assert.strictEqual(actual.address, '3MGbrbye4ttNUXM8WAvBFRKry4fkS9fjuw');
|
|
73
73
|
assert.strictEqual(actual.name, 'p2sh-p2wsh-p2pk');
|
|
74
74
|
assert.strictEqual(actual.redeem!.name, 'p2wsh-p2pk');
|