@anywalletkit/protocol-swap2-layerswap 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +97 -2
  3. package/dist/activity.cjs +122 -0
  4. package/dist/activity.cjs.map +1 -0
  5. package/dist/activity.d.cts +15 -0
  6. package/dist/activity.d.ts +15 -0
  7. package/dist/activity.js +10 -0
  8. package/dist/activity.js.map +1 -0
  9. package/dist/chunk-5QTIHCE4.js +99 -0
  10. package/dist/chunk-5QTIHCE4.js.map +1 -0
  11. package/dist/chunk-KCAZCR4I.js +27 -0
  12. package/dist/chunk-KCAZCR4I.js.map +1 -0
  13. package/dist/chunk-TEL62A5J.js +564 -0
  14. package/dist/chunk-TEL62A5J.js.map +1 -0
  15. package/dist/chunk-WEMNS3LY.js +21 -0
  16. package/dist/chunk-WEMNS3LY.js.map +1 -0
  17. package/dist/chunk-ZKT24QPJ.js +123 -0
  18. package/dist/chunk-ZKT24QPJ.js.map +1 -0
  19. package/dist/chunk-ZTFJO6AI.js +410 -0
  20. package/dist/chunk-ZTFJO6AI.js.map +1 -0
  21. package/dist/config.cjs +143 -0
  22. package/dist/config.cjs.map +1 -0
  23. package/dist/config.d.cts +74 -0
  24. package/dist/config.d.ts +74 -0
  25. package/dist/config.js +31 -0
  26. package/dist/config.js.map +1 -0
  27. package/dist/index.cjs +1189 -0
  28. package/dist/index.cjs.map +1 -0
  29. package/dist/index.d.cts +84 -0
  30. package/dist/index.d.ts +84 -0
  31. package/dist/index.js +126 -0
  32. package/dist/index.js.map +1 -0
  33. package/dist/pins.cjs +557 -0
  34. package/dist/pins.cjs.map +1 -0
  35. package/dist/pins.d.cts +6 -0
  36. package/dist/pins.d.ts +6 -0
  37. package/dist/pins.js +11 -0
  38. package/dist/pins.js.map +1 -0
  39. package/dist/protocol.cjs +1052 -0
  40. package/dist/protocol.cjs.map +1 -0
  41. package/dist/protocol.d.cts +19 -0
  42. package/dist/protocol.d.ts +19 -0
  43. package/dist/protocol.js +11 -0
  44. package/dist/protocol.js.map +1 -0
  45. package/dist/provider.cjs +1039 -0
  46. package/dist/provider.cjs.map +1 -0
  47. package/dist/provider.d.cts +23 -0
  48. package/dist/provider.d.ts +23 -0
  49. package/dist/provider.js +16 -0
  50. package/dist/provider.js.map +1 -0
  51. package/dist/verify.cjs +554 -0
  52. package/dist/verify.cjs.map +1 -0
  53. package/dist/verify.d.cts +169 -0
  54. package/dist/verify.d.ts +169 -0
  55. package/dist/verify.js +46 -0
  56. package/dist/verify.js.map +1 -0
  57. package/module.json +64 -0
  58. package/package.json +100 -3
package/dist/pins.cjs ADDED
@@ -0,0 +1,557 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/pins.ts
21
+ var pins_exports = {};
22
+ __export(pins_exports, {
23
+ layerswapPins: () => layerswapPins,
24
+ layerswapVerifyQuote: () => layerswapVerifyQuote
25
+ });
26
+ module.exports = __toCommonJS(pins_exports);
27
+ var import_swap2_assets = require("@anywalletkit/swap2-assets");
28
+ var import_util4 = require("@anywalletkit/protocol-swap2-core/util");
29
+ var import_errors3 = require("@anywalletkit/protocol-swap2-core/errors");
30
+
31
+ // src/config.ts
32
+ var import_util = require("@anywalletkit/protocol-swap2-core/util");
33
+ var LAYERSWAP_DEPOSITORY = "0xe226e4825cb215abafad98fdd400583eab6a594f";
34
+ var LAYERSWAP_RECEIVERS = Object.freeze([
35
+ "0x2fc617e933a52713247ce25730f6695920b3befe",
36
+ "0x08b00ceee2fb66029b53d76110b19eeaabfd1e65"
37
+ ]);
38
+ var LAYERSWAP_DEPOSITORY_CHAINS = Object.freeze([
39
+ 1,
40
+ 10,
41
+ 56,
42
+ 100,
43
+ 130,
44
+ 137,
45
+ 143,
46
+ 146,
47
+ 169,
48
+ 196,
49
+ 204,
50
+ 252,
51
+ 324,
52
+ 360,
53
+ 480,
54
+ 988,
55
+ 999,
56
+ 1329,
57
+ 1868,
58
+ 2020,
59
+ 2741,
60
+ 2818,
61
+ 4217,
62
+ 4326,
63
+ 4663,
64
+ 5e3,
65
+ 5042,
66
+ 8217,
67
+ 8453,
68
+ 9745,
69
+ 13371,
70
+ 42161,
71
+ 42220,
72
+ 42793,
73
+ 43114,
74
+ 48900,
75
+ 57073,
76
+ 59144,
77
+ 60808,
78
+ 80094,
79
+ 99901,
80
+ 167e3,
81
+ 534352,
82
+ 747474,
83
+ 7777777
84
+ ]);
85
+ var LAYERSWAP_EVM_CHAINS = Object.freeze([
86
+ ...LAYERSWAP_DEPOSITORY_CHAINS,
87
+ 14,
88
+ 25,
89
+ 30,
90
+ 40,
91
+ 122,
92
+ 747,
93
+ 4114,
94
+ 16661,
95
+ 34443
96
+ ].sort((a, b) => a - b));
97
+ var LAYERSWAP_NON_EVM = Object.freeze({
98
+ "solana:mainnet": "SOLANA_MAINNET",
99
+ "ton:-239": "TON_MAINNET",
100
+ "tron:728126428": "TRON_MAINNET",
101
+ "bitcoin:mainnet": "BITCOIN_MAINNET"
102
+ });
103
+ function fundingFor(sourceChain) {
104
+ const ns = (0, import_util.namespace)(sourceChain);
105
+ if (ns === "eip155") return LAYERSWAP_DEPOSITORY_CHAINS.includes(evmChainId(sourceChain) ?? -1) ? "depository" : null;
106
+ if (ns === "solana" || ns === "ton" || ns === "tron") return sourceChain in LAYERSWAP_NON_EVM ? "wallet" : null;
107
+ if (ns === "bitcoin") return sourceChain in LAYERSWAP_NON_EVM ? "deposit_address" : null;
108
+ return null;
109
+ }
110
+ function evmChainId(caip2) {
111
+ const m = /^eip155:([1-9]\d{0,9})$/.exec(caip2);
112
+ return m ? Number(m[1]) : null;
113
+ }
114
+ function sameNetwork(caip2, network) {
115
+ if (!network) return false;
116
+ const id = evmChainId(caip2);
117
+ if (id !== null) return String(network.chain_id ?? "") === String(id);
118
+ return network.name === LAYERSWAP_NON_EVM[caip2];
119
+ }
120
+
121
+ // src/verify.ts
122
+ var import_viem = require("viem");
123
+ var import_ed25519 = require("@noble/curves/ed25519.js");
124
+ var import_sha2 = require("@noble/hashes/sha2.js");
125
+ var import_util3 = require("@anywalletkit/protocol-swap2-core/util");
126
+ var import_errors2 = require("@anywalletkit/protocol-swap2-core/errors");
127
+
128
+ // src/api.ts
129
+ var import_util2 = require("@anywalletkit/protocol-swap2-core/util");
130
+ var import_errors = require("@anywalletkit/protocol-swap2-core/errors");
131
+ var MAX_BODY = 1024 * 1024;
132
+ var MAX_NETWORKS_BODY = 8 * 1024 * 1024;
133
+ var LAYERSWAP_ERROR_REASONS = Object.freeze(Object.assign(/* @__PURE__ */ Object.create(null), {
134
+ ROUTE_NOT_FOUND_ERROR: "unsupported_pair",
135
+ VALIDATION_ERROR: "unsupported_pair",
136
+ UNSUPPORTED_GASLESS_SOURCE: "unsupported_pair",
137
+ LESS_THAN_MIN_ERROR: "amount_too_small",
138
+ GREATER_THAN_MAX_ERROR: "amount_too_large",
139
+ INVALID_ADDRESS_ERROR: "recipient_mismatch",
140
+ UNACTIVATED_ADDRESS_ERROR: "recipient_mismatch",
141
+ REFUND_ADDRESS_MISSING: "policy_refused",
142
+ DUPLICATE_REFERENCE_ID: "policy_refused",
143
+ PRICE_IMPACT_TOO_HIGH: "price_check_failed",
144
+ API_KEY_FORBIDDEN: "key_rejected",
145
+ UNEXPECTED_ERROR: "upstream_unavailable",
146
+ NOT_FOUND: "upstream_unavailable"
147
+ }));
148
+ function baseUnits(value) {
149
+ return typeof value === "string" && /^(?:0|[1-9]\d{0,77})$/.test(value) ? BigInt(value) : null;
150
+ }
151
+ function tokenKey(caip2, contract) {
152
+ if (contract === null || contract === void 0 || contract === "" || /^0x0{40}$/i.test(contract)) return null;
153
+ if (caip2.startsWith("eip155:")) return contract.toLowerCase();
154
+ if (caip2.startsWith("ton:")) return (0, import_util2.tonRawAddress)(contract) ?? `?${contract}`;
155
+ return contract;
156
+ }
157
+ function addressKey(caip2, address) {
158
+ if (typeof address !== "string" || address.trim() === "") return null;
159
+ const a = address.trim();
160
+ if (caip2.startsWith("eip155:")) return /^0x[0-9a-fA-F]{40}$/.test(a) ? a.toLowerCase() : null;
161
+ if (caip2.startsWith("ton:")) return (0, import_util2.tonRawAddress)(a);
162
+ return a;
163
+ }
164
+
165
+ // src/verify.ts
166
+ var LayerswapRefusal = class extends Error {
167
+ constructor(check, message, reason) {
168
+ super(`layerswap: ${message}`);
169
+ this.check = check;
170
+ this.name = "LayerswapRefusal";
171
+ (0, import_errors2.withReason)(this, reason);
172
+ }
173
+ check;
174
+ };
175
+ function fail(check, message, reason = "policy_refused") {
176
+ throw new LayerswapRefusal(check, message, reason);
177
+ }
178
+ function describe(network) {
179
+ const n = network;
180
+ return clipName(n?.name ?? n?.chain_id ?? "an unnamed network");
181
+ }
182
+ function clipName(v) {
183
+ return String(v).replace(/[^\x20-\x7e]/g, "?").slice(0, 60);
184
+ }
185
+ function commonAction(request, a) {
186
+ if (!sameNetwork(request.sourceChain, a.network)) fail("action-network", `the deposit is on ${describe(a.network)}, not ${request.sourceChain}`, "vault_mismatch");
187
+ if (tokenKey(request.sourceChain, a.tokenContract) !== tokenKey(request.sourceChain, request.sourceContract)) {
188
+ fail("action-token", "the deposit moves another token than the swap's input", "policy_refused");
189
+ }
190
+ }
191
+ var DEPOSITORY_ABI = [
192
+ { type: "function", name: "depositNative", stateMutability: "payable", inputs: [{ name: "id", type: "bytes32" }, { name: "receiver", type: "address" }], outputs: [] },
193
+ { type: "function", name: "depositERC20", stateMutability: "nonpayable", inputs: [{ name: "id", type: "bytes32" }, { name: "token", type: "address" }, { name: "receiver", type: "address" }, { name: "amount", type: "uint256" }], outputs: [] }
194
+ ];
195
+ function depositId(sequence) {
196
+ return (0, import_viem.pad)((0, import_viem.toHex)(sequence), { size: 32 });
197
+ }
198
+ function checkEvmDeposit(request, swap, a) {
199
+ commonAction(request, a);
200
+ const chainId = evmChainId(request.sourceChain);
201
+ if (chainId === null) fail("evm-chain", `${request.sourceChain} is not an EVM chain`, "vault_mismatch");
202
+ if (a.type !== "transfer") fail("evm-action", "an EVM deposit must be the Depository call", "unsupported_pair");
203
+ if (a.toAddress.toLowerCase() !== LAYERSWAP_DEPOSITORY) fail("evm-target", `the deposit calls ${clipName(a.toAddress)}, not the pinned Depository`, "policy_refused");
204
+ const data = a.callData;
205
+ if (!data || !/^0x[0-9a-fA-F]+$/.test(data)) fail("evm-calldata", "the deposit carries no calldata", "upstream_unavailable");
206
+ let decoded;
207
+ try {
208
+ decoded = (0, import_viem.decodeFunctionData)({ abi: DEPOSITORY_ABI, data });
209
+ } catch {
210
+ return fail("evm-calldata", "the deposit is not a Depository deposit call", "policy_refused");
211
+ }
212
+ const receiver = String(decoded.args[decoded.functionName === "depositNative" ? 1 : 2] ?? "").toLowerCase();
213
+ if (!LAYERSWAP_RECEIVERS.includes(receiver)) fail("evm-receiver", `the deposit forwards to ${clipName(receiver)}, not a pinned Layerswap receiver`, "policy_refused");
214
+ const id = depositId(swap.sequence);
215
+ const native = request.sourceContract === null;
216
+ const expected = native ? (0, import_viem.encodeFunctionData)({ abi: DEPOSITORY_ABI, functionName: "depositNative", args: [id, receiver] }) : (0, import_viem.encodeFunctionData)({ abi: DEPOSITORY_ABI, functionName: "depositERC20", args: [id, request.sourceContract, receiver, request.amount] });
217
+ if (data.toLowerCase() !== expected.toLowerCase()) {
218
+ fail("evm-calldata", native ? "the deposit is not depositNative for this swap" : "the deposit is not depositERC20 of exactly the input for this swap", "policy_refused");
219
+ }
220
+ const value = a.amountInBaseUnits ?? 0n;
221
+ if (native ? value !== request.amount : value !== 0n) fail("evm-value", native ? `the deposit sends ${value}, not the input ${request.amount}` : "an ERC-20 deposit must not attach native value", "policy_refused");
222
+ return {
223
+ to: LAYERSWAP_DEPOSITORY,
224
+ data: expected,
225
+ value: native ? request.amount : 0n,
226
+ chainId,
227
+ ...native ? {} : { approvalAddress: LAYERSWAP_DEPOSITORY },
228
+ receiver,
229
+ gasLimit: a.gasLimit
230
+ };
231
+ }
232
+ function checkTonDeposit(request, swap, a) {
233
+ commonAction(request, a);
234
+ if (a.type !== "transfer") fail("ton-action", "a TON deposit must be a transfer with the swap's comment", "unsupported_pair");
235
+ let payload;
236
+ try {
237
+ payload = JSON.parse(a.callData ?? "");
238
+ } catch {
239
+ return fail("ton-payload", "the TON deposit carries no comment payload", "upstream_unavailable");
240
+ }
241
+ const comment = typeof payload?.comment === "string" ? payload.comment : "";
242
+ if (comment !== swap.sequence.toString(10)) fail("ton-comment", "the TON deposit comment does not name this swap", "policy_refused");
243
+ if (request.sourceContract === null) {
244
+ if (a.amountInBaseUnits !== request.amount) fail("ton-amount", `the deposit sends ${a.amountInBaseUnits ?? "nothing"}, not the input ${request.amount}`, "policy_refused");
245
+ } else if (baseUnits(payload.amount) !== request.amount) {
246
+ fail("ton-amount", "the jetton transfer amount is not the input", "policy_refused");
247
+ }
248
+ if (!addressKey(request.sourceChain, a.toAddress)) fail("ton-target", "the TON deposit address is not a TON address", "upstream_unavailable");
249
+ return { to: a.toAddress, value: request.amount, memo: comment };
250
+ }
251
+ var TRON_ADDRESS = /^T[1-9A-HJ-NP-Za-km-z]{33}$/;
252
+ function checkTronDeposit(request, swap, a) {
253
+ commonAction(request, a);
254
+ if (a.type !== "transfer") fail("tron-action", "a Tron deposit must be a transfer with the swap's memo", "unsupported_pair");
255
+ if (!TRON_ADDRESS.test(a.toAddress)) fail("tron-target", "the Tron deposit address is not a Tron address", "upstream_unavailable");
256
+ const memo = a.callData ?? "";
257
+ if (memo !== swap.sequence.toString(10)) fail("tron-memo", "the Tron deposit memo does not name this swap", "policy_refused");
258
+ if (a.amountInBaseUnits !== request.amount) fail("tron-amount", `the deposit sends ${a.amountInBaseUnits ?? "nothing"}, not the input ${request.amount}`, "policy_refused");
259
+ return { to: a.toAddress, value: request.amount, memo };
260
+ }
261
+ var BTC_ADDRESS = /^(?:bc1[02-9ac-hj-np-z]{11,87}|[13][1-9A-HJ-NP-Za-km-z]{25,34})$/;
262
+ function checkBitcoinDeposit(request, a) {
263
+ commonAction(request, a);
264
+ if (a.type !== "manual_transfer") fail("btc-action", "a Bitcoin deposit must go to the swap's deposit address", "unsupported_pair");
265
+ if (a.callData !== null) fail("btc-action", "the Bitcoin deposit asks for a memo output, which this account does not write", "unsupported_pair");
266
+ if (!BTC_ADDRESS.test(a.toAddress)) fail("btc-target", "the deposit address is not a Bitcoin mainnet address", "upstream_unavailable");
267
+ if (a.amountInBaseUnits !== request.amount) fail("btc-amount", `the deposit asks for ${a.amountInBaseUnits ?? "nothing"} sat, not the input ${request.amount}`, "policy_refused");
268
+ return { to: a.toAddress, value: request.amount };
269
+ }
270
+ var B58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
271
+ function base58Decode(text) {
272
+ if (!/^[1-9A-HJ-NP-Za-km-z]{1,64}$/.test(text)) return null;
273
+ let n = 0n;
274
+ for (const ch of text) n = n * 58n + BigInt(B58.indexOf(ch));
275
+ const bytes = [];
276
+ while (n > 0n) {
277
+ bytes.unshift(Number(n & 0xffn));
278
+ n >>= 8n;
279
+ }
280
+ for (const ch of text) {
281
+ if (ch !== "1") break;
282
+ bytes.unshift(0);
283
+ }
284
+ return Uint8Array.from(bytes);
285
+ }
286
+ function base58Encode(bytes) {
287
+ let n = 0n;
288
+ for (const b of bytes) n = n << 8n | BigInt(b);
289
+ let out = "";
290
+ while (n > 0n) {
291
+ out = B58[Number(n % 58n)] + out;
292
+ n /= 58n;
293
+ }
294
+ for (const b of bytes) {
295
+ if (b !== 0) break;
296
+ out = `1${out}`;
297
+ }
298
+ return out;
299
+ }
300
+ var SOLANA_MAX_COMPUTE_UNITS = 4e5;
301
+ var SOLANA_MAX_MICROLAMPORTS = 5000000n;
302
+ var SOLANA_PROGRAMS = Object.freeze({
303
+ system: "11111111111111111111111111111111",
304
+ token: "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
305
+ token2022: "TokenzQdBNbLqP5VEhdkAS6EPFLC1PazqiGFRk8ZVvo",
306
+ ata: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
307
+ computeBudget: "ComputeBudget111111111111111111111111111111",
308
+ memoV1: "Memo1UhkJRfHyvLMcVucJwxXeuD728EqVDDwQDxFMNo",
309
+ memoV2: "MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"
310
+ });
311
+ function withSignatureSlot(bytes, messageOffset) {
312
+ const message = bytes.subarray(messageOffset);
313
+ const out = new Uint8Array(1 + 64 + message.length);
314
+ out[0] = 1;
315
+ out.set(message, 65);
316
+ return base64Encode(out);
317
+ }
318
+ var B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
319
+ function base64Encode(bytes) {
320
+ let out = "";
321
+ for (let i = 0; i < bytes.length; i += 3) {
322
+ const b0 = bytes[i], b1 = bytes[i + 1], b2 = bytes[i + 2];
323
+ const n = b0 << 16 | (b1 ?? 0) << 8 | (b2 ?? 0);
324
+ out += B64[n >> 18 & 63] + B64[n >> 12 & 63] + (b1 === void 0 ? "=" : B64[n >> 6 & 63]) + (b2 === void 0 ? "=" : B64[n & 63]);
325
+ }
326
+ return out;
327
+ }
328
+ var SOLANA_RENT_SYSVAR = "SysvarRent111111111111111111111111111111111";
329
+ function parseLegacyTransaction(bytes) {
330
+ let o = 0;
331
+ const u8 = () => {
332
+ if (o >= bytes.length) throw new Error("short");
333
+ return bytes[o++];
334
+ };
335
+ const cu16 = () => {
336
+ let v = 0;
337
+ for (let s = 0; s < 21; s += 7) {
338
+ const b = u8();
339
+ v |= (b & 127) << s;
340
+ if (!(b & 128)) return v;
341
+ }
342
+ throw new Error("bad length");
343
+ };
344
+ const take = (n) => {
345
+ if (n < 0 || o + n > bytes.length) throw new Error("short");
346
+ const out = bytes.subarray(o, o + n);
347
+ o += n;
348
+ return out;
349
+ };
350
+ try {
351
+ const numSignatures = cu16();
352
+ if (numSignatures > 8) return null;
353
+ take(64 * numSignatures);
354
+ const messageOffset = o;
355
+ const first = u8();
356
+ if (first & 128) return null;
357
+ const requiredSignatures = first;
358
+ u8();
359
+ u8();
360
+ const numKeys = cu16();
361
+ if (numKeys === 0 || numKeys > 64) return null;
362
+ const keys = [];
363
+ for (let i = 0; i < numKeys; i++) keys.push(base58Encode(take(32)));
364
+ take(32);
365
+ const numIx = cu16();
366
+ if (numIx > 16) return null;
367
+ const instructions = [];
368
+ for (let i = 0; i < numIx; i++) {
369
+ const p = u8();
370
+ const na = cu16();
371
+ const idx = Array.from(take(na));
372
+ const data = take(cu16());
373
+ if (p >= keys.length || idx.some((k) => k >= keys.length)) return null;
374
+ instructions.push({ program: keys[p], accounts: idx.map((k) => keys[k]), data: Uint8Array.from(data) });
375
+ }
376
+ if (o !== bytes.length) return null;
377
+ return { numSignatures, requiredSignatures, messageOffset, keys, instructions };
378
+ } catch {
379
+ return null;
380
+ }
381
+ }
382
+ function associatedTokenAccount(owner, mint, tokenProgram) {
383
+ const o = base58Decode(owner), m = base58Decode(mint), t = base58Decode(tokenProgram), ata = base58Decode(SOLANA_PROGRAMS.ata);
384
+ if (!o || !m || !t || !ata || o.length !== 32 || m.length !== 32 || t.length !== 32) return null;
385
+ const marker = new TextEncoder().encode("ProgramDerivedAddress");
386
+ for (let bump = 255; bump >= 0; bump--) {
387
+ const h = (0, import_sha2.sha256)(Uint8Array.from([...o, ...t, ...m, bump, ...ata, ...marker]));
388
+ let onCurve = true;
389
+ try {
390
+ import_ed25519.ed25519.Point.fromBytes(h);
391
+ } catch {
392
+ onCurve = false;
393
+ }
394
+ if (!onCurve) return base58Encode(h);
395
+ }
396
+ return null;
397
+ }
398
+ var u64le = (d, at) => {
399
+ let v = 0n;
400
+ for (let i = 7; i >= 0; i--) v = v << 8n | BigInt(d[at + i]);
401
+ return v;
402
+ };
403
+ function checkSolanaDeposit(request, swap, a) {
404
+ commonAction(request, a);
405
+ if (a.type !== "transfer") fail("solana-action", "a Solana deposit must be the prepared transfer", "unsupported_pair");
406
+ const bytes = a.callData ? (0, import_util3.base64Bytes)(a.callData) : null;
407
+ const tx = bytes ? parseLegacyTransaction(bytes) : null;
408
+ if (!tx) fail("solana-tx", "the Solana deposit is not a legacy transaction", "policy_refused");
409
+ const t = tx;
410
+ if (t.requiredSignatures !== 1 || t.keys[0] !== request.sender) fail("solana-signer", "the Solana deposit is not signed by this account alone", "policy_refused");
411
+ if (t.numSignatures !== 0 && t.numSignatures !== 1) fail("solana-signer", "the Solana deposit carries signature slots for other signers", "policy_refused");
412
+ const allowed = new Set(Object.values(SOLANA_PROGRAMS));
413
+ const foreign = t.instructions.find((ix) => !allowed.has(ix.program));
414
+ if (foreign) fail("solana-program", `the Solana deposit calls ${clipName(foreign.program)}`, "policy_refused");
415
+ const memos = t.instructions.filter((ix) => ix.program === SOLANA_PROGRAMS.memoV1 || ix.program === SOLANA_PROGRAMS.memoV2);
416
+ const want = new TextEncoder().encode(swap.sequence.toString(10));
417
+ const memo = memos[0]?.data;
418
+ if (memos.length !== 1 || !memo || memo.length !== want.length || memo.some((b, i) => b !== want[i])) {
419
+ fail("solana-memo", "the Solana deposit does not carry exactly this swap's memo", "policy_refused");
420
+ }
421
+ const native = request.sourceContract === null;
422
+ let transfers = 0;
423
+ let limits = 0, prices = 0, creates = 0;
424
+ let createdUnder = null, transferredUnder = null;
425
+ for (const ix of t.instructions) {
426
+ if (ix.program === SOLANA_PROGRAMS.memoV1 || ix.program === SOLANA_PROGRAMS.memoV2) continue;
427
+ if (ix.program === SOLANA_PROGRAMS.computeBudget) {
428
+ if (ix.data.length === 5 && ix.data[0] === 2) {
429
+ const units = ix.data[1] | ix.data[2] << 8 | ix.data[3] << 16 | ix.data[4] * 2 ** 24;
430
+ if (++limits > 1 || units > SOLANA_MAX_COMPUTE_UNITS) fail("solana-compute", "the Solana deposit sets a compute limit above a transfer's", "policy_refused");
431
+ } else if (ix.data.length === 9 && ix.data[0] === 3) {
432
+ if (++prices > 1 || u64le(ix.data, 1) > SOLANA_MAX_MICROLAMPORTS) fail("solana-compute", "the Solana deposit sets a priority fee above the cap", "policy_refused");
433
+ } else {
434
+ fail("solana-compute", "the Solana deposit sets an unexpected compute-budget instruction", "policy_refused");
435
+ }
436
+ continue;
437
+ }
438
+ if (ix.program === SOLANA_PROGRAMS.system) {
439
+ if (!native || ix.data.length !== 12 || ix.data[0] !== 2 || ix.data[1] !== 0 || ix.data[2] !== 0 || ix.data[3] !== 0) fail("solana-system", "the Solana deposit runs an unexpected system instruction", "policy_refused");
440
+ if (ix.accounts[0] !== request.sender || ix.accounts[1] !== a.toAddress) fail("solana-transfer", "the SOL transfer is not from this account to the deposit address", "policy_refused");
441
+ if (u64le(ix.data, 4) !== request.amount) fail("solana-amount", "the SOL transfer is not exactly the input", "policy_refused");
442
+ transfers++;
443
+ continue;
444
+ }
445
+ if (ix.program === SOLANA_PROGRAMS.ata) {
446
+ if (native || !(ix.data.length === 0 || ix.data.length === 1 && (ix.data[0] === 0 || ix.data[0] === 1))) fail("solana-ata", "the Solana deposit runs an unexpected account instruction", "policy_refused");
447
+ const program = ix.accounts[5];
448
+ if (++creates > 1 || ix.accounts.length !== 6 && !(ix.accounts.length === 7 && ix.accounts[6] === SOLANA_RENT_SYSVAR) || ix.accounts[4] !== SOLANA_PROGRAMS.system || program !== SOLANA_PROGRAMS.token && program !== SOLANA_PROGRAMS.token2022) {
449
+ fail("solana-ata", "the Solana deposit runs an unexpected account instruction", "policy_refused");
450
+ }
451
+ const owner2 = ix.accounts[2];
452
+ const created = owner2 === request.sender || owner2 === a.toAddress ? associatedTokenAccount(owner2, request.sourceContract, program) : null;
453
+ if (ix.accounts[0] !== request.sender || ix.accounts[3] !== request.sourceContract || !created || ix.accounts[1] !== created) {
454
+ fail("solana-ata", "the Solana deposit creates an account that is neither this account's nor the deposit address's", "policy_refused");
455
+ }
456
+ createdUnder = program;
457
+ continue;
458
+ }
459
+ if (native) fail("solana-token", "a SOL deposit runs a token instruction", "policy_refused");
460
+ const checked = ix.data.length === 10 && ix.data[0] === 12;
461
+ const plain = ix.data.length === 9 && ix.data[0] === 3;
462
+ if (!checked && !plain) fail("solana-token", "the Solana deposit runs an unexpected token instruction", "policy_refused");
463
+ const [src, dst, owner] = checked ? [ix.accounts[0], ix.accounts[2], ix.accounts[3]] : [ix.accounts[0], ix.accounts[1], ix.accounts[2]];
464
+ if (checked && (ix.accounts[1] !== request.sourceContract || ix.data[9] !== request.sourceDecimals)) fail("solana-token", "the token transfer is not of the source mint", "policy_refused");
465
+ if (owner !== request.sender) fail("solana-token", "the token transfer is not authorized by this account", "policy_refused");
466
+ if (u64le(ix.data, 1) !== request.amount) fail("solana-amount", "the token transfer is not exactly the input", "policy_refused");
467
+ const own = associatedTokenAccount(request.sender, request.sourceContract, ix.program);
468
+ if (!own || src !== own) fail("solana-token", "the token transfer does not spend this account's own token account", "policy_refused");
469
+ const theirs = associatedTokenAccount(a.toAddress, request.sourceContract, ix.program);
470
+ if (!theirs || dst !== theirs) fail("solana-transfer", "the token transfer does not pay the deposit address's token account", "policy_refused");
471
+ transferredUnder = ix.program;
472
+ transfers++;
473
+ }
474
+ if (transfers !== 1) fail("solana-transfer", "the Solana deposit does not make exactly one transfer", "policy_refused");
475
+ if (createdUnder !== null && createdUnder !== transferredUnder) fail("solana-ata", "the Solana deposit creates the account under another token program than the transfer's", "policy_refused");
476
+ return { serializedTx: t.numSignatures === 1 ? a.callData : withSignatureSlot(bytes, t.messageOffset), depositTo: a.toAddress, value: native ? request.amount : 0n };
477
+ }
478
+
479
+ // src/pins.ts
480
+ var reasons = /* @__PURE__ */ new Map();
481
+ function noted(e) {
482
+ const text = e instanceof Error ? e.message : String(e);
483
+ const info = (0, import_errors3.refusalOf)(e);
484
+ if (info) {
485
+ reasons.delete(text);
486
+ reasons.set(text, info);
487
+ if (reasons.size > 256) reasons.delete(reasons.keys().next().value);
488
+ }
489
+ return text;
490
+ }
491
+ function layerswapVerifyQuote(params, quote) {
492
+ try {
493
+ const funding = fundingFor(params.sourceChain);
494
+ if (!funding) return "layerswap: the source chain has no funding method";
495
+ const source = (0, import_util4.sourceAssetOf)(params);
496
+ if (!source) return `layerswap: unknown source token ${params.sourceToken}`;
497
+ const dest = (0, import_swap2_assets.resolveAsset)(params.destChain, params.destToken);
498
+ const sender = params.sender ?? params.refundAddress ?? "";
499
+ const request = {
500
+ sourceChain: params.sourceChain,
501
+ destChain: params.destChain,
502
+ sourceContract: source.contract ?? null,
503
+ sourceDecimals: source.decimals,
504
+ destContract: dest ? dest.contract ?? null : params.destToken || null,
505
+ amount: BigInt(params.amountIn),
506
+ sender,
507
+ recipient: params.destAddress,
508
+ funding
509
+ };
510
+ const seq = quote.providerData?.sequence ?? "";
511
+ if (!/^[1-9]\d{0,15}$/.test(seq)) return "layerswap: the quote names no swap sequence number";
512
+ const record = { id: quote.intentId.replace(/^layerswap:/, ""), sequence: BigInt(seq), status: "user_transfer_pending" };
513
+ const action = (over) => ({
514
+ type: "transfer",
515
+ step: "deposit",
516
+ toAddress: "",
517
+ amountInBaseUnits: request.amount,
518
+ callData: null,
519
+ gasLimit: null,
520
+ network: { name: params.sourceChain.startsWith("eip155:") ? void 0 : { "solana:mainnet": "SOLANA_MAINNET", "ton:-239": "TON_MAINNET", "tron:728126428": "TRON_MAINNET", "bitcoin:mainnet": "BITCOIN_MAINNET" }[params.sourceChain], chain_id: params.sourceChain.startsWith("eip155:") ? params.sourceChain.slice(7) : void 0 },
521
+ tokenContract: request.sourceContract,
522
+ ...over
523
+ });
524
+ const tx = quote.txRequest;
525
+ if (funding === "depository") {
526
+ if (!tx?.to || !tx.data) return "layerswap: the quote carries no Depository call";
527
+ const e = checkEvmDeposit(request, record, action({ toAddress: tx.to, callData: tx.data, amountInBaseUnits: BigInt(tx.value ?? "0") }));
528
+ if (tx.data.toLowerCase() !== e.data.toLowerCase() || (tx.approvalAddress ?? "").toLowerCase() !== (e.approvalAddress ?? "")) return "layerswap: the quote's Depository call is not the one rebuilt";
529
+ return null;
530
+ }
531
+ if (params.sourceChain.startsWith("solana:")) {
532
+ if (!tx?.serializedTx) return "layerswap: the quote carries no Solana transaction";
533
+ checkSolanaDeposit(request, record, action({ toAddress: quote.providerData?.depositTo ?? "", callData: tx.serializedTx }));
534
+ return null;
535
+ }
536
+ if (tx) return "layerswap: a TON / Tron / Bitcoin deposit is a transfer to an address, not a transaction";
537
+ const memoAction = action({ toAddress: quote.depositAddress, callData: quote.depositMemo, type: funding === "deposit_address" ? "manual_transfer" : "transfer" });
538
+ if (params.sourceChain.startsWith("ton:")) {
539
+ checkTonDeposit(request, record, { ...memoAction, callData: JSON.stringify({ amount: String(request.amount), comment: quote.depositMemo }) });
540
+ } else if (params.sourceChain.startsWith("tron:")) {
541
+ checkTronDeposit(request, record, memoAction);
542
+ } else {
543
+ checkBitcoinDeposit(request, memoAction);
544
+ }
545
+ return null;
546
+ } catch (e) {
547
+ return noted(e);
548
+ }
549
+ }
550
+ var layerswapPins = Object.freeze({
551
+ callTargets: (chain) => fundingFor(chain) === "depository" ? [LAYERSWAP_DEPOSITORY] : [],
552
+ spenders: (chain) => fundingFor(chain) === "depository" ? [LAYERSWAP_DEPOSITORY] : [],
553
+ depositAddressRoutes: true,
554
+ verifyQuote: layerswapVerifyQuote,
555
+ reasonOf: (text) => reasons.get(text)
556
+ });
557
+ //# sourceMappingURL=pins.cjs.map