@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,231 @@
1
+ import type { BitcoinLib, Taptree } from './bitcoinLib';
2
+ import { type Network } from './networks';
3
+ import type { BIP32APILike, ECPairAPILike } from './bitcoinLib';
4
+ import type { PartialSig, TapBip32Derivation } from './bip174';
5
+ import type { ExpansionMap, KeyInfo, Preimage, TimeConstraints } from './types';
6
+ import type { TapLeafInfo, TapTreeInfoNode, TapTreeNode } from './tapTree';
7
+ /** @internal */
8
+ export type TapLeafExpansionOverride = {
9
+ expandedExpression: string;
10
+ expansionMap: ExpansionMap;
11
+ tapScript: Uint8Array;
12
+ };
13
+ /** @internal */
14
+ export type TaprootLeafSatisfaction = {
15
+ leaf: TapLeafInfo;
16
+ depth: number;
17
+ tapLeafHash: Uint8Array;
18
+ scriptSatisfaction: Uint8Array;
19
+ stackItems: Uint8Array[];
20
+ nLockTime: number | undefined;
21
+ nSequence: number | undefined;
22
+ totalWitnessSize: number;
23
+ };
24
+ /** @internal */
25
+ export type TaprootPsbtLeafMetadata = {
26
+ leaf: TapLeafInfo;
27
+ depth: number;
28
+ tapLeafHash: Uint8Array;
29
+ controlBlock: Uint8Array;
30
+ };
31
+ /**
32
+ * Compiles a taproot miniscript tree into per-leaf metadata.
33
+ * Each leaf contains expanded expression metadata, key expansion map,
34
+ * compiled tapscript and leaf version. This keeps the taproot script-path data
35
+ * ready for satisfactions and witness building.
36
+ *
37
+ * `leafExpansionOverride` allows descriptor-level script expressions to provide:
38
+ * - user-facing expanded expression metadata (for selector/template use), and
39
+ * - custom expansion map and tapscript bytes for compilation.
40
+ *
41
+ * Example: sortedmulti_a can expose `expandedExpression=sortedmulti_a(...)`
42
+ * while providing a tapscript already compiled.
43
+ */
44
+ /** @internal */
45
+ export declare function buildTapTreeInfo({ tapTree, network, BIP32, ECPair, leafExpansionOverride, scriptLib }: {
46
+ tapTree: TapTreeNode;
47
+ network?: Network;
48
+ BIP32: BIP32APILike;
49
+ ECPair: ECPairAPILike;
50
+ leafExpansionOverride: (expression: string) => TapLeafExpansionOverride | undefined;
51
+ scriptLib: BitcoinLib['script'];
52
+ }): TapTreeInfoNode;
53
+ /** @internal */
54
+ export declare function tapTreeInfoToScriptTree(tapTreeInfo: TapTreeInfoNode): Taptree;
55
+ /**
56
+ * Builds taproot PSBT leaf metadata for every leaf in a `tapTreeInfo`.
57
+ *
58
+ * For each leaf, this function computes:
59
+ * - `tapLeafHash`: BIP341 leaf hash of tapscript + leaf version
60
+ * - `depth`: leaf depth in the tree (root children have depth 1)
61
+ * - `controlBlock`: script-path proof used in PSBT `tapLeafScript`
62
+ *
63
+ * The control block layout is:
64
+ *
65
+ * ```text
66
+ * [1-byte (leafVersion | parity)] [32-byte internal key]
67
+ * [32-byte sibling hash #1] ... [32-byte sibling hash #N]
68
+ * ```
69
+ *
70
+ * where:
71
+ * - `parity` is derived from tweaking the internal key with the tree root
72
+ * - sibling hashes are the merkle path from that leaf to the root
73
+ *
74
+ * Example tree:
75
+ *
76
+ * ```text
77
+ * root
78
+ * / \
79
+ * L1 L2
80
+ * / \
81
+ * L3 L4
82
+ * ```
83
+ *
84
+ * Depths:
85
+ * - L1 depth = 1
86
+ * - L3 depth = 2
87
+ * - L4 depth = 2
88
+ *
89
+ * Conceptual output:
90
+ *
91
+ * ```text
92
+ * [
93
+ * L1 -> { depth: 1, tapLeafHash: h1, controlBlock: [v|p, ik, hash(L2)] }
94
+ * L3 -> { depth: 2, tapLeafHash: h3, controlBlock: [v|p, ik, hash(L4), hash(L1)] }
95
+ * L4 -> { depth: 2, tapLeafHash: h4, controlBlock: [v|p, ik, hash(L3), hash(L1)] }
96
+ * ]
97
+ * ```
98
+ *
99
+ * Legend:
100
+ * - `ik`: the 32-byte internal key placed in the control block.
101
+ * - `hash(X)`: the merkle sibling hash at each level when proving leaf `X`.
102
+ *
103
+ * Note: in this diagram, `L2` is a branch node (right subtree), not a leaf,
104
+ * so `hash(L2) = TapBranch(hash(L3), hash(L4))`.
105
+ *
106
+ * Notes:
107
+ * - Leaves are returned in deterministic left-first order.
108
+ * - One metadata entry is returned per leaf.
109
+ * - `controlBlock.length === 33 + 32 * depth`.
110
+ * - Throws if internal key is invalid or control block cannot be derived.
111
+ *
112
+ * Typical usage:
113
+ * - Convert this metadata into PSBT `tapLeafScript[]` entries
114
+ * for all leaves.
115
+ */
116
+ /** @internal */
117
+ export declare function buildTaprootLeafPsbtMetadata({ tapTreeInfo, internalPubkey, payments }: {
118
+ tapTreeInfo: TapTreeInfoNode;
119
+ internalPubkey: Uint8Array;
120
+ payments: BitcoinLib['payments'];
121
+ }): TaprootPsbtLeafMetadata[];
122
+ /**
123
+ * Builds PSBT `tapBip32Derivation` entries for taproot script-path spends.
124
+ *
125
+ * Leaf keys include the list of tapleaf hashes where they appear.
126
+ * If `internalKeyInfo` has derivation data, it is included with empty
127
+ * `leafHashes`.
128
+ *
129
+ * Example tree:
130
+ *
131
+ * ```text
132
+ * root
133
+ * / \
134
+ * L1 L2
135
+ *
136
+ * L1 uses key A
137
+ * L2 uses key A and key B
138
+ *
139
+ * h1 = tapleafHash(L1)
140
+ * h2 = tapleafHash(L2)
141
+ * ```
142
+ *
143
+ * Then output is conceptually:
144
+ *
145
+ * ```text
146
+ * [
147
+ * key A -> leafHashes [h1, h2]
148
+ * key B -> leafHashes [h2]
149
+ * internal key -> leafHashes []
150
+ * ]
151
+ * ```
152
+ *
153
+ * Notes:
154
+ * - Keys missing `masterFingerprint` or `path` are skipped.
155
+ * - Duplicate pubkeys are merged.
156
+ * - If the same pubkey appears with conflicting derivation metadata,
157
+ * this function throws.
158
+ * - Output and `leafHashes` are sorted deterministically.
159
+ */
160
+ /** @internal */
161
+ export declare function buildTaprootBip32Derivations({ tapTreeInfo, internalKeyInfo }: {
162
+ tapTreeInfo: TapTreeInfoNode;
163
+ internalKeyInfo?: KeyInfo;
164
+ }): TapBip32Derivation[];
165
+ /** @internal */
166
+ export declare function normalizeTaprootPubkey(pubkey: Uint8Array): Uint8Array;
167
+ /**
168
+ * Compiles an expanded `sortedmulti_a(...)` leaf expression to its internal
169
+ * `multi_a(...)` form by sorting placeholders using the resolved pubkeys from
170
+ * `expansionMap`.
171
+ */
172
+ /** @internal */
173
+ export declare function compileSortedMultiAExpandedExpression({ expandedExpression, expansionMap }: {
174
+ expandedExpression: string;
175
+ expansionMap: ExpansionMap;
176
+ }): string;
177
+ /**
178
+ * Computes satisfactions for taproot script-path leaves.
179
+ *
180
+ * If `tapLeaf` is undefined, all satisfiable leaves are returned. If `tapLeaf`
181
+ * is provided, only that leaf is considered.
182
+ *
183
+ * Callers are expected to pass real signatures, or fake signatures generated
184
+ * during planning. See satisfyMiniscript() for how timeConstraints keep the
185
+ * chosen leaf consistent between planning and signing.
186
+ */
187
+ /** @internal */
188
+ export declare function collectTaprootLeafSatisfactions({ tapTreeInfo, preimages, signatures, timeConstraints, tapLeaf, scriptLib }: {
189
+ tapTreeInfo: TapTreeInfoNode;
190
+ preimages: Preimage[];
191
+ signatures: PartialSig[];
192
+ timeConstraints?: TimeConstraints;
193
+ tapLeaf?: Uint8Array | string;
194
+ scriptLib: BitcoinLib['script'];
195
+ }): TaprootLeafSatisfaction[];
196
+ /**
197
+ * Selects the taproot leaf satisfaction with the smallest total witness size.
198
+ * Assumes the input list is in left-first tree order for deterministic ties.
199
+ */
200
+ /** @internal */
201
+ export declare function selectBestTaprootLeafSatisfaction(satisfactions: TaprootLeafSatisfaction[]): TaprootLeafSatisfaction;
202
+ /**
203
+ * Collects a unique set of taproot leaf pubkeys (x-only) across the tree.
204
+ * This is useful for building fake signatures when no signer subset is given.
205
+ */
206
+ /** @internal */
207
+ export declare function collectTapTreePubkeys(tapTreeInfo: TapTreeInfoNode): Uint8Array[];
208
+ /**
209
+ * Returns the best satisfaction for a taproot tree, by witness size.
210
+ *
211
+ * If `tapLeaf` is provided, only that leaf is considered. If `tapLeaf` is a
212
+ * bytes, it is treated as a tapLeafHash and must match exactly one leaf. If
213
+ * `tapLeaf` is a string, it is treated as a raw leaf expression and must
214
+ * match exactly one leaf (whitespace-insensitive).
215
+ *
216
+ * This function is typically called twice:
217
+ * 1) Planning pass: call it with fake signatures (built by the caller) to
218
+ * choose the best leaf without requiring user signatures.
219
+ * 2) Signing pass: call it again with real signatures and the timeConstraints
220
+ * returned from the first pass (see satisfyMiniscript() for why this keeps
221
+ * the chosen leaf consistent between planning and signing).
222
+ */
223
+ /** @internal */
224
+ export declare function satisfyTapTree({ tapTreeInfo, signatures, preimages, tapLeaf, timeConstraints, scriptLib }: {
225
+ tapTreeInfo: TapTreeInfoNode;
226
+ signatures: PartialSig[];
227
+ preimages: Preimage[];
228
+ tapLeaf?: Uint8Array | string;
229
+ timeConstraints?: TimeConstraints;
230
+ scriptLib: BitcoinLib['script'];
231
+ }): TaprootLeafSatisfaction;