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