@caravan/psbt 1.4.2 → 1.5.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.
package/dist/index.d.ts CHANGED
@@ -209,6 +209,65 @@ declare class PsbtV2 extends PsbtV2Maps {
209
209
  * validate the current state of the psbt.
210
210
  */
211
211
  private validate;
212
+ /**
213
+ * Sets the sequence number for a specific input in the transaction.
214
+ *
215
+ * This private helper method is crucial for implementing RBF and other
216
+ * sequence-based transaction features. It writes the provided sequence
217
+ * number as a 32-bit little-endian unsigned integer and stores it in the
218
+ * appropriate input's map using the PSBT_IN_SEQUENCE key.
219
+ *
220
+ * The sequence number has multiple uses in Bitcoin transactions:
221
+ * 1. Signaling RBF (values < 0xfffffffe)
222
+ * 2. Enabling nLockTime (values < 0xffffffff)
223
+ * 3. Relative timelock with BIP68 (if bit 31 is not set)
224
+ *
225
+ * According to BIP125 (Opt-in Full Replace-by-Fee Signaling):
226
+ *
227
+ * - For a transaction to be considered opt-in RBF, it must have at least
228
+ * one input with a sequence number < 0xfffffffe.
229
+ * - The recommended sequence for RBF is 0xffffffff-2 (0xfffffffd).
230
+ *
231
+ * Sequence number meanings:
232
+ * - = 0xffffffff: Then the transaction is final no matter the nLockTime.
233
+ * - < 0xfffffffe: Transaction signals for RBF.
234
+ * - < 0xefffffff : Then the transaction signals BIP68 relative locktime.
235
+ *
236
+ * For using nLocktime along with Opt-in RBF, the sequence value
237
+ * should be between 0xf0000000 and 0xfffffffd.
238
+ *
239
+ * Care should be taken when setting sequence numbers to ensure the desired
240
+ * transaction properties are correctly signaled. Improper use can lead to
241
+ * unexpected transaction behavior or rejection by the network.
242
+ *
243
+ * References:
244
+ * - BIP125: Opt-in Full Replace-by-Fee Signaling
245
+ * https://github.com/bitcoin/bips/blob/master/bip-0125.mediawiki
246
+ * - BIP68: Relative lock-time using consensus-enforced sequence numbers
247
+ * https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki
248
+ */
249
+ setInputSequence(inputIndex: number, sequence: number): void;
250
+ /**
251
+ * Checks if the transaction signals Replace-by-Fee (RBF).
252
+ *
253
+ * This method determines whether the transaction is eligible for RBF by
254
+ * examining the sequence numbers of all inputs. As per BIP125, a transaction
255
+ * is considered to have opted in to RBF if it contains at least one input
256
+ * with a sequence number less than (0xffffffff - 1).
257
+ *
258
+ * Return value:
259
+ * - true: If any input has a sequence number < 0xfffffffe, indicating RBF.
260
+ * - false: If all inputs have sequence numbers >= 0xfffffffe, indicating no RBF.
261
+ *
262
+ * This method is useful for wallets, block explorers, or any service that
263
+ * needs to determine if a transaction can potentially be replaced before
264
+ * confirmation.
265
+ *
266
+ * References:
267
+ * - BIP125: Opt-in Full Replace-by-Fee Signaling
268
+ * https://github.com/bitcoin/bips/blob/master/bip-0125.mediawiki
269
+ */
270
+ get isRBFSignaled(): boolean;
212
271
  /**
213
272
  * This method is provided for compatibility issues and probably shouldn't be
214
273
  * used since a PsbtV2 with PSBT_GLOBAL_TX_VERSION = 1 is BIP0370
package/dist/index.js CHANGED
@@ -789,6 +789,81 @@ var PsbtV2 = class _PsbtV2 extends PsbtV2Maps {
789
789
  }
790
790
  }
791
791
  }
792
+ /**
793
+ * Sets the sequence number for a specific input in the transaction.
794
+ *
795
+ * This private helper method is crucial for implementing RBF and other
796
+ * sequence-based transaction features. It writes the provided sequence
797
+ * number as a 32-bit little-endian unsigned integer and stores it in the
798
+ * appropriate input's map using the PSBT_IN_SEQUENCE key.
799
+ *
800
+ * The sequence number has multiple uses in Bitcoin transactions:
801
+ * 1. Signaling RBF (values < 0xfffffffe)
802
+ * 2. Enabling nLockTime (values < 0xffffffff)
803
+ * 3. Relative timelock with BIP68 (if bit 31 is not set)
804
+ *
805
+ * According to BIP125 (Opt-in Full Replace-by-Fee Signaling):
806
+ *
807
+ * - For a transaction to be considered opt-in RBF, it must have at least
808
+ * one input with a sequence number < 0xfffffffe.
809
+ * - The recommended sequence for RBF is 0xffffffff-2 (0xfffffffd).
810
+ *
811
+ * Sequence number meanings:
812
+ * - = 0xffffffff: Then the transaction is final no matter the nLockTime.
813
+ * - < 0xfffffffe: Transaction signals for RBF.
814
+ * - < 0xefffffff : Then the transaction signals BIP68 relative locktime.
815
+ *
816
+ * For using nLocktime along with Opt-in RBF, the sequence value
817
+ * should be between 0xf0000000 and 0xfffffffd.
818
+ *
819
+ * Care should be taken when setting sequence numbers to ensure the desired
820
+ * transaction properties are correctly signaled. Improper use can lead to
821
+ * unexpected transaction behavior or rejection by the network.
822
+ *
823
+ * References:
824
+ * - BIP125: Opt-in Full Replace-by-Fee Signaling
825
+ * https://github.com/bitcoin/bips/blob/master/bip-0125.mediawiki
826
+ * - BIP68: Relative lock-time using consensus-enforced sequence numbers
827
+ * https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki
828
+ */
829
+ setInputSequence(inputIndex, sequence) {
830
+ if (!this.isReadyForUpdater) {
831
+ throw new Error(
832
+ "PSBT is not ready for the Updater role. Sequence cannot be changed."
833
+ );
834
+ }
835
+ if (inputIndex < 0 || inputIndex >= this.PSBT_GLOBAL_INPUT_COUNT) {
836
+ throw new Error(`Input at index ${inputIndex} does not exist.`);
837
+ }
838
+ const bw = new import_bufio3.BufferWriter();
839
+ bw.writeU32(sequence);
840
+ this.inputMaps[inputIndex].set("10" /* PSBT_IN_SEQUENCE */, bw.render());
841
+ }
842
+ /**
843
+ * Checks if the transaction signals Replace-by-Fee (RBF).
844
+ *
845
+ * This method determines whether the transaction is eligible for RBF by
846
+ * examining the sequence numbers of all inputs. As per BIP125, a transaction
847
+ * is considered to have opted in to RBF if it contains at least one input
848
+ * with a sequence number less than (0xffffffff - 1).
849
+ *
850
+ * Return value:
851
+ * - true: If any input has a sequence number < 0xfffffffe, indicating RBF.
852
+ * - false: If all inputs have sequence numbers >= 0xfffffffe, indicating no RBF.
853
+ *
854
+ * This method is useful for wallets, block explorers, or any service that
855
+ * needs to determine if a transaction can potentially be replaced before
856
+ * confirmation.
857
+ *
858
+ * References:
859
+ * - BIP125: Opt-in Full Replace-by-Fee Signaling
860
+ * https://github.com/bitcoin/bips/blob/master/bip-0125.mediawiki
861
+ */
862
+ get isRBFSignaled() {
863
+ return this.PSBT_IN_SEQUENCE.some(
864
+ (seq) => seq !== null && seq < 4294967294
865
+ );
866
+ }
792
867
  /**
793
868
  * This method is provided for compatibility issues and probably shouldn't be
794
869
  * used since a PsbtV2 with PSBT_GLOBAL_TX_VERSION = 1 is BIP0370
@@ -93787,8 +93862,7 @@ var convertLegacyOutput = (output) => {
93787
93862
  address: output.address,
93788
93863
  value: new import_bignumber.default(output.amountSats).toNumber(),
93789
93864
  bip32Derivation: output.bip32Derivation || getBip32Derivation(output.multisig),
93790
- witnessScript: output.witnessScript || output.multisig?.witness?.output,
93791
- redeemScript: output.redeemScript || output.multisig?.redeem?.output
93865
+ ...psbtMultisigLock(output.multisig)
93792
93866
  };
93793
93867
  };
93794
93868
  var getWalletConfigFromInput = (input) => {
@@ -93841,10 +93915,11 @@ var getUnsignedMultisigPsbtV0 = ({
93841
93915
  var psbtInputFormatter = (input, addressType) => {
93842
93916
  const tx = import_bitcoinjs_lib_v63.Transaction.fromHex(input.transactionHex);
93843
93917
  const inputData = { ...input };
93918
+ const nonWitnessUtxo = tx.toBuffer();
93844
93919
  if (addressType === import_bitcoin4.P2SH) {
93845
- const nonWitnessUtxo = tx.toBuffer();
93846
93920
  inputData.nonWitnessUtxo = nonWitnessUtxo;
93847
93921
  } else {
93922
+ inputData.nonWitnessUtxo = nonWitnessUtxo;
93848
93923
  inputData.witnessUtxo = tx.outs[input.index];
93849
93924
  }
93850
93925
  Object.keys(inputData).forEach((key) => {
package/dist/index.mjs CHANGED
@@ -748,6 +748,81 @@ var PsbtV2 = class _PsbtV2 extends PsbtV2Maps {
748
748
  }
749
749
  }
750
750
  }
751
+ /**
752
+ * Sets the sequence number for a specific input in the transaction.
753
+ *
754
+ * This private helper method is crucial for implementing RBF and other
755
+ * sequence-based transaction features. It writes the provided sequence
756
+ * number as a 32-bit little-endian unsigned integer and stores it in the
757
+ * appropriate input's map using the PSBT_IN_SEQUENCE key.
758
+ *
759
+ * The sequence number has multiple uses in Bitcoin transactions:
760
+ * 1. Signaling RBF (values < 0xfffffffe)
761
+ * 2. Enabling nLockTime (values < 0xffffffff)
762
+ * 3. Relative timelock with BIP68 (if bit 31 is not set)
763
+ *
764
+ * According to BIP125 (Opt-in Full Replace-by-Fee Signaling):
765
+ *
766
+ * - For a transaction to be considered opt-in RBF, it must have at least
767
+ * one input with a sequence number < 0xfffffffe.
768
+ * - The recommended sequence for RBF is 0xffffffff-2 (0xfffffffd).
769
+ *
770
+ * Sequence number meanings:
771
+ * - = 0xffffffff: Then the transaction is final no matter the nLockTime.
772
+ * - < 0xfffffffe: Transaction signals for RBF.
773
+ * - < 0xefffffff : Then the transaction signals BIP68 relative locktime.
774
+ *
775
+ * For using nLocktime along with Opt-in RBF, the sequence value
776
+ * should be between 0xf0000000 and 0xfffffffd.
777
+ *
778
+ * Care should be taken when setting sequence numbers to ensure the desired
779
+ * transaction properties are correctly signaled. Improper use can lead to
780
+ * unexpected transaction behavior or rejection by the network.
781
+ *
782
+ * References:
783
+ * - BIP125: Opt-in Full Replace-by-Fee Signaling
784
+ * https://github.com/bitcoin/bips/blob/master/bip-0125.mediawiki
785
+ * - BIP68: Relative lock-time using consensus-enforced sequence numbers
786
+ * https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki
787
+ */
788
+ setInputSequence(inputIndex, sequence) {
789
+ if (!this.isReadyForUpdater) {
790
+ throw new Error(
791
+ "PSBT is not ready for the Updater role. Sequence cannot be changed."
792
+ );
793
+ }
794
+ if (inputIndex < 0 || inputIndex >= this.PSBT_GLOBAL_INPUT_COUNT) {
795
+ throw new Error(`Input at index ${inputIndex} does not exist.`);
796
+ }
797
+ const bw = new BufferWriter3();
798
+ bw.writeU32(sequence);
799
+ this.inputMaps[inputIndex].set("10" /* PSBT_IN_SEQUENCE */, bw.render());
800
+ }
801
+ /**
802
+ * Checks if the transaction signals Replace-by-Fee (RBF).
803
+ *
804
+ * This method determines whether the transaction is eligible for RBF by
805
+ * examining the sequence numbers of all inputs. As per BIP125, a transaction
806
+ * is considered to have opted in to RBF if it contains at least one input
807
+ * with a sequence number less than (0xffffffff - 1).
808
+ *
809
+ * Return value:
810
+ * - true: If any input has a sequence number < 0xfffffffe, indicating RBF.
811
+ * - false: If all inputs have sequence numbers >= 0xfffffffe, indicating no RBF.
812
+ *
813
+ * This method is useful for wallets, block explorers, or any service that
814
+ * needs to determine if a transaction can potentially be replaced before
815
+ * confirmation.
816
+ *
817
+ * References:
818
+ * - BIP125: Opt-in Full Replace-by-Fee Signaling
819
+ * https://github.com/bitcoin/bips/blob/master/bip-0125.mediawiki
820
+ */
821
+ get isRBFSignaled() {
822
+ return this.PSBT_IN_SEQUENCE.some(
823
+ (seq) => seq !== null && seq < 4294967294
824
+ );
825
+ }
751
826
  /**
752
827
  * This method is provided for compatibility issues and probably shouldn't be
753
828
  * used since a PsbtV2 with PSBT_GLOBAL_TX_VERSION = 1 is BIP0370
@@ -93765,8 +93840,7 @@ var convertLegacyOutput = (output) => {
93765
93840
  address: output.address,
93766
93841
  value: new BigNumber(output.amountSats).toNumber(),
93767
93842
  bip32Derivation: output.bip32Derivation || getBip32Derivation(output.multisig),
93768
- witnessScript: output.witnessScript || output.multisig?.witness?.output,
93769
- redeemScript: output.redeemScript || output.multisig?.redeem?.output
93843
+ ...psbtMultisigLock(output.multisig)
93770
93844
  };
93771
93845
  };
93772
93846
  var getWalletConfigFromInput = (input) => {
@@ -93819,10 +93893,11 @@ var getUnsignedMultisigPsbtV0 = ({
93819
93893
  var psbtInputFormatter = (input, addressType) => {
93820
93894
  const tx = Transaction.fromHex(input.transactionHex);
93821
93895
  const inputData = { ...input };
93896
+ const nonWitnessUtxo = tx.toBuffer();
93822
93897
  if (addressType === P2SH2) {
93823
- const nonWitnessUtxo = tx.toBuffer();
93824
93898
  inputData.nonWitnessUtxo = nonWitnessUtxo;
93825
93899
  } else {
93900
+ inputData.nonWitnessUtxo = nonWitnessUtxo;
93826
93901
  inputData.witnessUtxo = tx.outs[input.index];
93827
93902
  }
93828
93903
  Object.keys(inputData).forEach((key) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caravan/psbt",
3
- "version": "1.4.2",
3
+ "version": "1.5.0",
4
4
  "description": "typescript library for working with PSBTs",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",