@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,183 @@
1
+ import { OutputConstructor, OutputInstance } from './descriptors';
2
+ import type { PsbtLike, ScureTransactionLike } from './bitcoinLib';
3
+ import { type Network } from './networks';
4
+ import type { TinySecp256k1Interface } from './types';
5
+ /**
6
+ * Dynamically imports the '@ledgerhq/ledger-bitcoin' module and, if provided, checks if `ledgerClient` is an instance of `AppClient`.
7
+ *
8
+ * @async
9
+ * @param {unknown} ledgerClient - An optional parameter that, if provided, is checked to see if it's an instance of `AppClient`.
10
+ * @throws {Error} Throws an error if `ledgerClient` is provided but is not an instance of `AppClient`.
11
+ * @throws {Error} Throws an error if the '@ledgerhq/ledger-bitcoin' module cannot be imported. This typically indicates that the '@ledgerhq/ledger-bitcoin' peer dependency is not installed.
12
+ * @returns {Promise<unknown>} Returns a promise that resolves with the entire '@ledgerhq/ledger-bitcoin' module if it can be successfully imported. We force it to return an unknown type so that the declaration of this function won't break projects that don't use @ledgerhq/ledger-bitcoin as dependency
13
+ *
14
+ * @example
15
+ *
16
+ * importAndValidateLedgerBitcoin(ledgerClient)
17
+ * .then((module) => {
18
+ * const { AppClient, PsbtV2, DefaultWalletPolicy, WalletPolicy, DefaultDescriptorTemplate, PartialSignature } = module;
19
+ * // Use the imported objects...
20
+ * })
21
+ * .catch((error) => console.error(error));
22
+ */
23
+ export declare function importAndValidateLedgerBitcoin(ledgerClient?: unknown): Promise<unknown>;
24
+ /**
25
+ * Verifies if the Ledger device is connected, if the required Bitcoin App is opened,
26
+ * and if the version of the app meets the minimum requirements.
27
+ *
28
+ * @throws Will throw an error if the Ledger device is not connected, the required
29
+ * Bitcoin App is not opened, or if the version is below the required number.
30
+ *
31
+ * @returns Promise<void> - A promise that resolves if all assertions pass, or throws otherwise.
32
+ */
33
+ export declare function assertLedgerApp({ transport, name, minVersion }: {
34
+ /**
35
+ * Connection transport with the Ledger device.
36
+ * One of these: https://github.com/LedgerHQ/ledger-live#libs---libraries
37
+ */
38
+ transport: any;
39
+ /**
40
+ * The name of the Bitcoin App. "Bitcoin" for mainnet or "Bitcoin Test" for testnet.
41
+ */
42
+ name: string;
43
+ /**
44
+ * The minimum acceptable version of the Bitcoin App in semver format (major.minor.patch).
45
+ */
46
+ minVersion: string;
47
+ }): Promise<void>;
48
+ /** @internal */
49
+ export type LedgerPolicy = {
50
+ policyName?: string;
51
+ ledgerTemplate: string;
52
+ keyRoots: string[];
53
+ policyId?: Uint8Array;
54
+ policyHmac?: Uint8Array;
55
+ };
56
+ /**
57
+ * Ledger devices operate in a state-less manner. Therefore, policy information
58
+ * needs to be maintained in a separate data structure, `ledgerState`. For optimization,
59
+ * `ledgerState` also stores cached xpubs and the masterFingerprint.
60
+ */
61
+ export type LedgerState = {
62
+ masterFingerprint?: Uint8Array;
63
+ policies?: LedgerPolicy[];
64
+ xpubs?: {
65
+ [key: string]: string;
66
+ };
67
+ };
68
+ /**
69
+ * State and helpers needed for Ledger integration.
70
+ *
71
+ * Pass the pre-bound `Output` constructor from the package you are using:
72
+ * - `@bitcoinerlab/descriptors`
73
+ * - `@bitcoinerlab/descriptors-scure`
74
+ * - or `DescriptorsFactory(...)` when using `@bitcoinerlab/descriptors-core`
75
+ */
76
+ export type LedgerManager = {
77
+ /** Ledger Bitcoin app client instance. */
78
+ ledgerClient: unknown;
79
+ /** Mutable cache for fingerprints, xpubs and registered policies. */
80
+ ledgerState: LedgerState;
81
+ /** Pre-bound `Output` constructor from the package/backend you are using. */
82
+ Output: OutputConstructor;
83
+ /** Bitcoin network used for descriptor and policy interpretation. */
84
+ network: Network;
85
+ /** @internal @deprecated Use `Output` instead. */
86
+ ecc?: TinySecp256k1Interface;
87
+ };
88
+ /**
89
+ * Retrieves the master fingerprint of a Ledger device.
90
+ */
91
+ export declare function getLedgerMasterFingerPrint({ ledgerManager }: {
92
+ ledgerManager: LedgerManager;
93
+ }): Promise<Uint8Array>;
94
+ /**
95
+ * Retrieves the xpub for a given origin path from a Ledger device.
96
+ */
97
+ export declare function getLedgerXpub({ originPath, ledgerManager }: {
98
+ originPath: string;
99
+ ledgerManager: LedgerManager;
100
+ }): Promise<string>;
101
+ /**
102
+ * Checks whether there is a policy in ledgerState that the ledger
103
+ * could use to sign this psbt input.
104
+ *
105
+ * It found return the policy, otherwise, return undefined
106
+ *
107
+ * All considerations in the header of this file are applied
108
+ */
109
+ export declare function ledgerPolicyFromPsbtInput({ ledgerManager, psbt, index }: {
110
+ ledgerManager: LedgerManager;
111
+ psbt: PsbtLike | ScureTransactionLike;
112
+ index: number;
113
+ }): Promise<LedgerPolicy | undefined>;
114
+ /**
115
+ * Given an output, it extracts its descriptor and converts it to a Ledger
116
+ * Wallet Policy, that is, its keyRoots and template.
117
+ *
118
+ * keyRoots and template follow Ledger's specifications:
119
+ * https://github.com/LedgerHQ/app-bitcoin-new/blob/develop/doc/wallet.md
120
+ *
121
+ * keyRoots and template are a generalization of a descriptor and serve to
122
+ * describe internal and external addresses and any index.
123
+ *
124
+ * So, this function starts from a descriptor and obtains generalized Ledger
125
+ * wallet policy.
126
+ *
127
+ * keyRoots is an array of strings, encoding xpub-type key expressions up to the origin.
128
+ * F.ex.: [76223a6e/48'/1'/0'/2']tpubDE7NQymr4AFtewpAsWtnreyq9ghkzQBXpCZjWLFVRAvnbf7vya2eMTvT2fPapNqL8SuVvLQdbUbMfWLVDCZKnsEBqp6UK93QEzL8Ck23AwF
129
+ *
130
+ * Template encodes the descriptor script expression, where its key
131
+ * expressions are represented using variables for each keyRoot and finished with "/**"
132
+ * (for change 1 or 0 and any index). F.ex.:
133
+ * wsh(sortedmulti(2,@0/**,@1/**)), where @0 corresponds the first element in the keyRoots array.
134
+ *
135
+ * If this descriptor does not contain any key that can be signed with the ledger
136
+ * (non-matching masterFingerprint), then this function returns null.
137
+ *
138
+ * This function takes into account all the considerations regarding Ledger
139
+ * policy implementation details expressed in the header of this file.
140
+ */
141
+ export declare function ledgerPolicyFromOutput({ output, ledgerManager }: {
142
+ output: OutputInstance;
143
+ ledgerManager: LedgerManager;
144
+ }): Promise<{
145
+ ledgerTemplate: string;
146
+ keyRoots: string[];
147
+ } | null>;
148
+ /**
149
+ * Registers a policy based on a provided descriptor.
150
+ *
151
+ * This function will:
152
+ * 1. Store the policy in `ledgerState` inside the `ledgerManager`.
153
+ * 2. Avoid re-registering if the policy was previously registered.
154
+ * 3. Skip registration if the policy is considered "standard".
155
+ *
156
+ * It's important to understand the nature of the Ledger Policy being registered:
157
+ * - While a descriptor might point to a specific output index of a particular change address,
158
+ * the corresponding Ledger Policy abstracts this and represents potential outputs for
159
+ * all addresses (both external and internal).
160
+ * - This means that the registered Ledger Policy is a generalized version of the descriptor,
161
+ * not assuming specific values for the keyPath.
162
+ */
163
+ export declare function registerLedgerWallet({ descriptor, ledgerManager, policyName }: {
164
+ descriptor: string;
165
+ ledgerManager: LedgerManager;
166
+ /** The Name we want to assign to this specific policy */
167
+ policyName: string;
168
+ }): Promise<void>;
169
+ /**
170
+ * Retrieve a standard ledger policy or null if it does correspond.
171
+ **/
172
+ export declare function ledgerPolicyFromStandard({ output, ledgerManager }: {
173
+ output: OutputInstance;
174
+ ledgerManager: LedgerManager;
175
+ }): Promise<LedgerPolicy | null>;
176
+ export declare function comparePolicies(policyA: LedgerPolicy, policyB: LedgerPolicy): boolean;
177
+ /**
178
+ * Retrieve a ledger policy from ledgerState or null if it does not exist yet.
179
+ **/
180
+ export declare function ledgerPolicyFromState({ output, ledgerManager }: {
181
+ output: OutputInstance;
182
+ ledgerManager: LedgerManager;
183
+ }): Promise<LedgerPolicy | null>;