@babylonlabs-io/ts-sdk 0.39.5 → 0.41.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.
Files changed (58) hide show
  1. package/dist/PayoutManager-B5bovfkD.cjs.map +1 -1
  2. package/dist/PayoutManager-DChODEOJ.js.map +1 -1
  3. package/dist/{PeginManager-B1qjHs90.js → PeginManager-D9ZZ8wx2.js} +2 -2
  4. package/dist/{PeginManager-B1qjHs90.js.map → PeginManager-D9ZZ8wx2.js.map} +1 -1
  5. package/dist/{PeginManager-Dof0ZO1S.cjs → PeginManager-UqbOj2oV.cjs} +2 -2
  6. package/dist/{PeginManager-Dof0ZO1S.cjs.map → PeginManager-UqbOj2oV.cjs.map} +1 -1
  7. package/dist/assertPsbtUnsignedTxMatches-CzVv57QF.js.map +1 -1
  8. package/dist/assertPsbtUnsignedTxMatches-r1svclbd.cjs.map +1 -1
  9. package/dist/bitcoin-B5aNKtsk.js.map +1 -1
  10. package/dist/bitcoin-CHfKAhcI.cjs.map +1 -1
  11. package/dist/index.cjs +1 -1
  12. package/dist/index.js +74 -73
  13. package/dist/{mempoolApi-CAIge7Nj.js → mempoolApi-C7hkVkym.js} +31 -22
  14. package/dist/mempoolApi-C7hkVkym.js.map +1 -0
  15. package/dist/mempoolApi-DEAS9wVa.cjs +2 -0
  16. package/dist/mempoolApi-DEAS9wVa.cjs.map +1 -0
  17. package/dist/tbv/core/clients/index.cjs +1 -1
  18. package/dist/tbv/core/clients/index.js +33 -32
  19. package/dist/tbv/core/clients/mempool/index.d.ts +1 -1
  20. package/dist/tbv/core/clients/mempool/index.d.ts.map +1 -1
  21. package/dist/tbv/core/clients/mempool/mempoolApi.d.ts +11 -0
  22. package/dist/tbv/core/clients/mempool/mempoolApi.d.ts.map +1 -1
  23. package/dist/tbv/core/clients/vault-provider/auth/serverIdentity.d.ts +0 -7
  24. package/dist/tbv/core/clients/vault-provider/auth/serverIdentity.d.ts.map +1 -1
  25. package/dist/tbv/core/index.cjs +1 -1
  26. package/dist/tbv/core/index.js +74 -73
  27. package/dist/tbv/core/managers/index.cjs +1 -1
  28. package/dist/tbv/core/managers/index.js +1 -1
  29. package/dist/tbv/core/managers/pegin/assertAuthAnchorOpReturn.d.ts +0 -17
  30. package/dist/tbv/core/managers/pegin/assertAuthAnchorOpReturn.d.ts.map +1 -1
  31. package/dist/tbv/core/managers/pegin/index.d.ts +1 -1
  32. package/dist/tbv/core/managers/pegin/index.d.ts.map +1 -1
  33. package/dist/tbv/core/primitives/psbt/constants.d.ts +0 -4
  34. package/dist/tbv/core/primitives/psbt/constants.d.ts.map +1 -1
  35. package/dist/tbv/core/primitives/utils/bitcoin.d.ts +0 -5
  36. package/dist/tbv/core/primitives/utils/bitcoin.d.ts.map +1 -1
  37. package/dist/tbv/index.cjs +1 -1
  38. package/dist/tbv/index.js +74 -73
  39. package/dist/tbv/integrations/aave/index.cjs +1 -1
  40. package/dist/tbv/integrations/aave/index.cjs.map +1 -1
  41. package/dist/tbv/integrations/aave/index.js +265 -270
  42. package/dist/tbv/integrations/aave/index.js.map +1 -1
  43. package/dist/tbv/integrations/aave/utils/cascadeSimulation.d.ts +9 -0
  44. package/dist/tbv/integrations/aave/utils/cascadeSimulation.d.ts.map +1 -1
  45. package/dist/tbv/integrations/aave/utils/optimalOrder.d.ts +20 -3
  46. package/dist/tbv/integrations/aave/utils/optimalOrder.d.ts.map +1 -1
  47. package/dist/types-0bvDGR4x.js.map +1 -1
  48. package/dist/types-Be3sAYzr.cjs.map +1 -1
  49. package/package.json +3 -3
  50. package/dist/mempoolApi-CAIge7Nj.js.map +0 -1
  51. package/dist/mempoolApi-YNkKjQCU.cjs +0 -2
  52. package/dist/mempoolApi-YNkKjQCU.cjs.map +0 -1
  53. package/dist/tbv/core/primitives/psbt/index.d.ts +0 -34
  54. package/dist/tbv/core/primitives/psbt/index.d.ts.map +0 -1
  55. package/dist/tbv/core/primitives/scripts/index.d.ts +0 -10
  56. package/dist/tbv/core/primitives/scripts/index.d.ts.map +0 -1
  57. package/dist/tbv/core/primitives/utils/index.d.ts +0 -9
  58. package/dist/tbv/core/primitives/utils/index.d.ts.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"bitcoin-B5aNKtsk.js","sources":["../src/tbv/core/primitives/utils/bitcoin.ts"],"sourcesContent":["/**\n * Bitcoin Utilities\n *\n * Common pure utility functions for Bitcoin operations including:\n * - Public key conversions (x-only format)\n * - Hex string manipulation\n * - Uint8Array conversions and validation\n * - Address derivation and validation\n *\n * All functions are pure (no side effects) and work in Node.js, browsers,\n * and serverless environments.\n *\n * @module primitives/utils/bitcoin\n */\n\nimport { Buffer } from \"buffer\";\nimport { networks, payments } from \"bitcoinjs-lib\";\n\nimport type { Network } from \"@babylonlabs-io/babylon-tbv-rust-wasm\";\nimport type { Hex } from \"viem\";\n\n/**\n * BIP-341 Tapscript leaf version for script-path spends.\n * @see https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki\n * @see Rust: bitcoin::taproot::LeafVersion::TapScript\n */\nexport const TAPSCRIPT_LEAF_VERSION = 0xc0;\n\n/**\n * Hex-string length of a 32-byte BIP-340 x-only public key (taproot,\n * Schnorr). Doubles the byte count: `2 * 32 = 64`.\n */\nexport const X_ONLY_PUBKEY_HEX_LEN = 64;\n\n/**\n * Hex-string length of a 33-byte SEC1-compressed secp256k1 public key\n * (`0x02` or `0x03` prefix + 32-byte x-coordinate). `2 * 33 = 66`.\n */\nexport const COMPRESSED_PUBKEY_HEX_LEN = 66;\n\n/**\n * Hex-string length of a 65-byte SEC1-uncompressed secp256k1 public\n * key (`0x04` prefix + 32-byte x + 32-byte y). `2 * 65 = 130`.\n */\nexport const UNCOMPRESSED_PUBKEY_HEX_LEN = 130;\n\n/**\n * Hex-string length of a 64-byte BIP-340 Schnorr signature. `2 * 64 = 128`.\n */\nexport const SCHNORR_SIG_HEX_LEN = 128;\n\n/**\n * Strip \"0x\" prefix from hex string if present.\n *\n * Bitcoin expects plain hex (no \"0x\" prefix), but frontend often uses\n * Ethereum-style \"0x\"-prefixed hex.\n *\n * @param hex - Hex string with or without \"0x\" prefix\n * @returns Hex string without \"0x\" prefix\n */\nexport function stripHexPrefix(hex: string): string {\n return hex.startsWith(\"0x\") || hex.startsWith(\"0X\") ? hex.slice(2) : hex;\n}\n\n/**\n * Ensure \"0x\" prefix on a hex string, returning viem's Hex type.\n *\n * Ethereum/viem APIs expect `0x`-prefixed hex, but Bitcoin tooling\n * typically omits the prefix. This normalises either form.\n *\n * @param hex - Hex string with or without \"0x\" prefix\n * @returns `0x`-prefixed hex string typed as viem Hex\n */\nexport function ensureHexPrefix(hex: string): Hex {\n if (hex.startsWith(\"0x\")) return hex as Hex;\n if (hex.startsWith(\"0X\")) return `0x${hex.slice(2)}` as Hex;\n return `0x${hex}` as Hex;\n}\n\n/**\n * Convert hex string to Uint8Array.\n *\n * @param hex - Hex string (with or without 0x prefix)\n * @returns Uint8Array\n * @throws If hex is invalid\n */\nexport function hexToUint8Array(hex: string): Uint8Array {\n const cleanHex = stripHexPrefix(hex);\n if (!isValidHexRaw(cleanHex)) {\n throw new Error(`Invalid hex string: ${hex}`);\n }\n const bytes = new Uint8Array(cleanHex.length / 2);\n for (let i = 0; i < cleanHex.length; i += 2) {\n bytes[i / 2] = parseInt(cleanHex.slice(i, i + 2), 16);\n }\n return bytes;\n}\n\n/**\n * Convert Uint8Array to hex string (without 0x prefix).\n *\n * @param bytes - Uint8Array to convert\n * @returns Hex string without 0x prefix\n */\nexport function uint8ArrayToHex(bytes: Uint8Array): string {\n return Array.from(bytes)\n .map((b) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n}\n\n/**\n * Read the prevout txid (big-endian hex) from a bitcoinjs-lib transaction input.\n *\n * bitcoinjs-lib stores `hash` in little-endian internal byte order; txids are\n * displayed in big-endian, so the bytes must be reversed before hex-encoding.\n *\n * @param input - Transaction input with a `hash` field (Buffer or Uint8Array)\n * @returns Prevout txid as a hex string (big-endian, no 0x prefix)\n */\nexport function inputTxidHex(input: {\n hash: Buffer | Uint8Array;\n}): string {\n return uint8ArrayToHex(new Uint8Array(input.hash).slice().reverse());\n}\n\n/**\n * Convert a 33-byte public key to 32-byte x-only format (removes first byte).\n *\n * Used for Taproot/Schnorr signatures which only need the x-coordinate.\n * If the input is already 32 bytes, returns it unchanged.\n *\n * @param pubKey - 33-byte or 32-byte public key\n * @returns 32-byte x-only public key\n */\nexport function toXOnly(pubKey: Uint8Array): Uint8Array {\n return pubKey.length === 32 ? pubKey : pubKey.slice(1, 33);\n}\n\n/**\n * Internal helper: Validate hex string format without stripping prefix\n *\n * @internal\n * @param hex - Hex string (must already have prefix stripped)\n * @returns true if valid hex string\n */\nfunction isValidHexRaw(hex: string): boolean {\n return /^[0-9a-fA-F]*$/.test(hex) && hex.length % 2 === 0;\n}\n\n/**\n * Process and convert a public key to x-only format (32 bytes hex).\n *\n * Handles:\n * - 0x prefix removal\n * - Hex character validation\n * - Length validation\n * - Conversion to x-only format\n *\n * Accepts:\n * - 64 hex chars (32 bytes) - already x-only\n * - 66 hex chars (33 bytes) - compressed pubkey\n * - 130 hex chars (65 bytes) - uncompressed pubkey\n *\n * @param publicKeyHex - Public key in hex format (with or without 0x prefix)\n * @returns X-only public key as 32 bytes hex string (without 0x prefix)\n * @throws If public key format is invalid or contains invalid hex characters\n */\nexport function processPublicKeyToXOnly(publicKeyHex: string): string {\n // Remove '0x' prefix if present\n const cleanHex = stripHexPrefix(publicKeyHex);\n\n // Validate hex characters early to prevent silent failures\n if (!isValidHexRaw(cleanHex)) {\n throw new Error(`Invalid hex characters in public key: ${publicKeyHex}`);\n }\n\n // If already 64 chars (32 bytes), it's already x-only format\n if (cleanHex.length === X_ONLY_PUBKEY_HEX_LEN) {\n return cleanHex;\n }\n\n // Validate public key length (compressed SEC1 or uncompressed SEC1)\n if (\n cleanHex.length !== COMPRESSED_PUBKEY_HEX_LEN &&\n cleanHex.length !== UNCOMPRESSED_PUBKEY_HEX_LEN\n ) {\n throw new Error(\n `Invalid public key length: ${cleanHex.length} (expected ${X_ONLY_PUBKEY_HEX_LEN}, ${COMPRESSED_PUBKEY_HEX_LEN}, or ${UNCOMPRESSED_PUBKEY_HEX_LEN} hex chars)`,\n );\n }\n\n const pubkeyBytes = hexToUint8Array(cleanHex);\n return uint8ArrayToHex(toXOnly(pubkeyBytes));\n}\n\n/**\n * Validate hex string format.\n *\n * Checks that the string contains only valid hexadecimal characters (0-9, a-f, A-F)\n * and has an even length (since each byte is represented by 2 hex characters).\n *\n * @param hex - String to validate (with or without 0x prefix)\n * @returns true if valid hex string\n */\nexport function isValidHex(hex: string): boolean {\n const cleanHex = stripHexPrefix(hex);\n return isValidHexRaw(cleanHex);\n}\n\n/**\n * Result of validating a wallet public key against an expected depositor public key.\n */\nexport interface WalletPubkeyValidationResult {\n /** Wallet's raw public key (as returned by wallet, may be compressed) */\n walletPubkeyRaw: string;\n /** Wallet's public key in x-only format (32 bytes, 64 hex chars) */\n walletPubkeyXOnly: string;\n /** The validated depositor public key (x-only format) */\n depositorPubkey: string;\n}\n\n/**\n * Validate that a wallet's public key matches the expected depositor public key.\n *\n * This function:\n * 1. Converts the wallet pubkey to x-only format\n * 2. Validates the wallet x-only pubkey matches the expected depositor pubkey\n * (case-insensitive)\n *\n * @param walletPubkeyRaw - Raw public key from wallet (may be compressed 66 chars or x-only 64 chars)\n * @param expectedDepositorPubkey - Expected depositor public key (x-only).\n * Required: omitting it would degrade this check to a self-comparison.\n * @returns Validation result with both pubkey formats\n * @throws If `expectedDepositorPubkey` is missing/empty\n * @throws If wallet pubkey doesn't match expected depositor pubkey\n */\nexport function validateWalletPubkey(\n walletPubkeyRaw: string,\n expectedDepositorPubkey: string,\n): WalletPubkeyValidationResult {\n if (!expectedDepositorPubkey) {\n throw new Error(\n \"validateWalletPubkey requires expectedDepositorPubkey. Pass the on-chain registered depositor pubkey to avoid a self-comparison.\",\n );\n }\n\n const walletPubkeyXOnly = processPublicKeyToXOnly(walletPubkeyRaw);\n const depositorPubkey = expectedDepositorPubkey;\n\n if (walletPubkeyXOnly.toLowerCase() !== depositorPubkey.toLowerCase()) {\n throw new Error(\n `Wallet public key does not match vault depositor. ` +\n `Expected: ${depositorPubkey}, Got: ${walletPubkeyXOnly}. ` +\n `Please connect the wallet that was used to create this vault.`\n );\n }\n\n return { walletPubkeyRaw, walletPubkeyXOnly, depositorPubkey };\n}\n\n// ============================================================================\n// BTC formatting\n// ============================================================================\n\nconst SATOSHIS_PER_BTC = 100_000_000n;\n\n/**\n * Format satoshis as a human-readable BTC string with trailing zeros removed.\n */\nexport function formatSatoshisToBtc(satoshis: bigint): string {\n if (satoshis < 0n) {\n return `-${formatSatoshisToBtc(-satoshis)}`;\n }\n const whole = satoshis / SATOSHIS_PER_BTC;\n const fraction = satoshis % SATOSHIS_PER_BTC;\n let fractionStr = fraction.toString().padStart(8, \"0\");\n fractionStr = fractionStr.replace(/0+$/, \"\");\n return fractionStr.length > 0 ? `${whole}.${fractionStr}` : whole.toString();\n}\n\n// ============================================================================\n// Address derivation and validation\n// ============================================================================\n\n/**\n * Assert that the ECC library has been initialized via `initEccLib(ecc)`.\n *\n * The consuming application must call `initEccLib(ecc)` from `bitcoinjs-lib`\n * once at startup before using any SDK function that involves Taproot / P2TR\n * operations. This guard provides a clear error message when that step was\n * missed, instead of letting bitcoinjs-lib throw its generic\n * \"No ECC Library provided\" error deep in a call stack.\n */\nfunction assertEccInitialized(): void {\n try {\n payments.p2tr({ internalPubkey: Buffer.alloc(32, 1) });\n } catch (e) {\n if (e instanceof Error && e.message.includes(\"No ECC Library provided\")) {\n throw new Error(\n \"ECC library not initialized. \" +\n 'You must call initEccLib(ecc) from \"bitcoinjs-lib\" before using the SDK. ' +\n \"See the ts-sdk README for setup instructions.\",\n );\n }\n // Any other error means ECC is loaded (e.g. invalid key is fine — ECC worked).\n }\n}\n\n/**\n * Map SDK network type to bitcoinjs-lib Network object.\n *\n * @param network - Network type (\"bitcoin\", \"testnet\", \"signet\", \"regtest\")\n * @returns bitcoinjs-lib Network object\n */\nexport function getNetwork(network: Network): networks.Network {\n switch (network) {\n case \"bitcoin\":\n return networks.bitcoin;\n case \"testnet\":\n case \"signet\":\n return networks.testnet;\n case \"regtest\":\n return networks.regtest;\n default:\n throw new Error(`Unknown network: ${network}`);\n }\n}\n\n/**\n * Derive a Taproot (P2TR) address from a public key.\n *\n * @param publicKeyHex - Compressed (66 hex) or x-only (64 hex) public key\n * @param network - Bitcoin network\n * @returns Taproot address (bc1p... / tb1p... / bcrt1p...)\n */\nexport function deriveTaprootAddress(\n publicKeyHex: string,\n network: Network,\n): string {\n assertEccInitialized();\n const xOnly = hexToUint8Array(processPublicKeyToXOnly(publicKeyHex));\n const { address } = payments.p2tr({\n internalPubkey: Buffer.from(xOnly),\n network: getNetwork(network),\n });\n if (!address) {\n throw new Error(\"Failed to derive taproot address from public key\");\n }\n return address;\n}\n\n/**\n * Strip `0x` prefixes and lex-sort an array of x-only public keys.\n *\n * Used to produce the canonical (Rust-parity) keeper / challenger ordering\n * the protocol expects in payout and refund signing contexts.\n *\n * @param pubkeys - Array of x-only public keys (with or without `0x` prefix)\n * @returns Lex-sorted array of pubkeys with `0x` prefix stripped\n */\nexport function getSortedXOnlyPubkeys(pubkeys: string[]): string[] {\n return pubkeys.map(stripHexPrefix).sort();\n}\n\n/**\n * Derive the BIP-86 P2TR scriptPubKey (`0x`-prefixed hex) from an x-only\n * public key.\n *\n * Matches Rust `Bip86KeyConnector::generate_taproot_script_pubkey`: a\n * keypath-only P2TR output with no script tree. Used to compute the expected\n * payout address for vault keeper claimers, whose payout goes to their own\n * BIP-86 address rather than the depositor's registered payout address.\n *\n * Network-agnostic: P2TR scriptPubKey bytes are `OP_1 <32-byte tweaked-key>`\n * regardless of network.\n *\n * @param xOnlyPubkeyHex - X-only public key (64 hex chars, with or without `0x` prefix)\n * @returns `0x`-prefixed P2TR scriptPubKey hex\n * @throws If `xOnlyPubkeyHex` is not exactly 64 hex chars after prefix stripping\n */\nexport function deriveBip86ScriptPubKeyHex(xOnlyPubkeyHex: string): string {\n assertEccInitialized();\n const cleanHex = stripHexPrefix(xOnlyPubkeyHex);\n if (!/^[0-9a-fA-F]{64}$/.test(cleanHex)) {\n throw new Error(\n \"Invalid x-only pubkey: must be 64 hex characters (32 bytes, no 0x prefix)\",\n );\n }\n const { output } = payments.p2tr({\n internalPubkey: Buffer.from(cleanHex, \"hex\"),\n });\n if (!output) {\n throw new Error(\"Failed to derive BIP-86 P2TR scriptPubKey\");\n }\n return `0x${output.toString(\"hex\")}`;\n}\n\n/**\n * Derive a Native SegWit (P2WPKH) address from a compressed public key.\n *\n * @param publicKeyHex - Compressed public key (66 hex chars, with or without 0x prefix)\n * @param network - Bitcoin network\n * @returns Native SegWit address (bc1q... / tb1q... / bcrt1q...)\n * @throws If publicKeyHex is not a compressed public key (66 hex chars)\n */\nexport function deriveNativeSegwitAddress(\n publicKeyHex: string,\n network: Network,\n): string {\n const cleanHex = stripHexPrefix(publicKeyHex);\n if (cleanHex.length !== 66) {\n throw new Error(\n `Native SegWit requires a compressed public key (66 hex chars), got ${cleanHex.length}`,\n );\n }\n const { address } = payments.p2wpkh({\n pubkey: Buffer.from(hexToUint8Array(cleanHex)),\n network: getNetwork(network),\n });\n if (!address) {\n throw new Error(\n \"Failed to derive native segwit address from public key\",\n );\n }\n return address;\n}\n\n/**\n * Validate that a BTC address was derived from the given public key.\n *\n * Derives Taproot (P2TR) and Native SegWit (P2WPKH) addresses from the\n * public key and checks if the provided address matches any of them.\n *\n * P2WPKH derivation requires the full compressed key with explicit y-parity.\n * When only an x-only key is supplied, the y-parity is unknown and trying\n * both `02|x` and `03|x` would let an opposite-parity P2WPKH address — a\n * script the caller does NOT control — pass validation. We fail closed for\n * P2WPKH in that case; P2TR (which depends only on the x-coordinate) is\n * still validated and remains the supported path for Taproot wallets.\n *\n * @param address - BTC address to validate\n * @param publicKeyHex - Public key from the wallet (x-only 64 or compressed 66 hex chars)\n * @param network - Bitcoin network\n * @returns true if the address matches the public key\n */\nexport function isAddressFromPublicKey(\n address: string,\n publicKeyHex: string,\n network: Network,\n): boolean {\n const cleanHex = stripHexPrefix(publicKeyHex);\n\n // P2TR — works with both x-only and compressed keys\n try {\n if (address === deriveTaprootAddress(cleanHex, network)) {\n return true;\n }\n } catch {\n // derivation failed, continue\n }\n\n // P2WPKH — only attempt when the caller supplied a parity-bearing\n // compressed key. An x-only input is fail-closed here on purpose.\n if (cleanHex.length === COMPRESSED_PUBKEY_HEX_LEN) {\n try {\n if (address === deriveNativeSegwitAddress(cleanHex, network)) {\n return true;\n }\n } catch {\n // derivation failed, continue\n }\n }\n\n return false;\n}\n"],"names":["TAPSCRIPT_LEAF_VERSION","X_ONLY_PUBKEY_HEX_LEN","COMPRESSED_PUBKEY_HEX_LEN","UNCOMPRESSED_PUBKEY_HEX_LEN","SCHNORR_SIG_HEX_LEN","stripHexPrefix","hex","ensureHexPrefix","hexToUint8Array","cleanHex","isValidHexRaw","bytes","i","uint8ArrayToHex","b","inputTxidHex","input","toXOnly","pubKey","processPublicKeyToXOnly","publicKeyHex","pubkeyBytes","isValidHex","validateWalletPubkey","walletPubkeyRaw","expectedDepositorPubkey","walletPubkeyXOnly","depositorPubkey","SATOSHIS_PER_BTC","formatSatoshisToBtc","satoshis","whole","fractionStr","assertEccInitialized","payments","Buffer","e","getNetwork","network","networks","deriveTaprootAddress","xOnly","address","getSortedXOnlyPubkeys","pubkeys","deriveBip86ScriptPubKeyHex","xOnlyPubkeyHex","output","deriveNativeSegwitAddress","isAddressFromPublicKey"],"mappings":";;AA0BO,MAAMA,IAAyB,KAMzBC,IAAwB,IAMxBC,IAA4B,IAM5BC,IAA8B,KAK9BC,IAAsB;AAW5B,SAASC,EAAeC,GAAqB;AAClD,SAAOA,EAAI,WAAW,IAAI,KAAKA,EAAI,WAAW,IAAI,IAAIA,EAAI,MAAM,CAAC,IAAIA;AACvE;AAWO,SAASC,EAAgBD,GAAkB;AAChD,SAAIA,EAAI,WAAW,IAAI,IA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R;AAAA,IAAA;AAGJ,SAAOA;AACT;AAoBO,SAASO,EACdP,GACAtB,GACAkB,GACS;AACT,QAAM7B,IAAWJ,EAAee,CAAY;AAG5C,MAAI;AACF,QAAIsB,MAAYF,EAAqB/B,GAAU6B,CAAO;AACpD,aAAO;AAAA,EAEX,QAAQ;AAAA,EAER;AAIA,MAAI7B,EAAS,WAAWP;AACtB,QAAI;AACF,UAAIwC,MAAYM,EAA0BvC,GAAU6B,CAAO;AACzD,eAAO;AAAA,IAEX,QAAQ;AAAA,IAER;AAGF,SAAO;AACT;"}
1
+ {"version":3,"file":"bitcoin-B5aNKtsk.js","sources":["../src/tbv/core/primitives/utils/bitcoin.ts"],"sourcesContent":["/**\n * Bitcoin Utilities\n *\n * Common pure utility functions for Bitcoin operations including:\n * - Public key conversions (x-only format)\n * - Hex string manipulation\n * - Uint8Array conversions and validation\n * - Address derivation and validation\n *\n * All functions are pure (no side effects) and work in Node.js, browsers,\n * and serverless environments.\n *\n * @module primitives/utils/bitcoin\n */\n\nimport { Buffer } from \"buffer\";\nimport { networks, payments } from \"bitcoinjs-lib\";\n\nimport type { Network } from \"@babylonlabs-io/babylon-tbv-rust-wasm\";\nimport type { Hex } from \"viem\";\n\n/**\n * BIP-341 Tapscript leaf version for script-path spends.\n * @see https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki\n * @see Rust: bitcoin::taproot::LeafVersion::TapScript\n */\nexport const TAPSCRIPT_LEAF_VERSION = 0xc0;\n\n/**\n * Hex-string length of a 32-byte BIP-340 x-only public key (taproot,\n * Schnorr). Doubles the byte count: `2 * 32 = 64`.\n */\nexport const X_ONLY_PUBKEY_HEX_LEN = 64;\n\n/**\n * Hex-string length of a 33-byte SEC1-compressed secp256k1 public key\n * (`0x02` or `0x03` prefix + 32-byte x-coordinate). `2 * 33 = 66`.\n */\nexport const COMPRESSED_PUBKEY_HEX_LEN = 66;\n\n/**\n * Hex-string length of a 65-byte SEC1-uncompressed secp256k1 public\n * key (`0x04` prefix + 32-byte x + 32-byte y). `2 * 65 = 130`.\n */\nconst UNCOMPRESSED_PUBKEY_HEX_LEN = 130;\n\n/**\n * Hex-string length of a 64-byte BIP-340 Schnorr signature. `2 * 64 = 128`.\n */\nexport const SCHNORR_SIG_HEX_LEN = 128;\n\n/**\n * Strip \"0x\" prefix from hex string if present.\n *\n * Bitcoin expects plain hex (no \"0x\" prefix), but frontend often uses\n * Ethereum-style \"0x\"-prefixed hex.\n *\n * @param hex - Hex string with or without \"0x\" prefix\n * @returns Hex string without \"0x\" prefix\n */\nexport function stripHexPrefix(hex: string): string {\n return hex.startsWith(\"0x\") || hex.startsWith(\"0X\") ? hex.slice(2) : hex;\n}\n\n/**\n * Ensure \"0x\" prefix on a hex string, returning viem's Hex type.\n *\n * Ethereum/viem APIs expect `0x`-prefixed hex, but Bitcoin tooling\n * typically omits the prefix. This normalises either form.\n *\n * @param hex - Hex string with or without \"0x\" prefix\n * @returns `0x`-prefixed hex string typed as viem Hex\n */\nexport function ensureHexPrefix(hex: string): Hex {\n if (hex.startsWith(\"0x\")) return hex as Hex;\n if (hex.startsWith(\"0X\")) return `0x${hex.slice(2)}` as Hex;\n return `0x${hex}` as Hex;\n}\n\n/**\n * Convert hex string to Uint8Array.\n *\n * @param hex - Hex string (with or without 0x prefix)\n * @returns Uint8Array\n * @throws If hex is invalid\n */\nexport function hexToUint8Array(hex: string): Uint8Array {\n const cleanHex = stripHexPrefix(hex);\n if (!isValidHexRaw(cleanHex)) {\n throw new Error(`Invalid hex string: ${hex}`);\n }\n const bytes = new Uint8Array(cleanHex.length / 2);\n for (let i = 0; i < cleanHex.length; i += 2) {\n bytes[i / 2] = parseInt(cleanHex.slice(i, i + 2), 16);\n }\n return bytes;\n}\n\n/**\n * Convert Uint8Array to hex string (without 0x prefix).\n *\n * @param bytes - Uint8Array to convert\n * @returns Hex string without 0x prefix\n */\nexport function uint8ArrayToHex(bytes: Uint8Array): string {\n return Array.from(bytes)\n .map((b) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n}\n\n/**\n * Read the prevout txid (big-endian hex) from a bitcoinjs-lib transaction input.\n *\n * bitcoinjs-lib stores `hash` in little-endian internal byte order; txids are\n * displayed in big-endian, so the bytes must be reversed before hex-encoding.\n *\n * @param input - Transaction input with a `hash` field (Buffer or Uint8Array)\n * @returns Prevout txid as a hex string (big-endian, no 0x prefix)\n */\nexport function inputTxidHex(input: {\n hash: Buffer | Uint8Array;\n}): string {\n return uint8ArrayToHex(new Uint8Array(input.hash).slice().reverse());\n}\n\n/**\n * Convert a 33-byte public key to 32-byte x-only format (removes first byte).\n *\n * Used for Taproot/Schnorr signatures which only need the x-coordinate.\n * If the input is already 32 bytes, returns it unchanged.\n *\n * @param pubKey - 33-byte or 32-byte public key\n * @returns 32-byte x-only public key\n */\nexport function toXOnly(pubKey: Uint8Array): Uint8Array {\n return pubKey.length === 32 ? pubKey : pubKey.slice(1, 33);\n}\n\n/**\n * Internal helper: Validate hex string format without stripping prefix\n *\n * @internal\n * @param hex - Hex string (must already have prefix stripped)\n * @returns true if valid hex string\n */\nfunction isValidHexRaw(hex: string): boolean {\n return /^[0-9a-fA-F]*$/.test(hex) && hex.length % 2 === 0;\n}\n\n/**\n * Process and convert a public key to x-only format (32 bytes hex).\n *\n * Handles:\n * - 0x prefix removal\n * - Hex character validation\n * - Length validation\n * - Conversion to x-only format\n *\n * Accepts:\n * - 64 hex chars (32 bytes) - already x-only\n * - 66 hex chars (33 bytes) - compressed pubkey\n * - 130 hex chars (65 bytes) - uncompressed pubkey\n *\n * @param publicKeyHex - Public key in hex format (with or without 0x prefix)\n * @returns X-only public key as 32 bytes hex string (without 0x prefix)\n * @throws If public key format is invalid or contains invalid hex characters\n */\nexport function processPublicKeyToXOnly(publicKeyHex: string): string {\n // Remove '0x' prefix if present\n const cleanHex = stripHexPrefix(publicKeyHex);\n\n // Validate hex characters early to prevent silent failures\n if (!isValidHexRaw(cleanHex)) {\n throw new Error(`Invalid hex characters in public key: ${publicKeyHex}`);\n }\n\n // If already 64 chars (32 bytes), it's already x-only format\n if (cleanHex.length === X_ONLY_PUBKEY_HEX_LEN) {\n return cleanHex;\n }\n\n // Validate public key length (compressed SEC1 or uncompressed SEC1)\n if (\n cleanHex.length !== COMPRESSED_PUBKEY_HEX_LEN &&\n cleanHex.length !== UNCOMPRESSED_PUBKEY_HEX_LEN\n ) {\n throw new Error(\n `Invalid public key length: ${cleanHex.length} (expected ${X_ONLY_PUBKEY_HEX_LEN}, ${COMPRESSED_PUBKEY_HEX_LEN}, or ${UNCOMPRESSED_PUBKEY_HEX_LEN} hex chars)`,\n );\n }\n\n const pubkeyBytes = hexToUint8Array(cleanHex);\n return uint8ArrayToHex(toXOnly(pubkeyBytes));\n}\n\n/**\n * Validate hex string format.\n *\n * Checks that the string contains only valid hexadecimal characters (0-9, a-f, A-F)\n * and has an even length (since each byte is represented by 2 hex characters).\n *\n * @param hex - String to validate (with or without 0x prefix)\n * @returns true if valid hex string\n */\nexport function isValidHex(hex: string): boolean {\n const cleanHex = stripHexPrefix(hex);\n return isValidHexRaw(cleanHex);\n}\n\n/**\n * Result of validating a wallet public key against an expected depositor public key.\n */\nexport interface WalletPubkeyValidationResult {\n /** Wallet's raw public key (as returned by wallet, may be compressed) */\n walletPubkeyRaw: string;\n /** Wallet's public key in x-only format (32 bytes, 64 hex chars) */\n walletPubkeyXOnly: string;\n /** The validated depositor public key (x-only format) */\n depositorPubkey: string;\n}\n\n/**\n * Validate that a wallet's public key matches the expected depositor public key.\n *\n * This function:\n * 1. Converts the wallet pubkey to x-only format\n * 2. Validates the wallet x-only pubkey matches the expected depositor pubkey\n * (case-insensitive)\n *\n * @param walletPubkeyRaw - Raw public key from wallet (may be compressed 66 chars or x-only 64 chars)\n * @param expectedDepositorPubkey - Expected depositor public key (x-only).\n * Required: omitting it would degrade this check to a self-comparison.\n * @returns Validation result with both pubkey formats\n * @throws If `expectedDepositorPubkey` is missing/empty\n * @throws If wallet pubkey doesn't match expected depositor pubkey\n */\nexport function validateWalletPubkey(\n walletPubkeyRaw: string,\n expectedDepositorPubkey: string,\n): WalletPubkeyValidationResult {\n if (!expectedDepositorPubkey) {\n throw new Error(\n \"validateWalletPubkey requires expectedDepositorPubkey. Pass the on-chain registered depositor pubkey to avoid a self-comparison.\",\n );\n }\n\n const walletPubkeyXOnly = processPublicKeyToXOnly(walletPubkeyRaw);\n const depositorPubkey = expectedDepositorPubkey;\n\n if (walletPubkeyXOnly.toLowerCase() !== depositorPubkey.toLowerCase()) {\n throw new Error(\n `Wallet public key does not match vault depositor. ` +\n `Expected: ${depositorPubkey}, Got: ${walletPubkeyXOnly}. ` +\n `Please connect the wallet that was used to create this vault.`\n );\n }\n\n return { walletPubkeyRaw, walletPubkeyXOnly, depositorPubkey };\n}\n\n// ============================================================================\n// BTC formatting\n// ============================================================================\n\nconst SATOSHIS_PER_BTC = 100_000_000n;\n\n/**\n * Format satoshis as a human-readable BTC string with trailing zeros removed.\n */\nexport function formatSatoshisToBtc(satoshis: bigint): string {\n if (satoshis < 0n) {\n return `-${formatSatoshisToBtc(-satoshis)}`;\n }\n const whole = satoshis / SATOSHIS_PER_BTC;\n const fraction = satoshis % SATOSHIS_PER_BTC;\n let fractionStr = fraction.toString().padStart(8, \"0\");\n fractionStr = fractionStr.replace(/0+$/, \"\");\n return fractionStr.length > 0 ? `${whole}.${fractionStr}` : whole.toString();\n}\n\n// ============================================================================\n// Address derivation and validation\n// ============================================================================\n\n/**\n * Assert that the ECC library has been initialized via `initEccLib(ecc)`.\n *\n * The consuming application must call `initEccLib(ecc)` from `bitcoinjs-lib`\n * once at startup before using any SDK function that involves Taproot / P2TR\n * operations. This guard provides a clear error message when that step was\n * missed, instead of letting bitcoinjs-lib throw its generic\n * \"No ECC Library provided\" error deep in a call stack.\n */\nfunction assertEccInitialized(): void {\n try {\n payments.p2tr({ internalPubkey: Buffer.alloc(32, 1) });\n } catch (e) {\n if (e instanceof Error && e.message.includes(\"No ECC Library provided\")) {\n throw new Error(\n \"ECC library not initialized. \" +\n 'You must call initEccLib(ecc) from \"bitcoinjs-lib\" before using the SDK. ' +\n \"See the ts-sdk README for setup instructions.\",\n );\n }\n // Any other error means ECC is loaded (e.g. invalid key is fine — ECC worked).\n }\n}\n\n/**\n * Map SDK network type to bitcoinjs-lib Network object.\n *\n * @param network - Network type (\"bitcoin\", \"testnet\", \"signet\", \"regtest\")\n * @returns bitcoinjs-lib Network object\n */\nexport function getNetwork(network: Network): networks.Network {\n switch (network) {\n case \"bitcoin\":\n return networks.bitcoin;\n case \"testnet\":\n case \"signet\":\n return networks.testnet;\n case \"regtest\":\n return networks.regtest;\n default:\n throw new Error(`Unknown network: ${network}`);\n }\n}\n\n/**\n * Derive a Taproot (P2TR) address from a public key.\n *\n * @param publicKeyHex - Compressed (66 hex) or x-only (64 hex) public key\n * @param network - Bitcoin network\n * @returns Taproot address (bc1p... / tb1p... / bcrt1p...)\n */\nexport function deriveTaprootAddress(\n publicKeyHex: string,\n network: Network,\n): string {\n assertEccInitialized();\n const xOnly = hexToUint8Array(processPublicKeyToXOnly(publicKeyHex));\n const { address } = payments.p2tr({\n internalPubkey: Buffer.from(xOnly),\n network: getNetwork(network),\n });\n if (!address) {\n throw new Error(\"Failed to derive taproot address from public key\");\n }\n return address;\n}\n\n/**\n * Strip `0x` prefixes and lex-sort an array of x-only public keys.\n *\n * Used to produce the canonical (Rust-parity) keeper / challenger ordering\n * the protocol expects in payout and refund signing contexts.\n *\n * @param pubkeys - Array of x-only public keys (with or without `0x` prefix)\n * @returns Lex-sorted array of pubkeys with `0x` prefix stripped\n */\nexport function getSortedXOnlyPubkeys(pubkeys: string[]): string[] {\n return pubkeys.map(stripHexPrefix).sort();\n}\n\n/**\n * Derive the BIP-86 P2TR scriptPubKey (`0x`-prefixed hex) from an x-only\n * public key.\n *\n * Matches Rust `Bip86KeyConnector::generate_taproot_script_pubkey`: a\n * keypath-only P2TR output with no script tree. Used to compute the expected\n * payout address for vault keeper claimers, whose payout goes to their own\n * BIP-86 address rather than the depositor's registered payout address.\n *\n * Network-agnostic: P2TR scriptPubKey bytes are `OP_1 <32-byte tweaked-key>`\n * regardless of network.\n *\n * @param xOnlyPubkeyHex - X-only public key (64 hex chars, with or without `0x` prefix)\n * @returns `0x`-prefixed P2TR scriptPubKey hex\n * @throws If `xOnlyPubkeyHex` is not exactly 64 hex chars after prefix stripping\n */\nexport function deriveBip86ScriptPubKeyHex(xOnlyPubkeyHex: string): string {\n assertEccInitialized();\n const cleanHex = stripHexPrefix(xOnlyPubkeyHex);\n if (!/^[0-9a-fA-F]{64}$/.test(cleanHex)) {\n throw new Error(\n \"Invalid x-only pubkey: must be 64 hex characters (32 bytes, no 0x prefix)\",\n );\n }\n const { output } = payments.p2tr({\n internalPubkey: Buffer.from(cleanHex, \"hex\"),\n });\n if (!output) {\n throw new Error(\"Failed to derive BIP-86 P2TR scriptPubKey\");\n }\n return `0x${output.toString(\"hex\")}`;\n}\n\n/**\n * Derive a Native SegWit (P2WPKH) address from a compressed public key.\n *\n * @param publicKeyHex - Compressed public key (66 hex chars, with or without 0x prefix)\n * @param network - Bitcoin network\n * @returns Native SegWit address (bc1q... / tb1q... / bcrt1q...)\n * @throws If publicKeyHex is not a compressed public key (66 hex chars)\n */\nexport function deriveNativeSegwitAddress(\n publicKeyHex: string,\n network: Network,\n): string {\n const cleanHex = stripHexPrefix(publicKeyHex);\n if (cleanHex.length !== 66) {\n throw new Error(\n `Native SegWit requires a compressed public key (66 hex chars), got ${cleanHex.length}`,\n );\n }\n const { address } = payments.p2wpkh({\n pubkey: Buffer.from(hexToUint8Array(cleanHex)),\n network: getNetwork(network),\n });\n if (!address) {\n throw new Error(\n \"Failed to derive native segwit address from public key\",\n );\n }\n return address;\n}\n\n/**\n * Validate that a BTC address was derived from the given public key.\n *\n * Derives Taproot (P2TR) and Native SegWit (P2WPKH) addresses from the\n * public key and checks if the provided address matches any of them.\n *\n * P2WPKH derivation requires the full compressed key with explicit y-parity.\n * When only an x-only key is supplied, the y-parity is unknown and trying\n * both `02|x` and `03|x` would let an opposite-parity P2WPKH address — a\n * script the caller does NOT control — pass validation. We fail closed for\n * P2WPKH in that case; P2TR (which depends only on the x-coordinate) is\n * still validated and remains the supported path for Taproot wallets.\n *\n * @param address - BTC address to validate\n * @param publicKeyHex - Public key from the wallet (x-only 64 or compressed 66 hex chars)\n * @param network - Bitcoin network\n * @returns true if the address matches the public key\n */\nexport function isAddressFromPublicKey(\n address: string,\n publicKeyHex: string,\n network: Network,\n): boolean {\n const cleanHex = stripHexPrefix(publicKeyHex);\n\n // P2TR — works with both x-only and compressed keys\n try {\n if (address === deriveTaprootAddress(cleanHex, network)) {\n return true;\n }\n } catch {\n // derivation failed, continue\n }\n\n // P2WPKH — only attempt when the caller supplied a parity-bearing\n // compressed key. An x-only input is fail-closed here on purpose.\n if (cleanHex.length === COMPRESSED_PUBKEY_HEX_LEN) {\n try {\n if (address === deriveNativeSegwitAddress(cleanHex, network)) {\n return true;\n }\n } catch {\n // derivation failed, continue\n }\n }\n\n return false;\n}\n"],"names":["TAPSCRIPT_LEAF_VERSION","X_ONLY_PUBKEY_HEX_LEN","COMPRESSED_PUBKEY_HEX_LEN","UNCOMPRESSED_PUBKEY_HEX_LEN","SCHNORR_SIG_HEX_LEN","stripHexPrefix","hex","ensureHexPrefix","hexToUint8Array","cleanHex","isValidHexRaw","bytes","i","uint8ArrayToHex","b","inputTxidHex","input","toXOnly","pubKey","processPublicKeyToXOnly","publicKeyHex","pubkeyBytes","isValidHex","validateWalletPubkey","walletPubkeyRaw","expectedDepositorPubkey","walletPubkeyXOnly","depositorPubkey","SATOSHIS_PER_BTC","formatSatoshisToBtc","satoshis","whole","fractionStr","assertEccInitialized","payments","Buffer","e","getNetwork","network","networks","deriveTaprootAddress","xOnly","address","getSortedXOnlyPubkeys","pubkeys","deriveBip86ScriptPubKeyHex","xOnlyPubkeyHex","output","deriveNativeSegwitAddress","isAddressFromPublicKey"],"mappings":";;AA0BO,MAAMA,IAAyB,KAMzBC,IAAwB,IA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EAAS,MAAM;AAAA,IAAA;AAGzF,QAAM,EAAE,SAAAiC,EAAA,IAAYR,EAAS,OAAO;AAAA,IAClC,QAAQC,EAAO,KAAK3B,EAAgBC,CAAQ,CAAC;AAAA,IAC7C,SAAS4B,EAAWC,CAAO;AAAA,EAAA,CAC5B;AACD,MAAI,CAACI;AACH,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAGJ,SAAOA;AACT;AAoBO,SAASO,EACdP,GACAtB,GACAkB,GACS;AACT,QAAM7B,IAAWJ,EAAee,CAAY;AAG5C,MAAI;AACF,QAAIsB,MAAYF,EAAqB/B,GAAU6B,CAAO;AACpD,aAAO;AAAA,EAEX,QAAQ;AAAA,EAER;AAIA,MAAI7B,EAAS,WAAWP;AACtB,QAAI;AACF,UAAIwC,MAAYM,EAA0BvC,GAAU6B,CAAO;AACzD,eAAO;AAAA,IAEX,QAAQ;AAAA,IAER;AAGF,SAAO;AACT;"}
@@ -1 +1 @@
1
- {"version":3,"file":"bitcoin-CHfKAhcI.cjs","sources":["../src/tbv/core/primitives/utils/bitcoin.ts"],"sourcesContent":["/**\n * Bitcoin Utilities\n *\n * Common pure utility functions for Bitcoin operations including:\n * - Public key conversions (x-only format)\n * - Hex string manipulation\n * - Uint8Array conversions and validation\n * - Address derivation and validation\n *\n * All functions are pure (no side effects) and work in Node.js, browsers,\n * and serverless environments.\n *\n * @module primitives/utils/bitcoin\n */\n\nimport { Buffer } from \"buffer\";\nimport { networks, payments } from \"bitcoinjs-lib\";\n\nimport type { Network } from \"@babylonlabs-io/babylon-tbv-rust-wasm\";\nimport type { Hex } from \"viem\";\n\n/**\n * BIP-341 Tapscript leaf version for script-path spends.\n * @see https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki\n * @see Rust: bitcoin::taproot::LeafVersion::TapScript\n */\nexport const TAPSCRIPT_LEAF_VERSION = 0xc0;\n\n/**\n * Hex-string length of a 32-byte BIP-340 x-only public key (taproot,\n * Schnorr). Doubles the byte count: `2 * 32 = 64`.\n */\nexport const X_ONLY_PUBKEY_HEX_LEN = 64;\n\n/**\n * Hex-string length of a 33-byte SEC1-compressed secp256k1 public key\n * (`0x02` or `0x03` prefix + 32-byte x-coordinate). `2 * 33 = 66`.\n */\nexport const COMPRESSED_PUBKEY_HEX_LEN = 66;\n\n/**\n * Hex-string length of a 65-byte SEC1-uncompressed secp256k1 public\n * key (`0x04` prefix + 32-byte x + 32-byte y). `2 * 65 = 130`.\n */\nexport const UNCOMPRESSED_PUBKEY_HEX_LEN = 130;\n\n/**\n * Hex-string length of a 64-byte BIP-340 Schnorr signature. `2 * 64 = 128`.\n */\nexport const SCHNORR_SIG_HEX_LEN = 128;\n\n/**\n * Strip \"0x\" prefix from hex string if present.\n *\n * Bitcoin expects plain hex (no \"0x\" prefix), but frontend often uses\n * Ethereum-style \"0x\"-prefixed hex.\n *\n * @param hex - Hex string with or without \"0x\" prefix\n * @returns Hex string without \"0x\" prefix\n */\nexport function stripHexPrefix(hex: string): string {\n return hex.startsWith(\"0x\") || hex.startsWith(\"0X\") ? hex.slice(2) : hex;\n}\n\n/**\n * Ensure \"0x\" prefix on a hex string, returning viem's Hex type.\n *\n * Ethereum/viem APIs expect `0x`-prefixed hex, but Bitcoin tooling\n * typically omits the prefix. This normalises either form.\n *\n * @param hex - Hex string with or without \"0x\" prefix\n * @returns `0x`-prefixed hex string typed as viem Hex\n */\nexport function ensureHexPrefix(hex: string): Hex {\n if (hex.startsWith(\"0x\")) return hex as Hex;\n if (hex.startsWith(\"0X\")) return `0x${hex.slice(2)}` as Hex;\n return `0x${hex}` as Hex;\n}\n\n/**\n * Convert hex string to Uint8Array.\n *\n * @param hex - Hex string (with or without 0x prefix)\n * @returns Uint8Array\n * @throws If hex is invalid\n */\nexport function hexToUint8Array(hex: string): Uint8Array {\n const cleanHex = stripHexPrefix(hex);\n if (!isValidHexRaw(cleanHex)) {\n throw new Error(`Invalid hex string: ${hex}`);\n }\n const bytes = new Uint8Array(cleanHex.length / 2);\n for (let i = 0; i < cleanHex.length; i += 2) {\n bytes[i / 2] = parseInt(cleanHex.slice(i, i + 2), 16);\n }\n return bytes;\n}\n\n/**\n * Convert Uint8Array to hex string (without 0x prefix).\n *\n * @param bytes - Uint8Array to convert\n * @returns Hex string without 0x prefix\n */\nexport function uint8ArrayToHex(bytes: Uint8Array): string {\n return Array.from(bytes)\n .map((b) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n}\n\n/**\n * Read the prevout txid (big-endian hex) from a bitcoinjs-lib transaction input.\n *\n * bitcoinjs-lib stores `hash` in little-endian internal byte order; txids are\n * displayed in big-endian, so the bytes must be reversed before hex-encoding.\n *\n * @param input - Transaction input with a `hash` field (Buffer or Uint8Array)\n * @returns Prevout txid as a hex string (big-endian, no 0x prefix)\n */\nexport function inputTxidHex(input: {\n hash: Buffer | Uint8Array;\n}): string {\n return uint8ArrayToHex(new Uint8Array(input.hash).slice().reverse());\n}\n\n/**\n * Convert a 33-byte public key to 32-byte x-only format (removes first byte).\n *\n * Used for Taproot/Schnorr signatures which only need the x-coordinate.\n * If the input is already 32 bytes, returns it unchanged.\n *\n * @param pubKey - 33-byte or 32-byte public key\n * @returns 32-byte x-only public key\n */\nexport function toXOnly(pubKey: Uint8Array): Uint8Array {\n return pubKey.length === 32 ? pubKey : pubKey.slice(1, 33);\n}\n\n/**\n * Internal helper: Validate hex string format without stripping prefix\n *\n * @internal\n * @param hex - Hex string (must already have prefix stripped)\n * @returns true if valid hex string\n */\nfunction isValidHexRaw(hex: string): boolean {\n return /^[0-9a-fA-F]*$/.test(hex) && hex.length % 2 === 0;\n}\n\n/**\n * Process and convert a public key to x-only format (32 bytes hex).\n *\n * Handles:\n * - 0x prefix removal\n * - Hex character validation\n * - Length validation\n * - Conversion to x-only format\n *\n * Accepts:\n * - 64 hex chars (32 bytes) - already x-only\n * - 66 hex chars (33 bytes) - compressed pubkey\n * - 130 hex chars (65 bytes) - uncompressed pubkey\n *\n * @param publicKeyHex - Public key in hex format (with or without 0x prefix)\n * @returns X-only public key as 32 bytes hex string (without 0x prefix)\n * @throws If public key format is invalid or contains invalid hex characters\n */\nexport function processPublicKeyToXOnly(publicKeyHex: string): string {\n // Remove '0x' prefix if present\n const cleanHex = stripHexPrefix(publicKeyHex);\n\n // Validate hex characters early to prevent silent failures\n if (!isValidHexRaw(cleanHex)) {\n throw new Error(`Invalid hex characters in public key: ${publicKeyHex}`);\n }\n\n // If already 64 chars (32 bytes), it's already x-only format\n if (cleanHex.length === X_ONLY_PUBKEY_HEX_LEN) {\n return cleanHex;\n }\n\n // Validate public key length (compressed SEC1 or uncompressed SEC1)\n if (\n cleanHex.length !== COMPRESSED_PUBKEY_HEX_LEN &&\n cleanHex.length !== UNCOMPRESSED_PUBKEY_HEX_LEN\n ) {\n throw new Error(\n `Invalid public key length: ${cleanHex.length} (expected ${X_ONLY_PUBKEY_HEX_LEN}, ${COMPRESSED_PUBKEY_HEX_LEN}, or ${UNCOMPRESSED_PUBKEY_HEX_LEN} hex chars)`,\n );\n }\n\n const pubkeyBytes = hexToUint8Array(cleanHex);\n return uint8ArrayToHex(toXOnly(pubkeyBytes));\n}\n\n/**\n * Validate hex string format.\n *\n * Checks that the string contains only valid hexadecimal characters (0-9, a-f, A-F)\n * and has an even length (since each byte is represented by 2 hex characters).\n *\n * @param hex - String to validate (with or without 0x prefix)\n * @returns true if valid hex string\n */\nexport function isValidHex(hex: string): boolean {\n const cleanHex = stripHexPrefix(hex);\n return isValidHexRaw(cleanHex);\n}\n\n/**\n * Result of validating a wallet public key against an expected depositor public key.\n */\nexport interface WalletPubkeyValidationResult {\n /** Wallet's raw public key (as returned by wallet, may be compressed) */\n walletPubkeyRaw: string;\n /** Wallet's public key in x-only format (32 bytes, 64 hex chars) */\n walletPubkeyXOnly: string;\n /** The validated depositor public key (x-only format) */\n depositorPubkey: string;\n}\n\n/**\n * Validate that a wallet's public key matches the expected depositor public key.\n *\n * This function:\n * 1. Converts the wallet pubkey to x-only format\n * 2. Validates the wallet x-only pubkey matches the expected depositor pubkey\n * (case-insensitive)\n *\n * @param walletPubkeyRaw - Raw public key from wallet (may be compressed 66 chars or x-only 64 chars)\n * @param expectedDepositorPubkey - Expected depositor public key (x-only).\n * Required: omitting it would degrade this check to a self-comparison.\n * @returns Validation result with both pubkey formats\n * @throws If `expectedDepositorPubkey` is missing/empty\n * @throws If wallet pubkey doesn't match expected depositor pubkey\n */\nexport function validateWalletPubkey(\n walletPubkeyRaw: string,\n expectedDepositorPubkey: string,\n): WalletPubkeyValidationResult {\n if (!expectedDepositorPubkey) {\n throw new Error(\n \"validateWalletPubkey requires expectedDepositorPubkey. Pass the on-chain registered depositor pubkey to avoid a self-comparison.\",\n );\n }\n\n const walletPubkeyXOnly = processPublicKeyToXOnly(walletPubkeyRaw);\n const depositorPubkey = expectedDepositorPubkey;\n\n if (walletPubkeyXOnly.toLowerCase() !== depositorPubkey.toLowerCase()) {\n throw new Error(\n `Wallet public key does not match vault depositor. ` +\n `Expected: ${depositorPubkey}, Got: ${walletPubkeyXOnly}. ` +\n `Please connect the wallet that was used to create this vault.`\n );\n }\n\n return { walletPubkeyRaw, walletPubkeyXOnly, depositorPubkey };\n}\n\n// ============================================================================\n// BTC formatting\n// ============================================================================\n\nconst SATOSHIS_PER_BTC = 100_000_000n;\n\n/**\n * Format satoshis as a human-readable BTC string with trailing zeros removed.\n */\nexport function formatSatoshisToBtc(satoshis: bigint): string {\n if (satoshis < 0n) {\n return `-${formatSatoshisToBtc(-satoshis)}`;\n }\n const whole = satoshis / SATOSHIS_PER_BTC;\n const fraction = satoshis % SATOSHIS_PER_BTC;\n let fractionStr = fraction.toString().padStart(8, \"0\");\n fractionStr = fractionStr.replace(/0+$/, \"\");\n return fractionStr.length > 0 ? `${whole}.${fractionStr}` : whole.toString();\n}\n\n// ============================================================================\n// Address derivation and validation\n// ============================================================================\n\n/**\n * Assert that the ECC library has been initialized via `initEccLib(ecc)`.\n *\n * The consuming application must call `initEccLib(ecc)` from `bitcoinjs-lib`\n * once at startup before using any SDK function that involves Taproot / P2TR\n * operations. This guard provides a clear error message when that step was\n * missed, instead of letting bitcoinjs-lib throw its generic\n * \"No ECC Library provided\" error deep in a call stack.\n */\nfunction assertEccInitialized(): void {\n try {\n payments.p2tr({ internalPubkey: Buffer.alloc(32, 1) });\n } catch (e) {\n if (e instanceof Error && e.message.includes(\"No ECC Library provided\")) {\n throw new Error(\n \"ECC library not initialized. \" +\n 'You must call initEccLib(ecc) from \"bitcoinjs-lib\" before using the SDK. ' +\n \"See the ts-sdk README for setup instructions.\",\n );\n }\n // Any other error means ECC is loaded (e.g. invalid key is fine — ECC worked).\n }\n}\n\n/**\n * Map SDK network type to bitcoinjs-lib Network object.\n *\n * @param network - Network type (\"bitcoin\", \"testnet\", \"signet\", \"regtest\")\n * @returns bitcoinjs-lib Network object\n */\nexport function getNetwork(network: Network): networks.Network {\n switch (network) {\n case \"bitcoin\":\n return networks.bitcoin;\n case \"testnet\":\n case \"signet\":\n return networks.testnet;\n case \"regtest\":\n return networks.regtest;\n default:\n throw new Error(`Unknown network: ${network}`);\n }\n}\n\n/**\n * Derive a Taproot (P2TR) address from a public key.\n *\n * @param publicKeyHex - Compressed (66 hex) or x-only (64 hex) public key\n * @param network - Bitcoin network\n * @returns Taproot address (bc1p... / tb1p... / bcrt1p...)\n */\nexport function deriveTaprootAddress(\n publicKeyHex: string,\n network: Network,\n): string {\n assertEccInitialized();\n const xOnly = hexToUint8Array(processPublicKeyToXOnly(publicKeyHex));\n const { address } = payments.p2tr({\n internalPubkey: Buffer.from(xOnly),\n network: getNetwork(network),\n });\n if (!address) {\n throw new Error(\"Failed to derive taproot address from public key\");\n }\n return address;\n}\n\n/**\n * Strip `0x` prefixes and lex-sort an array of x-only public keys.\n *\n * Used to produce the canonical (Rust-parity) keeper / challenger ordering\n * the protocol expects in payout and refund signing contexts.\n *\n * @param pubkeys - Array of x-only public keys (with or without `0x` prefix)\n * @returns Lex-sorted array of pubkeys with `0x` prefix stripped\n */\nexport function getSortedXOnlyPubkeys(pubkeys: string[]): string[] {\n return pubkeys.map(stripHexPrefix).sort();\n}\n\n/**\n * Derive the BIP-86 P2TR scriptPubKey (`0x`-prefixed hex) from an x-only\n * public key.\n *\n * Matches Rust `Bip86KeyConnector::generate_taproot_script_pubkey`: a\n * keypath-only P2TR output with no script tree. Used to compute the expected\n * payout address for vault keeper claimers, whose payout goes to their own\n * BIP-86 address rather than the depositor's registered payout address.\n *\n * Network-agnostic: P2TR scriptPubKey bytes are `OP_1 <32-byte tweaked-key>`\n * regardless of network.\n *\n * @param xOnlyPubkeyHex - X-only public key (64 hex chars, with or without `0x` prefix)\n * @returns `0x`-prefixed P2TR scriptPubKey hex\n * @throws If `xOnlyPubkeyHex` is not exactly 64 hex chars after prefix stripping\n */\nexport function deriveBip86ScriptPubKeyHex(xOnlyPubkeyHex: string): string {\n assertEccInitialized();\n const cleanHex = stripHexPrefix(xOnlyPubkeyHex);\n if (!/^[0-9a-fA-F]{64}$/.test(cleanHex)) {\n throw new Error(\n \"Invalid x-only pubkey: must be 64 hex characters (32 bytes, no 0x prefix)\",\n );\n }\n const { output } = payments.p2tr({\n internalPubkey: Buffer.from(cleanHex, \"hex\"),\n });\n if (!output) {\n throw new Error(\"Failed to derive BIP-86 P2TR scriptPubKey\");\n }\n return `0x${output.toString(\"hex\")}`;\n}\n\n/**\n * Derive a Native SegWit (P2WPKH) address from a compressed public key.\n *\n * @param publicKeyHex - Compressed public key (66 hex chars, with or without 0x prefix)\n * @param network - Bitcoin network\n * @returns Native SegWit address (bc1q... / tb1q... / bcrt1q...)\n * @throws If publicKeyHex is not a compressed public key (66 hex chars)\n */\nexport function deriveNativeSegwitAddress(\n publicKeyHex: string,\n network: Network,\n): string {\n const cleanHex = stripHexPrefix(publicKeyHex);\n if (cleanHex.length !== 66) {\n throw new Error(\n `Native SegWit requires a compressed public key (66 hex chars), got ${cleanHex.length}`,\n );\n }\n const { address } = payments.p2wpkh({\n pubkey: Buffer.from(hexToUint8Array(cleanHex)),\n network: getNetwork(network),\n });\n if (!address) {\n throw new Error(\n \"Failed to derive native segwit address from public key\",\n );\n }\n return address;\n}\n\n/**\n * Validate that a BTC address was derived from the given public key.\n *\n * Derives Taproot (P2TR) and Native SegWit (P2WPKH) addresses from the\n * public key and checks if the provided address matches any of them.\n *\n * P2WPKH derivation requires the full compressed key with explicit y-parity.\n * When only an x-only key is supplied, the y-parity is unknown and trying\n * both `02|x` and `03|x` would let an opposite-parity P2WPKH address — a\n * script the caller does NOT control — pass validation. We fail closed for\n * P2WPKH in that case; P2TR (which depends only on the x-coordinate) is\n * still validated and remains the supported path for Taproot wallets.\n *\n * @param address - BTC address to validate\n * @param publicKeyHex - Public key from the wallet (x-only 64 or compressed 66 hex chars)\n * @param network - Bitcoin network\n * @returns true if the address matches the public key\n */\nexport function isAddressFromPublicKey(\n address: string,\n publicKeyHex: string,\n network: Network,\n): boolean {\n const cleanHex = stripHexPrefix(publicKeyHex);\n\n // P2TR — works with both x-only and compressed keys\n try {\n if (address === deriveTaprootAddress(cleanHex, network)) {\n return true;\n }\n } catch {\n // derivation failed, continue\n }\n\n // P2WPKH — only attempt when the caller supplied a parity-bearing\n // compressed key. An x-only input is fail-closed here on purpose.\n if (cleanHex.length === COMPRESSED_PUBKEY_HEX_LEN) {\n try {\n if (address === deriveNativeSegwitAddress(cleanHex, network)) {\n return true;\n }\n } catch {\n // derivation failed, continue\n }\n }\n\n return false;\n}\n"],"names":["TAPSCRIPT_LEAF_VERSION","X_ONLY_PUBKEY_HEX_LEN","COMPRESSED_PUBKEY_HEX_LEN","UNCOMPRESSED_PUBKEY_HEX_LEN","SCHNORR_SIG_HEX_LEN","stripHexPrefix","hex","ensureHexPrefix","hexToUint8Array","cleanHex","isValidHexRaw","bytes","i","uint8ArrayToHex","b","inputTxidHex","input","toXOnly","pubKey","processPublicKeyToXOnly","publicKeyHex","pubkeyBytes","isValidHex","validateWalletPubkey","walletPubkeyRaw","expectedDepositorPubkey","walletPubkeyXOnly","depositorPubkey","SATOSHIS_PER_BTC","formatSatoshisToBtc","satoshis","whole","fractionStr","assertEccInitialized","payments","Buffer","getNetwork","network","networks","deriveTaprootAddress","xOnly","address","getSortedXOnlyPubkeys","pubkeys","deriveBip86ScriptPubKeyHex","xOnlyPubkeyHex","output","deriveNativeSegwitAddress","isAddressFromPublicKey"],"mappings":"kEA0BaA,EAAyB,IAMzBC,EAAwB,GAMxBC,EAA4B,GAM5BC,EAA8B,IAK9BC,EAAsB,IAW5B,SAASC,EAAeC,EAAqB,CAClD,OAAOA,EAAI,WAAW,IAAI,GAAKA,EAAI,WAAW,IAAI,EAAIA,EAAI,MAAM,CAAC,EAAIA,CACvE,CAWO,SAASC,EAAgBD,EAAkB,CAChD,OAAIA,EAAI,WAAW,IAAI,EAAUA,EAC7BA,EAAI,WAAW,IAAI,EAAU,KAAKA,EAAI,MAAM,CAAC,CAAC,GAC3C,KAAKA,CAAG,EACjB,CASO,SAASE,EAAgBF,EAAyB,CACvD,MAAMG,EAAWJ,EAAeC,CAAG,EACnC,GAAI,CAACI,EAAcD,CAAQ,EACzB,MAAM,IAAI,MAAM,uBAAuBH,CAAG,EAAE,EAE9C,MAAMK,EAAQ,IAAI,WAAWF,EAAS,OAAS,CAAC,EAChD,QAASG,EAAI,EAAGA,EAAIH,EAAS,OAAQG,GAAK,EACxCD,EAAMC,EAAI,CAAC,EAAI,SAASH,EAAS,MAAMG,EAAGA,EAAI,CAAC,EAAG,EAAE,EAEtD,OAAOD,CACT,CAQO,SAASE,EAAgBF,EAA2B,CACzD,OAAO,MAAM,KAAKA,CAAK,EACpB,IAAKG,GAAMA,EAAE,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,CAAC,EAC1C,KAAK,EAAE,CACZ,CAWO,SAASC,EAAaC,EAElB,CACT,OAAOH,EAAgB,IAAI,WAAWG,EAAM,IAAI,EAAE,MAAA,EAAQ,SAAS,CACrE,CAWO,SAASC,EAAQC,EAAgC,CACtD,OAAOA,EAAO,SAAW,GAAKA,EAASA,EAAO,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O,SAASC,EACd3B,EACAiB,EACQ,CACR,MAAM5B,EAAWJ,EAAee,CAAY,EAC5C,GAAIX,EAAS,SAAW,GACtB,MAAM,IAAI,MACR,sEAAsEA,EAAS,MAAM,EAAA,EAGzF,KAAM,CAAE,QAAAgC,CAAA,EAAYP,EAAAA,SAAS,OAAO,CAClC,OAAQC,EAAAA,OAAO,KAAK3B,EAAgBC,CAAQ,CAAC,EAC7C,QAAS2B,EAAWC,CAAO,CAAA,CAC5B,EACD,GAAI,CAACI,EACH,MAAM,IAAI,MACR,wDAAA,EAGJ,OAAOA,CACT,CAoBO,SAASO,EACdP,EACArB,EACAiB,EACS,CACT,MAAM5B,EAAWJ,EAAee,CAAY,EAG5C,GAAI,CACF,GAAIqB,IAAYF,EAAqB9B,EAAU4B,CAAO,EACpD,MAAO,EAEX,MAAQ,CAER,CAIA,GAAI5B,EAAS,SAAWP,EACtB,GAAI,CACF,GAAIuC,IAAYM,EAA0BtC,EAAU4B,CAAO,EACzD,MAAO,EAEX,MAAQ,CAER,CAGF,MAAO,EACT"}
1
+ {"version":3,"file":"bitcoin-CHfKAhcI.cjs","sources":["../src/tbv/core/primitives/utils/bitcoin.ts"],"sourcesContent":["/**\n * Bitcoin Utilities\n *\n * Common pure utility functions for Bitcoin operations including:\n * - Public key conversions (x-only format)\n * - Hex string manipulation\n * - Uint8Array conversions and validation\n * - Address derivation and validation\n *\n * All functions are pure (no side effects) and work in Node.js, browsers,\n * and serverless environments.\n *\n * @module primitives/utils/bitcoin\n */\n\nimport { Buffer } from \"buffer\";\nimport { networks, payments } from \"bitcoinjs-lib\";\n\nimport type { Network } from \"@babylonlabs-io/babylon-tbv-rust-wasm\";\nimport type { Hex } from \"viem\";\n\n/**\n * BIP-341 Tapscript leaf version for script-path spends.\n * @see https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki\n * @see Rust: bitcoin::taproot::LeafVersion::TapScript\n */\nexport const TAPSCRIPT_LEAF_VERSION = 0xc0;\n\n/**\n * Hex-string length of a 32-byte BIP-340 x-only public key (taproot,\n * Schnorr). Doubles the byte count: `2 * 32 = 64`.\n */\nexport const X_ONLY_PUBKEY_HEX_LEN = 64;\n\n/**\n * Hex-string length of a 33-byte SEC1-compressed secp256k1 public key\n * (`0x02` or `0x03` prefix + 32-byte x-coordinate). `2 * 33 = 66`.\n */\nexport const COMPRESSED_PUBKEY_HEX_LEN = 66;\n\n/**\n * Hex-string length of a 65-byte SEC1-uncompressed secp256k1 public\n * key (`0x04` prefix + 32-byte x + 32-byte y). `2 * 65 = 130`.\n */\nconst UNCOMPRESSED_PUBKEY_HEX_LEN = 130;\n\n/**\n * Hex-string length of a 64-byte BIP-340 Schnorr signature. `2 * 64 = 128`.\n */\nexport const SCHNORR_SIG_HEX_LEN = 128;\n\n/**\n * Strip \"0x\" prefix from hex string if present.\n *\n * Bitcoin expects plain hex (no \"0x\" prefix), but frontend often uses\n * Ethereum-style \"0x\"-prefixed hex.\n *\n * @param hex - Hex string with or without \"0x\" prefix\n * @returns Hex string without \"0x\" prefix\n */\nexport function stripHexPrefix(hex: string): string {\n return hex.startsWith(\"0x\") || hex.startsWith(\"0X\") ? hex.slice(2) : hex;\n}\n\n/**\n * Ensure \"0x\" prefix on a hex string, returning viem's Hex type.\n *\n * Ethereum/viem APIs expect `0x`-prefixed hex, but Bitcoin tooling\n * typically omits the prefix. This normalises either form.\n *\n * @param hex - Hex string with or without \"0x\" prefix\n * @returns `0x`-prefixed hex string typed as viem Hex\n */\nexport function ensureHexPrefix(hex: string): Hex {\n if (hex.startsWith(\"0x\")) return hex as Hex;\n if (hex.startsWith(\"0X\")) return `0x${hex.slice(2)}` as Hex;\n return `0x${hex}` as Hex;\n}\n\n/**\n * Convert hex string to Uint8Array.\n *\n * @param hex - Hex string (with or without 0x prefix)\n * @returns Uint8Array\n * @throws If hex is invalid\n */\nexport function hexToUint8Array(hex: string): Uint8Array {\n const cleanHex = stripHexPrefix(hex);\n if (!isValidHexRaw(cleanHex)) {\n throw new Error(`Invalid hex string: ${hex}`);\n }\n const bytes = new Uint8Array(cleanHex.length / 2);\n for (let i = 0; i < cleanHex.length; i += 2) {\n bytes[i / 2] = parseInt(cleanHex.slice(i, i + 2), 16);\n }\n return bytes;\n}\n\n/**\n * Convert Uint8Array to hex string (without 0x prefix).\n *\n * @param bytes - Uint8Array to convert\n * @returns Hex string without 0x prefix\n */\nexport function uint8ArrayToHex(bytes: Uint8Array): string {\n return Array.from(bytes)\n .map((b) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n}\n\n/**\n * Read the prevout txid (big-endian hex) from a bitcoinjs-lib transaction input.\n *\n * bitcoinjs-lib stores `hash` in little-endian internal byte order; txids are\n * displayed in big-endian, so the bytes must be reversed before hex-encoding.\n *\n * @param input - Transaction input with a `hash` field (Buffer or Uint8Array)\n * @returns Prevout txid as a hex string (big-endian, no 0x prefix)\n */\nexport function inputTxidHex(input: {\n hash: Buffer | Uint8Array;\n}): string {\n return uint8ArrayToHex(new Uint8Array(input.hash).slice().reverse());\n}\n\n/**\n * Convert a 33-byte public key to 32-byte x-only format (removes first byte).\n *\n * Used for Taproot/Schnorr signatures which only need the x-coordinate.\n * If the input is already 32 bytes, returns it unchanged.\n *\n * @param pubKey - 33-byte or 32-byte public key\n * @returns 32-byte x-only public key\n */\nexport function toXOnly(pubKey: Uint8Array): Uint8Array {\n return pubKey.length === 32 ? pubKey : pubKey.slice(1, 33);\n}\n\n/**\n * Internal helper: Validate hex string format without stripping prefix\n *\n * @internal\n * @param hex - Hex string (must already have prefix stripped)\n * @returns true if valid hex string\n */\nfunction isValidHexRaw(hex: string): boolean {\n return /^[0-9a-fA-F]*$/.test(hex) && hex.length % 2 === 0;\n}\n\n/**\n * Process and convert a public key to x-only format (32 bytes hex).\n *\n * Handles:\n * - 0x prefix removal\n * - Hex character validation\n * - Length validation\n * - Conversion to x-only format\n *\n * Accepts:\n * - 64 hex chars (32 bytes) - already x-only\n * - 66 hex chars (33 bytes) - compressed pubkey\n * - 130 hex chars (65 bytes) - uncompressed pubkey\n *\n * @param publicKeyHex - Public key in hex format (with or without 0x prefix)\n * @returns X-only public key as 32 bytes hex string (without 0x prefix)\n * @throws If public key format is invalid or contains invalid hex characters\n */\nexport function processPublicKeyToXOnly(publicKeyHex: string): string {\n // Remove '0x' prefix if present\n const cleanHex = stripHexPrefix(publicKeyHex);\n\n // Validate hex characters early to prevent silent failures\n if (!isValidHexRaw(cleanHex)) {\n throw new Error(`Invalid hex characters in public key: ${publicKeyHex}`);\n }\n\n // If already 64 chars (32 bytes), it's already x-only format\n if (cleanHex.length === X_ONLY_PUBKEY_HEX_LEN) {\n return cleanHex;\n }\n\n // Validate public key length (compressed SEC1 or uncompressed SEC1)\n if (\n cleanHex.length !== COMPRESSED_PUBKEY_HEX_LEN &&\n cleanHex.length !== UNCOMPRESSED_PUBKEY_HEX_LEN\n ) {\n throw new Error(\n `Invalid public key length: ${cleanHex.length} (expected ${X_ONLY_PUBKEY_HEX_LEN}, ${COMPRESSED_PUBKEY_HEX_LEN}, or ${UNCOMPRESSED_PUBKEY_HEX_LEN} hex chars)`,\n );\n }\n\n const pubkeyBytes = hexToUint8Array(cleanHex);\n return uint8ArrayToHex(toXOnly(pubkeyBytes));\n}\n\n/**\n * Validate hex string format.\n *\n * Checks that the string contains only valid hexadecimal characters (0-9, a-f, A-F)\n * and has an even length (since each byte is represented by 2 hex characters).\n *\n * @param hex - String to validate (with or without 0x prefix)\n * @returns true if valid hex string\n */\nexport function isValidHex(hex: string): boolean {\n const cleanHex = stripHexPrefix(hex);\n return isValidHexRaw(cleanHex);\n}\n\n/**\n * Result of validating a wallet public key against an expected depositor public key.\n */\nexport interface WalletPubkeyValidationResult {\n /** Wallet's raw public key (as returned by wallet, may be compressed) */\n walletPubkeyRaw: string;\n /** Wallet's public key in x-only format (32 bytes, 64 hex chars) */\n walletPubkeyXOnly: string;\n /** The validated depositor public key (x-only format) */\n depositorPubkey: string;\n}\n\n/**\n * Validate that a wallet's public key matches the expected depositor public key.\n *\n * This function:\n * 1. Converts the wallet pubkey to x-only format\n * 2. Validates the wallet x-only pubkey matches the expected depositor pubkey\n * (case-insensitive)\n *\n * @param walletPubkeyRaw - Raw public key from wallet (may be compressed 66 chars or x-only 64 chars)\n * @param expectedDepositorPubkey - Expected depositor public key (x-only).\n * Required: omitting it would degrade this check to a self-comparison.\n * @returns Validation result with both pubkey formats\n * @throws If `expectedDepositorPubkey` is missing/empty\n * @throws If wallet pubkey doesn't match expected depositor pubkey\n */\nexport function validateWalletPubkey(\n walletPubkeyRaw: string,\n expectedDepositorPubkey: string,\n): WalletPubkeyValidationResult {\n if (!expectedDepositorPubkey) {\n throw new Error(\n \"validateWalletPubkey requires expectedDepositorPubkey. Pass the on-chain registered depositor pubkey to avoid a self-comparison.\",\n );\n }\n\n const walletPubkeyXOnly = processPublicKeyToXOnly(walletPubkeyRaw);\n const depositorPubkey = expectedDepositorPubkey;\n\n if (walletPubkeyXOnly.toLowerCase() !== depositorPubkey.toLowerCase()) {\n throw new Error(\n `Wallet public key does not match vault depositor. ` +\n `Expected: ${depositorPubkey}, Got: ${walletPubkeyXOnly}. ` +\n `Please connect the wallet that was used to create this vault.`\n );\n }\n\n return { walletPubkeyRaw, walletPubkeyXOnly, depositorPubkey };\n}\n\n// ============================================================================\n// BTC formatting\n// ============================================================================\n\nconst SATOSHIS_PER_BTC = 100_000_000n;\n\n/**\n * Format satoshis as a human-readable BTC string with trailing zeros removed.\n */\nexport function formatSatoshisToBtc(satoshis: bigint): string {\n if (satoshis < 0n) {\n return `-${formatSatoshisToBtc(-satoshis)}`;\n }\n const whole = satoshis / SATOSHIS_PER_BTC;\n const fraction = satoshis % SATOSHIS_PER_BTC;\n let fractionStr = fraction.toString().padStart(8, \"0\");\n fractionStr = fractionStr.replace(/0+$/, \"\");\n return fractionStr.length > 0 ? `${whole}.${fractionStr}` : whole.toString();\n}\n\n// ============================================================================\n// Address derivation and validation\n// ============================================================================\n\n/**\n * Assert that the ECC library has been initialized via `initEccLib(ecc)`.\n *\n * The consuming application must call `initEccLib(ecc)` from `bitcoinjs-lib`\n * once at startup before using any SDK function that involves Taproot / P2TR\n * operations. This guard provides a clear error message when that step was\n * missed, instead of letting bitcoinjs-lib throw its generic\n * \"No ECC Library provided\" error deep in a call stack.\n */\nfunction assertEccInitialized(): void {\n try {\n payments.p2tr({ internalPubkey: Buffer.alloc(32, 1) });\n } catch (e) {\n if (e instanceof Error && e.message.includes(\"No ECC Library provided\")) {\n throw new Error(\n \"ECC library not initialized. \" +\n 'You must call initEccLib(ecc) from \"bitcoinjs-lib\" before using the SDK. ' +\n \"See the ts-sdk README for setup instructions.\",\n );\n }\n // Any other error means ECC is loaded (e.g. invalid key is fine — ECC worked).\n }\n}\n\n/**\n * Map SDK network type to bitcoinjs-lib Network object.\n *\n * @param network - Network type (\"bitcoin\", \"testnet\", \"signet\", \"regtest\")\n * @returns bitcoinjs-lib Network object\n */\nexport function getNetwork(network: Network): networks.Network {\n switch (network) {\n case \"bitcoin\":\n return networks.bitcoin;\n case \"testnet\":\n case \"signet\":\n return networks.testnet;\n case \"regtest\":\n return networks.regtest;\n default:\n throw new Error(`Unknown network: ${network}`);\n }\n}\n\n/**\n * Derive a Taproot (P2TR) address from a public key.\n *\n * @param publicKeyHex - Compressed (66 hex) or x-only (64 hex) public key\n * @param network - Bitcoin network\n * @returns Taproot address (bc1p... / tb1p... / bcrt1p...)\n */\nexport function deriveTaprootAddress(\n publicKeyHex: string,\n network: Network,\n): string {\n assertEccInitialized();\n const xOnly = hexToUint8Array(processPublicKeyToXOnly(publicKeyHex));\n const { address } = payments.p2tr({\n internalPubkey: Buffer.from(xOnly),\n network: getNetwork(network),\n });\n if (!address) {\n throw new Error(\"Failed to derive taproot address from public key\");\n }\n return address;\n}\n\n/**\n * Strip `0x` prefixes and lex-sort an array of x-only public keys.\n *\n * Used to produce the canonical (Rust-parity) keeper / challenger ordering\n * the protocol expects in payout and refund signing contexts.\n *\n * @param pubkeys - Array of x-only public keys (with or without `0x` prefix)\n * @returns Lex-sorted array of pubkeys with `0x` prefix stripped\n */\nexport function getSortedXOnlyPubkeys(pubkeys: string[]): string[] {\n return pubkeys.map(stripHexPrefix).sort();\n}\n\n/**\n * Derive the BIP-86 P2TR scriptPubKey (`0x`-prefixed hex) from an x-only\n * public key.\n *\n * Matches Rust `Bip86KeyConnector::generate_taproot_script_pubkey`: a\n * keypath-only P2TR output with no script tree. Used to compute the expected\n * payout address for vault keeper claimers, whose payout goes to their own\n * BIP-86 address rather than the depositor's registered payout address.\n *\n * Network-agnostic: P2TR scriptPubKey bytes are `OP_1 <32-byte tweaked-key>`\n * regardless of network.\n *\n * @param xOnlyPubkeyHex - X-only public key (64 hex chars, with or without `0x` prefix)\n * @returns `0x`-prefixed P2TR scriptPubKey hex\n * @throws If `xOnlyPubkeyHex` is not exactly 64 hex chars after prefix stripping\n */\nexport function deriveBip86ScriptPubKeyHex(xOnlyPubkeyHex: string): string {\n assertEccInitialized();\n const cleanHex = stripHexPrefix(xOnlyPubkeyHex);\n if (!/^[0-9a-fA-F]{64}$/.test(cleanHex)) {\n throw new Error(\n \"Invalid x-only pubkey: must be 64 hex characters (32 bytes, no 0x prefix)\",\n );\n }\n const { output } = payments.p2tr({\n internalPubkey: Buffer.from(cleanHex, \"hex\"),\n });\n if (!output) {\n throw new Error(\"Failed to derive BIP-86 P2TR scriptPubKey\");\n }\n return `0x${output.toString(\"hex\")}`;\n}\n\n/**\n * Derive a Native SegWit (P2WPKH) address from a compressed public key.\n *\n * @param publicKeyHex - Compressed public key (66 hex chars, with or without 0x prefix)\n * @param network - Bitcoin network\n * @returns Native SegWit address (bc1q... / tb1q... / bcrt1q...)\n * @throws If publicKeyHex is not a compressed public key (66 hex chars)\n */\nexport function deriveNativeSegwitAddress(\n publicKeyHex: string,\n network: Network,\n): string {\n const cleanHex = stripHexPrefix(publicKeyHex);\n if (cleanHex.length !== 66) {\n throw new Error(\n `Native SegWit requires a compressed public key (66 hex chars), got ${cleanHex.length}`,\n );\n }\n const { address } = payments.p2wpkh({\n pubkey: Buffer.from(hexToUint8Array(cleanHex)),\n network: getNetwork(network),\n });\n if (!address) {\n throw new Error(\n \"Failed to derive native segwit address from public key\",\n );\n }\n return address;\n}\n\n/**\n * Validate that a BTC address was derived from the given public key.\n *\n * Derives Taproot (P2TR) and Native SegWit (P2WPKH) addresses from the\n * public key and checks if the provided address matches any of them.\n *\n * P2WPKH derivation requires the full compressed key with explicit y-parity.\n * When only an x-only key is supplied, the y-parity is unknown and trying\n * both `02|x` and `03|x` would let an opposite-parity P2WPKH address — a\n * script the caller does NOT control — pass validation. We fail closed for\n * P2WPKH in that case; P2TR (which depends only on the x-coordinate) is\n * still validated and remains the supported path for Taproot wallets.\n *\n * @param address - BTC address to validate\n * @param publicKeyHex - Public key from the wallet (x-only 64 or compressed 66 hex chars)\n * @param network - Bitcoin network\n * @returns true if the address matches the public key\n */\nexport function isAddressFromPublicKey(\n address: string,\n publicKeyHex: string,\n network: Network,\n): boolean {\n const cleanHex = stripHexPrefix(publicKeyHex);\n\n // P2TR — works with both x-only and compressed keys\n try {\n if (address === deriveTaprootAddress(cleanHex, network)) {\n return true;\n }\n } catch {\n // derivation failed, continue\n }\n\n // P2WPKH — only attempt when the caller supplied a parity-bearing\n // compressed key. An x-only input is fail-closed here on purpose.\n if (cleanHex.length === COMPRESSED_PUBKEY_HEX_LEN) {\n try {\n if (address === deriveNativeSegwitAddress(cleanHex, network)) {\n return true;\n }\n } catch {\n // derivation failed, continue\n }\n }\n\n return false;\n}\n"],"names":["TAPSCRIPT_LEAF_VERSION","X_ONLY_PUBKEY_HEX_LEN","COMPRESSED_PUBKEY_HEX_LEN","UNCOMPRESSED_PUBKEY_HEX_LEN","SCHNORR_SIG_HEX_LEN","stripHexPrefix","hex","ensureHexPrefix","hexToUint8Array","cleanHex","isValidHexRaw","bytes","i","uint8ArrayToHex","b","inputTxidHex","input","toXOnly","pubKey","processPublicKeyToXOnly","publicKeyHex","pubkeyBytes","isValidHex","validateWalletPubkey","walletPubkeyRaw","expectedDepositorPubkey","walletPubkeyXOnly","depositorPubkey","SATOSHIS_PER_BTC","formatSatoshisToBtc","satoshis","whole","fractionStr","assertEccInitialized","payments","Buffer","getNetwork","network","networks","deriveTaprootAddress","xOnly","address","getSortedXOnlyPubkeys","pubkeys","deriveBip86ScriptPubKeyHex","xOnlyPubkeyHex","output","deriveNativeSegwitAddress","isAddressFromPublicKey"],"mappings":"kEA0BaA,EAAyB,IAMzBC,EAAwB,GAMxBC,EAA4B,GAMnCC,EAA8B,IAKvBC,EAAsB,IAW5B,SAASC,EAAeC,EAAqB,CAClD,OAAOA,EAAI,WAAW,IAAI,GAAKA,EAAI,WAAW,IAAI,EAAIA,EAAI,MAAM,CAAC,EAAIA,CACvE,CAWO,SAASC,EAAgBD,EAAkB,CAChD,OAAIA,EAAI,WAAW,IAAI,EAAUA,EAC7BA,EAAI,WAAW,IAAI,EAAU,KAAKA,EAAI,MAAM,CAAC,CAAC,GAC3C,KAAKA,CAAG,EACjB,CASO,SAASE,EAAgBF,EAAyB,CACvD,MAAMG,EAAWJ,EAAeC,CAAG,EACnC,GAAI,CAACI,EAAcD,CAAQ,EACzB,MAAM,IAAI,MAAM,uBAAuBH,CAAG,EAAE,EAE9C,MAAMK,EAAQ,IAAI,WAAWF,EAAS,OAAS,CAAC,EAChD,QAASG,EAAI,EAAGA,EAAIH,EAAS,OAAQG,GAAK,EACxCD,EAAMC,EAAI,CAAC,EAAI,SAASH,EAAS,MAAMG,EAAGA,EAAI,CAAC,EAAG,EAAE,EAEtD,OAAOD,CACT,CAQO,SAASE,EAAgBF,EAA2B,CACzD,OAAO,MAAM,KAAKA,CAAK,EACpB,IAAKG,GAAMA,EAAE,SAAS,EAAE,EAAE,SAAS,EAAG,GAAG,CAAC,EAC1C,KAAK,EAAE,CACZ,CAWO,SAASC,EAAaC,EAElB,CACT,OAAOH,EAAgB,IAAI,WAAWG,EAAM,IAAI,EAAE,MAAA,EAAQ,SAAS,CACrE,CAWO,SAASC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AA2C,EAE7D,MAAO,KAAKA,EAAO,SAAS,KAAK,CAAC,EACpC,CAUO,SAASC,EACd3B,EACAiB,EACQ,CACR,MAAM5B,EAAWJ,EAAee,CAAY,EAC5C,GAAIX,EAAS,SAAW,GACtB,MAAM,IAAI,MACR,sEAAsEA,EAAS,MAAM,EAAA,EAGzF,KAAM,CAAE,QAAAgC,CAAA,EAAYP,EAAAA,SAAS,OAAO,CAClC,OAAQC,EAAAA,OAAO,KAAK3B,EAAgBC,CAAQ,CAAC,EAC7C,QAAS2B,EAAWC,CAAO,CAAA,CAC5B,EACD,GAAI,CAACI,EACH,MAAM,IAAI,MACR,wDAAA,EAGJ,OAAOA,CACT,CAoBO,SAASO,EACdP,EACArB,EACAiB,EACS,CACT,MAAM5B,EAAWJ,EAAee,CAAY,EAG5C,GAAI,CACF,GAAIqB,IAAYF,EAAqB9B,EAAU4B,CAAO,EACpD,MAAO,EAEX,MAAQ,CAER,CAIA,GAAI5B,EAAS,SAAWP,EACtB,GAAI,CACF,GAAIuC,IAAYM,EAA0BtC,EAAU4B,CAAO,EACzD,MAAO,EAEX,MAAQ,CAER,CAGF,MAAO,EACT"}
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const E=require("./challengeAssert-rpDaS3fH.cjs"),c=require("@babylonlabs-io/babylon-tbv-rust-wasm"),u=require("./assertPsbtUnsignedTxMatches-r1svclbd.cjs"),A=require("./peginInput-DH6X4ITS.cjs"),_=require("./noPayout-B06Z9RTe.cjs"),r=require("./bitcoin-CHfKAhcI.cjs"),g=require("./signing-Bnsro0hE.cjs"),d=require("./validation-u8W7Lp2x.cjs"),n=require("./PeginManager-Dof0ZO1S.cjs"),S=require("./PayoutManager-B5bovfkD.cjs"),p=require("./ProtocolParams.abi-DQhcqsNr.cjs"),m=require("./BTCVaultRegistry.abi-CHFGevwa.cjs"),P=require("./errors-BP73_stm.cjs"),I=require("./shared/index.cjs"),i=require("./waitForTransactionReceiptSmartAware-tv1mtSIY.cjs"),a=require("./fundPeginTransaction-BBE3wTjR.cjs"),l=require("./reservation-hjXStM03.cjs"),s=require("./mempoolApi-YNkKjQCU.cjs"),t=require("./types-Be3sAYzr.cjs"),o=require("./types-eYlq0p1o.cjs"),R=require("./errors-Bu0H-dZD.cjs"),e=require("./buildAndBroadcastRefund-Cj8JDI7F.cjs"),T=require("./peginState-BijNNT15.cjs");exports.buildChallengeAssertPsbt=E.buildChallengeAssertPsbt;exports.buildDepositorPayoutPsbt=E.buildDepositorPayoutPsbt;exports.computeNumLocalChallengers=E.computeNumLocalChallengers;Object.defineProperty(exports,"computeMinClaimValue",{enumerable:!0,get:()=>c.computeMinClaimValue});Object.defineProperty(exports,"deriveVaultId",{enumerable:!0,get:()=>c.deriveVaultId});Object.defineProperty(exports,"expandAuthAnchor",{enumerable:!0,get:()=>c.expandAuthAnchor});Object.defineProperty(exports,"expandHashlockSecret",{enumerable:!0,get:()=>c.expandHashlockSecret});Object.defineProperty(exports,"expandWotsSeed",{enumerable:!0,get:()=>c.expandWotsSeed});exports.PsbtSubstitutionError=u.PsbtSubstitutionError;exports.assertPsbtUnsignedTxMatches=u.assertPsbtUnsignedTxMatches;exports.buildPayoutPsbt=u.buildPayoutPsbt;exports.buildPeginTxFromFundedPrePegin=u.buildPeginTxFromFundedPrePegin;exports.buildPrePeginPsbt=u.buildPrePeginPsbt;exports.createPayoutScript=u.createPayoutScript;exports.extractPayoutSignature=u.extractPayoutSignature;exports.buildPeginInputPsbt=A.buildPeginInputPsbt;exports.extractPeginInputSignature=A.extractPeginInputSignature;exports.finalizePeginInputPsbt=A.finalizePeginInputPsbt;exports.buildNoPayoutPsbt=_.buildNoPayoutPsbt;exports.buildRefundPsbt=_.buildRefundPsbt;exports.deriveBip86ScriptPubKeyHex=r.deriveBip86ScriptPubKeyHex;exports.deriveNativeSegwitAddress=r.deriveNativeSegwitAddress;exports.deriveTaprootAddress=r.deriveTaprootAddress;exports.ensureHexPrefix=r.ensureHexPrefix;exports.formatSatoshisToBtc=r.formatSatoshisToBtc;exports.getNetwork=r.getNetwork;exports.getSortedXOnlyPubkeys=r.getSortedXOnlyPubkeys;exports.hexToUint8Array=r.hexToUint8Array;exports.isAddressFromPublicKey=r.isAddressFromPublicKey;exports.isValidHex=r.isValidHex;exports.processPublicKeyToXOnly=r.processPublicKeyToXOnly;exports.stripHexPrefix=r.stripHexPrefix;exports.toXOnly=r.toXOnly;exports.uint8ArrayToHex=r.uint8ArrayToHex;exports.validateWalletPubkey=r.validateWalletPubkey;exports.createTaprootScriptPathSignOptions=g.createTaprootScriptPathSignOptions;exports.BITCOIN_ADDRESS_RE=d.BITCOIN_ADDRESS_RE;exports.HEX_RE=d.HEX_RE;exports.KNOWN_SCRIPT_PREFIXES=d.KNOWN_SCRIPT_PREFIXES;exports.MAX_REASONABLE_FEE_SATS=d.MAX_REASONABLE_FEE_SATS;exports.TXID_RE=d.TXID_RE;exports.PeginManager=n.PeginManager;exports.VAULT_APP_NAME=n.VAULT_APP_NAME;exports.buildFundingOutpointsCommitment=n.buildFundingOutpointsCommitment;exports.buildVaultContext=n.buildVaultContext;exports.computeWotsBlockPublicKeysHash=n.computeWotsBlockPublicKeysHash;exports.deriveVaultRoot=n.deriveVaultRoot;exports.deriveWotsBlocksFromSeed=n.deriveWotsBlocksFromSeed;exports.estimateSubmitPeginRequestBatchGas=n.estimateSubmitPeginRequestBatchGas;exports.PayoutManager=S.PayoutManager;exports.computeHashlock=S.computeHashlock;exports.validateSecretAgainstHashlock=S.validateSecretAgainstHashlock;exports.ApplicationRegistryABI=p.ApplicationRegistryABI;exports.ProtocolParamsABI=p.ProtocolParamsABI;exports.BTCVaultRegistryABI=m.BTCVaultRegistryABI;exports.CONTRACT_ERRORS=P.CONTRACT_ERRORS;exports.extractErrorData=P.extractErrorData;exports.getContractErrorMessage=P.getContractErrorMessage;exports.handleContractError=P.handleContractError;exports.isKnownContractError=P.isKnownContractError;exports.BitcoinNetworks=I.BitcoinNetworks;exports.BitcoinScriptType=i.BitcoinScriptType;exports.applyChangeOutputPolicy=i.applyChangeOutputPolicy;exports.calculateBtcTxHash=i.calculateBtcTxHash;exports.computeChangeOutputFeeSats=i.computeChangeOutputFeeSats;exports.computeMaxDeposit=i.computeMaxDeposit;exports.computePeginBaseFeeSats=i.computePeginBaseFeeSats;exports.getDustThreshold=i.getDustThreshold;exports.getPsbtInputFields=i.getPsbtInputFields;exports.getScriptType=i.getScriptType;exports.selectUtxosForPegin=i.selectUtxosForPegin;exports.shouldAddChangeOutput=i.shouldAddChangeOutput;exports.waitForTransactionReceiptSmartAware=i.waitForTransactionReceiptSmartAware;exports.BTC_DUST_SAT=a.BTC_DUST_SAT;exports.DUST_THRESHOLD=a.DUST_THRESHOLD;exports.FEE_SAFETY_MARGIN=a.FEE_SAFETY_MARGIN;exports.LOW_RATE_ESTIMATION_ACCURACY_BUFFER=a.LOW_RATE_ESTIMATION_ACCURACY_BUFFER;exports.MAX_NON_LEGACY_OUTPUT_SIZE=a.MAX_NON_LEGACY_OUTPUT_SIZE;exports.P2TR_INPUT_SIZE=a.P2TR_INPUT_SIZE;exports.PEGIN_AUTH_ANCHOR_OUTPUTS=a.PEGIN_AUTH_ANCHOR_OUTPUTS;exports.PEGIN_FIXED_OUTPUTS=a.PEGIN_FIXED_OUTPUTS;exports.SPLIT_TX_FEE_SAFETY_MULTIPLIER=a.SPLIT_TX_FEE_SAFETY_MULTIPLIER;exports.TX_BUFFER_SIZE_OVERHEAD=a.TX_BUFFER_SIZE_OVERHEAD;exports.WALLET_RELAY_FEE_RATE_THRESHOLD=a.WALLET_RELAY_FEE_RATE_THRESHOLD;exports.fundPeginTransaction=a.fundPeginTransaction;exports.parseUnfundedWasmTransaction=a.parseUnfundedWasmTransaction;exports.peginOutputCount=a.peginOutputCount;exports.rateBasedTxBufferFee=a.rateBasedTxBufferFee;exports.UtxoNotAvailableError=l.UtxoNotAvailableError;exports.assertUtxosAvailable=l.assertUtxosAvailable;exports.collectReservedUtxoRefs=l.collectReservedUtxoRefs;exports.extractInputsFromTransaction=l.extractInputsFromTransaction;exports.selectUtxosForDeposit=l.selectUtxosForDeposit;exports.validateUtxosAvailable=l.validateUtxosAvailable;exports.MEMPOOL_API_URLS=s.MEMPOOL_API_URLS;exports.getAddressTxs=s.getAddressTxs;exports.getAddressUtxos=s.getAddressUtxos;exports.getMempoolApiUrl=s.getMempoolApiUrl;exports.getNetworkFees=s.getNetworkFees;exports.getTxHex=s.getTxHex;exports.getTxInfo=s.getTxInfo;exports.getUtxoInfo=s.getUtxoInfo;exports.pushTx=s.pushTx;exports.OnChainBtcVaultStatus=t.OnChainBtcVaultStatus;exports.ServerIdentityError=t.ServerIdentityError;exports.VaultProviderRpcClient=t.VaultProviderRpcClient;exports.ViemProtocolParamsReader=t.ViemProtocolParamsReader;exports.ViemUniversalChallengerReader=t.ViemUniversalChallengerReader;exports.ViemVaultKeeperReader=t.ViemVaultKeeperReader;exports.ViemVaultRegistryReader=t.ViemVaultRegistryReader;exports.VpResponseValidationError=t.VpResponseValidationError;exports.VpTokenRegistry=t.VpTokenRegistry;exports.batchPollByProvider=t.batchPollByProvider;exports.createAuthenticatedVpClient=t.createAuthenticatedVpClient;exports.primeVpTokenRegistry=t.primeVpTokenRegistry;exports.resolveProtocolAddresses=t.resolveProtocolAddresses;exports.validateOffchainParams=t.validateOffchainParams;exports.validatePegInConfiguration=t.validatePegInConfiguration;exports.validateRequestDepositorClaimerArtifactsResponse=t.validateRequestDepositorClaimerArtifactsResponse;exports.validateTBVProtocolParams=t.validateTBVProtocolParams;exports.verifyServerIdentity=t.verifyServerIdentity;exports.vpTokenRegistry=t.vpTokenRegistry;exports.AUTH_EXPIRED_DATA_KIND=o.AUTH_EXPIRED_DATA_KIND;exports.DaemonStatus=o.DaemonStatus;exports.JSON_RPC_ERROR_CODES=o.JSON_RPC_ERROR_CODES;exports.JsonRpcClient=o.JsonRpcClient;exports.JsonRpcError=o.JsonRpcError;exports.POST_WOTS_STATUSES=o.POST_WOTS_STATUSES;exports.PRE_DEPOSITOR_SIGNATURES_STATES=o.PRE_DEPOSITOR_SIGNATURES_STATES;exports.RpcErrorCode=o.RpcErrorCode;exports.VP_BATCH_MAX_SIZE=o.VP_BATCH_MAX_SIZE;exports.VP_TERMINAL_FAILURE_STATUSES=o.VP_TERMINAL_FAILURE_STATUSES;exports.VP_TRANSIENT_STATUSES=o.VP_TRANSIENT_STATUSES;exports.isWotsMismatchError=R.isWotsMismatchError;exports.parseFundingOutpointsFromTx=R.parseFundingOutpointsFromTx;exports.BIP68NotMatureError=e.BIP68NotMatureError;exports.ClaimerPegoutStatusValue=e.ClaimerPegoutStatusValue;exports.REFUND_MAX_FEE_FRACTION_DENOMINATOR=e.REFUND_MAX_FEE_FRACTION_DENOMINATOR;exports.REFUND_MAX_FEE_FRACTION_NUMERATOR=e.REFUND_MAX_FEE_FRACTION_NUMERATOR;exports.REFUND_MAX_FEE_RATE_SATS_VB=e.REFUND_MAX_FEE_RATE_SATS_VB;exports.REFUND_VSIZE=e.REFUND_VSIZE;exports.RegisteredVaultVersionMismatchError=e.RegisteredVaultVersionMismatchError;exports.activateVault=e.activateVault;exports.buildAndBroadcastRefund=e.buildAndBroadcastRefund;exports.estimateRefundFeeSats=e.estimateRefundFeeSats;exports.isDepositAmountValid=e.isDepositAmountValid;exports.isPegoutTerminalStatus=e.isPegoutTerminalStatus;exports.isRecognizedPegoutStatus=e.isRecognizedPegoutStatus;exports.isRegisteredVaultVersionMismatchError=e.isRegisteredVaultVersionMismatchError;exports.runDepositorPresignFlow=e.runDepositorPresignFlow;exports.signDepositorGraph=e.signDepositorGraph;exports.submitWotsPublicKey=e.submitWotsPublicKey;exports.validateDepositAmount=e.validateDepositAmount;exports.validateMultiVaultDepositInputs=e.validateMultiVaultDepositInputs;exports.validateOnChainParticipantKeys=e.validateOnChainParticipantKeys;exports.validateProviderSelection=e.validateProviderSelection;exports.validateRemainingCapacity=e.validateRemainingCapacity;exports.validateVaultAmounts=e.validateVaultAmounts;exports.validateVaultProviderPubkey=e.validateVaultProviderPubkey;exports.verifyRegisteredVaultVersions=e.verifyRegisteredVaultVersions;exports.waitForPeginStatus=e.waitForPeginStatus;exports.ContractStatus=T.ContractStatus;exports.PeginAction=T.PeginAction;exports.canPerformAction=T.canPerformAction;exports.getPeginProtocolState=T.getPeginProtocolState;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const E=require("./challengeAssert-rpDaS3fH.cjs"),c=require("@babylonlabs-io/babylon-tbv-rust-wasm"),u=require("./assertPsbtUnsignedTxMatches-r1svclbd.cjs"),A=require("./peginInput-DH6X4ITS.cjs"),_=require("./noPayout-B06Z9RTe.cjs"),r=require("./bitcoin-CHfKAhcI.cjs"),g=require("./signing-Bnsro0hE.cjs"),d=require("./validation-u8W7Lp2x.cjs"),n=require("./PeginManager-UqbOj2oV.cjs"),S=require("./PayoutManager-B5bovfkD.cjs"),p=require("./ProtocolParams.abi-DQhcqsNr.cjs"),m=require("./BTCVaultRegistry.abi-CHFGevwa.cjs"),P=require("./errors-BP73_stm.cjs"),I=require("./shared/index.cjs"),a=require("./waitForTransactionReceiptSmartAware-tv1mtSIY.cjs"),i=require("./fundPeginTransaction-BBE3wTjR.cjs"),l=require("./reservation-hjXStM03.cjs"),s=require("./mempoolApi-DEAS9wVa.cjs"),t=require("./types-Be3sAYzr.cjs"),o=require("./types-eYlq0p1o.cjs"),R=require("./errors-Bu0H-dZD.cjs"),e=require("./buildAndBroadcastRefund-Cj8JDI7F.cjs"),T=require("./peginState-BijNNT15.cjs");exports.buildChallengeAssertPsbt=E.buildChallengeAssertPsbt;exports.buildDepositorPayoutPsbt=E.buildDepositorPayoutPsbt;exports.computeNumLocalChallengers=E.computeNumLocalChallengers;Object.defineProperty(exports,"computeMinClaimValue",{enumerable:!0,get:()=>c.computeMinClaimValue});Object.defineProperty(exports,"deriveVaultId",{enumerable:!0,get:()=>c.deriveVaultId});Object.defineProperty(exports,"expandAuthAnchor",{enumerable:!0,get:()=>c.expandAuthAnchor});Object.defineProperty(exports,"expandHashlockSecret",{enumerable:!0,get:()=>c.expandHashlockSecret});Object.defineProperty(exports,"expandWotsSeed",{enumerable:!0,get:()=>c.expandWotsSeed});exports.PsbtSubstitutionError=u.PsbtSubstitutionError;exports.assertPsbtUnsignedTxMatches=u.assertPsbtUnsignedTxMatches;exports.buildPayoutPsbt=u.buildPayoutPsbt;exports.buildPeginTxFromFundedPrePegin=u.buildPeginTxFromFundedPrePegin;exports.buildPrePeginPsbt=u.buildPrePeginPsbt;exports.createPayoutScript=u.createPayoutScript;exports.extractPayoutSignature=u.extractPayoutSignature;exports.buildPeginInputPsbt=A.buildPeginInputPsbt;exports.extractPeginInputSignature=A.extractPeginInputSignature;exports.finalizePeginInputPsbt=A.finalizePeginInputPsbt;exports.buildNoPayoutPsbt=_.buildNoPayoutPsbt;exports.buildRefundPsbt=_.buildRefundPsbt;exports.deriveBip86ScriptPubKeyHex=r.deriveBip86ScriptPubKeyHex;exports.deriveNativeSegwitAddress=r.deriveNativeSegwitAddress;exports.deriveTaprootAddress=r.deriveTaprootAddress;exports.ensureHexPrefix=r.ensureHexPrefix;exports.formatSatoshisToBtc=r.formatSatoshisToBtc;exports.getNetwork=r.getNetwork;exports.getSortedXOnlyPubkeys=r.getSortedXOnlyPubkeys;exports.hexToUint8Array=r.hexToUint8Array;exports.isAddressFromPublicKey=r.isAddressFromPublicKey;exports.isValidHex=r.isValidHex;exports.processPublicKeyToXOnly=r.processPublicKeyToXOnly;exports.stripHexPrefix=r.stripHexPrefix;exports.toXOnly=r.toXOnly;exports.uint8ArrayToHex=r.uint8ArrayToHex;exports.validateWalletPubkey=r.validateWalletPubkey;exports.createTaprootScriptPathSignOptions=g.createTaprootScriptPathSignOptions;exports.BITCOIN_ADDRESS_RE=d.BITCOIN_ADDRESS_RE;exports.HEX_RE=d.HEX_RE;exports.KNOWN_SCRIPT_PREFIXES=d.KNOWN_SCRIPT_PREFIXES;exports.MAX_REASONABLE_FEE_SATS=d.MAX_REASONABLE_FEE_SATS;exports.TXID_RE=d.TXID_RE;exports.PeginManager=n.PeginManager;exports.VAULT_APP_NAME=n.VAULT_APP_NAME;exports.buildFundingOutpointsCommitment=n.buildFundingOutpointsCommitment;exports.buildVaultContext=n.buildVaultContext;exports.computeWotsBlockPublicKeysHash=n.computeWotsBlockPublicKeysHash;exports.deriveVaultRoot=n.deriveVaultRoot;exports.deriveWotsBlocksFromSeed=n.deriveWotsBlocksFromSeed;exports.estimateSubmitPeginRequestBatchGas=n.estimateSubmitPeginRequestBatchGas;exports.PayoutManager=S.PayoutManager;exports.computeHashlock=S.computeHashlock;exports.validateSecretAgainstHashlock=S.validateSecretAgainstHashlock;exports.ApplicationRegistryABI=p.ApplicationRegistryABI;exports.ProtocolParamsABI=p.ProtocolParamsABI;exports.BTCVaultRegistryABI=m.BTCVaultRegistryABI;exports.CONTRACT_ERRORS=P.CONTRACT_ERRORS;exports.extractErrorData=P.extractErrorData;exports.getContractErrorMessage=P.getContractErrorMessage;exports.handleContractError=P.handleContractError;exports.isKnownContractError=P.isKnownContractError;exports.BitcoinNetworks=I.BitcoinNetworks;exports.BitcoinScriptType=a.BitcoinScriptType;exports.applyChangeOutputPolicy=a.applyChangeOutputPolicy;exports.calculateBtcTxHash=a.calculateBtcTxHash;exports.computeChangeOutputFeeSats=a.computeChangeOutputFeeSats;exports.computeMaxDeposit=a.computeMaxDeposit;exports.computePeginBaseFeeSats=a.computePeginBaseFeeSats;exports.getDustThreshold=a.getDustThreshold;exports.getPsbtInputFields=a.getPsbtInputFields;exports.getScriptType=a.getScriptType;exports.selectUtxosForPegin=a.selectUtxosForPegin;exports.shouldAddChangeOutput=a.shouldAddChangeOutput;exports.waitForTransactionReceiptSmartAware=a.waitForTransactionReceiptSmartAware;exports.BTC_DUST_SAT=i.BTC_DUST_SAT;exports.DUST_THRESHOLD=i.DUST_THRESHOLD;exports.FEE_SAFETY_MARGIN=i.FEE_SAFETY_MARGIN;exports.LOW_RATE_ESTIMATION_ACCURACY_BUFFER=i.LOW_RATE_ESTIMATION_ACCURACY_BUFFER;exports.MAX_NON_LEGACY_OUTPUT_SIZE=i.MAX_NON_LEGACY_OUTPUT_SIZE;exports.P2TR_INPUT_SIZE=i.P2TR_INPUT_SIZE;exports.PEGIN_AUTH_ANCHOR_OUTPUTS=i.PEGIN_AUTH_ANCHOR_OUTPUTS;exports.PEGIN_FIXED_OUTPUTS=i.PEGIN_FIXED_OUTPUTS;exports.SPLIT_TX_FEE_SAFETY_MULTIPLIER=i.SPLIT_TX_FEE_SAFETY_MULTIPLIER;exports.TX_BUFFER_SIZE_OVERHEAD=i.TX_BUFFER_SIZE_OVERHEAD;exports.WALLET_RELAY_FEE_RATE_THRESHOLD=i.WALLET_RELAY_FEE_RATE_THRESHOLD;exports.fundPeginTransaction=i.fundPeginTransaction;exports.parseUnfundedWasmTransaction=i.parseUnfundedWasmTransaction;exports.peginOutputCount=i.peginOutputCount;exports.rateBasedTxBufferFee=i.rateBasedTxBufferFee;exports.UtxoNotAvailableError=l.UtxoNotAvailableError;exports.assertUtxosAvailable=l.assertUtxosAvailable;exports.collectReservedUtxoRefs=l.collectReservedUtxoRefs;exports.extractInputsFromTransaction=l.extractInputsFromTransaction;exports.selectUtxosForDeposit=l.selectUtxosForDeposit;exports.validateUtxosAvailable=l.validateUtxosAvailable;exports.MEMPOOL_API_URLS=s.MEMPOOL_API_URLS;exports.getAddressTxs=s.getAddressTxs;exports.getAddressUtxos=s.getAddressUtxos;exports.getMempoolApiUrl=s.getMempoolApiUrl;exports.getNetworkFees=s.getNetworkFees;exports.getTipHeight=s.getTipHeight;exports.getTxHex=s.getTxHex;exports.getTxInfo=s.getTxInfo;exports.getUtxoInfo=s.getUtxoInfo;exports.pushTx=s.pushTx;exports.OnChainBtcVaultStatus=t.OnChainBtcVaultStatus;exports.ServerIdentityError=t.ServerIdentityError;exports.VaultProviderRpcClient=t.VaultProviderRpcClient;exports.ViemProtocolParamsReader=t.ViemProtocolParamsReader;exports.ViemUniversalChallengerReader=t.ViemUniversalChallengerReader;exports.ViemVaultKeeperReader=t.ViemVaultKeeperReader;exports.ViemVaultRegistryReader=t.ViemVaultRegistryReader;exports.VpResponseValidationError=t.VpResponseValidationError;exports.VpTokenRegistry=t.VpTokenRegistry;exports.batchPollByProvider=t.batchPollByProvider;exports.createAuthenticatedVpClient=t.createAuthenticatedVpClient;exports.primeVpTokenRegistry=t.primeVpTokenRegistry;exports.resolveProtocolAddresses=t.resolveProtocolAddresses;exports.validateOffchainParams=t.validateOffchainParams;exports.validatePegInConfiguration=t.validatePegInConfiguration;exports.validateRequestDepositorClaimerArtifactsResponse=t.validateRequestDepositorClaimerArtifactsResponse;exports.validateTBVProtocolParams=t.validateTBVProtocolParams;exports.verifyServerIdentity=t.verifyServerIdentity;exports.vpTokenRegistry=t.vpTokenRegistry;exports.AUTH_EXPIRED_DATA_KIND=o.AUTH_EXPIRED_DATA_KIND;exports.DaemonStatus=o.DaemonStatus;exports.JSON_RPC_ERROR_CODES=o.JSON_RPC_ERROR_CODES;exports.JsonRpcClient=o.JsonRpcClient;exports.JsonRpcError=o.JsonRpcError;exports.POST_WOTS_STATUSES=o.POST_WOTS_STATUSES;exports.PRE_DEPOSITOR_SIGNATURES_STATES=o.PRE_DEPOSITOR_SIGNATURES_STATES;exports.RpcErrorCode=o.RpcErrorCode;exports.VP_BATCH_MAX_SIZE=o.VP_BATCH_MAX_SIZE;exports.VP_TERMINAL_FAILURE_STATUSES=o.VP_TERMINAL_FAILURE_STATUSES;exports.VP_TRANSIENT_STATUSES=o.VP_TRANSIENT_STATUSES;exports.isWotsMismatchError=R.isWotsMismatchError;exports.parseFundingOutpointsFromTx=R.parseFundingOutpointsFromTx;exports.BIP68NotMatureError=e.BIP68NotMatureError;exports.ClaimerPegoutStatusValue=e.ClaimerPegoutStatusValue;exports.REFUND_MAX_FEE_FRACTION_DENOMINATOR=e.REFUND_MAX_FEE_FRACTION_DENOMINATOR;exports.REFUND_MAX_FEE_FRACTION_NUMERATOR=e.REFUND_MAX_FEE_FRACTION_NUMERATOR;exports.REFUND_MAX_FEE_RATE_SATS_VB=e.REFUND_MAX_FEE_RATE_SATS_VB;exports.REFUND_VSIZE=e.REFUND_VSIZE;exports.RegisteredVaultVersionMismatchError=e.RegisteredVaultVersionMismatchError;exports.activateVault=e.activateVault;exports.buildAndBroadcastRefund=e.buildAndBroadcastRefund;exports.estimateRefundFeeSats=e.estimateRefundFeeSats;exports.isDepositAmountValid=e.isDepositAmountValid;exports.isPegoutTerminalStatus=e.isPegoutTerminalStatus;exports.isRecognizedPegoutStatus=e.isRecognizedPegoutStatus;exports.isRegisteredVaultVersionMismatchError=e.isRegisteredVaultVersionMismatchError;exports.runDepositorPresignFlow=e.runDepositorPresignFlow;exports.signDepositorGraph=e.signDepositorGraph;exports.submitWotsPublicKey=e.submitWotsPublicKey;exports.validateDepositAmount=e.validateDepositAmount;exports.validateMultiVaultDepositInputs=e.validateMultiVaultDepositInputs;exports.validateOnChainParticipantKeys=e.validateOnChainParticipantKeys;exports.validateProviderSelection=e.validateProviderSelection;exports.validateRemainingCapacity=e.validateRemainingCapacity;exports.validateVaultAmounts=e.validateVaultAmounts;exports.validateVaultProviderPubkey=e.validateVaultProviderPubkey;exports.verifyRegisteredVaultVersions=e.verifyRegisteredVaultVersions;exports.waitForPeginStatus=e.waitForPeginStatus;exports.ContractStatus=T.ContractStatus;exports.PeginAction=T.PeginAction;exports.canPerformAction=T.canPerformAction;exports.getPeginProtocolState=T.getPeginProtocolState;
2
2
  //# sourceMappingURL=index.cjs.map
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ import { a as b, b as I } from "./noPayout-CS2wnluA.js";
6
6
  import { d as O, a as v, b as F, e as V, f as U, j as h, g as N, h as y, i as D, c as B, p as M, s as H, t as L, u as k, v as X } from "./bitcoin-B5aNKtsk.js";
7
7
  import { c as w } from "./signing-DaLvGwQe.js";
8
8
  import { B as G, H as Y, K as Z, M as J, T as j } from "./validation-CxqROCno.js";
9
- import { P as z, V as Q, b as $, a as aa, c as ea, f as sa, d as ta, e as ra } from "./PeginManager-B1qjHs90.js";
9
+ import { P as z, V as Q, b as $, a as aa, c as ea, f as sa, d as ta, e as ra } from "./PeginManager-D9ZZ8wx2.js";
10
10
  import { P as ia, c as na, v as ua } from "./PayoutManager-DChODEOJ.js";
11
11
  import { A as pa, P as da } from "./ProtocolParams.abi-C2brDWTI.js";
12
12
  import { B as ca } from "./BTCVaultRegistry.abi-Cq9-JlqT.js";
@@ -15,76 +15,76 @@ import { BitcoinNetworks as ma } from "./shared/index.js";
15
15
  import { B as fa, a as ba, f as Ia, c as Ca, b as Oa, d as va, g as Fa, i as Va, h as Ua, s as ha, e as Na, w as ya } from "./waitForTransactionReceiptSmartAware-CmgFXFza.js";
16
16
  import { B as Ba, D as Ma, F as Ha, L as La, M as ka, P as Xa, b as Wa, a as wa, S as Ka, T as Ga, W as Ya, f as Za, c as Ja, p as ja, r as qa } from "./fundPeginTransaction-t-6TsHAY.js";
17
17
  import { U as Qa, a as $a, c as ae, e as ee, s as se, v as te } from "./reservation-CB-4FBPk.js";
18
- import { M as oe, g as ie, a as ne, b as ue, c as le, d as pe, e as de, f as Pe, p as ce } from "./mempoolApi-CAIge7Nj.js";
19
- import { O as Ee, S as Ae, V as Se, j as Re, k as _e, l as ge, m as me, a as xe, d as fe, b as be, f as Ie, p as Ce, r as Oe, g as ve, h as Fe, v as Ve, i as Ue, c as he, e as Ne } from "./types-0bvDGR4x.js";
20
- import { A as De, D as Be, b as Me, J as He, a as Le, d as ke, P as Xe, R as We, e as we, c as Ke, V as Ge } from "./types-BqGAMOZM.js";
21
- import { i as Ze, p as Je } from "./errors-9AkghWyk.js";
22
- import { B as qe, C as ze, p as Qe, q as $e, t as as, u as es, R as ss, a as ts, n as rs, o as os, i as is, m as ns, l as us, j as ls, r as ps, b as ds, s as Ps, v as cs, g as Ts, h as Es, d as As, c as Ss, e as Rs, f as _s, k as gs, w as ms } from "./buildAndBroadcastRefund-fIHDHiFh.js";
23
- import { C as fs, P as bs, c as Is, g as Cs } from "./peginState-CBAlxgXk.js";
18
+ import { M as oe, g as ie, a as ne, b as ue, c as le, d as pe, e as de, f as Pe, h as ce, p as Te } from "./mempoolApi-C7hkVkym.js";
19
+ import { O as Ae, S as Se, V as Re, j as _e, k as ge, l as me, m as xe, a as fe, d as be, b as Ie, f as Ce, p as Oe, r as ve, g as Fe, h as Ve, v as Ue, i as he, c as Ne, e as ye } from "./types-0bvDGR4x.js";
20
+ import { A as Be, D as Me, b as He, J as Le, a as ke, d as Xe, P as We, R as we, e as Ke, c as Ge, V as Ye } from "./types-BqGAMOZM.js";
21
+ import { i as Je, p as je } from "./errors-9AkghWyk.js";
22
+ import { B as ze, C as Qe, p as $e, q as as, t as es, u as ss, R as ts, a as rs, n as os, o as is, i as ns, m as us, l as ls, j as ps, r as ds, b as Ps, s as cs, v as Ts, g as Es, h as As, d as Ss, c as Rs, e as _s, f as gs, k as ms, w as xs } from "./buildAndBroadcastRefund-fIHDHiFh.js";
23
+ import { C as bs, P as Is, c as Cs, g as Os } from "./peginState-CBAlxgXk.js";
24
24
  export {
25
- De as AUTH_EXPIRED_DATA_KIND,
25
+ Be as AUTH_EXPIRED_DATA_KIND,
26
26
  pa as ApplicationRegistryABI,
27
- qe as BIP68NotMatureError,
27
+ ze as BIP68NotMatureError,
28
28
  G as BITCOIN_ADDRESS_RE,
29
29
  ca as BTCVaultRegistryABI,
30
30
  Ba as BTC_DUST_SAT,
31
31
  ma as BitcoinNetworks,
32
32
  fa as BitcoinScriptType,
33
33
  Ea as CONTRACT_ERRORS,
34
- ze as ClaimerPegoutStatusValue,
35
- fs as ContractStatus,
34
+ Qe as ClaimerPegoutStatusValue,
35
+ bs as ContractStatus,
36
36
  Ma as DUST_THRESHOLD,
37
- Be as DaemonStatus,
37
+ Me as DaemonStatus,
38
38
  Ha as FEE_SAFETY_MARGIN,
39
39
  Y as HEX_RE,
40
- Me as JSON_RPC_ERROR_CODES,
41
- He as JsonRpcClient,
42
- Le as JsonRpcError,
40
+ He as JSON_RPC_ERROR_CODES,
41
+ Le as JsonRpcClient,
42
+ ke as JsonRpcError,
43
43
  Z as KNOWN_SCRIPT_PREFIXES,
44
44
  La as LOW_RATE_ESTIMATION_ACCURACY_BUFFER,
45
45
  ka as MAX_NON_LEGACY_OUTPUT_SIZE,
46
46
  J as MAX_REASONABLE_FEE_SATS,
47
47
  oe as MEMPOOL_API_URLS,
48
- Ee as OnChainBtcVaultStatus,
48
+ Ae as OnChainBtcVaultStatus,
49
49
  Xa as P2TR_INPUT_SIZE,
50
50
  Wa as PEGIN_AUTH_ANCHOR_OUTPUTS,
51
51
  wa as PEGIN_FIXED_OUTPUTS,
52
- ke as POST_WOTS_STATUSES,
53
- Xe as PRE_DEPOSITOR_SIGNATURES_STATES,
52
+ Xe as POST_WOTS_STATUSES,
53
+ We as PRE_DEPOSITOR_SIGNATURES_STATES,
54
54
  ia as PayoutManager,
55
- bs as PeginAction,
55
+ Is as PeginAction,
56
56
  z as PeginManager,
57
57
  da as ProtocolParamsABI,
58
58
  P as PsbtSubstitutionError,
59
- Qe as REFUND_MAX_FEE_FRACTION_DENOMINATOR,
60
- $e as REFUND_MAX_FEE_FRACTION_NUMERATOR,
61
- as as REFUND_MAX_FEE_RATE_SATS_VB,
62
- es as REFUND_VSIZE,
63
- ss as RegisteredVaultVersionMismatchError,
64
- We as RpcErrorCode,
59
+ $e as REFUND_MAX_FEE_FRACTION_DENOMINATOR,
60
+ as as REFUND_MAX_FEE_FRACTION_NUMERATOR,
61
+ es as REFUND_MAX_FEE_RATE_SATS_VB,
62
+ ss as REFUND_VSIZE,
63
+ ts as RegisteredVaultVersionMismatchError,
64
+ we as RpcErrorCode,
65
65
  Ka as SPLIT_TX_FEE_SAFETY_MULTIPLIER,
66
- Ae as ServerIdentityError,
66
+ Se as ServerIdentityError,
67
67
  j as TXID_RE,
68
68
  Ga as TX_BUFFER_SIZE_OVERHEAD,
69
69
  Qa as UtxoNotAvailableError,
70
70
  Q as VAULT_APP_NAME,
71
- we as VP_BATCH_MAX_SIZE,
72
- Ke as VP_TERMINAL_FAILURE_STATUSES,
73
- Ge as VP_TRANSIENT_STATUSES,
74
- Se as VaultProviderRpcClient,
75
- Re as ViemProtocolParamsReader,
76
- _e as ViemUniversalChallengerReader,
77
- ge as ViemVaultKeeperReader,
78
- me as ViemVaultRegistryReader,
79
- xe as VpResponseValidationError,
80
- fe as VpTokenRegistry,
71
+ Ke as VP_BATCH_MAX_SIZE,
72
+ Ge as VP_TERMINAL_FAILURE_STATUSES,
73
+ Ye as VP_TRANSIENT_STATUSES,
74
+ Re as VaultProviderRpcClient,
75
+ _e as ViemProtocolParamsReader,
76
+ ge as ViemUniversalChallengerReader,
77
+ me as ViemVaultKeeperReader,
78
+ xe as ViemVaultRegistryReader,
79
+ fe as VpResponseValidationError,
80
+ be as VpTokenRegistry,
81
81
  Ya as WALLET_RELAY_FEE_RATE_THRESHOLD,
82
- ts as activateVault,
82
+ rs as activateVault,
83
83
  ba as applyChangeOutputPolicy,
84
84
  c as assertPsbtUnsignedTxMatches,
85
85
  $a as assertUtxosAvailable,
86
- be as batchPollByProvider,
87
- rs as buildAndBroadcastRefund,
86
+ Ie as batchPollByProvider,
87
+ os as buildAndBroadcastRefund,
88
88
  s as buildChallengeAssertPsbt,
89
89
  t as buildDepositorPayoutPsbt,
90
90
  $ as buildFundingOutpointsCommitment,
@@ -96,7 +96,7 @@ export {
96
96
  I as buildRefundPsbt,
97
97
  aa as buildVaultContext,
98
98
  Ia as calculateBtcTxHash,
99
- Is as canPerformAction,
99
+ Cs as canPerformAction,
100
100
  ae as collectReservedUtxoRefs,
101
101
  Ca as computeChangeOutputFeeSats,
102
102
  na as computeHashlock,
@@ -105,7 +105,7 @@ export {
105
105
  r as computeNumLocalChallengers,
106
106
  va as computePeginBaseFeeSats,
107
107
  ea as computeWotsBlockPublicKeysHash,
108
- Ie as createAuthenticatedVpClient,
108
+ Ce as createAuthenticatedVpClient,
109
109
  S as createPayoutScript,
110
110
  w as createTaprootScriptPathSignOptions,
111
111
  O as deriveBip86ScriptPubKeyHex,
@@ -115,7 +115,7 @@ export {
115
115
  sa as deriveVaultRoot,
116
116
  ta as deriveWotsBlocksFromSeed,
117
117
  V as ensureHexPrefix,
118
- os as estimateRefundFeeSats,
118
+ is as estimateRefundFeeSats,
119
119
  ra as estimateSubmitPeginRequestBatchGas,
120
120
  u as expandAuthAnchor,
121
121
  l as expandHashlockSecret,
@@ -134,58 +134,59 @@ export {
134
134
  ue as getMempoolApiUrl,
135
135
  h as getNetwork,
136
136
  le as getNetworkFees,
137
- Cs as getPeginProtocolState,
137
+ Os as getPeginProtocolState,
138
138
  Va as getPsbtInputFields,
139
139
  Ua as getScriptType,
140
140
  N as getSortedXOnlyPubkeys,
141
- pe as getTxHex,
142
- de as getTxInfo,
143
- Pe as getUtxoInfo,
141
+ pe as getTipHeight,
142
+ de as getTxHex,
143
+ Pe as getTxInfo,
144
+ ce as getUtxoInfo,
144
145
  Ra as handleContractError,
145
146
  y as hexToUint8Array,
146
147
  D as isAddressFromPublicKey,
147
- is as isDepositAmountValid,
148
+ ns as isDepositAmountValid,
148
149
  _a as isKnownContractError,
149
- ns as isPegoutTerminalStatus,
150
- us as isRecognizedPegoutStatus,
151
- ls as isRegisteredVaultVersionMismatchError,
150
+ us as isPegoutTerminalStatus,
151
+ ls as isRecognizedPegoutStatus,
152
+ ps as isRegisteredVaultVersionMismatchError,
152
153
  B as isValidHex,
153
- Ze as isWotsMismatchError,
154
- Je as parseFundingOutpointsFromTx,
154
+ Je as isWotsMismatchError,
155
+ je as parseFundingOutpointsFromTx,
155
156
  Ja as parseUnfundedWasmTransaction,
156
157
  ja as peginOutputCount,
157
- Ce as primeVpTokenRegistry,
158
+ Oe as primeVpTokenRegistry,
158
159
  M as processPublicKeyToXOnly,
159
- ce as pushTx,
160
+ Te as pushTx,
160
161
  qa as rateBasedTxBufferFee,
161
- Oe as resolveProtocolAddresses,
162
- ps as runDepositorPresignFlow,
162
+ ve as resolveProtocolAddresses,
163
+ ds as runDepositorPresignFlow,
163
164
  se as selectUtxosForDeposit,
164
165
  ha as selectUtxosForPegin,
165
166
  Na as shouldAddChangeOutput,
166
- ds as signDepositorGraph,
167
+ Ps as signDepositorGraph,
167
168
  H as stripHexPrefix,
168
- Ps as submitWotsPublicKey,
169
+ cs as submitWotsPublicKey,
169
170
  L as toXOnly,
170
171
  k as uint8ArrayToHex,
171
- cs as validateDepositAmount,
172
- Ts as validateMultiVaultDepositInputs,
173
- ve as validateOffchainParams,
174
- Es as validateOnChainParticipantKeys,
175
- Fe as validatePegInConfiguration,
176
- As as validateProviderSelection,
177
- Ss as validateRemainingCapacity,
178
- Ve as validateRequestDepositorClaimerArtifactsResponse,
172
+ Ts as validateDepositAmount,
173
+ Es as validateMultiVaultDepositInputs,
174
+ Fe as validateOffchainParams,
175
+ As as validateOnChainParticipantKeys,
176
+ Ve as validatePegInConfiguration,
177
+ Ss as validateProviderSelection,
178
+ Rs as validateRemainingCapacity,
179
+ Ue as validateRequestDepositorClaimerArtifactsResponse,
179
180
  ua as validateSecretAgainstHashlock,
180
- Ue as validateTBVProtocolParams,
181
+ he as validateTBVProtocolParams,
181
182
  te as validateUtxosAvailable,
182
- Rs as validateVaultAmounts,
183
- _s as validateVaultProviderPubkey,
183
+ _s as validateVaultAmounts,
184
+ gs as validateVaultProviderPubkey,
184
185
  X as validateWalletPubkey,
185
- gs as verifyRegisteredVaultVersions,
186
- he as verifyServerIdentity,
187
- Ne as vpTokenRegistry,
188
- ms as waitForPeginStatus,
186
+ ms as verifyRegisteredVaultVersions,
187
+ Ne as verifyServerIdentity,
188
+ ye as vpTokenRegistry,
189
+ xs as waitForPeginStatus,
189
190
  ya as waitForTransactionReceiptSmartAware
190
191
  };
191
192
  //# sourceMappingURL=index.js.map
@@ -1,5 +1,5 @@
1
1
  import { B as $, H as p, K as E, T as F } from "./validation-CxqROCno.js";
2
- const y = 21e6 * 1e8, u = 3e4;
2
+ const I = 21e6 * 1e8, u = 3e4;
3
3
  async function i(e, r) {
4
4
  const t = new AbortController(), n = setTimeout(
5
5
  () => t.abort(),
@@ -20,9 +20,9 @@ async function i(e, r) {
20
20
  }
21
21
  const l = 1e4;
22
22
  function w(e) {
23
- return Number.isInteger(e) && e > 0 && e <= y;
23
+ return Number.isInteger(e) && e > 0 && e <= I;
24
24
  }
25
- function I(e) {
25
+ function g(e) {
26
26
  return Number.isInteger(e) && e > 0 && e <= l;
27
27
  }
28
28
  function m(e, r) {
@@ -48,7 +48,7 @@ function d(e, r) {
48
48
  `Unrecognized scriptPubKey type for ${r}: prefix ${e.slice(0, 6)} does not match any known Bitcoin script type`
49
49
  );
50
50
  }
51
- const T = {
51
+ const y = {
52
52
  mainnet: "https://mempool.space/api",
53
53
  testnet: "https://mempool.space/testnet/api",
54
54
  signet: "https://mempool.space/signet/api"
@@ -94,9 +94,17 @@ async function v(e, r) {
94
94
  throw t instanceof Error ? new Error(`Failed to broadcast BTC transaction: ${t.message}`) : new Error("Failed to broadcast BTC transaction: Unknown error");
95
95
  }
96
96
  }
97
- async function g(e, r) {
97
+ async function T(e, r) {
98
98
  return c(e), s(`${r}/tx/${e}`);
99
99
  }
100
+ async function b(e) {
101
+ const r = await s(`${e}/blocks/tip/height`), t = r.trim();
102
+ if (!/^\d+$/.test(t))
103
+ throw new Error(
104
+ `Mempool API returned an invalid block tip height: "${r}"`
105
+ );
106
+ return Number.parseInt(t, 10);
107
+ }
100
108
  async function A(e, r) {
101
109
  c(e);
102
110
  try {
@@ -112,9 +120,9 @@ async function A(e, r) {
112
120
  throw t instanceof Error ? new Error(`Failed to get transaction hex for ${e}: ${t.message}`) : new Error(`Failed to get transaction hex for ${e}: Unknown error`);
113
121
  }
114
122
  }
115
- async function b(e, r, t) {
123
+ async function P(e, r, t) {
116
124
  c(e);
117
- const n = await g(e, t);
125
+ const n = await T(e, t);
118
126
  if (!m(r, n.vout.length))
119
127
  throw new Error(
120
128
  `Invalid vout ${r} for transaction ${e} (has ${n.vout.length} outputs)`
@@ -129,7 +137,7 @@ async function b(e, r, t) {
129
137
  scriptPubKey: a.scriptpubkey
130
138
  };
131
139
  }
132
- async function P(e, r) {
140
+ async function O(e, r) {
133
141
  h(e);
134
142
  try {
135
143
  const t = await s(`${r}/address/${e}/utxo`), n = await s(`${r}/v1/validate-address/${e}`);
@@ -161,13 +169,13 @@ async function P(e, r) {
161
169
  );
162
170
  }
163
171
  }
164
- function O(e) {
165
- return T[e];
172
+ function U(e) {
173
+ return y[e];
166
174
  }
167
- async function U(e, r) {
175
+ async function k(e, r) {
168
176
  return h(e), s(`${r}/address/${e}/txs`);
169
177
  }
170
- async function S(e) {
178
+ async function M(e) {
171
179
  const r = await i(`${e}/v1/fees/recommended`);
172
180
  if (!r.ok)
173
181
  throw new Error(
@@ -181,7 +189,7 @@ async function S(e) {
181
189
  "minimumFee"
182
190
  ];
183
191
  for (const a of n)
184
- if (!I(t[a]))
192
+ if (!g(t[a]))
185
193
  throw new Error(
186
194
  `Invalid fee rate ${a}=${t[a]} from mempool API: expected a positive number ≤ ${l}`
187
195
  );
@@ -192,14 +200,15 @@ async function S(e) {
192
200
  return t;
193
201
  }
194
202
  export {
195
- T as M,
196
- P as a,
197
- O as b,
198
- S as c,
199
- A as d,
200
- g as e,
201
- b as f,
202
- U as g,
203
+ y as M,
204
+ O as a,
205
+ U as b,
206
+ M as c,
207
+ b as d,
208
+ A as e,
209
+ T as f,
210
+ k as g,
211
+ P as h,
203
212
  v as p
204
213
  };
205
- //# sourceMappingURL=mempoolApi-CAIge7Nj.js.map
214
+ //# sourceMappingURL=mempoolApi-C7hkVkym.js.map