@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.
@@ -1,7 +1,7 @@
1
1
  import { BitGoPsbt as WasmBitGoPsbt } from "../wasm/wasm_utxo.js";
2
2
  import { RootWalletKeys } from "./RootWalletKeys.js";
3
3
  import { ReplayProtection } from "./ReplayProtection.js";
4
- import { BIP32 } from "../bip32.js";
4
+ import { BIP32, isBIP32Arg } from "../bip32.js";
5
5
  import { ECPair } from "../ecpair.js";
6
6
  export class BitGoPsbt {
7
7
  _wasm;
@@ -303,17 +303,49 @@ export class BitGoPsbt {
303
303
  const wasmECPair = ECPair.from(key).wasm;
304
304
  return this._wasm.verify_signature_with_pub(inputIndex, wasmECPair);
305
305
  }
306
+ sign(inputIndexOrKey, key) {
307
+ // Detect which overload was called
308
+ if (typeof inputIndexOrKey === "number") {
309
+ // Called as sign(inputIndex, key) - deprecated single-input signing
310
+ if (key === undefined) {
311
+ throw new Error("Key is required when signing a single input");
312
+ }
313
+ this.signInput(inputIndexOrKey, key);
314
+ return;
315
+ }
316
+ // Called as sign(key) - sign all matching inputs
317
+ const keyArg = inputIndexOrKey;
318
+ if (isBIP32Arg(keyArg)) {
319
+ // It's a BIP32Arg - sign all wallet inputs (ECDSA + MuSig2)
320
+ const wasmKey = BIP32.from(keyArg);
321
+ // Sign all non-MuSig2 wallet inputs
322
+ const walletSigned = this._wasm.sign_all_wallet_inputs(wasmKey.wasm);
323
+ // Sign all MuSig2 keypath inputs (more efficient - reuses SighashCache)
324
+ const musig2Signed = this._wasm.sign_all_musig2_inputs(wasmKey.wasm);
325
+ return [...walletSigned, ...musig2Signed];
326
+ }
327
+ else {
328
+ // It's an ECPairArg - sign all replay protection inputs
329
+ const wasmKey = ECPair.from(keyArg);
330
+ return this._wasm.sign_replay_protection_inputs(wasmKey.wasm);
331
+ }
332
+ }
306
333
  /**
307
- * Sign a single input with a private key
334
+ * Sign a single input with a private key.
308
335
  *
309
336
  * This method signs a specific input using the provided key. It accepts either:
310
- * - An xpriv (BIP32Arg: base58 string, BIP32 instance, or WasmBIP32) for wallet inputs - derives the key and signs
311
- * - A raw privkey (ECPairArg: Buffer, ECPair instance, or WasmECPair) for replay protection inputs - signs directly
337
+ * - An xpriv (BIP32Arg: base58 string, BIP32 instance, or WasmBIP32) for wallet inputs
338
+ * - A raw privkey (ECPairArg: Buffer, ECPair instance, or WasmECPair) for replay protection inputs
339
+ *
340
+ * **Important:** This method is NOT faster than `sign(key)` for non-MuSig2 inputs.
341
+ * The underlying miniscript library signs all inputs regardless. This method uses a
342
+ * save/restore pattern to ensure only the target input receives the signature.
312
343
  *
313
- * This method automatically detects and handles different input types:
314
- * - For regular inputs: uses standard PSBT signing
315
- * - For MuSig2 inputs: uses the FirstRound state stored by generateMusig2Nonces()
316
- * - For replay protection inputs: signs with legacy P2SH sighash
344
+ * Use this method only when you need precise control over which inputs are signed,
345
+ * for example:
346
+ * - Signing MuSig2 inputs (after calling generateMusig2Nonces())
347
+ * - Mixed transactions where different inputs need different keys
348
+ * - Testing or debugging signing behavior
317
349
  *
318
350
  * @param inputIndex - The index of the input to sign (0-based)
319
351
  * @param key - Either an xpriv (BIP32Arg) or a raw privkey (ECPairArg)
@@ -321,42 +353,30 @@ export class BitGoPsbt {
321
353
  *
322
354
  * @example
323
355
  * ```typescript
324
- * // Parse transaction to identify input types
325
- * const parsed = psbt.parseTransactionWithWalletKeys(walletKeys, replayProtection);
326
- *
327
- * // Sign regular wallet inputs with xpriv
328
- * for (let i = 0; i < parsed.inputs.length; i++) {
329
- * const input = parsed.inputs[i];
330
- * if (input.scriptId !== null && input.scriptType !== "p2shP2pk") {
331
- * psbt.sign(i, userXpriv);
332
- * }
333
- * }
356
+ * // Sign a specific MuSig2 input after nonce generation
357
+ * psbt.generateMusig2Nonces(userXpriv);
358
+ * psbt.signInput(musig2InputIndex, userXpriv);
334
359
  *
335
- * // Sign replay protection inputs with raw privkey
336
- * const userPrivkey = bip32.fromBase58(userXpriv).privateKey!;
337
- * for (let i = 0; i < parsed.inputs.length; i++) {
338
- * const input = parsed.inputs[i];
339
- * if (input.scriptType === "p2shP2pk") {
340
- * psbt.sign(i, userPrivkey);
341
- * }
342
- * }
360
+ * // Sign a specific replay protection input
361
+ * psbt.signInput(rpInputIndex, replayProtectionPrivkey);
343
362
  * ```
344
363
  */
345
- sign(inputIndex, key) {
346
- // Detect key type
347
- // If string or has 'derive' method → BIP32Arg
348
- // Otherwise → ECPairArg
349
- if (typeof key === "string" ||
350
- (typeof key === "object" &&
351
- key !== null &&
352
- "derive" in key &&
353
- typeof key.derive === "function")) {
364
+ signInput(inputIndex, key) {
365
+ if (isBIP32Arg(key)) {
354
366
  // It's a BIP32Arg
355
367
  const wasmKey = BIP32.from(key);
356
- this._wasm.sign_with_xpriv(inputIndex, wasmKey.wasm);
368
+ // Route to the appropriate method based on input type
369
+ if (this._wasm.is_musig2_input(inputIndex)) {
370
+ // MuSig2 keypath: true single-input signing (efficient)
371
+ this._wasm.sign_musig2_input(inputIndex, wasmKey.wasm);
372
+ }
373
+ else {
374
+ // ECDSA/Schnorr script path: save/restore pattern (not faster than bulk)
375
+ this._wasm.sign_wallet_input(inputIndex, wasmKey.wasm);
376
+ }
357
377
  }
358
378
  else {
359
- // It's an ECPairArg
379
+ // It's an ECPairArg - for replay protection inputs
360
380
  const wasmKey = ECPair.from(key);
361
381
  this._wasm.sign_with_privkey(inputIndex, wasmKey.wasm);
362
382
  }
@@ -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
  */