@bitcoinerlab/descriptors 3.0.6 → 3.1.1
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/README.md +9 -487
- package/index.d.ts +13 -0
- package/index.js +27 -0
- package/package.json +25 -52
- package/dist/applyPR2137.d.ts +0 -2
- package/dist/applyPR2137.js +0 -153
- package/dist/bitcoinjs-lib-internals.d.ts +0 -10
- package/dist/bitcoinjs-lib-internals.js +0 -60
- package/dist/checksum.d.ts +0 -6
- package/dist/checksum.js +0 -58
- package/dist/descriptors.d.ts +0 -433
- package/dist/descriptors.js +0 -1743
- package/dist/index.d.ts +0 -21
- package/dist/index.js +0 -85
- package/dist/keyExpressions.d.ts +0 -83
- package/dist/keyExpressions.js +0 -247
- package/dist/ledger.d.ts +0 -167
- package/dist/ledger.js +0 -580
- package/dist/miniscript.d.ts +0 -123
- package/dist/miniscript.js +0 -305
- package/dist/multipath.d.ts +0 -13
- package/dist/multipath.js +0 -76
- package/dist/networkUtils.d.ts +0 -3
- package/dist/networkUtils.js +0 -16
- package/dist/parseUtils.d.ts +0 -7
- package/dist/parseUtils.js +0 -46
- package/dist/psbt.d.ts +0 -44
- package/dist/psbt.js +0 -193
- package/dist/re.d.ts +0 -31
- package/dist/re.js +0 -79
- package/dist/resourceLimits.d.ts +0 -25
- package/dist/resourceLimits.js +0 -89
- package/dist/scriptExpressions.d.ts +0 -95
- package/dist/scriptExpressions.js +0 -89
- package/dist/signers.d.ts +0 -84
- package/dist/signers.js +0 -215
- package/dist/stackResourceLimits.d.ts +0 -17
- package/dist/stackResourceLimits.js +0 -35
- package/dist/tapMiniscript.d.ts +0 -220
- package/dist/tapMiniscript.js +0 -510
- package/dist/tapTree.d.ts +0 -86
- package/dist/tapTree.js +0 -166
- package/dist/types.d.ts +0 -238
- package/dist/types.js +0 -4
package/README.md
CHANGED
|
@@ -1,499 +1,21 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @bitcoinerlab/descriptors
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Bitcoinjs-ready package for `@bitcoinerlab/descriptors-core`.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install @bitcoinerlab/descriptors @bitcoinerlab/secp256k1 @bitcoinerlab/miniscript-policies
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
This quick example compiles a timelocked Miniscript policy, creates a descriptor address to fund, then builds, signs, and finalizes a PSBT that spends that funded UTXO and prints the final transaction hex.
|
|
12
|
-
|
|
13
|
-
```javascript
|
|
14
|
-
const ecpair = ECPair.makeRandom(); // Creates a signer for a single-key wallet
|
|
15
|
-
|
|
16
|
-
// Timelocked policy: signature + relative timelock (older)
|
|
17
|
-
const { miniscript } = compilePolicy('and(pk(@bob),older(10))');
|
|
18
|
-
|
|
19
|
-
const descriptor = `wsh(${miniscript.replace('@bob', toHex(ecpair.publicKey))})`;
|
|
20
|
-
|
|
21
|
-
// 1) Build the output description
|
|
22
|
-
const fundedOutput = new Output({ descriptor });
|
|
23
|
-
const address = fundedOutput.getAddress(); // Fund this address
|
|
24
|
-
|
|
25
|
-
// 2) Prepare PSBT input/output
|
|
26
|
-
const psbt = new Psbt();
|
|
27
|
-
|
|
28
|
-
const txHex = 'FUNDING_TX_HEX'; // hex of the tx that funded the address above
|
|
29
|
-
const vout = 0; // Output index (vout) of that UTXO within FUNDING_TX_HEX
|
|
30
|
-
const finalizeInput = fundedOutput.updatePsbtAsInput({ psbt, txHex, vout });
|
|
31
|
-
|
|
32
|
-
const recipient = new Output({
|
|
33
|
-
descriptor: 'addr(bc1qgw6xanldsz959z45y4dszehx4xkuzf7nfhya8x)'
|
|
34
|
-
}); // Final address where we'll send the funds after timelock
|
|
35
|
-
recipient.updatePsbtAsOutput({ psbt, value: 10000n }); // input covers this+fees
|
|
36
|
-
|
|
37
|
-
// 3) Sign and finalize
|
|
38
|
-
signers.signECPair({ psbt, ecpair });
|
|
39
|
-
finalizeInput({ psbt });
|
|
40
|
-
|
|
41
|
-
console.log('Push this: ' + psbt.extractTransaction().toHex());
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
## Features
|
|
45
|
-
|
|
46
|
-
- Parses and creates [Bitcoin Descriptors](https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md) (including those based on the [Miniscript language](https://bitcoinerlab.com/modules/miniscript)).
|
|
47
|
-
- Supports Taproot descriptors with trees: `tr(KEY,TREE)` (tapscript).
|
|
48
|
-
- Generates Partially Signed Bitcoin Transactions (PSBTs).
|
|
49
|
-
- Provides PSBT finalizers and signers for single-signature, BIP32 and Hardware Wallets (currently supports Ledger devices; more devices are planned).
|
|
50
|
-
|
|
51
|
-
### Version Compatibility
|
|
52
|
-
|
|
53
|
-
Starting in `3.x`, this library is aligned with the modern bitcoinjs stack (`bitcoinjs-lib 7.x`).
|
|
54
|
-
|
|
55
|
-
In practical terms, this means:
|
|
56
|
-
|
|
57
|
-
- byte arrays are represented as `Uint8Array`;
|
|
58
|
-
- satoshi values are represented as `bigint`.
|
|
59
|
-
|
|
60
|
-
If you need older bitcoinjs versions, keep using `@bitcoinerlab/descriptors 2.x`.
|
|
61
|
-
If you want Taproot trees (`tr(KEY,TREE)`), use `3.x`.
|
|
62
|
-
|
|
63
|
-
## Concepts
|
|
64
|
-
|
|
65
|
-
This library has two main capabilities related to Bitcoin descriptors. Firstly, it can generate `addresses` and `scriptPubKeys` from descriptors. These `addresses` and `scriptPubKeys` can be used to receive funds from other parties. Secondly, the library is able to sign transactions and spend unspent outputs described by those same descriptors. In order to do this, the descriptors must first be set into a PSBT.
|
|
66
|
-
|
|
67
|
-
If you are not familiar with _Bitcoin descriptors_ and _partially signed Bitcoin transactions (PSBTs)_, click on the section below to expand and read more about these concepts.
|
|
68
|
-
|
|
69
|
-
<details>
|
|
70
|
-
<summary>Concepts</summary>
|
|
71
|
-
|
|
72
|
-
### Descriptors
|
|
73
|
-
|
|
74
|
-
In Bitcoin, a transaction consists of a set of inputs that are spent into a different set of outputs. Each input spends an output in a previous transaction. A Bitcoin descriptor is a string of text that describes the rules and conditions required to spend an output in a transaction.
|
|
75
|
-
|
|
76
|
-
For example, `wpkh(02f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9)` is a descriptor that describes a pay-to-witness-public-key-hash (P2WPKH) type of output with the specified public key. If you know the corresponding private key for the transaction for which this descriptor is an output, you can spend it.
|
|
77
|
-
|
|
78
|
-
Descriptors can express much more complex conditions, such as multi-party cooperation, time-locked outputs and more. These conditions can be expressed using the Bitcoin Miniscript language, which is a way of writing Bitcoin Scripts in a structured and more easily understandable way.
|
|
79
|
-
|
|
80
|
-
### Partially Signed Bitcoin Transactions (PSBTs)
|
|
81
|
-
|
|
82
|
-
A PSBT (Partially Signed Bitcoin Transaction) is a format for sharing Bitcoin transactions between different parties.
|
|
83
|
-
|
|
84
|
-
PSBTs come in handy when working with descriptors, especially when using scripts, because they allow multiple parties to collaborate in the signing process. This is especially useful when using hardware wallets or other devices that require separate signatures or authorizations.
|
|
85
|
-
|
|
86
|
-
</details>
|
|
87
|
-
|
|
88
|
-
## Usage
|
|
89
|
-
|
|
90
|
-
Before we dive in, it's worth mentioning that we have several comprehensive guides available covering different aspects of the library. These guides provide explanations and code examples in interactive playgrounds, allowing you to see the changes in the output as you modify the code. This hands-on learning experience, combined with clear explanations, helps you better understand how to use the library effectively. [Check out the available guides here](https://bitcoinerlab.com/guides).
|
|
91
|
-
|
|
92
|
-
Furthermore, we've meticulously documented our API. For an in-depth look into Classes, functions and types, head over [here](https://bitcoinerlab.com/modules/descriptors/api).
|
|
93
|
-
|
|
94
|
-
To use this library (and accompanying libraries), you can install them using:
|
|
5
|
+
For most users this is the default package to install:
|
|
95
6
|
|
|
96
7
|
```bash
|
|
97
8
|
npm install @bitcoinerlab/descriptors
|
|
98
|
-
npm install @bitcoinerlab/miniscript
|
|
99
|
-
npm install @bitcoinerlab/secp256k1
|
|
100
9
|
```
|
|
101
10
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
- The `Output` class is the central component for managing descriptors. It facilitates the creation of outputs to receive funds and enables the signing and finalization of PSBTs (Partially Signed Bitcoin Transactions) for spending UTXOs (Unspent Transaction Outputs).
|
|
105
|
-
- PSBT signers and finalizers, which are used to manage the signing and finalization of PSBTs.
|
|
106
|
-
- `keyExpressions` and `scriptExpressions`, which provide functions to create key and standard descriptor expressions (strings) from structured data.
|
|
107
|
-
- Hardware wallet integration, which provides support for interacting with hardware wallets such as Ledger devices.
|
|
108
|
-
|
|
109
|
-
### Output class
|
|
110
|
-
|
|
111
|
-
The `Output` class is dynamically created by providing a cryptographic secp256k1 engine as shown below:
|
|
112
|
-
|
|
113
|
-
```javascript
|
|
114
|
-
import * as ecc from '@bitcoinerlab/secp256k1';
|
|
115
|
-
import * as descriptors from '@bitcoinerlab/descriptors';
|
|
116
|
-
const { Output } = descriptors.DescriptorsFactory(ecc);
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
Once set up, you can obtain an instance for an output, described by a descriptor such as a `wpkh`, as follows:
|
|
120
|
-
|
|
121
|
-
```javascript
|
|
122
|
-
const wpkhOutput = new Output({
|
|
123
|
-
descriptor:
|
|
124
|
-
'wpkh(02f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9)'
|
|
125
|
-
});
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
For advanced spend-path control (for example `signersPubKeys`, `taprootSpendPath`, and `tapLeaf`), see [Spending-path selection: from WSH Miniscript to Taproot](#spending-path-selection-from-wsh-miniscript-to-taproot).
|
|
129
|
-
|
|
130
|
-
Detailed information about constructor parameters can be found in [the API documentation](https://bitcoinerlab.com/modules/descriptors/api/classes/_Internal_.Output.html#constructor) and in [this Stack Exchange answer](https://bitcoin.stackexchange.com/a/118036/89665).
|
|
131
|
-
|
|
132
|
-
Some commonly used constructor parameters are:
|
|
133
|
-
|
|
134
|
-
- `index`: for ranged descriptors ending in `*`.
|
|
135
|
-
- `change`: for multipath key expressions such as `/**` or `/<0;1>/*`. For example, in `/<0;1>/*`, use `change: 0` or `change: 1`.
|
|
136
|
-
|
|
137
|
-
The `Output` class [offers various helpful methods](https://bitcoinerlab.com/modules/descriptors/api/classes/_Internal_.Output.html), including `getAddress()`, which returns the address associated with the descriptor, `getScriptPubKey()`, which returns the `scriptPubKey` for the descriptor, `expand()`, which decomposes a descriptor into its elemental parts, `updatePsbtAsInput()` and `updatePsbtAsOutput()`.
|
|
138
|
-
|
|
139
|
-
The library supports a wide range of descriptor types, including:
|
|
140
|
-
|
|
141
|
-
- Pay-to-Public-Key-Hash (P2PKH): `pkh(KEY)`
|
|
142
|
-
- Pay-to-Witness-Public-Key-Hash (P2WPKH): `wpkh(KEY)`
|
|
143
|
-
- Pay-to-Script-Hash (P2SH): `sh(SCRIPT)`
|
|
144
|
-
- Pay-to-Witness-Script-Hash (P2WSH): `wsh(SCRIPT)`
|
|
145
|
-
- Pay-to-Taproot (P2TR) with key-only or script tree: `tr(KEY)` and `tr(KEY,TREE)`
|
|
146
|
-
- Address-based descriptors: `addr(ADDRESS)`
|
|
147
|
-
|
|
148
|
-
These descriptors can be used with various key expressions, including raw public keys, BIP32 derivation paths and more.
|
|
149
|
-
|
|
150
|
-
For example, a Taproot descriptor with script leaves can look like:
|
|
151
|
-
|
|
152
|
-
```
|
|
153
|
-
tr(INTERNAL_KEY,{pk(KEY_A),{pk(KEY_B),and_v(v:pk(KEY_C),older(144))}})
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
This means the output has an internal-key spend path and additional script-path options inside the tree.
|
|
157
|
-
|
|
158
|
-
The `updatePsbtAsInput()` method is an essential part of the library, responsible for adding an input to the PSBT corresponding to the UTXO described by the descriptor. Additionally, when the descriptor expresses an absolute time-spending condition, such as "This UTXO can only be spent after block N", `updatePsbtAsInput()` adds timelock information to the PSBT.
|
|
159
|
-
|
|
160
|
-
To call `updatePsbtAsInput()`, use the following syntax:
|
|
161
|
-
|
|
162
|
-
```javascript
|
|
163
|
-
import { Psbt } from 'bitcoinjs-lib';
|
|
164
|
-
const psbt = new Psbt();
|
|
165
|
-
const inputFinalizer = output.updatePsbtAsInput({ psbt, txHex, vout, rbf });
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
Here, `psbt` refers to an instance of the [bitcoinjs-lib Psbt class](https://github.com/bitcoinjs/bitcoinjs-lib). The parameter `txHex` denotes a hex string that serializes the previous transaction containing this output. Meanwhile, `vout` is an integer that marks the position of the output within that transaction. Finally, `rbf` is an optional parameter (defaulting to `true`) used to indicate whether the transaction uses Replace-By-Fee (RBF). When RBF is enabled, transactions can be replaced while they are in the mempool with others that have higher fees. Note that RBF is enabled for the entire transaction if at least one input signals it. Also, note that transactions using relative time locks inherently opt into RBF due to the `nSequence` range used.
|
|
169
|
-
|
|
170
|
-
The method returns the `inputFinalizer()` function. This finalizer function completes a PSBT input by adding the unlocking script (`scriptWitness` or `scriptSig`) that satisfies the previous output's spending conditions. Bear in mind that both `scriptSig` and `scriptWitness` incorporate signatures. As such, you should complete all necessary signing operations before calling `inputFinalizer()`. Detailed [explanations on the `inputFinalizer` method](#signers-and-finalizers-finalize-psbt-input) can be found in the Signers and Finalizers section.
|
|
171
|
-
|
|
172
|
-
Similarly, `updatePsbtAsOutput` allows you to add an output to a PSBT. For instance, to configure a `psbt` that sends `10,000` sats to the SegWit address `bc1qgw6xanldsz959z45y4dszehx4xkuzf7nfhya8x`:
|
|
173
|
-
|
|
174
|
-
```javascript
|
|
175
|
-
const recipientOutput = new Output({
|
|
176
|
-
descriptor: `addr(bc1qgw6xanldsz959z45y4dszehx4xkuzf7nfhya8x)`
|
|
177
|
-
});
|
|
178
|
-
recipientOutput.updatePsbtAsOutput({ psbt, value: 10000n });
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
For further information on using the `Output` class, refer to the [comprehensive guides](https://bitcoinerlab.com/guides) that offer explanations and playgrounds to help learn the module. For specific details on the methods, refer directly to [the API](https://bitcoinerlab.com/modules/descriptors/api/classes/_Internal_.Output.html).
|
|
182
|
-
|
|
183
|
-
#### Parsing Descriptors with `expand()`
|
|
184
|
-
|
|
185
|
-
Most applications do not need `expand()` for normal receive/spend flows. It is mainly useful for debugging and introspection (for example, checking expanded expressions, key mappings, scripts, and parsed metadata).
|
|
186
|
-
|
|
187
|
-
```javascript
|
|
188
|
-
const { expand } = descriptors.DescriptorsFactory(ecc);
|
|
189
|
-
const info = expand({ descriptor });
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
For full details on returned fields, refer to [the API](https://bitcoinerlab.com/modules/descriptors/api/types/Expansion.html).
|
|
193
|
-
|
|
194
|
-
#### Spending-path selection: from WSH Miniscript to Taproot
|
|
195
|
-
|
|
196
|
-
When a descriptor has more than one valid way to spend, the library needs to know which path you intend to use.
|
|
197
|
-
|
|
198
|
-
For `wsh(miniscript)` and `sh(wsh(miniscript))`, this is usually done with `signersPubKeys`: you pass the public keys expected to sign and the library selects the most optimal satisfiable branch.
|
|
199
|
-
|
|
200
|
-
`signersPubKeys` is passed as an array of public keys (`Uint8Array[]`).
|
|
201
|
-
If omitted, the library assumes all descriptor keys may sign.
|
|
202
|
-
|
|
203
|
-
Example with two BIP32-derived keys and one preferred signer:
|
|
204
|
-
|
|
205
|
-
```javascript
|
|
206
|
-
import { randomBytes } from 'crypto';
|
|
207
|
-
import * as ecc from '@bitcoinerlab/secp256k1';
|
|
208
|
-
import { DescriptorsFactory, keyExpressionBIP32 } from '@bitcoinerlab/descriptors';
|
|
209
|
-
|
|
210
|
-
const { Output, BIP32 } = DescriptorsFactory(ecc);
|
|
211
|
-
|
|
212
|
-
const masterNode = BIP32.fromSeed(randomBytes(64));
|
|
213
|
-
|
|
214
|
-
const originPath = "/84'/0'/0'";
|
|
215
|
-
|
|
216
|
-
const keyPathA = '/0/0';
|
|
217
|
-
const keyPathB = '/0/1';
|
|
218
|
-
|
|
219
|
-
const keyExprA = keyExpressionBIP32({ masterNode, originPath, keyPath: keyPathA });
|
|
220
|
-
const keyExprB = keyExpressionBIP32({ masterNode, originPath, keyPath: keyPathB });
|
|
221
|
-
|
|
222
|
-
const signerPubKeyA = masterNode.derivePath( `m${originPath}${keyPathA}`).publicKey;
|
|
223
|
-
|
|
224
|
-
// Two possible branches:
|
|
225
|
-
// - branch 1: signature by keyA + older(10)
|
|
226
|
-
// - branch 2: signature by keyB (no timelock)
|
|
227
|
-
const output = new Output({
|
|
228
|
-
descriptor: `wsh(andor(pk(${keyExprA}),older(10),pk(${keyExprB})))`,
|
|
229
|
-
signersPubKeys: [signerPubKeyA] // choose the keyA (timelock branch)
|
|
230
|
-
});
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
Taproot uses the same idea. For `tr(KEY,TREE)`, `signersPubKeys` helps determine which leaves are satisfiable and which satisfiable path is more optimal. In addition, Taproot provides two optional controls:
|
|
234
|
-
|
|
235
|
-
- `taprootSpendPath` (`'key' | 'script'`) to force key-path or script-path spending.
|
|
236
|
-
- `tapLeaf` to force a specific script leaf when using script path.
|
|
237
|
-
|
|
238
|
-
If `taprootSpendPath` is omitted for `tr(KEY,TREE)`, the library uses script path and auto-selects the most optimal satisfiable leaf from available spending data (including `signersPubKeys` and preimages when relevant).
|
|
239
|
-
|
|
240
|
-
If you specifically plan to spend from the internal key, set:
|
|
241
|
-
|
|
242
|
-
```javascript
|
|
243
|
-
new Output({
|
|
244
|
-
descriptor: 'tr(INTERNAL_KEY,{pk(KEY_A),pk(KEY_B)})',
|
|
245
|
-
taprootSpendPath: 'key'
|
|
246
|
-
});
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
If you want to force a specific script leaf:
|
|
250
|
-
|
|
251
|
-
```javascript
|
|
252
|
-
new Output({
|
|
253
|
-
descriptor: 'tr(INTERNAL_KEY,{pk(KEY_A),pk(KEY_B)})',
|
|
254
|
-
taprootSpendPath: 'script',
|
|
255
|
-
tapLeaf: 'pk(KEY_A)'
|
|
256
|
-
});
|
|
257
|
-
```
|
|
258
|
-
|
|
259
|
-
These spending-path parameters (`signersPubKeys`, `taprootSpendPath`, `tapLeaf`) are only needed when spending/finalizing UTXOs with multiple candidate paths. They are not needed just to derive addresses or `scriptPubKeys`.
|
|
260
|
-
|
|
261
|
-
For a focused walkthrough of constructor choices (including `signersPubKeys`) and practical usage of `updatePsbtAsInput`, `getAddress` and `getScriptPubKey`, see this [Stack Exchange answer](https://bitcoin.stackexchange.com/a/118036/89665).
|
|
262
|
-
|
|
263
|
-
### Signers and Finalizers
|
|
264
|
-
|
|
265
|
-
This library encompasses a PSBT finalizer as well as three distinct signers: ECPair for single-signatures, BIP32 and Ledger (specifically crafted for Ledger Wallet devices, with upcoming support for other devices planned).
|
|
11
|
+
It bundles the bitcoinjs family dependencies and exposes pre-bound helpers such
|
|
12
|
+
as `Output`, `expand`, `ECPair`, `BIP32`, `Psbt` and `ecc` at the top level.
|
|
266
13
|
|
|
267
|
-
|
|
14
|
+
Most users can import directly from the package:
|
|
268
15
|
|
|
269
16
|
```javascript
|
|
270
|
-
import { signers } from '@bitcoinerlab/descriptors';
|
|
17
|
+
import { Output, Psbt, ECPair, signers } from '@bitcoinerlab/descriptors';
|
|
271
18
|
```
|
|
272
19
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
```javascript
|
|
276
|
-
// For Ledger
|
|
277
|
-
await signers.signLedger({ psbt, ledgerManager });
|
|
278
|
-
|
|
279
|
-
// For BIP32 - https://github.com/bitcoinjs/bip32
|
|
280
|
-
signers.signBIP32({ psbt, masterNode });
|
|
281
|
-
|
|
282
|
-
// For ECPair - https://github.com/bitcoinjs/ecpair
|
|
283
|
-
signers.signECPair({ psbt, ecpair }); // Here, `ecpair` is an instance of the bitcoinjs-lib ECPairInterface
|
|
284
|
-
```
|
|
285
|
-
|
|
286
|
-
Detailed information on Ledger integration will be provided in subsequent sections.
|
|
287
|
-
|
|
288
|
-
<a name="signers-and-finalizers-finalize-psbt-input"></a>
|
|
289
|
-
|
|
290
|
-
#### Finalizing the `psbt`
|
|
291
|
-
|
|
292
|
-
When finalizing the `psbt`, the [`updatePsbtAsInput` method](https://bitcoinerlab.com/modules/descriptors/api/classes/_Internal_.Output.html#updatePsbtAsInput) plays a key role. When invoked, the `output.updatePsbtAsInput()` sets up the `psbt` by designating the output as an input and, if required, adjusts the transaction locktime. In addition, it returns a `inputFinalizer` function tailored for this specific `psbt` input.
|
|
293
|
-
|
|
294
|
-
##### Procedure
|
|
295
|
-
|
|
296
|
-
1. For each unspent output from a previous transaction that you're referencing in a `psbt` as an input to be spent, call the `updatePsbtAsInput` method:
|
|
297
|
-
|
|
298
|
-
```javascript
|
|
299
|
-
const inputFinalizer = output.updatePsbtAsInput({ psbt, txHex, vout });
|
|
300
|
-
```
|
|
301
|
-
|
|
302
|
-
2. Once you've completed the necessary signing operations on the `psbt`, use the returned finalizer function on each input:
|
|
303
|
-
|
|
304
|
-
```javascript
|
|
305
|
-
inputFinalizer({ psbt });
|
|
306
|
-
```
|
|
307
|
-
|
|
308
|
-
##### Important Notes
|
|
309
|
-
|
|
310
|
-
- The finalizer function returned from `updatePsbtAsInput` adds the necessary unlocking script (`scriptWitness` or `scriptSig`) that satisfies the `Output`'s spending conditions. Remember, both `scriptSig` and `scriptWitness` contain signatures. Ensure that all necessary signing operations are completed before finalizing.
|
|
311
|
-
|
|
312
|
-
- When using `updatePsbtAsInput`, the `txHex` parameter is crucial. For Segwit inputs, you can choose to pass `txId` and `value` instead of `txHex` (`value` is `bigint` in v3). However, ensure the accuracy of the `value` to avoid potential fee attacks. When unsure, use `txHex` and skip `txId` and `value`.
|
|
313
|
-
|
|
314
|
-
- Hardware wallets require the [full `txHex` for Segwit](https://blog.trezor.io/details-of-firmware-updates-for-trezor-one-version-1-9-1-and-trezor-model-t-version-2-3-1-1eba8f60f2dd).
|
|
315
|
-
|
|
316
|
-
### Key Expressions and Script Expressions
|
|
317
|
-
|
|
318
|
-
This library also provides a series of function helpers designed to streamline the generation of `descriptor` strings. These strings can serve as input parameters in the `Output` class constructor. These helpers are nested within the `scriptExpressions` module. You can import them as illustrated below:
|
|
319
|
-
|
|
320
|
-
```javascript
|
|
321
|
-
import { scriptExpressions } from '@bitcoinerlab/descriptors';
|
|
322
|
-
```
|
|
323
|
-
|
|
324
|
-
Within the `scriptExpressions` module, there are functions designed to generate descriptors for commonly used scripts. Some examples include `pkhBIP32()`, `shWpkhBIP32()`, `wpkhBIP32()`, `pkhLedger()`, `shWpkhLedger()` and `wpkhLedger()`. Refer to [the API](https://bitcoinerlab.com/modules/descriptors/api/modules/scriptExpressions.html#expand) for a detailed list and further information.
|
|
325
|
-
|
|
326
|
-
When using BIP32-based descriptors, the following parameters are required for the `scriptExpressions` functions:
|
|
327
|
-
|
|
328
|
-
```javascript
|
|
329
|
-
pkhBIP32(params: {
|
|
330
|
-
masterNode: BIP32Interface; //bitcoinjs-lib BIP32 - https://github.com/bitcoinjs/bip32
|
|
331
|
-
network?: Network; //A bitcoinjs-lib network
|
|
332
|
-
account: number;
|
|
333
|
-
change?: number | undefined; //0 -> external (receive), 1 -> internal (change)
|
|
334
|
-
index?: number | undefined | '*';
|
|
335
|
-
keyPath?: string; //You can use change & index or a keyPath such as "/0/0"
|
|
336
|
-
isPublic?: boolean; //Whether to use xpub or xprv
|
|
337
|
-
})
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
For functions suffixed with _Ledger_ (designed to generate descriptors for Ledger Hardware devices), replace `masterNode` with `ledgerManager`. Detailed information on Ledger integration will be provided in the following section.
|
|
341
|
-
|
|
342
|
-
The `keyExpressions` category includes functions that generate string representations of key expressions for public keys.
|
|
343
|
-
|
|
344
|
-
This library includes the following `keyExpressions`: [`keyExpressionBIP32`](https://bitcoinerlab.com/modules/descriptors/api/functions/keyExpressionBIP32.html) and [`keyExpressionLedger`](https://bitcoinerlab.com/modules/descriptors/api/functions/keyExpressionLedger.html). They can be imported as follows:
|
|
345
|
-
|
|
346
|
-
```javascript
|
|
347
|
-
import { keyExpressionBIP32, keyExpressionLedger } from '@bitcoinerlab/descriptors';
|
|
348
|
-
```
|
|
349
|
-
|
|
350
|
-
The parameters required for these functions are:
|
|
351
|
-
|
|
352
|
-
```javascript
|
|
353
|
-
function keyExpressionBIP32({
|
|
354
|
-
masterNode: BIP32Interface; //bitcoinjs-lib BIP32 - https://github.com/bitcoinjs/bip32
|
|
355
|
-
originPath: string;
|
|
356
|
-
change?: number | undefined; //0 -> external (receive), 1 -> internal (change)
|
|
357
|
-
index?: number | undefined | '*';
|
|
358
|
-
keyPath?: string | undefined; //In the case of the Ledger, keyPath can also use multipath (e.g. /<0;1>/number)
|
|
359
|
-
isPublic?: boolean;
|
|
360
|
-
});
|
|
361
|
-
```
|
|
362
|
-
|
|
363
|
-
For the `keyExpressionLedger` function, you'd use `ledgerManager` instead of `masterNode`.
|
|
364
|
-
|
|
365
|
-
Both functions will generate strings that fully define BIP32 keys. For example:
|
|
366
|
-
|
|
367
|
-
```text
|
|
368
|
-
[d34db33f/44'/0'/0']xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/1/*
|
|
369
|
-
```
|
|
370
|
-
|
|
371
|
-
Read [Bitcoin Core descriptors documentation](https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md) to learn more about Key Expressions.
|
|
372
|
-
|
|
373
|
-
### Hardware Wallet Integration
|
|
374
|
-
|
|
375
|
-
This library currently provides integration with Ledger wallets. Support for more devices is planned.
|
|
376
|
-
|
|
377
|
-
Before we dive in, note that, in addition to the documentation below, it is highly recommended to visit the [Ledger Playground](https://bitcoinerlab.com/guides/ledger-programming) with an interactive code sandbox of this lib interacting with a Ledger device.
|
|
378
|
-
|
|
379
|
-
To use this library with Ledger devices, you must first install Ledger support:
|
|
380
|
-
|
|
381
|
-
```bash
|
|
382
|
-
npm install @ledgerhq/ledger-bitcoin @ledgerhq/hw-transport-node-hid
|
|
383
|
-
```
|
|
384
|
-
|
|
385
|
-
For Ledger device signing, import the necessary functions as follows:
|
|
386
|
-
|
|
387
|
-
```javascript
|
|
388
|
-
import Transport from '@ledgerhq/hw-transport-node-hid'; //or hw-transport-web-hid, for web
|
|
389
|
-
import { AppClient } from '@ledgerhq/ledger-bitcoin';
|
|
390
|
-
import { ledger } from '@bitcoinerlab/descriptors';
|
|
391
|
-
```
|
|
392
|
-
|
|
393
|
-
Then, use the following code to assert that the Ledger app is running Bitcoin Test version 2.1.0 or higher and to create a new Ledger client:
|
|
394
|
-
|
|
395
|
-
```javascript
|
|
396
|
-
const transport = await Transport.create();
|
|
397
|
-
//Throws if not running Bitcoin Test >= 2.1.0
|
|
398
|
-
await ledger.assertLedgerApp({ transport, name: 'Bitcoin Test', minVersion: '2.1.0' });
|
|
399
|
-
|
|
400
|
-
const ledgerClient = new AppClient(transport);
|
|
401
|
-
const ledgerManager = { ledgerClient, ledgerState: {}, ecc, network };
|
|
402
|
-
```
|
|
403
|
-
|
|
404
|
-
Here, `transport` is an instance of a Transport object that allows communication with Ledger devices. You can use any of the transports [provided by Ledger](https://github.com/LedgerHQ/ledger-live#libs---libraries).
|
|
405
|
-
|
|
406
|
-
To register the policies of non-standard descriptors on the Ledger device, use the following code:
|
|
407
|
-
|
|
408
|
-
```javascript
|
|
409
|
-
await ledger.registerLedgerWallet({
|
|
410
|
-
ledgerManager,
|
|
411
|
-
descriptor: wshDescriptor,
|
|
412
|
-
policyName: 'BitcoinerLab'
|
|
413
|
-
});
|
|
414
|
-
```
|
|
415
|
-
|
|
416
|
-
This code will auto-skip the policy registration process if it already exists. Please refer to [Ledger documentation](https://github.com/LedgerHQ/app-bitcoin-new/blob/develop/doc/wallet.md) to learn more about their Wallet Policies registration procedures.
|
|
417
|
-
|
|
418
|
-
Finally, `ledgerManager.ledgerState` is an object used to store information related to Ledger devices. Although Ledger devices themselves are stateless, this object can be used to store information such as xpubs, master fingerprints and wallet policies. You can pass an initially empty object that will be updated with more information as it is used. The object can be serialized and stored for future use.
|
|
419
|
-
|
|
420
|
-
The [API reference for the ledger module](https://bitcoinerlab.com/modules/descriptors/api/variables/ledger.html) provides a comprehensive list of functions related to the Ledger Hardware Wallet, along with detailed explanations of their parameters and behavior.
|
|
421
|
-
|
|
422
|
-
<a name="documentation"></a>
|
|
423
|
-
|
|
424
|
-
## Additional Resources
|
|
425
|
-
|
|
426
|
-
For more information, refer to the following resources:
|
|
427
|
-
|
|
428
|
-
- **[Guides](https://bitcoinerlab.com/guides)**: Comprehensive explanations and playgrounds to help you learn how to use the module.
|
|
429
|
-
- **[API](https://bitcoinerlab.com/modules/descriptors/api)**: Dive into the details of the Classes, functions and types.
|
|
430
|
-
- **[Stack Exchange answer](https://bitcoin.stackexchange.com/a/118036/89665)**: Focused explanation on the constructor, specifically the `signersPubKeys` parameter and the usage of `updatePsbtAsInput`, `getAddress` and `getScriptPubKey`.
|
|
431
|
-
- **[Integration tests](https://github.com/bitcoinerlab/descriptors/tree/main/test/integration)**: Well-commented code examples showcasing the usage of all functions in the module.
|
|
432
|
-
- **Local Documentation**: Generate comprehensive API documentation from the source code:
|
|
433
|
-
|
|
434
|
-
```bash
|
|
435
|
-
git clone https://github.com/bitcoinerlab/descriptors
|
|
436
|
-
cd descriptors/
|
|
437
|
-
npm install
|
|
438
|
-
npm run docs
|
|
439
|
-
```
|
|
440
|
-
|
|
441
|
-
The generated documentation will be available in the `docs/` directory. Open the `index.html` file to view the documentation.
|
|
442
|
-
|
|
443
|
-
## Authors and Contributors
|
|
444
|
-
|
|
445
|
-
The project was initially developed and is currently maintained by [Jose-Luis Landabaso](https://github.com/landabaso). Contributions and help from other developers are welcome.
|
|
446
|
-
|
|
447
|
-
Here are some resources to help you get started with contributing:
|
|
448
|
-
|
|
449
|
-
### Building from source
|
|
450
|
-
|
|
451
|
-
To download the source code and build the project, follow these steps:
|
|
452
|
-
|
|
453
|
-
1. Clone the repository:
|
|
454
|
-
|
|
455
|
-
```bash
|
|
456
|
-
git clone https://github.com/bitcoinerlab/descriptors.git
|
|
457
|
-
```
|
|
458
|
-
|
|
459
|
-
2. Install the dependencies:
|
|
460
|
-
|
|
461
|
-
```bash
|
|
462
|
-
npm install
|
|
463
|
-
```
|
|
464
|
-
|
|
465
|
-
3. Build the project:
|
|
466
|
-
|
|
467
|
-
```bash
|
|
468
|
-
npm run build
|
|
469
|
-
```
|
|
470
|
-
|
|
471
|
-
This will build the project and generate the necessary files in the `dist` directory.
|
|
472
|
-
|
|
473
|
-
### Testing
|
|
474
|
-
|
|
475
|
-
Before committing any code, make sure it passes all tests.
|
|
476
|
-
|
|
477
|
-
Run unit tests:
|
|
478
|
-
|
|
479
|
-
```bash
|
|
480
|
-
npm run test:unit
|
|
481
|
-
```
|
|
482
|
-
|
|
483
|
-
Run the full test pipeline (lint + build + unit + integration):
|
|
484
|
-
|
|
485
|
-
```bash
|
|
486
|
-
npm run test
|
|
487
|
-
```
|
|
488
|
-
|
|
489
|
-
Integration tests require Docker. Make sure the `docker` command is installed and available in your PATH. When integration tests run, they automatically start or reuse a local container with the regtest services needed by this repository.
|
|
490
|
-
|
|
491
|
-
And, in case you have a Ledger device:
|
|
492
|
-
|
|
493
|
-
```bash
|
|
494
|
-
npm run test:integration:ledger
|
|
495
|
-
```
|
|
496
|
-
|
|
497
|
-
### License
|
|
498
|
-
|
|
499
|
-
This project is licensed under the MIT License.
|
|
20
|
+
`DescriptorsFactory(ecc)` is still available for backwards compatibility and
|
|
21
|
+
advanced initialization.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from '@bitcoinerlab/descriptors-core';
|
|
2
|
+
export { createBitcoinjsLib } from '@bitcoinerlab/descriptors-core/bitcoinjs';
|
|
3
|
+
|
|
4
|
+
export declare const ecc: typeof import('@bitcoinerlab/secp256k1');
|
|
5
|
+
export declare const Psbt: typeof import('bitcoinjs-lib').Psbt;
|
|
6
|
+
|
|
7
|
+
type Bound = ReturnType<typeof import('@bitcoinerlab/descriptors-core').DescriptorsFactory>;
|
|
8
|
+
|
|
9
|
+
export declare const Output: Bound['Output'];
|
|
10
|
+
export declare const parseKeyExpression: Bound['parseKeyExpression'];
|
|
11
|
+
export declare const expand: Bound['expand'];
|
|
12
|
+
export declare const ECPair: Bound['ECPair'];
|
|
13
|
+
export declare const BIP32: Bound['BIP32'];
|
package/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const core = require('@bitcoinerlab/descriptors-core');
|
|
4
|
+
const { createBitcoinjsLib } = require('@bitcoinerlab/descriptors-core/bitcoinjs');
|
|
5
|
+
const ecc = require('@bitcoinerlab/secp256k1');
|
|
6
|
+
const { Psbt } = require('bitcoinjs-lib');
|
|
7
|
+
|
|
8
|
+
const bound = core.DescriptorsFactory(ecc);
|
|
9
|
+
|
|
10
|
+
exports.networks = core.networks;
|
|
11
|
+
exports.DescriptorsFactory = core.DescriptorsFactory;
|
|
12
|
+
exports.checksum = core.checksum;
|
|
13
|
+
exports.signers = core.signers;
|
|
14
|
+
exports.keyExpressionBIP32 = core.keyExpressionBIP32;
|
|
15
|
+
exports.keyExpressionLedger = core.keyExpressionLedger;
|
|
16
|
+
exports.scriptExpressions = core.scriptExpressions;
|
|
17
|
+
exports.ledger = core.ledger;
|
|
18
|
+
|
|
19
|
+
exports.Output = bound.Output;
|
|
20
|
+
exports.parseKeyExpression = bound.parseKeyExpression;
|
|
21
|
+
exports.expand = bound.expand;
|
|
22
|
+
exports.ECPair = bound.ECPair;
|
|
23
|
+
exports.BIP32 = bound.BIP32;
|
|
24
|
+
|
|
25
|
+
exports.ecc = ecc;
|
|
26
|
+
exports.Psbt = Psbt;
|
|
27
|
+
exports.createBitcoinjsLib = createBitcoinjsLib;
|
package/package.json
CHANGED
|
@@ -1,50 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bitcoinerlab/descriptors",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"version": "3.0.6",
|
|
3
|
+
"version": "3.1.1",
|
|
4
|
+
"description": "This library parses and creates Bitcoin Miniscript Descriptors and generates Partially Signed Bitcoin Transactions (PSBTs). It provides PSBT finalizers and signers for single-signature, BIP32 and Hardware Wallets, bundled for the bitcoinjs family of libraries.",
|
|
6
5
|
"author": "Jose-Luis Landabaso",
|
|
7
6
|
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/bitcoinerlab/descriptors",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "git+https://github.com/bitcoinerlab/descriptors.git"
|
|
11
11
|
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/bitcoinerlab/descriptors/issues"
|
|
14
|
+
},
|
|
12
15
|
"keywords": [
|
|
13
16
|
"bitcoin",
|
|
14
17
|
"descriptors",
|
|
18
|
+
"miniscript",
|
|
19
|
+
"psbt",
|
|
15
20
|
"bitcoinjs",
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"url": "https://github.com/bitcoinerlab/descriptors/issues"
|
|
20
|
-
},
|
|
21
|
-
"main": "dist/index.js",
|
|
22
|
-
"types": "dist/index.d.ts",
|
|
23
|
-
"prettier": "@bitcoinerlab/configs/prettierConfig.json",
|
|
24
|
-
"eslintConfig": {
|
|
25
|
-
"extends": "./node_modules/@bitcoinerlab/configs/eslintConfig"
|
|
26
|
-
},
|
|
27
|
-
"jest": {
|
|
28
|
-
"preset": "@bitcoinerlab/configs"
|
|
29
|
-
},
|
|
30
|
-
"scripts": {
|
|
31
|
-
"webdocs": "typedoc --options ./node_modules/@bitcoinerlab/configs/webtypedoc.json",
|
|
32
|
-
"docs": "typedoc --options ./node_modules/@bitcoinerlab/configs/typedoc.json",
|
|
33
|
-
"build:src": "tsc --project ./node_modules/@bitcoinerlab/configs/tsconfig.src.json",
|
|
34
|
-
"build:fixtures": "node test/tools/generateBitcoinCoreFixtures.js -i test/fixtures/descriptor_tests.cpp | npx prettier --parser typescript > test/fixtures/bitcoinCore.ts",
|
|
35
|
-
"build:test": "npm run build:fixtures && tsc --project ./node_modules/@bitcoinerlab/configs/tsconfig.test.json --resolveJsonModule",
|
|
36
|
-
"build": "npm run build:src && npm run build:test",
|
|
37
|
-
"lint": "./node_modules/@bitcoinerlab/configs/scripts/lint.sh",
|
|
38
|
-
"ensureTester": "./node_modules/@bitcoinerlab/configs/scripts/ensureTester.sh",
|
|
39
|
-
"test:integration:soft": "npm run ensureTester && node test/integration/standardOutputs.js && echo \"\n\n\" && node test/integration/miniscript.js && echo \"\n\n\" && node test/integration/sortedmulti.js && echo \"\n\n\" && node test/integration/taproot.js",
|
|
40
|
-
"test:integration:ledger": "npm run ensureTester && node test/integration/ledgerTaproot.js && echo \"\n\n\" && node test/integration/ledger.js",
|
|
41
|
-
"test:unit": "jest",
|
|
42
|
-
"test": "npm run lint && npm run build && npm run test:unit && npm run test:integration:soft",
|
|
43
|
-
"testledger": "npm run lint && npm run build && npm run test:integration:ledger",
|
|
44
|
-
"prepublishOnly": "npm run test && echo \"\n\n\" && npm run test:integration:ledger"
|
|
45
|
-
},
|
|
46
|
-
"files": [
|
|
47
|
-
"dist"
|
|
21
|
+
"bitcoinjs-lib",
|
|
22
|
+
"bip32",
|
|
23
|
+
"ecpair"
|
|
48
24
|
],
|
|
49
25
|
"peerDependencies": {
|
|
50
26
|
"@ledgerhq/ledger-bitcoin": "^0.3.0"
|
|
@@ -54,27 +30,24 @@
|
|
|
54
30
|
"optional": true
|
|
55
31
|
}
|
|
56
32
|
},
|
|
57
|
-
"
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
"
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
"bip65": "^1.0.3",
|
|
65
|
-
"bip68": "^1.0.4",
|
|
66
|
-
"regtest-client": "^0.2.1",
|
|
67
|
-
"yargs": "^17.7.2"
|
|
33
|
+
"main": "./index.js",
|
|
34
|
+
"types": "./index.d.ts",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"types": "./index.d.ts",
|
|
38
|
+
"default": "./index.js"
|
|
39
|
+
}
|
|
68
40
|
},
|
|
41
|
+
"files": [
|
|
42
|
+
"index.js",
|
|
43
|
+
"index.d.ts",
|
|
44
|
+
"README.md"
|
|
45
|
+
],
|
|
69
46
|
"dependencies": {
|
|
70
|
-
"@bitcoinerlab/
|
|
47
|
+
"@bitcoinerlab/descriptors-core": "^3.1.0",
|
|
71
48
|
"@bitcoinerlab/secp256k1": "^1.2.0",
|
|
72
|
-
"bip174": "^3.0.0",
|
|
73
49
|
"bip32": "^5.0.1",
|
|
74
50
|
"bitcoinjs-lib": "^7.0.1",
|
|
75
|
-
"ecpair": "^3.0.1"
|
|
76
|
-
"lodash.memoize": "^4.1.2",
|
|
77
|
-
"uint8array-tools": "^0.0.9",
|
|
78
|
-
"varuint-bitcoin": "^2.0.0"
|
|
51
|
+
"ecpair": "^3.0.1"
|
|
79
52
|
}
|
|
80
53
|
}
|
package/dist/applyPR2137.d.ts
DELETED