@btc-vision/transaction 1.3.2 → 1.3.4

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.
@@ -118,6 +118,12 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
118
118
  */
119
119
  protected utxos: UTXO[];
120
120
 
121
+ /**
122
+ * @description The inputs of the transaction
123
+ * @protected
124
+ */
125
+ protected optionalInputs: UTXO[];
126
+
121
127
  /**
122
128
  * @description The address where the transaction is sent to
123
129
  * @protected
@@ -154,6 +160,7 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
154
160
  this.priorityFee = parameters.priorityFee ?? 0n;
155
161
  this.gasSatFee = parameters.gasSatFee ?? 0n;
156
162
  this.utxos = parameters.utxos;
163
+ this.optionalInputs = parameters.optionalInputs || [];
157
164
  this.to = parameters.to || undefined;
158
165
 
159
166
  this.isPubKeyDestination = this.to
@@ -236,6 +243,7 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
236
243
  from: this.from,
237
244
  amount: this.estimatedFees,
238
245
  optionalOutputs: this.optionalOutputs,
246
+ optionalInputs: this.optionalInputs,
239
247
  };
240
248
  }
241
249
 
@@ -551,6 +559,11 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
551
559
  for (const utxo of this.utxos) {
552
560
  total += utxo.value;
553
561
  }
562
+
563
+ for (const utxo of this.optionalInputs) {
564
+ total += utxo.value;
565
+ }
566
+
554
567
  return total;
555
568
  }
556
569
 
@@ -565,6 +578,10 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
565
578
  total += utxo.value;
566
579
  }
567
580
 
581
+ for (const utxo of this.optionalInputs) {
582
+ total += utxo.value;
583
+ }
584
+
568
585
  return total;
569
586
  }
570
587
 
@@ -607,6 +624,19 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
607
624
  this.addInput(input);
608
625
  }
609
626
  }
627
+
628
+ if (this.optionalInputs) {
629
+ for (
630
+ let i = this.utxos.length;
631
+ i < this.optionalInputs.length + this.utxos.length;
632
+ i++
633
+ ) {
634
+ const utxo = this.optionalInputs[i - this.utxos.length];
635
+ const input = this.generatePsbtInputExtended(utxo, i, true);
636
+
637
+ this.addInput(input);
638
+ }
639
+ }
610
640
  }
611
641
 
612
642
  /**
@@ -676,6 +706,12 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
676
706
  throw new Error('Address is required');
677
707
  }
678
708
  }
709
+
710
+ for (const utxo of this.optionalInputs) {
711
+ if (!utxo.scriptPubKey) {
712
+ throw new Error('Address is required');
713
+ }
714
+ }
679
715
  }
680
716
 
681
717
  /**
@@ -11,7 +11,8 @@ export interface ITransactionParameters extends ITweakedTransactionData {
11
11
 
12
12
  nonWitnessUtxo?: Buffer;
13
13
  estimatedFees?: bigint;
14
-
14
+
15
+ optionalInputs?: UTXO[];
15
16
  optionalOutputs?: PsbtOutputExtended[];
16
17
 
17
18
  chainId?: ChainId;
@@ -462,6 +462,10 @@ export abstract class TweakedTransaction extends Logger {
462
462
  return;
463
463
  }
464
464
 
465
+ await this.signInputsNonWalletBased(transaction);
466
+ }
467
+
468
+ protected async signInputsNonWalletBased(transaction: Psbt): Promise<void> {
465
469
  // non web based signing.
466
470
  const txs: PsbtInput[] = transaction.data.inputs;
467
471
 
@@ -646,10 +650,15 @@ export abstract class TweakedTransaction extends Logger {
646
650
  * Generate the PSBT input extended, supporting various script types
647
651
  * @param {UTXO} utxo The UTXO
648
652
  * @param {number} i The index of the input
653
+ * @param {UTXO} extra Extra UTXO
649
654
  * @protected
650
655
  * @returns {PsbtInputExtended} The PSBT input extended
651
656
  */
652
- protected generatePsbtInputExtended(utxo: UTXO, i: number): PsbtInputExtended {
657
+ protected generatePsbtInputExtended(
658
+ utxo: UTXO,
659
+ i: number,
660
+ extra: boolean = false,
661
+ ): PsbtInputExtended {
653
662
  const script = Buffer.from(utxo.scriptPubKey.hex, 'hex');
654
663
 
655
664
  const input: PsbtInputExtended = {
@@ -788,9 +797,11 @@ export abstract class TweakedTransaction extends Logger {
788
797
  this.error(`Unknown or unsupported script type for output: ${utxo.scriptPubKey.hex}`);
789
798
  }
790
799
 
791
- // TapLeafScript if available
792
- if (this.tapLeafScript) {
793
- input.tapLeafScript = [this.tapLeafScript];
800
+ if (i === 0) {
801
+ // TapLeafScript if available
802
+ if (this.tapLeafScript) {
803
+ input.tapLeafScript = [this.tapLeafScript];
804
+ }
794
805
  }
795
806
 
796
807
  // If the first input and we have a global nonWitnessUtxo not yet set
@@ -798,6 +809,18 @@ export abstract class TweakedTransaction extends Logger {
798
809
  input.nonWitnessUtxo = this.nonWitnessUtxo;
799
810
  }
800
811
 
812
+ /*if (utxo.nonWitnessUtxo && extra) {
813
+ const witness = Buffer.isBuffer(utxo.nonWitnessUtxo)
814
+ ? utxo.nonWitnessUtxo
815
+ : typeof utxo.nonWitnessUtxo === 'string'
816
+ ? Buffer.from(utxo.nonWitnessUtxo, 'hex')
817
+ : (utxo.nonWitnessUtxo as unknown) instanceof Uint8Array
818
+ ? Buffer.from(utxo.nonWitnessUtxo)
819
+ : undefined;
820
+
821
+ input.nonWitnessUtxo = witness;
822
+ }*/
823
+
801
824
  return input;
802
825
  }
803
826
 
@@ -861,7 +884,16 @@ export abstract class TweakedTransaction extends Logger {
861
884
  }
862
885
 
863
886
  if (tweakedSigner) {
864
- await this.signTaprootInput(tweakedSigner, transaction, i);
887
+ try {
888
+ await this.signTaprootInput(tweakedSigner, transaction, i);
889
+ } catch (e) {
890
+ tweakedSigner = this.getTweakedSigner(false, this.signer);
891
+ if (!tweakedSigner) {
892
+ throw new Error(`Failed to obtain tweaked signer for input ${i}.`);
893
+ }
894
+
895
+ await this.signTaprootInput(tweakedSigner, transaction, i);
896
+ }
865
897
  } else {
866
898
  this.error(`Failed to obtain tweaked signer for input ${i}.`);
867
899
  }