@leather.io/bitcoin 0.34.0 → 0.35.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,5 +1,5 @@
1
1
 
2
- > @leather.io/bitcoin@0.34.0 build /home/runner/work/mono/mono/packages/bitcoin
2
+ > @leather.io/bitcoin@0.35.0 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 57.27 KB
12
- ESM dist/index.js.map 113.17 KB
13
- ESM ⚡️ Build success in 111ms
11
+ ESM dist/index.js 58.79 KB
12
+ ESM dist/index.js.map 116.39 KB
13
+ ESM ⚡️ Build success in 137ms
14
14
  DTS Build start
15
- DTS ⚡️ Build success in 6055ms
16
- DTS dist/index.d.ts 28.05 KB
15
+ DTS ⚡️ Build success in 6108ms
16
+ DTS dist/index.d.ts 28.76 KB
package/CHANGELOG.md CHANGED
@@ -939,6 +939,34 @@
939
939
  * @leather.io/prettier-config bumped to 0.9.0
940
940
  * @leather.io/rpc bumped to 2.20.17
941
941
 
942
+ ## [0.35.0](https://github.com/leather-io/mono/compare/@leather.io/bitcoin-v0.34.1...@leather.io/bitcoin-v0.35.0) (2025-10-31)
943
+
944
+
945
+ ### Features
946
+
947
+ * add regtest mempool api support to services ([d6d0d68](https://github.com/leather-io/mono/commit/d6d0d6830fe37119c524ec74b00df7c87aff0812))
948
+
949
+
950
+ ### Dependencies
951
+
952
+ * The following workspace dependencies were updated
953
+ * dependencies
954
+ * @leather.io/constants bumped to 0.27.3
955
+ * @leather.io/crypto bumped to 1.12.2
956
+ * @leather.io/models bumped to 0.46.0
957
+ * @leather.io/utils bumped to 0.48.0
958
+ * devDependencies
959
+ * @leather.io/rpc bumped to 2.20.21
960
+
961
+ ## [0.34.1](https://github.com/leather-io/mono/compare/@leather.io/bitcoin-v0.34.0...@leather.io/bitcoin-v0.34.1) (2025-10-17)
962
+
963
+
964
+ ### Dependencies
965
+
966
+ * The following workspace dependencies were updated
967
+ * devDependencies
968
+ * @leather.io/rpc bumped to 2.20.20
969
+
942
970
  ## [0.34.0](https://github.com/leather-io/mono/compare/@leather.io/bitcoin-v0.33.2...@leather.io/bitcoin-v0.34.0) (2025-10-16)
943
971
 
944
972
 
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ import * as ecpair from 'ecpair';
3
3
  import * as bitcoin from 'bitcoinjs-lib';
4
4
  import * as btc from '@scure/btc-signer';
5
5
  import * as _leather_io_models from '@leather.io/models';
6
- import { BitcoinAddress, BitcoinNetworkModes, Money, AverageBitcoinFeeRates, NetworkModes, Inscription, ValueOf, TransactionErrorKey } from '@leather.io/models';
6
+ import { BitcoinAddress, BitcoinNetworkModes, Money, AverageBitcoinFeeRates, NetworkModes, InscriptionAsset, ValueOf, TransactionErrorKey } from '@leather.io/models';
7
7
  import BigNumber from 'bignumber.js';
8
8
  import { z } from 'zod';
9
9
  import * as _scure_btc_signer_payment from '@scure/btc-signer/payment';
@@ -361,7 +361,7 @@ declare function publicKeyToPayToScriptHashAddress(publicKey: Uint8Array, networ
361
361
  interface PsbtInput {
362
362
  address: BitcoinAddress;
363
363
  index?: number;
364
- inscription?: Inscription;
364
+ inscription?: InscriptionAsset;
365
365
  isMutable: boolean;
366
366
  toSign: boolean;
367
367
  txid: string;
@@ -596,6 +596,18 @@ declare function getDescriptorFromKeychain<T extends {
596
596
  keyOrigin: string;
597
597
  xpub: string;
598
598
  }>(accountKeychain: T): string | undefined;
599
+ declare function inferPaymentTypeFromDescriptor(descriptor: string): SupportedPaymentType;
600
+ declare function extractXpubFromDescriptor(descriptor: string): string;
601
+ interface DeriveAddressesFromDescriptorArgs {
602
+ accountDescriptor: string;
603
+ network: BitcoinNetworkModes;
604
+ limit?: number;
605
+ }
606
+ interface DeriveAddressesFromDescriptorResult {
607
+ address: string;
608
+ path: string;
609
+ }
610
+ declare function deriveAddressesFromDescriptor({ accountDescriptor, network, limit, }: DeriveAddressesFromDescriptorArgs): DeriveAddressesFromDescriptorResult[];
599
611
 
600
612
  interface LookUpDerivationByAddressArgs {
601
613
  taprootXpub: string;
@@ -612,4 +624,4 @@ declare function lookupDerivationByAddress(args: LookUpDerivationByAddressArgs):
612
624
  readonly path?: undefined;
613
625
  };
614
626
 
615
- 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, btcAddressNetworkValidator, btcAddressValidator, 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, getNetworkTypeFromAddress, 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, mnemonicToRootNode, nonEmptyStringValidator, parseKnownPaymentType, payToScriptHashTestnetPrefix, payerToBip32Derivation, payerToBip32DerivationBitcoinJsLib, payerToTapBip32Derivation, payerToTapBip32DerivationBitcoinJsLib, paymentTypeMap, publicKeyToPayToScriptHashAddress, recipientAddress, segwitAddress, serializeKeyOrigin, signBip322MessageSimple, taprootAddress, toXOnly, tweakSigner, whenBitcoinNetwork, whenPaymentType, whenSupportedPaymentType };
627
+ 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 DeriveAddressesFromDescriptorArgs, type DeriveAddressesFromDescriptorResult, 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, btcAddressNetworkValidator, btcAddressValidator, btcSignerLibPaymentTypeToPaymentTypeMap, calculateMaxSpend, coinTypeMap, createBitcoinAddress, createNativeSegwitBitcoinJsSigner, createTaprootBitcoinJsSigner, createToSpendTx, createWalletIdDecoratedPath, decodeBitcoinTx, decodeCompressedWifPrivateKey, 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, isSupportedMessageSigningPaymentType, isValidBitcoinAddress, isValidBitcoinNetworkAddress, isValidBitcoinTransaction, 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 };
package/dist/index.js CHANGED
@@ -1497,6 +1497,7 @@ function isValidBitcoinTransaction({
1497
1497
  }
1498
1498
 
1499
1499
  // src/utils/bitcoin.descriptors.ts
1500
+ import { HARDENED_OFFSET as HARDENED_OFFSET2, HDKey as HDKey3 } from "@scure/bip32";
1500
1501
  function getDescriptorFromKeychain(accountKeychain) {
1501
1502
  switch (inferPaymentTypeFromPath(accountKeychain.keyOrigin)) {
1502
1503
  case "p2tr":
@@ -1507,20 +1508,62 @@ function getDescriptorFromKeychain(accountKeychain) {
1507
1508
  return void 0;
1508
1509
  }
1509
1510
  }
1511
+ function inferPaymentTypeFromDescriptor(descriptor) {
1512
+ const descriptorPrefix = descriptor.split("(")[0];
1513
+ switch (descriptorPrefix) {
1514
+ case "tr":
1515
+ return "p2tr";
1516
+ case "wpkh":
1517
+ return "p2wpkh";
1518
+ default:
1519
+ throw new Error("Unrecognized descriptor");
1520
+ }
1521
+ }
1522
+ function extractXpubFromDescriptor(descriptor) {
1523
+ const match = descriptor.match(/\((xpub[0-9A-Za-z]+)\)/);
1524
+ if (match && match[1]) {
1525
+ return match[1];
1526
+ }
1527
+ throw new Error("Invalid descriptor format");
1528
+ }
1529
+ function deriveAddressesFromDescriptor({
1530
+ accountDescriptor,
1531
+ network,
1532
+ limit = 1
1533
+ }) {
1534
+ const accountKeychain = HDKey3.fromExtendedKey(extractXpubFromDescriptor(accountDescriptor));
1535
+ const paymentType = inferPaymentTypeFromDescriptor(accountDescriptor);
1536
+ const derivationPathFn = whenSupportedPaymentType(paymentType)({
1537
+ p2tr: makeTaprootAddressIndexDerivationPath,
1538
+ p2wpkh: makeNativeSegwitAddressIndexDerivationPath
1539
+ });
1540
+ const results = [];
1541
+ for (let i = 0; i < limit; i++) {
1542
+ const address2 = whenSupportedPaymentType(paymentType)({
1543
+ p2tr: getTaprootAddress({ index: i, keychain: accountKeychain, network }),
1544
+ p2wpkh: getNativeSegwitAddress({ index: i, keychain: accountKeychain, network })
1545
+ });
1546
+ results.push({
1547
+ address: address2,
1548
+ path: derivationPathFn(network, accountKeychain.index - HARDENED_OFFSET2, i)
1549
+ });
1550
+ }
1551
+ return results;
1552
+ }
1510
1553
 
1511
1554
  // src/utils/lookup-derivation-by-address.ts
1512
- import { HARDENED_OFFSET as HARDENED_OFFSET2, HDKey as HDKey3 } from "@scure/bip32";
1555
+ import { HARDENED_OFFSET as HARDENED_OFFSET3, HDKey as HDKey4 } from "@scure/bip32";
1513
1556
  import { createCounter } from "@leather.io/utils";
1514
1557
  function lookupDerivationByAddress(args) {
1515
1558
  const { taprootXpub, nativeSegwitXpub, iterationLimit } = args;
1516
- const taprootKeychain = HDKey3.fromExtendedKey(taprootXpub);
1517
- const nativeSegwitKeychain = HDKey3.fromExtendedKey(nativeSegwitXpub);
1559
+ const taprootKeychain = HDKey4.fromExtendedKey(taprootXpub);
1560
+ const nativeSegwitKeychain = HDKey4.fromExtendedKey(nativeSegwitXpub);
1518
1561
  return (address2) => {
1519
1562
  const network = inferNetworkFromAddress(address2);
1520
1563
  const paymentType = inferPaymentTypeFromAddress(address2);
1521
1564
  const accountIndex = whenSupportedPaymentType(paymentType)({
1522
- p2tr: taprootKeychain.index - HARDENED_OFFSET2,
1523
- p2wpkh: nativeSegwitKeychain.index - HARDENED_OFFSET2
1565
+ p2tr: taprootKeychain.index - HARDENED_OFFSET3,
1566
+ p2wpkh: nativeSegwitKeychain.index - HARDENED_OFFSET3
1524
1567
  });
1525
1568
  function getTaprootAddressAtIndex(index) {
1526
1569
  return getTaprootAddress({ index, keychain: taprootKeychain, network });
@@ -1581,6 +1624,7 @@ export {
1581
1624
  decodeCompressedWifPrivateKey,
1582
1625
  deriveAddressIndexKeychainFromAccount,
1583
1626
  deriveAddressIndexZeroFromAccount,
1627
+ deriveAddressesFromDescriptor,
1584
1628
  deriveBitcoinPayerFromAccount,
1585
1629
  deriveBtcBip49SeedFromMnemonic,
1586
1630
  deriveNativeSegwitAccountFromRootKeychain,
@@ -1597,6 +1641,7 @@ export {
1597
1641
  extractExtendedPublicKeyFromPolicy,
1598
1642
  extractPayerInfoFromDerivationPath,
1599
1643
  extractRequiredKeyOrigins,
1644
+ extractXpubFromDescriptor,
1600
1645
  filterUneconomicalUtxos,
1601
1646
  generateBitcoinUnsignedTransaction,
1602
1647
  getAddressFromOutScript,
@@ -1638,6 +1683,7 @@ export {
1638
1683
  inferNetworkFromAddress,
1639
1684
  inferNetworkFromPath,
1640
1685
  inferPaymentTypeFromAddress,
1686
+ inferPaymentTypeFromDescriptor,
1641
1687
  inferPaymentTypeFromPath,
1642
1688
  initBitcoinAccount,
1643
1689
  initializeBitcoinAccountKeychainFromDescriptor,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/bip21/bip21.ts","../src/bip322/bip322-utils.ts","../src/utils/bitcoin.utils.ts","../src/payments/p2tr-address-gen.ts","../src/utils/bitcoin.network.ts","../src/payments/p2wpkh-address-gen.ts","../src/validation/address-validation.ts","../src/validation/bitcoin-error.ts","../src/validation/bitcoin-address.ts","../src/bip322/sign-message-bip322-bitcoinjs.ts","../src/coin-selection/calculate-max-spend.ts","../src/coin-selection/coin-selection.utils.ts","../src/fees/btc-size-fee-estimator.ts","../src/coin-selection/coin-selection.ts","../src/fees/bitcoin-fees.ts","../src/mocks/mocks.ts","../src/schemas/address-schema.ts","../src/payments/p2wsh-p2sh-address-gen.ts","../src/psbt/psbt-totals.ts","../src/psbt/psbt-inputs.ts","../src/psbt/psbt-outputs.ts","../src/psbt/psbt-details.ts","../src/psbt/utils.ts","../src/signer/bitcoin-signer.ts","../src/transactions/generate-unsigned-transaction.ts","../src/validation/amount-validation.ts","../src/validation/transaction-validation.ts","../src/utils/bitcoin.descriptors.ts","../src/utils/lookup-derivation-by-address.ts"],"sourcesContent":["import { Bip21Options, decode, encode } from 'bip21';\n\nimport { isError } from '@leather.io/utils';\n\ninterface Bip21ResultValue {\n address: string;\n [key: string]: string | number | undefined;\n}\n\ntype Bip21Result =\n | {\n success: true;\n data: Bip21ResultValue;\n }\n | {\n success: false;\n error: string;\n };\n\nexport const bip21 = {\n decode: (uri: string, urnScheme?: string): Bip21Result => {\n try {\n const { address, options } = decode(uri, urnScheme);\n return {\n success: true,\n data: { address, ...options },\n };\n } catch (error) {\n return {\n success: false,\n error: isError(error) ? error.message : 'invalid input',\n };\n }\n },\n encode: (address: string, options: Bip21Options, urnScheme?: string) => {\n try {\n return encode(address, options, urnScheme);\n } catch {\n return null;\n }\n },\n};\n","import ecc from '@bitcoinerlab/secp256k1';\nimport { sha256 } from '@noble/hashes/sha256';\nimport { hexToBytes, utf8ToBytes } from '@noble/hashes/utils';\nimport * as bitcoin from 'bitcoinjs-lib';\nimport { ECPairFactory } from 'ecpair';\nimport { encode } from 'varuint-bitcoin';\n\nimport { PaymentTypes } from '@leather.io/rpc';\nimport { isString } from '@leather.io/utils';\n\nimport { toXOnly } from '../utils/bitcoin.utils';\n\nconst bip322MessageTag = 'BIP0322-signed-message';\n\nconst ECPair = ECPairFactory(ecc);\nbitcoin.initEccLib(ecc);\n\nexport function ecPairFromPrivateKey(key: Uint8Array) {\n return ECPair.fromPrivateKey(Buffer.from(key));\n}\n\n// See tagged hashes section of BIP-340\n// https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki#design\nconst messageTagHash = Uint8Array.from([\n ...sha256(utf8ToBytes(bip322MessageTag)),\n ...sha256(utf8ToBytes(bip322MessageTag)),\n]);\n\nexport function hashBip322Message(message: Uint8Array | string) {\n return sha256(\n Uint8Array.from([...messageTagHash, ...(isString(message) ? utf8ToBytes(message) : message)])\n );\n}\n\nexport const bip322TransactionToSignValues = {\n prevoutHash: hexToBytes('0000000000000000000000000000000000000000000000000000000000000000'),\n prevoutIndex: 0xffffffff,\n sequence: 0,\n};\n\nfunction encodeVarString(b: Buffer) {\n return Buffer.concat([encode(b.byteLength), b]);\n}\n\nconst supportedMessageSigningPaymentTypes: PaymentTypes[] = ['p2wpkh', 'p2tr'];\n\nexport function isSupportedMessageSigningPaymentType(paymentType: string) {\n return supportedMessageSigningPaymentTypes.includes(paymentType as PaymentTypes);\n}\n\n/**\n * Encode witness data for a BIP322 message\n * TODO: Refactor to remove `Buffer` use\n */\nexport function encodeMessageWitnessData(witnessArray: Buffer[]) {\n const len = encode(witnessArray.length);\n return Buffer.concat([len, ...witnessArray.map(witness => encodeVarString(witness))]);\n}\n\nfunction tapTweakHash(pubKey: Buffer, h: Buffer | undefined): Buffer {\n return bitcoin.crypto.taggedHash('TapTweak', Buffer.concat(h ? [pubKey, h] : [pubKey]));\n}\n\nexport function tweakSigner(signer: bitcoin.Signer, opts: any = {}): bitcoin.Signer {\n // @ts-expect-error privateKey exists on signer\n let privateKey: Uint8Array | undefined = signer.privateKey;\n if (!privateKey) {\n throw new Error('Private key is required for tweaking signer!');\n }\n if (signer.publicKey[0] === 3) {\n privateKey = ecc.privateNegate(privateKey);\n }\n\n const tweakedPrivateKey = ecc.privateAdd(\n privateKey,\n tapTweakHash(toXOnly(signer.publicKey), opts.tweakHash)\n );\n if (!tweakedPrivateKey) {\n throw new Error('Invalid tweaked private key!');\n }\n\n return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {\n network: opts.network,\n });\n}\n","import { hexToBytes } from '@noble/hashes/utils';\nimport { HDKey, Versions } from '@scure/bip32';\nimport { mnemonicToSeedSync } from '@scure/bip39';\nimport * as btc from '@scure/btc-signer';\nimport { TransactionInput, TransactionOutput } from '@scure/btc-signer/psbt';\n\nimport {\n DerivationPathDepth,\n extractAccountIndexFromPath,\n extractPurposeFromPath,\n} from '@leather.io/crypto';\nimport { BitcoinAddress, BitcoinNetworkModes, NetworkModes } from '@leather.io/models';\nimport type { BitcoinPaymentTypes } from '@leather.io/rpc';\nimport { defaultWalletKeyId, isDefined, whenNetwork } from '@leather.io/utils';\n\nimport { getTaprootPayment } from '../payments/p2tr-address-gen';\nimport { getNativeSegwitPaymentFromAddressIndex } from '../payments/p2wpkh-address-gen';\nimport { createBitcoinAddress } from '../validation/bitcoin-address';\nimport { BtcSignerNetwork, getBtcSignerLibNetworkConfigByMode } from './bitcoin.network';\n\nexport interface BitcoinAccount {\n type: BitcoinPaymentTypes;\n derivationPath: string;\n keychain: HDKey;\n accountIndex: number;\n network: BitcoinNetworkModes;\n}\nexport function initBitcoinAccount(derivationPath: string, policy: string): BitcoinAccount {\n const xpub = extractExtendedPublicKeyFromPolicy(policy);\n const network = inferNetworkFromPath(derivationPath);\n return {\n keychain: HDKey.fromExtendedKey(xpub, getHdKeyVersionsFromNetwork(network)),\n network,\n derivationPath,\n type: inferPaymentTypeFromPath(derivationPath),\n accountIndex: extractAccountIndexFromPath(derivationPath),\n };\n}\n\n/**\n * Represents a map of `BitcoinNetworkModes` to `NetworkModes`. While Bitcoin\n * has a number of networks, its often only necessary to consider the higher\n * level concept of mainnet and testnet\n */\nexport const bitcoinNetworkToCoreNetworkMap: Record<BitcoinNetworkModes, NetworkModes> = {\n mainnet: 'mainnet',\n testnet: 'testnet',\n regtest: 'testnet',\n signet: 'testnet',\n};\nexport function bitcoinNetworkModeToCoreNetworkMode(mode: BitcoinNetworkModes) {\n return bitcoinNetworkToCoreNetworkMap[mode];\n}\n\ntype BitcoinNetworkMap<T> = Record<BitcoinNetworkModes, T>;\n\nexport function whenBitcoinNetwork(mode: BitcoinNetworkModes) {\n return <T extends BitcoinNetworkMap<unknown>>(networkMap: T) =>\n networkMap[mode] as T[BitcoinNetworkModes];\n}\n\n/**\n * Map representing the \"Coin Type\" section of a derivation path.\n * Consider example below, Coin type is one, thus testnet\n * @example\n * `m/86'/1'/0'/0/0`\n */\nexport const coinTypeMap: Record<NetworkModes, 0 | 1> = {\n mainnet: 0,\n testnet: 1,\n};\n\nexport function getBitcoinCoinTypeIndexByNetwork(network: BitcoinNetworkModes) {\n return coinTypeMap[bitcoinNetworkModeToCoreNetworkMode(network)];\n}\n\nexport function deriveAddressIndexKeychainFromAccount(keychain: HDKey) {\n if (keychain.depth !== DerivationPathDepth.Account)\n throw new Error('Keychain passed is not an account');\n\n return (index: number) => keychain.deriveChild(0).deriveChild(index);\n}\n\nexport function deriveAddressIndexZeroFromAccount(keychain: HDKey) {\n return deriveAddressIndexKeychainFromAccount(keychain)(0);\n}\n\nexport const ecdsaPublicKeyLength = 33;\n\nexport function ecdsaPublicKeyToSchnorr(pubKey: Uint8Array) {\n if (pubKey.byteLength !== ecdsaPublicKeyLength) throw new Error('Invalid public key length');\n return pubKey.slice(1);\n}\n\n// Basically same as above, to remove\nexport function toXOnly(pubKey: Buffer) {\n return pubKey.length === 32 ? pubKey : pubKey.subarray(1, 33);\n}\n\nexport function decodeBitcoinTx(tx: string): ReturnType<typeof btc.RawTx.decode> {\n return btc.RawTx.decode(hexToBytes(tx));\n}\n\nexport function getAddressFromOutScript(\n script: Uint8Array,\n bitcoinNetwork: BtcSignerNetwork\n): BitcoinAddress | null {\n const outputScript = btc.OutScript.decode(script);\n\n switch (outputScript.type) {\n case 'pkh':\n case 'sh':\n case 'wpkh':\n case 'wsh':\n return createBitcoinAddress(\n btc.Address(bitcoinNetwork).encode({\n type: outputScript.type,\n hash: outputScript.hash,\n })\n );\n case 'tr':\n return createBitcoinAddress(\n btc.Address(bitcoinNetwork).encode({\n type: outputScript.type,\n pubkey: outputScript.pubkey,\n })\n );\n case 'ms':\n return createBitcoinAddress(btc.p2ms(outputScript.m, outputScript.pubkeys).address ?? '');\n case 'pk':\n return createBitcoinAddress(btc.p2pk(outputScript.pubkey, bitcoinNetwork).address ?? '');\n case 'unknown':\n case 'tr_ms':\n case 'tr_ns':\n case 'p2a':\n default:\n return null;\n }\n}\n/**\n * Payment type identifiers, as described by `@scure/btc-signer` library\n */\nexport type BtcSignerLibPaymentTypeIdentifers = 'wpkh' | 'wsh' | 'tr' | 'pkh' | 'sh';\n\nexport const paymentTypeMap: Record<BtcSignerLibPaymentTypeIdentifers, BitcoinPaymentTypes> = {\n wpkh: 'p2wpkh',\n wsh: 'p2wpkh-p2sh',\n tr: 'p2tr',\n pkh: 'p2pkh',\n sh: 'p2sh',\n};\n\nexport function btcSignerLibPaymentTypeToPaymentTypeMap(\n payment: BtcSignerLibPaymentTypeIdentifers\n) {\n return paymentTypeMap[payment];\n}\n\nexport function isBtcSignerLibPaymentType(\n payment: string\n): payment is BtcSignerLibPaymentTypeIdentifers {\n return payment in paymentTypeMap;\n}\n\nexport function parseKnownPaymentType(\n payment: BtcSignerLibPaymentTypeIdentifers | BitcoinPaymentTypes\n) {\n return isBtcSignerLibPaymentType(payment)\n ? btcSignerLibPaymentTypeToPaymentTypeMap(payment)\n : payment;\n}\n\nexport type PaymentTypeMap<T> = Record<BitcoinPaymentTypes, T>;\nexport function whenPaymentType(mode: BitcoinPaymentTypes | BtcSignerLibPaymentTypeIdentifers) {\n return <T>(paymentMap: PaymentTypeMap<T>): T => paymentMap[parseKnownPaymentType(mode)];\n}\n\nexport type SupportedPaymentType = 'p2wpkh' | 'p2tr';\nexport type SupportedPaymentTypeMap<T> = Record<SupportedPaymentType, T>;\nexport function whenSupportedPaymentType(mode: SupportedPaymentType) {\n return <T>(paymentMap: SupportedPaymentTypeMap<T>): T => paymentMap[mode];\n}\n\n/**\n * Infers the Bitcoin payment type from the derivation path.\n * Below we see path has 86 in it, per convention, this refers to taproot payments\n * @example\n * `m/86'/1'/0'/0/0`\n */\nexport function inferPaymentTypeFromPath(path: string): BitcoinPaymentTypes {\n const purpose = extractPurposeFromPath(path);\n switch (purpose) {\n case 84:\n return 'p2wpkh';\n case 86:\n return 'p2tr';\n case 44:\n return 'p2pkh';\n default:\n throw new Error(`Unable to infer payment type from purpose=${purpose}`);\n }\n}\n\nexport function inferNetworkFromPath(path: string): NetworkModes {\n return path.split('/')[2].startsWith('0') ? 'mainnet' : 'testnet';\n}\n\nexport function extractExtendedPublicKeyFromPolicy(policy: string) {\n return policy.split(']')[1];\n}\n\nexport function createWalletIdDecoratedPath(policy: string, walletId: string) {\n return policy.split(']')[0].replace('[', '').replace('m', walletId);\n}\n\n// Primarily used to get the correct `Version` when passing Ledger Bitcoin\n// extended public keys to the HDKey constructor\nexport function getHdKeyVersionsFromNetwork(network: NetworkModes) {\n return whenNetwork(network)({\n mainnet: undefined,\n testnet: {\n private: 0x00000000,\n public: 0x043587cf,\n } as Versions,\n });\n}\n\nexport function getBitcoinInputAddress(input: TransactionInput, bitcoinNetwork: BtcSignerNetwork) {\n if (isDefined(input.witnessUtxo))\n return getAddressFromOutScript(input.witnessUtxo.script, bitcoinNetwork);\n if (isDefined(input.nonWitnessUtxo) && isDefined(input.index))\n return getAddressFromOutScript(\n input.nonWitnessUtxo.outputs[input.index]?.script,\n bitcoinNetwork\n );\n return null;\n}\n\nexport function getInputPaymentType(\n input: TransactionInput,\n network: BitcoinNetworkModes\n): BitcoinPaymentTypes {\n const address = getBitcoinInputAddress(input, getBtcSignerLibNetworkConfigByMode(network));\n if (address === null) throw new Error('Input address cannot be empty');\n if (address.startsWith('bc1p') || address.startsWith('tb1p') || address.startsWith('bcrt1p'))\n return 'p2tr';\n if (address.startsWith('bc1q') || address.startsWith('tb1q') || address.startsWith('bcrt1q'))\n return 'p2wpkh';\n throw new Error('Unable to infer payment type from input address');\n}\n\n// Ledger wallets are keyed by their derivation path. To reuse the look up logic\n// between payment types, this factory fn accepts a fn that generates the path\nexport function lookUpLedgerKeysByPath(\n getDerivationPath: (network: BitcoinNetworkModes, accountIndex: number) => string\n) {\n return (\n ledgerKeyMap: Record<string, { policy: string } | undefined>,\n network: BitcoinNetworkModes\n ) =>\n (accountIndex: number) => {\n const path = getDerivationPath(network, accountIndex);\n // Single wallet mode, hardcoded default walletId\n const account = ledgerKeyMap[path.replace('m', defaultWalletKeyId)];\n if (!account) return;\n return initBitcoinAccount(path, account.policy);\n };\n}\n\ninterface GetAddressArgs {\n index: number;\n keychain?: HDKey;\n network: BitcoinNetworkModes;\n}\n\nexport function getTaprootAddress({ index, keychain, network }: GetAddressArgs) {\n if (!keychain) throw new Error('Expected keychain to be provided');\n\n if (keychain.depth !== DerivationPathDepth.Account)\n throw new Error('Expects keychain to be on the account index');\n\n const addressIndex = deriveAddressIndexKeychainFromAccount(keychain)(index);\n\n if (!addressIndex.publicKey) throw new Error('Expected publicKey to be defined');\n\n const payment = getTaprootPayment(addressIndex.publicKey, network);\n\n if (!payment.address) throw new Error('Expected address to be defined');\n return payment.address;\n}\n\nexport function getNativeSegwitAddress({ index, keychain, network }: GetAddressArgs) {\n if (!keychain) throw new Error('Expected keychain to be provided');\n\n if (keychain.depth !== DerivationPathDepth.Account)\n throw new Error('Expects keychain to be on the account index');\n\n const addressIndex = deriveAddressIndexKeychainFromAccount(keychain)(index);\n\n if (!addressIndex.publicKey) throw new Error('Expected publicKey to be defined');\n\n const payment = getNativeSegwitPaymentFromAddressIndex(addressIndex, network);\n\n if (!payment.address) throw new Error('Expected address to be defined');\n return payment.address;\n}\n\n/**\n * @deprecated\n * Use `deriveRootBip32Keychain` in `@leather.io/crypto` instead\n */\nexport function mnemonicToRootNode(secretKey: string) {\n const seed = mnemonicToSeedSync(secretKey);\n return HDKey.fromMasterSeed(seed);\n}\n\nexport function getPsbtTxInputs(psbtTx: btc.Transaction): TransactionInput[] {\n const inputsLength = psbtTx.inputsLength;\n const inputs: TransactionInput[] = [];\n for (let i = 0; i < inputsLength; i++) inputs.push(psbtTx.getInput(i));\n return inputs;\n}\n\nexport function getPsbtTxOutputs(psbtTx: btc.Transaction): TransactionOutput[] {\n const outputsLength = psbtTx.outputsLength;\n const outputs: TransactionOutput[] = [];\n for (let i = 0; i < outputsLength; i++) outputs.push(psbtTx.getOutput(i));\n return outputs;\n}\n\nexport function inferNetworkFromAddress(address: BitcoinAddress): BitcoinNetworkModes {\n if (address.startsWith('bc1')) return 'mainnet';\n if (address.startsWith('tb1')) return 'testnet';\n if (address.startsWith('bcrt1')) return 'regtest';\n\n const firstChar = address[0];\n\n if (firstChar === '1' || firstChar === '3') return 'mainnet';\n if (firstChar === 'm' || firstChar === 'n') return 'testnet';\n if (firstChar === '2') return 'testnet';\n\n throw new Error('Invalid or unsupported Bitcoin address format');\n}\n\nexport function inferPaymentTypeFromAddress(address: BitcoinAddress): SupportedPaymentType {\n if (address.startsWith('bc1q') || address.startsWith('tb1q') || address.startsWith('bcrt1q'))\n return 'p2wpkh';\n\n if (address.startsWith('bc1p') || address.startsWith('tb1p') || address.startsWith('bcrt1p'))\n return 'p2tr';\n\n throw new Error('Unable to infer payment type from address');\n}\n\nexport function getBitcoinInputValue(input: TransactionInput) {\n if (isDefined(input.witnessUtxo)) return Number(input.witnessUtxo.amount);\n if (isDefined(input.nonWitnessUtxo) && isDefined(input.index))\n return Number(input.nonWitnessUtxo.outputs[input.index]?.amount);\n // logger.warn('Unable to find either `witnessUtxo` or `nonWitnessUtxo` in input. Defaulting to 0');\n return 0;\n}\n","import { HDKey } from '@scure/bip32';\nimport * as btc from '@scure/btc-signer';\n\nimport { DerivationPathDepth } from '@leather.io/crypto';\nimport { BitcoinNetworkModes } from '@leather.io/models';\n\nimport { getBtcSignerLibNetworkConfigByMode } from '../utils/bitcoin.network';\nimport {\n BitcoinAccount,\n deriveAddressIndexZeroFromAccount,\n ecdsaPublicKeyToSchnorr,\n getBitcoinCoinTypeIndexByNetwork,\n} from '../utils/bitcoin.utils';\n\nexport function makeTaprootAccountDerivationPath(\n network: BitcoinNetworkModes,\n accountIndex: number\n) {\n return `m/86'/${getBitcoinCoinTypeIndexByNetwork(network)}'/${accountIndex}'`;\n}\n/** @deprecated Use makeTaprootAccountDerivationPath */\nexport const getTaprootAccountDerivationPath = makeTaprootAccountDerivationPath;\n\nexport function makeTaprootAddressIndexDerivationPath(\n network: BitcoinNetworkModes,\n accountIndex: number,\n addressIndex: number\n) {\n return makeTaprootAccountDerivationPath(network, accountIndex) + `/0/${addressIndex}`;\n}\n/** @deprecated Use makeTaprootAddressIndexDerivationPath */\nexport const getTaprootAddressIndexDerivationPath = makeTaprootAddressIndexDerivationPath;\n\nexport function deriveTaprootAccount(keychain: HDKey, network: BitcoinNetworkModes) {\n if (keychain.depth !== DerivationPathDepth.Root)\n throw new Error('Keychain passed is not an account');\n\n return (accountIndex: number): BitcoinAccount => ({\n type: 'p2tr',\n network,\n accountIndex,\n derivationPath: makeTaprootAccountDerivationPath(network, accountIndex),\n keychain: keychain.derive(makeTaprootAccountDerivationPath(network, accountIndex)),\n });\n}\n\nexport function getTaprootPayment(publicKey: Uint8Array, network: BitcoinNetworkModes) {\n return btc.p2tr(\n ecdsaPublicKeyToSchnorr(publicKey),\n undefined,\n getBtcSignerLibNetworkConfigByMode(network),\n true // allow unknown outputs\n );\n}\n\nexport function getTaprootPaymentFromAddressIndex(keychain: HDKey, network: BitcoinNetworkModes) {\n if (keychain.depth !== DerivationPathDepth.AddressIndex)\n throw new Error('Keychain passed is not an address index');\n\n if (!keychain.publicKey) throw new Error('Keychain has no public key');\n\n return getTaprootPayment(keychain.publicKey, network);\n}\n\ninterface DeriveTaprootReceiveAddressIndexArgs {\n keychain: HDKey;\n network: BitcoinNetworkModes;\n}\nexport function deriveTaprootReceiveAddressIndexZero({\n keychain,\n network,\n}: DeriveTaprootReceiveAddressIndexArgs) {\n const zeroAddressIndex = deriveAddressIndexZeroFromAccount(keychain);\n return {\n keychain: zeroAddressIndex,\n payment: getTaprootPaymentFromAddressIndex(zeroAddressIndex, network),\n };\n}\n","import * as bitcoinJs from 'bitcoinjs-lib';\n\nimport { BitcoinNetworkModes } from '@leather.io/models';\n\n// TODO - this PR was merged so we could update this\n// https://github.com/paulmillr/scure-btc-signer/blob/main/src/utils.ts\n// See this PR https://github.com/paulmillr/@scure/btc-signer/pull/15\n// Atttempting to add these directly to the library\nexport interface BtcSignerNetwork {\n bech32: string;\n pubKeyHash: number;\n scriptHash: number;\n wif: number;\n}\n\nconst bitcoinMainnet: BtcSignerNetwork = {\n bech32: 'bc',\n pubKeyHash: 0x00,\n scriptHash: 0x05,\n wif: 0x80,\n};\n\nconst bitcoinTestnet: BtcSignerNetwork = {\n bech32: 'tb',\n pubKeyHash: 0x6f,\n scriptHash: 0xc4,\n wif: 0xef,\n};\n\nconst bitcoinRegtest: BtcSignerNetwork = {\n bech32: 'bcrt',\n pubKeyHash: 0x6f,\n scriptHash: 0xc4,\n wif: 0xef,\n};\n\nconst btcSignerLibNetworks: Record<BitcoinNetworkModes, BtcSignerNetwork> = {\n mainnet: bitcoinMainnet,\n testnet: bitcoinTestnet,\n regtest: bitcoinRegtest,\n // Signet originally was going to have its own prefix but authors decided to\n // copy testnet\n signet: bitcoinTestnet,\n};\n\nexport function getBtcSignerLibNetworkConfigByMode(network: BitcoinNetworkModes) {\n return btcSignerLibNetworks[network];\n}\n\nconst bitcoinJsLibNetworks: Record<BitcoinNetworkModes, bitcoinJs.Network> = {\n mainnet: bitcoinJs.networks.bitcoin,\n testnet: bitcoinJs.networks.testnet,\n regtest: bitcoinJs.networks.regtest,\n signet: bitcoinJs.networks.testnet,\n};\n\nexport function getBitcoinJsLibNetworkConfigByMode(network: BitcoinNetworkModes) {\n return bitcoinJsLibNetworks[network];\n}\n","import { HDKey } from '@scure/bip32';\nimport * as btc from '@scure/btc-signer';\n\nimport { DerivationPathDepth } from '@leather.io/crypto';\nimport { BitcoinNetworkModes } from '@leather.io/models';\n\nimport { getBtcSignerLibNetworkConfigByMode } from '../utils/bitcoin.network';\nimport {\n BitcoinAccount,\n deriveAddressIndexZeroFromAccount,\n getBitcoinCoinTypeIndexByNetwork,\n} from '../utils/bitcoin.utils';\n\nexport function makeNativeSegwitAccountDerivationPath(\n network: BitcoinNetworkModes,\n accountIndex: number\n) {\n return `m/84'/${getBitcoinCoinTypeIndexByNetwork(network)}'/${accountIndex}'`;\n}\n\n/** @deprecated Use makeNativeSegwitAccountDerivationPath */\nexport const getNativeSegwitAccountDerivationPath = makeNativeSegwitAccountDerivationPath;\n\nexport function makeNativeSegwitAddressIndexDerivationPath(\n network: BitcoinNetworkModes,\n accountIndex: number,\n addressIndex: number\n) {\n return makeNativeSegwitAccountDerivationPath(network, accountIndex) + `/0/${addressIndex}`;\n}\n\n/** @deprecated Use makeNativeSegwitAddressIndexDerivationPath */\nexport const getNativeSegwitAddressIndexDerivationPath = makeNativeSegwitAddressIndexDerivationPath;\n\nexport function deriveNativeSegwitAccountFromRootKeychain(\n keychain: HDKey,\n network: BitcoinNetworkModes\n) {\n if (keychain.depth !== DerivationPathDepth.Root) throw new Error('Keychain passed is not a root');\n return (accountIndex: number): BitcoinAccount => ({\n type: 'p2wpkh',\n network,\n accountIndex,\n derivationPath: makeNativeSegwitAccountDerivationPath(network, accountIndex),\n keychain: keychain.derive(makeNativeSegwitAccountDerivationPath(network, accountIndex)),\n });\n}\n\nexport function getNativeSegwitPaymentFromAddressIndex(\n keychain: HDKey,\n network: BitcoinNetworkModes\n) {\n if (keychain.depth !== DerivationPathDepth.AddressIndex)\n throw new Error('Keychain passed is not an address index');\n\n if (!keychain.publicKey) throw new Error('Keychain does not have a public key');\n\n return btc.p2wpkh(keychain.publicKey, getBtcSignerLibNetworkConfigByMode(network));\n}\n\ninterface DeriveNativeSegwitReceiveAddressIndexArgs {\n keychain: HDKey;\n network: BitcoinNetworkModes;\n}\nexport function deriveNativeSegwitReceiveAddressIndexZero({\n keychain,\n network,\n}: DeriveNativeSegwitReceiveAddressIndexArgs) {\n const zeroAddressIndex = deriveAddressIndexZeroFromAccount(keychain);\n return {\n keychain: zeroAddressIndex,\n payment: getNativeSegwitPaymentFromAddressIndex(zeroAddressIndex, network),\n };\n}\n","import { Network, validate } from 'bitcoin-address-validation';\n\nimport { BitcoinNetworkModes } from '@leather.io/models';\nimport { isEmptyString, isUndefined } from '@leather.io/utils';\n\n// todo investigate handling this in bitcoinNetworkToNetworkMode\nexport function getBitcoinAddressNetworkType(network: BitcoinNetworkModes): Network {\n // Signet uses testnet address format, this parsing is to please the\n // validation library - 'bitcoin-address-validation'\n if (network === 'signet') return Network.testnet;\n return network as Network;\n}\n\nexport function isValidBitcoinAddress(address: string) {\n if (isUndefined(address) || isEmptyString(address)) {\n return false;\n }\n\n return validate(address);\n}\n\nexport function isValidBitcoinNetworkAddress(address: string, network: BitcoinNetworkModes) {\n return validate(address, getBitcoinAddressNetworkType(network));\n}\n","import { TransactionErrorKey } from '@leather.io/models';\n\nexport class BitcoinError extends Error {\n public message: BitcoinErrorKey;\n constructor(message: BitcoinErrorKey) {\n super(message);\n this.name = 'BitcoinError';\n this.message = message;\n\n // Fix the prototype chain\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\nexport type BitcoinErrorKey =\n | TransactionErrorKey\n | 'InsufficientAmount'\n | 'NoInputsToSign'\n | 'NoOutputsToSign'\n | 'InscribedUtxos';\n","import { BitcoinAddress } from '@leather.io/models';\n\nimport { isValidBitcoinAddress } from './address-validation';\nimport { BitcoinError } from './bitcoin-error';\n\nexport function isBitcoinAddress(value: string): value is BitcoinAddress {\n try {\n isValidBitcoinAddress(value);\n return true;\n } catch {\n return false;\n }\n}\n\n// Function to create a BitcoinAddress\nexport function createBitcoinAddress(value: string): BitcoinAddress {\n if (!isBitcoinAddress(value)) {\n throw new BitcoinError('InvalidAddress');\n }\n\n return value;\n}\n","import { base64 } from '@scure/base';\nimport * as btc from '@scure/btc-signer';\nimport * as bitcoin from 'bitcoinjs-lib';\n\nimport { BitcoinAddress, BitcoinNetworkModes } from '@leather.io/models';\n\nimport { getBitcoinJsLibNetworkConfigByMode } from '../utils/bitcoin.network';\nimport {\n bip322TransactionToSignValues,\n ecPairFromPrivateKey,\n encodeMessageWitnessData,\n hashBip322Message,\n tweakSigner,\n} from './bip322-utils';\n\nexport function createNativeSegwitBitcoinJsSigner(privateKey: Buffer) {\n return ecPairFromPrivateKey(privateKey);\n}\n\nexport function createTaprootBitcoinJsSigner(privateKey: Buffer) {\n return tweakSigner(ecPairFromPrivateKey(privateKey));\n}\n\nexport function createToSpendTx(\n address: BitcoinAddress,\n message: string,\n network: BitcoinNetworkModes\n) {\n const { prevoutHash, prevoutIndex, sequence } = bip322TransactionToSignValues;\n\n const script = bitcoin.address.toOutputScript(\n address,\n getBitcoinJsLibNetworkConfigByMode(network)\n );\n\n const hash = hashBip322Message(message);\n const commands = [0, Buffer.from(hash)];\n const scriptSig = bitcoin.script.compile(commands);\n\n const virtualToSpend = new bitcoin.Transaction();\n virtualToSpend.version = 0;\n virtualToSpend.addInput(Buffer.from(prevoutHash), prevoutIndex, sequence, scriptSig);\n virtualToSpend.addOutput(script, 0);\n return { virtualToSpend, script };\n}\n\nfunction createToSignTx(toSpendTxHex: Buffer, script: Buffer, network: BitcoinNetworkModes) {\n const virtualToSign = new bitcoin.Psbt({ network: getBitcoinJsLibNetworkConfigByMode(network) });\n virtualToSign.setVersion(0);\n const prevTxHash = toSpendTxHex;\n const prevOutIndex = 0;\n const toSignScriptSig = bitcoin.script.compile([bitcoin.script.OPS.OP_RETURN]);\n\n virtualToSign.addInput({\n hash: prevTxHash,\n index: prevOutIndex,\n sequence: 0,\n witnessUtxo: { script, value: 0 },\n });\n\n virtualToSign.addOutput({ script: toSignScriptSig, value: 0 });\n return virtualToSign;\n}\n\ninterface SignBip322MessageSimple {\n address: BitcoinAddress;\n message: string;\n network: BitcoinNetworkModes;\n signPsbt(psbt: bitcoin.Psbt): Promise<btc.Transaction>;\n}\nexport async function signBip322MessageSimple(args: SignBip322MessageSimple) {\n const { address, message, network, signPsbt } = args;\n\n const { virtualToSpend, script } = createToSpendTx(address, message, network);\n\n const virtualToSign = createToSignTx(virtualToSpend.getHash(), script, network);\n\n const signedTx = await signPsbt(virtualToSign);\n\n const asBitcoinJsTransaction = bitcoin.Psbt.fromBuffer(Buffer.from(signedTx.toPSBT()));\n\n asBitcoinJsTransaction.finalizeInput(0);\n\n // sign the tx\n // section 5.1\n // github.com/LegReq/bip0322-signatures/blob/master/BIP0322_signing.ipynb\n const toSignTx = asBitcoinJsTransaction.extractTransaction();\n\n const result = encodeMessageWitnessData(toSignTx.ins[0].witness);\n\n return {\n virtualToSpend,\n virtualToSign: toSignTx,\n unencodedSig: result,\n signature: base64.encode(result),\n };\n}\n","import BigNumber from 'bignumber.js';\n\nimport type { AverageBitcoinFeeRates, Money } from '@leather.io/models';\nimport { createMoney, satToBtc } from '@leather.io/utils';\n\nimport { filterUneconomicalUtxos, getSpendableAmount } from './coin-selection.utils';\n\ninterface CalculateMaxSpendArgs {\n // recipient is intentionally string instead of BitcoinAddress, as it's being validated\n // with a fallback in a subroutine.\n recipient: string;\n utxos: { value: number; txid: string }[];\n feeRates?: AverageBitcoinFeeRates;\n feeRate?: number;\n}\n\nexport interface CalculateMaxSpendResponse {\n spendAllFee: number;\n amount: Money;\n spendableBitcoin: BigNumber;\n}\nexport function calculateMaxSpend({\n recipient,\n utxos,\n feeRate,\n feeRates,\n}: CalculateMaxSpendArgs): CalculateMaxSpendResponse {\n if (!utxos.length || !feeRates)\n return {\n spendAllFee: 0,\n amount: createMoney(0, 'BTC'),\n spendableBitcoin: new BigNumber(0),\n };\n\n const currentFeeRate = feeRate ?? feeRates.halfHourFee.toNumber();\n\n const filteredUtxos = filterUneconomicalUtxos({\n utxos,\n feeRate: currentFeeRate,\n recipients: [{ address: recipient, amount: createMoney(0, 'BTC') }],\n });\n\n const { spendableAmount, fee } = getSpendableAmount({\n utxos: filteredUtxos,\n feeRate: currentFeeRate,\n recipients: [{ address: recipient, amount: createMoney(0, 'BTC') }],\n isSendMax: true,\n });\n\n return {\n spendAllFee: fee,\n amount: createMoney(spendableAmount, 'BTC'),\n spendableBitcoin: satToBtc(spendableAmount),\n };\n}\n","import BigNumber from 'bignumber.js';\nimport validate, { AddressInfo, AddressType, getAddressInfo } from 'bitcoin-address-validation';\n\nimport { BTC_P2WPKH_DUST_AMOUNT } from '@leather.io/constants';\nimport { sumNumbers } from '@leather.io/utils';\n\nimport { BtcSizeFeeEstimator } from '../fees/btc-size-fee-estimator';\nimport { CoinSelectionRecipient } from './coin-selection';\n\nexport function getUtxoTotal<T extends { value: number }>(utxos: T[]) {\n return sumNumbers(utxos.map(utxo => utxo.value));\n}\n\nexport function getSizeInfo(payload: {\n inputLength: number;\n recipients: CoinSelectionRecipient[];\n isSendMax?: boolean;\n}) {\n const { inputLength, recipients, isSendMax } = payload;\n\n const validAddressesInfo = recipients\n .map(recipient => validate(recipient.address) && getAddressInfo(recipient.address))\n .filter(Boolean) as AddressInfo[];\n\n function getTxOutputsLengthByPaymentType() {\n return validAddressesInfo.reduce(\n (acc, { type }) => {\n acc[type] = (acc[type] || 0) + 1;\n return acc;\n },\n {} as Record<AddressType, number>\n );\n }\n\n const outputTypesCount = getTxOutputsLengthByPaymentType();\n\n // Add a change address if not sending max (defaults to p2wpkh)\n if (!isSendMax) {\n outputTypesCount[AddressType.p2wpkh] = (outputTypesCount[AddressType.p2wpkh] || 0) + 1;\n }\n\n // Prepare the output data map for consumption by the txSizer\n const outputsData = Object.entries(outputTypesCount).reduce(\n (acc, [type, count]) => {\n acc[type + '_output_count'] = count;\n return acc;\n },\n {} as Record<string, number>\n );\n\n const txSizer = new BtcSizeFeeEstimator();\n const sizeInfo = txSizer.calcTxSize({\n input_script: 'p2wpkh',\n input_count: inputLength,\n ...outputsData,\n });\n\n return sizeInfo;\n}\ninterface GetSpendableAmountArgs<T> {\n utxos: T[];\n feeRate: number;\n recipients: CoinSelectionRecipient[];\n isSendMax?: boolean;\n}\nexport function getSpendableAmount<T extends { value: number }>({\n utxos,\n feeRate,\n recipients,\n}: GetSpendableAmountArgs<T>) {\n const balance = utxos.map(utxo => utxo.value).reduce((prevVal, curVal) => prevVal + curVal, 0);\n\n const size = getSizeInfo({\n inputLength: utxos.length,\n recipients,\n });\n const fee = Math.ceil(size.txVBytes * feeRate);\n const bigNumberBalance = BigNumber(balance);\n return {\n spendableAmount: BigNumber.max(0, bigNumberBalance.minus(fee)),\n fee,\n };\n}\n\n// Check if the spendable amount drops when adding a utxo\nexport function filterUneconomicalUtxos<T extends { value: number; txid: string }>({\n utxos,\n feeRate,\n recipients,\n}: {\n utxos: T[];\n feeRate: number;\n recipients: CoinSelectionRecipient[];\n}) {\n const { spendableAmount: fullSpendableAmount } = getSpendableAmount({\n utxos,\n feeRate,\n recipients,\n });\n\n const filteredUtxos = utxos\n .filter(utxo => Number(utxo.value) >= BTC_P2WPKH_DUST_AMOUNT)\n .filter(utxo => {\n // Calculate spendableAmount without that utxo\n const { spendableAmount } = getSpendableAmount({\n utxos: utxos.filter(u => u.txid !== utxo.txid),\n feeRate,\n recipients,\n });\n // If fullSpendableAmount is greater, do not use utxo\n return spendableAmount.toNumber() < fullSpendableAmount.toNumber();\n });\n return filteredUtxos;\n}\n","// https://github.com/argvil19/bitcoin-transaction-size-calculator/blob/master/index.js\nimport BigNumber from 'bignumber.js';\n\nimport { assertUnreachable } from '@leather.io/utils';\n\nexport type InputScriptType =\n | 'p2pkh'\n | 'p2sh'\n | 'p2sh-p2wpkh'\n | 'p2sh-p2wsh'\n | 'p2wpkh'\n | 'p2wsh'\n | 'p2tr';\n\nexport interface TxSizerParams {\n input_count: number;\n input_script: InputScriptType;\n input_m: number;\n input_n: number;\n p2pkh_output_count: number;\n p2sh_output_count: number;\n p2sh_p2wpkh_output_count: number;\n p2sh_p2wsh_output_count: number;\n p2wpkh_output_count: number;\n p2wsh_output_count: number;\n p2tr_output_count: number;\n}\n\nexport class BtcSizeFeeEstimator {\n P2PKH_IN_SIZE = 148;\n P2PKH_OUT_SIZE = 34;\n P2SH_OUT_SIZE = 32;\n P2SH_P2WPKH_OUT_SIZE = 32;\n P2SH_P2WSH_OUT_SIZE = 32;\n P2SH_P2WPKH_IN_SIZE = 91;\n P2WPKH_IN_SIZE = 67.75;\n P2WPKH_OUT_SIZE = 31;\n P2WSH_OUT_SIZE = 43;\n P2TR_OUT_SIZE = 43;\n P2TR_IN_SIZE = 57.25;\n PUBKEY_SIZE = 33;\n SIGNATURE_SIZE = 72;\n SUPPORTED_INPUT_SCRIPT_TYPES: InputScriptType[] = [\n 'p2pkh',\n 'p2sh',\n 'p2sh-p2wpkh',\n 'p2sh-p2wsh',\n 'p2wpkh',\n 'p2wsh',\n 'p2tr',\n ];\n\n defaultParams: TxSizerParams = {\n input_count: 0,\n input_script: 'p2wpkh',\n input_m: 0,\n input_n: 0,\n p2pkh_output_count: 0,\n p2sh_output_count: 0,\n p2sh_p2wpkh_output_count: 0,\n p2sh_p2wsh_output_count: 0,\n p2wpkh_output_count: 0,\n p2wsh_output_count: 0,\n p2tr_output_count: 0,\n };\n\n params: TxSizerParams = { ...this.defaultParams };\n\n getSizeOfScriptLengthElement(length: number) {\n if (length < 75) {\n return 1;\n } else if (length <= 255) {\n return 2;\n } else if (length <= 65535) {\n return 3;\n } else if (length <= 4294967295) {\n return 5;\n } else {\n throw new Error('Size of redeem script is too large');\n }\n }\n\n getSizeOfletInt(length: number) {\n if (length < 253) {\n return 1;\n } else if (length < 65535) {\n return 3;\n } else if (length < 4294967295) {\n return 5;\n } else if (new BigNumber(length).isLessThan('18446744073709551615')) {\n return 9;\n } else {\n throw new Error('Invalid let int');\n }\n }\n\n getTxOverheadVBytes(input_script: InputScriptType, input_count: number, output_count: number) {\n let witness_vbytes;\n if (input_script === 'p2pkh' || input_script === 'p2sh') {\n witness_vbytes = 0;\n } else {\n // Transactions with segwit inputs have extra overhead\n witness_vbytes =\n 0.25 + // segwit marker\n 0.25 + // segwit flag\n this.getSizeOfletInt(input_count) / 4; // witness element count\n }\n\n return (\n 4 + // nVersion\n this.getSizeOfletInt(input_count) + // number of inputs\n this.getSizeOfletInt(output_count) + // number of outputs\n 4 + // nLockTime\n witness_vbytes\n );\n }\n\n getTxOverheadExtraRawBytes(input_script: InputScriptType, input_count: number) {\n let witness_vbytes;\n if (input_script === 'p2pkh' || input_script === 'p2sh') {\n witness_vbytes = 0;\n } else {\n // Transactions with segwit inputs have extra overhead\n witness_vbytes =\n 0.25 + // segwit marker\n 0.25 + // segwit flag\n this.getSizeOfletInt(input_count) / 4; // witness element count\n }\n\n return witness_vbytes * 3;\n }\n\n prepareParams(opts: Partial<TxSizerParams>) {\n // Verify opts and set them to this.params\n opts = opts || Object.assign(this.defaultParams);\n\n const input_count = opts.input_count || this.defaultParams.input_count;\n if (!Number.isInteger(input_count) || input_count < 0) {\n throw new Error('expecting positive input count, got: ' + input_count);\n }\n\n const input_script = opts.input_script || this.defaultParams.input_script;\n if (this.SUPPORTED_INPUT_SCRIPT_TYPES.indexOf(input_script) === -1) {\n throw new Error('Not supported input script type');\n }\n\n const input_m = opts.input_m || this.defaultParams.input_m;\n if (!Number.isInteger(input_m) || input_m < 0) {\n throw new Error('expecting positive signature count');\n }\n\n const input_n = opts.input_n || this.defaultParams.input_n;\n if (!Number.isInteger(input_n) || input_n < 0) {\n throw new Error('expecting positive pubkey count');\n }\n\n const p2pkh_output_count = opts.p2pkh_output_count || this.defaultParams.p2pkh_output_count;\n if (!Number.isInteger(p2pkh_output_count) || p2pkh_output_count < 0) {\n throw new Error('expecting positive p2pkh output count');\n }\n\n const p2sh_output_count = opts.p2sh_output_count || this.defaultParams.p2sh_output_count;\n if (!Number.isInteger(p2sh_output_count) || p2sh_output_count < 0) {\n throw new Error('expecting positive p2sh output count');\n }\n\n const p2sh_p2wpkh_output_count =\n opts.p2sh_p2wpkh_output_count || this.defaultParams.p2sh_p2wpkh_output_count;\n if (!Number.isInteger(p2sh_p2wpkh_output_count) || p2sh_p2wpkh_output_count < 0) {\n throw new Error('expecting positive p2sh-p2wpkh output count');\n }\n\n const p2sh_p2wsh_output_count =\n opts.p2sh_p2wsh_output_count || this.defaultParams.p2sh_p2wsh_output_count;\n if (!Number.isInteger(p2sh_p2wsh_output_count) || p2sh_p2wsh_output_count < 0) {\n throw new Error('expecting positive p2sh-p2wsh output count');\n }\n\n const p2wpkh_output_count = opts.p2wpkh_output_count || this.defaultParams.p2wpkh_output_count;\n if (!Number.isInteger(p2wpkh_output_count) || p2wpkh_output_count < 0) {\n throw new Error('expecting positive p2wpkh output count');\n }\n\n const p2wsh_output_count = opts.p2wsh_output_count || this.defaultParams.p2wsh_output_count;\n if (!Number.isInteger(p2wsh_output_count) || p2wsh_output_count < 0) {\n throw new Error('expecting positive p2wsh output count');\n }\n\n const p2tr_output_count = opts.p2tr_output_count || this.defaultParams.p2tr_output_count;\n if (!Number.isInteger(p2tr_output_count) || p2tr_output_count < 0) {\n throw new Error('expecting positive p2tr output count');\n }\n\n this.params = {\n input_count,\n input_script,\n input_m,\n input_n,\n p2pkh_output_count,\n p2sh_output_count,\n p2sh_p2wpkh_output_count,\n p2sh_p2wsh_output_count,\n p2wpkh_output_count,\n p2wsh_output_count,\n p2tr_output_count,\n };\n\n return this.params;\n }\n\n getOutputCount() {\n return (\n this.params.p2pkh_output_count +\n this.params.p2sh_output_count +\n this.params.p2sh_p2wpkh_output_count +\n this.params.p2sh_p2wsh_output_count +\n this.params.p2wpkh_output_count +\n this.params.p2wsh_output_count +\n this.params.p2tr_output_count\n );\n }\n\n getSizeBasedOnInputType() {\n // In most cases the input size is predictable. For multisig inputs we need to perform a detailed calculation\n let inputSize = 0; // in virtual bytes\n let inputWitnessSize = 0;\n let redeemScriptSize;\n switch (this.params.input_script) {\n case 'p2pkh':\n inputSize = this.P2PKH_IN_SIZE;\n break;\n case 'p2sh-p2wpkh':\n inputSize = this.P2SH_P2WPKH_IN_SIZE;\n inputWitnessSize = 107; // size(signature) + signature + size(pubkey) + pubkey\n break;\n case 'p2wpkh':\n inputSize = this.P2WPKH_IN_SIZE;\n inputWitnessSize = 107; // size(signature) + signature + size(pubkey) + pubkey\n break;\n case 'p2tr': // Only consider the cooperative taproot signing path assume multisig is done via aggregate signatures\n inputSize = this.P2TR_IN_SIZE;\n inputWitnessSize = 65; // getSizeOfletInt(schnorrSignature) + schnorrSignature\n break;\n case 'p2sh':\n redeemScriptSize =\n 1 + // OP_M\n this.params.input_n * (1 + this.PUBKEY_SIZE) + // OP_PUSH33 <pubkey>\n 1 + // OP_N\n 1; // OP_CHECKMULTISIG\n // eslint-disable-next-line no-case-declarations\n const scriptSigSize =\n 1 + // size(0)\n this.params.input_m * (1 + this.SIGNATURE_SIZE) + // size(SIGNATURE_SIZE) + signature\n this.getSizeOfScriptLengthElement(redeemScriptSize) +\n redeemScriptSize;\n inputSize = 32 + 4 + this.getSizeOfletInt(scriptSigSize) + scriptSigSize + 4;\n break;\n case 'p2sh-p2wsh':\n case 'p2wsh':\n redeemScriptSize =\n 1 + // OP_M\n this.params.input_n * (1 + this.PUBKEY_SIZE) + // OP_PUSH33 <pubkey>\n 1 + // OP_N\n 1; // OP_CHECKMULTISIG\n inputWitnessSize =\n 1 + // size(0)\n this.params.input_m * (1 + this.SIGNATURE_SIZE) + // size(SIGNATURE_SIZE) + signature\n this.getSizeOfScriptLengthElement(redeemScriptSize) +\n redeemScriptSize;\n inputSize =\n 36 + // outpoint (spent UTXO ID)\n inputWitnessSize / 4 + // witness program\n 4; // nSequence\n if (this.params.input_script === 'p2sh-p2wsh') {\n inputSize += 32 + 3; // P2SH wrapper (redeemscript hash) + overhead?\n }\n break;\n default:\n assertUnreachable(this.params.input_script);\n }\n\n return {\n inputSize,\n inputWitnessSize,\n };\n }\n\n calcTxSize(opts: Partial<TxSizerParams>) {\n this.prepareParams(opts);\n const output_count = this.getOutputCount();\n const { inputSize, inputWitnessSize } = this.getSizeBasedOnInputType();\n\n const txVBytes =\n this.getTxOverheadVBytes(this.params.input_script, this.params.input_count, output_count) +\n inputSize * this.params.input_count +\n this.P2PKH_OUT_SIZE * this.params.p2pkh_output_count +\n this.P2SH_OUT_SIZE * this.params.p2sh_output_count +\n this.P2SH_P2WPKH_OUT_SIZE * this.params.p2sh_p2wpkh_output_count +\n this.P2SH_P2WSH_OUT_SIZE * this.params.p2sh_p2wsh_output_count +\n this.P2WPKH_OUT_SIZE * this.params.p2wpkh_output_count +\n this.P2WSH_OUT_SIZE * this.params.p2wsh_output_count +\n this.P2TR_OUT_SIZE * this.params.p2tr_output_count;\n\n const txBytes =\n this.getTxOverheadExtraRawBytes(this.params.input_script, this.params.input_count) +\n txVBytes +\n inputWitnessSize * this.params.input_count;\n const txWeight = txVBytes * 4;\n\n return { txVBytes, txBytes, txWeight };\n }\n\n estimateFee(vbyte: number, satVb: number) {\n if (isNaN(vbyte) || isNaN(satVb)) {\n throw new Error('Parameters should be numbers');\n }\n return vbyte * satVb;\n }\n\n formatFeeRange(fee: number, multiplier: number) {\n if (isNaN(fee) || isNaN(multiplier)) {\n throw new Error('Parameters should be numbers');\n }\n\n if (multiplier < 0) {\n throw new Error('Multiplier cant be negative');\n }\n\n const multipliedFee = fee * multiplier;\n\n return fee - multipliedFee + ' - ' + (fee + multipliedFee);\n }\n}\n","import BigNumber from 'bignumber.js';\nimport { validate } from 'bitcoin-address-validation';\n\nimport { BTC_P2WPKH_DUST_AMOUNT } from '@leather.io/constants';\nimport { Money } from '@leather.io/models';\nimport { createMoney, sumMoney } from '@leather.io/utils';\n\nimport { BitcoinError } from '../validation/bitcoin-error';\nimport { filterUneconomicalUtxos, getSizeInfo, getUtxoTotal } from './coin-selection.utils';\n\nexport interface CoinSelectionOutput {\n value: bigint;\n address?: string;\n}\n\nexport interface CoinSelectionRecipient {\n address: string;\n amount: Money;\n}\n\nexport interface DetermineUtxosForSpendArgs<T> {\n feeRate: number;\n recipients: CoinSelectionRecipient[];\n utxos: T[];\n}\nexport function determineUtxosForSpendAll<T extends { value: number; txid: string }>({\n feeRate,\n recipients,\n utxos,\n}: DetermineUtxosForSpendArgs<T>) {\n recipients.forEach(recipient => {\n if (!validate(recipient.address)) throw new BitcoinError('InvalidAddress');\n });\n const filteredUtxos = filterUneconomicalUtxos({ utxos, feeRate, recipients });\n\n const sizeInfo = getSizeInfo({\n inputLength: filteredUtxos.length,\n isSendMax: true,\n recipients,\n });\n\n // Fee has already been deducted from the amount with send all\n const outputs = recipients.map(({ address, amount }) => ({\n value: BigInt(amount.amount.toNumber()),\n address,\n }));\n\n const fee = Math.ceil(sizeInfo.txVBytes * feeRate);\n\n return {\n inputs: filteredUtxos,\n outputs,\n size: sizeInfo.txVBytes,\n fee: createMoney(new BigNumber(fee), 'BTC'),\n };\n}\n\nexport function determineUtxosForSpend<T extends { value: number; txid: string }>({\n feeRate,\n recipients,\n utxos,\n}: DetermineUtxosForSpendArgs<T>) {\n recipients.forEach(recipient => {\n if (!validate(recipient.address)) throw new BitcoinError('InvalidAddress');\n });\n const filteredUtxos = filterUneconomicalUtxos({\n utxos: utxos.sort((a, b) => b.value - a.value),\n feeRate,\n recipients,\n });\n if (!filteredUtxos.length) throw new BitcoinError('InsufficientFunds');\n\n const amount = sumMoney(recipients.map(recipient => recipient.amount));\n\n // Prepopulate with first utxo, at least one is needed\n const neededUtxos: T[] = [filteredUtxos[0]];\n\n function estimateTransactionSize() {\n return getSizeInfo({\n inputLength: neededUtxos.length,\n recipients,\n });\n }\n\n function hasSufficientUtxosForTx() {\n const txEstimation = estimateTransactionSize();\n const neededAmount = new BigNumber(txEstimation.txVBytes * feeRate).plus(amount.amount);\n return getUtxoTotal(neededUtxos).isGreaterThanOrEqualTo(neededAmount);\n }\n\n function getRemainingUnspentUtxos() {\n return filteredUtxos.filter(utxo => !neededUtxos.includes(utxo));\n }\n\n while (!hasSufficientUtxosForTx()) {\n const [nextUtxo] = getRemainingUnspentUtxos();\n if (!nextUtxo) throw new BitcoinError('InsufficientFunds');\n neededUtxos.push(nextUtxo);\n }\n\n const fee = Math.ceil(\n new BigNumber(estimateTransactionSize().txVBytes).multipliedBy(feeRate).toNumber()\n );\n\n const changeAmount =\n BigInt(getUtxoTotal(neededUtxos).toString()) - BigInt(amount.amount.toNumber()) - BigInt(fee);\n\n const changeUtxos: CoinSelectionOutput[] =\n changeAmount > BTC_P2WPKH_DUST_AMOUNT\n ? [\n {\n value: changeAmount,\n },\n ]\n : [];\n\n const outputs: CoinSelectionOutput[] = [\n ...recipients.map(({ address, amount }) => ({\n value: BigInt(amount.amount.toNumber()),\n address,\n })),\n ...changeUtxos,\n ];\n\n return {\n filteredUtxos,\n inputs: neededUtxos,\n outputs,\n size: estimateTransactionSize().txVBytes,\n fee: createMoney(new BigNumber(fee), 'BTC'),\n ...estimateTransactionSize(),\n };\n}\n","import { AverageBitcoinFeeRates, Money } from '@leather.io/models';\n\nimport {\n CoinSelectionRecipient,\n DetermineUtxosForSpendArgs,\n determineUtxosForSpend,\n determineUtxosForSpendAll,\n} from '../coin-selection/coin-selection';\n\ntype GetBitcoinTransactionFeeArgs = DetermineUtxosForSpendArgs<{ value: number; txid: string }> & {\n isSendingMax?: boolean;\n};\n\nexport function getBitcoinTransactionFee({ isSendingMax, ...props }: GetBitcoinTransactionFeeArgs) {\n try {\n const { fee } = isSendingMax\n ? determineUtxosForSpendAll({ ...props })\n : determineUtxosForSpend({ ...props });\n return fee;\n } catch {\n return null;\n }\n}\n\nexport interface BitcoinFees {\n blockchain: 'bitcoin';\n high: { fee: Money | null; feeRate: number };\n standard: { fee: Money | null; feeRate: number };\n low: { fee: Money | null; feeRate: number };\n}\n\nexport interface GetBitcoinFeesArgs {\n feeRates: AverageBitcoinFeeRates;\n isSendingMax?: boolean;\n recipients: CoinSelectionRecipient[];\n utxos: { value: number; txid: string }[];\n}\nexport function getBitcoinFees({ feeRates, isSendingMax, recipients, utxos }: GetBitcoinFeesArgs) {\n const defaultArgs = {\n isSendingMax,\n recipients,\n utxos,\n };\n\n const highFeeRate = feeRates.fastestFee.toNumber();\n const standardFeeRate = feeRates.halfHourFee.toNumber();\n const lowFeeRate = feeRates.hourFee.toNumber();\n\n const highFeeValue = getBitcoinTransactionFee({\n ...defaultArgs,\n feeRate: highFeeRate,\n });\n const standardFeeValue = getBitcoinTransactionFee({\n ...defaultArgs,\n feeRate: standardFeeRate,\n });\n const lowFeeValue = getBitcoinTransactionFee({\n ...defaultArgs,\n feeRate: lowFeeRate,\n });\n\n return {\n high: { feeRate: highFeeRate, fee: highFeeValue },\n standard: { feeRate: standardFeeRate, fee: standardFeeValue },\n low: { feeRate: lowFeeRate, fee: lowFeeValue },\n };\n}\n","import { createBitcoinAddress } from '../validation/bitcoin-address';\n\n// maybe these should be in mono/config?\n// from extension/tests/mocks/constants\nexport const TEST_ACCOUNT_1_NATIVE_SEGWIT_ADDRESS = createBitcoinAddress(\n 'bc1q530dz4h80kwlzywlhx2qn0k6vdtftd93c499yq'\n);\nexport const TEST_ACCOUNT_1_TAPROOT_ADDRESS = createBitcoinAddress(\n 'bc1putuzj9lyfcm8fef9jpy85nmh33cxuq9u6wyuk536t9kemdk37yjqmkc0pg'\n);\nexport const TEST_ACCOUNT_2_TAPROOT_ADDRESS = createBitcoinAddress(\n 'bc1pmk2sacpfyy4v5phl8tq6eggu4e8laztep7fsgkkx0nc6m9vydjesaw0g2r'\n);\n\nexport const TEST_TESNET_ACCOUNT_1_NATIVE_SEGWIT_ADDRESS = createBitcoinAddress(\n 'tb1q4qgnjewwun2llgken94zqjrx5kpqqycaz5522d'\n);\n\nexport const TEST_TESTNET_ACCOUNT_2_BTC_ADDRESS = createBitcoinAddress(\n 'tb1qr8me8t9gu9g6fu926ry5v44yp0wyljrespjtnz'\n);\n\nexport const TEST_TESTNET_ACCOUNT_2_TAPROOT_ADDRESS = createBitcoinAddress(\n 'tb1pve00jmp43whpqj2wpcxtc7m8wqhz0azq689y4r7h8tmj8ltaj87qj2nj6w'\n);\n\n// coin-selection.spec\nexport const recipientAddress = createBitcoinAddress('tb1qt28eagxcl9gvhq2rpj5slg7dwgxae2dn2hk93m');\nexport const legacyAddress = createBitcoinAddress('15PyZveQd28E2SHZu2ugkWZBp6iER41vXj');\nexport const segwitAddress = createBitcoinAddress('33SVjoCHJovrXxjDKLFSXo1h3t5KgkPzfH');\nexport const taprootAddress = createBitcoinAddress(\n 'tb1parwmj7533de3k2fw2kntyqacspvhm67qnjcmpqnnpfvzu05l69nsczdywd'\n);\nexport const invalidAddress = 'whoop-de-da-boop-da-de-not-a-bitcoin-address';\n\nexport const inValidCharactersAddress = createBitcoinAddress(\n 'tb1&*%wmj7533de3k2fw2kntyqacspvhm67qnjcmpqnnpfvzu05l69nsczdywd'\n);\nexport const inValidLengthAddress = createBitcoinAddress('tb1parwmj7533de3k2fw2kntyqacspvhm67wd');\n","import { Network, getAddressInfo, validate } from 'bitcoin-address-validation';\nimport { z } from 'zod';\n\nimport type { BitcoinNetworkModes } from '@leather.io/models';\nimport { isEmptyString, isUndefined } from '@leather.io/utils';\n\nexport function nonEmptyStringValidator(message = '') {\n return z\n .string()\n .refine((value: string) => value !== undefined && value.trim() !== '', { message });\n}\n\nexport function btcAddressValidator() {\n return z.string().refine(\n (value: string) => {\n if (isUndefined(value) || isEmptyString(value)) return true;\n return validate(value);\n },\n { message: 'Bitcoin address is not valid' }\n );\n}\n\nexport function getNetworkTypeFromAddress(address: string) {\n return getAddressInfo(address).network as BitcoinNetworkModes;\n}\n\nfunction btcAddressNetworkValidatorFactory(network: BitcoinNetworkModes) {\n function getAddressNetworkType(network: BitcoinNetworkModes): Network {\n // Signet uses testnet address format, this parsing is to please the\n // validation library\n if (network === 'signet') return Network.testnet;\n return network as Network;\n }\n return (value?: string) => {\n if (isUndefined(value) || isEmptyString(value)) return true;\n return validate(value, getAddressNetworkType(network));\n };\n}\n\nexport function btcAddressNetworkValidator(network: BitcoinNetworkModes) {\n return z.string().refine(btcAddressNetworkValidatorFactory(network), {\n message: 'Address is for incorrect network',\n });\n}\n","import { ripemd160 } from '@noble/hashes/ripemd160';\nimport { sha256 } from '@noble/hashes/sha256';\nimport { base58check } from '@scure/base';\n\nimport { deriveBip39SeedFromMnemonic, deriveRootBip32Keychain } from '@leather.io/crypto';\nimport { NetworkModes } from '@leather.io/models';\n\n/**\n * @deprecated\n * Use `deriveBip39MnemonicFromSeed` from `@leather.io/crypto`\n */\nexport const deriveBtcBip49SeedFromMnemonic = deriveBip39SeedFromMnemonic;\n\n/**\n * @deprecated\n * Use `deriveRootBip32Keychain` from `@leather.io/crypto`\n */\nexport const deriveRootBtcKeychain = deriveRootBip32Keychain;\n\nexport function decodeCompressedWifPrivateKey(key: string) {\n // https://en.bitcoinwiki.org/wiki/Wallet_import_format\n // Decode Compressed WIF format private key\n const compressedWifFormatPrivateKey = base58check(sha256).decode(key);\n // Drop leading network byte, trailing public key SEC format byte\n return compressedWifFormatPrivateKey.slice(1, compressedWifFormatPrivateKey.length - 1);\n}\n\n// https://en.bitcoin.it/wiki/List_of_address_prefixes\nconst payToScriptHashMainnetPrefix = 0x05;\nexport const payToScriptHashTestnetPrefix = 0xc4;\n\nconst payToScriptHashPrefixMap: Record<NetworkModes, number> = {\n mainnet: payToScriptHashMainnetPrefix,\n testnet: payToScriptHashTestnetPrefix,\n};\n\nfunction hash160(input: Uint8Array) {\n return ripemd160(sha256(input));\n}\n\nexport function makePayToScriptHashKeyHash(publicKey: Uint8Array) {\n return hash160(publicKey);\n}\n\nexport function makePayToScriptHashAddressBytes(keyHash: Uint8Array) {\n const redeemScript = Uint8Array.from([\n ...Uint8Array.of(0x00),\n ...Uint8Array.of(keyHash.length),\n ...keyHash,\n ]);\n return hash160(redeemScript);\n}\n\nexport function makePayToScriptHashAddress(addressBytes: Uint8Array, network: NetworkModes) {\n const networkByte = payToScriptHashPrefixMap[network];\n const addressWithPrefix = Uint8Array.from([networkByte, ...addressBytes]);\n return base58check(sha256).encode(addressWithPrefix);\n}\n\nexport function publicKeyToPayToScriptHashAddress(publicKey: Uint8Array, network: NetworkModes) {\n const hash = makePayToScriptHashKeyHash(publicKey);\n const addrBytes = makePayToScriptHashAddressBytes(hash);\n return makePayToScriptHashAddress(addrBytes, network);\n}\n","import { BitcoinAddress } from '@leather.io/models';\nimport { createMoney, sumNumbers } from '@leather.io/utils';\n\nimport { inferPaymentTypeFromAddress } from '../utils/bitcoin.utils';\nimport { PsbtInput } from './psbt-inputs';\nimport { PsbtOutput } from './psbt-outputs';\n\nfunction calculateAddressInputsTotal(addresses: string[], inputs: PsbtInput[]) {\n const sumsByAddress = addresses.map(address =>\n inputs\n .filter(input => input.address === address)\n .map(input => input.value)\n .reduce((acc, curVal) => acc + curVal, 0)\n );\n\n return createMoney(sumNumbers(sumsByAddress), 'BTC');\n}\n\nfunction calculateAddressOutputsTotal(addresses: string[], outputs: PsbtOutput[]) {\n const sumsByAddress = addresses.map(address =>\n outputs\n .filter(output => output.address === address)\n .map(output => Number(output.value))\n .reduce((acc, curVal) => acc + curVal, 0)\n );\n return createMoney(sumNumbers(sumsByAddress), 'BTC');\n}\n\nfunction calculatePsbtInputsTotal(inputs: PsbtInput[]) {\n return createMoney(sumNumbers(inputs.map(input => input.value)), 'BTC');\n}\n\nfunction calculatePsbtOutputsTotal(outputs: PsbtOutput[]) {\n return createMoney(sumNumbers(outputs.map(output => output.value)), 'BTC');\n}\n\ninterface GetPsbtTotalsProps {\n psbtAddresses: BitcoinAddress[];\n parsedInputs: PsbtInput[];\n parsedOutputs: PsbtOutput[];\n}\nexport function getPsbtTotals({ psbtAddresses, parsedInputs, parsedOutputs }: GetPsbtTotalsProps) {\n const nativeSegwitAddresses = psbtAddresses.filter(\n addr => inferPaymentTypeFromAddress(addr) === 'p2wpkh'\n );\n const taprootAddresses = psbtAddresses.filter(\n addr => inferPaymentTypeFromAddress(addr) === 'p2tr'\n );\n\n return {\n inputsTotalNativeSegwit: calculateAddressInputsTotal(nativeSegwitAddresses, parsedInputs),\n inputsTotalTaproot: calculateAddressInputsTotal(taprootAddresses, parsedInputs),\n outputsTotalNativeSegwit: calculateAddressOutputsTotal(nativeSegwitAddresses, parsedOutputs),\n outputsTotalTaproot: calculateAddressOutputsTotal(taprootAddresses, parsedOutputs),\n psbtInputsTotal: calculatePsbtInputsTotal(parsedInputs),\n psbtOutputsTotal: calculatePsbtOutputsTotal(parsedOutputs),\n };\n}\n","import { bytesToHex } from '@noble/hashes/utils';\nimport type { TransactionInput } from '@scure/btc-signer/psbt';\n\nimport type { BitcoinAddress, BitcoinNetworkModes, Inscription } from '@leather.io/models';\nimport { isDefined, isUndefined } from '@leather.io/utils';\n\nimport { getBtcSignerLibNetworkConfigByMode } from '../utils/bitcoin.network';\nimport { getBitcoinInputAddress, getBitcoinInputValue } from '../utils/bitcoin.utils';\n\nexport interface PsbtInput {\n address: BitcoinAddress;\n index?: number;\n // TODO: inject inscription later on. getParsedInputs should be a pure function\n inscription?: Inscription;\n isMutable: boolean;\n toSign: boolean;\n txid: string;\n value: number;\n bip32Derivation: TransactionInput['bip32Derivation'];\n tapBip32Derivation: TransactionInput['tapBip32Derivation'];\n}\n\ninterface GetParsedInputsArgs {\n inputs: TransactionInput[];\n indexesToSign?: number[];\n networkMode: BitcoinNetworkModes;\n psbtAddresses: BitcoinAddress[];\n}\n\ninterface GetParsedInputsResponse {\n isPsbtMutable: boolean;\n parsedInputs: PsbtInput[];\n}\nexport function getParsedInputs({\n inputs,\n indexesToSign,\n networkMode,\n psbtAddresses,\n}: GetParsedInputsArgs): GetParsedInputsResponse {\n const bitcoinNetwork = getBtcSignerLibNetworkConfigByMode(networkMode);\n\n const signAll = isUndefined(indexesToSign);\n const psbtInputs = inputs.map((input, i) => {\n const bitcoinAddress = isDefined(input.index)\n ? getBitcoinInputAddress(input, bitcoinNetwork)\n : null;\n if (bitcoinAddress === null) {\n throw new Error('PSBT input has unsupported bitcoin address');\n }\n const isCurrentAddress = psbtAddresses.includes(bitcoinAddress);\n // Flags when not signing ALL inputs/outputs (NONE, SINGLE, and ANYONECANPAY)\n const canChange =\n isCurrentAddress &&\n !(!input.sighashType || input.sighashType === 0 || input.sighashType === 1);\n // Should we check the sighashType here before it gets to the signing lib?\n const toSignAll = isCurrentAddress && signAll;\n const toSignIndex = isCurrentAddress && !signAll && indexesToSign.includes(i);\n\n return {\n address: bitcoinAddress,\n index: input.index,\n bip32Derivation: input.bip32Derivation,\n tapBip32Derivation: input.tapBip32Derivation,\n // inscription: inscriptions[i],\n isMutable: canChange,\n toSign: toSignAll || toSignIndex,\n txid: input.txid ? bytesToHex(input.txid) : '',\n value: isDefined(input.index) ? getBitcoinInputValue(input) : 0,\n };\n });\n\n const isPsbtMutable = psbtInputs.some(input => input.isMutable);\n\n return { isPsbtMutable, parsedInputs: psbtInputs };\n}\n","import type { TransactionOutput } from '@scure/btc-signer/psbt';\n\nimport { BitcoinAddress, BitcoinNetworkModes } from '@leather.io/models';\nimport { isDefined, isUndefined } from '@leather.io/utils';\n\nimport { getBtcSignerLibNetworkConfigByMode } from '../utils/bitcoin.network';\nimport { getAddressFromOutScript } from '../utils/bitcoin.utils';\n\nexport interface PsbtOutput {\n address: BitcoinAddress;\n isMutable: boolean;\n toSign: boolean;\n value: number;\n}\n\ninterface GetParsedOutputsArgs {\n isPsbtMutable: boolean;\n outputs: TransactionOutput[];\n networkMode: BitcoinNetworkModes;\n psbtAddresses: BitcoinAddress[];\n}\n\nexport function getParsedOutputs({\n isPsbtMutable,\n outputs,\n networkMode,\n psbtAddresses,\n}: GetParsedOutputsArgs): PsbtOutput[] {\n const bitcoinNetwork = getBtcSignerLibNetworkConfigByMode(networkMode);\n\n return outputs\n .map(output => {\n if (isUndefined(output.script)) {\n // TODO: handle error here\n // logger.error('Output has no script');\n return;\n }\n const outputAddress = getAddressFromOutScript(output.script, bitcoinNetwork);\n if (outputAddress === null) {\n throw new Error('PSBT output has unsupported bitcoin address');\n }\n\n const isCurrentAddress = psbtAddresses.includes(outputAddress);\n\n return {\n address: outputAddress,\n isMutable: isPsbtMutable,\n toSign: isCurrentAddress,\n value: Number(output.amount),\n };\n })\n .filter(isDefined);\n}\n","import { BitcoinAddress, BitcoinNetworkModes } from '@leather.io/models';\nimport { createMoney, subtractMoney } from '@leather.io/utils';\n\nimport { getPsbtTxInputs, getPsbtTxOutputs } from '../utils/bitcoin.utils';\nimport { getParsedInputs } from './psbt-inputs';\nimport { getParsedOutputs } from './psbt-outputs';\nimport { getPsbtTotals } from './psbt-totals';\nimport { getPsbtAsTransaction } from './utils';\n\ninterface GetPsbtDetailsArgs {\n psbtHex: string;\n psbtAddresses: BitcoinAddress[];\n networkMode: BitcoinNetworkModes;\n indexesToSign?: number[];\n}\nexport function getPsbtDetails({\n psbtHex,\n networkMode,\n indexesToSign,\n psbtAddresses,\n}: GetPsbtDetailsArgs) {\n const tx = getPsbtAsTransaction(psbtHex);\n const inputs = getPsbtTxInputs(tx);\n const outputs = getPsbtTxOutputs(tx);\n\n const { isPsbtMutable, parsedInputs } = getParsedInputs({\n inputs,\n indexesToSign,\n networkMode,\n psbtAddresses,\n });\n const parsedOutputs = getParsedOutputs({ isPsbtMutable, outputs, networkMode, psbtAddresses });\n\n const {\n inputsTotalNativeSegwit,\n inputsTotalTaproot,\n outputsTotalNativeSegwit,\n outputsTotalTaproot,\n psbtInputsTotal,\n psbtOutputsTotal,\n } = getPsbtTotals({\n psbtAddresses,\n parsedInputs,\n parsedOutputs,\n });\n function getFee() {\n if (psbtInputsTotal.amount.isGreaterThan(psbtOutputsTotal.amount))\n return subtractMoney(psbtInputsTotal, psbtOutputsTotal);\n return createMoney(0, 'BTC');\n }\n return {\n addressNativeSegwitTotal: subtractMoney(inputsTotalNativeSegwit, outputsTotalNativeSegwit),\n addressTaprootTotal: subtractMoney(inputsTotalTaproot, outputsTotalTaproot),\n fee: getFee(),\n isPsbtMutable,\n psbtInputs: parsedInputs,\n psbtOutputs: parsedOutputs,\n };\n}\n","import { hexToBytes } from '@noble/hashes/utils';\nimport * as btc from '@scure/btc-signer';\nimport { RawPSBTV0, RawPSBTV2 } from '@scure/btc-signer/psbt';\n\nimport { isString } from '@leather.io/utils';\n\nexport type RawPsbt = ReturnType<typeof RawPSBTV0.decode>;\n\nexport function getPsbtAsTransaction(psbt: string | Uint8Array) {\n const bytes = isString(psbt) ? hexToBytes(psbt) : psbt;\n return btc.Transaction.fromPSBT(bytes);\n}\n\nexport function getRawPsbt(psbt: string | Uint8Array): ReturnType<typeof RawPSBTV0.decode> {\n const bytes = isString(psbt) ? hexToBytes(psbt) : psbt;\n try {\n return RawPSBTV0.decode(bytes);\n } catch (e1) {\n try {\n return RawPSBTV2.decode(bytes);\n } catch (e2) {\n throw new Error(`Unable to decode PSBT, ${e1 ?? e2}`);\n }\n }\n}\n","import { HARDENED_OFFSET, HDKey } from '@scure/bip32';\nimport * as btc from '@scure/btc-signer';\nimport { P2Ret, P2TROut } from '@scure/btc-signer/payment';\nimport { SigHash } from '@scure/btc-signer/transaction';\nimport * as bitcoin from 'bitcoinjs-lib';\n\nimport {\n DerivationPathDepth,\n appendAddressIndexToPath,\n decomposeDescriptor,\n deriveKeychainFromXpub,\n extractAddressIndexFromPath,\n extractChangeIndexFromPath,\n keyOriginToDerivationPath,\n} from '@leather.io/crypto';\nimport type { BitcoinAddress, BitcoinNetworkModes, ValueOf } from '@leather.io/models';\nimport { PaymentTypes, signatureHash } from '@leather.io/rpc';\nimport { hexToNumber, toHexString } from '@leather.io/utils';\n\nimport { getTaprootPaymentFromAddressIndex } from '../payments/p2tr-address-gen';\nimport { getNativeSegwitPaymentFromAddressIndex } from '../payments/p2wpkh-address-gen';\nimport {\n SupportedPaymentType,\n ecdsaPublicKeyToSchnorr,\n extractExtendedPublicKeyFromPolicy,\n inferPaymentTypeFromPath,\n whenSupportedPaymentType,\n} from '../utils/bitcoin.utils';\n\nexport type AllowedSighashTypes = ValueOf<typeof signatureHash> | SigHash;\n\nexport interface BitcoinAccountKeychain {\n descriptor: string;\n masterKeyFingerprint: string;\n keyOrigin: string;\n keychain: HDKey;\n xpub: string;\n}\n\nexport type WithDerivePayer<T, P> = T & { derivePayer: (args: BitcoinPayerInfo) => P };\n\nexport interface BitcoinSigner<Payment> {\n network: BitcoinNetworkModes;\n payment: Payment;\n keychain: HDKey;\n derivationPath: string;\n address: BitcoinAddress;\n publicKey: Uint8Array;\n sign(tx: btc.Transaction): void;\n signIndex(tx: btc.Transaction, index: number, allowedSighash?: AllowedSighashTypes[]): void;\n}\n\nexport interface BitcoinPayerBase {\n paymentType: PaymentTypes;\n network: BitcoinNetworkModes;\n address: BitcoinAddress;\n keyOrigin: string;\n masterKeyFingerprint: string;\n publicKey: Uint8Array;\n}\n\nexport interface BitcoinNativeSegwitPayer extends BitcoinPayerBase {\n paymentType: 'p2wpkh';\n payment: P2Ret;\n}\n\nexport interface BitcoinTaprootPayer extends BitcoinPayerBase {\n paymentType: 'p2tr';\n payment: P2TROut;\n}\n\nexport type BitcoinPayer = BitcoinNativeSegwitPayer | BitcoinTaprootPayer;\n\nexport function initializeBitcoinAccountKeychainFromDescriptor(\n descriptor: string\n): BitcoinAccountKeychain {\n const { fingerprint, keyOrigin } = decomposeDescriptor(descriptor);\n return {\n descriptor,\n xpub: extractExtendedPublicKeyFromPolicy(descriptor),\n keyOrigin,\n masterKeyFingerprint: fingerprint,\n keychain: deriveKeychainFromXpub(extractExtendedPublicKeyFromPolicy(descriptor)),\n };\n}\n\nexport interface BitcoinPayerInfo {\n change: number;\n addressIndex: number;\n}\nexport function deriveBitcoinPayerFromAccount(descriptor: string, network: BitcoinNetworkModes) {\n const { fingerprint, keyOrigin } = decomposeDescriptor(descriptor);\n const accountKeychain = deriveKeychainFromXpub(extractExtendedPublicKeyFromPolicy(descriptor));\n const paymentType = inferPaymentTypeFromPath(keyOrigin) as SupportedPaymentType;\n\n if (accountKeychain.depth !== DerivationPathDepth.Account)\n throw new Error('Keychain passed is not an account');\n\n return ({ change, addressIndex }: BitcoinPayerInfo) => {\n const childKeychain = accountKeychain.deriveChild(change).deriveChild(addressIndex);\n\n const derivePayerFromAccount = whenSupportedPaymentType(paymentType)({\n p2tr: getTaprootPaymentFromAddressIndex,\n p2wpkh: getNativeSegwitPaymentFromAddressIndex,\n });\n\n const payment = derivePayerFromAccount(childKeychain, network);\n\n return {\n keyOrigin: appendAddressIndexToPath(keyOrigin, change, addressIndex),\n masterKeyFingerprint: fingerprint,\n paymentType,\n network,\n payment,\n get address() {\n if (!payment.address) throw new Error('Payment address could not be derived');\n return payment.address;\n },\n get publicKey() {\n if (!childKeychain.publicKey) throw new Error('Public key could not be derived');\n return childKeychain.publicKey;\n },\n };\n };\n}\n\ninterface BtcSignerDerivationPath {\n fingerprint: number;\n path: number[];\n}\nexport type BtcSignerDefaultBip32Derivation = [Uint8Array, BtcSignerDerivationPath];\nexport type BtcSignerTapBip32Derivation = [\n Uint8Array,\n { hashes: Uint8Array[]; der: BtcSignerDerivationPath },\n];\n\ntype BtcSignerBip32Derivation = BtcSignerDefaultBip32Derivation | BtcSignerTapBip32Derivation;\n\ntype PayerToBip32DerivationArgs = Pick<\n BitcoinPayer,\n 'masterKeyFingerprint' | 'keyOrigin' | 'publicKey'\n>;\n\n/**\n * @example\n * ```ts\n * tx.addInput({\n * ...input,\n * bip32Derivation: [payerToBip32Derivation(payer)],\n * })\n * ```\n */\nexport function payerToBip32Derivation(\n args: PayerToBip32DerivationArgs\n): BtcSignerDefaultBip32Derivation {\n return [\n args.publicKey,\n {\n fingerprint: hexToNumber(args.masterKeyFingerprint),\n path: btc.bip32Path(keyOriginToDerivationPath(args.keyOrigin)),\n },\n ];\n}\n\n/**\n * @example\n * ```ts\n * tx.addInput({\n * ...input,\n * tapBip32Derivation: [payerToTapBip32Derivation(payer)],\n * })\n * ```\n */\nexport function payerToTapBip32Derivation(\n args: PayerToBip32DerivationArgs\n): BtcSignerTapBip32Derivation {\n return [\n // TODO: @kyranjamie to default to schnoor when TR so conversion isn't\n // necessary here?\n ecdsaPublicKeyToSchnorr(args.publicKey),\n {\n hashes: [],\n der: {\n fingerprint: hexToNumber(args.masterKeyFingerprint),\n path: btc.bip32Path(keyOriginToDerivationPath(args.keyOrigin)),\n },\n },\n ];\n}\n\ntype PsbtInputBitcoinJsLib = bitcoin.Psbt['data']['inputs']['0'];\n\ntype TapBip32DerivationBitcoinJsLib = NonNullable<PsbtInputBitcoinJsLib['tapBip32Derivation']>['0'];\n\nexport function payerToTapBip32DerivationBitcoinJsLib(\n args: PayerToBip32DerivationArgs\n): TapBip32DerivationBitcoinJsLib {\n return {\n masterFingerprint: Buffer.from(args.masterKeyFingerprint, 'hex'),\n path: keyOriginToDerivationPath(args.keyOrigin),\n leafHashes: [],\n pubkey: Buffer.from(ecdsaPublicKeyToSchnorr(args.publicKey)),\n };\n}\n\ntype Bip32DerivationBitcoinJsLib = NonNullable<PsbtInputBitcoinJsLib['bip32Derivation']>['0'];\n\nexport function payerToBip32DerivationBitcoinJsLib(\n args: PayerToBip32DerivationArgs\n): Bip32DerivationBitcoinJsLib {\n return {\n masterFingerprint: Buffer.from(args.masterKeyFingerprint, 'hex'),\n path: keyOriginToDerivationPath(args.keyOrigin),\n pubkey: Buffer.from(args.publicKey),\n };\n}\n\nexport function extractPayerInfoFromDerivationPath(path: string) {\n return {\n change: extractChangeIndexFromPath(path),\n addressIndex: extractAddressIndexFromPath(path),\n };\n}\n\n/**\n * @description\n * Turns key format from @scure/btc-signer lib back into key origin string\n * @example\n * ```ts\n * const [inputOne] = getPsbtTxInputs(tx);\n * const keyOrigin = serializeKeyOrigin(inputOne.bip32Derivation[0][1]);\n * ```\n */\nexport function serializeKeyOrigin({ fingerprint, path }: BtcSignerDerivationPath) {\n const values = path.map(num => (num >= HARDENED_OFFSET ? num - HARDENED_OFFSET + \"'\" : num));\n return `${toHexString(fingerprint)}/${values.join('/')}`;\n}\n\n/**\n * @description\n * Of a given set of a `tx.input`s bip32 derivation paths from\n * `@scure/btc-signer`, serialize the paths back to the string format used\n * internally\n */\nexport function extractRequiredKeyOrigins(derivation: BtcSignerBip32Derivation[]) {\n return derivation.map(([_pubkey, path]) =>\n serializeKeyOrigin('hashes' in path ? path.der : path)\n );\n}\n","import * as btc from '@scure/btc-signer';\n\nimport {\n CoinSelectionRecipient,\n determineUtxosForSpend,\n determineUtxosForSpendAll,\n} from '../coin-selection/coin-selection';\nimport {\n BitcoinNativeSegwitPayer,\n BitcoinTaprootPayer,\n payerToBip32Derivation,\n payerToTapBip32Derivation,\n} from '../signer/bitcoin-signer';\nimport { BtcSignerNetwork } from '../utils/bitcoin.network';\nimport { BitcoinError } from '../validation/bitcoin-error';\n\nexport interface GenerateBitcoinUnsignedTransactionArgs<T> {\n feeRate: number;\n isSendingMax?: boolean;\n network: BtcSignerNetwork;\n recipients: CoinSelectionRecipient[];\n utxos: T[];\n changeAddress: string;\n payerLookup(keyOrigin: string): BitcoinNativeSegwitPayer | BitcoinTaprootPayer | undefined;\n}\nexport function generateBitcoinUnsignedTransaction<\n T extends { txid: string; vout: number; value: number; keyOrigin: string },\n>({\n feeRate,\n isSendingMax,\n network,\n recipients,\n changeAddress,\n utxos,\n payerLookup,\n}: GenerateBitcoinUnsignedTransactionArgs<T>) {\n const determineUtxosArgs = { feeRate, recipients, utxos };\n const { inputs, outputs, fee } = isSendingMax\n ? determineUtxosForSpendAll(determineUtxosArgs)\n : determineUtxosForSpend(determineUtxosArgs);\n\n if (!inputs.length) throw new BitcoinError('NoInputsToSign');\n if (!outputs.length) throw new BitcoinError('NoOutputsToSign');\n\n const tx = new btc.Transaction();\n\n for (const input of inputs) {\n const payer = payerLookup(input.keyOrigin);\n\n if (!payer) {\n // eslint-disable-next-line no-console\n console.log(`No payer found for input with keyOrigin ${input.keyOrigin}`);\n continue;\n }\n\n const bip32Derivation =\n payer.paymentType === 'p2tr'\n ? { tapBip32Derivation: [payerToTapBip32Derivation(payer)] }\n : { bip32Derivation: [payerToBip32Derivation(payer)] };\n\n const tapInternalKey =\n payer.paymentType === 'p2tr' ? { tapInternalKey: payer.payment.tapInternalKey } : {};\n\n tx.addInput({\n txid: input.txid,\n index: input.vout,\n witnessUtxo: {\n script: payer.payment.script,\n amount: BigInt(input.value),\n },\n ...bip32Derivation,\n ...tapInternalKey,\n });\n }\n\n outputs.forEach(output => {\n // When coin selection returns an output with no address,\n // we assume it is a change output\n if (!output.address) {\n tx.addOutputAddress(changeAddress, BigInt(output.value), network);\n return;\n }\n tx.addOutputAddress(output.address, BigInt(output.value), network);\n });\n\n return { tx, hex: tx.hex, psbt: tx.toPSBT(), inputs, fee };\n}\n","import { BITCOIN_MINIMUM_SPEND_IN_SATS } from '@leather.io/constants';\nimport { Money } from '@leather.io/models';\n\ninterface isBtcBalanceSufficientParams {\n desiredSpend: Money;\n maxSpend: Money;\n}\nexport function isBtcBalanceSufficient({ desiredSpend, maxSpend }: isBtcBalanceSufficientParams) {\n return !desiredSpend.amount.isGreaterThan(maxSpend.amount);\n}\n\nexport function isBtcMinimumSpend(desiredSpend: Money) {\n return !desiredSpend.amount.isLessThan(BITCOIN_MINIMUM_SPEND_IN_SATS);\n}\n","import { BitcoinAddress, type BitcoinNetworkModes, Money } from '@leather.io/models';\n\nimport { calculateMaxSpend } from '../coin-selection/calculate-max-spend';\nimport { GetBitcoinFeesArgs } from '../fees/bitcoin-fees';\nimport { BitcoinError } from '../validation/bitcoin-error';\nimport { isValidBitcoinAddress, isValidBitcoinNetworkAddress } from './address-validation';\nimport { isBtcBalanceSufficient, isBtcMinimumSpend } from './amount-validation';\n\ninterface BitcoinTransaction extends Omit<GetBitcoinFeesArgs, 'recipients'> {\n amount: Money;\n payer: BitcoinAddress;\n recipient: BitcoinAddress;\n network: BitcoinNetworkModes;\n feeRate: number;\n}\n\nexport function isValidBitcoinTransaction({\n amount,\n payer,\n recipient,\n network,\n utxos,\n feeRate,\n feeRates,\n}: BitcoinTransaction) {\n if (!isValidBitcoinAddress(payer) || !isValidBitcoinAddress(recipient)) {\n throw new BitcoinError('InvalidAddress');\n }\n if (\n !isValidBitcoinNetworkAddress(payer, network) ||\n !isValidBitcoinNetworkAddress(recipient, network)\n ) {\n throw new BitcoinError('InvalidNetworkAddress');\n }\n\n if (!isBtcMinimumSpend(amount)) {\n throw new BitcoinError('InsufficientAmount');\n }\n\n const maxSpend = calculateMaxSpend({ recipient, utxos, feeRate, feeRates });\n if (!isBtcBalanceSufficient({ desiredSpend: amount, maxSpend: maxSpend.amount })) {\n throw new BitcoinError('InsufficientFunds');\n }\n}\n","import { inferPaymentTypeFromPath } from './bitcoin.utils';\n\nexport function getDescriptorFromKeychain<T extends { keyOrigin: string; xpub: string }>(\n accountKeychain: T\n) {\n switch (inferPaymentTypeFromPath(accountKeychain.keyOrigin)) {\n case 'p2tr':\n return `tr(${accountKeychain.xpub})`;\n case 'p2wpkh':\n return `wpkh(${accountKeychain.xpub})`;\n default:\n return undefined;\n }\n}\n","import { HARDENED_OFFSET, HDKey } from '@scure/bip32';\n\nimport { BitcoinAddress } from '@leather.io/models';\nimport { createCounter } from '@leather.io/utils';\n\nimport { makeTaprootAddressIndexDerivationPath } from '../payments/p2tr-address-gen';\nimport { makeNativeSegwitAddressIndexDerivationPath } from '../payments/p2wpkh-address-gen';\nimport {\n getNativeSegwitAddress,\n getTaprootAddress,\n inferNetworkFromAddress,\n inferPaymentTypeFromAddress,\n whenSupportedPaymentType,\n} from './bitcoin.utils';\n\ninterface LookUpDerivationByAddressArgs {\n taprootXpub: string;\n nativeSegwitXpub: string;\n iterationLimit: number;\n}\nexport function lookupDerivationByAddress(args: LookUpDerivationByAddressArgs) {\n const { taprootXpub, nativeSegwitXpub, iterationLimit } = args;\n\n const taprootKeychain = HDKey.fromExtendedKey(taprootXpub);\n const nativeSegwitKeychain = HDKey.fromExtendedKey(nativeSegwitXpub);\n\n return (address: BitcoinAddress) => {\n const network = inferNetworkFromAddress(address);\n const paymentType = inferPaymentTypeFromAddress(address);\n\n const accountIndex = whenSupportedPaymentType(paymentType)({\n p2tr: taprootKeychain.index - HARDENED_OFFSET,\n p2wpkh: nativeSegwitKeychain.index - HARDENED_OFFSET,\n });\n\n function getTaprootAddressAtIndex(index: number) {\n return getTaprootAddress({ index, keychain: taprootKeychain, network });\n }\n\n function getNativeSegwitAddressAtIndex(index: number) {\n return getNativeSegwitAddress({ index, keychain: nativeSegwitKeychain, network });\n }\n\n const paymentFn = whenSupportedPaymentType(paymentType)({\n p2tr: getTaprootAddressAtIndex,\n p2wpkh: getNativeSegwitAddressAtIndex,\n });\n\n const derivationPathFn = whenSupportedPaymentType(paymentType)({\n p2tr: makeTaprootAddressIndexDerivationPath,\n p2wpkh: makeNativeSegwitAddressIndexDerivationPath,\n });\n\n const count = createCounter();\n const t0 = performance.now();\n\n while (count.getValue() <= iterationLimit) {\n const currentIndex = count.getValue();\n\n const addressToCheck = paymentFn(currentIndex);\n\n if (addressToCheck !== address) {\n count.increment();\n continue;\n }\n\n const t1 = performance.now();\n\n return {\n status: 'success',\n duration: t1 - t0,\n path: derivationPathFn(network, accountIndex, currentIndex),\n } as const;\n }\n\n return { status: 'failure' } as const;\n };\n}\n"],"mappings":";AAAA,SAAuB,QAAQ,cAAc;AAE7C,SAAS,eAAe;AAiBjB,IAAM,QAAQ;AAAA,EACnB,QAAQ,CAAC,KAAa,cAAoC;AACxD,QAAI;AACF,YAAM,EAAE,SAAAA,UAAS,QAAQ,IAAI,OAAO,KAAK,SAAS;AAClD,aAAO;AAAA,QACL,SAAS;AAAA,QACT,MAAM,EAAE,SAAAA,UAAS,GAAG,QAAQ;AAAA,MAC9B;AAAA,IACF,SAAS,OAAO;AACd,aAAO;AAAA,QACL,SAAS;AAAA,QACT,OAAO,QAAQ,KAAK,IAAI,MAAM,UAAU;AAAA,MAC1C;AAAA,IACF;AAAA,EACF;AAAA,EACA,QAAQ,CAACA,UAAiB,SAAuB,cAAuB;AACtE,QAAI;AACF,aAAO,OAAOA,UAAS,SAAS,SAAS;AAAA,IAC3C,QAAQ;AACN,aAAO;AAAA,I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AAgB,WAAW;AACzD,QAAM,uBAAuBA,OAAM,gBAAgB,gBAAgB;AAEnE,SAAO,CAACC,aAA4B;AAClC,UAAM,UAAU,wBAAwBA,QAAO;AAC/C,UAAM,cAAc,4BAA4BA,QAAO;AAEvD,UAAM,eAAe,yBAAyB,WAAW,EAAE;AAAA,MACzD,MAAM,gBAAgB,QAAQC;AAAA,MAC9B,QAAQ,qBAAqB,QAAQA;AAAA,IACvC,CAAC;AAED,aAAS,yBAAyB,OAAe;AAC/C,aAAO,kBAAkB,EAAE,OAAO,UAAU,iBAAiB,QAAQ,CAAC;AAAA,IACxE;AAEA,aAAS,8BAA8B,OAAe;AACpD,aAAO,uBAAuB,EAAE,OAAO,UAAU,sBAAsB,QAAQ,CAAC;AAAA,IAClF;AAEA,UAAM,YAAY,yBAAyB,WAAW,EAAE;AAAA,MACtD,MAAM;AAAA,MACN,QAAQ;AAAA,IACV,CAAC;AAED,UAAM,mBAAmB,yBAAyB,WAAW,EAAE;AAAA,MAC7D,MAAM;AAAA,MACN,QAAQ;AAAA,IACV,CAAC;AAED,UAAM,QAAQ,cAAc;AAC5B,UAAM,KAAK,YAAY,IAAI;AAE3B,WAAO,MAAM,SAAS,KAAK,gBAAgB;AACzC,YAAM,eAAe,MAAM,SAAS;AAEpC,YAAM,iBAAiB,UAAU,YAAY;AAE7C,UAAI,mBAAmBD,UAAS;AAC9B,cAAM,UAAU;AAChB;AAAA,MACF;AAEA,YAAM,KAAK,YAAY,IAAI;AAE3B,aAAO;AAAA,QACL,QAAQ;AAAA,QACR,UAAU,KAAK;AAAA,QACf,MAAM,iBAAiB,SAAS,cAAc,YAAY;AAAA,MAC5D;AAAA,IACF;AAEA,WAAO,EAAE,QAAQ,UAAU;AAAA,EAC7B;AACF;","names":["address","hexToBytes","encode","btc","DerivationPathDepth","btc","DerivationPathDepth","DerivationPathDepth","address","DerivationPathDepth","script","address","hexToBytes","encode","bitcoin","address","script","BigNumber","BigNumber","validate","validate","BigNumber","BigNumber","BigNumber","validate","BTC_P2WPKH_DUST_AMOUNT","createMoney","validate","address","createMoney","BigNumber","BTC_P2WPKH_DUST_AMOUNT","amount","Network","getAddressInfo","validate","isEmptyString","isUndefined","address","network","sha256","createMoney","sumNumbers","address","createMoney","sumNumbers","isDefined","isUndefined","isUndefined","isDefined","isDefined","isUndefined","isUndefined","isDefined","createMoney","hexToBytes","btc","isString","createMoney","btc","DerivationPathDepth","DerivationPathDepth","btc","HARDENED_OFFSET","HDKey","HDKey","address","HARDENED_OFFSET"]}
1
+ {"version":3,"sources":["../src/bip21/bip21.ts","../src/bip322/bip322-utils.ts","../src/utils/bitcoin.utils.ts","../src/payments/p2tr-address-gen.ts","../src/utils/bitcoin.network.ts","../src/payments/p2wpkh-address-gen.ts","../src/validation/address-validation.ts","../src/validation/bitcoin-error.ts","../src/validation/bitcoin-address.ts","../src/bip322/sign-message-bip322-bitcoinjs.ts","../src/coin-selection/calculate-max-spend.ts","../src/coin-selection/coin-selection.utils.ts","../src/fees/btc-size-fee-estimator.ts","../src/coin-selection/coin-selection.ts","../src/fees/bitcoin-fees.ts","../src/mocks/mocks.ts","../src/schemas/address-schema.ts","../src/payments/p2wsh-p2sh-address-gen.ts","../src/psbt/psbt-totals.ts","../src/psbt/psbt-inputs.ts","../src/psbt/psbt-outputs.ts","../src/psbt/psbt-details.ts","../src/psbt/utils.ts","../src/signer/bitcoin-signer.ts","../src/transactions/generate-unsigned-transaction.ts","../src/validation/amount-validation.ts","../src/validation/transaction-validation.ts","../src/utils/bitcoin.descriptors.ts","../src/utils/lookup-derivation-by-address.ts"],"sourcesContent":["import { Bip21Options, decode, encode } from 'bip21';\n\nimport { isError } from '@leather.io/utils';\n\ninterface Bip21ResultValue {\n address: string;\n [key: string]: string | number | undefined;\n}\n\ntype Bip21Result =\n | {\n success: true;\n data: Bip21ResultValue;\n }\n | {\n success: false;\n error: string;\n };\n\nexport const bip21 = {\n decode: (uri: string, urnScheme?: string): Bip21Result => {\n try {\n const { address, options } = decode(uri, urnScheme);\n return {\n success: true,\n data: { address, ...options },\n };\n } catch (error) {\n return {\n success: false,\n error: isError(error) ? error.message : 'invalid input',\n };\n }\n },\n encode: (address: string, options: Bip21Options, urnScheme?: string) => {\n try {\n return encode(address, options, urnScheme);\n } catch {\n return null;\n }\n },\n};\n","import ecc from '@bitcoinerlab/secp256k1';\nimport { sha256 } from '@noble/hashes/sha256';\nimport { hexToBytes, utf8ToBytes } from '@noble/hashes/utils';\nimport * as bitcoin from 'bitcoinjs-lib';\nimport { ECPairFactory } from 'ecpair';\nimport { encode } from 'varuint-bitcoin';\n\nimport { PaymentTypes } from '@leather.io/rpc';\nimport { isString } from '@leather.io/utils';\n\nimport { toXOnly } from '../utils/bitcoin.utils';\n\nconst bip322MessageTag = 'BIP0322-signed-message';\n\nconst ECPair = ECPairFactory(ecc);\nbitcoin.initEccLib(ecc);\n\nexport function ecPairFromPrivateKey(key: Uint8Array) {\n return ECPair.fromPrivateKey(Buffer.from(key));\n}\n\n// See tagged hashes section of BIP-340\n// https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki#design\nconst messageTagHash = Uint8Array.from([\n ...sha256(utf8ToBytes(bip322MessageTag)),\n ...sha256(utf8ToBytes(bip322MessageTag)),\n]);\n\nexport function hashBip322Message(message: Uint8Array | string) {\n return sha256(\n Uint8Array.from([...messageTagHash, ...(isString(message) ? utf8ToBytes(message) : message)])\n );\n}\n\nexport const bip322TransactionToSignValues = {\n prevoutHash: hexToBytes('0000000000000000000000000000000000000000000000000000000000000000'),\n prevoutIndex: 0xffffffff,\n sequence: 0,\n};\n\nfunction encodeVarString(b: Buffer) {\n return Buffer.concat([encode(b.byteLength), b]);\n}\n\nconst supportedMessageSigningPaymentTypes: PaymentTypes[] = ['p2wpkh', 'p2tr'];\n\nexport function isSupportedMessageSigningPaymentType(paymentType: string) {\n return supportedMessageSigningPaymentTypes.includes(paymentType as PaymentTypes);\n}\n\n/**\n * Encode witness data for a BIP322 message\n * TODO: Refactor to remove `Buffer` use\n */\nexport function encodeMessageWitnessData(witnessArray: Buffer[]) {\n const len = encode(witnessArray.length);\n return Buffer.concat([len, ...witnessArray.map(witness => encodeVarString(witness))]);\n}\n\nfunction tapTweakHash(pubKey: Buffer, h: Buffer | undefined): Buffer {\n return bitcoin.crypto.taggedHash('TapTweak', Buffer.concat(h ? [pubKey, h] : [pubKey]));\n}\n\nexport function tweakSigner(signer: bitcoin.Signer, opts: any = {}): bitcoin.Signer {\n // @ts-expect-error privateKey exists on signer\n let privateKey: Uint8Array | undefined = signer.privateKey;\n if (!privateKey) {\n throw new Error('Private key is required for tweaking signer!');\n }\n if (signer.publicKey[0] === 3) {\n privateKey = ecc.privateNegate(privateKey);\n }\n\n const tweakedPrivateKey = ecc.privateAdd(\n privateKey,\n tapTweakHash(toXOnly(signer.publicKey), opts.tweakHash)\n );\n if (!tweakedPrivateKey) {\n throw new Error('Invalid tweaked private key!');\n }\n\n return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {\n network: opts.network,\n });\n}\n","import { hexToBytes } from '@noble/hashes/utils';\nimport { HDKey, Versions } from '@scure/bip32';\nimport { mnemonicToSeedSync } from '@scure/bip39';\nimport * as btc from '@scure/btc-signer';\nimport { TransactionInput, TransactionOutput } from '@scure/btc-signer/psbt';\n\nimport {\n DerivationPathDepth,\n extractAccountIndexFromPath,\n extractPurposeFromPath,\n} from '@leather.io/crypto';\nimport { BitcoinAddress, BitcoinNetworkModes, NetworkModes } from '@leather.io/models';\nimport type { BitcoinPaymentTypes } from '@leather.io/rpc';\nimport { defaultWalletKeyId, isDefined, whenNetwork } from '@leather.io/utils';\n\nimport { getTaprootPayment } from '../payments/p2tr-address-gen';\nimport { getNativeSegwitPaymentFromAddressIndex } from '../payments/p2wpkh-address-gen';\nimport { createBitcoinAddress } from '../validation/bitcoin-address';\nimport { BtcSignerNetwork, getBtcSignerLibNetworkConfigByMode } from './bitcoin.network';\n\nexport interface BitcoinAccount {\n type: BitcoinPaymentTypes;\n derivationPath: string;\n keychain: HDKey;\n accountIndex: number;\n network: BitcoinNetworkModes;\n}\nexport function initBitcoinAccount(derivationPath: string, policy: string): BitcoinAccount {\n const xpub = extractExtendedPublicKeyFromPolicy(policy);\n const network = inferNetworkFromPath(derivationPath);\n return {\n keychain: HDKey.fromExtendedKey(xpub, getHdKeyVersionsFromNetwork(network)),\n network,\n derivationPath,\n type: inferPaymentTypeFromPath(derivationPath),\n accountIndex: extractAccountIndexFromPath(derivationPath),\n };\n}\n\n/**\n * Represents a map of `BitcoinNetworkModes` to `NetworkModes`. While Bitcoin\n * has a number of networks, its often only necessary to consider the higher\n * level concept of mainnet and testnet\n */\nexport const bitcoinNetworkToCoreNetworkMap: Record<BitcoinNetworkModes, NetworkModes> = {\n mainnet: 'mainnet',\n testnet: 'testnet',\n regtest: 'testnet',\n signet: 'testnet',\n};\nexport function bitcoinNetworkModeToCoreNetworkMode(mode: BitcoinNetworkModes) {\n return bitcoinNetworkToCoreNetworkMap[mode];\n}\n\ntype BitcoinNetworkMap<T> = Record<BitcoinNetworkModes, T>;\n\nexport function whenBitcoinNetwork(mode: BitcoinNetworkModes) {\n return <T extends BitcoinNetworkMap<unknown>>(networkMap: T) =>\n networkMap[mode] as T[BitcoinNetworkModes];\n}\n\n/**\n * Map representing the \"Coin Type\" section of a derivation path.\n * Consider example below, Coin type is one, thus testnet\n * @example\n * `m/86'/1'/0'/0/0`\n */\nexport const coinTypeMap: Record<NetworkModes, 0 | 1> = {\n mainnet: 0,\n testnet: 1,\n};\n\nexport function getBitcoinCoinTypeIndexByNetwork(network: BitcoinNetworkModes) {\n return coinTypeMap[bitcoinNetworkModeToCoreNetworkMode(network)];\n}\n\nexport function deriveAddressIndexKeychainFromAccount(keychain: HDKey) {\n if (keychain.depth !== DerivationPathDepth.Account)\n throw new Error('Keychain passed is not an account');\n\n return (index: number) => keychain.deriveChild(0).deriveChild(index);\n}\n\nexport function deriveAddressIndexZeroFromAccount(keychain: HDKey) {\n return deriveAddressIndexKeychainFromAccount(keychain)(0);\n}\n\nexport const ecdsaPublicKeyLength = 33;\n\nexport function ecdsaPublicKeyToSchnorr(pubKey: Uint8Array) {\n if (pubKey.byteLength !== ecdsaPublicKeyLength) throw new Error('Invalid public key length');\n return pubKey.slice(1);\n}\n\n// Basically same as above, to remove\nexport function toXOnly(pubKey: Buffer) {\n return pubKey.length === 32 ? pubKey : pubKey.subarray(1, 33);\n}\n\nexport function decodeBitcoinTx(tx: string): ReturnType<typeof btc.RawTx.decode> {\n return btc.RawTx.decode(hexToBytes(tx));\n}\n\nexport function getAddressFromOutScript(\n script: Uint8Array,\n bitcoinNetwork: BtcSignerNetwork\n): BitcoinAddress | null {\n const outputScript = btc.OutScript.decode(script);\n\n switch (outputScript.type) {\n case 'pkh':\n case 'sh':\n case 'wpkh':\n case 'wsh':\n return createBitcoinAddress(\n btc.Address(bitcoinNetwork).encode({\n type: outputScript.type,\n hash: outputScript.hash,\n })\n );\n case 'tr':\n return createBitcoinAddress(\n btc.Address(bitcoinNetwork).encode({\n type: outputScript.type,\n pubkey: outputScript.pubkey,\n })\n );\n case 'ms':\n return createBitcoinAddress(btc.p2ms(outputScript.m, outputScript.pubkeys).address ?? '');\n case 'pk':\n return createBitcoinAddress(btc.p2pk(outputScript.pubkey, bitcoinNetwork).address ?? '');\n case 'unknown':\n case 'tr_ms':\n case 'tr_ns':\n case 'p2a':\n default:\n return null;\n }\n}\n/**\n * Payment type identifiers, as described by `@scure/btc-signer` library\n */\nexport type BtcSignerLibPaymentTypeIdentifers = 'wpkh' | 'wsh' | 'tr' | 'pkh' | 'sh';\n\nexport const paymentTypeMap: Record<BtcSignerLibPaymentTypeIdentifers, BitcoinPaymentTypes> = {\n wpkh: 'p2wpkh',\n wsh: 'p2wpkh-p2sh',\n tr: 'p2tr',\n pkh: 'p2pkh',\n sh: 'p2sh',\n};\n\nexport function btcSignerLibPaymentTypeToPaymentTypeMap(\n payment: BtcSignerLibPaymentTypeIdentifers\n) {\n return paymentTypeMap[payment];\n}\n\nexport function isBtcSignerLibPaymentType(\n payment: string\n): payment is BtcSignerLibPaymentTypeIdentifers {\n return payment in paymentTypeMap;\n}\n\nexport function parseKnownPaymentType(\n payment: BtcSignerLibPaymentTypeIdentifers | BitcoinPaymentTypes\n) {\n return isBtcSignerLibPaymentType(payment)\n ? btcSignerLibPaymentTypeToPaymentTypeMap(payment)\n : payment;\n}\n\nexport type PaymentTypeMap<T> = Record<BitcoinPaymentTypes, T>;\nexport function whenPaymentType(mode: BitcoinPaymentTypes | BtcSignerLibPaymentTypeIdentifers) {\n return <T>(paymentMap: PaymentTypeMap<T>): T => paymentMap[parseKnownPaymentType(mode)];\n}\n\nexport type SupportedPaymentType = 'p2wpkh' | 'p2tr';\nexport type SupportedPaymentTypeMap<T> = Record<SupportedPaymentType, T>;\nexport function whenSupportedPaymentType(mode: SupportedPaymentType) {\n return <T>(paymentMap: SupportedPaymentTypeMap<T>): T => paymentMap[mode];\n}\n\n/**\n * Infers the Bitcoin payment type from the derivation path.\n * Below we see path has 86 in it, per convention, this refers to taproot payments\n * @example\n * `m/86'/1'/0'/0/0`\n */\nexport function inferPaymentTypeFromPath(path: string): BitcoinPaymentTypes {\n const purpose = extractPurposeFromPath(path);\n switch (purpose) {\n case 84:\n return 'p2wpkh';\n case 86:\n return 'p2tr';\n case 44:\n return 'p2pkh';\n default:\n throw new Error(`Unable to infer payment type from purpose=${purpose}`);\n }\n}\n\nexport function inferNetworkFromPath(path: string): NetworkModes {\n return path.split('/')[2].startsWith('0') ? 'mainnet' : 'testnet';\n}\n\nexport function extractExtendedPublicKeyFromPolicy(policy: string) {\n return policy.split(']')[1];\n}\n\nexport function createWalletIdDecoratedPath(policy: string, walletId: string) {\n return policy.split(']')[0].replace('[', '').replace('m', walletId);\n}\n\n// Primarily used to get the correct `Version` when passing Ledger Bitcoin\n// extended public keys to the HDKey constructor\nexport function getHdKeyVersionsFromNetwork(network: NetworkModes) {\n return whenNetwork(network)({\n mainnet: undefined,\n testnet: {\n private: 0x00000000,\n public: 0x043587cf,\n } as Versions,\n });\n}\n\nexport function getBitcoinInputAddress(input: TransactionInput, bitcoinNetwork: BtcSignerNetwork) {\n if (isDefined(input.witnessUtxo))\n return getAddressFromOutScript(input.witnessUtxo.script, bitcoinNetwork);\n if (isDefined(input.nonWitnessUtxo) && isDefined(input.index))\n return getAddressFromOutScript(\n input.nonWitnessUtxo.outputs[input.index]?.script,\n bitcoinNetwork\n );\n return null;\n}\n\nexport function getInputPaymentType(\n input: TransactionInput,\n network: BitcoinNetworkModes\n): BitcoinPaymentTypes {\n const address = getBitcoinInputAddress(input, getBtcSignerLibNetworkConfigByMode(network));\n if (address === null) throw new Error('Input address cannot be empty');\n if (address.startsWith('bc1p') || address.startsWith('tb1p') || address.startsWith('bcrt1p'))\n return 'p2tr';\n if (address.startsWith('bc1q') || address.startsWith('tb1q') || address.startsWith('bcrt1q'))\n return 'p2wpkh';\n throw new Error('Unable to infer payment type from input address');\n}\n\n// Ledger wallets are keyed by their derivation path. To reuse the look up logic\n// between payment types, this factory fn accepts a fn that generates the path\nexport function lookUpLedgerKeysByPath(\n getDerivationPath: (network: BitcoinNetworkModes, accountIndex: number) => string\n) {\n return (\n ledgerKeyMap: Record<string, { policy: string } | undefined>,\n network: BitcoinNetworkModes\n ) =>\n (accountIndex: number) => {\n const path = getDerivationPath(network, accountIndex);\n // Single wallet mode, hardcoded default walletId\n const account = ledgerKeyMap[path.replace('m', defaultWalletKeyId)];\n if (!account) return;\n return initBitcoinAccount(path, account.policy);\n };\n}\n\ninterface GetAddressArgs {\n index: number;\n keychain?: HDKey;\n network: BitcoinNetworkModes;\n}\n\nexport function getTaprootAddress({ index, keychain, network }: GetAddressArgs) {\n if (!keychain) throw new Error('Expected keychain to be provided');\n\n if (keychain.depth !== DerivationPathDepth.Account)\n throw new Error('Expects keychain to be on the account index');\n\n const addressIndex = deriveAddressIndexKeychainFromAccount(keychain)(index);\n\n if (!addressIndex.publicKey) throw new Error('Expected publicKey to be defined');\n\n const payment = getTaprootPayment(addressIndex.publicKey, network);\n\n if (!payment.address) throw new Error('Expected address to be defined');\n return payment.address;\n}\n\nexport function getNativeSegwitAddress({ index, keychain, network }: GetAddressArgs) {\n if (!keychain) throw new Error('Expected keychain to be provided');\n\n if (keychain.depth !== DerivationPathDepth.Account)\n throw new Error('Expects keychain to be on the account index');\n\n const addressIndex = deriveAddressIndexKeychainFromAccount(keychain)(index);\n\n if (!addressIndex.publicKey) throw new Error('Expected publicKey to be defined');\n\n const payment = getNativeSegwitPaymentFromAddressIndex(addressIndex, network);\n\n if (!payment.address) throw new Error('Expected address to be defined');\n return payment.address;\n}\n\n/**\n * @deprecated\n * Use `deriveRootBip32Keychain` in `@leather.io/crypto` instead\n */\nexport function mnemonicToRootNode(secretKey: string) {\n const seed = mnemonicToSeedSync(secretKey);\n return HDKey.fromMasterSeed(seed);\n}\n\nexport function getPsbtTxInputs(psbtTx: btc.Transaction): TransactionInput[] {\n const inputsLength = psbtTx.inputsLength;\n const inputs: TransactionInput[] = [];\n for (let i = 0; i < inputsLength; i++) inputs.push(psbtTx.getInput(i));\n return inputs;\n}\n\nexport function getPsbtTxOutputs(psbtTx: btc.Transaction): TransactionOutput[] {\n const outputsLength = psbtTx.outputsLength;\n const outputs: TransactionOutput[] = [];\n for (let i = 0; i < outputsLength; i++) outputs.push(psbtTx.getOutput(i));\n return outputs;\n}\n\nexport function inferNetworkFromAddress(address: BitcoinAddress): BitcoinNetworkModes {\n if (address.startsWith('bc1')) return 'mainnet';\n if (address.startsWith('tb1')) return 'testnet';\n if (address.startsWith('bcrt1')) return 'regtest';\n\n const firstChar = address[0];\n\n if (firstChar === '1' || firstChar === '3') return 'mainnet';\n if (firstChar === 'm' || firstChar === 'n') return 'testnet';\n if (firstChar === '2') return 'testnet';\n\n throw new Error('Invalid or unsupported Bitcoin address format');\n}\n\nexport function inferPaymentTypeFromAddress(address: BitcoinAddress): SupportedPaymentType {\n if (address.startsWith('bc1q') || address.startsWith('tb1q') || address.startsWith('bcrt1q'))\n return 'p2wpkh';\n\n if (address.startsWith('bc1p') || address.startsWith('tb1p') || address.startsWith('bcrt1p'))\n return 'p2tr';\n\n throw new Error('Unable to infer payment type from address');\n}\n\nexport function getBitcoinInputValue(input: TransactionInput) {\n if (isDefined(input.witnessUtxo)) return Number(input.witnessUtxo.amount);\n if (isDefined(input.nonWitnessUtxo) && isDefined(input.index))\n return Number(input.nonWitnessUtxo.outputs[input.index]?.amount);\n // logger.warn('Unable to find either `witnessUtxo` or `nonWitnessUtxo` in input. Defaulting to 0');\n return 0;\n}\n","import { HDKey } from '@scure/bip32';\nimport * as btc from '@scure/btc-signer';\n\nimport { DerivationPathDepth } from '@leather.io/crypto';\nimport { BitcoinNetworkModes } from '@leather.io/models';\n\nimport { getBtcSignerLibNetworkConfigByMode } from '../utils/bitcoin.network';\nimport {\n BitcoinAccount,\n deriveAddressIndexZeroFromAccount,\n ecdsaPublicKeyToSchnorr,\n getBitcoinCoinTypeIndexByNetwork,\n} from '../utils/bitcoin.utils';\n\nexport function makeTaprootAccountDerivationPath(\n network: BitcoinNetworkModes,\n accountIndex: number\n) {\n return `m/86'/${getBitcoinCoinTypeIndexByNetwork(network)}'/${accountIndex}'`;\n}\n/** @deprecated Use makeTaprootAccountDerivationPath */\nexport const getTaprootAccountDerivationPath = makeTaprootAccountDerivationPath;\n\nexport function makeTaprootAddressIndexDerivationPath(\n network: BitcoinNetworkModes,\n accountIndex: number,\n addressIndex: number\n) {\n return makeTaprootAccountDerivationPath(network, accountIndex) + `/0/${addressIndex}`;\n}\n/** @deprecated Use makeTaprootAddressIndexDerivationPath */\nexport const getTaprootAddressIndexDerivationPath = makeTaprootAddressIndexDerivationPath;\n\nexport function deriveTaprootAccount(keychain: HDKey, network: BitcoinNetworkModes) {\n if (keychain.depth !== DerivationPathDepth.Root)\n throw new Error('Keychain passed is not an account');\n\n return (accountIndex: number): BitcoinAccount => ({\n type: 'p2tr',\n network,\n accountIndex,\n derivationPath: makeTaprootAccountDerivationPath(network, accountIndex),\n keychain: keychain.derive(makeTaprootAccountDerivationPath(network, accountIndex)),\n });\n}\n\nexport function getTaprootPayment(publicKey: Uint8Array, network: BitcoinNetworkModes) {\n return btc.p2tr(\n ecdsaPublicKeyToSchnorr(publicKey),\n undefined,\n getBtcSignerLibNetworkConfigByMode(network),\n true // allow unknown outputs\n );\n}\n\nexport function getTaprootPaymentFromAddressIndex(keychain: HDKey, network: BitcoinNetworkModes) {\n if (keychain.depth !== DerivationPathDepth.AddressIndex)\n throw new Error('Keychain passed is not an address index');\n\n if (!keychain.publicKey) throw new Error('Keychain has no public key');\n\n return getTaprootPayment(keychain.publicKey, network);\n}\n\ninterface DeriveTaprootReceiveAddressIndexArgs {\n keychain: HDKey;\n network: BitcoinNetworkModes;\n}\nexport function deriveTaprootReceiveAddressIndexZero({\n keychain,\n network,\n}: DeriveTaprootReceiveAddressIndexArgs) {\n const zeroAddressIndex = deriveAddressIndexZeroFromAccount(keychain);\n return {\n keychain: zeroAddressIndex,\n payment: getTaprootPaymentFromAddressIndex(zeroAddressIndex, network),\n };\n}\n","import * as bitcoinJs from 'bitcoinjs-lib';\n\nimport { BitcoinNetworkModes } from '@leather.io/models';\n\n// TODO - this PR was merged so we could update this\n// https://github.com/paulmillr/scure-btc-signer/blob/main/src/utils.ts\n// See this PR https://github.com/paulmillr/@scure/btc-signer/pull/15\n// Atttempting to add these directly to the library\nexport interface BtcSignerNetwork {\n bech32: string;\n pubKeyHash: number;\n scriptHash: number;\n wif: number;\n}\n\nconst bitcoinMainnet: BtcSignerNetwork = {\n bech32: 'bc',\n pubKeyHash: 0x00,\n scriptHash: 0x05,\n wif: 0x80,\n};\n\nconst bitcoinTestnet: BtcSignerNetwork = {\n bech32: 'tb',\n pubKeyHash: 0x6f,\n scriptHash: 0xc4,\n wif: 0xef,\n};\n\nconst bitcoinRegtest: BtcSignerNetwork = {\n bech32: 'bcrt',\n pubKeyHash: 0x6f,\n scriptHash: 0xc4,\n wif: 0xef,\n};\n\nconst btcSignerLibNetworks: Record<BitcoinNetworkModes, BtcSignerNetwork> = {\n mainnet: bitcoinMainnet,\n testnet: bitcoinTestnet,\n regtest: bitcoinRegtest,\n // Signet originally was going to have its own prefix but authors decided to\n // copy testnet\n signet: bitcoinTestnet,\n};\n\nexport function getBtcSignerLibNetworkConfigByMode(network: BitcoinNetworkModes) {\n return btcSignerLibNetworks[network];\n}\n\nconst bitcoinJsLibNetworks: Record<BitcoinNetworkModes, bitcoinJs.Network> = {\n mainnet: bitcoinJs.networks.bitcoin,\n testnet: bitcoinJs.networks.testnet,\n regtest: bitcoinJs.networks.regtest,\n signet: bitcoinJs.networks.testnet,\n};\n\nexport function getBitcoinJsLibNetworkConfigByMode(network: BitcoinNetworkModes) {\n return bitcoinJsLibNetworks[network];\n}\n","import { HDKey } from '@scure/bip32';\nimport * as btc from '@scure/btc-signer';\n\nimport { DerivationPathDepth } from '@leather.io/crypto';\nimport { BitcoinNetworkModes } from '@leather.io/models';\n\nimport { getBtcSignerLibNetworkConfigByMode } from '../utils/bitcoin.network';\nimport {\n BitcoinAccount,\n deriveAddressIndexZeroFromAccount,\n getBitcoinCoinTypeIndexByNetwork,\n} from '../utils/bitcoin.utils';\n\nexport function makeNativeSegwitAccountDerivationPath(\n network: BitcoinNetworkModes,\n accountIndex: number\n) {\n return `m/84'/${getBitcoinCoinTypeIndexByNetwork(network)}'/${accountIndex}'`;\n}\n\n/** @deprecated Use makeNativeSegwitAccountDerivationPath */\nexport const getNativeSegwitAccountDerivationPath = makeNativeSegwitAccountDerivationPath;\n\nexport function makeNativeSegwitAddressIndexDerivationPath(\n network: BitcoinNetworkModes,\n accountIndex: number,\n addressIndex: number\n) {\n return makeNativeSegwitAccountDerivationPath(network, accountIndex) + `/0/${addressIndex}`;\n}\n\n/** @deprecated Use makeNativeSegwitAddressIndexDerivationPath */\nexport const getNativeSegwitAddressIndexDerivationPath = makeNativeSegwitAddressIndexDerivationPath;\n\nexport function deriveNativeSegwitAccountFromRootKeychain(\n keychain: HDKey,\n network: BitcoinNetworkModes\n) {\n if (keychain.depth !== DerivationPathDepth.Root) throw new Error('Keychain passed is not a root');\n return (accountIndex: number): BitcoinAccount => ({\n type: 'p2wpkh',\n network,\n accountIndex,\n derivationPath: makeNativeSegwitAccountDerivationPath(network, accountIndex),\n keychain: keychain.derive(makeNativeSegwitAccountDerivationPath(network, accountIndex)),\n });\n}\n\nexport function getNativeSegwitPaymentFromAddressIndex(\n keychain: HDKey,\n network: BitcoinNetworkModes\n) {\n if (keychain.depth !== DerivationPathDepth.AddressIndex)\n throw new Error('Keychain passed is not an address index');\n\n if (!keychain.publicKey) throw new Error('Keychain does not have a public key');\n\n return btc.p2wpkh(keychain.publicKey, getBtcSignerLibNetworkConfigByMode(network));\n}\n\ninterface DeriveNativeSegwitReceiveAddressIndexArgs {\n keychain: HDKey;\n network: BitcoinNetworkModes;\n}\nexport function deriveNativeSegwitReceiveAddressIndexZero({\n keychain,\n network,\n}: DeriveNativeSegwitReceiveAddressIndexArgs) {\n const zeroAddressIndex = deriveAddressIndexZeroFromAccount(keychain);\n return {\n keychain: zeroAddressIndex,\n payment: getNativeSegwitPaymentFromAddressIndex(zeroAddressIndex, network),\n };\n}\n","import { Network, validate } from 'bitcoin-address-validation';\n\nimport { BitcoinNetworkModes } from '@leather.io/models';\nimport { isEmptyString, isUndefined } from '@leather.io/utils';\n\n// todo investigate handling this in bitcoinNetworkToNetworkMode\nexport function getBitcoinAddressNetworkType(network: BitcoinNetworkModes): Network {\n // Signet uses testnet address format, this parsing is to please the\n // validation library - 'bitcoin-address-validation'\n if (network === 'signet') return Network.testnet;\n return network as Network;\n}\n\nexport function isValidBitcoinAddress(address: string) {\n if (isUndefined(address) || isEmptyString(address)) {\n return false;\n }\n\n return validate(address);\n}\n\nexport function isValidBitcoinNetworkAddress(address: string, network: BitcoinNetworkModes) {\n return validate(address, getBitcoinAddressNetworkType(network));\n}\n","import { TransactionErrorKey } from '@leather.io/models';\n\nexport class BitcoinError extends Error {\n public message: BitcoinErrorKey;\n constructor(message: BitcoinErrorKey) {\n super(message);\n this.name = 'BitcoinError';\n this.message = message;\n\n // Fix the prototype chain\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\nexport type BitcoinErrorKey =\n | TransactionErrorKey\n | 'InsufficientAmount'\n | 'NoInputsToSign'\n | 'NoOutputsToSign'\n | 'InscribedUtxos';\n","import { BitcoinAddress } from '@leather.io/models';\n\nimport { isValidBitcoinAddress } from './address-validation';\nimport { BitcoinError } from './bitcoin-error';\n\nexport function isBitcoinAddress(value: string): value is BitcoinAddress {\n try {\n isValidBitcoinAddress(value);\n return true;\n } catch {\n return false;\n }\n}\n\n// Function to create a BitcoinAddress\nexport function createBitcoinAddress(value: string): BitcoinAddress {\n if (!isBitcoinAddress(value)) {\n throw new BitcoinError('InvalidAddress');\n }\n\n return value;\n}\n","import { base64 } from '@scure/base';\nimport * as btc from '@scure/btc-signer';\nimport * as bitcoin from 'bitcoinjs-lib';\n\nimport { BitcoinAddress, BitcoinNetworkModes } from '@leather.io/models';\n\nimport { getBitcoinJsLibNetworkConfigByMode } from '../utils/bitcoin.network';\nimport {\n bip322TransactionToSignValues,\n ecPairFromPrivateKey,\n encodeMessageWitnessData,\n hashBip322Message,\n tweakSigner,\n} from './bip322-utils';\n\nexport function createNativeSegwitBitcoinJsSigner(privateKey: Buffer) {\n return ecPairFromPrivateKey(privateKey);\n}\n\nexport function createTaprootBitcoinJsSigner(privateKey: Buffer) {\n return tweakSigner(ecPairFromPrivateKey(privateKey));\n}\n\nexport function createToSpendTx(\n address: BitcoinAddress,\n message: string,\n network: BitcoinNetworkModes\n) {\n const { prevoutHash, prevoutIndex, sequence } = bip322TransactionToSignValues;\n\n const script = bitcoin.address.toOutputScript(\n address,\n getBitcoinJsLibNetworkConfigByMode(network)\n );\n\n const hash = hashBip322Message(message);\n const commands = [0, Buffer.from(hash)];\n const scriptSig = bitcoin.script.compile(commands);\n\n const virtualToSpend = new bitcoin.Transaction();\n virtualToSpend.version = 0;\n virtualToSpend.addInput(Buffer.from(prevoutHash), prevoutIndex, sequence, scriptSig);\n virtualToSpend.addOutput(script, 0);\n return { virtualToSpend, script };\n}\n\nfunction createToSignTx(toSpendTxHex: Buffer, script: Buffer, network: BitcoinNetworkModes) {\n const virtualToSign = new bitcoin.Psbt({ network: getBitcoinJsLibNetworkConfigByMode(network) });\n virtualToSign.setVersion(0);\n const prevTxHash = toSpendTxHex;\n const prevOutIndex = 0;\n const toSignScriptSig = bitcoin.script.compile([bitcoin.script.OPS.OP_RETURN]);\n\n virtualToSign.addInput({\n hash: prevTxHash,\n index: prevOutIndex,\n sequence: 0,\n witnessUtxo: { script, value: 0 },\n });\n\n virtualToSign.addOutput({ script: toSignScriptSig, value: 0 });\n return virtualToSign;\n}\n\ninterface SignBip322MessageSimple {\n address: BitcoinAddress;\n message: string;\n network: BitcoinNetworkModes;\n signPsbt(psbt: bitcoin.Psbt): Promise<btc.Transaction>;\n}\nexport async function signBip322MessageSimple(args: SignBip322MessageSimple) {\n const { address, message, network, signPsbt } = args;\n\n const { virtualToSpend, script } = createToSpendTx(address, message, network);\n\n const virtualToSign = createToSignTx(virtualToSpend.getHash(), script, network);\n\n const signedTx = await signPsbt(virtualToSign);\n\n const asBitcoinJsTransaction = bitcoin.Psbt.fromBuffer(Buffer.from(signedTx.toPSBT()));\n\n asBitcoinJsTransaction.finalizeInput(0);\n\n // sign the tx\n // section 5.1\n // github.com/LegReq/bip0322-signatures/blob/master/BIP0322_signing.ipynb\n const toSignTx = asBitcoinJsTransaction.extractTransaction();\n\n const result = encodeMessageWitnessData(toSignTx.ins[0].witness);\n\n return {\n virtualToSpend,\n virtualToSign: toSignTx,\n unencodedSig: result,\n signature: base64.encode(result),\n };\n}\n","import BigNumber from 'bignumber.js';\n\nimport type { AverageBitcoinFeeRates, Money } from '@leather.io/models';\nimport { createMoney, satToBtc } from '@leather.io/utils';\n\nimport { filterUneconomicalUtxos, getSpendableAmount } from './coin-selection.utils';\n\ninterface CalculateMaxSpendArgs {\n // recipient is intentionally string instead of BitcoinAddress, as it's being validated\n // with a fallback in a subroutine.\n recipient: string;\n utxos: { value: number; txid: string }[];\n feeRates?: AverageBitcoinFeeRates;\n feeRate?: number;\n}\n\nexport interface CalculateMaxSpendResponse {\n spendAllFee: number;\n amount: Money;\n spendableBitcoin: BigNumber;\n}\nexport function calculateMaxSpend({\n recipient,\n utxos,\n feeRate,\n feeRates,\n}: CalculateMaxSpendArgs): CalculateMaxSpendResponse {\n if (!utxos.length || !feeRates)\n return {\n spendAllFee: 0,\n amount: createMoney(0, 'BTC'),\n spendableBitcoin: new BigNumber(0),\n };\n\n const currentFeeRate = feeRate ?? feeRates.halfHourFee.toNumber();\n\n const filteredUtxos = filterUneconomicalUtxos({\n utxos,\n feeRate: currentFeeRate,\n recipients: [{ address: recipient, amount: createMoney(0, 'BTC') }],\n });\n\n const { spendableAmount, fee } = getSpendableAmount({\n utxos: filteredUtxos,\n feeRate: currentFeeRate,\n recipients: [{ address: recipient, amount: createMoney(0, 'BTC') }],\n isSendMax: true,\n });\n\n return {\n spendAllFee: fee,\n amount: createMoney(spendableAmount, 'BTC'),\n spendableBitcoin: satToBtc(spendableAmount),\n };\n}\n","import BigNumber from 'bignumber.js';\nimport validate, { AddressInfo, AddressType, getAddressInfo } from 'bitcoin-address-validation';\n\nimport { BTC_P2WPKH_DUST_AMOUNT } from '@leather.io/constants';\nimport { sumNumbers } from '@leather.io/utils';\n\nimport { BtcSizeFeeEstimator } from '../fees/btc-size-fee-estimator';\nimport { CoinSelectionRecipient } from './coin-selection';\n\nexport function getUtxoTotal<T extends { value: number }>(utxos: T[]) {\n return sumNumbers(utxos.map(utxo => utxo.value));\n}\n\nexport function getSizeInfo(payload: {\n inputLength: number;\n recipients: CoinSelectionRecipient[];\n isSendMax?: boolean;\n}) {\n const { inputLength, recipients, isSendMax } = payload;\n\n const validAddressesInfo = recipients\n .map(recipient => validate(recipient.address) && getAddressInfo(recipient.address))\n .filter(Boolean) as AddressInfo[];\n\n function getTxOutputsLengthByPaymentType() {\n return validAddressesInfo.reduce(\n (acc, { type }) => {\n acc[type] = (acc[type] || 0) + 1;\n return acc;\n },\n {} as Record<AddressType, number>\n );\n }\n\n const outputTypesCount = getTxOutputsLengthByPaymentType();\n\n // Add a change address if not sending max (defaults to p2wpkh)\n if (!isSendMax) {\n outputTypesCount[AddressType.p2wpkh] = (outputTypesCount[AddressType.p2wpkh] || 0) + 1;\n }\n\n // Prepare the output data map for consumption by the txSizer\n const outputsData = Object.entries(outputTypesCount).reduce(\n (acc, [type, count]) => {\n acc[type + '_output_count'] = count;\n return acc;\n },\n {} as Record<string, number>\n );\n\n const txSizer = new BtcSizeFeeEstimator();\n const sizeInfo = txSizer.calcTxSize({\n input_script: 'p2wpkh',\n input_count: inputLength,\n ...outputsData,\n });\n\n return sizeInfo;\n}\ninterface GetSpendableAmountArgs<T> {\n utxos: T[];\n feeRate: number;\n recipients: CoinSelectionRecipient[];\n isSendMax?: boolean;\n}\nexport function getSpendableAmount<T extends { value: number }>({\n utxos,\n feeRate,\n recipients,\n}: GetSpendableAmountArgs<T>) {\n const balance = utxos.map(utxo => utxo.value).reduce((prevVal, curVal) => prevVal + curVal, 0);\n\n const size = getSizeInfo({\n inputLength: utxos.length,\n recipients,\n });\n const fee = Math.ceil(size.txVBytes * feeRate);\n const bigNumberBalance = BigNumber(balance);\n return {\n spendableAmount: BigNumber.max(0, bigNumberBalance.minus(fee)),\n fee,\n };\n}\n\n// Check if the spendable amount drops when adding a utxo\nexport function filterUneconomicalUtxos<T extends { value: number; txid: string }>({\n utxos,\n feeRate,\n recipients,\n}: {\n utxos: T[];\n feeRate: number;\n recipients: CoinSelectionRecipient[];\n}) {\n const { spendableAmount: fullSpendableAmount } = getSpendableAmount({\n utxos,\n feeRate,\n recipients,\n });\n\n const filteredUtxos = utxos\n .filter(utxo => Number(utxo.value) >= BTC_P2WPKH_DUST_AMOUNT)\n .filter(utxo => {\n // Calculate spendableAmount without that utxo\n const { spendableAmount } = getSpendableAmount({\n utxos: utxos.filter(u => u.txid !== utxo.txid),\n feeRate,\n recipients,\n });\n // If fullSpendableAmount is greater, do not use utxo\n return spendableAmount.toNumber() < fullSpendableAmount.toNumber();\n });\n return filteredUtxos;\n}\n","// https://github.com/argvil19/bitcoin-transaction-size-calculator/blob/master/index.js\nimport BigNumber from 'bignumber.js';\n\nimport { assertUnreachable } from '@leather.io/utils';\n\nexport type InputScriptType =\n | 'p2pkh'\n | 'p2sh'\n | 'p2sh-p2wpkh'\n | 'p2sh-p2wsh'\n | 'p2wpkh'\n | 'p2wsh'\n | 'p2tr';\n\nexport interface TxSizerParams {\n input_count: number;\n input_script: InputScriptType;\n input_m: number;\n input_n: number;\n p2pkh_output_count: number;\n p2sh_output_count: number;\n p2sh_p2wpkh_output_count: number;\n p2sh_p2wsh_output_count: number;\n p2wpkh_output_count: number;\n p2wsh_output_count: number;\n p2tr_output_count: number;\n}\n\nexport class BtcSizeFeeEstimator {\n P2PKH_IN_SIZE = 148;\n P2PKH_OUT_SIZE = 34;\n P2SH_OUT_SIZE = 32;\n P2SH_P2WPKH_OUT_SIZE = 32;\n P2SH_P2WSH_OUT_SIZE = 32;\n P2SH_P2WPKH_IN_SIZE = 91;\n P2WPKH_IN_SIZE = 67.75;\n P2WPKH_OUT_SIZE = 31;\n P2WSH_OUT_SIZE = 43;\n P2TR_OUT_SIZE = 43;\n P2TR_IN_SIZE = 57.25;\n PUBKEY_SIZE = 33;\n SIGNATURE_SIZE = 72;\n SUPPORTED_INPUT_SCRIPT_TYPES: InputScriptType[] = [\n 'p2pkh',\n 'p2sh',\n 'p2sh-p2wpkh',\n 'p2sh-p2wsh',\n 'p2wpkh',\n 'p2wsh',\n 'p2tr',\n ];\n\n defaultParams: TxSizerParams = {\n input_count: 0,\n input_script: 'p2wpkh',\n input_m: 0,\n input_n: 0,\n p2pkh_output_count: 0,\n p2sh_output_count: 0,\n p2sh_p2wpkh_output_count: 0,\n p2sh_p2wsh_output_count: 0,\n p2wpkh_output_count: 0,\n p2wsh_output_count: 0,\n p2tr_output_count: 0,\n };\n\n params: TxSizerParams = { ...this.defaultParams };\n\n getSizeOfScriptLengthElement(length: number) {\n if (length < 75) {\n return 1;\n } else if (length <= 255) {\n return 2;\n } else if (length <= 65535) {\n return 3;\n } else if (length <= 4294967295) {\n return 5;\n } else {\n throw new Error('Size of redeem script is too large');\n }\n }\n\n getSizeOfletInt(length: number) {\n if (length < 253) {\n return 1;\n } else if (length < 65535) {\n return 3;\n } else if (length < 4294967295) {\n return 5;\n } else if (new BigNumber(length).isLessThan('18446744073709551615')) {\n return 9;\n } else {\n throw new Error('Invalid let int');\n }\n }\n\n getTxOverheadVBytes(input_script: InputScriptType, input_count: number, output_count: number) {\n let witness_vbytes;\n if (input_script === 'p2pkh' || input_script === 'p2sh') {\n witness_vbytes = 0;\n } else {\n // Transactions with segwit inputs have extra overhead\n witness_vbytes =\n 0.25 + // segwit marker\n 0.25 + // segwit flag\n this.getSizeOfletInt(input_count) / 4; // witness element count\n }\n\n return (\n 4 + // nVersion\n this.getSizeOfletInt(input_count) + // number of inputs\n this.getSizeOfletInt(output_count) + // number of outputs\n 4 + // nLockTime\n witness_vbytes\n );\n }\n\n getTxOverheadExtraRawBytes(input_script: InputScriptType, input_count: number) {\n let witness_vbytes;\n if (input_script === 'p2pkh' || input_script === 'p2sh') {\n witness_vbytes = 0;\n } else {\n // Transactions with segwit inputs have extra overhead\n witness_vbytes =\n 0.25 + // segwit marker\n 0.25 + // segwit flag\n this.getSizeOfletInt(input_count) / 4; // witness element count\n }\n\n return witness_vbytes * 3;\n }\n\n prepareParams(opts: Partial<TxSizerParams>) {\n // Verify opts and set them to this.params\n opts = opts || Object.assign(this.defaultParams);\n\n const input_count = opts.input_count || this.defaultParams.input_count;\n if (!Number.isInteger(input_count) || input_count < 0) {\n throw new Error('expecting positive input count, got: ' + input_count);\n }\n\n const input_script = opts.input_script || this.defaultParams.input_script;\n if (this.SUPPORTED_INPUT_SCRIPT_TYPES.indexOf(input_script) === -1) {\n throw new Error('Not supported input script type');\n }\n\n const input_m = opts.input_m || this.defaultParams.input_m;\n if (!Number.isInteger(input_m) || input_m < 0) {\n throw new Error('expecting positive signature count');\n }\n\n const input_n = opts.input_n || this.defaultParams.input_n;\n if (!Number.isInteger(input_n) || input_n < 0) {\n throw new Error('expecting positive pubkey count');\n }\n\n const p2pkh_output_count = opts.p2pkh_output_count || this.defaultParams.p2pkh_output_count;\n if (!Number.isInteger(p2pkh_output_count) || p2pkh_output_count < 0) {\n throw new Error('expecting positive p2pkh output count');\n }\n\n const p2sh_output_count = opts.p2sh_output_count || this.defaultParams.p2sh_output_count;\n if (!Number.isInteger(p2sh_output_count) || p2sh_output_count < 0) {\n throw new Error('expecting positive p2sh output count');\n }\n\n const p2sh_p2wpkh_output_count =\n opts.p2sh_p2wpkh_output_count || this.defaultParams.p2sh_p2wpkh_output_count;\n if (!Number.isInteger(p2sh_p2wpkh_output_count) || p2sh_p2wpkh_output_count < 0) {\n throw new Error('expecting positive p2sh-p2wpkh output count');\n }\n\n const p2sh_p2wsh_output_count =\n opts.p2sh_p2wsh_output_count || this.defaultParams.p2sh_p2wsh_output_count;\n if (!Number.isInteger(p2sh_p2wsh_output_count) || p2sh_p2wsh_output_count < 0) {\n throw new Error('expecting positive p2sh-p2wsh output count');\n }\n\n const p2wpkh_output_count = opts.p2wpkh_output_count || this.defaultParams.p2wpkh_output_count;\n if (!Number.isInteger(p2wpkh_output_count) || p2wpkh_output_count < 0) {\n throw new Error('expecting positive p2wpkh output count');\n }\n\n const p2wsh_output_count = opts.p2wsh_output_count || this.defaultParams.p2wsh_output_count;\n if (!Number.isInteger(p2wsh_output_count) || p2wsh_output_count < 0) {\n throw new Error('expecting positive p2wsh output count');\n }\n\n const p2tr_output_count = opts.p2tr_output_count || this.defaultParams.p2tr_output_count;\n if (!Number.isInteger(p2tr_output_count) || p2tr_output_count < 0) {\n throw new Error('expecting positive p2tr output count');\n }\n\n this.params = {\n input_count,\n input_script,\n input_m,\n input_n,\n p2pkh_output_count,\n p2sh_output_count,\n p2sh_p2wpkh_output_count,\n p2sh_p2wsh_output_count,\n p2wpkh_output_count,\n p2wsh_output_count,\n p2tr_output_count,\n };\n\n return this.params;\n }\n\n getOutputCount() {\n return (\n this.params.p2pkh_output_count +\n this.params.p2sh_output_count +\n this.params.p2sh_p2wpkh_output_count +\n this.params.p2sh_p2wsh_output_count +\n this.params.p2wpkh_output_count +\n this.params.p2wsh_output_count +\n this.params.p2tr_output_count\n );\n }\n\n getSizeBasedOnInputType() {\n // In most cases the input size is predictable. For multisig inputs we need to perform a detailed calculation\n let inputSize = 0; // in virtual bytes\n let inputWitnessSize = 0;\n let redeemScriptSize;\n switch (this.params.input_script) {\n case 'p2pkh':\n inputSize = this.P2PKH_IN_SIZE;\n break;\n case 'p2sh-p2wpkh':\n inputSize = this.P2SH_P2WPKH_IN_SIZE;\n inputWitnessSize = 107; // size(signature) + signature + size(pubkey) + pubkey\n break;\n case 'p2wpkh':\n inputSize = this.P2WPKH_IN_SIZE;\n inputWitnessSize = 107; // size(signature) + signature + size(pubkey) + pubkey\n break;\n case 'p2tr': // Only consider the cooperative taproot signing path assume multisig is done via aggregate signatures\n inputSize = this.P2TR_IN_SIZE;\n inputWitnessSize = 65; // getSizeOfletInt(schnorrSignature) + schnorrSignature\n break;\n case 'p2sh':\n redeemScriptSize =\n 1 + // OP_M\n this.params.input_n * (1 + this.PUBKEY_SIZE) + // OP_PUSH33 <pubkey>\n 1 + // OP_N\n 1; // OP_CHECKMULTISIG\n // eslint-disable-next-line no-case-declarations\n const scriptSigSize =\n 1 + // size(0)\n this.params.input_m * (1 + this.SIGNATURE_SIZE) + // size(SIGNATURE_SIZE) + signature\n this.getSizeOfScriptLengthElement(redeemScriptSize) +\n redeemScriptSize;\n inputSize = 32 + 4 + this.getSizeOfletInt(scriptSigSize) + scriptSigSize + 4;\n break;\n case 'p2sh-p2wsh':\n case 'p2wsh':\n redeemScriptSize =\n 1 + // OP_M\n this.params.input_n * (1 + this.PUBKEY_SIZE) + // OP_PUSH33 <pubkey>\n 1 + // OP_N\n 1; // OP_CHECKMULTISIG\n inputWitnessSize =\n 1 + // size(0)\n this.params.input_m * (1 + this.SIGNATURE_SIZE) + // size(SIGNATURE_SIZE) + signature\n this.getSizeOfScriptLengthElement(redeemScriptSize) +\n redeemScriptSize;\n inputSize =\n 36 + // outpoint (spent UTXO ID)\n inputWitnessSize / 4 + // witness program\n 4; // nSequence\n if (this.params.input_script === 'p2sh-p2wsh') {\n inputSize += 32 + 3; // P2SH wrapper (redeemscript hash) + overhead?\n }\n break;\n default:\n assertUnreachable(this.params.input_script);\n }\n\n return {\n inputSize,\n inputWitnessSize,\n };\n }\n\n calcTxSize(opts: Partial<TxSizerParams>) {\n this.prepareParams(opts);\n const output_count = this.getOutputCount();\n const { inputSize, inputWitnessSize } = this.getSizeBasedOnInputType();\n\n const txVBytes =\n this.getTxOverheadVBytes(this.params.input_script, this.params.input_count, output_count) +\n inputSize * this.params.input_count +\n this.P2PKH_OUT_SIZE * this.params.p2pkh_output_count +\n this.P2SH_OUT_SIZE * this.params.p2sh_output_count +\n this.P2SH_P2WPKH_OUT_SIZE * this.params.p2sh_p2wpkh_output_count +\n this.P2SH_P2WSH_OUT_SIZE * this.params.p2sh_p2wsh_output_count +\n this.P2WPKH_OUT_SIZE * this.params.p2wpkh_output_count +\n this.P2WSH_OUT_SIZE * this.params.p2wsh_output_count +\n this.P2TR_OUT_SIZE * this.params.p2tr_output_count;\n\n const txBytes =\n this.getTxOverheadExtraRawBytes(this.params.input_script, this.params.input_count) +\n txVBytes +\n inputWitnessSize * this.params.input_count;\n const txWeight = txVBytes * 4;\n\n return { txVBytes, txBytes, txWeight };\n }\n\n estimateFee(vbyte: number, satVb: number) {\n if (isNaN(vbyte) || isNaN(satVb)) {\n throw new Error('Parameters should be numbers');\n }\n return vbyte * satVb;\n }\n\n formatFeeRange(fee: number, multiplier: number) {\n if (isNaN(fee) || isNaN(multiplier)) {\n throw new Error('Parameters should be numbers');\n }\n\n if (multiplier < 0) {\n throw new Error('Multiplier cant be negative');\n }\n\n const multipliedFee = fee * multiplier;\n\n return fee - multipliedFee + ' - ' + (fee + multipliedFee);\n }\n}\n","import BigNumber from 'bignumber.js';\nimport { validate } from 'bitcoin-address-validation';\n\nimport { BTC_P2WPKH_DUST_AMOUNT } from '@leather.io/constants';\nimport { Money } from '@leather.io/models';\nimport { createMoney, sumMoney } from '@leather.io/utils';\n\nimport { BitcoinError } from '../validation/bitcoin-error';\nimport { filterUneconomicalUtxos, getSizeInfo, getUtxoTotal } from './coin-selection.utils';\n\nexport interface CoinSelectionOutput {\n value: bigint;\n address?: string;\n}\n\nexport interface CoinSelectionRecipient {\n address: string;\n amount: Money;\n}\n\nexport interface DetermineUtxosForSpendArgs<T> {\n feeRate: number;\n recipients: CoinSelectionRecipient[];\n utxos: T[];\n}\nexport function determineUtxosForSpendAll<T extends { value: number; txid: string }>({\n feeRate,\n recipients,\n utxos,\n}: DetermineUtxosForSpendArgs<T>) {\n recipients.forEach(recipient => {\n if (!validate(recipient.address)) throw new BitcoinError('InvalidAddress');\n });\n const filteredUtxos = filterUneconomicalUtxos({ utxos, feeRate, recipients });\n\n const sizeInfo = getSizeInfo({\n inputLength: filteredUtxos.length,\n isSendMax: true,\n recipients,\n });\n\n // Fee has already been deducted from the amount with send all\n const outputs = recipients.map(({ address, amount }) => ({\n value: BigInt(amount.amount.toNumber()),\n address,\n }));\n\n const fee = Math.ceil(sizeInfo.txVBytes * feeRate);\n\n return {\n inputs: filteredUtxos,\n outputs,\n size: sizeInfo.txVBytes,\n fee: createMoney(new BigNumber(fee), 'BTC'),\n };\n}\n\nexport function determineUtxosForSpend<T extends { value: number; txid: string }>({\n feeRate,\n recipients,\n utxos,\n}: DetermineUtxosForSpendArgs<T>) {\n recipients.forEach(recipient => {\n if (!validate(recipient.address)) throw new BitcoinError('InvalidAddress');\n });\n const filteredUtxos = filterUneconomicalUtxos({\n utxos: utxos.sort((a, b) => b.value - a.value),\n feeRate,\n recipients,\n });\n if (!filteredUtxos.length) throw new BitcoinError('InsufficientFunds');\n\n const amount = sumMoney(recipients.map(recipient => recipient.amount));\n\n // Prepopulate with first utxo, at least one is needed\n const neededUtxos: T[] = [filteredUtxos[0]];\n\n function estimateTransactionSize() {\n return getSizeInfo({\n inputLength: neededUtxos.length,\n recipients,\n });\n }\n\n function hasSufficientUtxosForTx() {\n const txEstimation = estimateTransactionSize();\n const neededAmount = new BigNumber(txEstimation.txVBytes * feeRate).plus(amount.amount);\n return getUtxoTotal(neededUtxos).isGreaterThanOrEqualTo(neededAmount);\n }\n\n function getRemainingUnspentUtxos() {\n return filteredUtxos.filter(utxo => !neededUtxos.includes(utxo));\n }\n\n while (!hasSufficientUtxosForTx()) {\n const [nextUtxo] = getRemainingUnspentUtxos();\n if (!nextUtxo) throw new BitcoinError('InsufficientFunds');\n neededUtxos.push(nextUtxo);\n }\n\n const fee = Math.ceil(\n new BigNumber(estimateTransactionSize().txVBytes).multipliedBy(feeRate).toNumber()\n );\n\n const changeAmount =\n BigInt(getUtxoTotal(neededUtxos).toString()) - BigInt(amount.amount.toNumber()) - BigInt(fee);\n\n const changeUtxos: CoinSelectionOutput[] =\n changeAmount > BTC_P2WPKH_DUST_AMOUNT\n ? [\n {\n value: changeAmount,\n },\n ]\n : [];\n\n const outputs: CoinSelectionOutput[] = [\n ...recipients.map(({ address, amount }) => ({\n value: BigInt(amount.amount.toNumber()),\n address,\n })),\n ...changeUtxos,\n ];\n\n return {\n filteredUtxos,\n inputs: neededUtxos,\n outputs,\n size: estimateTransactionSize().txVBytes,\n fee: createMoney(new BigNumber(fee), 'BTC'),\n ...estimateTransactionSize(),\n };\n}\n","import { AverageBitcoinFeeRates, Money } from '@leather.io/models';\n\nimport {\n CoinSelectionRecipient,\n DetermineUtxosForSpendArgs,\n determineUtxosForSpend,\n determineUtxosForSpendAll,\n} from '../coin-selection/coin-selection';\n\ntype GetBitcoinTransactionFeeArgs = DetermineUtxosForSpendArgs<{ value: number; txid: string }> & {\n isSendingMax?: boolean;\n};\n\nexport function getBitcoinTransactionFee({ isSendingMax, ...props }: GetBitcoinTransactionFeeArgs) {\n try {\n const { fee } = isSendingMax\n ? determineUtxosForSpendAll({ ...props })\n : determineUtxosForSpend({ ...props });\n return fee;\n } catch {\n return null;\n }\n}\n\nexport interface BitcoinFees {\n blockchain: 'bitcoin';\n high: { fee: Money | null; feeRate: number };\n standard: { fee: Money | null; feeRate: number };\n low: { fee: Money | null; feeRate: number };\n}\n\nexport interface GetBitcoinFeesArgs {\n feeRates: AverageBitcoinFeeRates;\n isSendingMax?: boolean;\n recipients: CoinSelectionRecipient[];\n utxos: { value: number; txid: string }[];\n}\nexport function getBitcoinFees({ feeRates, isSendingMax, recipients, utxos }: GetBitcoinFeesArgs) {\n const defaultArgs = {\n isSendingMax,\n recipients,\n utxos,\n };\n\n const highFeeRate = feeRates.fastestFee.toNumber();\n const standardFeeRate = feeRates.halfHourFee.toNumber();\n const lowFeeRate = feeRates.hourFee.toNumber();\n\n const highFeeValue = getBitcoinTransactionFee({\n ...defaultArgs,\n feeRate: highFeeRate,\n });\n const standardFeeValue = getBitcoinTransactionFee({\n ...defaultArgs,\n feeRate: standardFeeRate,\n });\n const lowFeeValue = getBitcoinTransactionFee({\n ...defaultArgs,\n feeRate: lowFeeRate,\n });\n\n return {\n high: { feeRate: highFeeRate, fee: highFeeValue },\n standard: { feeRate: standardFeeRate, fee: standardFeeValue },\n low: { feeRate: lowFeeRate, fee: lowFeeValue },\n };\n}\n","import { createBitcoinAddress } from '../validation/bitcoin-address';\n\n// maybe these should be in mono/config?\n// from extension/tests/mocks/constants\nexport const TEST_ACCOUNT_1_NATIVE_SEGWIT_ADDRESS = createBitcoinAddress(\n 'bc1q530dz4h80kwlzywlhx2qn0k6vdtftd93c499yq'\n);\nexport const TEST_ACCOUNT_1_TAPROOT_ADDRESS = createBitcoinAddress(\n 'bc1putuzj9lyfcm8fef9jpy85nmh33cxuq9u6wyuk536t9kemdk37yjqmkc0pg'\n);\nexport const TEST_ACCOUNT_2_TAPROOT_ADDRESS = createBitcoinAddress(\n 'bc1pmk2sacpfyy4v5phl8tq6eggu4e8laztep7fsgkkx0nc6m9vydjesaw0g2r'\n);\n\nexport const TEST_TESNET_ACCOUNT_1_NATIVE_SEGWIT_ADDRESS = createBitcoinAddress(\n 'tb1q4qgnjewwun2llgken94zqjrx5kpqqycaz5522d'\n);\n\nexport const TEST_TESTNET_ACCOUNT_2_BTC_ADDRESS = createBitcoinAddress(\n 'tb1qr8me8t9gu9g6fu926ry5v44yp0wyljrespjtnz'\n);\n\nexport const TEST_TESTNET_ACCOUNT_2_TAPROOT_ADDRESS = createBitcoinAddress(\n 'tb1pve00jmp43whpqj2wpcxtc7m8wqhz0azq689y4r7h8tmj8ltaj87qj2nj6w'\n);\n\n// coin-selection.spec\nexport const recipientAddress = createBitcoinAddress('tb1qt28eagxcl9gvhq2rpj5slg7dwgxae2dn2hk93m');\nexport const legacyAddress = createBitcoinAddress('15PyZveQd28E2SHZu2ugkWZBp6iER41vXj');\nexport const segwitAddress = createBitcoinAddress('33SVjoCHJovrXxjDKLFSXo1h3t5KgkPzfH');\nexport const taprootAddress = createBitcoinAddress(\n 'tb1parwmj7533de3k2fw2kntyqacspvhm67qnjcmpqnnpfvzu05l69nsczdywd'\n);\nexport const invalidAddress = 'whoop-de-da-boop-da-de-not-a-bitcoin-address';\n\nexport const inValidCharactersAddress = createBitcoinAddress(\n 'tb1&*%wmj7533de3k2fw2kntyqacspvhm67qnjcmpqnnpfvzu05l69nsczdywd'\n);\nexport const inValidLengthAddress = createBitcoinAddress('tb1parwmj7533de3k2fw2kntyqacspvhm67wd');\n","import { Network, getAddressInfo, validate } from 'bitcoin-address-validation';\nimport { z } from 'zod';\n\nimport type { BitcoinNetworkModes } from '@leather.io/models';\nimport { isEmptyString, isUndefined } from '@leather.io/utils';\n\nexport function nonEmptyStringValidator(message = '') {\n return z\n .string()\n .refine((value: string) => value !== undefined && value.trim() !== '', { message });\n}\n\nexport function btcAddressValidator() {\n return z.string().refine(\n (value: string) => {\n if (isUndefined(value) || isEmptyString(value)) return true;\n return validate(value);\n },\n { message: 'Bitcoin address is not valid' }\n );\n}\n\nexport function getNetworkTypeFromAddress(address: string) {\n return getAddressInfo(address).network as BitcoinNetworkModes;\n}\n\nfunction btcAddressNetworkValidatorFactory(network: BitcoinNetworkModes) {\n function getAddressNetworkType(network: BitcoinNetworkModes): Network {\n // Signet uses testnet address format, this parsing is to please the\n // validation library\n if (network === 'signet') return Network.testnet;\n return network as Network;\n }\n return (value?: string) => {\n if (isUndefined(value) || isEmptyString(value)) return true;\n return validate(value, getAddressNetworkType(network));\n };\n}\n\nexport function btcAddressNetworkValidator(network: BitcoinNetworkModes) {\n return z.string().refine(btcAddressNetworkValidatorFactory(network), {\n message: 'Address is for incorrect network',\n });\n}\n","import { ripemd160 } from '@noble/hashes/ripemd160';\nimport { sha256 } from '@noble/hashes/sha256';\nimport { base58check } from '@scure/base';\n\nimport { deriveBip39SeedFromMnemonic, deriveRootBip32Keychain } from '@leather.io/crypto';\nimport { NetworkModes } from '@leather.io/models';\n\n/**\n * @deprecated\n * Use `deriveBip39MnemonicFromSeed` from `@leather.io/crypto`\n */\nexport const deriveBtcBip49SeedFromMnemonic = deriveBip39SeedFromMnemonic;\n\n/**\n * @deprecated\n * Use `deriveRootBip32Keychain` from `@leather.io/crypto`\n */\nexport const deriveRootBtcKeychain = deriveRootBip32Keychain;\n\nexport function decodeCompressedWifPrivateKey(key: string) {\n // https://en.bitcoinwiki.org/wiki/Wallet_import_format\n // Decode Compressed WIF format private key\n const compressedWifFormatPrivateKey = base58check(sha256).decode(key);\n // Drop leading network byte, trailing public key SEC format byte\n return compressedWifFormatPrivateKey.slice(1, compressedWifFormatPrivateKey.length - 1);\n}\n\n// https://en.bitcoin.it/wiki/List_of_address_prefixes\nconst payToScriptHashMainnetPrefix = 0x05;\nexport const payToScriptHashTestnetPrefix = 0xc4;\n\nconst payToScriptHashPrefixMap: Record<NetworkModes, number> = {\n mainnet: payToScriptHashMainnetPrefix,\n testnet: payToScriptHashTestnetPrefix,\n};\n\nfunction hash160(input: Uint8Array) {\n return ripemd160(sha256(input));\n}\n\nexport function makePayToScriptHashKeyHash(publicKey: Uint8Array) {\n return hash160(publicKey);\n}\n\nexport function makePayToScriptHashAddressBytes(keyHash: Uint8Array) {\n const redeemScript = Uint8Array.from([\n ...Uint8Array.of(0x00),\n ...Uint8Array.of(keyHash.length),\n ...keyHash,\n ]);\n return hash160(redeemScript);\n}\n\nexport function makePayToScriptHashAddress(addressBytes: Uint8Array, network: NetworkModes) {\n const networkByte = payToScriptHashPrefixMap[network];\n const addressWithPrefix = Uint8Array.from([networkByte, ...addressBytes]);\n return base58check(sha256).encode(addressWithPrefix);\n}\n\nexport function publicKeyToPayToScriptHashAddress(publicKey: Uint8Array, network: NetworkModes) {\n const hash = makePayToScriptHashKeyHash(publicKey);\n const addrBytes = makePayToScriptHashAddressBytes(hash);\n return makePayToScriptHashAddress(addrBytes, network);\n}\n","import { BitcoinAddress } from '@leather.io/models';\nimport { createMoney, sumNumbers } from '@leather.io/utils';\n\nimport { inferPaymentTypeFromAddress } from '../utils/bitcoin.utils';\nimport { PsbtInput } from './psbt-inputs';\nimport { PsbtOutput } from './psbt-outputs';\n\nfunction calculateAddressInputsTotal(addresses: string[], inputs: PsbtInput[]) {\n const sumsByAddress = addresses.map(address =>\n inputs\n .filter(input => input.address === address)\n .map(input => input.value)\n .reduce((acc, curVal) => acc + curVal, 0)\n );\n\n return createMoney(sumNumbers(sumsByAddress), 'BTC');\n}\n\nfunction calculateAddressOutputsTotal(addresses: string[], outputs: PsbtOutput[]) {\n const sumsByAddress = addresses.map(address =>\n outputs\n .filter(output => output.address === address)\n .map(output => Number(output.value))\n .reduce((acc, curVal) => acc + curVal, 0)\n );\n return createMoney(sumNumbers(sumsByAddress), 'BTC');\n}\n\nfunction calculatePsbtInputsTotal(inputs: PsbtInput[]) {\n return createMoney(sumNumbers(inputs.map(input => input.value)), 'BTC');\n}\n\nfunction calculatePsbtOutputsTotal(outputs: PsbtOutput[]) {\n return createMoney(sumNumbers(outputs.map(output => output.value)), 'BTC');\n}\n\ninterface GetPsbtTotalsProps {\n psbtAddresses: BitcoinAddress[];\n parsedInputs: PsbtInput[];\n parsedOutputs: PsbtOutput[];\n}\nexport function getPsbtTotals({ psbtAddresses, parsedInputs, parsedOutputs }: GetPsbtTotalsProps) {\n const nativeSegwitAddresses = psbtAddresses.filter(\n addr => inferPaymentTypeFromAddress(addr) === 'p2wpkh'\n );\n const taprootAddresses = psbtAddresses.filter(\n addr => inferPaymentTypeFromAddress(addr) === 'p2tr'\n );\n\n return {\n inputsTotalNativeSegwit: calculateAddressInputsTotal(nativeSegwitAddresses, parsedInputs),\n inputsTotalTaproot: calculateAddressInputsTotal(taprootAddresses, parsedInputs),\n outputsTotalNativeSegwit: calculateAddressOutputsTotal(nativeSegwitAddresses, parsedOutputs),\n outputsTotalTaproot: calculateAddressOutputsTotal(taprootAddresses, parsedOutputs),\n psbtInputsTotal: calculatePsbtInputsTotal(parsedInputs),\n psbtOutputsTotal: calculatePsbtOutputsTotal(parsedOutputs),\n };\n}\n","import { bytesToHex } from '@noble/hashes/utils';\nimport type { TransactionInput } from '@scure/btc-signer/psbt';\n\nimport type { BitcoinAddress, BitcoinNetworkModes, InscriptionAsset } from '@leather.io/models';\nimport { isDefined, isUndefined } from '@leather.io/utils';\n\nimport { getBtcSignerLibNetworkConfigByMode } from '../utils/bitcoin.network';\nimport { getBitcoinInputAddress, getBitcoinInputValue } from '../utils/bitcoin.utils';\n\nexport interface PsbtInput {\n address: BitcoinAddress;\n index?: number;\n // TODO: inject inscription later on. getParsedInputs should be a pure function\n inscription?: InscriptionAsset;\n isMutable: boolean;\n toSign: boolean;\n txid: string;\n value: number;\n bip32Derivation: TransactionInput['bip32Derivation'];\n tapBip32Derivation: TransactionInput['tapBip32Derivation'];\n}\n\ninterface GetParsedInputsArgs {\n inputs: TransactionInput[];\n indexesToSign?: number[];\n networkMode: BitcoinNetworkModes;\n psbtAddresses: BitcoinAddress[];\n}\n\ninterface GetParsedInputsResponse {\n isPsbtMutable: boolean;\n parsedInputs: PsbtInput[];\n}\nexport function getParsedInputs({\n inputs,\n indexesToSign,\n networkMode,\n psbtAddresses,\n}: GetParsedInputsArgs): GetParsedInputsResponse {\n const bitcoinNetwork = getBtcSignerLibNetworkConfigByMode(networkMode);\n\n const signAll = isUndefined(indexesToSign);\n const psbtInputs = inputs.map((input, i) => {\n const bitcoinAddress = isDefined(input.index)\n ? getBitcoinInputAddress(input, bitcoinNetwork)\n : null;\n if (bitcoinAddress === null) {\n throw new Error('PSBT input has unsupported bitcoin address');\n }\n const isCurrentAddress = psbtAddresses.includes(bitcoinAddress);\n // Flags when not signing ALL inputs/outputs (NONE, SINGLE, and ANYONECANPAY)\n const canChange =\n isCurrentAddress &&\n !(!input.sighashType || input.sighashType === 0 || input.sighashType === 1);\n // Should we check the sighashType here before it gets to the signing lib?\n const toSignAll = isCurrentAddress && signAll;\n const toSignIndex = isCurrentAddress && !signAll && indexesToSign.includes(i);\n\n return {\n address: bitcoinAddress,\n index: input.index,\n bip32Derivation: input.bip32Derivation,\n tapBip32Derivation: input.tapBip32Derivation,\n // inscription: inscriptions[i],\n isMutable: canChange,\n toSign: toSignAll || toSignIndex,\n txid: input.txid ? bytesToHex(input.txid) : '',\n value: isDefined(input.index) ? getBitcoinInputValue(input) : 0,\n };\n });\n\n const isPsbtMutable = psbtInputs.some(input => input.isMutable);\n\n return { isPsbtMutable, parsedInputs: psbtInputs };\n}\n","import type { TransactionOutput } from '@scure/btc-signer/psbt';\n\nimport { BitcoinAddress, BitcoinNetworkModes } from '@leather.io/models';\nimport { isDefined, isUndefined } from '@leather.io/utils';\n\nimport { getBtcSignerLibNetworkConfigByMode } from '../utils/bitcoin.network';\nimport { getAddressFromOutScript } from '../utils/bitcoin.utils';\n\nexport interface PsbtOutput {\n address: BitcoinAddress;\n isMutable: boolean;\n toSign: boolean;\n value: number;\n}\n\ninterface GetParsedOutputsArgs {\n isPsbtMutable: boolean;\n outputs: TransactionOutput[];\n networkMode: BitcoinNetworkModes;\n psbtAddresses: BitcoinAddress[];\n}\n\nexport function getParsedOutputs({\n isPsbtMutable,\n outputs,\n networkMode,\n psbtAddresses,\n}: GetParsedOutputsArgs): PsbtOutput[] {\n const bitcoinNetwork = getBtcSignerLibNetworkConfigByMode(networkMode);\n\n return outputs\n .map(output => {\n if (isUndefined(output.script)) {\n // TODO: handle error here\n // logger.error('Output has no script');\n return;\n }\n const outputAddress = getAddressFromOutScript(output.script, bitcoinNetwork);\n if (outputAddress === null) {\n throw new Error('PSBT output has unsupported bitcoin address');\n }\n\n const isCurrentAddress = psbtAddresses.includes(outputAddress);\n\n return {\n address: outputAddress,\n isMutable: isPsbtMutable,\n toSign: isCurrentAddress,\n value: Number(output.amount),\n };\n })\n .filter(isDefined);\n}\n","import { BitcoinAddress, BitcoinNetworkModes } from '@leather.io/models';\nimport { createMoney, subtractMoney } from '@leather.io/utils';\n\nimport { getPsbtTxInputs, getPsbtTxOutputs } from '../utils/bitcoin.utils';\nimport { getParsedInputs } from './psbt-inputs';\nimport { getParsedOutputs } from './psbt-outputs';\nimport { getPsbtTotals } from './psbt-totals';\nimport { getPsbtAsTransaction } from './utils';\n\ninterface GetPsbtDetailsArgs {\n psbtHex: string;\n psbtAddresses: BitcoinAddress[];\n networkMode: BitcoinNetworkModes;\n indexesToSign?: number[];\n}\nexport function getPsbtDetails({\n psbtHex,\n networkMode,\n indexesToSign,\n psbtAddresses,\n}: GetPsbtDetailsArgs) {\n const tx = getPsbtAsTransaction(psbtHex);\n const inputs = getPsbtTxInputs(tx);\n const outputs = getPsbtTxOutputs(tx);\n\n const { isPsbtMutable, parsedInputs } = getParsedInputs({\n inputs,\n indexesToSign,\n networkMode,\n psbtAddresses,\n });\n const parsedOutputs = getParsedOutputs({ isPsbtMutable, outputs, networkMode, psbtAddresses });\n\n const {\n inputsTotalNativeSegwit,\n inputsTotalTaproot,\n outputsTotalNativeSegwit,\n outputsTotalTaproot,\n psbtInputsTotal,\n psbtOutputsTotal,\n } = getPsbtTotals({\n psbtAddresses,\n parsedInputs,\n parsedOutputs,\n });\n function getFee() {\n if (psbtInputsTotal.amount.isGreaterThan(psbtOutputsTotal.amount))\n return subtractMoney(psbtInputsTotal, psbtOutputsTotal);\n return createMoney(0, 'BTC');\n }\n return {\n addressNativeSegwitTotal: subtractMoney(inputsTotalNativeSegwit, outputsTotalNativeSegwit),\n addressTaprootTotal: subtractMoney(inputsTotalTaproot, outputsTotalTaproot),\n fee: getFee(),\n isPsbtMutable,\n psbtInputs: parsedInputs,\n psbtOutputs: parsedOutputs,\n };\n}\n","import { hexToBytes } from '@noble/hashes/utils';\nimport * as btc from '@scure/btc-signer';\nimport { RawPSBTV0, RawPSBTV2 } from '@scure/btc-signer/psbt';\n\nimport { isString } from '@leather.io/utils';\n\nexport type RawPsbt = ReturnType<typeof RawPSBTV0.decode>;\n\nexport function getPsbtAsTransaction(psbt: string | Uint8Array) {\n const bytes = isString(psbt) ? hexToBytes(psbt) : psbt;\n return btc.Transaction.fromPSBT(bytes);\n}\n\nexport function getRawPsbt(psbt: string | Uint8Array): ReturnType<typeof RawPSBTV0.decode> {\n const bytes = isString(psbt) ? hexToBytes(psbt) : psbt;\n try {\n return RawPSBTV0.decode(bytes);\n } catch (e1) {\n try {\n return RawPSBTV2.decode(bytes);\n } catch (e2) {\n throw new Error(`Unable to decode PSBT, ${e1 ?? e2}`);\n }\n }\n}\n","import { HARDENED_OFFSET, HDKey } from '@scure/bip32';\nimport * as btc from '@scure/btc-signer';\nimport { P2Ret, P2TROut } from '@scure/btc-signer/payment';\nimport { SigHash } from '@scure/btc-signer/transaction';\nimport * as bitcoin from 'bitcoinjs-lib';\n\nimport {\n DerivationPathDepth,\n appendAddressIndexToPath,\n decomposeDescriptor,\n deriveKeychainFromXpub,\n extractAddressIndexFromPath,\n extractChangeIndexFromPath,\n keyOriginToDerivationPath,\n} from '@leather.io/crypto';\nimport type { BitcoinAddress, BitcoinNetworkModes, ValueOf } from '@leather.io/models';\nimport { PaymentTypes, signatureHash } from '@leather.io/rpc';\nimport { hexToNumber, toHexString } from '@leather.io/utils';\n\nimport { getTaprootPaymentFromAddressIndex } from '../payments/p2tr-address-gen';\nimport { getNativeSegwitPaymentFromAddressIndex } from '../payments/p2wpkh-address-gen';\nimport {\n SupportedPaymentType,\n ecdsaPublicKeyToSchnorr,\n extractExtendedPublicKeyFromPolicy,\n inferPaymentTypeFromPath,\n whenSupportedPaymentType,\n} from '../utils/bitcoin.utils';\n\nexport type AllowedSighashTypes = ValueOf<typeof signatureHash> | SigHash;\n\nexport interface BitcoinAccountKeychain {\n descriptor: string;\n masterKeyFingerprint: string;\n keyOrigin: string;\n keychain: HDKey;\n xpub: string;\n}\n\nexport type WithDerivePayer<T, P> = T & { derivePayer: (args: BitcoinPayerInfo) => P };\n\nexport interface BitcoinSigner<Payment> {\n network: BitcoinNetworkModes;\n payment: Payment;\n keychain: HDKey;\n derivationPath: string;\n address: BitcoinAddress;\n publicKey: Uint8Array;\n sign(tx: btc.Transaction): void;\n signIndex(tx: btc.Transaction, index: number, allowedSighash?: AllowedSighashTypes[]): void;\n}\n\nexport interface BitcoinPayerBase {\n paymentType: PaymentTypes;\n network: BitcoinNetworkModes;\n address: BitcoinAddress;\n keyOrigin: string;\n masterKeyFingerprint: string;\n publicKey: Uint8Array;\n}\n\nexport interface BitcoinNativeSegwitPayer extends BitcoinPayerBase {\n paymentType: 'p2wpkh';\n payment: P2Ret;\n}\n\nexport interface BitcoinTaprootPayer extends BitcoinPayerBase {\n paymentType: 'p2tr';\n payment: P2TROut;\n}\n\nexport type BitcoinPayer = BitcoinNativeSegwitPayer | BitcoinTaprootPayer;\n\nexport function initializeBitcoinAccountKeychainFromDescriptor(\n descriptor: string\n): BitcoinAccountKeychain {\n const { fingerprint, keyOrigin } = decomposeDescriptor(descriptor);\n return {\n descriptor,\n xpub: extractExtendedPublicKeyFromPolicy(descriptor),\n keyOrigin,\n masterKeyFingerprint: fingerprint,\n keychain: deriveKeychainFromXpub(extractExtendedPublicKeyFromPolicy(descriptor)),\n };\n}\n\nexport interface BitcoinPayerInfo {\n change: number;\n addressIndex: number;\n}\nexport function deriveBitcoinPayerFromAccount(descriptor: string, network: BitcoinNetworkModes) {\n const { fingerprint, keyOrigin } = decomposeDescriptor(descriptor);\n const accountKeychain = deriveKeychainFromXpub(extractExtendedPublicKeyFromPolicy(descriptor));\n const paymentType = inferPaymentTypeFromPath(keyOrigin) as SupportedPaymentType;\n\n if (accountKeychain.depth !== DerivationPathDepth.Account)\n throw new Error('Keychain passed is not an account');\n\n return ({ change, addressIndex }: BitcoinPayerInfo) => {\n const childKeychain = accountKeychain.deriveChild(change).deriveChild(addressIndex);\n\n const derivePayerFromAccount = whenSupportedPaymentType(paymentType)({\n p2tr: getTaprootPaymentFromAddressIndex,\n p2wpkh: getNativeSegwitPaymentFromAddressIndex,\n });\n\n const payment = derivePayerFromAccount(childKeychain, network);\n\n return {\n keyOrigin: appendAddressIndexToPath(keyOrigin, change, addressIndex),\n masterKeyFingerprint: fingerprint,\n paymentType,\n network,\n payment,\n get address() {\n if (!payment.address) throw new Error('Payment address could not be derived');\n return payment.address;\n },\n get publicKey() {\n if (!childKeychain.publicKey) throw new Error('Public key could not be derived');\n return childKeychain.publicKey;\n },\n };\n };\n}\n\ninterface BtcSignerDerivationPath {\n fingerprint: number;\n path: number[];\n}\nexport type BtcSignerDefaultBip32Derivation = [Uint8Array, BtcSignerDerivationPath];\nexport type BtcSignerTapBip32Derivation = [\n Uint8Array,\n { hashes: Uint8Array[]; der: BtcSignerDerivationPath },\n];\n\ntype BtcSignerBip32Derivation = BtcSignerDefaultBip32Derivation | BtcSignerTapBip32Derivation;\n\ntype PayerToBip32DerivationArgs = Pick<\n BitcoinPayer,\n 'masterKeyFingerprint' | 'keyOrigin' | 'publicKey'\n>;\n\n/**\n * @example\n * ```ts\n * tx.addInput({\n * ...input,\n * bip32Derivation: [payerToBip32Derivation(payer)],\n * })\n * ```\n */\nexport function payerToBip32Derivation(\n args: PayerToBip32DerivationArgs\n): BtcSignerDefaultBip32Derivation {\n return [\n args.publicKey,\n {\n fingerprint: hexToNumber(args.masterKeyFingerprint),\n path: btc.bip32Path(keyOriginToDerivationPath(args.keyOrigin)),\n },\n ];\n}\n\n/**\n * @example\n * ```ts\n * tx.addInput({\n * ...input,\n * tapBip32Derivation: [payerToTapBip32Derivation(payer)],\n * })\n * ```\n */\nexport function payerToTapBip32Derivation(\n args: PayerToBip32DerivationArgs\n): BtcSignerTapBip32Derivation {\n return [\n // TODO: @kyranjamie to default to schnoor when TR so conversion isn't\n // necessary here?\n ecdsaPublicKeyToSchnorr(args.publicKey),\n {\n hashes: [],\n der: {\n fingerprint: hexToNumber(args.masterKeyFingerprint),\n path: btc.bip32Path(keyOriginToDerivationPath(args.keyOrigin)),\n },\n },\n ];\n}\n\ntype PsbtInputBitcoinJsLib = bitcoin.Psbt['data']['inputs']['0'];\n\ntype TapBip32DerivationBitcoinJsLib = NonNullable<PsbtInputBitcoinJsLib['tapBip32Derivation']>['0'];\n\nexport function payerToTapBip32DerivationBitcoinJsLib(\n args: PayerToBip32DerivationArgs\n): TapBip32DerivationBitcoinJsLib {\n return {\n masterFingerprint: Buffer.from(args.masterKeyFingerprint, 'hex'),\n path: keyOriginToDerivationPath(args.keyOrigin),\n leafHashes: [],\n pubkey: Buffer.from(ecdsaPublicKeyToSchnorr(args.publicKey)),\n };\n}\n\ntype Bip32DerivationBitcoinJsLib = NonNullable<PsbtInputBitcoinJsLib['bip32Derivation']>['0'];\n\nexport function payerToBip32DerivationBitcoinJsLib(\n args: PayerToBip32DerivationArgs\n): Bip32DerivationBitcoinJsLib {\n return {\n masterFingerprint: Buffer.from(args.masterKeyFingerprint, 'hex'),\n path: keyOriginToDerivationPath(args.keyOrigin),\n pubkey: Buffer.from(args.publicKey),\n };\n}\n\nexport function extractPayerInfoFromDerivationPath(path: string) {\n return {\n change: extractChangeIndexFromPath(path),\n addressIndex: extractAddressIndexFromPath(path),\n };\n}\n\n/**\n * @description\n * Turns key format from @scure/btc-signer lib back into key origin string\n * @example\n * ```ts\n * const [inputOne] = getPsbtTxInputs(tx);\n * const keyOrigin = serializeKeyOrigin(inputOne.bip32Derivation[0][1]);\n * ```\n */\nexport function serializeKeyOrigin({ fingerprint, path }: BtcSignerDerivationPath) {\n const values = path.map(num => (num >= HARDENED_OFFSET ? num - HARDENED_OFFSET + \"'\" : num));\n return `${toHexString(fingerprint)}/${values.join('/')}`;\n}\n\n/**\n * @description\n * Of a given set of a `tx.input`s bip32 derivation paths from\n * `@scure/btc-signer`, serialize the paths back to the string format used\n * internally\n */\nexport function extractRequiredKeyOrigins(derivation: BtcSignerBip32Derivation[]) {\n return derivation.map(([_pubkey, path]) =>\n serializeKeyOrigin('hashes' in path ? path.der : path)\n );\n}\n","import * as btc from '@scure/btc-signer';\n\nimport {\n CoinSelectionRecipient,\n determineUtxosForSpend,\n determineUtxosForSpendAll,\n} from '../coin-selection/coin-selection';\nimport {\n BitcoinNativeSegwitPayer,\n BitcoinTaprootPayer,\n payerToBip32Derivation,\n payerToTapBip32Derivation,\n} from '../signer/bitcoin-signer';\nimport { BtcSignerNetwork } from '../utils/bitcoin.network';\nimport { BitcoinError } from '../validation/bitcoin-error';\n\nexport interface GenerateBitcoinUnsignedTransactionArgs<T> {\n feeRate: number;\n isSendingMax?: boolean;\n network: BtcSignerNetwork;\n recipients: CoinSelectionRecipient[];\n utxos: T[];\n changeAddress: string;\n payerLookup(keyOrigin: string): BitcoinNativeSegwitPayer | BitcoinTaprootPayer | undefined;\n}\nexport function generateBitcoinUnsignedTransaction<\n T extends { txid: string; vout: number; value: number; keyOrigin: string },\n>({\n feeRate,\n isSendingMax,\n network,\n recipients,\n changeAddress,\n utxos,\n payerLookup,\n}: GenerateBitcoinUnsignedTransactionArgs<T>) {\n const determineUtxosArgs = { feeRate, recipients, utxos };\n const { inputs, outputs, fee } = isSendingMax\n ? determineUtxosForSpendAll(determineUtxosArgs)\n : determineUtxosForSpend(determineUtxosArgs);\n\n if (!inputs.length) throw new BitcoinError('NoInputsToSign');\n if (!outputs.length) throw new BitcoinError('NoOutputsToSign');\n\n const tx = new btc.Transaction();\n\n for (const input of inputs) {\n const payer = payerLookup(input.keyOrigin);\n\n if (!payer) {\n // eslint-disable-next-line no-console\n console.log(`No payer found for input with keyOrigin ${input.keyOrigin}`);\n continue;\n }\n\n const bip32Derivation =\n payer.paymentType === 'p2tr'\n ? { tapBip32Derivation: [payerToTapBip32Derivation(payer)] }\n : { bip32Derivation: [payerToBip32Derivation(payer)] };\n\n const tapInternalKey =\n payer.paymentType === 'p2tr' ? { tapInternalKey: payer.payment.tapInternalKey } : {};\n\n tx.addInput({\n txid: input.txid,\n index: input.vout,\n witnessUtxo: {\n script: payer.payment.script,\n amount: BigInt(input.value),\n },\n ...bip32Derivation,\n ...tapInternalKey,\n });\n }\n\n outputs.forEach(output => {\n // When coin selection returns an output with no address,\n // we assume it is a change output\n if (!output.address) {\n tx.addOutputAddress(changeAddress, BigInt(output.value), network);\n return;\n }\n tx.addOutputAddress(output.address, BigInt(output.value), network);\n });\n\n return { tx, hex: tx.hex, psbt: tx.toPSBT(), inputs, fee };\n}\n","import { BITCOIN_MINIMUM_SPEND_IN_SATS } from '@leather.io/constants';\nimport { Money } from '@leather.io/models';\n\ninterface isBtcBalanceSufficientParams {\n desiredSpend: Money;\n maxSpend: Money;\n}\nexport function isBtcBalanceSufficient({ desiredSpend, maxSpend }: isBtcBalanceSufficientParams) {\n return !desiredSpend.amount.isGreaterThan(maxSpend.amount);\n}\n\nexport function isBtcMinimumSpend(desiredSpend: Money) {\n return !desiredSpend.amount.isLessThan(BITCOIN_MINIMUM_SPEND_IN_SATS);\n}\n","import { BitcoinAddress, type BitcoinNetworkModes, Money } from '@leather.io/models';\n\nimport { calculateMaxSpend } from '../coin-selection/calculate-max-spend';\nimport { GetBitcoinFeesArgs } from '../fees/bitcoin-fees';\nimport { BitcoinError } from '../validation/bitcoin-error';\nimport { isValidBitcoinAddress, isValidBitcoinNetworkAddress } from './address-validation';\nimport { isBtcBalanceSufficient, isBtcMinimumSpend } from './amount-validation';\n\ninterface BitcoinTransaction extends Omit<GetBitcoinFeesArgs, 'recipients'> {\n amount: Money;\n payer: BitcoinAddress;\n recipient: BitcoinAddress;\n network: BitcoinNetworkModes;\n feeRate: number;\n}\n\nexport function isValidBitcoinTransaction({\n amount,\n payer,\n recipient,\n network,\n utxos,\n feeRate,\n feeRates,\n}: BitcoinTransaction) {\n if (!isValidBitcoinAddress(payer) || !isValidBitcoinAddress(recipient)) {\n throw new BitcoinError('InvalidAddress');\n }\n if (\n !isValidBitcoinNetworkAddress(payer, network) ||\n !isValidBitcoinNetworkAddress(recipient, network)\n ) {\n throw new BitcoinError('InvalidNetworkAddress');\n }\n\n if (!isBtcMinimumSpend(amount)) {\n throw new BitcoinError('InsufficientAmount');\n }\n\n const maxSpend = calculateMaxSpend({ recipient, utxos, feeRate, feeRates });\n if (!isBtcBalanceSufficient({ desiredSpend: amount, maxSpend: maxSpend.amount })) {\n throw new BitcoinError('InsufficientFunds');\n }\n}\n","import { HARDENED_OFFSET, HDKey } from '@scure/bip32';\nimport { makeTaprootAddressIndexDerivationPath } from 'payments/p2tr-address-gen';\nimport { makeNativeSegwitAddressIndexDerivationPath } from 'payments/p2wpkh-address-gen';\n\nimport { BitcoinNetworkModes } from '@leather.io/models';\n\nimport {\n SupportedPaymentType,\n getNativeSegwitAddress,\n getTaprootAddress,\n inferPaymentTypeFromPath,\n whenSupportedPaymentType,\n} from './bitcoin.utils';\n\nexport function getDescriptorFromKeychain<T extends { keyOrigin: string; xpub: string }>(\n accountKeychain: T\n) {\n switch (inferPaymentTypeFromPath(accountKeychain.keyOrigin)) {\n case 'p2tr':\n return `tr(${accountKeychain.xpub})`;\n case 'p2wpkh':\n return `wpkh(${accountKeychain.xpub})`;\n default:\n return undefined;\n }\n}\n\nexport function inferPaymentTypeFromDescriptor(descriptor: string): SupportedPaymentType {\n const descriptorPrefix = descriptor.split('(')[0];\n switch (descriptorPrefix) {\n case 'tr':\n return 'p2tr';\n case 'wpkh':\n return 'p2wpkh';\n default:\n throw new Error('Unrecognized descriptor');\n }\n}\n\nexport function extractXpubFromDescriptor(descriptor: string) {\n const match = descriptor.match(/\\((xpub[0-9A-Za-z]+)\\)/);\n if (match && match[1]) {\n return match[1];\n }\n throw new Error('Invalid descriptor format');\n}\n\nexport interface DeriveAddressesFromDescriptorArgs {\n accountDescriptor: string;\n network: BitcoinNetworkModes;\n limit?: number; // number of addresses to derive\n}\n\nexport interface DeriveAddressesFromDescriptorResult {\n address: string;\n path: string;\n}\n\nexport function deriveAddressesFromDescriptor({\n accountDescriptor,\n network,\n limit = 1,\n}: DeriveAddressesFromDescriptorArgs): DeriveAddressesFromDescriptorResult[] {\n const accountKeychain = HDKey.fromExtendedKey(extractXpubFromDescriptor(accountDescriptor));\n const paymentType = inferPaymentTypeFromDescriptor(accountDescriptor);\n\n const derivationPathFn = whenSupportedPaymentType(paymentType)({\n p2tr: makeTaprootAddressIndexDerivationPath,\n p2wpkh: makeNativeSegwitAddressIndexDerivationPath,\n });\n\n const results: DeriveAddressesFromDescriptorResult[] = [];\n\n for (let i = 0; i < limit; i++) {\n const address = whenSupportedPaymentType(paymentType)({\n p2tr: getTaprootAddress({ index: i, keychain: accountKeychain, network }),\n p2wpkh: getNativeSegwitAddress({ index: i, keychain: accountKeychain, network }),\n });\n results.push({\n address,\n path: derivationPathFn(network, accountKeychain.index - HARDENED_OFFSET, i),\n });\n }\n return results;\n}\n","import { HARDENED_OFFSET, HDKey } from '@scure/bip32';\n\nimport { BitcoinAddress } from '@leather.io/models';\nimport { createCounter } from '@leather.io/utils';\n\nimport { makeTaprootAddressIndexDerivationPath } from '../payments/p2tr-address-gen';\nimport { makeNativeSegwitAddressIndexDerivationPath } from '../payments/p2wpkh-address-gen';\nimport {\n getNativeSegwitAddress,\n getTaprootAddress,\n inferNetworkFromAddress,\n inferPaymentTypeFromAddress,\n whenSupportedPaymentType,\n} from './bitcoin.utils';\n\ninterface LookUpDerivationByAddressArgs {\n taprootXpub: string;\n nativeSegwitXpub: string;\n iterationLimit: number;\n}\nexport function lookupDerivationByAddress(args: LookUpDerivationByAddressArgs) {\n const { taprootXpub, nativeSegwitXpub, iterationLimit } = args;\n\n const taprootKeychain = HDKey.fromExtendedKey(taprootXpub);\n const nativeSegwitKeychain = HDKey.fromExtendedKey(nativeSegwitXpub);\n\n return (address: BitcoinAddress) => {\n const network = inferNetworkFromAddress(address);\n const paymentType = inferPaymentTypeFromAddress(address);\n\n const accountIndex = whenSupportedPaymentType(paymentType)({\n p2tr: taprootKeychain.index - HARDENED_OFFSET,\n p2wpkh: nativeSegwitKeychain.index - HARDENED_OFFSET,\n });\n\n function getTaprootAddressAtIndex(index: number) {\n return getTaprootAddress({ index, keychain: taprootKeychain, network });\n }\n\n function getNativeSegwitAddressAtIndex(index: number) {\n return getNativeSegwitAddress({ index, keychain: nativeSegwitKeychain, network });\n }\n\n const paymentFn = whenSupportedPaymentType(paymentType)({\n p2tr: getTaprootAddressAtIndex,\n p2wpkh: getNativeSegwitAddressAtIndex,\n });\n\n const derivationPathFn = whenSupportedPaymentType(paymentType)({\n p2tr: makeTaprootAddressIndexDerivationPath,\n p2wpkh: makeNativeSegwitAddressIndexDerivationPath,\n });\n\n const count = createCounter();\n const t0 = performance.now();\n\n while (count.getValue() <= iterationLimit) {\n const currentIndex = count.getValue();\n\n const addressToCheck = paymentFn(currentIndex);\n\n if (addressToCheck !== address) {\n count.increment();\n continue;\n }\n\n const t1 = performance.now();\n\n return {\n status: 'success',\n duration: t1 - t0,\n path: derivationPathFn(network, accountIndex, currentIndex),\n } as const;\n }\n\n return { status: 'failure' } as const;\n };\n}\n"],"mappings":";AAAA,SAAuB,QAAQ,cAAc;AAE7C,SAAS,eAAe;AAiBjB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M,UAAU;AAChB;AAAA,MACF;AAEA,YAAM,KAAK,YAAY,IAAI;AAE3B,aAAO;AAAA,QACL,QAAQ;AAAA,QACR,UAAU,KAAK;AAAA,QACf,MAAM,iBAAiB,SAAS,cAAc,YAAY;AAAA,MAC5D;AAAA,IACF;AAEA,WAAO,EAAE,QAAQ,UAAU;AAAA,EAC7B;AACF;","names":["address","hexToBytes","encode","btc","DerivationPathDepth","btc","DerivationPathDepth","DerivationPathDepth","address","DerivationPathDepth","script","address","hexToBytes","encode","bitcoin","address","script","BigNumber","BigNumber","validate","validate","BigNumber","BigNumber","BigNumber","validate","BTC_P2WPKH_DUST_AMOUNT","createMoney","validate","address","createMoney","BigNumber","BTC_P2WPKH_DUST_AMOUNT","amount","Network","getAddressInfo","validate","isEmptyString","isUndefined","address","network","sha256","createMoney","sumNumbers","address","createMoney","sumNumbers","isDefined","isUndefined","isUndefined","isDefined","isDefined","isUndefined","isUndefined","isDefined","createMoney","hexToBytes","btc","isString","createMoney","btc","DerivationPathDepth","DerivationPathDepth","btc","HARDENED_OFFSET","HDKey","HDKey","address","HARDENED_OFFSET","HARDENED_OFFSET","HDKey","HDKey","address","HARDENED_OFFSET"]}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@leather.io/bitcoin",
3
3
  "author": "Leather.io contact@leather.io",
4
4
  "description": "Shared bitcoin utilities",
5
- "version": "0.34.0",
5
+ "version": "0.35.0",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/leather.io/mono/tree/dev/packages/bitcoin",
8
8
  "repository": {
@@ -32,10 +32,10 @@
32
32
  "just-memoize": "2.2.0",
33
33
  "varuint-bitcoin": "1.1.2",
34
34
  "zod": "4.0.17",
35
- "@leather.io/constants": "0.27.2",
36
- "@leather.io/models": "0.45.0",
37
- "@leather.io/crypto": "1.12.1",
38
- "@leather.io/utils": "0.47.0"
35
+ "@leather.io/constants": "0.27.3",
36
+ "@leather.io/crypto": "1.12.2",
37
+ "@leather.io/utils": "0.48.0",
38
+ "@leather.io/models": "0.46.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "prettier": "3.5.1",
@@ -44,8 +44,8 @@
44
44
  "typescript": "5.8.3",
45
45
  "vitest": "2.1.9",
46
46
  "@leather.io/prettier-config": "0.9.0",
47
- "@leather.io/tsconfig-config": "0.11.0",
48
- "@leather.io/rpc": "2.20.19"
47
+ "@leather.io/rpc": "2.20.21",
48
+ "@leather.io/tsconfig-config": "0.11.0"
49
49
  },
50
50
  "keywords": [
51
51
  "bitcoin",
@@ -1,7 +1,7 @@
1
1
  import { bytesToHex } from '@noble/hashes/utils';
2
2
  import type { TransactionInput } from '@scure/btc-signer/psbt';
3
3
 
4
- import type { BitcoinAddress, BitcoinNetworkModes, Inscription } from '@leather.io/models';
4
+ import type { BitcoinAddress, BitcoinNetworkModes, InscriptionAsset } from '@leather.io/models';
5
5
  import { isDefined, isUndefined } from '@leather.io/utils';
6
6
 
7
7
  import { getBtcSignerLibNetworkConfigByMode } from '../utils/bitcoin.network';
@@ -11,7 +11,7 @@ export interface PsbtInput {
11
11
  address: BitcoinAddress;
12
12
  index?: number;
13
13
  // TODO: inject inscription later on. getParsedInputs should be a pure function
14
- inscription?: Inscription;
14
+ inscription?: InscriptionAsset;
15
15
  isMutable: boolean;
16
16
  toSign: boolean;
17
17
  txid: string;
@@ -1,4 +1,16 @@
1
- import { inferPaymentTypeFromPath } from './bitcoin.utils';
1
+ import { HARDENED_OFFSET, HDKey } from '@scure/bip32';
2
+ import { makeTaprootAddressIndexDerivationPath } from 'payments/p2tr-address-gen';
3
+ import { makeNativeSegwitAddressIndexDerivationPath } from 'payments/p2wpkh-address-gen';
4
+
5
+ import { BitcoinNetworkModes } from '@leather.io/models';
6
+
7
+ import {
8
+ SupportedPaymentType,
9
+ getNativeSegwitAddress,
10
+ getTaprootAddress,
11
+ inferPaymentTypeFromPath,
12
+ whenSupportedPaymentType,
13
+ } from './bitcoin.utils';
2
14
 
3
15
  export function getDescriptorFromKeychain<T extends { keyOrigin: string; xpub: string }>(
4
16
  accountKeychain: T
@@ -12,3 +24,62 @@ export function getDescriptorFromKeychain<T extends { keyOrigin: string; xpub: s
12
24
  return undefined;
13
25
  }
14
26
  }
27
+
28
+ export function inferPaymentTypeFromDescriptor(descriptor: string): SupportedPaymentType {
29
+ const descriptorPrefix = descriptor.split('(')[0];
30
+ switch (descriptorPrefix) {
31
+ case 'tr':
32
+ return 'p2tr';
33
+ case 'wpkh':
34
+ return 'p2wpkh';
35
+ default:
36
+ throw new Error('Unrecognized descriptor');
37
+ }
38
+ }
39
+
40
+ export function extractXpubFromDescriptor(descriptor: string) {
41
+ const match = descriptor.match(/\((xpub[0-9A-Za-z]+)\)/);
42
+ if (match && match[1]) {
43
+ return match[1];
44
+ }
45
+ throw new Error('Invalid descriptor format');
46
+ }
47
+
48
+ export interface DeriveAddressesFromDescriptorArgs {
49
+ accountDescriptor: string;
50
+ network: BitcoinNetworkModes;
51
+ limit?: number; // number of addresses to derive
52
+ }
53
+
54
+ export interface DeriveAddressesFromDescriptorResult {
55
+ address: string;
56
+ path: string;
57
+ }
58
+
59
+ export function deriveAddressesFromDescriptor({
60
+ accountDescriptor,
61
+ network,
62
+ limit = 1,
63
+ }: DeriveAddressesFromDescriptorArgs): DeriveAddressesFromDescriptorResult[] {
64
+ const accountKeychain = HDKey.fromExtendedKey(extractXpubFromDescriptor(accountDescriptor));
65
+ const paymentType = inferPaymentTypeFromDescriptor(accountDescriptor);
66
+
67
+ const derivationPathFn = whenSupportedPaymentType(paymentType)({
68
+ p2tr: makeTaprootAddressIndexDerivationPath,
69
+ p2wpkh: makeNativeSegwitAddressIndexDerivationPath,
70
+ });
71
+
72
+ const results: DeriveAddressesFromDescriptorResult[] = [];
73
+
74
+ for (let i = 0; i < limit; i++) {
75
+ const address = whenSupportedPaymentType(paymentType)({
76
+ p2tr: getTaprootAddress({ index: i, keychain: accountKeychain, network }),
77
+ p2wpkh: getNativeSegwitAddress({ index: i, keychain: accountKeychain, network }),
78
+ });
79
+ results.push({
80
+ address,
81
+ path: derivationPathFn(network, accountKeychain.index - HARDENED_OFFSET, i),
82
+ });
83
+ }
84
+ return results;
85
+ }