@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/script.d.ts CHANGED
@@ -1,145 +1,282 @@
1
1
  import * as P from 'micro-packed';
2
- import { type ValueOf, type Bytes } from './utils.ts';
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
- export declare const OP: {
5
- OP_0: number;
6
- PUSHDATA1: number;
7
- PUSHDATA2: number;
8
- PUSHDATA4: number;
9
- '1NEGATE': number;
10
- RESERVED: number;
11
- OP_1: number;
12
- OP_2: number;
13
- OP_3: number;
14
- OP_4: number;
15
- OP_5: number;
16
- OP_6: number;
17
- OP_7: number;
18
- OP_8: number;
19
- OP_9: number;
20
- OP_10: number;
21
- OP_11: number;
22
- OP_12: number;
23
- OP_13: number;
24
- OP_14: number;
25
- OP_15: number;
26
- OP_16: number;
27
- NOP: number;
28
- VER: number;
29
- IF: number;
30
- NOTIF: number;
31
- VERIF: number;
32
- VERNOTIF: number;
33
- ELSE: number;
34
- ENDIF: number;
35
- VERIFY: number;
36
- RETURN: number;
37
- TOALTSTACK: number;
38
- FROMALTSTACK: number;
39
- '2DROP': number;
40
- '2DUP': number;
41
- '3DUP': number;
42
- '2OVER': number;
43
- '2ROT': number;
44
- '2SWAP': number;
45
- IFDUP: number;
46
- DEPTH: number;
47
- DROP: number;
48
- DUP: number;
49
- NIP: number;
50
- OVER: number;
51
- PICK: number;
52
- ROLL: number;
53
- ROT: number;
54
- SWAP: number;
55
- TUCK: number;
56
- CAT: number;
57
- SUBSTR: number;
58
- LEFT: number;
59
- RIGHT: number;
60
- SIZE: number;
61
- INVERT: number;
62
- AND: number;
63
- OR: number;
64
- XOR: number;
65
- EQUAL: number;
66
- EQUALVERIFY: number;
67
- RESERVED1: number;
68
- RESERVED2: number;
69
- '1ADD': number;
70
- '1SUB': number;
71
- '2MUL': number;
72
- '2DIV': number;
73
- NEGATE: number;
74
- ABS: number;
75
- NOT: number;
76
- '0NOTEQUAL': number;
77
- ADD: number;
78
- SUB: number;
79
- MUL: number;
80
- DIV: number;
81
- MOD: number;
82
- LSHIFT: number;
83
- RSHIFT: number;
84
- BOOLAND: number;
85
- BOOLOR: number;
86
- NUMEQUAL: number;
87
- NUMEQUALVERIFY: number;
88
- NUMNOTEQUAL: number;
89
- LESSTHAN: number;
90
- GREATERTHAN: number;
91
- LESSTHANOREQUAL: number;
92
- GREATERTHANOREQUAL: number;
93
- MIN: number;
94
- MAX: number;
95
- WITHIN: number;
96
- RIPEMD160: number;
97
- SHA1: number;
98
- SHA256: number;
99
- HASH160: number;
100
- HASH256: number;
101
- CODESEPARATOR: number;
102
- CHECKSIG: number;
103
- CHECKSIGVERIFY: number;
104
- CHECKMULTISIG: number;
105
- CHECKMULTISIGVERIFY: number;
106
- NOP1: number;
107
- CHECKLOCKTIMEVERIFY: number;
108
- CHECKSEQUENCEVERIFY: number;
109
- NOP4: number;
110
- NOP5: number;
111
- NOP6: number;
112
- NOP7: number;
113
- NOP8: number;
114
- NOP9: number;
115
- NOP10: number;
116
- CHECKSIGADD: number;
117
- INVALID: number;
118
- };
119
- export declare const OPNames: {
120
- [x: number]: "OP_0" | "PUSHDATA1" | "PUSHDATA2" | "PUSHDATA4" | "1NEGATE" | "RESERVED" | "OP_1" | "OP_2" | "OP_3" | "OP_4" | "OP_5" | "OP_6" | "OP_7" | "OP_8" | "OP_9" | "OP_10" | "OP_11" | "OP_12" | "OP_13" | "OP_14" | "OP_15" | "OP_16" | "NOP" | "VER" | "IF" | "NOTIF" | "VERIF" | "VERNOTIF" | "ELSE" | "ENDIF" | "VERIFY" | "RETURN" | "TOALTSTACK" | "FROMALTSTACK" | "2DROP" | "2DUP" | "3DUP" | "2OVER" | "2ROT" | "2SWAP" | "IFDUP" | "DEPTH" | "DROP" | "DUP" | "NIP" | "OVER" | "PICK" | "ROLL" | "ROT" | "SWAP" | "TUCK" | "CAT" | "SUBSTR" | "LEFT" | "RIGHT" | "SIZE" | "INVERT" | "AND" | "OR" | "XOR" | "EQUAL" | "EQUALVERIFY" | "RESERVED1" | "RESERVED2" | "1ADD" | "1SUB" | "2MUL" | "2DIV" | "NEGATE" | "ABS" | "NOT" | "0NOTEQUAL" | "ADD" | "SUB" | "MUL" | "DIV" | "MOD" | "LSHIFT" | "RSHIFT" | "BOOLAND" | "BOOLOR" | "NUMEQUAL" | "NUMEQUALVERIFY" | "NUMNOTEQUAL" | "LESSTHAN" | "GREATERTHAN" | "LESSTHANOREQUAL" | "GREATERTHANOREQUAL" | "MIN" | "MAX" | "WITHIN" | "RIPEMD160" | "SHA1" | "SHA256" | "HASH160" | "HASH256" | "CODESEPARATOR" | "CHECKSIG" | "CHECKSIGVERIFY" | "CHECKMULTISIG" | "CHECKMULTISIGVERIFY" | "NOP1" | "CHECKLOCKTIMEVERIFY" | "CHECKSEQUENCEVERIFY" | "NOP4" | "NOP5" | "NOP6" | "NOP7" | "NOP8" | "NOP9" | "NOP10" | "CHECKSIGADD" | "INVALID";
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
- export declare function OpToNum(op: ScriptOP, bytesLimit?: number, forceMinimal?: boolean): number | undefined;
127
- export declare const Script: P.CoderType<ScriptType>;
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
- export declare const VarBytes: P.CoderType<Bytes>;
131
- export declare const RawWitness: P.CoderType<Bytes[]>;
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
- export declare const RawInput: P.CoderType<P.StructInput<{
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
- export declare const RawOutput: P.CoderType<P.StructInput<{
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
- export declare const RawOldTx: P.CoderType<P.StructInput<{
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