@bitcoinerlab/descriptors 2.0.2 → 2.0.3

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 CHANGED
@@ -73,11 +73,11 @@ const wpkhOutput = new Output({
73
73
  });
74
74
  ```
75
75
 
76
- Refer to [the API](https://bitcoinerlab.com/modules/descriptors/api/classes/_Internal_.Output.html#constructor) for the complete list of parameters in the constructor.
76
+ For miniscript-based descriptors, the `signersPubKeys` parameter in the constuctor becomes particularly important. It specifies the spending path of a previous output with multiple spending paths. Detailed information about the constructor parameters, including `signersPubKeys`, 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).
77
77
 
78
78
  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()`.
79
79
 
80
- 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.
80
+ 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.
81
81
 
82
82
  To call `updatePsbtAsInput()`, use the following syntax:
83
83
 
@@ -66,23 +66,6 @@ export declare function DescriptorsFactory(ecc: TinySecp256k1Interface): {
66
66
  * satisfyMiniscript verifies that the time constraints did not change.
67
67
  */
68
68
  "__#1@#getTimeConstraints"(): TimeConstraints | undefined;
69
- /**
70
- * Retrieves the byte length of the script satisfaction for a Miniscript-based
71
- * descriptor, using only the expression, signers' public keys, and preimages
72
- * provided in the constructor.
73
- *
74
- * Useful in scenarios like coin selection algorithms for transaction creation,
75
- * where signatures are not yet available. Since signatures are still to be
76
- * computed, the function assigns a standard length of 72 bytes for each
77
- * signature. However, note that this may not always be completely accurate,
78
- * as approximately 50% of signatures are 71 bytes in length
79
- * (source: https://transactionfee.info/charts/bitcoin-script-ecdsa-length/).
80
- * The function returns the byte length for a worst-case scenario.
81
- *
82
- * @returns The byte length of the compiled script satisfaction, or `undefined`
83
- * if this was not a miniscript-based descriptor.
84
- */
85
- getScriptSatisfactionSize(): number | undefined;
86
69
  /**
87
70
  * Creates and returns an instance of bitcoinjs-lib
88
71
  * [`Payment`](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/ts_src/payments/index.ts)'s interface with the `scriptPubKey` of this `Output`.
@@ -107,7 +90,7 @@ export declare function DescriptorsFactory(ecc: TinySecp256k1Interface): {
107
90
  * Important: As mentioned above, note that this function only applies to
108
91
  * miniscript descriptors.
109
92
  */
110
- getScriptSatisfaction(signatures: PartialSig[]): Buffer;
93
+ getScriptSatisfaction(signatures: PartialSig[] | 'DANGEROUSLY_USE_FAKE_SIGNATURES'): Buffer;
111
94
  /**
112
95
  * Gets the nSequence required to fulfill this `Output`.
113
96
  */
@@ -274,22 +257,34 @@ export declare function DescriptorsFactory(ecc: TinySecp256k1Interface): {
274
257
  */
275
258
  network?: Network;
276
259
  /**
277
- * An array of preimages. This info is necessary to finalize Psbts.
260
+ * An array of preimages if the miniscript-based descriptor uses them.
261
+ *
262
+ * This info is necessary to finalize Psbts. Leave it `undefined` if your
263
+ * miniscript-based descriptor does not use preimages or you don't know
264
+ * or don't wanto use them.
265
+ *
266
+ * You can also leave it `undefined` if only need to generate the
267
+ * `scriptPubKey` or `address` for a descriptor.
268
+ *
278
269
  * @defaultValue `[]`
279
270
  */
280
271
  preimages?: Preimage[];
281
272
  /**
282
273
  * An array of the public keys used for signing the transaction when
283
- * spending the output associated with this descriptor. This parameter is
284
- * only used if the descriptor object is being used to finalize a
285
- * transaction. It is necessary to specify the spending path when working
286
- * with miniscript-based expressions that have multiple spending paths.
287
- * Set this parameter to an array containing the public keys involved in
288
- * the desired spending path. Leave it `undefined` if you only need to
289
- * generate the `scriptPubKey` or `address` for a descriptor, or if all
290
- * the public keys involved in the descriptor will sign the transaction.
291
- * In the latter case, the satisfier will automatically choose the most
292
- * optimal spending path (if more than one is available).
274
+ * spending the previous output associated with this descriptor.
275
+ *
276
+ * This parameter is only used if the descriptor object is being used to
277
+ * finalize a transaction. It is necessary to specify the spending path
278
+ * when working with miniscript-based expressions that have multiple
279
+ * spending paths.
280
+ *
281
+ * Set this parameter to an array containing the public
282
+ * keys involved in the desired spending path. Leave it `undefined` if you
283
+ * only need to generate the `scriptPubKey` or `address` for a descriptor,
284
+ * or if all the public keys involved in the descriptor will sign the
285
+ * transaction. In the latter case, the satisfier will automatically
286
+ * choose the most optimal spending path (if more than one is available).
287
+ *
293
288
  * For more details on using this parameter, refer to [this Stack Exchange
294
289
  * answer](https://bitcoin.stackexchange.com/a/118036/89665).
295
290
  */
@@ -321,23 +316,6 @@ export declare function DescriptorsFactory(ecc: TinySecp256k1Interface): {
321
316
  * satisfyMiniscript verifies that the time constraints did not change.
322
317
  */
323
318
  "__#1@#getTimeConstraints"(): TimeConstraints | undefined;
324
- /**
325
- * Retrieves the byte length of the script satisfaction for a Miniscript-based
326
- * descriptor, using only the expression, signers' public keys, and preimages
327
- * provided in the constructor.
328
- *
329
- * Useful in scenarios like coin selection algorithms for transaction creation,
330
- * where signatures are not yet available. Since signatures are still to be
331
- * computed, the function assigns a standard length of 72 bytes for each
332
- * signature. However, note that this may not always be completely accurate,
333
- * as approximately 50% of signatures are 71 bytes in length
334
- * (source: https://transactionfee.info/charts/bitcoin-script-ecdsa-length/).
335
- * The function returns the byte length for a worst-case scenario.
336
- *
337
- * @returns The byte length of the compiled script satisfaction, or `undefined`
338
- * if this was not a miniscript-based descriptor.
339
- */
340
- getScriptSatisfactionSize(): number | undefined;
341
319
  /**
342
320
  * Creates and returns an instance of bitcoinjs-lib
343
321
  * [`Payment`](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/ts_src/payments/index.ts)'s interface with the `scriptPubKey` of this `Output`.
@@ -362,7 +340,7 @@ export declare function DescriptorsFactory(ecc: TinySecp256k1Interface): {
362
340
  * Important: As mentioned above, note that this function only applies to
363
341
  * miniscript descriptors.
364
342
  */
365
- getScriptSatisfaction(signatures: PartialSig[]): Buffer;
343
+ getScriptSatisfaction(signatures: PartialSig[] | 'DANGEROUSLY_USE_FAKE_SIGNATURES'): Buffer;
366
344
  /**
367
345
  * Gets the nSequence required to fulfill this `Output`.
368
346
  */
@@ -481,49 +481,6 @@ function DescriptorsFactory(ecc) {
481
481
  }
482
482
  }
483
483
  }
484
- /**
485
- * Retrieves the byte length of the script satisfaction for a Miniscript-based
486
- * descriptor, using only the expression, signers' public keys, and preimages
487
- * provided in the constructor.
488
- *
489
- * Useful in scenarios like coin selection algorithms for transaction creation,
490
- * where signatures are not yet available. Since signatures are still to be
491
- * computed, the function assigns a standard length of 72 bytes for each
492
- * signature. However, note that this may not always be completely accurate,
493
- * as approximately 50% of signatures are 71 bytes in length
494
- * (source: https://transactionfee.info/charts/bitcoin-script-ecdsa-length/).
495
- * The function returns the byte length for a worst-case scenario.
496
- *
497
- * @returns The byte length of the compiled script satisfaction, or `undefined`
498
- * if this was not a miniscript-based descriptor.
499
- */
500
- getScriptSatisfactionSize() {
501
- const miniscript = __classPrivateFieldGet(this, _Output_miniscript, "f");
502
- const preimages = __classPrivateFieldGet(this, _Output_preimages, "f");
503
- const expandedMiniscript = __classPrivateFieldGet(this, _Output_expandedMiniscript, "f");
504
- const expansionMap = __classPrivateFieldGet(this, _Output_expansionMap, "f");
505
- const signersPubKeys = __classPrivateFieldGet(this, _Output_signersPubKeys, "f");
506
- //Create a method. solvePreimages to solve them.
507
- if (miniscript) {
508
- if (expandedMiniscript === undefined || expansionMap === undefined)
509
- throw new Error(`Error: cannot get script satisfactions from not expanded miniscript ${miniscript}`);
510
- //We create some fakeSignatures since we may not have them yet.
511
- const fakeSignatures = signersPubKeys.map(pubkey => ({
512
- pubkey,
513
- // https://transactionfee.info/charts/bitcoin-script-ecdsa-length/
514
- signature: Buffer.alloc(72, 0)
515
- }));
516
- const { scriptSatisfaction } = (0, miniscript_1.satisfyMiniscript)({
517
- expandedMiniscript,
518
- expansionMap,
519
- signatures: fakeSignatures,
520
- preimages
521
- });
522
- return scriptSatisfaction.length;
523
- }
524
- else
525
- return undefined;
526
- }
527
484
  /**
528
485
  * Creates and returns an instance of bitcoinjs-lib
529
486
  * [`Payment`](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/ts_src/payments/index.ts)'s interface with the `scriptPubKey` of this `Output`.
@@ -565,8 +522,28 @@ function DescriptorsFactory(ecc) {
565
522
  *
566
523
  * `signatures` must be passed using this format (pairs of `pubKey/signature`):
567
524
  * `interface PartialSig { pubkey: Buffer; signature: Buffer; }`
525
+ *
526
+ * * Alternatively, if you do not have the signatures, you can use the option
527
+ * `'DANGEROUSLY_USE_FAKE_SIGNATURES'`. This will generate script satisfactions
528
+ * using 72-byte zero-padded signatures. While this can be useful in
529
+ * modules like coinselector that require estimating transaction size before
530
+ * signing, it is critical to understand the risks:
531
+ * - Using this option generales invalid unlocking scripts.
532
+ * - It should NEVER be used with real transactions.
533
+ * - Its primary use is for testing and size estimation purposes only.
534
+ *
535
+ * ⚠️ Warning: Misuse of 'DANGEROUSLY_USE_FAKE_SIGNATURES' can lead to security
536
+ * vulnerabilities, including but not limited to invalid transaction generation.
537
+ * Ensure you fully understand the implications before use.
538
+ *
568
539
  */
569
540
  signatures) {
541
+ if (signatures === 'DANGEROUSLY_USE_FAKE_SIGNATURES')
542
+ signatures = __classPrivateFieldGet(this, _Output_signersPubKeys, "f").map(pubkey => ({
543
+ pubkey,
544
+ // https://transactionfee.info/charts/bitcoin-script-ecdsa-length/
545
+ signature: Buffer.alloc(72, 0)
546
+ }));
570
547
  const miniscript = __classPrivateFieldGet(this, _Output_miniscript, "f");
571
548
  const expandedMiniscript = __classPrivateFieldGet(this, _Output_expandedMiniscript, "f");
572
549
  const expansionMap = __classPrivateFieldGet(this, _Output_expansionMap, "f");
package/dist/types.d.ts CHANGED
@@ -9,13 +9,15 @@ import type { Payment, Network } from 'bitcoinjs-lib';
9
9
  */
10
10
  export type Preimage = {
11
11
  /**
12
- * Use same expressions as in miniscript. For example: "sha256(cdabb7f2dce7bfbd8a0b9570c6fd1e712e5d64045e9d6b517b3d5072251dc204)" or "ripemd160(095ff41131e5946f3c85f79e44adbcf8e27e080e)"
12
+ * Use same string expressions as in miniscript. For example: "sha256(cdabb7f2dce7bfbd8a0b9570c6fd1e712e5d64045e9d6b517b3d5072251dc204)" or "ripemd160(095ff41131e5946f3c85f79e44adbcf8e27e080e)"
13
+ *
13
14
  * Accepted functions: sha256, hash256, ripemd160, hash160
15
+ *
14
16
  * Digests must be: 64-character HEX for sha256, hash160 or 30-character HEX for ripemd160 or hash160.
15
17
  */
16
18
  digest: string;
17
19
  /**
18
- * Hex encoded preimate. Preimages are always 32 bytes (so, 64 character in hex).
20
+ * Hex encoded preimage. Preimages are always 32 bytes (so, 64 character in hex).
19
21
  */
20
22
  preimage: string;
21
23
  };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@bitcoinerlab/descriptors",
3
3
  "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.",
4
4
  "homepage": "https://github.com/bitcoinerlab/descriptors",
5
- "version": "2.0.2",
5
+ "version": "2.0.3",
6
6
  "author": "Jose-Luis Landabaso",
7
7
  "license": "MIT",
8
8
  "repository": {