@bitgo/wasm-utxo 1.27.0 → 1.28.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.
@@ -138,3 +138,10 @@ export declare class BIP32 implements BIP32Interface {
138
138
  */
139
139
  get wasm(): WasmBIP32;
140
140
  }
141
+ /**
142
+ * Type guard to check if a value is a BIP32Arg
143
+ *
144
+ * @param key - The value to check
145
+ * @returns true if the value is a BIP32Arg (string, BIP32, WasmBIP32, or BIP32Interface)
146
+ */
147
+ export declare function isBIP32Arg(key: unknown): key is BIP32Arg;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BIP32 = void 0;
4
+ exports.isBIP32Arg = isBIP32Arg;
4
5
  const wasm_utxo_js_1 = require("./wasm/wasm_utxo.js");
5
6
  /**
6
7
  * BIP32 wrapper class for extended key operations
@@ -175,3 +176,18 @@ class BIP32 {
175
176
  }
176
177
  }
177
178
  exports.BIP32 = BIP32;
179
+ /**
180
+ * Type guard to check if a value is a BIP32Arg
181
+ *
182
+ * @param key - The value to check
183
+ * @returns true if the value is a BIP32Arg (string, BIP32, WasmBIP32, or BIP32Interface)
184
+ */
185
+ function isBIP32Arg(key) {
186
+ return (typeof key === "string" ||
187
+ key instanceof BIP32 ||
188
+ key instanceof wasm_utxo_js_1.WasmBIP32 ||
189
+ (typeof key === "object" &&
190
+ key !== null &&
191
+ "derive" in key &&
192
+ typeof key.derive === "function"));
193
+ }
@@ -371,16 +371,63 @@ export declare class BitGoPsbt {
371
371
  */
372
372
  verifySignature(inputIndex: number, key: BIP32Arg | ECPairArg): boolean;
373
373
  /**
374
- * Sign a single input with a private key
374
+ * Sign all matching inputs with a private key.
375
+ *
376
+ * This method signs all inputs that match the provided key in a single efficient pass.
377
+ * It accepts either:
378
+ * - An xpriv (BIP32Arg: base58 string, BIP32 instance, or WasmBIP32) for wallet inputs
379
+ * - A raw privkey (ECPairArg: Buffer, ECPair instance, or WasmECPair) for replay protection inputs
380
+ *
381
+ * **Note:** MuSig2 inputs are skipped by this method when using xpriv because they require
382
+ * FirstRound state. After calling this method, sign MuSig2 inputs individually using
383
+ * `signInput()` after calling `generateMusig2Nonces()`.
384
+ *
385
+ * @param key - Either an xpriv (BIP32Arg) or a raw privkey (ECPairArg)
386
+ * @returns Array of input indices that were signed
387
+ * @throws Error if signing fails
388
+ *
389
+ * @example
390
+ * ```typescript
391
+ * // Sign all wallet inputs with user's xpriv
392
+ * const signedIndices = psbt.sign(userXpriv);
393
+ * console.log(`Signed inputs: ${signedIndices.join(", ")}`);
394
+ *
395
+ * // Sign all replay protection inputs with raw privkey
396
+ * const rpSignedIndices = psbt.sign(replayProtectionPrivkey);
397
+ * ```
398
+ */
399
+ sign(key: BIP32Arg | ECPairArg): number[];
400
+ /**
401
+ * Sign a single input with a private key.
402
+ *
403
+ * @deprecated Use `sign(key)` to sign all matching inputs (more efficient), or use
404
+ * `signInput(inputIndex, key)` for explicit single-input signing.
405
+ *
406
+ * **Note:** This method is NOT more efficient than `sign(key)` for non-MuSig2 inputs.
407
+ * The underlying miniscript library signs all inputs regardless. This overload exists
408
+ * for backward compatibility only.
409
+ *
410
+ * @param inputIndex - The index of the input to sign (0-based)
411
+ * @param key - Either an xpriv (BIP32Arg) or a raw privkey (ECPairArg)
412
+ * @throws Error if signing fails, or if generateMusig2Nonces() was not called first for MuSig2 inputs
413
+ */
414
+ sign(inputIndex: number, key: BIP32Arg | ECPairArg): void;
415
+ /**
416
+ * Sign a single input with a private key.
375
417
  *
376
418
  * This method signs a specific input using the provided key. It accepts either:
377
- * - An xpriv (BIP32Arg: base58 string, BIP32 instance, or WasmBIP32) for wallet inputs - derives the key and signs
378
- * - A raw privkey (ECPairArg: Buffer, ECPair instance, or WasmECPair) for replay protection inputs - signs directly
419
+ * - An xpriv (BIP32Arg: base58 string, BIP32 instance, or WasmBIP32) for wallet inputs
420
+ * - A raw privkey (ECPairArg: Buffer, ECPair instance, or WasmECPair) for replay protection inputs
379
421
  *
380
- * This method automatically detects and handles different input types:
381
- * - For regular inputs: uses standard PSBT signing
382
- * - For MuSig2 inputs: uses the FirstRound state stored by generateMusig2Nonces()
383
- * - For replay protection inputs: signs with legacy P2SH sighash
422
+ * **Important:** This method is NOT faster than `sign(key)` for non-MuSig2 inputs.
423
+ * The underlying miniscript library signs all inputs regardless. This method uses a
424
+ * save/restore pattern to ensure only the target input receives the signature.
425
+ *
426
+ * Use this method only when you need precise control over which inputs are signed,
427
+ * for example:
428
+ * - Signing MuSig2 inputs (after calling generateMusig2Nonces())
429
+ * - Mixed transactions where different inputs need different keys
430
+ * - Testing or debugging signing behavior
384
431
  *
385
432
  * @param inputIndex - The index of the input to sign (0-based)
386
433
  * @param key - Either an xpriv (BIP32Arg) or a raw privkey (ECPairArg)
@@ -388,28 +435,15 @@ export declare class BitGoPsbt {
388
435
  *
389
436
  * @example
390
437
  * ```typescript
391
- * // Parse transaction to identify input types
392
- * const parsed = psbt.parseTransactionWithWalletKeys(walletKeys, replayProtection);
393
- *
394
- * // Sign regular wallet inputs with xpriv
395
- * for (let i = 0; i < parsed.inputs.length; i++) {
396
- * const input = parsed.inputs[i];
397
- * if (input.scriptId !== null && input.scriptType !== "p2shP2pk") {
398
- * psbt.sign(i, userXpriv);
399
- * }
400
- * }
438
+ * // Sign a specific MuSig2 input after nonce generation
439
+ * psbt.generateMusig2Nonces(userXpriv);
440
+ * psbt.signInput(musig2InputIndex, userXpriv);
401
441
  *
402
- * // Sign replay protection inputs with raw privkey
403
- * const userPrivkey = bip32.fromBase58(userXpriv).privateKey!;
404
- * for (let i = 0; i < parsed.inputs.length; i++) {
405
- * const input = parsed.inputs[i];
406
- * if (input.scriptType === "p2shP2pk") {
407
- * psbt.sign(i, userPrivkey);
408
- * }
409
- * }
442
+ * // Sign a specific replay protection input
443
+ * psbt.signInput(rpInputIndex, replayProtectionPrivkey);
410
444
  * ```
411
445
  */
412
- sign(inputIndex: number, key: BIP32Arg | ECPairArg): void;
446
+ signInput(inputIndex: number, key: BIP32Arg | ECPairArg): void;
413
447
  /**
414
448
  * @deprecated - use verifySignature with the replay protection key instead
415
449
  *
@@ -306,17 +306,49 @@ class BitGoPsbt {
306
306
  const wasmECPair = ecpair_js_1.ECPair.from(key).wasm;
307
307
  return this._wasm.verify_signature_with_pub(inputIndex, wasmECPair);
308
308
  }
309
+ sign(inputIndexOrKey, key) {
310
+ // Detect which overload was called
311
+ if (typeof inputIndexOrKey === "number") {
312
+ // Called as sign(inputIndex, key) - deprecated single-input signing
313
+ if (key === undefined) {
314
+ throw new Error("Key is required when signing a single input");
315
+ }
316
+ this.signInput(inputIndexOrKey, key);
317
+ return;
318
+ }
319
+ // Called as sign(key) - sign all matching inputs
320
+ const keyArg = inputIndexOrKey;
321
+ if ((0, bip32_js_1.isBIP32Arg)(keyArg)) {
322
+ // It's a BIP32Arg - sign all wallet inputs (ECDSA + MuSig2)
323
+ const wasmKey = bip32_js_1.BIP32.from(keyArg);
324
+ // Sign all non-MuSig2 wallet inputs
325
+ const walletSigned = this._wasm.sign_all_wallet_inputs(wasmKey.wasm);
326
+ // Sign all MuSig2 keypath inputs (more efficient - reuses SighashCache)
327
+ const musig2Signed = this._wasm.sign_all_musig2_inputs(wasmKey.wasm);
328
+ return [...walletSigned, ...musig2Signed];
329
+ }
330
+ else {
331
+ // It's an ECPairArg - sign all replay protection inputs
332
+ const wasmKey = ecpair_js_1.ECPair.from(keyArg);
333
+ return this._wasm.sign_replay_protection_inputs(wasmKey.wasm);
334
+ }
335
+ }
309
336
  /**
310
- * Sign a single input with a private key
337
+ * Sign a single input with a private key.
311
338
  *
312
339
  * This method signs a specific input using the provided key. It accepts either:
313
- * - An xpriv (BIP32Arg: base58 string, BIP32 instance, or WasmBIP32) for wallet inputs - derives the key and signs
314
- * - A raw privkey (ECPairArg: Buffer, ECPair instance, or WasmECPair) for replay protection inputs - signs directly
340
+ * - An xpriv (BIP32Arg: base58 string, BIP32 instance, or WasmBIP32) for wallet inputs
341
+ * - A raw privkey (ECPairArg: Buffer, ECPair instance, or WasmECPair) for replay protection inputs
342
+ *
343
+ * **Important:** This method is NOT faster than `sign(key)` for non-MuSig2 inputs.
344
+ * The underlying miniscript library signs all inputs regardless. This method uses a
345
+ * save/restore pattern to ensure only the target input receives the signature.
315
346
  *
316
- * This method automatically detects and handles different input types:
317
- * - For regular inputs: uses standard PSBT signing
318
- * - For MuSig2 inputs: uses the FirstRound state stored by generateMusig2Nonces()
319
- * - For replay protection inputs: signs with legacy P2SH sighash
347
+ * Use this method only when you need precise control over which inputs are signed,
348
+ * for example:
349
+ * - Signing MuSig2 inputs (after calling generateMusig2Nonces())
350
+ * - Mixed transactions where different inputs need different keys
351
+ * - Testing or debugging signing behavior
320
352
  *
321
353
  * @param inputIndex - The index of the input to sign (0-based)
322
354
  * @param key - Either an xpriv (BIP32Arg) or a raw privkey (ECPairArg)
@@ -324,42 +356,30 @@ class BitGoPsbt {
324
356
  *
325
357
  * @example
326
358
  * ```typescript
327
- * // Parse transaction to identify input types
328
- * const parsed = psbt.parseTransactionWithWalletKeys(walletKeys, replayProtection);
329
- *
330
- * // Sign regular wallet inputs with xpriv
331
- * for (let i = 0; i < parsed.inputs.length; i++) {
332
- * const input = parsed.inputs[i];
333
- * if (input.scriptId !== null && input.scriptType !== "p2shP2pk") {
334
- * psbt.sign(i, userXpriv);
335
- * }
336
- * }
359
+ * // Sign a specific MuSig2 input after nonce generation
360
+ * psbt.generateMusig2Nonces(userXpriv);
361
+ * psbt.signInput(musig2InputIndex, userXpriv);
337
362
  *
338
- * // Sign replay protection inputs with raw privkey
339
- * const userPrivkey = bip32.fromBase58(userXpriv).privateKey!;
340
- * for (let i = 0; i < parsed.inputs.length; i++) {
341
- * const input = parsed.inputs[i];
342
- * if (input.scriptType === "p2shP2pk") {
343
- * psbt.sign(i, userPrivkey);
344
- * }
345
- * }
363
+ * // Sign a specific replay protection input
364
+ * psbt.signInput(rpInputIndex, replayProtectionPrivkey);
346
365
  * ```
347
366
  */
348
- sign(inputIndex, key) {
349
- // Detect key type
350
- // If string or has 'derive' method → BIP32Arg
351
- // Otherwise → ECPairArg
352
- if (typeof key === "string" ||
353
- (typeof key === "object" &&
354
- key !== null &&
355
- "derive" in key &&
356
- typeof key.derive === "function")) {
367
+ signInput(inputIndex, key) {
368
+ if ((0, bip32_js_1.isBIP32Arg)(key)) {
357
369
  // It's a BIP32Arg
358
370
  const wasmKey = bip32_js_1.BIP32.from(key);
359
- this._wasm.sign_with_xpriv(inputIndex, wasmKey.wasm);
371
+ // Route to the appropriate method based on input type
372
+ if (this._wasm.is_musig2_input(inputIndex)) {
373
+ // MuSig2 keypath: true single-input signing (efficient)
374
+ this._wasm.sign_musig2_input(inputIndex, wasmKey.wasm);
375
+ }
376
+ else {
377
+ // ECDSA/Schnorr script path: save/restore pattern (not faster than bulk)
378
+ this._wasm.sign_wallet_input(inputIndex, wasmKey.wasm);
379
+ }
360
380
  }
361
381
  else {
362
- // It's an ECPairArg
382
+ // It's an ECPairArg - for replay protection inputs
363
383
  const wasmKey = ecpair_js_1.ECPair.from(key);
364
384
  this._wasm.sign_with_privkey(inputIndex, wasmKey.wasm);
365
385
  }
@@ -146,6 +146,20 @@ export class BitGoPsbt {
146
146
  * Get the unsigned transaction ID
147
147
  */
148
148
  unsigned_txid(): string;
149
+ /**
150
+ * Check if an input is a MuSig2 keypath input.
151
+ *
152
+ * MuSig2 inputs require special handling: nonces must be generated first with
153
+ * `generate_musig2_nonces()`, then signed with `sign_musig2_input()`.
154
+ *
155
+ * # Arguments
156
+ * - `input_index`: The index of the input to check (0-based)
157
+ *
158
+ * # Returns
159
+ * - `true` if the input is a MuSig2 keypath input
160
+ * - `false` otherwise (or if input_index is out of bounds)
161
+ */
162
+ is_musig2_input(input_index: number): boolean;
149
163
  /**
150
164
  * Sign a single input with an extended private key (xpriv)
151
165
  *
@@ -208,6 +222,45 @@ export class BitGoPsbt {
208
222
  * The index of the newly added output
209
223
  */
210
224
  add_wallet_output(chain: number, index: number, value: bigint, wallet_keys: WasmRootWalletKeys): number;
225
+ /**
226
+ * Sign a single MuSig2 keypath input.
227
+ *
228
+ * This uses the FirstRound state generated by `generate_musig2_nonces()`.
229
+ * Each FirstRound can only be used once (nonce reuse is a security risk).
230
+ *
231
+ * For non-MuSig2 inputs, returns an error (use `sign_wallet_input` instead).
232
+ *
233
+ * # Arguments
234
+ * - `input_index`: The index of the input to sign (0-based)
235
+ * - `xpriv`: The extended private key as a WasmBIP32 instance
236
+ *
237
+ * # Returns
238
+ * - `Ok(())` if signing was successful
239
+ * - `Err(WasmUtxoError)` if signing fails, no FirstRound exists, or not a MuSig2 input
240
+ */
241
+ sign_musig2_input(input_index: number, xpriv: WasmBIP32): void;
242
+ /**
243
+ * Sign a single non-MuSig2 wallet input using save/restore pattern.
244
+ *
245
+ * For MuSig2 inputs, returns an error (use `sign_musig2_input` instead).
246
+ * For ECDSA inputs, this uses a save/restore pattern: clones the PSBT,
247
+ * signs all inputs on the clone, then copies only the target input's
248
+ * signatures back.
249
+ *
250
+ * **Important:** This is NOT faster than `sign_all_wallet_inputs()` for ECDSA inputs.
251
+ * The underlying library signs all inputs regardless. This method just ensures
252
+ * that only the specified input gets signatures added to the PSBT.
253
+ * Use `sign_all_wallet_inputs()` when signing multiple inputs with the same key.
254
+ *
255
+ * # Arguments
256
+ * - `input_index`: The index of the input to sign (0-based)
257
+ * - `xpriv`: The extended private key as a WasmBIP32 instance
258
+ *
259
+ * # Returns
260
+ * - `Ok(())` if the input was signed
261
+ * - `Err(WasmUtxoError)` if signing fails or input is MuSig2
262
+ */
263
+ sign_wallet_input(input_index: number, xpriv: WasmBIP32): void;
211
264
  /**
212
265
  * Sign a single input with a raw private key
213
266
  *
@@ -266,6 +319,24 @@ export class BitGoPsbt {
266
319
  * - `Err(WasmUtxoError)` if any input failed to finalize
267
320
  */
268
321
  finalize_all_inputs(): void;
322
+ /**
323
+ * Sign all non-MuSig2 inputs with an extended private key (xpriv) in a single pass.
324
+ *
325
+ * This is more efficient than calling `sign_with_xpriv` for each input individually.
326
+ * The underlying miniscript library's `sign` method signs all matching inputs at once.
327
+ *
328
+ * **Note:** MuSig2 inputs are skipped by this method because they require FirstRound
329
+ * state from nonce generation. After calling this method, sign MuSig2 inputs
330
+ * individually using `sign_with_xpriv`.
331
+ *
332
+ * # Arguments
333
+ * - `xpriv`: The extended private key as a WasmBIP32 instance
334
+ *
335
+ * # Returns
336
+ * - `Ok(JsValue)` with an array of input indices that were signed
337
+ * - `Err(WasmUtxoError)` if signing fails
338
+ */
339
+ sign_all_with_xpriv(xpriv: WasmBIP32): any;
269
340
  /**
270
341
  * Add a PayGo attestation to a PSBT output
271
342
  *
@@ -325,6 +396,41 @@ export class BitGoPsbt {
325
396
  * generated for security. Custom session_id is only allowed on testnets for testing purposes.
326
397
  */
327
398
  generate_musig2_nonces(xpriv: WasmBIP32, session_id_bytes?: Uint8Array | null): void;
399
+ /**
400
+ * Sign all MuSig2 keypath inputs in a single pass with optimized sighash computation.
401
+ *
402
+ * This is more efficient than calling `sign_musig2_input()` for each input because
403
+ * it reuses the SighashCache across all inputs, avoiding redundant computation of
404
+ * sha_prevouts, sha_amounts, sha_scriptpubkeys, sha_sequences, and sha_outputs.
405
+ *
406
+ * Each MuSig2 input requires a FirstRound from `generate_musig2_nonces()`.
407
+ * FirstRounds that have already been consumed (signed) are skipped.
408
+ *
409
+ * # Arguments
410
+ * - `xpriv`: The extended private key as a WasmBIP32 instance
411
+ *
412
+ * # Returns
413
+ * - `Ok(JsValue)` with an array of input indices that were signed
414
+ * - `Err(WasmUtxoError)` if signing fails
415
+ */
416
+ sign_all_musig2_inputs(xpriv: WasmBIP32): any;
417
+ /**
418
+ * Sign all non-MuSig2 wallet inputs in a single efficient pass.
419
+ *
420
+ * This signs all ECDSA (P2SH, P2SH-P2WSH, P2WSH) and Taproot script path (P2TR)
421
+ * inputs that match the provided xpriv. MuSig2 keypath inputs are skipped.
422
+ *
423
+ * This is the most efficient way to sign wallet inputs. After calling this,
424
+ * sign any MuSig2 inputs using `sign_all_musig2_inputs()` or `sign_musig2_input()`.
425
+ *
426
+ * # Arguments
427
+ * - `xpriv`: The extended private key as a WasmBIP32 instance
428
+ *
429
+ * # Returns
430
+ * - `Ok(JsValue)` with an array of input indices that were signed
431
+ * - `Err(WasmUtxoError)` if signing fails
432
+ */
433
+ sign_all_wallet_inputs(xpriv: WasmBIP32): any;
328
434
  /**
329
435
  * Add an output to the PSBT by address
330
436
  *
@@ -411,12 +517,79 @@ export class BitGoPsbt {
411
517
  * Returns error if block height is before Overwinter activation
412
518
  */
413
519
  static create_empty_zcash_at_height(network: string, wallet_keys: WasmRootWalletKeys, block_height: number, version?: number | null, lock_time?: number | null, version_group_id?: number | null, expiry_height?: number | null): BitGoPsbt;
520
+ /**
521
+ * Sign a single input with an extended private key, using save/restore for ECDSA inputs.
522
+ *
523
+ * For MuSig2 inputs, this returns an error (use sign_with_xpriv which handles FirstRound).
524
+ * For ECDSA inputs, this clones the PSBT, signs all, then copies only the target
525
+ * input's signatures back.
526
+ *
527
+ * **Important:** This is NOT faster than `sign_all_with_xpriv` for ECDSA inputs.
528
+ * The underlying miniscript library signs all inputs regardless. This method
529
+ * just prevents signatures from being added to other inputs.
530
+ *
531
+ * # Arguments
532
+ * - `input_index`: The index of the input to sign (0-based)
533
+ * - `xpriv`: The extended private key as a WasmBIP32 instance
534
+ *
535
+ * # Returns
536
+ * - `Ok(())` if the input was signed
537
+ * - `Err(WasmUtxoError)` if signing fails
538
+ */
539
+ sign_single_input_with_xpriv(input_index: number, xpriv: WasmBIP32): void;
540
+ /**
541
+ * Sign all replay protection inputs with a raw private key.
542
+ *
543
+ * This iterates through all inputs looking for P2SH-P2PK (replay protection) inputs
544
+ * that match the provided public key and signs them.
545
+ *
546
+ * # Arguments
547
+ * - `ecpair`: The ECPair containing the private key
548
+ *
549
+ * # Returns
550
+ * - `Ok(JsValue)` with an array of input indices that were signed
551
+ * - `Err(WasmUtxoError)` if signing fails
552
+ */
553
+ sign_replay_protection_inputs(ecpair: WasmECPair): any;
414
554
  /**
415
555
  * Parse outputs with wallet keys to identify which outputs belong to a wallet
416
556
  *
417
557
  * Note: This method does NOT validate wallet inputs. It only parses outputs.
418
558
  */
419
559
  parse_outputs_with_wallet_keys(wallet_keys: WasmRootWalletKeys, paygo_pubkeys?: WasmECPair[] | null): any;
560
+ /**
561
+ * Sign a single input with a raw private key, using save/restore for regular inputs.
562
+ *
563
+ * For replay protection inputs (P2SH-P2PK), this uses direct signing which is
564
+ * already single-input. For regular inputs, this clones the PSBT, signs all,
565
+ * then copies only the target input's signatures back.
566
+ *
567
+ * **Important:** This is NOT faster than signing all inputs for regular (non-RP) inputs.
568
+ * The underlying miniscript library signs all inputs regardless.
569
+ *
570
+ * # Arguments
571
+ * - `input_index`: The index of the input to sign (0-based)
572
+ * - `ecpair`: The ECPair containing the private key
573
+ *
574
+ * # Returns
575
+ * - `Ok(())` if the input was signed
576
+ * - `Err(WasmUtxoError)` if signing fails
577
+ */
578
+ sign_single_input_with_privkey(input_index: number, ecpair: WasmECPair): void;
579
+ /**
580
+ * Sign all replay protection inputs with a raw private key.
581
+ *
582
+ * This iterates through all inputs looking for P2SH-P2PK (replay protection) inputs
583
+ * that match the provided public key and signs them.
584
+ *
585
+ * # Arguments
586
+ * - `ecpair`: The ECPair containing the private key
587
+ *
588
+ * # Returns
589
+ * - `Ok(JsValue)` with an array of input indices that were signed
590
+ * - `Err(WasmUtxoError)` if signing fails
591
+ */
592
+ sign_all_replay_protection_inputs(ecpair: WasmECPair): any;
420
593
  /**
421
594
  * Parse transaction with wallet keys to identify wallet inputs/outputs
422
595
  */