@bitcoinerlab/descriptors-core 3.1.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 (76) hide show
  1. package/README.md +710 -0
  2. package/dist/adapters/applyPR2137.d.ts +2 -0
  3. package/dist/adapters/applyPR2137.js +150 -0
  4. package/dist/adapters/bitcoinjs.d.ts +8 -0
  5. package/dist/adapters/bitcoinjs.js +36 -0
  6. package/dist/adapters/scure/address.d.ts +2 -0
  7. package/dist/adapters/scure/address.js +50 -0
  8. package/dist/adapters/scure/bip32.d.ts +2 -0
  9. package/dist/adapters/scure/bip32.js +16 -0
  10. package/dist/adapters/scure/common.d.ts +14 -0
  11. package/dist/adapters/scure/common.js +36 -0
  12. package/dist/adapters/scure/ecpair.d.ts +2 -0
  13. package/dist/adapters/scure/ecpair.js +58 -0
  14. package/dist/adapters/scure/payments.d.ts +2 -0
  15. package/dist/adapters/scure/payments.js +216 -0
  16. package/dist/adapters/scure/psbt.d.ts +43 -0
  17. package/dist/adapters/scure/psbt.js +382 -0
  18. package/dist/adapters/scure/script.d.ts +20 -0
  19. package/dist/adapters/scure/script.js +163 -0
  20. package/dist/adapters/scure/transaction.d.ts +2 -0
  21. package/dist/adapters/scure/transaction.js +32 -0
  22. package/dist/adapters/scure.d.ts +6 -0
  23. package/dist/adapters/scure.js +37 -0
  24. package/dist/adapters/scureKeys.d.ts +4 -0
  25. package/dist/adapters/scureKeys.js +135 -0
  26. package/dist/bip174.d.ts +87 -0
  27. package/dist/bip174.js +12 -0
  28. package/dist/bitcoinLib.d.ts +385 -0
  29. package/dist/bitcoinLib.js +19 -0
  30. package/dist/bitcoinjs-lib-internals.d.ts +6 -0
  31. package/dist/bitcoinjs-lib-internals.js +60 -0
  32. package/dist/bitcoinjs.d.ts +12 -0
  33. package/dist/bitcoinjs.js +18 -0
  34. package/dist/checksum.d.ts +6 -0
  35. package/dist/checksum.js +58 -0
  36. package/dist/crypto.d.ts +3 -0
  37. package/dist/crypto.js +79 -0
  38. package/dist/descriptors.d.ts +481 -0
  39. package/dist/descriptors.js +1888 -0
  40. package/dist/index.d.ts +23 -0
  41. package/dist/index.js +87 -0
  42. package/dist/keyExpressions.d.ts +124 -0
  43. package/dist/keyExpressions.js +310 -0
  44. package/dist/keyInterfaces.d.ts +5 -0
  45. package/dist/keyInterfaces.js +50 -0
  46. package/dist/ledger.d.ts +183 -0
  47. package/dist/ledger.js +618 -0
  48. package/dist/miniscript.d.ts +125 -0
  49. package/dist/miniscript.js +310 -0
  50. package/dist/multipath.d.ts +13 -0
  51. package/dist/multipath.js +76 -0
  52. package/dist/networkUtils.d.ts +3 -0
  53. package/dist/networkUtils.js +16 -0
  54. package/dist/networks.d.ts +16 -0
  55. package/dist/networks.js +31 -0
  56. package/dist/parseUtils.d.ts +7 -0
  57. package/dist/parseUtils.js +46 -0
  58. package/dist/psbt.d.ts +40 -0
  59. package/dist/psbt.js +228 -0
  60. package/dist/re.d.ts +31 -0
  61. package/dist/re.js +79 -0
  62. package/dist/resourceLimits.d.ts +28 -0
  63. package/dist/resourceLimits.js +84 -0
  64. package/dist/scriptExpressions.d.ts +95 -0
  65. package/dist/scriptExpressions.js +98 -0
  66. package/dist/scure.d.ts +4 -0
  67. package/dist/scure.js +10 -0
  68. package/dist/signers.d.ts +161 -0
  69. package/dist/signers.js +324 -0
  70. package/dist/tapMiniscript.d.ts +231 -0
  71. package/dist/tapMiniscript.js +524 -0
  72. package/dist/tapTree.d.ts +91 -0
  73. package/dist/tapTree.js +166 -0
  74. package/dist/types.d.ts +296 -0
  75. package/dist/types.js +4 -0
  76. package/package.json +148 -0
@@ -0,0 +1,382 @@
1
+ "use strict";
2
+ // Copyright (c) 2026 Jose-Luis Landabaso - https://bitcoinerlab.com
3
+ // Distributed under the MIT software license
4
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
+ if (k2 === undefined) k2 = k;
6
+ var desc = Object.getOwnPropertyDescriptor(m, k);
7
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
+ desc = { enumerable: true, get: function() { return m[k]; } };
9
+ }
10
+ Object.defineProperty(o, k2, desc);
11
+ }) : (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ o[k2] = m[k];
14
+ }));
15
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
17
+ }) : function(o, v) {
18
+ o["default"] = v;
19
+ });
20
+ var __importStar = (this && this.__importStar) || (function () {
21
+ var ownKeys = function(o) {
22
+ ownKeys = Object.getOwnPropertyNames || function (o) {
23
+ var ar = [];
24
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
25
+ return ar;
26
+ };
27
+ return ownKeys(o);
28
+ };
29
+ return function (mod) {
30
+ if (mod && mod.__esModule) return mod;
31
+ var result = {};
32
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
33
+ __setModuleDefault(result, mod);
34
+ return result;
35
+ };
36
+ })();
37
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
38
+ if (kind === "m") throw new TypeError("Private method is not writable");
39
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
40
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
41
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
42
+ };
43
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
44
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
45
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
46
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
47
+ };
48
+ var _ScurePsbtAdapter_tx;
49
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.wrapScureTransaction = wrapScureTransaction;
51
+ const btc = __importStar(require("@scure/btc-signer"));
52
+ const base_1 = require("@scure/base");
53
+ const script_js_1 = require("@scure/btc-signer/script.js");
54
+ const uint8array_tools_1 = require("uint8array-tools");
55
+ const common_1 = require("./common");
56
+ let warnedAboutScureSignatureValidation = false;
57
+ function copyDefinedFields(source, target, keys) {
58
+ for (const key of keys) {
59
+ const value = source[key];
60
+ if (value !== undefined)
61
+ target[key] = source[key];
62
+ }
63
+ }
64
+ function pathArrayToString(path) {
65
+ if (path.length === 0)
66
+ return 'm';
67
+ const parts = path.map(idx => {
68
+ if (idx >= 0x80000000)
69
+ return `${idx - 0x80000000}'`;
70
+ return `${idx}`;
71
+ });
72
+ return `m/${parts.join('/')}`;
73
+ }
74
+ function toScureInputUpdate(input) {
75
+ const result = {};
76
+ copyDefinedFields(input, result, [
77
+ 'nonWitnessUtxo',
78
+ 'redeemScript',
79
+ 'witnessScript',
80
+ 'tapInternalKey',
81
+ 'tapMerkleRoot',
82
+ 'tapKeySig',
83
+ 'sighashType',
84
+ 'finalScriptSig'
85
+ ]);
86
+ if (input.witnessUtxo)
87
+ result.witnessUtxo = {
88
+ script: input.witnessUtxo.script,
89
+ amount: input.witnessUtxo.value
90
+ };
91
+ if (input.bip32Derivation)
92
+ result.bip32Derivation = input.bip32Derivation.map(({ pubkey, masterFingerprint, path }) => [
93
+ pubkey,
94
+ {
95
+ fingerprint: (0, common_1.uint32FromBytesBE)(masterFingerprint),
96
+ path: btc.bip32Path(path)
97
+ }
98
+ ]);
99
+ if (input.tapBip32Derivation)
100
+ result.tapBip32Derivation = input.tapBip32Derivation.map(({ pubkey, masterFingerprint, path, leafHashes }) => [
101
+ pubkey,
102
+ {
103
+ hashes: leafHashes,
104
+ der: {
105
+ fingerprint: (0, common_1.uint32FromBytesBE)(masterFingerprint),
106
+ path: btc.bip32Path(path)
107
+ }
108
+ }
109
+ ]);
110
+ if (input.partialSig)
111
+ result.partialSig = input.partialSig.map(({ pubkey, signature }) => [
112
+ pubkey,
113
+ signature
114
+ ]);
115
+ if (input.tapLeafScript)
116
+ result.tapLeafScript = input.tapLeafScript.map(leaf => {
117
+ const decoded = btc.TaprootControlBlock.decode(leaf.controlBlock);
118
+ const controlBlock = {
119
+ ...decoded,
120
+ version: (decoded.version & 1) | leaf.leafVersion
121
+ };
122
+ return [
123
+ controlBlock,
124
+ (0, uint8array_tools_1.concat)([leaf.script, Uint8Array.from([leaf.leafVersion])])
125
+ ];
126
+ });
127
+ if (input.tapScriptSig)
128
+ result.tapScriptSig = input.tapScriptSig.map(sig => [
129
+ { pubKey: sig.pubkey, leafHash: sig.leafHash },
130
+ sig.signature
131
+ ]);
132
+ if (input.finalScriptWitness)
133
+ result.finalScriptWitness = script_js_1.RawWitness.decode(input.finalScriptWitness);
134
+ return result;
135
+ }
136
+ function scureInputToBitcoinjs(raw) {
137
+ const input = {};
138
+ copyDefinedFields(raw, input, [
139
+ 'redeemScript',
140
+ 'witnessScript',
141
+ 'tapInternalKey',
142
+ 'tapMerkleRoot',
143
+ 'tapKeySig',
144
+ 'sighashType',
145
+ 'finalScriptSig'
146
+ ]);
147
+ if (raw.nonWitnessUtxo)
148
+ input.nonWitnessUtxo = script_js_1.RawTx.encode(raw.nonWitnessUtxo);
149
+ if (raw.witnessUtxo)
150
+ input.witnessUtxo = {
151
+ script: raw.witnessUtxo.script,
152
+ value: raw.witnessUtxo.amount
153
+ };
154
+ if (raw.bip32Derivation)
155
+ input.bip32Derivation = raw.bip32Derivation.map(([pubkey, { fingerprint, path }]) => ({
156
+ pubkey,
157
+ masterFingerprint: (0, common_1.uint32ToBytesBE)(fingerprint),
158
+ path: pathArrayToString(path)
159
+ }));
160
+ if (raw.tapBip32Derivation)
161
+ input.tapBip32Derivation = raw.tapBip32Derivation.map(([pubkey, { hashes, der }]) => ({
162
+ pubkey,
163
+ masterFingerprint: (0, common_1.uint32ToBytesBE)(der.fingerprint),
164
+ path: pathArrayToString(der.path),
165
+ leafHashes: hashes
166
+ }));
167
+ if (raw.partialSig)
168
+ input.partialSig = raw.partialSig.map(([pubkey, signature]) => ({
169
+ pubkey,
170
+ signature
171
+ }));
172
+ if (raw.tapLeafScript)
173
+ input.tapLeafScript = raw.tapLeafScript.map(([controlBlock, scriptWithVersion]) => ({
174
+ controlBlock: btc.TaprootControlBlock.encode(controlBlock),
175
+ script: scriptWithVersion.subarray(0, -1),
176
+ leafVersion: scriptWithVersion[scriptWithVersion.length - 1] ?? 0xc0
177
+ }));
178
+ if (raw.tapScriptSig)
179
+ input.tapScriptSig = raw.tapScriptSig.map(([key, signature]) => ({
180
+ pubkey: key.pubKey,
181
+ leafHash: key.leafHash,
182
+ signature
183
+ }));
184
+ if (raw.finalScriptWitness)
185
+ input.finalScriptWitness = script_js_1.RawWitness.encode(raw.finalScriptWitness);
186
+ return input;
187
+ }
188
+ function requirePrivateKey(signer) {
189
+ if (!signer.privateKey)
190
+ throw new Error('Error: signer must expose a privateKey for scure signing');
191
+ return signer.privateKey;
192
+ }
193
+ function toScureHDKey(hdSigner) {
194
+ if (!hdSigner.privateKey)
195
+ throw new Error('Error: HD signer must expose a privateKey for scure signing');
196
+ return {
197
+ publicKey: hdSigner.publicKey,
198
+ privateKey: hdSigner.privateKey,
199
+ fingerprint: hdSigner.fingerprint instanceof Uint8Array
200
+ ? (0, common_1.uint32FromBytesBE)(hdSigner.fingerprint)
201
+ : hdSigner.fingerprint,
202
+ derive(path) {
203
+ return toScureHDKey(hdSigner.derivePath(path));
204
+ },
205
+ deriveChild(index) {
206
+ return toScureHDKey(hdSigner.derive(index));
207
+ },
208
+ sign: hdSigner.sign.bind(hdSigner)
209
+ };
210
+ }
211
+ class ScurePsbtAdapter {
212
+ constructor(tx) {
213
+ _ScurePsbtAdapter_tx.set(this, void 0);
214
+ __classPrivateFieldSet(this, _ScurePsbtAdapter_tx, tx, "f");
215
+ }
216
+ get raw() {
217
+ return __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f");
218
+ }
219
+ addInput(input) {
220
+ if (!input.hash)
221
+ throw new Error('PSBT input hash is required');
222
+ if (input.index === undefined)
223
+ throw new Error('PSBT input index is required');
224
+ const scureInput = toScureInputUpdate(input);
225
+ // bitcoinjs stores input hash little-endian; scure expects txid order.
226
+ scureInput.txid = Uint8Array.from(input.hash).reverse();
227
+ scureInput.index = input.index;
228
+ if (input.sequence !== undefined)
229
+ scureInput.sequence = input.sequence;
230
+ __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").addInput(scureInput);
231
+ }
232
+ addOutput(output) {
233
+ __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").addOutput({ script: output.script, amount: output.value });
234
+ }
235
+ get inputCount() {
236
+ return __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").inputsLength;
237
+ }
238
+ get data() {
239
+ return {
240
+ inputs: Array.from({ length: __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").inputsLength }, (_value, index) => scureInputToBitcoinjs(__classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").getInput(index)))
241
+ };
242
+ }
243
+ get txInputs() {
244
+ return Array.from({ length: __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").inputsLength }, (_value, index) => {
245
+ const raw = __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").getInput(index);
246
+ if (!raw.txid)
247
+ throw new Error(`PSBT input ${index} missing txid`);
248
+ if (raw.index === undefined)
249
+ throw new Error(`PSBT input ${index} missing index`);
250
+ return {
251
+ // scure keeps txid in natural order; bitcoinjs txInputs.hash uses LE.
252
+ hash: Uint8Array.from(raw.txid).reverse(),
253
+ index: raw.index,
254
+ sequence: raw.sequence ?? 0xffffffff
255
+ };
256
+ });
257
+ }
258
+ setLocktime(locktime) {
259
+ if (!Number.isSafeInteger(locktime) ||
260
+ locktime < 0 ||
261
+ locktime > 0xffffffff)
262
+ throw new Error('Error: locktime must be a uint32');
263
+ const mutableTx = __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f");
264
+ if (!mutableTx.global || typeof mutableTx.global !== 'object')
265
+ throw new Error('Error: @scure/btc-signer Transaction internals changed; cannot set locktime');
266
+ mutableTx.global.fallbackLocktime = locktime;
267
+ }
268
+ get locktime() {
269
+ return __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").lockTime;
270
+ }
271
+ signInput(index, signer) {
272
+ const signed = __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").signIdx(requirePrivateKey(signer), index);
273
+ if (!signed)
274
+ throw new Error(`Input ${index} was not signed`);
275
+ }
276
+ signAllInputs(signer) {
277
+ const signedInputs = __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").sign(requirePrivateKey(signer));
278
+ if (signedInputs === 0)
279
+ throw new Error('No inputs were signed');
280
+ }
281
+ signInputHD(index, hdSigner) {
282
+ const scureHdSigner = hdSigner;
283
+ const input = __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").getInput(index);
284
+ const tapBip32 = input.tapBip32Derivation;
285
+ if (tapBip32 && tapBip32.length > 0) {
286
+ const fp = (0, common_1.uint32FromBytesBE)(hdSigner.fingerprint);
287
+ let matchedFingerprint = false;
288
+ let signed = false;
289
+ for (const [, { der }] of tapBip32) {
290
+ if (der.fingerprint !== fp)
291
+ continue;
292
+ matchedFingerprint = true;
293
+ let derivedNode = scureHdSigner;
294
+ for (const childIndex of der.path)
295
+ derivedNode = derivedNode.derive(childIndex);
296
+ signed =
297
+ __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").signIdx(requirePrivateKey(derivedNode), index) || signed;
298
+ }
299
+ if (!matchedFingerprint)
300
+ throw new Error('No taproot BIP32 derivation matches signer fingerprint');
301
+ if (!signed)
302
+ throw new Error('No inputs were signed');
303
+ }
304
+ else {
305
+ const signed = __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").signIdx(toScureHDKey(scureHdSigner), index);
306
+ if (!signed)
307
+ throw new Error('No inputs were signed');
308
+ }
309
+ }
310
+ signAllInputsHD(hdSigner) {
311
+ let signedInputs = 0;
312
+ for (let i = 0; i < __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").inputsLength; i++) {
313
+ try {
314
+ this.signInputHD(i, hdSigner);
315
+ signedInputs++;
316
+ }
317
+ catch {
318
+ // Keep parity with bitcoinjs signAllInputs*: skip unsignable inputs.
319
+ }
320
+ }
321
+ if (signedInputs === 0)
322
+ throw new Error('No inputs were signed');
323
+ }
324
+ finalizeInput(index, finalizer) {
325
+ if (finalizer) {
326
+ const rawInput = __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").getInput(index);
327
+ if (!rawInput)
328
+ throw new Error(`Invalid input index ${index}`);
329
+ const input = scureInputToBitcoinjs(rawInput);
330
+ const witnessUtxo = input.witnessUtxo;
331
+ const redeemScript = input.redeemScript;
332
+ const witnessScript = input.witnessScript;
333
+ const script = witnessScript ??
334
+ redeemScript ??
335
+ witnessUtxo?.script ??
336
+ new Uint8Array();
337
+ const result = finalizer(index, input, script, !!witnessUtxo, !!redeemScript, !!witnessScript);
338
+ const updateFields = {};
339
+ if (result.finalScriptSig)
340
+ updateFields['finalScriptSig'] = result.finalScriptSig;
341
+ if (result.finalScriptWitness)
342
+ updateFields['finalScriptWitness'] = script_js_1.RawWitness.decode(result.finalScriptWitness);
343
+ __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").updateInput(index, updateFields, true);
344
+ return;
345
+ }
346
+ __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").finalizeIdx(index);
347
+ }
348
+ finalizeTaprootInput(index, tapLeafHashToFinalize, finalizer) {
349
+ if (tapLeafHashToFinalize !== undefined)
350
+ throw new Error('Error: scure adapter does not implement tapLeafHashToFinalize in finalizeTaprootInput');
351
+ const result = finalizer();
352
+ __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").updateInput(index, {
353
+ finalScriptWitness: script_js_1.RawWitness.decode(result.finalScriptWitness)
354
+ }, true);
355
+ }
356
+ validateSignaturesOfInput(index, validator) {
357
+ void validator;
358
+ if (!warnedAboutScureSignatureValidation) {
359
+ console.warn('Warning: @scure/btc-signer adapter does not perform full cryptographic signature validation. Use validate=false to skip validation checks, or use bitcoinjs-lib if full signature validation is required.');
360
+ warnedAboutScureSignatureValidation = true;
361
+ }
362
+ const input = __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").getInput(index);
363
+ if (!input)
364
+ throw new Error(`Invalid input index ${index}`);
365
+ if (input.tapKeySig)
366
+ return true;
367
+ if (Array.isArray(input.tapScriptSig) && input.tapScriptSig.length > 0)
368
+ return true;
369
+ return !!(input.partialSig && input.partialSig.length > 0);
370
+ }
371
+ updateInput(index, data) {
372
+ __classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").updateInput(index, toScureInputUpdate(data));
373
+ }
374
+ toBase64() {
375
+ return base_1.base64.encode(__classPrivateFieldGet(this, _ScurePsbtAdapter_tx, "f").toPSBT());
376
+ }
377
+ }
378
+ _ScurePsbtAdapter_tx = new WeakMap();
379
+ /** Wrap a raw @scure/btc-signer Transaction into a PsbtLike adapter. */
380
+ function wrapScureTransaction(transaction) {
381
+ return new ScurePsbtAdapter(transaction);
382
+ }
@@ -0,0 +1,20 @@
1
+ /** Convert ASM string to script bytes. */
2
+ export declare function fromASM(asm: string): import("micro-packed").Bytes;
3
+ /** Decompile script to array of opcodes (numbers) and data (Uint8Array). */
4
+ export declare function decompile(script: Uint8Array): (number | Uint8Array<ArrayBufferLike>)[] | null;
5
+ /** Count non-push-only opcodes in a script. */
6
+ export declare function countNonPushOnlyOPs(chunks: Array<number | Uint8Array>): number;
7
+ /** Decompile a script into its push-data items. */
8
+ export declare function toStack(scriptBuf: Uint8Array): Uint8Array<ArrayBufferLike>[];
9
+ /** Encode a number for use in Bitcoin Script (CScriptNum format). */
10
+ declare function numberEncode(n: number): import("micro-packed").Bytes;
11
+ export declare function createScureScriptAdapter(): {
12
+ fromASM: typeof fromASM;
13
+ toStack: typeof toStack;
14
+ decompile: typeof decompile;
15
+ countNonPushOnlyOPs: typeof countNonPushOnlyOPs;
16
+ number: {
17
+ encode: typeof numberEncode;
18
+ };
19
+ };
20
+ export {};
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+ // Copyright (c) 2026 Jose-Luis Landabaso - https://bitcoinerlab.com
3
+ // Distributed under the MIT software license
4
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
+ if (k2 === undefined) k2 = k;
6
+ var desc = Object.getOwnPropertyDescriptor(m, k);
7
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
+ desc = { enumerable: true, get: function() { return m[k]; } };
9
+ }
10
+ Object.defineProperty(o, k2, desc);
11
+ }) : (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ o[k2] = m[k];
14
+ }));
15
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
17
+ }) : function(o, v) {
18
+ o["default"] = v;
19
+ });
20
+ var __importStar = (this && this.__importStar) || (function () {
21
+ var ownKeys = function(o) {
22
+ ownKeys = Object.getOwnPropertyNames || function (o) {
23
+ var ar = [];
24
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
25
+ return ar;
26
+ };
27
+ return ownKeys(o);
28
+ };
29
+ return function (mod) {
30
+ if (mod && mod.__esModule) return mod;
31
+ var result = {};
32
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
33
+ __setModuleDefault(result, mod);
34
+ return result;
35
+ };
36
+ })();
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.fromASM = fromASM;
39
+ exports.decompile = decompile;
40
+ exports.countNonPushOnlyOPs = countNonPushOnlyOPs;
41
+ exports.toStack = toStack;
42
+ exports.createScureScriptAdapter = createScureScriptAdapter;
43
+ const btc = __importStar(require("@scure/btc-signer"));
44
+ const base_1 = require("@scure/base");
45
+ const SIGNER_OP_NAMES = new Set();
46
+ for (const key of Object.keys(btc.OP))
47
+ if (isNaN(Number(key)))
48
+ SIGNER_OP_NAMES.add(key);
49
+ function isPushOnlyChunk(chunk) {
50
+ if (chunk instanceof Uint8Array)
51
+ return true;
52
+ return (chunk === btc.OP.OP_0 ||
53
+ chunk === btc.OP['1NEGATE'] ||
54
+ (chunk >= btc.OP.OP_1 && chunk <= btc.OP.OP_16));
55
+ }
56
+ function asmTokenToSignerOp(token) {
57
+ if (token === 'OP_0' || token === 'OP_FALSE')
58
+ return 'OP_0';
59
+ if (token === 'OP_1' || token === 'OP_TRUE')
60
+ return 'OP_1';
61
+ for (let i = 2; i <= 16; i++)
62
+ if (token === `OP_${i}`)
63
+ return `OP_${i}`;
64
+ if (token === '1NEGATE' || token === 'OP_1NEGATE')
65
+ return '1NEGATE';
66
+ if (token.startsWith('OP_')) {
67
+ const stripped = token.slice(3);
68
+ if (SIGNER_OP_NAMES.has(stripped))
69
+ return stripped;
70
+ }
71
+ if (SIGNER_OP_NAMES.has(token))
72
+ return token;
73
+ return undefined;
74
+ }
75
+ /** Convert ASM string to script bytes. */
76
+ function fromASM(asm) {
77
+ const trimmed = asm.trim();
78
+ if (trimmed === '')
79
+ throw new Error('Invalid ASM string');
80
+ const tokens = trimmed.split(/\s+/);
81
+ const scriptElements = [];
82
+ for (const token of tokens) {
83
+ if (token === '')
84
+ continue;
85
+ const op = asmTokenToSignerOp(token);
86
+ if (op !== undefined) {
87
+ scriptElements.push(op);
88
+ }
89
+ else {
90
+ const data = base_1.hex.decode(token);
91
+ if (data.length === 0)
92
+ scriptElements.push('OP_0');
93
+ else if (data.length === 1 && data[0] >= 1 && data[0] <= 16)
94
+ scriptElements.push(`OP_${data[0]}`);
95
+ else if (data.length === 1 && data[0] === 0x81)
96
+ scriptElements.push('1NEGATE');
97
+ else
98
+ scriptElements.push(data);
99
+ }
100
+ }
101
+ return btc.Script.encode(scriptElements);
102
+ }
103
+ /** Decompile script to array of opcodes (numbers) and data (Uint8Array). */
104
+ function decompile(script) {
105
+ try {
106
+ const decoded = btc.Script.decode(script);
107
+ return decoded.map(item => {
108
+ if (typeof item === 'number') {
109
+ if (item === 0)
110
+ return btc.OP.OP_0;
111
+ if (item >= 1 && item <= 16)
112
+ return btc.OP.RESERVED + item;
113
+ return item;
114
+ }
115
+ if (item instanceof Uint8Array)
116
+ return item;
117
+ const opNum = btc.OP[item];
118
+ if (opNum !== undefined)
119
+ return opNum;
120
+ throw new Error('Unknown script token');
121
+ });
122
+ }
123
+ catch {
124
+ return null;
125
+ }
126
+ }
127
+ /** Count non-push-only opcodes in a script. */
128
+ function countNonPushOnlyOPs(chunks) {
129
+ return chunks.length - chunks.filter(isPushOnlyChunk).length;
130
+ }
131
+ /** Decompile a script into its push-data items. */
132
+ function toStack(scriptBuf) {
133
+ const chunks = decompile(scriptBuf);
134
+ if (!chunks)
135
+ throw new Error('Could not decompile script');
136
+ if (!chunks.every(isPushOnlyChunk))
137
+ throw new Error('Non push-only script');
138
+ return chunks.map(chunk => {
139
+ if (typeof chunk === 'number') {
140
+ if (chunk === btc.OP.OP_0)
141
+ return new Uint8Array(0);
142
+ return numberEncode(chunk - btc.OP.RESERVED);
143
+ }
144
+ return chunk;
145
+ });
146
+ }
147
+ /** Encode a number for use in Bitcoin Script (CScriptNum format). */
148
+ function numberEncode(n) {
149
+ if (!Number.isSafeInteger(n))
150
+ throw new Error(`Error: invalid script number ${n}`);
151
+ return btc.ScriptNum().encode(BigInt(n));
152
+ }
153
+ function createScureScriptAdapter() {
154
+ return {
155
+ fromASM,
156
+ toStack,
157
+ decompile,
158
+ countNonPushOnlyOPs,
159
+ number: {
160
+ encode: numberEncode
161
+ }
162
+ };
163
+ }
@@ -0,0 +1,2 @@
1
+ import type { BitcoinLib } from '../../bitcoinLib';
2
+ export declare function createScureTransactionAdapter(): BitcoinLib['Transaction'];
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ // Copyright (c) 2026 Jose-Luis Landabaso - https://bitcoinerlab.com
3
+ // Distributed under the MIT software license
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.createScureTransactionAdapter = createScureTransactionAdapter;
6
+ const base_1 = require("@scure/base");
7
+ const script_js_1 = require("@scure/btc-signer/script.js");
8
+ const crypto_1 = require("../../crypto");
9
+ function parseRawTx(rawBytes) {
10
+ const parsed = script_js_1.RawTx.decode(rawBytes);
11
+ const nonWitnessSerialization = script_js_1.RawOldTx.encode(parsed);
12
+ const txidHash = (0, crypto_1.sha256)((0, crypto_1.sha256)(nonWitnessSerialization));
13
+ const txidBytes = txidHash.slice().reverse();
14
+ return {
15
+ getId: () => base_1.hex.encode(txidBytes),
16
+ outs: parsed.outputs.map(o => ({
17
+ script: o.script,
18
+ value: o.amount
19
+ })),
20
+ toBuffer: () => rawBytes
21
+ };
22
+ }
23
+ function createScureTransactionAdapter() {
24
+ return {
25
+ fromHex(hexStr) {
26
+ return parseRawTx(base_1.hex.decode(hexStr));
27
+ },
28
+ fromBuffer(buf) {
29
+ return parseRawTx(buf);
30
+ }
31
+ };
32
+ }
@@ -0,0 +1,6 @@
1
+ import type { BitcoinLib } from '../bitcoinLib';
2
+ export { wrapScureTransaction } from './scure/psbt';
3
+ /**
4
+ * Create a BitcoinLib backed by @scure/btc-signer.
5
+ */
6
+ export declare function createScureLib(): BitcoinLib;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ // Copyright (c) 2026 Jose-Luis Landabaso - https://bitcoinerlab.com
3
+ // Distributed under the MIT software license
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.wrapScureTransaction = void 0;
6
+ exports.createScureLib = createScureLib;
7
+ /**
8
+ * @scure/btc-signer adapter for BitcoinLib.
9
+ *
10
+ * Wraps @scure/btc-signer and related scure/noble packages into the
11
+ * BitcoinLib interface.
12
+ */
13
+ const secp256k1_js_1 = require("@noble/curves/secp256k1.js");
14
+ const payments_1 = require("./scure/payments");
15
+ const script_1 = require("./scure/script");
16
+ const transaction_1 = require("./scure/transaction");
17
+ const address_1 = require("./scure/address");
18
+ const ecpair_1 = require("./scure/ecpair");
19
+ const bip32_1 = require("./scure/bip32");
20
+ var psbt_1 = require("./scure/psbt");
21
+ Object.defineProperty(exports, "wrapScureTransaction", { enumerable: true, get: function () { return psbt_1.wrapScureTransaction; } });
22
+ /**
23
+ * Create a BitcoinLib backed by @scure/btc-signer.
24
+ */
25
+ function createScureLib() {
26
+ return {
27
+ payments: (0, payments_1.createScurePaymentsAdapter)(),
28
+ script: (0, script_1.createScureScriptAdapter)(),
29
+ Transaction: (0, transaction_1.createScureTransactionAdapter)(),
30
+ address: (0, address_1.createScureAddressAdapter)(),
31
+ ECPair: (0, ecpair_1.createScureECPairAdapter)(),
32
+ BIP32: (0, bip32_1.createScureBIP32Adapter)(),
33
+ verifySchnorr(msghash, pubkey, signature) {
34
+ return secp256k1_js_1.schnorr.verify(signature, msghash, pubkey);
35
+ }
36
+ };
37
+ }
@@ -0,0 +1,4 @@
1
+ import type { BIP32InterfaceLike, ECPairInterfaceLike, ScureHDKeyLike } from '../bitcoinLib';
2
+ export declare function wrapScurePrivateKey(privateKey: Uint8Array, compressed?: boolean): ECPairInterfaceLike;
3
+ export declare function wrapScurePublicKey(publicKey: Uint8Array): ECPairInterfaceLike;
4
+ export declare function wrapScureHDKey(node: ScureHDKeyLike): BIP32InterfaceLike;