@scure/btc-signer 2.0.1 → 2.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.
- package/README.md +334 -64
- package/index.d.ts +15 -6
- package/index.js +16 -7
- package/musig2.d.ts +212 -69
- package/musig2.js +352 -99
- package/net.d.ts +355 -0
- package/net.js +875 -0
- package/p2p.d.ts +17 -8
- package/p2p.js +63 -11
- package/package.json +17 -17
- package/payment.d.ts +406 -41
- package/payment.js +570 -69
- package/psbt.d.ts +2958 -560
- package/psbt.js +475 -119
- package/script.d.ts +311 -133
- package/script.js +313 -90
- package/src/_type_test.ts +69 -0
- package/src/index.ts +34 -11
- package/src/musig2.ts +424 -155
- package/src/net.ts +1106 -0
- package/src/p2p.ts +76 -24
- package/src/payment.ts +882 -235
- package/src/psbt.ts +648 -229
- package/src/script.ts +397 -139
- package/src/transaction.ts +667 -196
- package/src/utils.ts +392 -47
- package/src/utxo.ts +182 -83
- package/transaction.d.ts +242 -32
- package/transaction.js +531 -121
- package/utils.d.ts +296 -25
- package/utils.js +337 -30
- package/utxo.d.ts +438 -76
- package/utxo.js +150 -59
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/musig2.d.ts.map +0 -1
- package/musig2.js.map +0 -1
- package/p2p.d.ts.map +0 -1
- package/p2p.js.map +0 -1
- package/payment.d.ts.map +0 -1
- package/payment.js.map +0 -1
- package/psbt.d.ts.map +0 -1
- package/psbt.js.map +0 -1
- package/script.d.ts.map +0 -1
- package/script.js.map +0 -1
- package/transaction.d.ts.map +0 -1
- package/transaction.js.map +0 -1
- package/utils.d.ts.map +0 -1
- package/utils.js.map +0 -1
- package/utxo.d.ts.map +0 -1
- package/utxo.js.map +0 -1
package/script.d.ts
CHANGED
|
@@ -1,145 +1,282 @@
|
|
|
1
1
|
import * as P from 'micro-packed';
|
|
2
|
-
import { type
|
|
2
|
+
import { type Bytes, type TArg, type TRet, type ValueOf } from './utils.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Maximum byte size allowed for a single pushed script element.
|
|
5
|
+
* BIP 342 keeps this 520-byte stack-element limit even though tapscript removes
|
|
6
|
+
* the old 10,000-byte overall script-size cap.
|
|
7
|
+
*/
|
|
3
8
|
export declare const MAX_SCRIPT_BYTE_LENGTH = 520;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Bitcoin Script opcode table.
|
|
11
|
+
* @example
|
|
12
|
+
* Use opcode numbers when you need the raw byte form instead of Script mnemonics.
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { OP } from '@scure/btc-signer/script.js';
|
|
15
|
+
* new Uint8Array([OP.OP_1, OP.OP_2, OP.CHECKMULTISIG]);
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare const OP: Readonly<{
|
|
19
|
+
OP_0: 0;
|
|
20
|
+
PUSHDATA1: 76;
|
|
21
|
+
PUSHDATA2: 77;
|
|
22
|
+
PUSHDATA4: 78;
|
|
23
|
+
'1NEGATE': 79;
|
|
24
|
+
RESERVED: 80;
|
|
25
|
+
OP_1: 81;
|
|
26
|
+
OP_2: 82;
|
|
27
|
+
OP_3: 83;
|
|
28
|
+
OP_4: 84;
|
|
29
|
+
OP_5: 85;
|
|
30
|
+
OP_6: 86;
|
|
31
|
+
OP_7: 87;
|
|
32
|
+
OP_8: 88;
|
|
33
|
+
OP_9: 89;
|
|
34
|
+
OP_10: 90;
|
|
35
|
+
OP_11: 91;
|
|
36
|
+
OP_12: 92;
|
|
37
|
+
OP_13: 93;
|
|
38
|
+
OP_14: 94;
|
|
39
|
+
OP_15: 95;
|
|
40
|
+
OP_16: 96;
|
|
41
|
+
NOP: 97;
|
|
42
|
+
VER: 98;
|
|
43
|
+
IF: 99;
|
|
44
|
+
NOTIF: 100;
|
|
45
|
+
VERIF: 101;
|
|
46
|
+
VERNOTIF: 102;
|
|
47
|
+
ELSE: 103;
|
|
48
|
+
ENDIF: 104;
|
|
49
|
+
VERIFY: 105;
|
|
50
|
+
RETURN: 106;
|
|
51
|
+
TOALTSTACK: 107;
|
|
52
|
+
FROMALTSTACK: 108;
|
|
53
|
+
'2DROP': 109;
|
|
54
|
+
'2DUP': 110;
|
|
55
|
+
'3DUP': 111;
|
|
56
|
+
'2OVER': 112;
|
|
57
|
+
'2ROT': 113;
|
|
58
|
+
'2SWAP': 114;
|
|
59
|
+
IFDUP: 115;
|
|
60
|
+
DEPTH: 116;
|
|
61
|
+
DROP: 117;
|
|
62
|
+
DUP: 118;
|
|
63
|
+
NIP: 119;
|
|
64
|
+
OVER: 120;
|
|
65
|
+
PICK: 121;
|
|
66
|
+
ROLL: 122;
|
|
67
|
+
ROT: 123;
|
|
68
|
+
SWAP: 124;
|
|
69
|
+
TUCK: 125;
|
|
70
|
+
CAT: 126;
|
|
71
|
+
SUBSTR: 127;
|
|
72
|
+
LEFT: 128;
|
|
73
|
+
RIGHT: 129;
|
|
74
|
+
SIZE: 130;
|
|
75
|
+
INVERT: 131;
|
|
76
|
+
AND: 132;
|
|
77
|
+
OR: 133;
|
|
78
|
+
XOR: 134;
|
|
79
|
+
EQUAL: 135;
|
|
80
|
+
EQUALVERIFY: 136;
|
|
81
|
+
RESERVED1: 137;
|
|
82
|
+
RESERVED2: 138;
|
|
83
|
+
'1ADD': 139;
|
|
84
|
+
'1SUB': 140;
|
|
85
|
+
'2MUL': 141;
|
|
86
|
+
'2DIV': 142;
|
|
87
|
+
NEGATE: 143;
|
|
88
|
+
ABS: 144;
|
|
89
|
+
NOT: 145;
|
|
90
|
+
'0NOTEQUAL': 146;
|
|
91
|
+
ADD: 147;
|
|
92
|
+
SUB: 148;
|
|
93
|
+
MUL: 149;
|
|
94
|
+
DIV: 150;
|
|
95
|
+
MOD: 151;
|
|
96
|
+
LSHIFT: 152;
|
|
97
|
+
RSHIFT: 153;
|
|
98
|
+
BOOLAND: 154;
|
|
99
|
+
BOOLOR: 155;
|
|
100
|
+
NUMEQUAL: 156;
|
|
101
|
+
NUMEQUALVERIFY: 157;
|
|
102
|
+
NUMNOTEQUAL: 158;
|
|
103
|
+
LESSTHAN: 159;
|
|
104
|
+
GREATERTHAN: 160;
|
|
105
|
+
LESSTHANOREQUAL: 161;
|
|
106
|
+
GREATERTHANOREQUAL: 162;
|
|
107
|
+
MIN: 163;
|
|
108
|
+
MAX: 164;
|
|
109
|
+
WITHIN: 165;
|
|
110
|
+
RIPEMD160: 166;
|
|
111
|
+
SHA1: 167;
|
|
112
|
+
SHA256: 168;
|
|
113
|
+
HASH160: 169;
|
|
114
|
+
HASH256: 170;
|
|
115
|
+
CODESEPARATOR: 171;
|
|
116
|
+
CHECKSIG: 172;
|
|
117
|
+
CHECKSIGVERIFY: 173;
|
|
118
|
+
CHECKMULTISIG: 174;
|
|
119
|
+
CHECKMULTISIGVERIFY: 175;
|
|
120
|
+
NOP1: 176;
|
|
121
|
+
CHECKLOCKTIMEVERIFY: 177;
|
|
122
|
+
CHECKSEQUENCEVERIFY: 178;
|
|
123
|
+
NOP4: 179;
|
|
124
|
+
NOP5: 180;
|
|
125
|
+
NOP6: 181;
|
|
126
|
+
NOP7: 182;
|
|
127
|
+
NOP8: 183;
|
|
128
|
+
NOP9: 184;
|
|
129
|
+
NOP10: 185;
|
|
130
|
+
CHECKSIGADD: 186;
|
|
131
|
+
INVALID: 255;
|
|
132
|
+
}>;
|
|
133
|
+
/**
|
|
134
|
+
* Reverse lookup map from opcode numbers back to names.
|
|
135
|
+
* @example
|
|
136
|
+
* Turn parsed opcode numbers back into their mnemonic names.
|
|
137
|
+
* ```ts
|
|
138
|
+
* import { OP, OPNames } from '@scure/btc-signer/script.js';
|
|
139
|
+
* OPNames[OP.CHECKSIG];
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
export declare const OPNames: Record<number, keyof typeof OP>;
|
|
143
|
+
/** Numeric opcode value from {@link OP}. */
|
|
122
144
|
export type OP = ValueOf<typeof OP>;
|
|
145
|
+
/** Single script element accepted by the script encoder. */
|
|
123
146
|
export type ScriptOP = keyof typeof OP | Uint8Array | number;
|
|
147
|
+
/** Parsed Bitcoin script as a list of script elements. */
|
|
124
148
|
export type ScriptType = ScriptOP[];
|
|
149
|
+
/**
|
|
150
|
+
* Coder for Bitcoin Script numbers.
|
|
151
|
+
* bytesLimit only constrains decode. encode still serializes any bigint in
|
|
152
|
+
* Script's signed-magnitude byte form so higher-level consumers can enforce
|
|
153
|
+
* opcode-specific 4-byte or 5-byte bounds separately.
|
|
154
|
+
* @param bytesLimit - maximum decoded length in bytes
|
|
155
|
+
* @param forceMinimal - whether to reject non-minimal encodings
|
|
156
|
+
* @returns Script number coder.
|
|
157
|
+
* @example
|
|
158
|
+
* Encode a small integer using Script number rules.
|
|
159
|
+
* ```ts
|
|
160
|
+
* ScriptNum().encode(1n);
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
125
163
|
export declare function ScriptNum(bytesLimit?: number, forceMinimal?: boolean): P.CoderType<bigint>;
|
|
126
|
-
|
|
127
|
-
|
|
164
|
+
/**
|
|
165
|
+
* Attempts to decode a numeric script element into a JavaScript number.
|
|
166
|
+
* Accepts decoded small integers already represented as JS numbers and pushed
|
|
167
|
+
* `ScriptNum` byte payloads, but does not interpret opcode mnemonics like `1NEGATE`.
|
|
168
|
+
* @param op - script element to decode
|
|
169
|
+
* @param bytesLimit - maximum encoded length in bytes
|
|
170
|
+
* @param forceMinimal - whether to enforce minimal `ScriptNum` encoding
|
|
171
|
+
* @returns Decoded number, or `undefined` when the element is not a JS number or valid `ScriptNum` bytes.
|
|
172
|
+
* @example
|
|
173
|
+
* Decode a script element back into a JavaScript number when possible.
|
|
174
|
+
* ```ts
|
|
175
|
+
* OpToNum(1);
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
export declare function OpToNum(op: TArg<ScriptOP>, bytesLimit?: number, forceMinimal?: boolean): number | undefined;
|
|
179
|
+
/**
|
|
180
|
+
* Returns the pushed-data length for a push opcode.
|
|
181
|
+
* @param op - opcode byte already read from the script stream
|
|
182
|
+
* @param read - callback that reads the following 1/2/4-byte little-endian length
|
|
183
|
+
* @returns Push length for data-carrying opcodes, or `undefined` for non-push opcodes.
|
|
184
|
+
* @throws If the opcode falls through the recognized push-opcode set unexpectedly.
|
|
185
|
+
* {@link Error}
|
|
186
|
+
*/
|
|
187
|
+
export declare const scriptPushLen: (op: number, read: (bytes: 1 | 2 | 4) => number) => number | undefined;
|
|
188
|
+
/**
|
|
189
|
+
* Bitcoin script coder.
|
|
190
|
+
* @example
|
|
191
|
+
* Encode a short script from opcode mnemonics and small integers.
|
|
192
|
+
* ```ts
|
|
193
|
+
* Script.encode(['OP_1', 'OP_2']);
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
export declare const Script: TRet<P.CoderType<ScriptType>>;
|
|
197
|
+
/**
|
|
198
|
+
* Bitcoin CompactSize integer coder.
|
|
199
|
+
* @example
|
|
200
|
+
* Encode a CompactSize integer for wire serialization.
|
|
201
|
+
* ```ts
|
|
202
|
+
* CompactSize.encode(1n);
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
128
205
|
export declare const CompactSize: P.CoderType<bigint>;
|
|
206
|
+
/**
|
|
207
|
+
* CompactSize coder that decodes into JavaScript numbers.
|
|
208
|
+
* @example
|
|
209
|
+
* Use the number-based CompactSize helper when the value fits a JS number.
|
|
210
|
+
* ```ts
|
|
211
|
+
* CompactSizeLen.encode(1);
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
129
214
|
export declare const CompactSizeLen: P.CoderType<number>;
|
|
130
|
-
|
|
131
|
-
|
|
215
|
+
/**
|
|
216
|
+
* Length-prefixed byte array coder.
|
|
217
|
+
* @example
|
|
218
|
+
* Prefix a byte array with its CompactSize length.
|
|
219
|
+
* ```ts
|
|
220
|
+
* VarBytes.encode(new Uint8Array([1, 2, 3]));
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
export declare const VarBytes: TRet<P.CoderType<Bytes>>;
|
|
224
|
+
/**
|
|
225
|
+
* SegWit witness stack coder.
|
|
226
|
+
* @example
|
|
227
|
+
* Encode one witness stack for a SegWit input.
|
|
228
|
+
* ```ts
|
|
229
|
+
* RawWitness.encode([new Uint8Array([1])]);
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
export declare const RawWitness: TRet<P.CoderType<Bytes[]>>;
|
|
233
|
+
/**
|
|
234
|
+
* Coder for CompactSize-prefixed arrays.
|
|
235
|
+
* @param t - element coder
|
|
236
|
+
* @returns Array coder.
|
|
237
|
+
* @example
|
|
238
|
+
* CompactSize-prefix a small list of fixed-width integers.
|
|
239
|
+
* ```ts
|
|
240
|
+
* import * as P from 'micro-packed';
|
|
241
|
+
* import { BTCArray } from '@scure/btc-signer/script.js';
|
|
242
|
+
* BTCArray(P.U8).encode([1, 2, 3]);
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
132
245
|
export declare const BTCArray: <T>(t: P.CoderType<T>) => P.CoderType<T[]>;
|
|
133
|
-
|
|
246
|
+
/**
|
|
247
|
+
* Raw Bitcoin transaction input coder.
|
|
248
|
+
* @example
|
|
249
|
+
* Encode one transaction input exactly as it appears on the wire.
|
|
250
|
+
* ```ts
|
|
251
|
+
* import { hex } from '@scure/base';
|
|
252
|
+
* import { RawInput } from '@scure/btc-signer/script.js';
|
|
253
|
+
* RawInput.encode({
|
|
254
|
+
* txid: hex.decode('0000000000000000000000000000000000000000000000000000000000000001'),
|
|
255
|
+
* index: 0,
|
|
256
|
+
* finalScriptSig: new Uint8Array([0x51]),
|
|
257
|
+
* sequence: 0xffffffff,
|
|
258
|
+
* });
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
261
|
+
export declare const RawInput: Readonly<P.CoderType<P.StructInput<{
|
|
134
262
|
txid: P.Bytes;
|
|
135
263
|
index: number;
|
|
136
|
-
finalScriptSig: Bytes;
|
|
264
|
+
finalScriptSig: P.Bytes;
|
|
137
265
|
sequence: number;
|
|
138
|
-
}
|
|
139
|
-
|
|
266
|
+
}>>>;
|
|
267
|
+
/**
|
|
268
|
+
* Raw Bitcoin transaction output coder.
|
|
269
|
+
* @example
|
|
270
|
+
* Encode one transaction output with amount and scriptPubKey.
|
|
271
|
+
* ```ts
|
|
272
|
+
* import { RawOutput } from '@scure/btc-signer/script.js';
|
|
273
|
+
* RawOutput.encode({ amount: 1n, script: new Uint8Array([0x51]) });
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
276
|
+
export declare const RawOutput: Readonly<P.CoderType<P.StructInput<{
|
|
140
277
|
amount: bigint;
|
|
141
|
-
script: Bytes;
|
|
142
|
-
}
|
|
278
|
+
script: P.Bytes;
|
|
279
|
+
}>>>;
|
|
143
280
|
declare const _RawTx: P.CoderType<P.StructInput<{
|
|
144
281
|
version: number;
|
|
145
282
|
segwitFlag: boolean | undefined;
|
|
@@ -153,11 +290,53 @@ declare const _RawTx: P.CoderType<P.StructInput<{
|
|
|
153
290
|
amount: /*elided*/ any;
|
|
154
291
|
script: /*elided*/ any;
|
|
155
292
|
}>[];
|
|
156
|
-
witnesses: P.Option<Bytes[][]>;
|
|
293
|
+
witnesses: P.Option<P.Bytes[][]>;
|
|
157
294
|
lockTime: number;
|
|
158
295
|
}>>;
|
|
296
|
+
/**
|
|
297
|
+
* Raw Bitcoin transaction coder.
|
|
298
|
+
* @example
|
|
299
|
+
* Encode a SegWit transaction with one input, one output, and one witness stack.
|
|
300
|
+
* ```ts
|
|
301
|
+
* import { hex } from '@scure/base';
|
|
302
|
+
* import { RawTx } from '@scure/btc-signer/script.js';
|
|
303
|
+
* RawTx.encode({
|
|
304
|
+
* version: 2,
|
|
305
|
+
* segwitFlag: true,
|
|
306
|
+
* inputs: [{
|
|
307
|
+
* txid: hex.decode('0000000000000000000000000000000000000000000000000000000000000001'),
|
|
308
|
+
* index: 0,
|
|
309
|
+
* finalScriptSig: new Uint8Array(),
|
|
310
|
+
* sequence: 0xffffffff,
|
|
311
|
+
* }],
|
|
312
|
+
* outputs: [{ amount: 1n, script: new Uint8Array([0x51]) }],
|
|
313
|
+
* witnesses: [[new Uint8Array([1])]],
|
|
314
|
+
* lockTime: 0,
|
|
315
|
+
* });
|
|
316
|
+
* ```
|
|
317
|
+
*/
|
|
159
318
|
export declare const RawTx: typeof _RawTx;
|
|
160
|
-
|
|
319
|
+
/**
|
|
320
|
+
* Pre-SegWit transaction coder used by PSBTv0.
|
|
321
|
+
* @example
|
|
322
|
+
* Encode the legacy unsigned transaction format used inside PSBTv0 globals.
|
|
323
|
+
* ```ts
|
|
324
|
+
* import { hex } from '@scure/base';
|
|
325
|
+
* import { RawOldTx } from '@scure/btc-signer/script.js';
|
|
326
|
+
* RawOldTx.encode({
|
|
327
|
+
* version: 2,
|
|
328
|
+
* inputs: [{
|
|
329
|
+
* txid: hex.decode('0000000000000000000000000000000000000000000000000000000000000001'),
|
|
330
|
+
* index: 0,
|
|
331
|
+
* finalScriptSig: new Uint8Array(),
|
|
332
|
+
* sequence: 0xffffffff,
|
|
333
|
+
* }],
|
|
334
|
+
* outputs: [{ amount: 1n, script: new Uint8Array([0x51]) }],
|
|
335
|
+
* lockTime: 0,
|
|
336
|
+
* });
|
|
337
|
+
* ```
|
|
338
|
+
*/
|
|
339
|
+
export declare const RawOldTx: Readonly<P.CoderType<P.StructInput<{
|
|
161
340
|
version: number;
|
|
162
341
|
inputs: P.StructInput<{
|
|
163
342
|
txid: /*elided*/ any;
|
|
@@ -170,6 +349,5 @@ export declare const RawOldTx: P.CoderType<P.StructInput<{
|
|
|
170
349
|
script: /*elided*/ any;
|
|
171
350
|
}>[];
|
|
172
351
|
lockTime: number;
|
|
173
|
-
}
|
|
352
|
+
}>>>;
|
|
174
353
|
export {};
|
|
175
|
-
//# sourceMappingURL=script.d.ts.map
|