@leather.io/bitcoin 0.26.8 → 0.26.9

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,5 +1,5 @@
1
1
 
2
- > @leather.io/bitcoin@0.26.8 build /home/runner/work/mono/mono/packages/bitcoin
2
+ > @leather.io/bitcoin@0.26.9 build /home/runner/work/mono/mono/packages/bitcoin
3
3
  > tsup
4
4
 
5
5
  CLI Building entry: src/index.ts
@@ -8,9 +8,9 @@
8
8
  CLI Using tsup config: /home/runner/work/mono/mono/packages/bitcoin/tsup.config.ts
9
9
  CLI Target: es2022
10
10
  ESM Build start
11
- ESM dist/index.js 55.48 KB
12
- ESM dist/index.js.map 109.87 KB
13
- ESM ⚡️ Build success in 107ms
11
+ ESM dist/index.js 56.01 KB
12
+ ESM dist/index.js.map 110.95 KB
13
+ ESM ⚡️ Build success in 94ms
14
14
  DTS Build start
15
- DTS ⚡️ Build success in 5807ms
16
- DTS dist/index.d.ts 27.55 KB
15
+ DTS ⚡️ Build success in 6050ms
16
+ DTS dist/index.d.ts 27.83 KB
package/CHANGELOG.md CHANGED
@@ -776,6 +776,20 @@
776
776
  * devDependencies
777
777
  * @leather.io/rpc bumped to 2.16.5
778
778
 
779
+ ## [0.26.9](https://github.com/leather-io/mono/compare/@leather.io/bitcoin-v0.26.8...@leather.io/bitcoin-v0.26.9) (2025-06-04)
780
+
781
+
782
+ ### Bug Fixes
783
+
784
+ * **mobile:** disable external psbt fee editing, closes LEA-2679 ([eaf6a15](https://github.com/leather-io/mono/commit/eaf6a15669167244e5aab8a7d748266e6f4b72aa))
785
+
786
+
787
+ ### Dependencies
788
+
789
+ * The following workspace dependencies were updated
790
+ * dependencies
791
+ * @leather.io/crypto bumped to 1.7.6
792
+
779
793
  ## [0.26.0](https://github.com/leather-io/mono/compare/@leather.io/bitcoin-v0.25.2...@leather.io/bitcoin-v0.26.0) (2025-05-16)
780
794
 
781
795
 
package/dist/index.d.ts CHANGED
@@ -64,27 +64,40 @@ declare function signBip322MessageSimple(args: SignBip322MessageSimple): Promise
64
64
  signature: string;
65
65
  }>;
66
66
 
67
+ interface CalculateMaxSpendArgs {
68
+ recipient: string;
69
+ utxos: {
70
+ value: number;
71
+ txid: string;
72
+ }[];
73
+ feeRates?: AverageBitcoinFeeRates;
74
+ feeRate?: number;
75
+ }
76
+ interface CalculateMaxSpendResponse {
77
+ spendAllFee: number;
78
+ amount: Money;
79
+ spendableBitcoin: BigNumber;
80
+ }
81
+ declare function calculateMaxSpend({ recipient, utxos, feeRate, feeRates, }: CalculateMaxSpendArgs): CalculateMaxSpendResponse;
82
+
67
83
  interface CoinSelectionOutput {
68
84
  value: bigint;
69
85
  address?: string;
70
86
  }
71
- interface CoinSelectionUtxo {
72
- address: string;
73
- txid: string;
74
- value: number;
75
- vout: number;
76
- }
77
87
  interface CoinSelectionRecipient {
78
88
  address: string;
79
89
  amount: Money;
80
90
  }
81
- interface DetermineUtxosForSpendArgs {
91
+ interface DetermineUtxosForSpendArgs<T> {
82
92
  feeRate: number;
83
93
  recipients: CoinSelectionRecipient[];
84
- utxos: CoinSelectionUtxo[];
94
+ utxos: T[];
85
95
  }
86
- declare function determineUtxosForSpendAll({ feeRate, recipients, utxos, }: DetermineUtxosForSpendArgs): {
87
- inputs: CoinSelectionUtxo[];
96
+ declare function determineUtxosForSpendAll<T extends {
97
+ value: number;
98
+ txid: string;
99
+ }>({ feeRate, recipients, utxos, }: DetermineUtxosForSpendArgs<T>): {
100
+ inputs: T[];
88
101
  outputs: {
89
102
  value: bigint;
90
103
  address: string;
@@ -92,31 +105,23 @@ declare function determineUtxosForSpendAll({ feeRate, recipients, utxos, }: Dete
92
105
  size: number;
93
106
  fee: Money;
94
107
  };
95
- declare function determineUtxosForSpend({ feeRate, recipients, utxos }: DetermineUtxosForSpendArgs): {
108
+ declare function determineUtxosForSpend<T extends {
109
+ value: number;
110
+ txid: string;
111
+ }>({ feeRate, recipients, utxos, }: DetermineUtxosForSpendArgs<T>): {
96
112
  txVBytes: number;
97
113
  txBytes: number;
98
114
  txWeight: number;
99
- filteredUtxos: CoinSelectionUtxo[];
100
- inputs: CoinSelectionUtxo[];
115
+ filteredUtxos: T[];
116
+ inputs: T[];
101
117
  outputs: CoinSelectionOutput[];
102
118
  size: number;
103
119
  fee: Money;
104
120
  };
105
121
 
106
- interface CalculateMaxSpendArgs {
107
- recipient: string;
108
- utxos: CoinSelectionUtxo[];
109
- feeRates?: AverageBitcoinFeeRates;
110
- feeRate?: number;
111
- }
112
- interface CalculateMaxSpendResponse {
113
- spendAllFee: number;
114
- amount: Money;
115
- spendableBitcoin: BigNumber;
116
- }
117
- declare function calculateMaxSpend({ recipient, utxos, feeRate, feeRates, }: CalculateMaxSpendArgs): CalculateMaxSpendResponse;
118
-
119
- declare function getUtxoTotal(utxos: CoinSelectionUtxo[]): BigNumber;
122
+ declare function getUtxoTotal<T extends {
123
+ value: number;
124
+ }>(utxos: T[]): BigNumber;
120
125
  declare function getSizeInfo(payload: {
121
126
  inputLength: number;
122
127
  recipients: CoinSelectionRecipient[];
@@ -126,23 +131,31 @@ declare function getSizeInfo(payload: {
126
131
  txBytes: number;
127
132
  txWeight: number;
128
133
  };
129
- interface GetSpendableAmountArgs {
130
- utxos: CoinSelectionUtxo[];
134
+ interface GetSpendableAmountArgs<T> {
135
+ utxos: T[];
131
136
  feeRate: number;
132
137
  recipients: CoinSelectionRecipient[];
133
138
  isSendMax?: boolean;
134
139
  }
135
- declare function getSpendableAmount({ utxos, feeRate, recipients }: GetSpendableAmountArgs): {
140
+ declare function getSpendableAmount<T extends {
141
+ value: number;
142
+ }>({ utxos, feeRate, recipients, }: GetSpendableAmountArgs<T>): {
136
143
  spendableAmount: BigNumber;
137
144
  fee: number;
138
145
  };
139
- declare function filterUneconomicalUtxos({ utxos, feeRate, recipients, }: {
140
- utxos: CoinSelectionUtxo[];
146
+ declare function filterUneconomicalUtxos<T extends {
147
+ value: number;
148
+ txid: string;
149
+ }>({ utxos, feeRate, recipients, }: {
150
+ utxos: T[];
141
151
  feeRate: number;
142
152
  recipients: CoinSelectionRecipient[];
143
- }): CoinSelectionUtxo[];
153
+ }): T[];
144
154
 
145
- type GetBitcoinTransactionFeeArgs = DetermineUtxosForSpendArgs & {
155
+ type GetBitcoinTransactionFeeArgs = DetermineUtxosForSpendArgs<{
156
+ value: number;
157
+ txid: string;
158
+ }> & {
146
159
  isSendingMax?: boolean;
147
160
  };
148
161
  declare function getBitcoinTransactionFee({ isSendingMax, ...props }: GetBitcoinTransactionFeeArgs): Money | null;
@@ -165,7 +178,10 @@ interface GetBitcoinFeesArgs {
165
178
  feeRates: AverageBitcoinFeeRates;
166
179
  isSendingMax?: boolean;
167
180
  recipients: CoinSelectionRecipient[];
168
- utxos: CoinSelectionUtxo[];
181
+ utxos: {
182
+ value: number;
183
+ txid: string;
184
+ }[];
169
185
  }
170
186
  declare function getBitcoinFees({ feeRates, isSendingMax, recipients, utxos }: GetBitcoinFeesArgs): {
171
187
  high: {
@@ -447,10 +463,10 @@ interface BitcoinTaprootPayer extends BitcoinPayerBase {
447
463
  type BitcoinPayer = BitcoinNativeSegwitPayer | BitcoinTaprootPayer;
448
464
  declare function initializeBitcoinAccountKeychainFromDescriptor(descriptor: string): BitcoinAccountKeychain;
449
465
  interface BitcoinPayerInfo {
450
- receive?: number;
466
+ change: number;
451
467
  addressIndex: number;
452
468
  }
453
- declare function deriveBitcoinPayerFromAccount(descriptor: string, network: BitcoinNetworkModes): ({ receive, addressIndex }: BitcoinPayerInfo) => {
469
+ declare function deriveBitcoinPayerFromAccount(descriptor: string, network: BitcoinNetworkModes): ({ change, addressIndex }: BitcoinPayerInfo) => {
454
470
  keyOrigin: string;
455
471
  masterKeyFingerprint: string;
456
472
  paymentType: SupportedPaymentType;
@@ -498,6 +514,10 @@ type TapBip32DerivationBitcoinJsLib = NonNullable<PsbtInputBitcoinJsLib['tapBip3
498
514
  declare function payerToTapBip32DerivationBitcoinJsLib(args: PayerToBip32DerivationArgs): TapBip32DerivationBitcoinJsLib;
499
515
  type Bip32DerivationBitcoinJsLib = NonNullable<PsbtInputBitcoinJsLib['bip32Derivation']>['0'];
500
516
  declare function payerToBip32DerivationBitcoinJsLib(args: PayerToBip32DerivationArgs): Bip32DerivationBitcoinJsLib;
517
+ declare function extractPayerInfoFromDerivationPath(path: string): {
518
+ change: number;
519
+ addressIndex: number;
520
+ };
501
521
  /**
502
522
  * @description
503
523
  * Turns key format from @scure/btc-signer lib back into key origin string
@@ -516,21 +536,25 @@ declare function serializeKeyOrigin({ fingerprint, path }: BtcSignerDerivationPa
516
536
  */
517
537
  declare function extractRequiredKeyOrigins(derivation: BtcSignerBip32Derivation[]): string[];
518
538
 
519
- interface GenerateBitcoinUnsignedTransactionArgs {
539
+ interface GenerateBitcoinUnsignedTransactionArgs<T> {
520
540
  feeRate: number;
521
541
  isSendingMax?: boolean;
522
- payerAddress: string;
523
- payerPublicKey: string;
524
- bip32Derivation: BtcSignerDefaultBip32Derivation[];
525
542
  network: BtcSignerNetwork;
526
543
  recipients: CoinSelectionRecipient[];
527
- utxos: CoinSelectionUtxo[];
544
+ utxos: T[];
545
+ changeAddress: string;
546
+ payerLookup(keyOrigin: string): BitcoinNativeSegwitPayer | BitcoinTaprootPayer | undefined;
528
547
  }
529
- declare function generateBitcoinUnsignedTransactionNativeSegwit({ feeRate, isSendingMax, payerAddress, payerPublicKey, bip32Derivation, network, recipients, utxos, }: GenerateBitcoinUnsignedTransactionArgs): {
548
+ declare function generateBitcoinUnsignedTransaction<T extends {
549
+ txid: string;
550
+ vout: number;
551
+ value: number;
552
+ keyOrigin: string;
553
+ }>({ feeRate, isSendingMax, network, recipients, changeAddress, utxos, payerLookup, }: GenerateBitcoinUnsignedTransactionArgs<T>): {
530
554
  tx: btc.Transaction;
531
555
  hex: string;
532
556
  psbt: Uint8Array<ArrayBufferLike>;
533
- inputs: CoinSelectionUtxo[];
557
+ inputs: T[];
534
558
  fee: _leather_io_models.Money;
535
559
  };
536
560
 
@@ -584,4 +608,4 @@ declare function lookupDerivationByAddress(args: LookUpDerivationByAddressArgs):
584
608
  readonly path?: undefined;
585
609
  };
586
610
 
587
- export { type AllowedSighashTypes, type BitcoinAccount, type BitcoinAccountKeychain, BitcoinError, type BitcoinErrorKey, type BitcoinFees, type BitcoinNativeSegwitPayer, type BitcoinPayer, type BitcoinPayerBase, type BitcoinPayerInfo, type BitcoinSigner, type BitcoinTaprootPayer, type BtcSignerDefaultBip32Derivation, type BtcSignerLibPaymentTypeIdentifers, type BtcSignerNetwork, type BtcSignerTapBip32Derivation, type CalculateMaxSpendResponse, type CoinSelectionOutput, type CoinSelectionRecipient, type CoinSelectionUtxo, type DetermineUtxosForSpendArgs, type GenerateBitcoinUnsignedTransactionArgs, type GetBitcoinFeesArgs, type PaymentTypeMap, type PsbtInput, type PsbtOutput, type RawPsbt, type SupportedPaymentType, type SupportedPaymentTypeMap, 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, type WithDerivePayer, bip21, bip322TransactionToSignValues, bitcoinNetworkModeToCoreNetworkMode, bitcoinNetworkToCoreNetworkMap, btcSignerLibPaymentTypeToPaymentTypeMap, calculateMaxSpend, coinTypeMap, createBitcoinAddress, createNativeSegwitBitcoinJsSigner, createTaprootBitcoinJsSigner, createToSpendTx, createWalletIdDecoratedPath, decodeBitcoinTx, decodeCompressedWifPrivateKey, deriveAddressIndexKeychainFromAccount, deriveAddressIndexZeroFromAccount, deriveBitcoinPayerFromAccount, deriveBtcBip49SeedFromMnemonic, deriveNativeSegwitAccountFromRootKeychain, deriveNativeSegwitReceiveAddressIndexZero, deriveRootBtcKeychain, deriveTaprootAccount, deriveTaprootReceiveAddressIndexZero, determineUtxosForSpend, determineUtxosForSpendAll, ecPairFromPrivateKey, ecdsaPublicKeyLength, ecdsaPublicKeyToSchnorr, encodeMessageWitnessData, extractExtendedPublicKeyFromPolicy, extractRequiredKeyOrigins, filterUneconomicalUtxos, generateBitcoinUnsignedTransactionNativeSegwit, getAddressFromOutScript, getBitcoinAddressNetworkType, getBitcoinCoinTypeIndexByNetwork, getBitcoinFees, getBitcoinInputAddress, getBitcoinInputValue, getBitcoinJsLibNetworkConfigByMode, getBitcoinTransactionFee, getBtcSignerLibNetworkConfigByMode, getDescriptorFromKeychain, getHdKeyVersionsFromNetwork, getInputPaymentType, getNativeSegwitAccountDerivationPath, getNativeSegwitAddress, getNativeSegwitAddressIndexDerivationPath, getNativeSegwitPaymentFromAddressIndex, getParsedInputs, getParsedOutputs, getPsbtAsTransaction, getPsbtDetails, getPsbtTotals, getPsbtTxInputs, getPsbtTxOutputs, getRawPsbt, getSizeInfo, getSpendableAmount, getTaprootAccountDerivationPath, getTaprootAddress, getTaprootAddressIndexDerivationPath, getTaprootPayment, getTaprootPaymentFromAddressIndex, getUtxoTotal, hashBip322Message, inValidCharactersAddress, inValidLengthAddress, inferNetworkFromAddress, inferNetworkFromPath, inferPaymentTypeFromAddress, inferPaymentTypeFromPath, initBitcoinAccount, initializeBitcoinAccountKeychainFromDescriptor, invalidAddress, isBitcoinAddress, isBtcBalanceSufficient, isBtcMinimumSpend, isBtcSignerLibPaymentType, isSupportedMessageSigningPaymentType, isValidBitcoinAddress, isValidBitcoinNetworkAddress, isValidBitcoinTransaction, legacyAddress, lookUpLedgerKeysByPath, lookupDerivationByAddress, makeNativeSegwitAccountDerivationPath, makeNativeSegwitAddressIndexDerivationPath, makePayToScriptHashAddress, makePayToScriptHashAddressBytes, makePayToScriptHashKeyHash, makeTaprootAccountDerivationPath, makeTaprootAddressIndexDerivationPath, minSpendAmountInSats, mnemonicToRootNode, parseKnownPaymentType, payToScriptHashTestnetPrefix, payerToBip32Derivation, payerToBip32DerivationBitcoinJsLib, payerToTapBip32Derivation, payerToTapBip32DerivationBitcoinJsLib, paymentTypeMap, publicKeyToPayToScriptHashAddress, recipientAddress, segwitAddress, serializeKeyOrigin, serializeSatPoint, signBip322MessageSimple, taprootAddress, toXOnly, tweakSigner, whenBitcoinNetwork, whenPaymentType, whenSupportedPaymentType };
611
+ export { type AllowedSighashTypes, type BitcoinAccount, type BitcoinAccountKeychain, BitcoinError, type BitcoinErrorKey, type BitcoinFees, type BitcoinNativeSegwitPayer, type BitcoinPayer, type BitcoinPayerBase, type BitcoinPayerInfo, type BitcoinSigner, type BitcoinTaprootPayer, type BtcSignerDefaultBip32Derivation, type BtcSignerLibPaymentTypeIdentifers, type BtcSignerNetwork, type BtcSignerTapBip32Derivation, type CalculateMaxSpendResponse, type CoinSelectionOutput, type CoinSelectionRecipient, type DetermineUtxosForSpendArgs, type GenerateBitcoinUnsignedTransactionArgs, type GetBitcoinFeesArgs, type PaymentTypeMap, type PsbtInput, type PsbtOutput, type RawPsbt, type SupportedPaymentType, type SupportedPaymentTypeMap, 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, type WithDerivePayer, bip21, bip322TransactionToSignValues, bitcoinNetworkModeToCoreNetworkMode, bitcoinNetworkToCoreNetworkMap, btcSignerLibPaymentTypeToPaymentTypeMap, calculateMaxSpend, coinTypeMap, createBitcoinAddress, createNativeSegwitBitcoinJsSigner, createTaprootBitcoinJsSigner, createToSpendTx, createWalletIdDecoratedPath, decodeBitcoinTx, decodeCompressedWifPrivateKey, deriveAddressIndexKeychainFromAccount, deriveAddressIndexZeroFromAccount, deriveBitcoinPayerFromAccount, deriveBtcBip49SeedFromMnemonic, deriveNativeSegwitAccountFromRootKeychain, deriveNativeSegwitReceiveAddressIndexZero, deriveRootBtcKeychain, deriveTaprootAccount, deriveTaprootReceiveAddressIndexZero, determineUtxosForSpend, determineUtxosForSpendAll, ecPairFromPrivateKey, ecdsaPublicKeyLength, ecdsaPublicKeyToSchnorr, encodeMessageWitnessData, extractExtendedPublicKeyFromPolicy, extractPayerInfoFromDerivationPath, extractRequiredKeyOrigins, filterUneconomicalUtxos, generateBitcoinUnsignedTransaction, getAddressFromOutScript, getBitcoinAddressNetworkType, getBitcoinCoinTypeIndexByNetwork, getBitcoinFees, getBitcoinInputAddress, getBitcoinInputValue, getBitcoinJsLibNetworkConfigByMode, getBitcoinTransactionFee, getBtcSignerLibNetworkConfigByMode, getDescriptorFromKeychain, getHdKeyVersionsFromNetwork, getInputPaymentType, getNativeSegwitAccountDerivationPath, getNativeSegwitAddress, getNativeSegwitAddressIndexDerivationPath, getNativeSegwitPaymentFromAddressIndex, getParsedInputs, getParsedOutputs, getPsbtAsTransaction, getPsbtDetails, getPsbtTotals, getPsbtTxInputs, getPsbtTxOutputs, getRawPsbt, getSizeInfo, getSpendableAmount, getTaprootAccountDerivationPath, getTaprootAddress, getTaprootAddressIndexDerivationPath, getTaprootPayment, getTaprootPaymentFromAddressIndex, getUtxoTotal, hashBip322Message, inValidCharactersAddress, inValidLengthAddress, inferNetworkFromAddress, inferNetworkFromPath, inferPaymentTypeFromAddress, inferPaymentTypeFromPath, initBitcoinAccount, initializeBitcoinAccountKeychainFromDescriptor, invalidAddress, isBitcoinAddress, isBtcBalanceSufficient, isBtcMinimumSpend, isBtcSignerLibPaymentType, isSupportedMessageSigningPaymentType, isValidBitcoinAddress, isValidBitcoinNetworkAddress, isValidBitcoinTransaction, legacyAddress, lookUpLedgerKeysByPath, lookupDerivationByAddress, makeNativeSegwitAccountDerivationPath, makeNativeSegwitAddressIndexDerivationPath, makePayToScriptHashAddress, makePayToScriptHashAddressBytes, makePayToScriptHashKeyHash, makeTaprootAccountDerivationPath, makeTaprootAddressIndexDerivationPath, minSpendAmountInSats, mnemonicToRootNode, parseKnownPaymentType, payToScriptHashTestnetPrefix, payerToBip32Derivation, payerToBip32DerivationBitcoinJsLib, payerToTapBip32Derivation, payerToTapBip32DerivationBitcoinJsLib, paymentTypeMap, publicKeyToPayToScriptHashAddress, recipientAddress, segwitAddress, serializeKeyOrigin, serializeSatPoint, signBip322MessageSimple, taprootAddress, toXOnly, tweakSigner, whenBitcoinNetwork, whenPaymentType, whenSupportedPaymentType };
package/dist/index.js CHANGED
@@ -837,8 +837,12 @@ function getSizeInfo(payload) {
837
837
  });
838
838
  return sizeInfo;
839
839
  }
840
- function getSpendableAmount({ utxos, feeRate, recipients }) {
841
- const balance = utxos.map((utxo) => Number(utxo.value)).reduce((prevVal, curVal) => prevVal + curVal, 0);
840
+ function getSpendableAmount({
841
+ utxos,
842
+ feeRate,
843
+ recipients
844
+ }) {
845
+ const balance = utxos.map((utxo) => utxo.value).reduce((prevVal, curVal) => prevVal + curVal, 0);
842
846
  const size = getSizeInfo({
843
847
  inputLength: utxos.length,
844
848
  recipients
@@ -860,7 +864,7 @@ function filterUneconomicalUtxos({
860
864
  feeRate,
861
865
  recipients
862
866
  });
863
- const filteredUtxos = utxos.filter((utxo) => utxo.value >= BTC_P2WPKH_DUST_AMOUNT).filter((utxo) => {
867
+ const filteredUtxos = utxos.filter((utxo) => Number(utxo.value) >= BTC_P2WPKH_DUST_AMOUNT).filter((utxo) => {
864
868
  const { spendableAmount } = getSpendableAmount({
865
869
  utxos: utxos.filter((u) => u.txid !== utxo.txid),
866
870
  feeRate,
@@ -934,7 +938,11 @@ function determineUtxosForSpendAll({
934
938
  fee: createMoney2(new BigNumber4(fee), "BTC")
935
939
  };
936
940
  }
937
- function determineUtxosForSpend({ feeRate, recipients, utxos }) {
941
+ function determineUtxosForSpend({
942
+ feeRate,
943
+ recipients,
944
+ utxos
945
+ }) {
938
946
  recipients.forEach((recipient) => {
939
947
  if (!validate3(recipient.address)) throw new BitcoinError("InvalidAddress");
940
948
  });
@@ -1278,6 +1286,8 @@ import {
1278
1286
  appendAddressIndexToPath,
1279
1287
  decomposeDescriptor,
1280
1288
  deriveKeychainFromXpub,
1289
+ extractAddressIndexFromPath,
1290
+ extractChangeIndexFromPath,
1281
1291
  keyOriginToDerivationPath
1282
1292
  } from "@leather.io/crypto";
1283
1293
  import { hexToNumber, toHexString } from "@leather.io/utils";
@@ -1297,15 +1307,15 @@ function deriveBitcoinPayerFromAccount(descriptor, network) {
1297
1307
  const paymentType = inferPaymentTypeFromPath(keyOrigin);
1298
1308
  if (accountKeychain.depth !== DerivationPathDepth4.Account)
1299
1309
  throw new Error("Keychain passed is not an account");
1300
- return ({ receive = 0, addressIndex }) => {
1301
- const childKeychain = accountKeychain.deriveChild(receive).deriveChild(addressIndex);
1310
+ return ({ change, addressIndex }) => {
1311
+ const childKeychain = accountKeychain.deriveChild(change).deriveChild(addressIndex);
1302
1312
  const derivePayerFromAccount = whenSupportedPaymentType(paymentType)({
1303
1313
  p2tr: getTaprootPaymentFromAddressIndex,
1304
1314
  p2wpkh: getNativeSegwitPaymentFromAddressIndex
1305
1315
  });
1306
1316
  const payment = derivePayerFromAccount(childKeychain, network);
1307
1317
  return {
1308
- keyOrigin: appendAddressIndexToPath(keyOrigin, 0),
1318
+ keyOrigin: appendAddressIndexToPath(keyOrigin, change, addressIndex),
1309
1319
  masterKeyFingerprint: fingerprint,
1310
1320
  paymentType,
1311
1321
  network,
@@ -1359,6 +1369,12 @@ function payerToBip32DerivationBitcoinJsLib(args) {
1359
1369
  pubkey: Buffer.from(args.publicKey)
1360
1370
  };
1361
1371
  }
1372
+ function extractPayerInfoFromDerivationPath(path) {
1373
+ return {
1374
+ change: extractChangeIndexFromPath(path),
1375
+ addressIndex: extractAddressIndexFromPath(path)
1376
+ };
1377
+ }
1362
1378
  function serializeKeyOrigin({ fingerprint, path }) {
1363
1379
  const values = path.map((num) => num >= HARDENED_OFFSET ? num - HARDENED_OFFSET + "'" : num);
1364
1380
  return `${toHexString(fingerprint)}/${values.join("/")}`;
@@ -1370,40 +1386,43 @@ function extractRequiredKeyOrigins(derivation) {
1370
1386
  }
1371
1387
 
1372
1388
  // src/transactions/generate-unsigned-transaction.ts
1373
- import { hexToBytes as hexToBytes4 } from "@noble/hashes/utils";
1374
1389
  import * as btc6 from "@scure/btc-signer";
1375
- function generateBitcoinUnsignedTransactionNativeSegwit({
1390
+ function generateBitcoinUnsignedTransaction({
1376
1391
  feeRate,
1377
1392
  isSendingMax,
1378
- payerAddress,
1379
- payerPublicKey,
1380
- bip32Derivation,
1381
1393
  network,
1382
1394
  recipients,
1383
- utxos
1395
+ changeAddress,
1396
+ utxos,
1397
+ payerLookup
1384
1398
  }) {
1385
1399
  const determineUtxosArgs = { feeRate, recipients, utxos };
1386
1400
  const { inputs, outputs, fee } = isSendingMax ? determineUtxosForSpendAll(determineUtxosArgs) : determineUtxosForSpend(determineUtxosArgs);
1387
1401
  if (!inputs.length) throw new BitcoinError("NoInputsToSign");
1388
1402
  if (!outputs.length) throw new BitcoinError("NoOutputsToSign");
1389
1403
  const tx = new btc6.Transaction();
1390
- const p2wpkh3 = btc6.p2wpkh(hexToBytes4(payerPublicKey), network);
1391
1404
  for (const input of inputs) {
1405
+ const payer = payerLookup(input.keyOrigin);
1406
+ if (!payer) {
1407
+ console.log(`No payer found for input with keyOrigin ${input.keyOrigin}`);
1408
+ continue;
1409
+ }
1410
+ const bip32Derivation = payer.paymentType === "p2tr" ? { tapBip32Derivation: [payerToTapBip32Derivation(payer)] } : { bip32Derivation: [payerToBip32Derivation(payer)] };
1411
+ const tapInternalKey = payer.paymentType === "p2tr" ? { tapInternalKey: payer.payment.tapInternalKey } : {};
1392
1412
  tx.addInput({
1393
1413
  txid: input.txid,
1394
1414
  index: input.vout,
1395
- sequence: 0,
1396
- bip32Derivation,
1397
1415
  witnessUtxo: {
1398
- // script = 0014 + pubKeyHash
1399
- script: p2wpkh3.script,
1416
+ script: payer.payment.script,
1400
1417
  amount: BigInt(input.value)
1401
- }
1418
+ },
1419
+ ...bip32Derivation,
1420
+ ...tapInternalKey
1402
1421
  });
1403
1422
  }
1404
1423
  outputs.forEach((output) => {
1405
1424
  if (!output.address) {
1406
- tx.addOutputAddress(payerAddress, BigInt(output.value), network);
1425
+ tx.addOutputAddress(changeAddress, BigInt(output.value), network);
1407
1426
  return;
1408
1427
  }
1409
1428
  tx.addOutputAddress(output.address, BigInt(output.value), network);
@@ -1542,9 +1561,10 @@ export {
1542
1561
  ecdsaPublicKeyToSchnorr,
1543
1562
  encodeMessageWitnessData,
1544
1563
  extractExtendedPublicKeyFromPolicy,
1564
+ extractPayerInfoFromDerivationPath,
1545
1565
  extractRequiredKeyOrigins,
1546
1566
  filterUneconomicalUtxos,
1547
- generateBitcoinUnsignedTransactionNativeSegwit,
1567
+ generateBitcoinUnsignedTransaction,
1548
1568
  getAddressFromOutScript,
1549
1569
  getBitcoinAddressNetworkType,
1550
1570
  getBitcoinCoinTypeIndexByNetwork,