@lendasat/lendaswap-sdk-pure 0.2.21-preview.0 → 0.2.21

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 (205) hide show
  1. package/dist/api/client.d.ts.map +1 -1
  2. package/dist/api/client.js +4 -1
  3. package/dist/api/client.js.map +1 -1
  4. package/dist/version.d.ts +4 -0
  5. package/dist/version.d.ts.map +1 -0
  6. package/dist/version.js +6 -0
  7. package/dist/version.js.map +1 -0
  8. package/package.json +4 -1
  9. package/dist/price-feed.d.ts +0 -124
  10. package/dist/price-feed.d.ts.map +0 -1
  11. package/dist/price-feed.js +0 -178
  12. package/dist/price-feed.js.map +0 -1
  13. package/dist/src/api/client.d.ts +0 -31
  14. package/dist/src/api/client.d.ts.map +0 -1
  15. package/dist/src/api/client.js +0 -12
  16. package/dist/src/api/client.js.map +0 -1
  17. package/dist/src/arkade.d.ts +0 -36
  18. package/dist/src/arkade.d.ts.map +0 -1
  19. package/dist/src/arkade.js +0 -68
  20. package/dist/src/arkade.js.map +0 -1
  21. package/dist/src/client.d.ts +0 -760
  22. package/dist/src/client.d.ts.map +0 -1
  23. package/dist/src/client.js +0 -2169
  24. package/dist/src/client.js.map +0 -1
  25. package/dist/src/create/arkade.d.ts +0 -34
  26. package/dist/src/create/arkade.d.ts.map +0 -1
  27. package/dist/src/create/arkade.js +0 -76
  28. package/dist/src/create/arkade.js.map +0 -1
  29. package/dist/src/create/bitcoin-to-arkade.d.ts +0 -36
  30. package/dist/src/create/bitcoin-to-arkade.d.ts.map +0 -1
  31. package/dist/src/create/bitcoin-to-arkade.js +0 -69
  32. package/dist/src/create/bitcoin-to-arkade.js.map +0 -1
  33. package/dist/src/create/bitcoin.d.ts +0 -31
  34. package/dist/src/create/bitcoin.d.ts.map +0 -1
  35. package/dist/src/create/bitcoin.js +0 -67
  36. package/dist/src/create/bitcoin.js.map +0 -1
  37. package/dist/src/create/evm-to-arkade.d.ts +0 -34
  38. package/dist/src/create/evm-to-arkade.d.ts.map +0 -1
  39. package/dist/src/create/evm-to-arkade.js +0 -69
  40. package/dist/src/create/evm-to-arkade.js.map +0 -1
  41. package/dist/src/create/evm-to-bitcoin.d.ts +0 -35
  42. package/dist/src/create/evm-to-bitcoin.d.ts.map +0 -1
  43. package/dist/src/create/evm-to-bitcoin.js +0 -71
  44. package/dist/src/create/evm-to-bitcoin.js.map +0 -1
  45. package/dist/src/create/evm-to-lightning.d.ts +0 -34
  46. package/dist/src/create/evm-to-lightning.d.ts.map +0 -1
  47. package/dist/src/create/evm-to-lightning.js +0 -66
  48. package/dist/src/create/evm-to-lightning.js.map +0 -1
  49. package/dist/src/create/index.d.ts +0 -19
  50. package/dist/src/create/index.d.ts.map +0 -1
  51. package/dist/src/create/index.js +0 -18
  52. package/dist/src/create/index.js.map +0 -1
  53. package/dist/src/create/lightning.d.ts +0 -31
  54. package/dist/src/create/lightning.d.ts.map +0 -1
  55. package/dist/src/create/lightning.js +0 -72
  56. package/dist/src/create/lightning.js.map +0 -1
  57. package/dist/src/create/types.d.ts +0 -247
  58. package/dist/src/create/types.d.ts.map +0 -1
  59. package/dist/src/create/types.js +0 -5
  60. package/dist/src/create/types.js.map +0 -1
  61. package/dist/src/delegate.d.ts +0 -62
  62. package/dist/src/delegate.d.ts.map +0 -1
  63. package/dist/src/delegate.js +0 -284
  64. package/dist/src/delegate.js.map +0 -1
  65. package/dist/src/esplora.d.ts +0 -41
  66. package/dist/src/esplora.d.ts.map +0 -1
  67. package/dist/src/esplora.js +0 -47
  68. package/dist/src/esplora.js.map +0 -1
  69. package/dist/src/evm/coordinator.d.ts +0 -247
  70. package/dist/src/evm/coordinator.d.ts.map +0 -1
  71. package/dist/src/evm/coordinator.js +0 -414
  72. package/dist/src/evm/coordinator.js.map +0 -1
  73. package/dist/src/evm/htlc.d.ts +0 -238
  74. package/dist/src/evm/htlc.d.ts.map +0 -1
  75. package/dist/src/evm/htlc.js +0 -278
  76. package/dist/src/evm/htlc.js.map +0 -1
  77. package/dist/src/evm/index.d.ts +0 -9
  78. package/dist/src/evm/index.d.ts.map +0 -1
  79. package/dist/src/evm/index.js +0 -9
  80. package/dist/src/evm/index.js.map +0 -1
  81. package/dist/src/evm/signing.d.ts +0 -30
  82. package/dist/src/evm/signing.d.ts.map +0 -1
  83. package/dist/src/evm/signing.js +0 -91
  84. package/dist/src/evm/signing.js.map +0 -1
  85. package/dist/src/generated/api.d.ts +0 -2736
  86. package/dist/src/generated/api.d.ts.map +0 -1
  87. package/dist/src/generated/api.js +0 -6
  88. package/dist/src/generated/api.js.map +0 -1
  89. package/dist/src/index.d.ts +0 -18
  90. package/dist/src/index.d.ts.map +0 -1
  91. package/dist/src/index.js +0 -24
  92. package/dist/src/index.js.map +0 -1
  93. package/dist/src/node.d.ts +0 -19
  94. package/dist/src/node.d.ts.map +0 -1
  95. package/dist/src/node.js +0 -19
  96. package/dist/src/node.js.map +0 -1
  97. package/dist/src/price-calculations.d.ts +0 -109
  98. package/dist/src/price-calculations.d.ts.map +0 -1
  99. package/dist/src/price-calculations.js +0 -135
  100. package/dist/src/price-calculations.js.map +0 -1
  101. package/dist/src/redeem/arkade.d.ts +0 -65
  102. package/dist/src/redeem/arkade.d.ts.map +0 -1
  103. package/dist/src/redeem/arkade.js +0 -217
  104. package/dist/src/redeem/arkade.js.map +0 -1
  105. package/dist/src/redeem/ethereum.d.ts +0 -52
  106. package/dist/src/redeem/ethereum.d.ts.map +0 -1
  107. package/dist/src/redeem/ethereum.js +0 -206
  108. package/dist/src/redeem/ethereum.js.map +0 -1
  109. package/dist/src/redeem/gasless.d.ts +0 -41
  110. package/dist/src/redeem/gasless.d.ts.map +0 -1
  111. package/dist/src/redeem/gasless.js +0 -71
  112. package/dist/src/redeem/gasless.js.map +0 -1
  113. package/dist/src/redeem/index.d.ts +0 -49
  114. package/dist/src/redeem/index.d.ts.map +0 -1
  115. package/dist/src/redeem/index.js +0 -189
  116. package/dist/src/redeem/index.js.map +0 -1
  117. package/dist/src/redeem/types.d.ts +0 -126
  118. package/dist/src/redeem/types.d.ts.map +0 -1
  119. package/dist/src/redeem/types.js +0 -36
  120. package/dist/src/redeem/types.js.map +0 -1
  121. package/dist/src/refund/arkade.d.ts +0 -62
  122. package/dist/src/refund/arkade.d.ts.map +0 -1
  123. package/dist/src/refund/arkade.js +0 -212
  124. package/dist/src/refund/arkade.js.map +0 -1
  125. package/dist/src/refund/index.d.ts +0 -10
  126. package/dist/src/refund/index.d.ts.map +0 -1
  127. package/dist/src/refund/index.js +0 -10
  128. package/dist/src/refund/index.js.map +0 -1
  129. package/dist/src/refund/onchain.d.ts +0 -137
  130. package/dist/src/refund/onchain.d.ts.map +0 -1
  131. package/dist/src/refund/onchain.js +0 -366
  132. package/dist/src/refund/onchain.js.map +0 -1
  133. package/dist/src/signer/index.d.ts +0 -106
  134. package/dist/src/signer/index.d.ts.map +0 -1
  135. package/dist/src/signer/index.js +0 -179
  136. package/dist/src/signer/index.js.map +0 -1
  137. package/dist/src/storage/idb.d.ts +0 -70
  138. package/dist/src/storage/idb.d.ts.map +0 -1
  139. package/dist/src/storage/idb.js +0 -236
  140. package/dist/src/storage/idb.js.map +0 -1
  141. package/dist/src/storage/index.d.ts +0 -152
  142. package/dist/src/storage/index.d.ts.map +0 -1
  143. package/dist/src/storage/index.js +0 -98
  144. package/dist/src/storage/index.js.map +0 -1
  145. package/dist/src/storage/sqlite.d.ts +0 -95
  146. package/dist/src/storage/sqlite.d.ts.map +0 -1
  147. package/dist/src/storage/sqlite.js +0 -206
  148. package/dist/src/storage/sqlite.js.map +0 -1
  149. package/dist/src/storage/types.d.ts +0 -57
  150. package/dist/src/storage/types.d.ts.map +0 -1
  151. package/dist/src/storage/types.js +0 -9
  152. package/dist/src/storage/types.js.map +0 -1
  153. package/dist/src/tokens.d.ts +0 -29
  154. package/dist/src/tokens.d.ts.map +0 -1
  155. package/dist/src/tokens.js +0 -89
  156. package/dist/src/tokens.js.map +0 -1
  157. package/dist/src/usd-price.d.ts +0 -34
  158. package/dist/src/usd-price.d.ts.map +0 -1
  159. package/dist/src/usd-price.js +0 -83
  160. package/dist/src/usd-price.js.map +0 -1
  161. package/dist/tests/api-client.test.d.ts +0 -2
  162. package/dist/tests/api-client.test.d.ts.map +0 -1
  163. package/dist/tests/api-client.test.js +0 -86
  164. package/dist/tests/api-client.test.js.map +0 -1
  165. package/dist/tests/client.test.d.ts +0 -2
  166. package/dist/tests/client.test.d.ts.map +0 -1
  167. package/dist/tests/client.test.js +0 -150
  168. package/dist/tests/client.test.js.map +0 -1
  169. package/dist/tests/index.test.d.ts +0 -2
  170. package/dist/tests/index.test.d.ts.map +0 -1
  171. package/dist/tests/index.test.js +0 -8
  172. package/dist/tests/index.test.js.map +0 -1
  173. package/dist/tests/onchain-refund.test.d.ts +0 -2
  174. package/dist/tests/onchain-refund.test.d.ts.map +0 -1
  175. package/dist/tests/onchain-refund.test.js +0 -279
  176. package/dist/tests/onchain-refund.test.js.map +0 -1
  177. package/dist/tests/signer.test.d.ts +0 -2
  178. package/dist/tests/signer.test.d.ts.map +0 -1
  179. package/dist/tests/signer.test.js +0 -92
  180. package/dist/tests/signer.test.js.map +0 -1
  181. package/dist/tests/sqlite-storage.test.d.ts +0 -2
  182. package/dist/tests/sqlite-storage.test.d.ts.map +0 -1
  183. package/dist/tests/sqlite-storage.test.js +0 -160
  184. package/dist/tests/sqlite-storage.test.js.map +0 -1
  185. package/dist/tests/storage.test.d.ts +0 -2
  186. package/dist/tests/storage.test.d.ts.map +0 -1
  187. package/dist/tests/storage.test.js +0 -184
  188. package/dist/tests/storage.test.js.map +0 -1
  189. package/dist/tsconfig.tsbuildinfo +0 -1
  190. package/dist/usdt0-bridge/bridge.d.ts +0 -82
  191. package/dist/usdt0-bridge/bridge.d.ts.map +0 -1
  192. package/dist/usdt0-bridge/bridge.js +0 -56
  193. package/dist/usdt0-bridge/bridge.js.map +0 -1
  194. package/dist/usdt0-bridge/chains.d.ts +0 -41
  195. package/dist/usdt0-bridge/chains.d.ts.map +0 -1
  196. package/dist/usdt0-bridge/chains.js +0 -117
  197. package/dist/usdt0-bridge/chains.js.map +0 -1
  198. package/dist/usdt0-bridge/layerzero-tracker.d.ts +0 -34
  199. package/dist/usdt0-bridge/layerzero-tracker.d.ts.map +0 -1
  200. package/dist/usdt0-bridge/layerzero-tracker.js +0 -86
  201. package/dist/usdt0-bridge/layerzero-tracker.js.map +0 -1
  202. package/dist/usdt0-bridge/oft-abi.d.ts +0 -145
  203. package/dist/usdt0-bridge/oft-abi.d.ts.map +0 -1
  204. package/dist/usdt0-bridge/oft-abi.js +0 -117
  205. package/dist/usdt0-bridge/oft-abi.js.map +0 -1
@@ -1,366 +0,0 @@
1
- /**
2
- * On-chain Bitcoin HTLC refund implementation.
3
- *
4
- * This module provides Taproot HTLC refund transaction building for BTC → EVM swaps
5
- * where users lock on-chain Bitcoin. If the swap times out, users can reclaim
6
- * their funds using this refund logic.
7
- *
8
- * The HTLC uses a Taproot output with:
9
- * - Unspendable key spend (NUMS internal key)
10
- * - Hashlock script path: server claims with preimage
11
- * - Timelock script path: user refunds after locktime
12
- */
13
- import { schnorr } from "@noble/curves/secp256k1.js";
14
- import { ripemd160 } from "@noble/hashes/legacy";
15
- import { sha256 } from "@noble/hashes/sha2";
16
- import { hex } from "@scure/base";
17
- import * as btc from "@scure/btc-signer";
18
- /**
19
- * NUMS (Nothing Up My Sleeve) point - provably unspendable public key.
20
- * This is the standard BIP-341 NUMS point used as the internal key
21
- * for script-only Taproot outputs.
22
- */
23
- const NUMS_POINT = hex.decode("0250929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0");
24
- /**
25
- * Regtest network configuration.
26
- * btc-signer only provides NETWORK (mainnet) and TEST_NETWORK (testnet/signet),
27
- * but regtest uses a different bech32 prefix ('bcrt' instead of 'tb').
28
- */
29
- const REGTEST_NETWORK = {
30
- bech32: "bcrt",
31
- pubKeyHash: 0x6f,
32
- scriptHash: 0xc4,
33
- wif: 0xef,
34
- };
35
- /**
36
- * Get the btc-signer network configuration.
37
- */
38
- function getNetwork(network) {
39
- switch (network) {
40
- case "mainnet":
41
- return btc.NETWORK;
42
- case "testnet":
43
- case "signet":
44
- return btc.TEST_NETWORK;
45
- case "regtest":
46
- return REGTEST_NETWORK;
47
- default:
48
- throw new Error(`Unknown network: ${network}`);
49
- }
50
- }
51
- /**
52
- * Build the hashlock tapscript for server claim.
53
- *
54
- * Script: `<server_pk> OP_CHECKSIGVERIFY OP_HASH160 <hash_lock> OP_EQUAL`
55
- *
56
- * The server must provide a valid Schnorr signature AND the preimage.
57
- */
58
- function buildHashlockScript(hashLock, serverPubKey) {
59
- return btc.Script.encode([
60
- serverPubKey,
61
- "CHECKSIGVERIFY",
62
- "HASH160",
63
- hashLock,
64
- "EQUAL",
65
- ]);
66
- }
67
- /**
68
- * Build the timelock tapscript for user refund.
69
- *
70
- * Script: `<locktime> OP_CLTV OP_DROP <user_pk> OP_CHECKSIG`
71
- *
72
- * The user can spend after the locktime has passed.
73
- */
74
- function buildTimelockScript(userPubKey, refundLocktime) {
75
- return btc.Script.encode([
76
- refundLocktime,
77
- "CHECKLOCKTIMEVERIFY",
78
- "DROP",
79
- userPubKey,
80
- "CHECKSIG",
81
- ]);
82
- }
83
- /**
84
- * Compute HASH160 (RIPEMD160(SHA256(data))).
85
- */
86
- export function computeHash160(data) {
87
- return ripemd160(sha256(data));
88
- }
89
- /**
90
- * Build a Taproot HTLC spending info.
91
- *
92
- * Creates the P2TR structure with:
93
- * - Unspendable internal key (NUMS point)
94
- * - Left leaf: hashlock script (server claim)
95
- * - Right leaf: timelock script (user refund)
96
- */
97
- function buildHtlcTaprootInfo(hashLock, serverPubKey, userPubKey, refundLocktime) {
98
- const hashlockScript = buildHashlockScript(hashLock, serverPubKey);
99
- const timelockScript = buildTimelockScript(userPubKey, refundLocktime);
100
- // Build the taproot tree with two leaves
101
- // Using NUMS point as internal key (script-path only)
102
- const p2tr = btc.p2tr(NUMS_POINT.slice(1), // Remove the 02/03 prefix for x-only
103
- [{ script: hashlockScript }, { script: timelockScript }], undefined, true);
104
- return { hashlockScript, timelockScript, p2tr };
105
- }
106
- /**
107
- * Estimate the virtual size of a Taproot script-path refund transaction.
108
- *
109
- * Components:
110
- * - Version: 4 bytes
111
- * - Marker + Flag: 2 bytes (for witness)
112
- * - Input count: 1 byte
113
- * - Input: 32 (txid) + 4 (vout) + 1 (script len) + 4 (sequence) = 41 bytes
114
- * - Output count: 1 byte
115
- * - Output: 8 (value) + 1 (script len) + 34 (P2TR output) = 43 bytes
116
- * - Witness: ~130 bytes (sig + script + control block)
117
- * - Locktime: 4 bytes
118
- *
119
- * Total base: ~96 bytes, witness: ~130 bytes
120
- * vBytes = base + witness/4 ≈ 96 + 33 = 129 vBytes
121
- *
122
- * Using a conservative estimate of 130 vBytes.
123
- */
124
- const REFUND_TX_VBYTES = 130n;
125
- /**
126
- * Estimate vBytes for a claim transaction.
127
- * Similar to refund but includes the preimage in the witness (~32 bytes extra).
128
- * Conservative estimate: 150 vBytes.
129
- */
130
- const CLAIM_TX_VBYTES = 150n;
131
- /**
132
- * Build and sign a claim transaction for an on-chain Bitcoin Taproot HTLC.
133
- *
134
- * This creates a transaction that spends from the HTLC using the hashlock
135
- * script path by providing the preimage and a valid signature.
136
- *
137
- * Used for EVM-to-Bitcoin swaps where the user claims BTC after the server
138
- * has funded the on-chain HTLC.
139
- *
140
- * @param params - The claim parameters
141
- * @returns The signed transaction and related info
142
- * @throws Error if the transaction cannot be built
143
- */
144
- export function buildOnchainClaimTransaction(params) {
145
- const { fundingTxId, fundingVout, htlcAmount, hashLock, userClaimPubKey, serverRefundPubKey, userSecretKey, preimage, refundLocktime, destinationAddress, feeRateSatPerVb, network, } = params;
146
- // Parse hex inputs
147
- const hashLockBytes = hex.decode(hashLock);
148
- const userClaimPkBytes = hex.decode(userClaimPubKey);
149
- const serverRefundPkBytes = hex.decode(serverRefundPubKey);
150
- const userSkBytes = hex.decode(userSecretKey);
151
- const preimageBytes = hex.decode(preimage);
152
- if (hashLockBytes.length !== 20) {
153
- throw new Error(`Invalid hash lock length: expected 20, got ${hashLockBytes.length}`);
154
- }
155
- if (userClaimPkBytes.length !== 32) {
156
- throw new Error(`Invalid user claim pubkey length: expected 32, got ${userClaimPkBytes.length}`);
157
- }
158
- if (serverRefundPkBytes.length !== 32) {
159
- throw new Error(`Invalid server refund pubkey length: expected 32, got ${serverRefundPkBytes.length}`);
160
- }
161
- if (userSkBytes.length !== 32) {
162
- throw new Error(`Invalid user secret key length: expected 32, got ${userSkBytes.length}`);
163
- }
164
- if (preimageBytes.length !== 32) {
165
- throw new Error(`Invalid preimage length: expected 32, got ${preimageBytes.length}`);
166
- }
167
- // Verify preimage matches hash lock
168
- const computedHashLock = computeHash160(preimageBytes);
169
- if (hex.encode(computedHashLock) !== hex.encode(hashLockBytes)) {
170
- throw new Error("Preimage does not match hash lock: HASH160(preimage) != hashLock");
171
- }
172
- // Build the HTLC Taproot structure
173
- // For evm_to_bitcoin: user is claimer (hashlock), server is refunder (timelock)
174
- // buildHtlcTaprootInfo(hashLock, claimerPk, refunderPk, locktime)
175
- const { p2tr } = buildHtlcTaprootInfo(hashLockBytes, userClaimPkBytes, serverRefundPkBytes, refundLocktime);
176
- // Calculate fee
177
- const fee = CLAIM_TX_VBYTES * BigInt(Math.ceil(feeRateSatPerVb));
178
- if (fee >= htlcAmount) {
179
- throw new Error(`Fee (${fee} sats) exceeds HTLC amount (${htlcAmount} sats)`);
180
- }
181
- const claimAmount = htlcAmount - fee;
182
- // Get network config
183
- const networkConfig = getNetwork(network);
184
- // Find the tapLeafScript for the hashlock script (index 0 in our tree)
185
- const tapLeafScript = p2tr.tapLeafScript;
186
- if (!tapLeafScript || tapLeafScript.length < 1) {
187
- throw new Error("Failed to build tapLeafScript for hashlock");
188
- }
189
- // The hashlock script is at index 0 in our tree
190
- const hashlockLeaf = tapLeafScript[0];
191
- // Build the transaction
192
- const tx = new btc.Transaction({
193
- allowUnknownOutputs: true,
194
- allowUnknownInputs: true,
195
- });
196
- // Add input (the HTLC output we're spending)
197
- tx.addInput({
198
- txid: fundingTxId,
199
- index: fundingVout,
200
- witnessUtxo: {
201
- script: p2tr.script,
202
- amount: htlcAmount,
203
- },
204
- tapLeafScript: [hashlockLeaf],
205
- sequence: 0xffffffff,
206
- });
207
- // Add output (destination)
208
- tx.addOutputAddress(destinationAddress, claimAmount, networkConfig);
209
- // Manually compute sighash, sign, and build the full witness.
210
- // We can't use tx.signIdx() because finalize() only produces [signature]
211
- // but the hashlock script requires [preimage, signature] in the witness.
212
- // After signIdx, btc-signer locks the transaction and won't let us modify
213
- // the witness fields. So we do everything manually.
214
- const [controlBlockInfo, leafScriptWithVersion] = hashlockLeaf;
215
- // tapLeafScript entries have the leaf version byte appended to the script.
216
- // Strip it for sighash computation and the witness.
217
- const leafScript = leafScriptWithVersion.slice(0, -1);
218
- const leafVersion = leafScriptWithVersion[leafScriptWithVersion.length - 1];
219
- // Compute the Taproot script-path sighash (BIP 342)
220
- // preimageWitnessV1 returns the final tagged hash, not a preimage to be hashed again.
221
- const sighash = tx.preimageWitnessV1(0, [p2tr.script], btc.SigHash.DEFAULT, [htlcAmount], undefined, leafScript, leafVersion);
222
- // Sign the sighash with Schnorr (BIP 340)
223
- const sig = schnorr.sign(sighash, userSkBytes);
224
- // Encode the control block struct to raw bytes:
225
- // [version(1)] [internalKey(32)] [merklePath(32*n)]
226
- const cbInfo = controlBlockInfo;
227
- const encodedControlBlock = new Uint8Array(1 + 32 + 32 * cbInfo.merklePath.length);
228
- encodedControlBlock[0] = cbInfo.version;
229
- encodedControlBlock.set(cbInfo.internalKey, 1);
230
- for (let i = 0; i < cbInfo.merklePath.length; i++) {
231
- encodedControlBlock.set(cbInfo.merklePath[i], 33 + 32 * i);
232
- }
233
- // Set the final witness directly (no signIdx needed):
234
- // [preimage, signature, script, control_block]
235
- tx.updateInput(0, {
236
- finalScriptWitness: [preimageBytes, sig, leafScript, encodedControlBlock],
237
- });
238
- // Extract the signed transaction
239
- const txHex = hex.encode(tx.extract());
240
- const txId = tx.id;
241
- // Compute the HTLC address
242
- const htlcAddress = btc.Address(networkConfig).encode({
243
- type: "tr",
244
- pubkey: p2tr.tweakedPubkey,
245
- });
246
- return {
247
- txHex,
248
- txId,
249
- claimAmount,
250
- fee,
251
- htlcAddress,
252
- };
253
- }
254
- /**
255
- * Build and sign a refund transaction for an on-chain Bitcoin HTLC.
256
- *
257
- * This creates a transaction that spends from the HTLC using the timelock
258
- * script path after the refund locktime has passed.
259
- *
260
- * @param params - The refund parameters
261
- * @returns The signed transaction and related info
262
- * @throws Error if the transaction cannot be built
263
- */
264
- export function buildOnchainRefundTransaction(params) {
265
- const { fundingTxId, fundingVout, htlcAmount, hashLock, serverPubKey, userPubKey, userSecretKey, refundLocktime, destinationAddress, feeRateSatPerVb, network, } = params;
266
- // Parse hex inputs
267
- const hashLockBytes = hex.decode(hashLock);
268
- const serverPkBytes = hex.decode(serverPubKey);
269
- const userPkBytes = hex.decode(userPubKey);
270
- const userSkBytes = hex.decode(userSecretKey);
271
- if (hashLockBytes.length !== 20) {
272
- throw new Error(`Invalid hash lock length: expected 20, got ${hashLockBytes.length}`);
273
- }
274
- if (serverPkBytes.length !== 32) {
275
- throw new Error(`Invalid server pubkey length: expected 32, got ${serverPkBytes.length}`);
276
- }
277
- if (userPkBytes.length !== 32) {
278
- throw new Error(`Invalid user pubkey length: expected 32, got ${userPkBytes.length}`);
279
- }
280
- if (userSkBytes.length !== 32) {
281
- throw new Error(`Invalid user secret key length: expected 32, got ${userSkBytes.length}`);
282
- }
283
- // Build the HTLC Taproot structure
284
- const { p2tr } = buildHtlcTaprootInfo(hashLockBytes, serverPkBytes, userPkBytes, refundLocktime);
285
- // Calculate fee
286
- const fee = REFUND_TX_VBYTES * BigInt(Math.ceil(feeRateSatPerVb));
287
- if (fee >= htlcAmount) {
288
- throw new Error(`Fee (${fee} sats) exceeds HTLC amount (${htlcAmount} sats)`);
289
- }
290
- const refundAmount = htlcAmount - fee;
291
- // Get network config
292
- const networkConfig = getNetwork(network);
293
- // Find the tapLeafScript for the timelock script (index 1 in our tree)
294
- // p2tr.tapLeafScript is an array of [controlBlockInfo, script] tuples
295
- const tapLeafScript = p2tr.tapLeafScript;
296
- if (!tapLeafScript || tapLeafScript.length < 2) {
297
- throw new Error("Failed to build tapLeafScript for timelock");
298
- }
299
- // The timelock script is at index 1 in our tree
300
- const timelockLeaf = tapLeafScript[1];
301
- // Build the transaction with lockTime in constructor
302
- const tx = new btc.Transaction({
303
- allowUnknownOutputs: true,
304
- allowUnknownInputs: true,
305
- lockTime: refundLocktime,
306
- });
307
- // Add input (the HTLC output we're spending)
308
- tx.addInput({
309
- txid: fundingTxId,
310
- index: fundingVout,
311
- witnessUtxo: {
312
- script: p2tr.script,
313
- amount: htlcAmount,
314
- },
315
- tapLeafScript: [timelockLeaf],
316
- sequence: 0xfffffffe, // Enable locktime (< 0xffffffff)
317
- });
318
- // Add output (destination)
319
- tx.addOutputAddress(destinationAddress, refundAmount, networkConfig);
320
- // Sign the input
321
- tx.signIdx(userSkBytes, 0);
322
- // Finalize
323
- tx.finalize();
324
- // Extract the signed transaction
325
- const txHex = hex.encode(tx.extract());
326
- const txId = tx.id;
327
- // Compute the HTLC address
328
- const htlcAddress = btc.Address(networkConfig).encode({
329
- type: "tr",
330
- pubkey: p2tr.tweakedPubkey,
331
- });
332
- return {
333
- txHex,
334
- txId,
335
- refundAmount,
336
- fee,
337
- htlcAddress,
338
- };
339
- }
340
- /**
341
- * Verify that a Taproot address matches the expected HTLC parameters.
342
- *
343
- * This is useful to confirm the HTLC address returned by the server
344
- * matches what we expect based on the swap parameters.
345
- *
346
- * @param expectedAddress - The address to verify
347
- * @param hashLock - Hash lock (20-byte hex)
348
- * @param serverPubKey - Server's x-only public key (32-byte hex)
349
- * @param userPubKey - User's x-only public key (32-byte hex)
350
- * @param refundLocktime - Refund locktime (unix timestamp)
351
- * @param network - Bitcoin network
352
- * @returns true if the address matches, false otherwise
353
- */
354
- export function verifyHtlcAddress(expectedAddress, hashLock, serverPubKey, userPubKey, refundLocktime, network) {
355
- const hashLockBytes = hex.decode(hashLock);
356
- const serverPkBytes = hex.decode(serverPubKey);
357
- const userPkBytes = hex.decode(userPubKey);
358
- const { p2tr } = buildHtlcTaprootInfo(hashLockBytes, serverPkBytes, userPkBytes, refundLocktime);
359
- const networkConfig = getNetwork(network);
360
- const computedAddress = btc.Address(networkConfig).encode({
361
- type: "tr",
362
- pubkey: p2tr.tweakedPubkey,
363
- });
364
- return computedAddress === expectedAddress;
365
- }
366
- //# sourceMappingURL=onchain.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"onchain.js","sourceRoot":"","sources":["../../../src/refund/onchain.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,KAAK,GAAG,MAAM,mBAAmB,CAAC;AAuFzC;;;;GAIG;AACH,MAAM,UAAU,GAAG,GAAG,CAAC,MAAM,CAC3B,oEAAoE,CACrE,CAAC;AAEF;;;;GAIG;AACH,MAAM,eAAe,GAAG;IACtB,MAAM,EAAE,MAAM;IACd,UAAU,EAAE,IAAI;IAChB,UAAU,EAAE,IAAI;IAChB,GAAG,EAAE,IAAI;CACD,CAAC;AAEX;;GAEG;AACH,SAAS,UAAU,CACjB,OAAuB;IAEvB,QAAQ,OAAO,EAAE,CAAC;QAChB,KAAK,SAAS;YACZ,OAAO,GAAG,CAAC,OAAO,CAAC;QACrB,KAAK,SAAS,CAAC;QACf,KAAK,QAAQ;YACX,OAAO,GAAG,CAAC,YAAY,CAAC;QAC1B,KAAK,SAAS;YACZ,OAAO,eAAe,CAAC;QACzB;YACE,MAAM,IA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ACV,MAAM,EAAE,IAAI,CAAC,aAAa;KAC3B,CAAC,CAAC;IAEH,OAAO;QACL,KAAK;QACL,IAAI;QACJ,YAAY;QACZ,GAAG;QACH,WAAW;KACZ,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,iBAAiB,CAC/B,eAAuB,EACvB,QAAgB,EAChB,YAAoB,EACpB,UAAkB,EAClB,cAAsB,EACtB,OAAuB;IAEvB,MAAM,aAAa,GAAG,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC3C,MAAM,aAAa,GAAG,GAAG,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;IAC/C,MAAM,WAAW,GAAG,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IAE3C,MAAM,EAAE,IAAI,EAAE,GAAG,oBAAoB,CACnC,aAAa,EACb,aAAa,EACb,WAAW,EACX,cAAc,CACf,CAAC;IAEF,MAAM,aAAa,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;IAC1C,MAAM,eAAe,GAAG,GAAG,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,MAAM,CAAC;QACxD,IAAI,EAAE,IAAI;QACV,MAAM,EAAE,IAAI,CAAC,aAAa;KAC3B,CAAC,CAAC;IAEH,OAAO,eAAe,KAAK,eAAe,CAAC;AAC7C,CAAC"}
@@ -1,106 +0,0 @@
1
- /**
2
- * Signer module for HD wallet key derivation.
3
- *
4
- * This module provides BIP39/BIP32 key derivation for Lendaswap swaps,
5
- * mirroring the Rust implementation in `client-sdk/core/src/hd_wallet.rs`.
6
- */
7
- import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
8
- import { HDKey } from "@scure/bip32";
9
- /**
10
- * Parameters derived for a single swap.
11
- */
12
- export interface SwapParams {
13
- /** The secret key (32 bytes) for signing. */
14
- secretKey: Uint8Array;
15
- /** The compressed public key (33 bytes). */
16
- publicKey: Uint8Array;
17
- /** The preimage (32 bytes) for the HTLC. */
18
- preimage: Uint8Array;
19
- /** The SHA256 hash of the preimage (32 bytes). */
20
- preimageHash: Uint8Array;
21
- /** The user ID public key (33 bytes) for swap recovery. */
22
- userId: Uint8Array;
23
- /** The key index used for derivation. */
24
- keyIndex: number;
25
- }
26
- /**
27
- * HD Wallet Signer for Lendaswap key derivation.
28
- *
29
- * Provides BIP39/BIP32 key derivation matching the Rust `HdWallet` implementation.
30
- *
31
- * @example
32
- * ```ts
33
- * // Generate a new signer with a random mnemonic
34
- * const signer = Signer.generate();
35
- *
36
- * // Or restore from an existing mnemonic
37
- * const signer = Signer.fromMnemonic("your twelve word mnemonic phrase here ...");
38
- *
39
- * // Derive swap parameters at a specific index
40
- * const params = signer.deriveSwapParams(0);
41
- * ```
42
- */
43
- export declare class Signer {
44
- #private;
45
- private constructor();
46
- /**
47
- * Generate a new Signer with a random mnemonic.
48
- *
49
- * @param wordCount - Number of words (12, 15, 18, 21, or 24). Defaults to 12.
50
- * @returns A new Signer instance.
51
- * @throws Error if the word count is invalid.
52
- */
53
- static generate(wordCount?: 12 | 15 | 18 | 21 | 24): Signer;
54
- /**
55
- * Create a Signer from an existing mnemonic phrase.
56
- *
57
- * @param phrase - The BIP39 mnemonic phrase (12, 15, 18, 21, or 24 words).
58
- * @returns A new Signer instance.
59
- * @throws Error if the mnemonic is invalid.
60
- */
61
- static fromMnemonic(phrase: string): Signer;
62
- /**
63
- * Get the mnemonic phrase.
64
- *
65
- * @returns The BIP39 mnemonic phrase.
66
- */
67
- get mnemonic(): string;
68
- /**
69
- * Derive swap parameters at the given index.
70
- *
71
- * Derivation path: `m/{SIGNING_PREFIX}'/{LSW_IDENTIFIER}'/{index}'`
72
- *
73
- * @param index - The key index to derive.
74
- * @returns The derived swap parameters.
75
- */
76
- deriveSwapParams(index: number): SwapParams;
77
- /**
78
- * Derive a user ID at the specified index.
79
- *
80
- * User IDs are derived using a non-hardened path, so that the corresponding
81
- * Xpub can be shared with the server for efficient recovery of swap data.
82
- *
83
- * @param index - The key index.
84
- * @returns The user ID public key (33 bytes).
85
- */
86
- private deriveUserId;
87
- /**
88
- * Derive the Xpub used for user ID derivation.
89
- *
90
- * This Xpub is derived using a hardened path from the master key,
91
- * ensuring parent key safety even if individual derived keys are leaked.
92
- *
93
- * This Xpub can be shared with the server for wallet recovery.
94
- *
95
- * @returns The HDKey representing the user ID Xpub.
96
- */
97
- deriveUserIdXpub(): HDKey;
98
- /**
99
- * Get the serialized user ID Xpub string.
100
- *
101
- * @returns The base58check-encoded extended public key (xpub...).
102
- */
103
- getUserIdXpubString(): string;
104
- }
105
- export { bytesToHex, hexToBytes };
106
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/signer/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAarC;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,6CAA6C;IAC7C,SAAS,EAAE,UAAU,CAAC;IACtB,4CAA4C;IAC5C,SAAS,EAAE,UAAU,CAAC;IACtB,4CAA4C;IAC5C,QAAQ,EAAE,UAAU,CAAC;IACrB,kDAAkD;IAClD,YAAY,EAAE,UAAU,CAAC;IACzB,2DAA2D;IAC3D,MAAM,EAAE,UAAU,CAAC;IACnB,yCAAyC;IACzC,QAAQ,EAAE,MAAM,CAAC;CAClB;AAoBD;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,MAAM;;IAIjB,OAAO;IAMP;;;;;;OAMG;IACH,MAAM,CAAC,QAAQ,CAAC,SAAS,GAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAO,GAAG,MAAM;IAM/D;;;;;;OAMG;IACH,MAAM,CAAC,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IAQ3C;;;;OAIG;IACH,IAAI,QAAQ,IAAI,MAAM,CAErB;IAED;;;;;;;OAOG;IACH,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU;IAiC3C;;;;;;;;OAQG;IACH,OAAO,CAAC,YAAY;IAcpB;;;;;;;;;OASG;IACH,gBAAgB,IAAI,KAAK;IAWzB;;;;OAIG;IACH,mBAAmB,IAAI,MAAM;CAK9B;AAGD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC"}
@@ -1,179 +0,0 @@
1
- /**
2
- * Signer module for HD wallet key derivation.
3
- *
4
- * This module provides BIP39/BIP32 key derivation for Lendaswap swaps,
5
- * mirroring the Rust implementation in `client-sdk/core/src/hd_wallet.rs`.
6
- */
7
- import { sha256 } from "@noble/hashes/sha256";
8
- import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
9
- import { HDKey } from "@scure/bip32";
10
- import * as bip39 from "@scure/bip39";
11
- import { wordlist } from "@scure/bip39/wordlists/english";
12
- /** BIP-85 prefix for signing keys. */
13
- const SIGNING_PREFIX = 83696968;
14
- /** Prefix for identity key derivation. */
15
- const ID_PREFIX = 9419;
16
- /** Lendaswap identifier ("LSW" encoded). */
17
- const LSW_IDENTIFIER = 121923;
18
- /** Tag for BIP340-style tagged hash preimage generation. */
19
- const PREIMAGE_TAG = "lendaswap/preimage";
20
- /**
21
- * BIP340-style tagged hash function for domain separation.
22
- *
23
- * Computes: sha256(sha256(tag) || sha256(tag) || data).
24
- *
25
- * @param tag - The domain separation tag.
26
- * @param data - The data to hash.
27
- * @returns The tagged hash (32 bytes).
28
- */
29
- function taggedHash(tag, data) {
30
- const tagHash = sha256(new TextEncoder().encode(tag));
31
- const combined = new Uint8Array(tagHash.length * 2 + data.length);
32
- combined.set(tagHash, 0);
33
- combined.set(tagHash, tagHash.length);
34
- combined.set(data, tagHash.length * 2);
35
- return sha256(combined);
36
- }
37
- /**
38
- * HD Wallet Signer for Lendaswap key derivation.
39
- *
40
- * Provides BIP39/BIP32 key derivation matching the Rust `HdWallet` implementation.
41
- *
42
- * @example
43
- * ```ts
44
- * // Generate a new signer with a random mnemonic
45
- * const signer = Signer.generate();
46
- *
47
- * // Or restore from an existing mnemonic
48
- * const signer = Signer.fromMnemonic("your twelve word mnemonic phrase here ...");
49
- *
50
- * // Derive swap parameters at a specific index
51
- * const params = signer.deriveSwapParams(0);
52
- * ```
53
- */
54
- export class Signer {
55
- #mnemonic;
56
- #seed;
57
- constructor(mnemonic) {
58
- this.#mnemonic = mnemonic;
59
- // No passphrase, matching Rust implementation
60
- this.#seed = bip39.mnemonicToSeedSync(mnemonic, "");
61
- }
62
- /**
63
- * Generate a new Signer with a random mnemonic.
64
- *
65
- * @param wordCount - Number of words (12, 15, 18, 21, or 24). Defaults to 12.
66
- * @returns A new Signer instance.
67
- * @throws Error if the word count is invalid.
68
- */
69
- static generate(wordCount = 12) {
70
- const strength = (wordCount / 3) * 32; // 128, 160, 192, 224, or 256 bits
71
- const mnemonic = bip39.generateMnemonic(wordlist, strength);
72
- return new Signer(mnemonic);
73
- }
74
- /**
75
- * Create a Signer from an existing mnemonic phrase.
76
- *
77
- * @param phrase - The BIP39 mnemonic phrase (12, 15, 18, 21, or 24 words).
78
- * @returns A new Signer instance.
79
- * @throws Error if the mnemonic is invalid.
80
- */
81
- static fromMnemonic(phrase) {
82
- const normalized = phrase.trim().toLowerCase();
83
- if (!bip39.validateMnemonic(normalized, wordlist)) {
84
- throw new Error("Invalid mnemonic phrase");
85
- }
86
- return new Signer(normalized);
87
- }
88
- /**
89
- * Get the mnemonic phrase.
90
- *
91
- * @returns The BIP39 mnemonic phrase.
92
- */
93
- get mnemonic() {
94
- return this.#mnemonic;
95
- }
96
- /**
97
- * Derive swap parameters at the given index.
98
- *
99
- * Derivation path: `m/{SIGNING_PREFIX}'/{LSW_IDENTIFIER}'/{index}'`
100
- *
101
- * @param index - The key index to derive.
102
- * @returns The derived swap parameters.
103
- */
104
- deriveSwapParams(index) {
105
- const master = HDKey.fromMasterSeed(this.#seed);
106
- // Derive signing key: m/{SIGNING_PREFIX}'/{LSW_IDENTIFIER}'/{index}'
107
- const signingPath = `m/${SIGNING_PREFIX}'/${LSW_IDENTIFIER}'/${index}'`;
108
- const derived = master.derive(signingPath);
109
- if (!derived.privateKey || !derived.publicKey) {
110
- throw new Error("Failed to derive key");
111
- }
112
- const secretKey = derived.privateKey;
113
- const publicKey = derived.publicKey;
114
- // Generate preimage using tagged hash (BIP340-style)
115
- const preimage = taggedHash(PREIMAGE_TAG, secretKey);
116
- // preimageHash = SHA256(preimage)
117
- const preimageHash = sha256(preimage);
118
- // Derive user ID
119
- const userId = this.deriveUserId(index);
120
- return {
121
- secretKey,
122
- publicKey,
123
- preimage,
124
- preimageHash,
125
- userId,
126
- keyIndex: index,
127
- };
128
- }
129
- /**
130
- * Derive a user ID at the specified index.
131
- *
132
- * User IDs are derived using a non-hardened path, so that the corresponding
133
- * Xpub can be shared with the server for efficient recovery of swap data.
134
- *
135
- * @param index - The key index.
136
- * @returns The user ID public key (33 bytes).
137
- */
138
- deriveUserId(index) {
139
- const xpub = this.deriveUserIdXpub();
140
- // Build non-hardened derivation path from the xpub
141
- const path = `m/${ID_PREFIX}/${LSW_IDENTIFIER}/${index}`;
142
- const derived = xpub.derive(path);
143
- if (!derived.publicKey) {
144
- throw new Error("Failed to derive user ID");
145
- }
146
- return derived.publicKey;
147
- }
148
- /**
149
- * Derive the Xpub used for user ID derivation.
150
- *
151
- * This Xpub is derived using a hardened path from the master key,
152
- * ensuring parent key safety even if individual derived keys are leaked.
153
- *
154
- * This Xpub can be shared with the server for wallet recovery.
155
- *
156
- * @returns The HDKey representing the user ID Xpub.
157
- */
158
- deriveUserIdXpub() {
159
- const master = HDKey.fromMasterSeed(this.#seed);
160
- // Build hardened derivation path
161
- const path = `m/${ID_PREFIX}'/${LSW_IDENTIFIER}'/0'`;
162
- const derived = master.derive(path);
163
- // Return neutered key (Xpub only, no private key)
164
- return derived.wipePrivateData();
165
- }
166
- /**
167
- * Get the serialized user ID Xpub string.
168
- *
169
- * @returns The base58check-encoded extended public key (xpub...).
170
- */
171
- getUserIdXpubString() {
172
- const xpub = this.deriveUserIdXpub();
173
- // HDKey.publicExtendedKey returns the base58check-encoded xpub
174
- return xpub.publicExtendedKey;
175
- }
176
- }
177
- // Re-export utility functions
178
- export { bytesToHex, hexToBytes };
179
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/signer/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AAE1D,sCAAsC;AACtC,MAAM,cAAc,GAAG,QAAQ,CAAC;AAChC,0CAA0C;AAC1C,MAAM,SAAS,GAAG,IAAI,CAAC;AACvB,4CAA4C;AAC5C,MAAM,cAAc,GAAG,MAAM,CAAC;AAC9B,4DAA4D;AAC5D,MAAM,YAAY,GAAG,oBAAoB,CAAC;AAoB1C;;;;;;;;GAQG;AACH,SAAS,UAAU,CAAC,GAAW,EAAE,IAAgB;IAC/C,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;IACtD,MAAM,QAAQ,GAAG,IAAI,UAAU,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;IAClE,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;IACzB,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IACtC,QAAQ,CAAC,GAAG,CAA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