@babylonlabs-io/ts-sdk 0.19.0 → 0.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/README.md +56 -12
  2. package/dist/bitcoin-CNnPFU6Y.cjs +2 -0
  3. package/dist/bitcoin-CNnPFU6Y.cjs.map +1 -0
  4. package/dist/{constants-Q7v2O7Ps.js → bitcoin-nOOgeRyl.js} +64 -76
  5. package/dist/bitcoin-nOOgeRyl.js.map +1 -0
  6. package/dist/buildAndBroadcastRefund-DWEQvj9T.cjs +2 -0
  7. package/dist/buildAndBroadcastRefund-DWEQvj9T.cjs.map +1 -0
  8. package/dist/buildAndBroadcastRefund-DnTQkCgG.js +1331 -0
  9. package/dist/buildAndBroadcastRefund-DnTQkCgG.js.map +1 -0
  10. package/dist/challengeAssert-D1lpvuMv.js +283 -0
  11. package/dist/challengeAssert-D1lpvuMv.js.map +1 -0
  12. package/dist/challengeAssert-nYlgeAI8.cjs +2 -0
  13. package/dist/challengeAssert-nYlgeAI8.cjs.map +1 -0
  14. package/dist/errors-D9EtjJoD.cjs +2 -0
  15. package/dist/errors-D9EtjJoD.cjs.map +1 -0
  16. package/dist/{errors-aMocmFr-.js → errors-D_PI__IT.js} +98 -97
  17. package/dist/errors-D_PI__IT.js.map +1 -0
  18. package/dist/fundPeginTransaction-BLYXxLBv.js +70 -0
  19. package/dist/fundPeginTransaction-BLYXxLBv.js.map +1 -0
  20. package/dist/fundPeginTransaction-DxNOeyNI.cjs +2 -0
  21. package/dist/fundPeginTransaction-DxNOeyNI.cjs.map +1 -0
  22. package/dist/index-BpXpESWu.cjs +2 -0
  23. package/dist/index-BpXpESWu.cjs.map +1 -0
  24. package/dist/{index-BvFLf3vm.js → index-CFwoGJNQ.js} +146 -518
  25. package/dist/index-CFwoGJNQ.js.map +1 -0
  26. package/dist/index.cjs +1 -1
  27. package/dist/index.js +133 -109
  28. package/dist/payout-DzGsAnhf.js +261 -0
  29. package/dist/payout-DzGsAnhf.js.map +1 -0
  30. package/dist/payout-RFIh7GLN.cjs +2 -0
  31. package/dist/payout-RFIh7GLN.cjs.map +1 -0
  32. package/dist/psbtInputFields-05ZrwRzf.cjs +2 -0
  33. package/dist/psbtInputFields-05ZrwRzf.cjs.map +1 -0
  34. package/dist/psbtInputFields-r1ss6WLU.js +237 -0
  35. package/dist/psbtInputFields-r1ss6WLU.js.map +1 -0
  36. package/dist/signing-DeWVBl7m.js +64 -0
  37. package/dist/signing-DeWVBl7m.js.map +1 -0
  38. package/dist/signing-cl-lowxV.cjs +2 -0
  39. package/dist/signing-cl-lowxV.cjs.map +1 -0
  40. package/dist/tbv/core/clients/index.cjs +1 -1
  41. package/dist/tbv/core/clients/index.js +2 -2
  42. package/dist/tbv/core/index.cjs +1 -1
  43. package/dist/tbv/core/index.d.ts +3 -1
  44. package/dist/tbv/core/index.d.ts.map +1 -1
  45. package/dist/tbv/core/index.js +130 -106
  46. package/dist/tbv/core/managers/PeginManager.d.ts +1 -1
  47. package/dist/tbv/core/primitives/index.cjs +1 -1
  48. package/dist/tbv/core/primitives/index.d.ts +3 -2
  49. package/dist/tbv/core/primitives/index.d.ts.map +1 -1
  50. package/dist/tbv/core/primitives/index.js +24 -23
  51. package/dist/tbv/core/primitives/psbt/__tests__/helpers.d.ts +1 -1
  52. package/dist/tbv/core/primitives/psbt/index.d.ts +1 -1
  53. package/dist/tbv/core/primitives/psbt/payout.d.ts.map +1 -1
  54. package/dist/tbv/core/primitives/psbt/pegin.d.ts +2 -3
  55. package/dist/tbv/core/primitives/psbt/pegin.d.ts.map +1 -1
  56. package/dist/tbv/core/primitives/utils/bitcoin.d.ts +4 -0
  57. package/dist/tbv/core/primitives/utils/bitcoin.d.ts.map +1 -1
  58. package/dist/tbv/core/primitives/utils/index.d.ts +1 -1
  59. package/dist/tbv/core/primitives/utils/index.d.ts.map +1 -1
  60. package/dist/tbv/core/services/activation/__tests__/activateVault.test.d.ts +2 -0
  61. package/dist/tbv/core/services/activation/__tests__/activateVault.test.d.ts.map +1 -0
  62. package/dist/tbv/core/services/activation/activateVault.d.ts +77 -0
  63. package/dist/tbv/core/services/activation/activateVault.d.ts.map +1 -0
  64. package/dist/tbv/core/services/activation/index.d.ts +7 -0
  65. package/dist/tbv/core/services/activation/index.d.ts.map +1 -0
  66. package/dist/tbv/core/services/deposit/__tests__/peginState.test.d.ts +2 -0
  67. package/dist/tbv/core/services/deposit/__tests__/peginState.test.d.ts.map +1 -0
  68. package/dist/tbv/core/services/deposit/__tests__/validation.test.d.ts +5 -0
  69. package/dist/tbv/core/services/deposit/__tests__/validation.test.d.ts.map +1 -0
  70. package/dist/tbv/core/services/deposit/index.d.ts +4 -0
  71. package/dist/tbv/core/services/deposit/index.d.ts.map +1 -1
  72. package/dist/tbv/core/services/deposit/peginState.d.ts +93 -0
  73. package/dist/tbv/core/services/deposit/peginState.d.ts.map +1 -0
  74. package/dist/tbv/core/services/deposit/validation.d.ts +114 -0
  75. package/dist/tbv/core/services/deposit/validation.d.ts.map +1 -0
  76. package/dist/tbv/core/services/index.cjs +1 -1
  77. package/dist/tbv/core/services/index.d.ts +3 -0
  78. package/dist/tbv/core/services/index.d.ts.map +1 -1
  79. package/dist/tbv/core/services/index.js +25 -7
  80. package/dist/tbv/core/services/index.js.map +1 -1
  81. package/dist/tbv/core/services/pegout/__tests__/state.test.d.ts +2 -0
  82. package/dist/tbv/core/services/pegout/__tests__/state.test.d.ts.map +1 -0
  83. package/dist/tbv/core/services/pegout/index.d.ts +2 -0
  84. package/dist/tbv/core/services/pegout/index.d.ts.map +1 -0
  85. package/dist/tbv/core/services/pegout/state.d.ts +30 -0
  86. package/dist/tbv/core/services/pegout/state.d.ts.map +1 -0
  87. package/dist/tbv/core/services/refund/__tests__/buildAndBroadcastRefund.test.d.ts +2 -0
  88. package/dist/tbv/core/services/refund/__tests__/buildAndBroadcastRefund.test.d.ts.map +1 -0
  89. package/dist/tbv/core/services/refund/buildAndBroadcastRefund.d.ts +103 -0
  90. package/dist/tbv/core/services/refund/buildAndBroadcastRefund.d.ts.map +1 -0
  91. package/dist/tbv/core/services/refund/errors.d.ts +13 -0
  92. package/dist/tbv/core/services/refund/errors.d.ts.map +1 -0
  93. package/dist/tbv/core/services/refund/index.d.ts +8 -0
  94. package/dist/tbv/core/services/refund/index.d.ts.map +1 -0
  95. package/dist/tbv/core/utils/index.cjs +1 -1
  96. package/dist/tbv/core/utils/index.js +30 -24
  97. package/dist/tbv/core/utils/transaction/fundPeginTransaction.d.ts +1 -1
  98. package/dist/tbv/core/utils/utxo/__tests__/availability.test.d.ts +3 -0
  99. package/dist/tbv/core/utils/utxo/__tests__/availability.test.d.ts.map +1 -0
  100. package/dist/tbv/core/utils/utxo/__tests__/reservation.test.d.ts +3 -0
  101. package/dist/tbv/core/utils/utxo/__tests__/reservation.test.d.ts.map +1 -0
  102. package/dist/tbv/core/utils/utxo/availability.d.ts +62 -0
  103. package/dist/tbv/core/utils/utxo/availability.d.ts.map +1 -0
  104. package/dist/tbv/core/utils/utxo/index.d.ts +2 -0
  105. package/dist/tbv/core/utils/utxo/index.d.ts.map +1 -1
  106. package/dist/tbv/core/utils/utxo/reservation.d.ts +91 -0
  107. package/dist/tbv/core/utils/utxo/reservation.d.ts.map +1 -0
  108. package/dist/tbv/index.cjs +1 -1
  109. package/dist/tbv/index.js +130 -106
  110. package/dist/tbv/integrations/aave/index.cjs +1 -1
  111. package/dist/tbv/integrations/aave/index.cjs.map +1 -1
  112. package/dist/tbv/integrations/aave/index.d.ts +3 -3
  113. package/dist/tbv/integrations/aave/index.d.ts.map +1 -1
  114. package/dist/tbv/integrations/aave/index.js +145 -169
  115. package/dist/tbv/integrations/aave/index.js.map +1 -1
  116. package/dist/tbv/integrations/aave/utils/healthFactor.d.ts +0 -30
  117. package/dist/tbv/integrations/aave/utils/healthFactor.d.ts.map +1 -1
  118. package/dist/tbv/integrations/aave/utils/index.d.ts +2 -2
  119. package/dist/tbv/integrations/aave/utils/index.d.ts.map +1 -1
  120. package/dist/types-B-p4dhEH.cjs +2 -0
  121. package/dist/types-B-p4dhEH.cjs.map +1 -0
  122. package/dist/types-DWjaqVfP.js +608 -0
  123. package/dist/types-DWjaqVfP.js.map +1 -0
  124. package/package.json +5 -1
  125. package/dist/challengeAssert-D_k_ADgP.cjs +0 -2
  126. package/dist/challengeAssert-D_k_ADgP.cjs.map +0 -1
  127. package/dist/challengeAssert-k5_LWUtO.js +0 -362
  128. package/dist/challengeAssert-k5_LWUtO.js.map +0 -1
  129. package/dist/constants-EiyZkXce.cjs +0 -2
  130. package/dist/constants-EiyZkXce.cjs.map +0 -1
  131. package/dist/constants-Q7v2O7Ps.js.map +0 -1
  132. package/dist/errors-DVNYib5y.cjs +0 -2
  133. package/dist/errors-DVNYib5y.cjs.map +0 -1
  134. package/dist/errors-aMocmFr-.js.map +0 -1
  135. package/dist/fundPeginTransaction-DpwnDslW.js +0 -50
  136. package/dist/fundPeginTransaction-DpwnDslW.js.map +0 -1
  137. package/dist/fundPeginTransaction-EbrZzlrh.cjs +0 -2
  138. package/dist/fundPeginTransaction-EbrZzlrh.cjs.map +0 -1
  139. package/dist/index-BX-V3C9t.js +0 -995
  140. package/dist/index-BX-V3C9t.js.map +0 -1
  141. package/dist/index-BgnxXV5G.cjs +0 -2
  142. package/dist/index-BgnxXV5G.cjs.map +0 -1
  143. package/dist/index-BvFLf3vm.js.map +0 -1
  144. package/dist/index-DpKhuCta.cjs +0 -2
  145. package/dist/index-DpKhuCta.cjs.map +0 -1
  146. package/dist/payout-Ce9vSs9e.js +0 -164
  147. package/dist/payout-Ce9vSs9e.js.map +0 -1
  148. package/dist/payout-CfsDnjKI.cjs +0 -2
  149. package/dist/payout-CfsDnjKI.cjs.map +0 -1
  150. package/dist/psbtInputFields-C0nKn_GD.cjs +0 -2
  151. package/dist/psbtInputFields-C0nKn_GD.cjs.map +0 -1
  152. package/dist/psbtInputFields-DO0ELwiv.js +0 -116
  153. package/dist/psbtInputFields-DO0ELwiv.js.map +0 -1
  154. package/dist/signing-Deg5lCoC.cjs +0 -2
  155. package/dist/signing-Deg5lCoC.cjs.map +0 -1
  156. package/dist/signing-Drwr3bXB.js +0 -16
  157. package/dist/signing-Drwr3bXB.js.map +0 -1
  158. package/dist/types-D1rYwwCu.js +0 -235
  159. package/dist/types-D1rYwwCu.js.map +0 -1
  160. package/dist/types-DEWiqXYp.cjs +0 -2
  161. package/dist/types-DEWiqXYp.cjs.map +0 -1
package/README.md CHANGED
@@ -30,11 +30,30 @@ This SDK handles the complex Bitcoin and Ethereum interactions needed to create
30
30
  - **🧩 Modular Design** - Use only what you need via subpath exports
31
31
  - **🔧 Extensible** - Easy to build custom integrations
32
32
 
33
+ ## Platforms
34
+
35
+ This SDK runs on **both Node.js backends and browser/React frontends** — the
36
+ entire library is framework-agnostic. The *only* difference between the two
37
+ environments is how you supply two things: a Bitcoin wallet and an Ethereum
38
+ wallet. The SDK provides interfaces (`BitcoinWallet`, viem's `WalletClient`)
39
+ and you adapt whichever wallet is available on your platform.
40
+
41
+ | Concern | Node.js backend | Browser / React |
42
+ |---------|-----------------|-----------------|
43
+ | `BitcoinWallet` | Build from a seed via `bitcoinjs-lib` + `bip32` (or call KMS/HSM) | Adapt an injected wallet (Unisat, OKX, Xverse, Leather) |
44
+ | viem `WalletClient` | `createWalletClient({ account: privateKeyToAccount(pk), chain, transport: http(RPC_URL) })` | `createWalletClient({ account, chain, transport: custom(window.ethereum) })` via wagmi |
45
+ | Secret storage | Your DB / KMS (required for activation later) | Session/local storage or user-supplied |
46
+ | WASM loading | Automatic (SDK reads `.wasm` off disk with `node:fs`) | Automatic (SDK fetches `.wasm` via bundler) |
47
+
48
+ If you're using a bundler (Vite, webpack, Next.js), ensure it's configured to
49
+ handle `.wasm` assets and that `Buffer` is available on the global in browser
50
+ targets — see the [Troubleshooting Guide](./docs/get-started/troubleshooting.md).
51
+
33
52
  ## Installation
34
53
 
35
54
  ### Requirements
36
55
 
37
- - Node.js >= 24.0.0
56
+ - **Node.js 20.3.0** (for `AbortSignal.any()`). Works on Node 20 LTS, 22 LTS, 24 LTS.
38
57
  - Package manager: npm, yarn, or pnpm
39
58
 
40
59
  ### Install
@@ -50,27 +69,34 @@ yarn add @babylonlabs-io/ts-sdk viem bitcoinjs-lib @bitcoin-js/tiny-secp256k1-as
50
69
  pnpm add @babylonlabs-io/ts-sdk viem bitcoinjs-lib @bitcoin-js/tiny-secp256k1-asmjs
51
70
  ```
52
71
 
53
- ### ECC Library Initialization
72
+ ### ECC Library Initialization (required, once at startup)
54
73
 
55
- The SDK uses `bitcoinjs-lib` for Taproot (P2TR) operations, which requires an ECC library to be initialized **before** any SDK function that touches Bitcoin addresses or PSBTs. Your application must call `initEccLib()` once at startup:
74
+ The SDK uses `bitcoinjs-lib` for Taproot operations. Call `initEccLib()` once
75
+ at application startup **before any SDK call that builds a PSBT or derives a
76
+ Bitcoin address**:
56
77
 
57
78
  ```typescript
58
79
  import * as ecc from "@bitcoin-js/tiny-secp256k1-asmjs";
59
80
  import { initEccLib } from "bitcoinjs-lib";
60
81
 
61
- // Call once at app startup, before any SDK usage
62
82
  initEccLib(ecc);
63
83
  ```
64
84
 
65
- For React apps, place this call in your entry point (e.g., `main.tsx`) before `createRoot()`. Failing to initialize will cause a runtime error: `"No ECC Library provided"`.
85
+ - **Node.js**: call this at the top of your entry point (`index.ts` / CLI entry).
86
+ - **React**: call it in `main.tsx` / `app.tsx` before `createRoot()`.
87
+
88
+ Failing to initialize produces a runtime error: `"No ECC Library provided"`.
89
+
90
+ The WASM package used internally (`@babylonlabs-io/babylon-tbv-rust-wasm`)
91
+ initializes itself lazily — no extra call needed.
66
92
 
67
93
  ### Verify Installation
68
94
 
69
95
  ```typescript
70
- import { buildPeginPsbt } from "@babylonlabs-io/ts-sdk/tbv/core/primitives";
96
+ import { buildPrePeginPsbt } from "@babylonlabs-io/ts-sdk/tbv/core/primitives";
71
97
 
72
98
  console.log("✅ SDK installed successfully!");
73
- console.log("buildPeginPsbt type:", typeof buildPeginPsbt);
99
+ console.log("buildPrePeginPsbt type:", typeof buildPrePeginPsbt);
74
100
  ```
75
101
 
76
102
  Run with: `npx tsx verify-install.ts`
@@ -87,26 +113,44 @@ import { PeginManager, PayoutManager } from "@babylonlabs-io/ts-sdk/tbv/core";
87
113
 
88
114
  // Low-level primitives (advanced use cases)
89
115
  import {
90
- buildPeginPsbt,
116
+ buildPrePeginPsbt,
117
+ buildPeginTxFromFundedPrePegin,
91
118
  buildPayoutPsbt,
92
119
  buildDepositorPayoutPsbt,
93
120
  buildNoPayoutPsbt,
94
121
  buildChallengeAssertPsbt,
122
+ buildRefundPsbt,
123
+ formatSatoshisToBtc,
95
124
  } from "@babylonlabs-io/ts-sdk/tbv/core/primitives";
96
125
 
126
+ // Services (activation, deposit validation, protocol state)
127
+ import {
128
+ activateVault,
129
+ getPeginProtocolState,
130
+ ContractStatus,
131
+ validateDepositAmount,
132
+ isPegoutTerminalStatus,
133
+ } from "@babylonlabs-io/ts-sdk/tbv/core/services";
134
+
97
135
  // Utilities
98
- import { selectUtxosForPegin } from "@babylonlabs-io/ts-sdk/tbv/core";
136
+ import {
137
+ selectUtxosForPegin,
138
+ collectReservedUtxoRefs,
139
+ validateUtxosAvailable,
140
+ } from "@babylonlabs-io/ts-sdk/tbv/core";
99
141
 
100
- // Shared types and wallet interfaces
101
- import { BitcoinWallet, UTXO } from "@babylonlabs-io/ts-sdk/shared";
142
+ // Shared wallet interfaces
143
+ import { BitcoinWallet } from "@babylonlabs-io/ts-sdk/shared";
144
+ // UTXO type lives in the core utils module
145
+ import { UTXO } from "@babylonlabs-io/ts-sdk/tbv/core";
102
146
 
103
147
  // Contract ABIs
104
148
  import { BTCVaultRegistryABI } from "@babylonlabs-io/ts-sdk/tbv/core";
105
149
 
106
150
  // Protocol integrations (Aave)
107
151
  import {
108
- buildAddCollateralTx,
109
152
  buildBorrowTx,
153
+ buildRepayTx,
110
154
  getUserAccountData,
111
155
  calculateHealthFactor,
112
156
  } from "@babylonlabs-io/ts-sdk/tbv/integrations/aave";
@@ -0,0 +1,2 @@
1
+ "use strict";const a=require("buffer"),i=require("bitcoinjs-lib"),k=192;function o(e){return e.startsWith("0x")||e.startsWith("0X")?e.slice(2):e}function E(e){return e.startsWith("0x")?e:e.startsWith("0X")?`0x${e.slice(2)}`:`0x${e}`}function s(e){const t=o(e);if(!l(t))throw new Error(`Invalid hex string: ${e}`);const n=new Uint8Array(t.length/2);for(let r=0;r<t.length;r+=2)n[r/2]=parseInt(t.slice(r,r+2),16);return n}function h(e){return Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join("")}function y(e){return e.length===32?e:e.slice(1,33)}function l(e){return/^[0-9a-fA-F]*$/.test(e)&&e.length%2===0}function u(e){const t=o(e);if(!l(t))throw new Error(`Invalid hex characters in public key: ${e}`);if(t.length===64)return t;if(t.length!==66&&t.length!==130)throw new Error(`Invalid public key length: ${t.length} (expected 64, 66, or 130 hex chars)`);const n=s(t);return h(y(n))}function x(e){const t=o(e);return l(t)}function S(e,t){const n=u(e),r=t??n;if(n.toLowerCase()!==r.toLowerCase())throw new Error(`Wallet public key does not match vault depositor. Expected: ${r}, Got: ${n}. Please connect the wallet that was used to create this vault.`);return{walletPubkeyRaw:e,walletPubkeyXOnly:n,depositorPubkey:r}}const d=100000000n;function w(e){if(e<0n)return`-${w(-e)}`;const t=e/d;let r=(e%d).toString().padStart(8,"0");return r=r.replace(/0+$/,""),r.length>0?`${t}.${r}`:t.toString()}function m(){try{i.payments.p2tr({internalPubkey:a.Buffer.alloc(32,1)})}catch(e){if(e instanceof Error&&e.message.includes("No ECC Library provided"))throw new Error('ECC library not initialized. You must call initEccLib(ecc) from "bitcoinjs-lib" before using the SDK. See the ts-sdk README for setup instructions.')}}function f(e){switch(e){case"bitcoin":return i.networks.bitcoin;case"testnet":case"signet":return i.networks.testnet;case"regtest":return i.networks.regtest;default:throw new Error(`Unknown network: ${e}`)}}function p(e,t){m();const n=s(u(e)),{address:r}=i.payments.p2tr({internalPubkey:a.Buffer.from(n),network:f(t)});if(!r)throw new Error("Failed to derive taproot address from public key");return r}function g(e,t){const n=o(e);if(n.length!==66)throw new Error(`Native SegWit requires a compressed public key (66 hex chars), got ${n.length}`);const{address:r}=i.payments.p2wpkh({pubkey:a.Buffer.from(s(n)),network:f(t)});if(!r)throw new Error("Failed to derive native segwit address from public key");return r}function A(e,t,n){const r=o(t);try{if(e===p(r,n))return!0}catch{}const c=[];r.length===66?c.push(r):r.length===64&&c.push(`02${r}`,`03${r}`);for(const b of c)try{if(e===g(b,n))return!0}catch{}return!1}exports.TAPSCRIPT_LEAF_VERSION=k;exports.deriveNativeSegwitAddress=g;exports.deriveTaprootAddress=p;exports.ensureHexPrefix=E;exports.formatSatoshisToBtc=w;exports.getNetwork=f;exports.hexToUint8Array=s;exports.isAddressFromPublicKey=A;exports.isValidHex=x;exports.processPublicKeyToXOnly=u;exports.stripHexPrefix=o;exports.toXOnly=y;exports.uint8ArrayToHex=h;exports.validateWalletPubkey=S;
2
+ //# sourceMappingURL=bitcoin-CNnPFU6Y.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bitcoin-CNnPFU6Y.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 * 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 * 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 === 64) {\n return cleanHex;\n }\n\n // Validate public key length (should be 66 chars for compressed or 130 for uncompressed)\n if (cleanHex.length !== 66 && cleanHex.length !== 130) {\n throw new Error(\n `Invalid public key length: ${cleanHex.length} (expected 64, 66, or 130 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. Uses the expected depositor pubkey if provided, otherwise falls back to wallet pubkey\n * 3. Validates they match (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, optional)\n * @returns Validation result with both pubkey formats\n * @throws If wallet pubkey doesn't match expected depositor pubkey\n */\nexport function validateWalletPubkey(\n walletPubkeyRaw: string,\n expectedDepositorPubkey?: string,\n): WalletPubkeyValidationResult {\n const walletPubkeyXOnly = processPublicKeyToXOnly(walletPubkeyRaw);\n const depositorPubkey = expectedDepositorPubkey ?? walletPubkeyXOnly;\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 * 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 * When the input is an x-only key (64 hex chars), both possible compressed\n * keys (`02` + x and `03` + x) are tried for Native SegWit derivation,\n * since the y-parity is unknown.\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 // Build the list of compressed keys to try for P2WPKH\n const compressedKeys: string[] = [];\n if (cleanHex.length === 66) {\n compressedKeys.push(cleanHex);\n } else if (cleanHex.length === 64) {\n // x-only key — try both even (02) and odd (03) y-parity\n compressedKeys.push(`02${cleanHex}`, `03${cleanHex}`);\n }\n\n for (const key of compressedKeys) {\n try {\n if (address === deriveNativeSegwitAddress(key, network)) {\n return true;\n }\n } catch {\n // derivation failed, continue\n }\n }\n\n return false;\n}\n"],"names":["TAPSCRIPT_LEAF_VERSION","stripHexPrefix","hex","ensureHexPrefix","hexToUint8Array","cleanHex","isValidHexRaw","bytes","i","uint8ArrayToHex","b","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","deriveNativeSegwitAddress","isAddressFromPublicKey","compressedKeys","key"],"mappings":"kEA0BaA,EAAyB,IAW/B,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,CAW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EAAU0B,CAAO,EACpD,MAAO,EAEX,MAAQ,CAER,CAGA,MAAMO,EAA2B,CAAA,EAC7BjC,EAAS,SAAW,GACtBiC,EAAe,KAAKjC,CAAQ,EACnBA,EAAS,SAAW,IAE7BiC,EAAe,KAAK,KAAKjC,CAAQ,GAAI,KAAKA,CAAQ,EAAE,EAGtD,UAAWkC,KAAOD,EAChB,GAAI,CACF,GAAIH,IAAYC,EAA0BG,EAAKR,CAAO,EACpD,MAAO,EAEX,MAAQ,CAER,CAGF,MAAO,EACT"}
@@ -1,33 +1,33 @@
1
1
  import { Buffer as a } from "buffer";
2
- import { payments as c, networks as i } from "bitcoinjs-lib";
3
- const k = 192;
4
- function s(t) {
2
+ import { payments as c, networks as s } from "bitcoinjs-lib";
3
+ const $ = 192;
4
+ function o(t) {
5
5
  return t.startsWith("0x") || t.startsWith("0X") ? t.slice(2) : t;
6
6
  }
7
- function P(t) {
8
- return t.startsWith("0x") ? t : `0x${t}`;
7
+ function v(t) {
8
+ return t.startsWith("0x") ? t : t.startsWith("0X") ? `0x${t.slice(2)}` : `0x${t}`;
9
9
  }
10
- function u(t) {
11
- const e = s(t);
12
- if (!l(e))
10
+ function l(t) {
11
+ const e = o(t);
12
+ if (!u(e))
13
13
  throw new Error(`Invalid hex string: ${t}`);
14
14
  const n = new Uint8Array(e.length / 2);
15
15
  for (let r = 0; r < e.length; r += 2)
16
16
  n[r / 2] = parseInt(e.slice(r, r + 2), 16);
17
17
  return n;
18
18
  }
19
- function h(t) {
19
+ function w(t) {
20
20
  return Array.from(t).map((e) => e.toString(16).padStart(2, "0")).join("");
21
21
  }
22
- function p(t) {
22
+ function g(t) {
23
23
  return t.length === 32 ? t : t.slice(1, 33);
24
24
  }
25
- function l(t) {
25
+ function u(t) {
26
26
  return /^[0-9a-fA-F]*$/.test(t) && t.length % 2 === 0;
27
27
  }
28
- function f(t) {
29
- const e = s(t);
30
- if (!l(e))
28
+ function d(t) {
29
+ const e = o(t);
30
+ if (!u(e))
31
31
  throw new Error(`Invalid hex characters in public key: ${t}`);
32
32
  if (e.length === 64)
33
33
  return e;
@@ -35,22 +35,30 @@ function f(t) {
35
35
  throw new Error(
36
36
  `Invalid public key length: ${e.length} (expected 64, 66, or 130 hex chars)`
37
37
  );
38
- const n = u(e);
39
- return h(p(n));
38
+ const n = l(e);
39
+ return w(g(n));
40
40
  }
41
- function m(t) {
42
- const e = s(t);
43
- return l(e);
41
+ function x(t) {
42
+ const e = o(t);
43
+ return u(e);
44
44
  }
45
- function x(t, e) {
46
- const n = f(t), r = e ?? n;
45
+ function A(t, e) {
46
+ const n = d(t), r = e ?? n;
47
47
  if (n.toLowerCase() !== r.toLowerCase())
48
48
  throw new Error(
49
49
  `Wallet public key does not match vault depositor. Expected: ${r}, Got: ${n}. Please connect the wallet that was used to create this vault.`
50
50
  );
51
51
  return { walletPubkeyRaw: t, walletPubkeyXOnly: n, depositorPubkey: r };
52
52
  }
53
- function w() {
53
+ const f = 100000000n;
54
+ function y(t) {
55
+ if (t < 0n)
56
+ return `-${y(-t)}`;
57
+ const e = t / f;
58
+ let r = (t % f).toString().padStart(8, "0");
59
+ return r = r.replace(/0+$/, ""), r.length > 0 ? `${e}.${r}` : e.toString();
60
+ }
61
+ function b() {
54
62
  try {
55
63
  c.p2tr({ internalPubkey: a.alloc(32, 1) });
56
64
  } catch (t) {
@@ -60,38 +68,38 @@ function w() {
60
68
  );
61
69
  }
62
70
  }
63
- function E(t) {
71
+ function h(t) {
64
72
  switch (t) {
65
73
  case "bitcoin":
66
- return i.bitcoin;
74
+ return s.bitcoin;
67
75
  case "testnet":
68
76
  case "signet":
69
- return i.testnet;
77
+ return s.testnet;
70
78
  case "regtest":
71
- return i.regtest;
79
+ return s.regtest;
72
80
  default:
73
81
  throw new Error(`Unknown network: ${t}`);
74
82
  }
75
83
  }
76
- function T(t, e) {
77
- w();
78
- const n = u(f(t)), { address: r } = c.p2tr({
84
+ function E(t, e) {
85
+ b();
86
+ const n = l(d(t)), { address: r } = c.p2tr({
79
87
  internalPubkey: a.from(n),
80
- network: E(e)
88
+ network: h(e)
81
89
  });
82
90
  if (!r)
83
91
  throw new Error("Failed to derive taproot address from public key");
84
92
  return r;
85
93
  }
86
- function _(t, e) {
87
- const n = s(t);
94
+ function k(t, e) {
95
+ const n = o(t);
88
96
  if (n.length !== 66)
89
97
  throw new Error(
90
98
  `Native SegWit requires a compressed public key (66 hex chars), got ${n.length}`
91
99
  );
92
100
  const { address: r } = c.p2wpkh({
93
- pubkey: a.from(u(n)),
94
- network: E(e)
101
+ pubkey: a.from(l(n)),
102
+ network: h(e)
95
103
  });
96
104
  if (!r)
97
105
  throw new Error(
@@ -99,57 +107,37 @@ function _(t, e) {
99
107
  );
100
108
  return r;
101
109
  }
102
- function F(t, e, n) {
103
- const r = s(e);
110
+ function P(t, e, n) {
111
+ const r = o(e);
104
112
  try {
105
- if (t === T(r, n))
113
+ if (t === E(r, n))
106
114
  return !0;
107
115
  } catch {
108
116
  }
109
- const o = [];
110
- r.length === 66 ? o.push(r) : r.length === 64 && o.push(`02${r}`, `03${r}`);
111
- for (const d of o)
117
+ const i = [];
118
+ r.length === 66 ? i.push(r) : r.length === 64 && i.push(`02${r}`, `03${r}`);
119
+ for (const p of i)
112
120
  try {
113
- if (t === _(d, n))
121
+ if (t === k(p, n))
114
122
  return !0;
115
123
  } catch {
116
124
  }
117
125
  return !1;
118
126
  }
119
- const L = 58, v = 43, R = 11, y = 546, H = BigInt(y), g = 30, A = 2, O = 1.1;
120
- function U(t) {
121
- return t <= A ? g : 0;
122
- }
123
- const S = 1;
124
- function C(t) {
125
- return t + S;
126
- }
127
- const N = 5;
128
127
  export {
129
- y as B,
130
- H as D,
131
- O as F,
132
- g as L,
133
- v as M,
134
- L as P,
135
- N as S,
136
- R as T,
137
- A as W,
138
- T as a,
139
- m as b,
140
- S as c,
141
- _ as d,
142
- P as e,
143
- C as f,
144
- E as g,
145
- u as h,
146
- F as i,
147
- k as j,
148
- f as p,
149
- U as r,
150
- s,
151
- p as t,
152
- h as u,
153
- x as v
128
+ $ as T,
129
+ E as a,
130
+ x as b,
131
+ k as d,
132
+ v as e,
133
+ y as f,
134
+ h as g,
135
+ l as h,
136
+ P as i,
137
+ d as p,
138
+ o as s,
139
+ g as t,
140
+ w as u,
141
+ A as v
154
142
  };
155
- //# sourceMappingURL=constants-Q7v2O7Ps.js.map
143
+ //# sourceMappingURL=bitcoin-nOOgeRyl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bitcoin-nOOgeRyl.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 * 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 * 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 === 64) {\n return cleanHex;\n }\n\n // Validate public key length (should be 66 chars for compressed or 130 for uncompressed)\n if (cleanHex.length !== 66 && cleanHex.length !== 130) {\n throw new Error(\n `Invalid public key length: ${cleanHex.length} (expected 64, 66, or 130 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. Uses the expected depositor pubkey if provided, otherwise falls back to wallet pubkey\n * 3. Validates they match (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, optional)\n * @returns Validation result with both pubkey formats\n * @throws If wallet pubkey doesn't match expected depositor pubkey\n */\nexport function validateWalletPubkey(\n walletPubkeyRaw: string,\n expectedDepositorPubkey?: string,\n): WalletPubkeyValidationResult {\n const walletPubkeyXOnly = processPublicKeyToXOnly(walletPubkeyRaw);\n const depositorPubkey = expectedDepositorPubkey ?? walletPubkeyXOnly;\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 * 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 * When the input is an x-only key (64 hex chars), both possible compressed\n * keys (`02` + x and `03` + x) are tried for Native SegWit derivation,\n * since the y-parity is unknown.\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 // Build the list of compressed keys to try for P2WPKH\n const compressedKeys: string[] = [];\n if (cleanHex.length === 66) {\n compressedKeys.push(cleanHex);\n } else if (cleanHex.length === 64) {\n // x-only key — try both even (02) and odd (03) y-parity\n compressedKeys.push(`02${cleanHex}`, `03${cleanHex}`);\n }\n\n for (const key of compressedKeys) {\n try {\n if (address === deriveNativeSegwitAddress(key, network)) {\n return true;\n }\n } catch {\n // derivation failed, continue\n }\n }\n\n return false;\n}\n"],"names":["TAPSCRIPT_LEAF_VERSION","stripHexPrefix","hex","ensureHexPrefix","hexToUint8Array","cleanHex","isValidHexRaw","bytes","i","uint8ArrayToHex","b","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","deriveNativeSegwit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AA0BG,GAAKR,CAAO;AACpD,eAAO;AAAA,IAEX,QAAQ;AAAA,IAER;AAGF,SAAO;AACT;"}
@@ -0,0 +1,2 @@
1
+ "use strict";var Ct=Object.defineProperty;var Lt=(t,e,r)=>e in t?Ct(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var f=(t,e,r)=>Lt(t,typeof e!="symbol"?e+"":e,r);const m=require("./types-B-p4dhEH.cjs"),d=require("./bitcoin-CNnPFU6Y.cjs"),N=require("bitcoinjs-lib"),bt=require("buffer"),W=require("./signing-cl-lowxV.cjs");require("@babylonlabs-io/babylon-tbv-rust-wasm");const $=require("./payout-RFIh7GLN.cjs");function $t(t){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(t){for(const r in t)if(r!=="default"){const n=Object.getOwnPropertyDescriptor(t,r);Object.defineProperty(e,r,n.get?n:{enumerable:!0,get:()=>t[r]})}}return e.default=t,Object.freeze(e)}const Ot=$t(N);class xt{constructor(e){f(this,"config");this.config=e}async signPayoutTransaction(e){this.validatePayoutOutputs(e.payoutTxHex,e.registeredPayoutScriptPubKey);const r=await this.config.btcWallet.getPublicKeyHex(),{depositorPubkey:n}=d.validateWalletPubkey(r,e.depositorBtcPubkey),o=await $.buildPayoutPsbt({payoutTxHex:e.payoutTxHex,peginTxHex:e.peginTxHex,assertTxHex:e.assertTxHex,depositorBtcPubkey:n,vaultProviderBtcPubkey:e.vaultProviderBtcPubkey,vaultKeeperBtcPubkeys:e.vaultKeeperBtcPubkeys,universalChallengerBtcPubkeys:e.universalChallengerBtcPubkeys,timelockPegin:e.timelockPegin,network:this.config.network}),c=await this.config.btcWallet.signPsbt(o.psbtHex,W.createTaprootScriptPathSignOptions(r,1));return{signature:$.extractPayoutSignature(c,n),depositorBtcPubkey:n}}getNetwork(){return this.config.network}supportsBatchSigning(){return typeof this.config.btcWallet.signPsbts=="function"}async signPayoutTransactionsBatch(e){if(!this.supportsBatchSigning())throw new Error("Wallet does not support batch signing (signPsbts method not available)");const r=await this.config.btcWallet.getPublicKeyHex(),n=[],o=[],c=[];for(const s of e){this.validatePayoutOutputs(s.payoutTxHex,s.registeredPayoutScriptPubKey);const{depositorPubkey:u}=d.validateWalletPubkey(r,s.depositorBtcPubkey);c.push(u);const h=await $.buildPayoutPsbt({payoutTxHex:s.payoutTxHex,peginTxHex:s.peginTxHex,assertTxHex:s.assertTxHex,depositorBtcPubkey:u,vaultProviderBtcPubkey:s.vaultProviderBtcPubkey,vaultKeeperBtcPubkeys:s.vaultKeeperBtcPubkeys,universalChallengerBtcPubkeys:s.universalChallengerBtcPubkeys,timelockPegin:s.timelockPegin,network:this.config.network});n.push(h.psbtHex),o.push(W.createTaprootScriptPathSignOptions(r,1))}const a=await this.config.btcWallet.signPsbts(n,o);if(a.length!==e.length)throw new Error(`Expected ${e.length} signed PSBTs but received ${a.length}`);const i=[];for(let s=0;s<e.length;s++){const u=c[s],h=$.extractPayoutSignature(a[s],u);i.push({payoutSignature:h,depositorBtcPubkey:u})}return i}validatePayoutOutputs(e,r){if(!d.isValidHex(r))throw new Error("Invalid registeredPayoutScriptPubKey: not valid hex");const n=bt.Buffer.from(d.stripHexPrefix(r),"hex"),o=N.Transaction.fromHex(d.stripHexPrefix(e));if(o.outs.length===0)throw new Error("Payout transaction has no outputs");if(!o.outs.reduce((a,i)=>i.value>a.value?i:a).script.equals(n))throw new Error("Payout transaction does not pay to the registered depositor payout address")}}function Rt(t){return t instanceof Uint8Array||ArrayBuffer.isView(t)&&t.constructor.name==="Uint8Array"}function Z(t,e=""){if(!Number.isSafeInteger(t)||t<0){const r=e&&`"${e}" `;throw new Error(`${r}expected integer >= 0, got ${t}`)}}function z(t,e,r=""){const n=Rt(t),o=t==null?void 0:t.length,c=e!==void 0;if(!n||c&&o!==e){const a=r&&`"${r}" `,i=c?` of length ${e}`:"",s=n?`length=${o}`:`type=${typeof t}`;throw new Error(a+"expected Uint8Array"+i+", got "+s)}return t}function Dt(t){if(typeof t!="function"||typeof t.create!="function")throw new Error("Hash must wrapped by utils.createHasher");Z(t.outputLen),Z(t.blockLen)}function tt(t,e=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&t.finished)throw new Error("Hash#digest() has already been called")}function pt(t,e){z(t,void 0,"digestInto() output");const r=e.outputLen;if(t.length<r)throw new Error('"digestInto() output" expected to be of length >='+r)}function Nt(t){return new Uint32Array(t.buffer,t.byteOffset,Math.floor(t.byteLength/4))}function D(...t){for(let e=0;e<t.length;e++)t[e].fill(0)}function G(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}function S(t,e){return t<<32-e|t>>>e}function Ut(t,e){return t<<e|t>>>32-e>>>0}const Vt=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function Kt(t){return t<<24&4278190080|t<<8&16711680|t>>>8&65280|t>>>24&255}function Ft(t){for(let e=0;e<t.length;e++)t[e]=Kt(t[e]);return t}const Mt=Vt?t=>t:Ft;function Gt(t){if(typeof t!="string")throw new Error("string expected");return new Uint8Array(new TextEncoder().encode(t))}function Wt(t,e=""){return typeof t=="string"?Gt(t):z(t,void 0,e)}function zt(t,e){if(e!==void 0&&{}.toString.call(e)!=="[object Object]")throw new Error("options must be object or undefined");return Object.assign(t,e)}function rt(t,e={}){const r=(o,c)=>t(c).update(o).digest(),n=t(void 0);return r.outputLen=n.outputLen,r.blockLen=n.blockLen,r.create=o=>t(o),Object.assign(r,e),Object.freeze(r)}const gt=t=>({oid:Uint8Array.from([6,9,96,134,72,1,101,3,4,2,t])});function Xt(t,e,r){return t&e^~t&r}function jt(t,e,r){return t&e^t&r^e&r}class nt{constructor(e,r,n,o){f(this,"blockLen");f(this,"outputLen");f(this,"padOffset");f(this,"isLE");f(this,"buffer");f(this,"view");f(this,"finished",!1);f(this,"length",0);f(this,"pos",0);f(this,"destroyed",!1);this.blockLen=e,this.outputLen=r,this.padOffset=n,this.isLE=o,this.buffer=new Uint8Array(e),this.view=G(this.buffer)}update(e){tt(this),z(e);const{view:r,buffer:n,blockLen:o}=this,c=e.length;for(let a=0;a<c;){const i=Math.min(o-this.pos,c-a);if(i===o){const s=G(e);for(;o<=c-a;a+=o)this.process(s,a);continue}n.set(e.subarray(a,a+i),this.pos),this.pos+=i,a+=i,this.pos===o&&(this.process(r,0),this.pos=0)}return this.length+=e.length,this.roundClean(),this}digestInto(e){tt(this),pt(e,this),this.finished=!0;const{buffer:r,view:n,blockLen:o,isLE:c}=this;let{pos:a}=this;r[a++]=128,D(this.buffer.subarray(a)),this.padOffset>o-a&&(this.process(n,0),a=0);for(let l=a;l<o;l++)r[l]=0;n.setBigUint64(o-8,BigInt(this.length*8),c),this.process(n,0);const i=G(e),s=this.outputLen;if(s%4)throw new Error("_sha2: outputLen must be aligned to 32bit");const u=s/4,h=this.get();if(u>h.length)throw new Error("_sha2: outputLen bigger than state");for(let l=0;l<u;l++)i.setUint32(4*l,h[l],c)}digest(){const{buffer:e,outputLen:r}=this;this.digestInto(e);const n=e.slice(0,r);return this.destroy(),n}_cloneInto(e){e||(e=new this.constructor),e.set(...this.get());const{blockLen:r,buffer:n,length:o,finished:c,destroyed:a,pos:i}=this;return e.destroyed=a,e.finished=c,e.length=o,e.pos=i,o%r&&e.buffer.set(n),e}clone(){return this._cloneInto()}}const k=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),P=Uint32Array.from([1779033703,4089235720,3144134277,2227873595,1013904242,4271175723,2773480762,1595750129,1359893119,2917565137,2600822924,725511199,528734635,4215389547,1541459225,327033209]),K=BigInt(2**32-1),at=BigInt(32);function qt(t,e=!1){return e?{h:Number(t&K),l:Number(t>>at&K)}:{h:Number(t>>at&K)|0,l:Number(t&K)|0}}function yt(t,e=!1){const r=t.length;let n=new Uint32Array(r),o=new Uint32Array(r);for(let c=0;c<r;c++){const{h:a,l:i}=qt(t[c],e);[n[c],o[c]]=[a,i]}return[n,o]}const ct=(t,e,r)=>t>>>r,ut=(t,e,r)=>t<<32-r|e>>>r,O=(t,e,r)=>t>>>r|e<<32-r,R=(t,e,r)=>t<<32-r|e>>>r,F=(t,e,r)=>t<<64-r|e>>>r-32,M=(t,e,r)=>t>>>r-32|e<<64-r,Yt=(t,e,r)=>t<<r|e>>>32-r,Qt=(t,e,r)=>e<<r|t>>>32-r,Jt=(t,e,r)=>e<<r-32|t>>>64-r,Zt=(t,e,r)=>t<<r-32|e>>>64-r;function v(t,e,r,n){const o=(e>>>0)+(n>>>0);return{h:t+r+(o/2**32|0)|0,l:o|0}}const te=(t,e,r)=>(t>>>0)+(e>>>0)+(r>>>0),ee=(t,e,r,n)=>e+r+n+(t/2**32|0)|0,re=(t,e,r,n)=>(t>>>0)+(e>>>0)+(r>>>0)+(n>>>0),ne=(t,e,r,n,o)=>e+r+n+o+(t/2**32|0)|0,oe=(t,e,r,n,o)=>(t>>>0)+(e>>>0)+(r>>>0)+(n>>>0)+(o>>>0),se=(t,e,r,n,o,c)=>e+r+n+o+c+(t/2**32|0)|0,ie=Uint32Array.from([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),_=new Uint32Array(64);class ae extends nt{constructor(e){super(64,e,8,!1)}get(){const{A:e,B:r,C:n,D:o,E:c,F:a,G:i,H:s}=this;return[e,r,n,o,c,a,i,s]}set(e,r,n,o,c,a,i,s){this.A=e|0,this.B=r|0,this.C=n|0,this.D=o|0,this.E=c|0,this.F=a|0,this.G=i|0,this.H=s|0}process(e,r){for(let l=0;l<16;l++,r+=4)_[l]=e.getUint32(r,!1);for(let l=16;l<64;l++){const b=_[l-15],p=_[l-2],y=S(b,7)^S(b,18)^b>>>3,g=S(p,17)^S(p,19)^p>>>10;_[l]=g+_[l-7]+y+_[l-16]|0}let{A:n,B:o,C:c,D:a,E:i,F:s,G:u,H:h}=this;for(let l=0;l<64;l++){const b=S(i,6)^S(i,11)^S(i,25),p=h+b+Xt(i,s,u)+ie[l]+_[l]|0,g=(S(n,2)^S(n,13)^S(n,22))+jt(n,o,c)|0;h=u,u=s,s=i,i=a+p|0,a=c,c=o,o=n,n=p+g|0}n=n+this.A|0,o=o+this.B|0,c=c+this.C|0,a=a+this.D|0,i=i+this.E|0,s=s+this.F|0,u=u+this.G|0,h=h+this.H|0,this.set(n,o,c,a,i,s,u,h)}roundClean(){D(_)}destroy(){this.set(0,0,0,0,0,0,0,0),D(this.buffer)}}class ce extends ae{constructor(){super(32);f(this,"A",k[0]|0);f(this,"B",k[1]|0);f(this,"C",k[2]|0);f(this,"D",k[3]|0);f(this,"E",k[4]|0);f(this,"F",k[5]|0);f(this,"G",k[6]|0);f(this,"H",k[7]|0)}}const Pt=yt(["0x428a2f98d728ae22","0x7137449123ef65cd","0xb5c0fbcfec4d3b2f","0xe9b5dba58189dbbc","0x3956c25bf348b538","0x59f111f1b605d019","0x923f82a4af194f9b","0xab1c5ed5da6d8118","0xd807aa98a3030242","0x12835b0145706fbe","0x243185be4ee4b28c","0x550c7dc3d5ffb4e2","0x72be5d74f27b896f","0x80deb1fe3b1696b1","0x9bdc06a725c71235","0xc19bf174cf692694","0xe49b69c19ef14ad2","0xefbe4786384f25e3","0x0fc19dc68b8cd5b5","0x240ca1cc77ac9c65","0x2de92c6f592b0275","0x4a7484aa6ea6e483","0x5cb0a9dcbd41fbd4","0x76f988da831153b5","0x983e5152ee66dfab","0xa831c66d2db43210","0xb00327c898fb213f","0xbf597fc7beef0ee4","0xc6e00bf33da88fc2","0xd5a79147930aa725","0x06ca6351e003826f","0x142929670a0e6e70","0x27b70a8546d22ffc","0x2e1b21385c26c926","0x4d2c6dfc5ac42aed","0x53380d139d95b3df","0x650a73548baf63de","0x766a0abb3c77b2a8","0x81c2c92e47edaee6","0x92722c851482353b","0xa2bfe8a14cf10364","0xa81a664bbc423001","0xc24b8b70d0f89791","0xc76c51a30654be30","0xd192e819d6ef5218","0xd69906245565a910","0xf40e35855771202a","0x106aa07032bbd1b8","0x19a4c116b8d2d0c8","0x1e376c085141ab53","0x2748774cdf8eeb99","0x34b0bcb5e19b48a8","0x391c0cb3c5c95a63","0x4ed8aa4ae3418acb","0x5b9cca4f7763e373","0x682e6ff3d6b2b8a3","0x748f82ee5defb2fc","0x78a5636f43172f60","0x84c87814a1f0ab72","0x8cc702081a6439ec","0x90befffa23631e28","0xa4506cebde82bde9","0xbef9a3f7b2c67915","0xc67178f2e372532b","0xca273eceea26619c","0xd186b8c721c0c207","0xeada7dd6cde0eb1e","0xf57d4f7fee6ed178","0x06f067aa72176fba","0x0a637dc5a2c898a6","0x113f9804bef90dae","0x1b710b35131c471b","0x28db77f523047d84","0x32caab7b40c72493","0x3c9ebe0a15c9bebc","0x431d67c49c100d4c","0x4cc5d4becb3e42b6","0x597f299cfc657e2a","0x5fcb6fab3ad6faec","0x6c44198c4a475817"].map(t=>BigInt(t))),ue=Pt[0],le=Pt[1],I=new Uint32Array(80),C=new Uint32Array(80);class fe extends nt{constructor(e){super(128,e,16,!1)}get(){const{Ah:e,Al:r,Bh:n,Bl:o,Ch:c,Cl:a,Dh:i,Dl:s,Eh:u,El:h,Fh:l,Fl:b,Gh:p,Gl:y,Hh:g,Hl:w}=this;return[e,r,n,o,c,a,i,s,u,h,l,b,p,y,g,w]}set(e,r,n,o,c,a,i,s,u,h,l,b,p,y,g,w){this.Ah=e|0,this.Al=r|0,this.Bh=n|0,this.Bl=o|0,this.Ch=c|0,this.Cl=a|0,this.Dh=i|0,this.Dl=s|0,this.Eh=u|0,this.El=h|0,this.Fh=l|0,this.Fl=b|0,this.Gh=p|0,this.Gl=y|0,this.Hh=g|0,this.Hl=w|0}process(e,r){for(let x=0;x<16;x++,r+=4)I[x]=e.getUint32(r),C[x]=e.getUint32(r+=4);for(let x=16;x<80;x++){const H=I[x-15]|0,B=C[x-15]|0,X=O(H,B,1)^O(H,B,8)^ct(H,B,7),j=R(H,B,1)^R(H,B,8)^ut(H,B,7),T=I[x-2]|0,E=C[x-2]|0,U=O(T,E,19)^F(T,E,61)^ct(T,E,6),q=R(T,E,19)^M(T,E,61)^ut(T,E,6),V=re(j,q,C[x-7],C[x-16]),Y=ne(V,X,U,I[x-7],I[x-16]);I[x]=Y|0,C[x]=V|0}let{Ah:n,Al:o,Bh:c,Bl:a,Ch:i,Cl:s,Dh:u,Dl:h,Eh:l,El:b,Fh:p,Fl:y,Gh:g,Gl:w,Hh:A,Hl:L}=this;for(let x=0;x<80;x++){const H=O(l,b,14)^O(l,b,18)^F(l,b,41),B=R(l,b,14)^R(l,b,18)^M(l,b,41),X=l&p^~l&g,j=b&y^~b&w,T=oe(L,B,j,le[x],C[x]),E=se(T,A,H,X,ue[x],I[x]),U=T|0,q=O(n,o,28)^F(n,o,34)^F(n,o,39),V=R(n,o,28)^M(n,o,34)^M(n,o,39),Y=n&c^n&i^c&i,It=o&a^o&s^a&s;A=g|0,L=w|0,g=p|0,w=y|0,p=l|0,y=b|0,{h:l,l:b}=v(u|0,h|0,E|0,U|0),u=i|0,h=s|0,i=c|0,s=a|0,c=n|0,a=o|0;const it=te(U,V,It);n=ee(it,E,q,Y),o=it|0}({h:n,l:o}=v(this.Ah|0,this.Al|0,n|0,o|0)),{h:c,l:a}=v(this.Bh|0,this.Bl|0,c|0,a|0),{h:i,l:s}=v(this.Ch|0,this.Cl|0,i|0,s|0),{h:u,l:h}=v(this.Dh|0,this.Dl|0,u|0,h|0),{h:l,l:b}=v(this.Eh|0,this.El|0,l|0,b|0),{h:p,l:y}=v(this.Fh|0,this.Fl|0,p|0,y|0),{h:g,l:w}=v(this.Gh|0,this.Gl|0,g|0,w|0),{h:A,l:L}=v(this.Hh|0,this.Hl|0,A|0,L|0),this.set(n,o,c,a,i,s,u,h,l,b,p,y,g,w,A,L)}roundClean(){D(I,C)}destroy(){D(this.buffer),this.set(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)}}class he extends fe{constructor(){super(64);f(this,"Ah",P[0]|0);f(this,"Al",P[1]|0);f(this,"Bh",P[2]|0);f(this,"Bl",P[3]|0);f(this,"Ch",P[4]|0);f(this,"Cl",P[5]|0);f(this,"Dh",P[6]|0);f(this,"Dl",P[7]|0);f(this,"Eh",P[8]|0);f(this,"El",P[9]|0);f(this,"Fh",P[10]|0);f(this,"Fl",P[11]|0);f(this,"Gh",P[12]|0);f(this,"Gl",P[13]|0);f(this,"Hh",P[14]|0);f(this,"Hl",P[15]|0)}}const wt=rt(()=>new ce,gt(1)),de=rt(()=>new he,gt(3)),lt=66;function mt(t){if(!t.startsWith("0x")&&!t.startsWith("0X"))throw new Error("Expected 0x-prefixed hex string");const e=t.slice(2);if(e.length%2!==0)throw new Error(`Hex string has odd length: ${e.length}`);if(!/^[0-9a-fA-F]*$/.test(e))throw new Error("Hex string contains non-hex characters");const r=new Uint8Array(e.length/2);for(let n=0;n<r.length;n++)r[n]=parseInt(e.slice(n*2,n*2+2),16);return r}function be(t){return`0x${Array.from(t).map(e=>e.toString(16).padStart(2,"0")).join("")}`}function et(t,e){if(t.length!==lt)throw new Error(`${e} must be exactly 32 bytes (${lt} hex chars with 0x prefix), got ${t.length}`)}function St(t){et(t,"Secret");const e=mt(t),r=wt(e);return be(r)}function Tt(t,e){return et(t,"Secret"),et(e,"Hashlock"),mt(e),St(t).toLowerCase()===e.toLowerCase()}const xe=/^0x[0-9a-fA-F]{64}$/,pe=/^0x[0-9a-fA-F]{40}$/,ge=/^0x([0-9a-fA-F]{2})*$/;function Q(t,e){if(t.length!==66)throw new Error(`${e} must be 32 bytes (66 hex chars with 0x prefix), got length ${t.length}`);if(!xe.test(t))throw new Error(`${e} must contain only hex characters after the 0x prefix`)}function ye(t,e){if(!pe.test(t))throw new Error(`${e} must be a 20-byte 0x-prefixed hex address (42 chars)`)}function Pe(t,e){if(!ge.test(t))throw new Error(`${e} must be a 0x-prefixed hex string with an even number of hex chars`)}async function we(t){const{btcVaultRegistryAddress:e,vaultId:r,hashlock:n,activationMetadata:o,writeContract:c,signal:a}=t;a==null||a.throwIfAborted(),ye(e,"btcVaultRegistryAddress"),Q(r,"vaultId");const i=d.ensureHexPrefix(t.secret);if(Q(i,"secret"),n!==void 0&&(Q(n,"hashlock"),!Tt(i,n)))throw new Error("Invalid secret: SHA256(secret) does not match the provided hashlock");return Pe(o,"activationMetadata"),c({address:e,abi:m.BTCVaultRegistryABI,functionName:"activateVaultWithSecret",args:[r,i,o]})}const me=1e4;async function ot(t){const{statusReader:e,peginTxid:r,targetStatuses:n,timeoutMs:o,pollIntervalMs:c=me,signal:a}=t,i=Date.now();for(;;){if(a!=null&&a.aborted)throw new Error(`Polling aborted for pegin ${r.slice(0,8)}… (target: ${[...n].join(", ")})`);if(Date.now()-i>=o)throw new Error(`Polling timeout after ${o}ms for pegin ${r.slice(0,8)}… (target: ${[...n].join(", ")})`);try{const u=(await e.getPeginStatus({pegin_txid:r},a)).status;if(n.has(u))return u;if(m.VP_TERMINAL_STATUSES.has(u)&&!n.has(u))throw new Error(`Pegin ${r.slice(0,8)}… reached terminal status "${u}" while waiting for ${[...n].join(", ")}`)}catch(s){if(!(s instanceof m.JsonRpcError&&s.code===m.RpcErrorCode.NOT_FOUND||s instanceof Error&&s.message.includes("PegIn not found")))throw s}await new Promise((s,u)=>{const h=()=>{clearTimeout(l),u(new Error(`Polling aborted for pegin ${r.slice(0,8)}… (target: ${[...n].join(", ")})`))},l=setTimeout(()=>{a==null||a.removeEventListener("abort",h),s()},c);a==null||a.addEventListener("abort",h,{once:!0})})}}const Se=300*1e3,Te=new Set([m.DaemonStatus.PENDING_DEPOSITOR_WOTS_PK,...m.POST_WOTS_STATUSES]);async function Ee(t){const{statusReader:e,wotsSubmitter:r,peginTxid:n,depositorPk:o,wotsPublicKeys:c,timeoutMs:a=Se,signal:i}=t;i==null||i.throwIfAborted();const s=await ot({statusReader:e,peginTxid:n,targetStatuses:Te,timeoutMs:a,signal:i});m.POST_WOTS_STATUSES.has(s)||(i==null||i.throwIfAborted(),await r.submitDepositorWotsKey({pegin_txid:n,depositor_pk:o,wots_public_keys:c},i))}const ve=1;function Ae(t,e,r){const n=N.Psbt.fromBase64(t),o=n.data.getTransaction().toString("hex").toLowerCase(),c=d.stripHexPrefix(e).toLowerCase();if(o!==c)throw new Error(`PSBT integrity check failed for ${r}: unsigned transaction does not match tx_hex`);return n}function He(t){const e=N.Psbt.fromHex(t.toHex());for(const r of e.data.inputs)delete r.tapBip32Derivation,delete r.tapMerkleRoot;return e}function ft(t,e,r){if(!t)throw new Error(`Missing ${r} PSBT`);const n=Ae(t,e,r);return He(n).toHex()}function Be(t,e){const r=[],n=[],o=[],c=W.createTaprootScriptPathSignOptions(e,ve),a=ft(t.payout_psbt,t.payout_tx.tx_hex,"depositor payout");r.push(a),n.push(c);for(const i of t.challenger_presign_data){const s=d.stripHexPrefix(i.challenger_pubkey),u=r.length,h=ft(i.nopayout_psbt,i.nopayout_tx.tx_hex,`nopayout (challenger ${s})`);r.push(h),n.push(c),o.push({challengerPubkey:s,noPayoutIdx:u})}return{psbtHexes:r,signOptions:n,challengerEntries:o}}function ke(t,e,r){const n=$.extractPayoutSignature(t[0],r),o={};for(const c of e)o[c.challengerPubkey]={nopayout_signature:$.extractPayoutSignature(t[c.noPayoutIdx],r)};return{payout_signatures:{payout_signature:n},per_challenger:o}}async function _e(t,e,r){if(typeof t.signPsbts=="function")return t.signPsbts(e,r);const n=[];for(let o=0;o<e.length;o++)n.push(await t.signPsbt(e[o],r==null?void 0:r[o]));return n}async function Et(t){const{depositorGraph:e,depositorBtcPubkey:r,btcWallet:n}=t,o=d.stripHexPrefix(r),c=await n.getPublicKeyHex(),{psbtHexes:a,signOptions:i,challengerEntries:s}=Be(e,c),u=await _e(n,a,i);if(u.length!==a.length)throw new Error(`Wallet returned ${u.length} signed PSBTs, expected ${a.length}`);return ke(u,s,o)}const Ie=1200*1e3,vt=new Set([m.DaemonStatus.PENDING_ACKS,m.DaemonStatus.PENDING_ACTIVATION,m.DaemonStatus.ACTIVATED]),Ce=new Set([m.DaemonStatus.PENDING_DEPOSITOR_SIGNATURES,...vt]);function Le(t){return t.map(e=>({claimerPubkeyXOnly:d.processPublicKeyToXOnly(e.claimer_pubkey),payoutTxHex:e.payout_tx.tx_hex,assertTxHex:e.assert_tx.tx_hex}))}function $e(t){const{output:e}=Ot.payments.p2tr({internalPubkey:bt.Buffer.from(t,"hex")});if(!e)throw new Error("Failed to derive BIP-86 P2TR scriptPubKey");return e.toString("hex")}function Oe(t,e){const r=d.stripHexPrefix(t).toLowerCase(),n=d.stripHexPrefix(e.vaultProviderBtcPubkey).toLowerCase(),o=d.stripHexPrefix(e.depositorBtcPubkey).toLowerCase();if(r===n||r===o)return e.registeredPayoutScriptPubKey;if(!e.vaultKeeperBtcPubkeys.some(i=>d.stripHexPrefix(i).toLowerCase()===r))throw new Error(`Unknown claimer pubkey ${r}: not VP, depositor, or a registered vault keeper`);return`0x${$e(r)}`}function ht(t,e){return{payoutTxHex:t.payoutTxHex,peginTxHex:e.peginTxHex,assertTxHex:t.assertTxHex,vaultProviderBtcPubkey:e.vaultProviderBtcPubkey,vaultKeeperBtcPubkeys:e.vaultKeeperBtcPubkeys,universalChallengerBtcPubkeys:e.universalChallengerBtcPubkeys,depositorBtcPubkey:e.depositorBtcPubkey,timelockPegin:e.timelockPegin,registeredPayoutScriptPubKey:Oe(t.claimerPubkeyXOnly,e)}}async function Re(t,e,r,n){const o=new xt({network:e.network,btcWallet:t}),c=r.length;n==null||n(0,c);let a;if(o.supportsBatchSigning())a=(await o.signPayoutTransactionsBatch(r.map(u=>ht(u,e)))).map(u=>u.payoutSignature);else{a=[];for(let s=0;s<r.length;s++){n==null||n(s,c);const u=await o.signPayoutTransaction(ht(r[s],e));a.push(u.signature)}}const i={};for(let s=0;s<r.length;s++)i[r[s].claimerPubkeyXOnly]={payout_signature:a[s]};return n==null||n(c,c),i}async function De(t){const{statusReader:e,presignClient:r,btcWallet:n,peginTxid:o,depositorPk:c,signingContext:a,timeoutMs:i=Ie,signal:s,onProgress:u}=t,h=await ot({statusReader:e,peginTxid:o,targetStatuses:Ce,timeoutMs:i,signal:s});if(vt.has(h))return;s==null||s.throwIfAborted();const l=await r.requestDepositorPresignTransactions({pegin_txid:o,depositor_pk:c},s);s==null||s.throwIfAborted();const b=d.processPublicKeyToXOnly(c),p=l.txs.filter(L=>d.processPublicKeyToXOnly(L.claimer_pubkey)!==b),y=Le(p),g=await Re(n,a,y,u);s==null||s.throwIfAborted();const w=await Et({depositorGraph:l.depositor_graph,depositorBtcPubkey:c,btcWallet:n});s==null||s.throwIfAborted();const A={...g};A[d.stripHexPrefix(c)]=w.payout_signatures,await r.submitDepositorPresignatures({pegin_txid:o,depositor_pk:c,signatures:A,depositor_claimer_presignatures:w},s)}function Ne(t){return/^[0-9a-fA-F]{64}$/.test(t)}function Ue(t){const{amountSats:e,minDeposit:r,maxDeposit:n,btcBalance:o,estimatedFeeSats:c,depositorClaimValue:a}=t;return!(e<=0n||e<r||n&&n>0n&&e>n||c==null||a==null||e+c+a>o)}function Ve(t,e,r){return t<=0n?{valid:!1,error:"Deposit amount must be greater than zero"}:t<e?{valid:!1,error:`Minimum deposit is ${d.formatSatoshisToBtc(e)} BTC`}:r&&r>0n&&t>r?{valid:!1,error:`Maximum deposit is ${d.formatSatoshisToBtc(r)} BTC`}:{valid:!0}}function Ke(t){const{amount:e,effectiveRemaining:r}=t;return r===null?{valid:!0}:r===0n?{valid:!1,error:"Supply cap reached — deposits temporarily paused"}:e>r?{valid:!1,error:`Vault size exceeds remaining capacity (${d.formatSatoshisToBtc(r)} BTC)`}:{valid:!0}}function Fe(t,e){if(!t||t.length===0)return{valid:!1,error:"At least one vault provider must be selected"};const r=e.map(o=>o.toLowerCase());return t.filter(o=>!r.includes(o.toLowerCase())).length>0?{valid:!1,error:"Invalid vault provider selected"}:{valid:!0}}function At(t,e,r){if(!t||t.length===0)return{valid:!1,error:"At least one vault amount required"};for(let n=0;n<t.length;n++){const o=t[n];if(o<=0n)return{valid:!1,error:`Vault ${n+1} amount must be positive`};if(e&&o<e)return{valid:!1,error:`Vault ${n+1} amount ${d.formatSatoshisToBtc(o)} BTC is below minimum deposit ${d.formatSatoshisToBtc(e)} BTC`};if(r&&o>r)return{valid:!1,error:`Vault ${n+1} amount ${d.formatSatoshisToBtc(o)} BTC exceeds maximum deposit ${d.formatSatoshisToBtc(r)} BTC`}}return{valid:!0}}function Ht(t){const e=d.stripHexPrefix(t);return Ne(e)?{valid:!0}:{valid:!1,error:"Invalid pubkey format: must be 64 hex characters (32-byte x-only public key, no 0x prefix)"}}function Me(t){if(!t||t.length===0)throw new Error("No vault keepers available. The system requires at least one vault keeper to create a deposit.")}function Ge(t){if(!t||t.length===0)throw new Error("No universal challengers available. The system requires at least one universal challenger to create a deposit.")}function We(t){if(t.length===0)throw new Error("No spendable UTXOs available")}function ze(t){const{vaultAmounts:e,confirmedUTXOs:r,vaultProviderBtcPubkey:n,vaultKeeperBtcPubkeys:o,universalChallengerBtcPubkeys:c,minDeposit:a,maxDeposit:i,htlcSecretHexesLength:s,depositorSecretHashesLength:u}=t,h=e.length;if(s!==h)throw new Error(`htlcSecretHexes length (${s}) must match vaultAmounts length (${h})`);if(u!==h)throw new Error(`depositorSecretHashes length (${u}) must match vaultAmounts length (${h})`);const l=At(e,a,i);if(!l.valid)throw new Error(l.error);const b=Ht(n);if(!b.valid)throw new Error(b.error);Me(o),Ge(c),We(r)}var st=(t=>(t.CLAIM_EVENT_RECEIVED="ClaimEventReceived",t.CLAIM_BROADCAST="ClaimBroadcast",t.ASSERT_BROADCAST="AssertBroadcast",t.CHALLENGE_ASSERT_OBSERVED="ChallengeAssertObserved",t.WRONGLY_CHALLENGED_BROADCAST="WronglyChallengedBroadcast",t.PAYOUT_BROADCAST="PayoutBroadcast",t.FAILED="Failed",t))(st||{});const Xe=new Set(["PayoutBroadcast","Failed"]);function je(t){return Object.values(st).includes(t)}function qe(t){return!!t&&Xe.has(t)}class Bt extends Error{constructor(r,n){super(`Refund not yet mature (BIP68 not final): ${n.message}`);f(this,"vaultId");f(this,"cause");this.name="BIP68NotMatureError",this.vaultId=r,this.cause=n}}const Ye=/^0x[0-9a-fA-F]{64}$/,Qe=/^(?:0x)?(?:[0-9a-fA-F]{2})+$/,kt=/^(?:0x)?(?:[0-9a-fA-F]{64}|[0-9a-fA-F]{66})$/,Je=160,Ze=1,dt=65535,tr=/non-BIP68-final/i;function _t(t,e){if(t.length!==66)throw new Error(`${e} must be 32 bytes (66 hex chars with 0x prefix), got length ${t.length}`);if(!Ye.test(t))throw new Error(`${e} must contain only hex characters after the 0x prefix`)}function J(t,e){if(!Number.isInteger(t)||t<0)throw new Error(`${e} must be a non-negative integer, got ${t}`)}function er(t){if(_t(t.hashlock,"hashlock"),!Number.isInteger(t.htlcVout)||t.htlcVout<0||t.htlcVout>dt)throw new Error(`htlcVout must be an integer 0-${dt}, got ${t.htlcVout}`);if(J(t.offchainParamsVersion,"offchainParamsVersion"),J(t.appVaultKeepersVersion,"appVaultKeepersVersion"),J(t.universalChallengersVersion,"universalChallengersVersion"),typeof t.unsignedPrePeginTxHex!="string"||t.unsignedPrePeginTxHex.length===0)throw new Error("unsignedPrePeginTxHex must be a non-empty hex string");if(!Qe.test(t.unsignedPrePeginTxHex))throw new Error("unsignedPrePeginTxHex must be a hex byte string (optional 0x prefix, even length)");if(!t.depositorBtcPubkey||!kt.test(t.depositorBtcPubkey))throw new Error("depositorBtcPubkey must be 32 or 33 bytes of hex (optional 0x prefix)");if(typeof t.amount!="bigint"||t.amount<=0n)throw new Error(`amount must be a positive bigint, got ${t.amount}`)}function rr(t){if(!t.vaultProviderPubkey||!kt.test(t.vaultProviderPubkey))throw new Error("vaultProviderPubkey must be 32 or 33 bytes of hex");if(t.vaultKeeperPubkeys.length===0)throw new Error("vaultKeeperPubkeys must be non-empty");if(t.universalChallengerPubkeys.length===0)throw new Error("universalChallengerPubkeys must be non-empty");if(!Number.isInteger(t.timelockRefund)||t.timelockRefund<=0)throw new Error(`timelockRefund must be a positive integer, got ${t.timelockRefund}`);if(typeof t.feeRate!="bigint"||t.feeRate<=0n)throw new Error(`protocol feeRate must be a positive bigint, got ${t.feeRate}`);if(!Number.isInteger(t.numLocalChallengers)||t.numLocalChallengers<0)throw new Error("numLocalChallengers must be a non-negative integer");if(!Number.isInteger(t.councilQuorum)||!Number.isInteger(t.councilSize)||t.councilQuorum<=0||t.councilSize<=0||t.councilQuorum>t.councilSize)throw new Error(`councilQuorum (${t.councilQuorum}) must be in [1, councilSize=${t.councilSize}]`)}function nr(t){const e=N.Psbt.fromHex(t);try{e.finalizeAllInputs()}catch(r){const n=r instanceof Error?r.message:String(r);if(!n.includes("already finalized"))throw new Error(`Failed to finalize refund PSBT: ${n}`)}return e.extractTransaction().toHex()}async function or(t){const{vaultId:e,readVault:r,readPrePeginContext:n,feeRate:o,signPsbt:c,broadcastTx:a,signal:i}=t;i==null||i.throwIfAborted(),_t(e,"vaultId");const s=await r();er(s),i==null||i.throwIfAborted();const u=await n(s);if(rr(u),i==null||i.throwIfAborted(),!Number.isFinite(o)||o<=0)throw new Error(`feeRate must be a positive number, got ${o}`);const h=BigInt(Math.ceil(o*Je));i==null||i.throwIfAborted();const{psbtHex:l}=await $.buildRefundPsbt({prePeginParams:{depositorPubkey:d.stripHexPrefix(s.depositorBtcPubkey),vaultProviderPubkey:d.stripHexPrefix(u.vaultProviderPubkey),vaultKeeperPubkeys:u.vaultKeeperPubkeys.map(d.stripHexPrefix),universalChallengerPubkeys:u.universalChallengerPubkeys.map(d.stripHexPrefix),hashlocks:[d.stripHexPrefix(s.hashlock)],timelockRefund:u.timelockRefund,pegInAmounts:[s.amount],feeRate:u.feeRate,numLocalChallengers:u.numLocalChallengers,councilQuorum:u.councilQuorum,councilSize:u.councilSize,network:u.network},fundedPrePeginTxHex:d.stripHexPrefix(s.unsignedPrePeginTxHex),htlcVout:s.htlcVout,refundFee:h,hashlock:d.stripHexPrefix(s.hashlock)});i==null||i.throwIfAborted();const b=W.createTaprootScriptPathSignOptions(s.depositorBtcPubkey,Ze),p=await c(l,b),y=nr(p);i==null||i.throwIfAborted();try{return await a(y)}catch(g){throw g instanceof Error&&tr.test(g.message)?new Bt(e,g):g}}exports.BIP68NotMatureError=Bt;exports.ClaimerPegoutStatusValue=st;exports.HashMD=nt;exports.PayoutManager=xt;exports.abytes=z;exports.activateVault=we;exports.aexists=tt;exports.ahash=Dt;exports.anumber=Z;exports.aoutput=pt;exports.buildAndBroadcastRefund=or;exports.checkOpts=zt;exports.clean=D;exports.computeHashlock=St;exports.createHasher=rt;exports.createView=G;exports.isDepositAmountValid=Ue;exports.isPegoutTerminalStatus=qe;exports.isRecognizedPegoutStatus=je;exports.kdfInputToBytes=Wt;exports.pollAndSignPayouts=De;exports.rotl=Ut;exports.rotlBH=Jt;exports.rotlBL=Zt;exports.rotlSH=Yt;exports.rotlSL=Qt;exports.sha256=wt;exports.sha512=de;exports.signDepositorGraph=Et;exports.split=yt;exports.submitWotsPublicKey=Ee;exports.swap32IfBE=Mt;exports.u32=Nt;exports.validateDepositAmount=Ve;exports.validateMultiVaultDepositInputs=ze;exports.validateProviderSelection=Fe;exports.validateRemainingCapacity=Ke;exports.validateSecretAgainstHashlock=Tt;exports.validateVaultAmounts=At;exports.validateVaultProviderPubkey=Ht;exports.waitForPeginStatus=ot;
2
+ //# sourceMappingURL=buildAndBroadcastRefund-DWEQvj9T.cjs.map