@bitcoinerlab/descriptors 3.0.6 → 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 (44) hide show
  1. package/README.md +9 -487
  2. package/index.d.ts +13 -0
  3. package/index.js +16 -0
  4. package/package.json +22 -53
  5. package/dist/applyPR2137.d.ts +0 -2
  6. package/dist/applyPR2137.js +0 -153
  7. package/dist/bitcoinjs-lib-internals.d.ts +0 -10
  8. package/dist/bitcoinjs-lib-internals.js +0 -60
  9. package/dist/checksum.d.ts +0 -6
  10. package/dist/checksum.js +0 -58
  11. package/dist/descriptors.d.ts +0 -433
  12. package/dist/descriptors.js +0 -1743
  13. package/dist/index.d.ts +0 -21
  14. package/dist/index.js +0 -85
  15. package/dist/keyExpressions.d.ts +0 -83
  16. package/dist/keyExpressions.js +0 -247
  17. package/dist/ledger.d.ts +0 -167
  18. package/dist/ledger.js +0 -580
  19. package/dist/miniscript.d.ts +0 -123
  20. package/dist/miniscript.js +0 -305
  21. package/dist/multipath.d.ts +0 -13
  22. package/dist/multipath.js +0 -76
  23. package/dist/networkUtils.d.ts +0 -3
  24. package/dist/networkUtils.js +0 -16
  25. package/dist/parseUtils.d.ts +0 -7
  26. package/dist/parseUtils.js +0 -46
  27. package/dist/psbt.d.ts +0 -44
  28. package/dist/psbt.js +0 -193
  29. package/dist/re.d.ts +0 -31
  30. package/dist/re.js +0 -79
  31. package/dist/resourceLimits.d.ts +0 -25
  32. package/dist/resourceLimits.js +0 -89
  33. package/dist/scriptExpressions.d.ts +0 -95
  34. package/dist/scriptExpressions.js +0 -89
  35. package/dist/signers.d.ts +0 -84
  36. package/dist/signers.js +0 -215
  37. package/dist/stackResourceLimits.d.ts +0 -17
  38. package/dist/stackResourceLimits.js +0 -35
  39. package/dist/tapMiniscript.d.ts +0 -220
  40. package/dist/tapMiniscript.js +0 -510
  41. package/dist/tapTree.d.ts +0 -86
  42. package/dist/tapTree.js +0 -166
  43. package/dist/types.d.ts +0 -238
  44. package/dist/types.js +0 -4
@@ -1,510 +0,0 @@
1
- "use strict";
2
- // Distributed under the MIT software license
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.buildTapTreeInfo = buildTapTreeInfo;
5
- exports.tapTreeInfoToScriptTree = tapTreeInfoToScriptTree;
6
- exports.buildTaprootLeafPsbtMetadata = buildTaprootLeafPsbtMetadata;
7
- exports.buildTaprootBip32Derivations = buildTaprootBip32Derivations;
8
- exports.normalizeTaprootPubkey = normalizeTaprootPubkey;
9
- exports.compileSortedMultiAExpandedExpression = compileSortedMultiAExpandedExpression;
10
- exports.collectTaprootLeafSatisfactions = collectTaprootLeafSatisfactions;
11
- exports.selectBestTaprootLeafSatisfaction = selectBestTaprootLeafSatisfaction;
12
- exports.collectTapTreePubkeys = collectTapTreePubkeys;
13
- exports.satisfyTapTree = satisfyTapTree;
14
- const bitcoinjs_lib_1 = require("bitcoinjs-lib");
15
- const bitcoinjs_lib_internals_1 = require("./bitcoinjs-lib-internals");
16
- const varuint_bitcoin_1 = require("varuint-bitcoin");
17
- const uint8array_tools_1 = require("uint8array-tools");
18
- const miniscript_1 = require("./miniscript");
19
- const tapTree_1 = require("./tapTree");
20
- const resourceLimits_1 = require("./resourceLimits");
21
- const TAPROOT_LEAF_VERSION_TAPSCRIPT = 0xc0;
22
- const { p2tr } = bitcoinjs_lib_1.payments;
23
- function expandTaprootMiniscript({ miniscript, network = bitcoinjs_lib_1.networks.bitcoin, BIP32, ECPair }) {
24
- return (0, miniscript_1.expandMiniscript)({
25
- miniscript,
26
- isSegwit: true,
27
- isTaproot: true,
28
- network,
29
- BIP32,
30
- ECPair
31
- });
32
- }
33
- /**
34
- * Compiles a taproot miniscript tree into per-leaf metadata.
35
- * Each leaf contains expanded expression metadata, key expansion map,
36
- * compiled tapscript and leaf version. This keeps the taproot script-path data
37
- * ready for satisfactions and witness building.
38
- *
39
- * `leafExpansionOverride` allows descriptor-level script expressions to provide:
40
- * - user-facing expanded expression metadata (for selector/template use), and
41
- * - custom expansion map and tapscript bytes for compilation.
42
- *
43
- * Example: sortedmulti_a can expose `expandedExpression=sortedmulti_a(...)`
44
- * while providing a tapscript already compiled.
45
- */
46
- function buildTapTreeInfo({ tapTree, network = bitcoinjs_lib_1.networks.bitcoin, BIP32, ECPair, leafExpansionOverride }) {
47
- // Defensive: parseTapTreeExpression() already enforces this for descriptor
48
- // strings, but buildTapTreeInfo is exported and can be called directly.
49
- (0, tapTree_1.assertTapTreeDepth)(tapTree);
50
- if ('expression' in tapTree) {
51
- // Tap-tree leaves store generic descriptor expressions. Most leaves are
52
- // miniscript fragments, but descriptor-level script expressions (such as
53
- // sortedmulti_a) can also appear and be normalized through
54
- // `leafExpansionOverride`.
55
- const expression = tapTree.expression;
56
- const override = leafExpansionOverride(expression);
57
- let expandedExpression;
58
- let expandedMiniscript;
59
- let expansionMap;
60
- let tapScript;
61
- if (override) {
62
- // Descriptor-level expression overrides (e.g. sortedmulti_a) preserve a
63
- // user-facing expanded expression while allowing custom compilation.
64
- expandedExpression = override.expandedExpression;
65
- expansionMap = override.expansionMap;
66
- tapScript = override.tapScript;
67
- }
68
- else {
69
- const expanded = expandTaprootMiniscript({
70
- miniscript: expression,
71
- network,
72
- BIP32,
73
- ECPair
74
- });
75
- expandedExpression = expanded.expandedMiniscript;
76
- expandedMiniscript = expanded.expandedMiniscript;
77
- expansionMap = expanded.expansionMap;
78
- tapScript = (0, miniscript_1.miniscript2Script)({
79
- expandedMiniscript: expanded.expandedMiniscript,
80
- expansionMap,
81
- tapscript: true
82
- });
83
- }
84
- return {
85
- expression,
86
- expandedExpression,
87
- ...(expandedMiniscript !== undefined ? { expandedMiniscript } : {}),
88
- expansionMap,
89
- tapScript,
90
- version: TAPROOT_LEAF_VERSION_TAPSCRIPT
91
- };
92
- }
93
- return {
94
- left: buildTapTreeInfo({
95
- tapTree: tapTree.left,
96
- network,
97
- BIP32,
98
- ECPair,
99
- leafExpansionOverride
100
- }),
101
- right: buildTapTreeInfo({
102
- tapTree: tapTree.right,
103
- network,
104
- BIP32,
105
- ECPair,
106
- leafExpansionOverride
107
- })
108
- };
109
- }
110
- function tapTreeInfoToScriptTree(tapTreeInfo) {
111
- if ('expression' in tapTreeInfo) {
112
- return {
113
- output: tapTreeInfo.tapScript,
114
- version: tapTreeInfo.version
115
- };
116
- }
117
- return [
118
- tapTreeInfoToScriptTree(tapTreeInfo.left),
119
- tapTreeInfoToScriptTree(tapTreeInfo.right)
120
- ];
121
- }
122
- /**
123
- * Builds taproot PSBT leaf metadata for every leaf in a `tapTreeInfo`.
124
- *
125
- * For each leaf, this function computes:
126
- * - `tapLeafHash`: BIP341 leaf hash of tapscript + leaf version
127
- * - `depth`: leaf depth in the tree (root children have depth 1)
128
- * - `controlBlock`: script-path proof used in PSBT `tapLeafScript`
129
- *
130
- * The control block layout is:
131
- *
132
- * ```text
133
- * [1-byte (leafVersion | parity)] [32-byte internal key]
134
- * [32-byte sibling hash #1] ... [32-byte sibling hash #N]
135
- * ```
136
- *
137
- * where:
138
- * - `parity` is derived from tweaking the internal key with the tree root
139
- * - sibling hashes are the merkle path from that leaf to the root
140
- *
141
- * Example tree:
142
- *
143
- * ```text
144
- * root
145
- * / \
146
- * L1 L2
147
- * / \
148
- * L3 L4
149
- * ```
150
- *
151
- * Depths:
152
- * - L1 depth = 1
153
- * - L3 depth = 2
154
- * - L4 depth = 2
155
- *
156
- * Conceptual output:
157
- *
158
- * ```text
159
- * [
160
- * L1 -> { depth: 1, tapLeafHash: h1, controlBlock: [v|p, ik, hash(L2)] }
161
- * L3 -> { depth: 2, tapLeafHash: h3, controlBlock: [v|p, ik, hash(L4), hash(L1)] }
162
- * L4 -> { depth: 2, tapLeafHash: h4, controlBlock: [v|p, ik, hash(L3), hash(L1)] }
163
- * ]
164
- * ```
165
- *
166
- * Legend:
167
- * - `ik`: the 32-byte internal key placed in the control block.
168
- * - `hash(X)`: the merkle sibling hash at each level when proving leaf `X`.
169
- *
170
- * Note: in this diagram, `L2` is a branch node (right subtree), not a leaf,
171
- * so `hash(L2) = TapBranch(hash(L3), hash(L4))`.
172
- *
173
- * Notes:
174
- * - Leaves are returned in deterministic left-first order.
175
- * - One metadata entry is returned per leaf.
176
- * - `controlBlock.length === 33 + 32 * depth`.
177
- * - Throws if internal key is invalid or control block cannot be derived.
178
- *
179
- * Typical usage:
180
- * - Convert this metadata into PSBT `tapLeafScript[]` entries
181
- * for all leaves.
182
- */
183
- function buildTaprootLeafPsbtMetadata({ tapTreeInfo, internalPubkey }) {
184
- const normalizedInternalPubkey = normalizeTaprootPubkey(internalPubkey);
185
- const scriptTree = tapTreeInfoToScriptTree(tapTreeInfo);
186
- return (0, tapTree_1.collectTapTreeLeaves)(tapTreeInfo).map(({ leaf, depth }) => {
187
- if (depth > tapTree_1.MAX_TAPTREE_DEPTH)
188
- throw new Error(`Error: taproot tree depth is too large, ${depth} is larger than ${tapTree_1.MAX_TAPTREE_DEPTH}`);
189
- const tapLeafHash = (0, bitcoinjs_lib_internals_1.tapleafHash)({
190
- output: leaf.tapScript,
191
- version: leaf.version
192
- });
193
- const payment = p2tr({
194
- internalPubkey: normalizedInternalPubkey,
195
- scriptTree,
196
- redeem: {
197
- output: leaf.tapScript,
198
- redeemVersion: leaf.version
199
- }
200
- });
201
- const controlBlock = payment.witness?.[1];
202
- if (!controlBlock)
203
- throw new Error(`Error: could not build controlBlock for taproot leaf ${leaf.expression}`);
204
- return { leaf, depth, tapLeafHash, controlBlock };
205
- });
206
- }
207
- /**
208
- * Builds PSBT `tapBip32Derivation` entries for taproot script-path spends.
209
- *
210
- * Leaf keys include the list of tapleaf hashes where they appear.
211
- * If `internalKeyInfo` has derivation data, it is included with empty
212
- * `leafHashes`.
213
- *
214
- * Example tree:
215
- *
216
- * ```text
217
- * root
218
- * / \
219
- * L1 L2
220
- *
221
- * L1 uses key A
222
- * L2 uses key A and key B
223
- *
224
- * h1 = tapleafHash(L1)
225
- * h2 = tapleafHash(L2)
226
- * ```
227
- *
228
- * Then output is conceptually:
229
- *
230
- * ```text
231
- * [
232
- * key A -> leafHashes [h1, h2]
233
- * key B -> leafHashes [h2]
234
- * internal key -> leafHashes []
235
- * ]
236
- * ```
237
- *
238
- * Notes:
239
- * - Keys missing `masterFingerprint` or `path` are skipped.
240
- * - Duplicate pubkeys are merged.
241
- * - If the same pubkey appears with conflicting derivation metadata,
242
- * this function throws.
243
- * - Output and `leafHashes` are sorted deterministically.
244
- */
245
- function buildTaprootBip32Derivations({ tapTreeInfo, internalKeyInfo }) {
246
- const entries = new Map();
247
- const updateAndInsert = ({ pubkey, masterFingerprint, path, leafHash }) => {
248
- const normalizedPubkey = normalizeTaprootPubkey(pubkey);
249
- const pubkeyHex = (0, uint8array_tools_1.toHex)(normalizedPubkey);
250
- const current = entries.get(pubkeyHex);
251
- if (!current) {
252
- const next = {
253
- masterFingerprint,
254
- pubkey: normalizedPubkey,
255
- path,
256
- leafHashes: new Map()
257
- };
258
- if (leafHash)
259
- next.leafHashes.set((0, uint8array_tools_1.toHex)(leafHash), leafHash);
260
- entries.set(pubkeyHex, next);
261
- return;
262
- }
263
- if ((0, uint8array_tools_1.compare)(current.masterFingerprint, masterFingerprint) !== 0 ||
264
- current.path !== path) {
265
- throw new Error(`Error: inconsistent taproot key derivation metadata for pubkey ${pubkeyHex}`);
266
- }
267
- if (leafHash)
268
- current.leafHashes.set((0, uint8array_tools_1.toHex)(leafHash), leafHash);
269
- };
270
- const leaves = (0, tapTree_1.collectTapTreeLeaves)(tapTreeInfo);
271
- for (const { leaf } of leaves) {
272
- const leafHash = (0, bitcoinjs_lib_internals_1.tapleafHash)({
273
- output: leaf.tapScript,
274
- version: leaf.version
275
- });
276
- for (const keyInfo of Object.values(leaf.expansionMap)) {
277
- if (!keyInfo.pubkey || !keyInfo.masterFingerprint || !keyInfo.path)
278
- continue;
279
- updateAndInsert({
280
- pubkey: keyInfo.pubkey,
281
- masterFingerprint: keyInfo.masterFingerprint,
282
- path: keyInfo.path,
283
- leafHash
284
- });
285
- }
286
- }
287
- if (internalKeyInfo?.pubkey &&
288
- internalKeyInfo.masterFingerprint &&
289
- internalKeyInfo.path) {
290
- updateAndInsert({
291
- pubkey: internalKeyInfo.pubkey,
292
- masterFingerprint: internalKeyInfo.masterFingerprint,
293
- path: internalKeyInfo.path
294
- });
295
- }
296
- return [...entries.entries()]
297
- .sort(([a], [b]) => a.localeCompare(b))
298
- .map(([, entry]) => ({
299
- masterFingerprint: entry.masterFingerprint,
300
- pubkey: entry.pubkey,
301
- path: entry.path,
302
- leafHashes: [...entry.leafHashes.entries()]
303
- .sort(([a], [b]) => a.localeCompare(b))
304
- .map(([, leafHash]) => leafHash)
305
- }));
306
- }
307
- function varSliceSize(someScript) {
308
- const length = someScript.length;
309
- return (0, varuint_bitcoin_1.encodingLength)(length) + length;
310
- }
311
- function vectorSize(someVector) {
312
- const length = someVector.length;
313
- return ((0, varuint_bitcoin_1.encodingLength)(length) +
314
- someVector.reduce((sum, witness) => sum + varSliceSize(witness), 0));
315
- }
316
- function witnessStackSize(witness) {
317
- return vectorSize(witness);
318
- }
319
- function estimateTaprootWitnessSize({ stackItems, tapScript, depth }) {
320
- if (depth > tapTree_1.MAX_TAPTREE_DEPTH)
321
- throw new Error(`Error: taproot tree depth is too large, ${depth} is larger than ${tapTree_1.MAX_TAPTREE_DEPTH}`);
322
- const controlBlock = new Uint8Array(33 + 32 * depth);
323
- return witnessStackSize([...stackItems, tapScript, controlBlock]);
324
- }
325
- function normalizeTaprootPubkey(pubkey) {
326
- if (pubkey.length === 32)
327
- return pubkey;
328
- if (pubkey.length === 33)
329
- return pubkey.slice(1, 33);
330
- throw new Error(`Error: invalid taproot pubkey length`);
331
- }
332
- /**
333
- * Compiles an expanded `sortedmulti_a(...)` leaf expression to its internal
334
- * `multi_a(...)` form by sorting placeholders using the resolved pubkeys from
335
- * `expansionMap`.
336
- */
337
- function compileSortedMultiAExpandedExpression({ expandedExpression, expansionMap }) {
338
- const trimmed = expandedExpression.trim();
339
- const match = trimmed.match(/^sortedmulti_a\((.*)\)$/);
340
- if (!match)
341
- throw new Error(`Error: invalid sortedmulti_a() expression: ${trimmed}`);
342
- const inner = match[1];
343
- if (!inner)
344
- throw new Error(`Error: invalid sortedmulti_a() expression: ${trimmed}`);
345
- const parts = inner.split(',').map(part => part.trim());
346
- if (parts.length < 2)
347
- throw new Error(`Error: invalid sortedmulti_a() expression: ${trimmed}`);
348
- const threshold = parts[0];
349
- if (!threshold)
350
- throw new Error(`Error: invalid sortedmulti_a() threshold: ${trimmed}`);
351
- const placeholders = parts.slice(1);
352
- const sortedPlaceholders = placeholders
353
- .map(placeholder => {
354
- const keyInfo = expansionMap[placeholder];
355
- if (!keyInfo?.pubkey)
356
- throw new Error(`Error: sortedmulti_a() placeholder ${placeholder} not found in expansionMap`);
357
- return { placeholder, pubkey: keyInfo.pubkey };
358
- })
359
- .sort((a, b) => (0, uint8array_tools_1.compare)(a.pubkey, b.pubkey))
360
- .map(({ placeholder }) => placeholder);
361
- return `multi_a(${[threshold, ...sortedPlaceholders].join(',')})`;
362
- }
363
- /**
364
- * Computes satisfactions for taproot script-path leaves.
365
- *
366
- * If `tapLeaf` is undefined, all satisfiable leaves are returned. If `tapLeaf`
367
- * is provided, only that leaf is considered.
368
- *
369
- * Callers are expected to pass real signatures, or fake signatures generated
370
- * during planning. See satisfyMiniscript() for how timeConstraints keep the
371
- * chosen leaf consistent between planning and signing.
372
- */
373
- function collectTaprootLeafSatisfactions({ tapTreeInfo, preimages, signatures, timeConstraints, tapLeaf }) {
374
- const candidates = (0, tapTree_1.selectTapLeafCandidates)({
375
- tapTreeInfo,
376
- ...(tapLeaf !== undefined ? { tapLeaf } : {})
377
- });
378
- const getLeafPubkeys = (leaf) => {
379
- return Object.values(leaf.expansionMap).map(keyInfo => {
380
- if (!keyInfo.pubkey)
381
- throw new Error(`Error: taproot leaf key missing pubkey`);
382
- return normalizeTaprootPubkey(keyInfo.pubkey);
383
- });
384
- };
385
- const resolveLeafSignatures = (leaf) => {
386
- const leafPubkeys = getLeafPubkeys(leaf);
387
- const leafPubkeySet = new Set(leafPubkeys.map(pubkey => (0, uint8array_tools_1.toHex)(pubkey)));
388
- return signatures
389
- .map((sig) => ({
390
- pubkey: normalizeTaprootPubkey(sig.pubkey),
391
- signature: sig.signature
392
- }))
393
- .filter((sig) => leafPubkeySet.has((0, uint8array_tools_1.toHex)(sig.pubkey)));
394
- };
395
- const satisfactions = [];
396
- for (const candidate of candidates) {
397
- const { leaf } = candidate;
398
- const leafSignatures = resolveLeafSignatures(leaf);
399
- try {
400
- let satisfierMiniscript = leaf.expandedMiniscript;
401
- // Most taproot leaves are Miniscript fragments, so `expandedMiniscript`
402
- // is available (and currently matches `expandedExpression`).
403
- // Descriptor-level leaf expressions (currently `sortedmulti_a`)
404
- // expose only `expandedExpression`, so we derive an internal
405
- // Miniscript-equivalent form here before calling `satisfyMiniscript(...)`
406
- if (!satisfierMiniscript) {
407
- if (!/^sortedmulti_a\(/.test(leaf.expandedExpression.trim()))
408
- throw new Error(`Error: taproot leaf does not provide a satisfier miniscript`);
409
- satisfierMiniscript = compileSortedMultiAExpandedExpression({
410
- expandedExpression: leaf.expandedExpression,
411
- expansionMap: leaf.expansionMap
412
- });
413
- }
414
- const { scriptSatisfaction, nLockTime, nSequence } = (0, miniscript_1.satisfyMiniscript)({
415
- expandedMiniscript: satisfierMiniscript,
416
- expansionMap: leaf.expansionMap,
417
- signatures: leafSignatures,
418
- preimages,
419
- ...(timeConstraints !== undefined ? { timeConstraints } : {}),
420
- tapscript: true
421
- });
422
- const satisfactionStackItems = bitcoinjs_lib_1.script.toStack(scriptSatisfaction);
423
- (0, resourceLimits_1.assertTaprootScriptPathSatisfactionResourceLimits)({
424
- stackItems: satisfactionStackItems
425
- });
426
- const totalWitnessSize = estimateTaprootWitnessSize({
427
- stackItems: satisfactionStackItems,
428
- tapScript: leaf.tapScript,
429
- depth: candidate.depth
430
- });
431
- satisfactions.push({
432
- leaf,
433
- depth: candidate.depth,
434
- tapLeafHash: candidate.tapLeafHash,
435
- scriptSatisfaction,
436
- stackItems: satisfactionStackItems,
437
- nLockTime,
438
- nSequence,
439
- totalWitnessSize
440
- });
441
- }
442
- catch (error) {
443
- if (tapLeaf !== undefined)
444
- throw error;
445
- }
446
- }
447
- if (satisfactions.length === 0)
448
- throw new Error(`Error: no satisfiable taproot leaves found`);
449
- return satisfactions;
450
- }
451
- /**
452
- * Selects the taproot leaf satisfaction with the smallest total witness size.
453
- * Assumes the input list is in left-first tree order for deterministic ties.
454
- */
455
- function selectBestTaprootLeafSatisfaction(satisfactions) {
456
- return satisfactions.reduce((best, current) => {
457
- if (!best)
458
- return current;
459
- if (current.totalWitnessSize < best.totalWitnessSize)
460
- return current;
461
- return best;
462
- });
463
- }
464
- /**
465
- * Collects a unique set of taproot leaf pubkeys (x-only) across the tree.
466
- * This is useful for building fake signatures when no signer subset is given.
467
- */
468
- function collectTapTreePubkeys(tapTreeInfo) {
469
- const pubkeySet = new Set();
470
- const pubkeys = [];
471
- const leaves = (0, tapTree_1.collectTapTreeLeaves)(tapTreeInfo);
472
- for (const entry of leaves) {
473
- for (const keyInfo of Object.values(entry.leaf.expansionMap)) {
474
- if (!keyInfo.pubkey)
475
- throw new Error(`Error: taproot leaf key missing pubkey`);
476
- const normalized = normalizeTaprootPubkey(keyInfo.pubkey);
477
- const hex = (0, uint8array_tools_1.toHex)(normalized);
478
- if (pubkeySet.has(hex))
479
- continue;
480
- pubkeySet.add(hex);
481
- pubkeys.push(normalized);
482
- }
483
- }
484
- return pubkeys;
485
- }
486
- /**
487
- * Returns the best satisfaction for a taproot tree, by witness size.
488
- *
489
- * If `tapLeaf` is provided, only that leaf is considered. If `tapLeaf` is a
490
- * bytes, it is treated as a tapLeafHash and must match exactly one leaf. If
491
- * `tapLeaf` is a string, it is treated as a raw leaf expression and must
492
- * match exactly one leaf (whitespace-insensitive).
493
- *
494
- * This function is typically called twice:
495
- * 1) Planning pass: call it with fake signatures (built by the caller) to
496
- * choose the best leaf without requiring user signatures.
497
- * 2) Signing pass: call it again with real signatures and the timeConstraints
498
- * returned from the first pass (see satisfyMiniscript() for why this keeps
499
- * the chosen leaf consistent between planning and signing).
500
- */
501
- function satisfyTapTree({ tapTreeInfo, signatures, preimages, tapLeaf, timeConstraints }) {
502
- const satisfactions = collectTaprootLeafSatisfactions({
503
- tapTreeInfo,
504
- preimages,
505
- signatures,
506
- ...(tapLeaf !== undefined ? { tapLeaf } : {}),
507
- ...(timeConstraints !== undefined ? { timeConstraints } : {})
508
- });
509
- return selectBestTaprootLeafSatisfaction(satisfactions);
510
- }
package/dist/tapTree.d.ts DELETED
@@ -1,86 +0,0 @@
1
- import type { ExpansionMap } from './types';
2
- export type TreeNode<TLeaf> = TLeaf | {
3
- left: TreeNode<TLeaf>;
4
- right: TreeNode<TLeaf>;
5
- };
6
- export type TapLeaf = {
7
- /** Raw leaf expression as written in tr(KEY,TREE). */
8
- expression: string;
9
- };
10
- export type TapTreeNode = TreeNode<TapLeaf>;
11
- export type TapLeafInfo = {
12
- /** Raw leaf expression as written in tr(KEY,TREE). */
13
- expression: string;
14
- /** Expanded descriptor-level expression for this leaf. */
15
- expandedExpression: string;
16
- /**
17
- * Expanded miniscript form when the leaf expression is a miniscript fragment.
18
- *
19
- * For descriptor-level script expressions (e.g. sortedmulti_a), this can be
20
- * undefined even though `tapScript` is available.
21
- */
22
- expandedMiniscript?: string;
23
- expansionMap: ExpansionMap;
24
- tapScript: Uint8Array;
25
- version: number;
26
- };
27
- export type TapTreeInfoNode = TreeNode<TapLeafInfo>;
28
- export type TapLeafSelection = {
29
- leaf: TapLeafInfo;
30
- depth: number;
31
- tapLeafHash: Uint8Array;
32
- };
33
- export declare const MAX_TAPTREE_DEPTH = 128;
34
- export declare function assertTapTreeDepth(tapTree: TapTreeNode): void;
35
- /**
36
- * Collects taproot leaf metadata with depth from a tree.
37
- * Traversal is left-first, following the order of `{left,right}` in the
38
- * expression so tie-breaks are deterministic.
39
- *
40
- * Example tree:
41
- * ```
42
- * {pk(A),{pk(B),pk(C)}}
43
- * ```
44
- * Visual shape:
45
- * ```
46
- * root
47
- * / \
48
- * pk(A) branch
49
- * / \
50
- * pk(B) pk(C)
51
- * ```
52
- * Collected leaves with depth:
53
- * ```
54
- * [
55
- * { leaf: pk(A), depth: 1 },
56
- * { leaf: pk(B), depth: 2 },
57
- * { leaf: pk(C), depth: 2 }
58
- * ]
59
- * ```
60
- */
61
- export declare function collectTapTreeLeaves(tapTreeInfo: TapTreeInfoNode): Array<{
62
- leaf: TapLeafInfo;
63
- depth: number;
64
- }>;
65
- /**
66
- * Resolves taproot leaf candidates based on an optional selector.
67
- *
68
- * If `tapLeaf` is undefined, all leaves are returned for auto-selection.
69
- * If `tapLeaf` is bytes, it is treated as a tapleaf hash and must match
70
- * exactly one leaf.
71
- * If `tapLeaf` is a string, it is treated as a raw taproot leaf expression
72
- * (not expanded). Matching is whitespace-insensitive. If the expression appears
73
- * more than once, this function throws an error.
74
- *
75
- * Example:
76
- * ```
77
- * const candidates = selectTapLeafCandidates({ tapTreeInfo, tapLeaf });
78
- * // tapLeaf can be undefined, bytes (tapleaf hash) or a leaf expression:
79
- * // f.ex.: 'pk(03bb...)'
80
- * ```
81
- */
82
- export declare function selectTapLeafCandidates({ tapTreeInfo, tapLeaf }: {
83
- tapTreeInfo: TapTreeInfoNode;
84
- tapLeaf?: Uint8Array | string;
85
- }): TapLeafSelection[];
86
- export declare function parseTapTreeExpression(expression: string): TapTreeNode;