@leather.io/bitcoin 0.36.6 → 0.37.1

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.js CHANGED
@@ -430,6 +430,9 @@ function isTaprootDerivationPath(path) {
430
430
  function isNativeSegwitDerivationPath(path) {
431
431
  return extractPurposeFromPath(path) === 84;
432
432
  }
433
+ function isP2TROut(signer) {
434
+ return !!signer && isTaprootDerivationPath(signer.derivationPath);
435
+ }
433
436
 
434
437
  //#endregion
435
438
  //#region src/bip322/bip322-utils.ts
@@ -692,6 +695,26 @@ var BtcSizeFeeEstimator = class {
692
695
  txWeight: txVBytes * 4
693
696
  };
694
697
  }
698
+ calcMixedInputTxSize(opts) {
699
+ const { p2wpkh_input_count, p2tr_input_count } = opts;
700
+ const totalInputCount = p2wpkh_input_count + p2tr_input_count;
701
+ const p2pkh_output_count = opts.p2pkh_output_count ?? 0;
702
+ const p2sh_output_count = opts.p2sh_output_count ?? 0;
703
+ const p2sh_p2wpkh_output_count = opts.p2sh_p2wpkh_output_count ?? 0;
704
+ const p2sh_p2wsh_output_count = opts.p2sh_p2wsh_output_count ?? 0;
705
+ const p2wpkh_output_count = opts.p2wpkh_output_count ?? 0;
706
+ const p2wsh_output_count = opts.p2wsh_output_count ?? 0;
707
+ const p2tr_output_count = opts.p2tr_output_count ?? 0;
708
+ const output_count = p2pkh_output_count + p2sh_output_count + p2sh_p2wpkh_output_count + p2sh_p2wsh_output_count + p2wpkh_output_count + p2wsh_output_count + p2tr_output_count;
709
+ const inputVBytes = p2wpkh_input_count * this.P2WPKH_IN_SIZE + p2tr_input_count * this.P2TR_IN_SIZE;
710
+ const inputWitnessBytes = p2wpkh_input_count * 107 + p2tr_input_count * 65;
711
+ const txVBytes = this.getTxOverheadVBytes("p2wpkh", totalInputCount, output_count) + inputVBytes + this.P2PKH_OUT_SIZE * p2pkh_output_count + this.P2SH_OUT_SIZE * p2sh_output_count + this.P2SH_P2WPKH_OUT_SIZE * p2sh_p2wpkh_output_count + this.P2SH_P2WSH_OUT_SIZE * p2sh_p2wsh_output_count + this.P2WPKH_OUT_SIZE * p2wpkh_output_count + this.P2WSH_OUT_SIZE * p2wsh_output_count + this.P2TR_OUT_SIZE * p2tr_output_count;
712
+ return {
713
+ txVBytes,
714
+ txBytes: this.getTxOverheadExtraRawBytes("p2wpkh", totalInputCount) + txVBytes + inputWitnessBytes,
715
+ txWeight: txVBytes * 4
716
+ };
717
+ }
695
718
  estimateFee(vbyte, satVb) {
696
719
  if (isNaN(vbyte) || isNaN(satVb)) throw new Error("Parameters should be numbers");
697
720
  return vbyte * satVb;
@@ -709,8 +732,20 @@ var BtcSizeFeeEstimator = class {
709
732
  function getUtxoTotal(utxos) {
710
733
  return sumNumbers(utxos.map((utxo) => utxo.value));
711
734
  }
735
+ function countInputsByScriptType(utxos) {
736
+ return utxos.reduce((acc, utxo) => {
737
+ const paymentType = inferPaymentTypeFromAddress(createBitcoinAddress(utxo.address));
738
+ return {
739
+ ...acc,
740
+ [paymentType]: acc[paymentType] + 1
741
+ };
742
+ }, {
743
+ p2tr: 0,
744
+ p2wpkh: 0
745
+ });
746
+ }
712
747
  function getSizeInfo(payload) {
713
- const { inputLength, recipients, isSendMax } = payload;
748
+ const { utxos, recipients, isSendMax } = payload;
714
749
  const validAddressesInfo = recipients.map((recipient) => validate$1(recipient.address) && getAddressInfo(recipient.address)).filter(Boolean);
715
750
  function getTxOutputsLengthByPaymentType() {
716
751
  return validAddressesInfo.reduce((acc, { type }) => {
@@ -724,17 +759,19 @@ function getSizeInfo(payload) {
724
759
  acc[type + "_output_count"] = count;
725
760
  return acc;
726
761
  }, {});
727
- return new BtcSizeFeeEstimator().calcTxSize({
728
- input_script: "p2wpkh",
729
- input_count: inputLength,
762
+ const { p2wpkh, p2tr } = countInputsByScriptType(utxos);
763
+ return new BtcSizeFeeEstimator().calcMixedInputTxSize({
764
+ p2wpkh_input_count: p2wpkh,
765
+ p2tr_input_count: p2tr,
730
766
  ...outputsData
731
767
  });
732
768
  }
733
- function getSpendableAmount({ utxos, feeRate, recipients }) {
769
+ function getSpendableAmount({ utxos, feeRate, recipients, isSendMax }) {
734
770
  const balance = utxos.map((utxo) => utxo.value).reduce((prevVal, curVal) => prevVal + curVal, 0);
735
771
  const size = getSizeInfo({
736
- inputLength: utxos.length,
737
- recipients
772
+ utxos,
773
+ recipients,
774
+ isSendMax
738
775
  });
739
776
  const fee = Math.ceil(size.txVBytes * feeRate);
740
777
  const bigNumberBalance = BigNumber(balance);
@@ -803,7 +840,7 @@ function determineUtxosForSpendAll({ feeRate, recipients, utxos }) {
803
840
  });
804
841
  if (!filteredUtxos.length) throw new BitcoinError("InsufficientFunds");
805
842
  const sizeInfo = getSizeInfo({
806
- inputLength: filteredUtxos.length,
843
+ utxos: filteredUtxos,
807
844
  isSendMax: true,
808
845
  recipients
809
846
  });
@@ -833,7 +870,7 @@ function determineUtxosForSpend({ feeRate, recipients, utxos }) {
833
870
  const neededUtxos = [filteredUtxos[0]];
834
871
  function estimateTransactionSize() {
835
872
  return getSizeInfo({
836
- inputLength: neededUtxos.length,
873
+ utxos: neededUtxos,
837
874
  recipients
838
875
  });
839
876
  }
@@ -1443,5 +1480,5 @@ function deconstructBtcAddress(address) {
1443
1480
  }
1444
1481
 
1445
1482
  //#endregion
1446
- export { BitcoinError, TEST_ACCOUNT_1_NATIVE_SEGWIT_ADDRESS, TEST_ACCOUNT_1_TAPROOT_ADDRESS, TEST_ACCOUNT_2_TAPROOT_ADDRESS, TEST_TESNET_ACCOUNT_1_NATIVE_SEGWIT_ADDRESS, TEST_TESTNET_ACCOUNT_2_BTC_ADDRESS, TEST_TESTNET_ACCOUNT_2_TAPROOT_ADDRESS, bip21, bip322TransactionToSignValues, bitcoinNetworkModeToCoreNetworkMode, bitcoinNetworkToCoreNetworkMap, btcAddressNetworkValidator, btcAddressValidator, btcSignerLibPaymentTypeToPaymentTypeMap, calculateMaxSpend, coinTypeMap, createBitcoinAddress, createNativeSegwitBitcoinJsSigner, createTaprootBitcoinJsSigner, createToSpendTx, createWalletIdDecoratedPath, decodeBitcoinTx, decodeCompressedWifPrivateKey, deconstructBtcAddress, deriveAddressIndexKeychainFromAccount, deriveAddressIndexZeroFromAccount, deriveAddressesFromDescriptor, deriveBitcoinPayerFromAccount, deriveBtcBip49SeedFromMnemonic, deriveNativeSegwitAccountFromRootKeychain, deriveNativeSegwitReceiveAddressIndexZero, deriveRootBtcKeychain, deriveTaprootAccount, deriveTaprootReceiveAddressIndexZero, determineUtxosForSpend, determineUtxosForSpendAll, ecPairFromPrivateKey, ecdsaPublicKeyLength, ecdsaPublicKeyToSchnorr, encodeMessageWitnessData, extractExtendedPublicKeyFromPolicy, extractPayerInfoFromDerivationPath, extractRequiredKeyOrigins, extractXpubFromDescriptor, filterUneconomicalUtxos, generateBitcoinUnsignedTransaction, getAddressFromOutScript, getBitcoinAddressNetworkType, getBitcoinCoinTypeIndexByNetwork, getBitcoinFees, getBitcoinInputAddress, getBitcoinInputValue, getBitcoinJsLibNetworkConfigByMode, getBitcoinTransactionFee, getBtcSignerLibNetworkConfigByMode, getDescriptorFromKeychain, getHdKeyVersionsFromNetwork, getInputPaymentType, getNativeSegwitAccountDerivationPath, getNativeSegwitAddress, getNativeSegwitAddressIndexDerivationPath, getNativeSegwitPaymentFromAddressIndex, getNetworkTypeFromAddress, getParsedInputs, getParsedOutputs, getPsbtAsTransaction, getPsbtDetails, getPsbtTotals, getPsbtTxInputs, getPsbtTxOutputs, getRawPsbt, getSizeInfo, getSpendableAmount, getTaprootAccountDerivationPath, getTaprootAddress, getTaprootAddressIndexDerivationPath, getTaprootPayment, getTaprootPaymentFromAddressIndex, getUtxoTotal, hashBip322Message, inValidCharactersAddress, inValidLengthAddress, inferNetworkFromAddress, inferNetworkFromPath, inferPaymentTypeFromAddress, inferPaymentTypeFromDescriptor, inferPaymentTypeFromPath, initBitcoinAccount, initializeBitcoinAccountKeychainFromDescriptor, invalidAddress, isBitcoinAddress, isBtcBalanceSufficient, isBtcMinimumSpend, isBtcSignerLibPaymentType, isNativeSegwitDerivationPath, isSupportedMessageSigningPaymentType, isTaprootDerivationPath, isValidBitcoinAddress, isValidBitcoinNetworkAddress, legacyAddress, lookUpLedgerKeysByPath, lookupDerivationByAddress, makeNativeSegwitAccountDerivationPath, makeNativeSegwitAddressIndexDerivationPath, makePayToScriptHashAddress, makePayToScriptHashAddressBytes, makePayToScriptHashKeyHash, makeTaprootAccountDerivationPath, makeTaprootAddressIndexDerivationPath, mnemonicToRootNode, nonEmptyStringValidator, parseKnownPaymentType, payToScriptHashTestnetPrefix, payerToBip32Derivation, payerToBip32DerivationBitcoinJsLib, payerToTapBip32Derivation, payerToTapBip32DerivationBitcoinJsLib, paymentTypeMap, publicKeyToPayToScriptHashAddress, recipientAddress, segwitAddress, serializeKeyOrigin, signBip322MessageSimple, taprootAddress, toXOnly, tweakSigner, whenBitcoinNetwork, whenPaymentType, whenSupportedPaymentType };
1483
+ export { BitcoinError, BtcSizeFeeEstimator, TEST_ACCOUNT_1_NATIVE_SEGWIT_ADDRESS, TEST_ACCOUNT_1_TAPROOT_ADDRESS, TEST_ACCOUNT_2_TAPROOT_ADDRESS, TEST_TESNET_ACCOUNT_1_NATIVE_SEGWIT_ADDRESS, TEST_TESTNET_ACCOUNT_2_BTC_ADDRESS, TEST_TESTNET_ACCOUNT_2_TAPROOT_ADDRESS, bip21, bip322TransactionToSignValues, bitcoinNetworkModeToCoreNetworkMode, bitcoinNetworkToCoreNetworkMap, btcAddressNetworkValidator, btcAddressValidator, btcSignerLibPaymentTypeToPaymentTypeMap, calculateMaxSpend, coinTypeMap, countInputsByScriptType, createBitcoinAddress, createNativeSegwitBitcoinJsSigner, createTaprootBitcoinJsSigner, createToSpendTx, createWalletIdDecoratedPath, decodeBitcoinTx, decodeCompressedWifPrivateKey, deconstructBtcAddress, deriveAddressIndexKeychainFromAccount, deriveAddressIndexZeroFromAccount, deriveAddressesFromDescriptor, deriveBitcoinPayerFromAccount, deriveBtcBip49SeedFromMnemonic, deriveNativeSegwitAccountFromRootKeychain, deriveNativeSegwitReceiveAddressIndexZero, deriveRootBtcKeychain, deriveTaprootAccount, deriveTaprootReceiveAddressIndexZero, determineUtxosForSpend, determineUtxosForSpendAll, ecPairFromPrivateKey, ecdsaPublicKeyLength, ecdsaPublicKeyToSchnorr, encodeMessageWitnessData, extractExtendedPublicKeyFromPolicy, extractPayerInfoFromDerivationPath, extractRequiredKeyOrigins, extractXpubFromDescriptor, filterUneconomicalUtxos, generateBitcoinUnsignedTransaction, getAddressFromOutScript, getBitcoinAddressNetworkType, getBitcoinCoinTypeIndexByNetwork, getBitcoinFees, getBitcoinInputAddress, getBitcoinInputValue, getBitcoinJsLibNetworkConfigByMode, getBitcoinTransactionFee, getBtcSignerLibNetworkConfigByMode, getDescriptorFromKeychain, getHdKeyVersionsFromNetwork, getInputPaymentType, getNativeSegwitAccountDerivationPath, getNativeSegwitAddress, getNativeSegwitAddressIndexDerivationPath, getNativeSegwitPaymentFromAddressIndex, getNetworkTypeFromAddress, getParsedInputs, getParsedOutputs, getPsbtAsTransaction, getPsbtDetails, getPsbtTotals, getPsbtTxInputs, getPsbtTxOutputs, getRawPsbt, getSizeInfo, getSpendableAmount, getTaprootAccountDerivationPath, getTaprootAddress, getTaprootAddressIndexDerivationPath, getTaprootPayment, getTaprootPaymentFromAddressIndex, getUtxoTotal, hashBip322Message, inValidCharactersAddress, inValidLengthAddress, inferNetworkFromAddress, inferNetworkFromPath, inferPaymentTypeFromAddress, inferPaymentTypeFromDescriptor, inferPaymentTypeFromPath, initBitcoinAccount, initializeBitcoinAccountKeychainFromDescriptor, invalidAddress, isBitcoinAddress, isBtcBalanceSufficient, isBtcMinimumSpend, isBtcSignerLibPaymentType, isNativeSegwitDerivationPath, isP2TROut, isSupportedMessageSigningPaymentType, isTaprootDerivationPath, isValidBitcoinAddress, isValidBitcoinNetworkAddress, legacyAddress, lookUpLedgerKeysByPath, lookupDerivationByAddress, makeNativeSegwitAccountDerivationPath, makeNativeSegwitAddressIndexDerivationPath, makePayToScriptHashAddress, makePayToScriptHashAddressBytes, makePayToScriptHashKeyHash, makeTaprootAccountDerivationPath, makeTaprootAddressIndexDerivationPath, mnemonicToRootNode, nonEmptyStringValidator, parseKnownPaymentType, payToScriptHashTestnetPrefix, payerToBip32Derivation, payerToBip32DerivationBitcoinJsLib, payerToTapBip32Derivation, payerToTapBip32DerivationBitcoinJsLib, paymentTypeMap, publicKeyToPayToScriptHashAddress, recipientAddress, segwitAddress, serializeKeyOrigin, signBip322MessageSimple, taprootAddress, toXOnly, tweakSigner, whenBitcoinNetwork, whenPaymentType, whenSupportedPaymentType };
1447
1484
  //# sourceMappingURL=index.js.map