0xtrails 0.16.0 → 0.16.2

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/dist/recover.js CHANGED
@@ -1,51 +1,54 @@
1
- import { logger as q } from "./logger/index.js";
2
- import { getChainInfo as P, getPublicClient as F } from "./chains.js";
3
- import { normalizeAddress as x } from "./utils/address.js";
4
- import { isPositiveBigInt as Z } from "./utils/validation.js";
5
- import { getERC20TransferData as ee } from "./utils.js";
6
- import { attemptSwitchChain as ne } from "./chainSwitch.js";
7
- import { abortIntent as te } from "./intents.js";
8
- import { decodeGuestModuleEvents as $ } from "./decoders.js";
9
- import { WIDGET_REFRESH_INTERVAL as re, useTokenBalancesForMultipleAccounts as oe } from "./query/balance.hooks.js";
10
- import { useTrailsClient as H } from "./trailsClient.js";
11
- import { useGetIntent as ae } from "./hooks/useGetIntent.js";
12
- import { useKeyMachineClient as se } from "./keyMachineClient.js";
13
- import { Constants as G, Erc6492 as ie, Payload as b, Signature as K } from "@0xsequence/wallet-primitives";
14
- import { parseSignature as ce, zeroAddress as R } from "viem";
15
- import { useCallback as I, useMemo as w } from "react";
16
- import { checkProviderHealth as X, signWithTimeout as J, wrapWalletClient as W } from "@0xtrails/wallet";
17
- import { AbiFunction as de, Address as f, Bytes as B } from "ox";
18
- async function le(e, t) {
19
- const r = await F(t).getCode({ address: e }), n = r !== void 0 && r !== "0x";
20
- return q.console.log("[refund] Address deployment check:", {
1
+ import { logger as k } from "./logger/index.js";
2
+ import { getChainInfo as L, getPublicClient as D } from "./chains.js";
3
+ import { normalizeAddress as B } from "./utils/address.js";
4
+ import { isPositiveBigInt as ae } from "./utils/validation.js";
5
+ import { getERC20TransferData as se } from "./utils.js";
6
+ import { attemptSwitchChain as ie } from "./chainSwitch.js";
7
+ import { abortIntent as ce } from "./intents.js";
8
+ import { decodeGuestModuleEvents as W } from "./decoders.js";
9
+ import { getChainIndexerUrl as de } from "./indexerClient.js";
10
+ import { WIDGET_REFRESH_INTERVAL as le, useTokenBalancesForMultipleAccounts as ue } from "./query/balance.hooks.js";
11
+ import { useTrails as fe } from "./widget/providers/TrailsProvider.js";
12
+ import { useTrailsClient as M } from "./trailsClient.js";
13
+ import { useGetIntent as he } from "./hooks/useGetIntent.js";
14
+ import { useKeyMachineClient as ge } from "./keyMachineClient.js";
15
+ import { Constants as O, Erc6492 as ye, Payload as R, Signature as U } from "@0xsequence/wallet-primitives";
16
+ import { zeroAddress as E } from "viem";
17
+ import { useCallback as A, useMemo as I } from "react";
18
+ import { checkProviderHealth as K, signWithTimeout as V, wrapWalletClient as G } from "@0xtrails/wallet";
19
+ import { AbiFunction as pe, Address as f, Bytes as H } from "ox";
20
+ import { SequenceIndexer as we } from "@0xsequence/indexer";
21
+ async function ve(e, t) {
22
+ const n = await D(t).getCode({ address: e }), r = n !== void 0 && n !== "0x";
23
+ return k.console.log("[refund] Address deployment check:", {
21
24
  address: e,
22
25
  chainId: t,
23
- codeLength: r?.length ?? 0,
24
- isDeployed: n
25
- }), n;
26
+ codeLength: n?.length ?? 0,
27
+ isDeployed: r
28
+ }), r;
26
29
  }
27
- async function Pe(e, t, r, n) {
30
+ async function en(e, t, n, r) {
28
31
  if (!e.account) throw new Error("Wallet client account is required for signing");
29
- await X(e);
30
- const o = b.toTyped(f.from(t), r, n), a = await J(e.signTypedData({
32
+ await K(e);
33
+ const o = R.toTyped(f.from(t), n, r), a = await V(e.signTypedData({
31
34
  account: e.account,
32
35
  ...o
33
36
  }), "Payload signature");
34
- return q.console.log("[refund] Signature received:", a), a;
37
+ return k.console.log("[refund] Signature received:", a), a;
35
38
  }
36
- async function Fe(e) {
37
- const { trailsClient: t, intentId: r, payload: n, intentAddress: o, signature: a } = e, s = n.calls[0];
39
+ async function nn(e) {
40
+ const { trailsClient: t, intentId: n, payload: r, intentAddress: o, signature: a } = e, s = r.calls[0];
38
41
  if (!s) throw new Error("Refund call not found in payload");
39
- if (f.isEqual(s.to, R)) throw new Error("Refund call 'to' address cannot be zero address");
40
- if (!o || f.isEqual(o, R)) throw new Error("Intent address cannot be zero address");
42
+ if (f.isEqual(s.to, E)) throw new Error("Refund call 'to' address cannot be zero address");
43
+ if (!o || f.isEqual(o, E)) throw new Error("Intent address cannot be zero address");
41
44
  const i = {
42
45
  kind: "v3_calls_payload",
43
46
  encoding: "hex",
44
- encoded: B.toHex(b.encode(n)),
47
+ encoded: H.toHex(R.encode(r)),
45
48
  intentAddress: o,
46
- chainId: n.type === "call" ? Number(e.chainId) : void 0
49
+ chainId: r.type === "call" ? Number(e.chainId) : void 0
47
50
  }, d = await t.buildIntentRecoveryTransaction({
48
- intentId: r,
51
+ intentId: n,
49
52
  payload: i,
50
53
  signature: a,
51
54
  intentAddress: o
@@ -55,12 +58,12 @@ async function Fe(e) {
55
58
  data: d.data
56
59
  };
57
60
  }
58
- function ue(e, t) {
61
+ function Ie(e, t) {
59
62
  if (e && typeof e == "object" && "kind" in e && e.kind === "v3_calls_payload") {
60
- const r = e;
61
- if (r.encoding !== "hex" || !r.encoded) throw new Error("PrepareIntentRecovery returned invalid canonical payload encoding");
62
- const n = r.intentAddress ? f.from(r.intentAddress) : t, o = b.decode(B.fromHex(r.encoded), n);
63
- if (!b.isCalls(o)) throw new Error("PrepareIntentRecovery returned non-call payload for recovery");
63
+ const n = e;
64
+ if (n.encoding !== "hex" || !n.encoded) throw new Error("PrepareIntentRecovery returned invalid canonical payload encoding");
65
+ const r = n.intentAddress ? f.from(n.intentAddress) : t, o = R.decode(H.fromHex(n.encoded), r);
66
+ if (!R.isCalls(o)) throw new Error("PrepareIntentRecovery returned non-call payload for recovery");
64
67
  const a = o.calls?.[0];
65
68
  if (!a) throw new Error("PrepareIntentRecovery returned invalid payload: refund call missing");
66
69
  return {
@@ -70,90 +73,90 @@ function ue(e, t) {
70
73
  }
71
74
  throw new Error("PrepareIntentRecovery returned unsupported payload format (expected v3_calls_payload)");
72
75
  }
73
- function V(e) {
76
+ function $(e) {
74
77
  if (!e?.tokens) return 0;
75
78
  let t = 0;
76
- for (const r of e.tokens) {
77
- const n = r.balanceUsd || 0;
78
- n > t && (t = n);
79
+ for (const n of e.tokens) {
80
+ const r = n.balanceUsd || 0;
81
+ r > t && (t = r);
79
82
  }
80
83
  return t;
81
84
  }
82
- function j(e) {
85
+ function z(e) {
83
86
  if (!e?.tokens) return 0n;
84
87
  let t = 0n;
85
- for (const r of e.tokens) {
86
- const n = BigInt(r.balance || "0");
87
- n > t && (t = n);
88
+ for (const n of e.tokens) {
89
+ const r = BigInt(n.balance || "0");
90
+ r > t && (t = r);
88
91
  }
89
92
  return t;
90
93
  }
91
- function N(e, t, r, n, o) {
92
- const a = V(t), s = V(r), i = j(t), d = j(r);
93
- let l = !1;
94
- o && (a > 0 && s > 0 ? l = s > a : s > 0 && a === 0 ? l = !0 : a > 0 && s === 0 ? l = !1 : l = d > i);
95
- const c = () => !e.originIntentAddress || !n ? null : {
96
- address: n,
94
+ function j(e, t, n, r, o) {
95
+ const a = $(t), s = $(n), i = z(t), d = z(n);
96
+ let c = !1;
97
+ o && (a > 0 && s > 0 ? c = s > a : s > 0 && a === 0 ? c = !0 : a > 0 && s === 0 ? c = !1 : c = d > i);
98
+ const u = () => !e.originIntentAddress || !r ? null : {
99
+ address: r,
97
100
  chainId: Number(e.originChainId || 0),
98
101
  intentAddress: f.from(e.originIntentAddress),
99
102
  tokenBalancesData: t,
100
103
  isOrigin: !0
101
104
  };
102
- if (l) {
103
- const h = e.destinationChainId;
104
- return !h || !e.destinationIntentAddress || !o ? c() : {
105
+ if (c) {
106
+ const l = e.destinationChainId;
107
+ return !l || !e.destinationIntentAddress || !o ? u() : {
105
108
  address: o,
106
- chainId: Number(h),
109
+ chainId: Number(l),
107
110
  intentAddress: f.from(e.destinationIntentAddress),
108
- tokenBalancesData: r,
111
+ tokenBalancesData: n,
109
112
  isOrigin: !1
110
113
  };
111
114
  }
112
- return c();
115
+ return u();
113
116
  }
114
- function L(e, t) {
115
- const r = (n) => {
116
- if (!n?.tokens) return !1;
117
- for (const o of n.tokens) if (Z(o.balance || "0")) return !0;
117
+ function X(e, t) {
118
+ const n = (r) => {
119
+ if (!r?.tokens) return !1;
120
+ for (const o of r.tokens) if (ae(o.balance || "0")) return !0;
118
121
  return !1;
119
122
  };
120
- return r(e) || r(t);
123
+ return n(e) || n(t);
121
124
  }
122
- function _(e) {
125
+ function Y(e) {
123
126
  if (!e?.tokenBalancesData?.tokens) return null;
124
- let t = null, r = 0n;
125
- for (const n of e.tokenBalancesData.tokens) {
126
- const o = BigInt(n.balance || "0");
127
- o > 0n && o > r && (r = o, t = n);
127
+ let t = null, n = 0n;
128
+ for (const r of e.tokenBalancesData.tokens) {
129
+ const o = BigInt(r.balance || "0");
130
+ o > 0n && o > n && (n = o, t = r);
128
131
  }
129
132
  return t;
130
133
  }
131
- function O(e) {
134
+ function J(e) {
132
135
  if (!e) throw new Error("Refund address is required. Provide refundToAddress or connect a wallet with a valid address");
133
- if (f.isEqual(e, R)) throw new Error("Refund address cannot be zero address");
136
+ if (f.isEqual(e, E)) throw new Error("Refund address cannot be zero address");
134
137
  }
135
- async function z(e) {
136
- const { txHash: t, chainId: r } = e, n = F(r);
138
+ async function Q(e) {
139
+ const { txHash: t, chainId: n } = e, r = D(n);
137
140
  try {
138
- if (await n.getTransaction({ hash: t })) try {
139
- const o = await n.getTransactionReceipt({ hash: t });
140
- return o.status === "reverted" ? { status: "fail" } : $(o).some((a) => a.type === "CallFailed") ? { status: "fail" } : { status: "success" };
141
+ if (await r.getTransaction({ hash: t })) try {
142
+ const o = await r.getTransactionReceipt({ hash: t });
143
+ return o.status === "reverted" ? { status: "fail" } : W(o).some((a) => a.type === "CallFailed") ? { status: "fail" } : { status: "success" };
141
144
  } catch {
142
145
  return { status: "pending" };
143
146
  }
144
147
  } catch {
145
148
  try {
146
- const o = await n.getTransactionReceipt({ hash: t });
147
- return o.status === "reverted" ? { status: "fail" } : $(o).some((a) => a.type === "CallFailed") ? { status: "fail" } : { status: "success" };
149
+ const o = await r.getTransactionReceipt({ hash: t });
150
+ return o.status === "reverted" ? { status: "fail" } : W(o).some((a) => a.type === "CallFailed") ? { status: "fail" } : { status: "success" };
148
151
  } catch {
149
152
  return { status: "pending" };
150
153
  }
151
154
  }
152
155
  return { status: "pending" };
153
156
  }
154
- function M(e) {
157
+ function Z(e) {
155
158
  if (!e) return null;
156
- const t = P(e.chainId);
159
+ const t = L(e.chainId);
157
160
  return {
158
161
  address: e.address,
159
162
  chainId: e.chainId,
@@ -162,46 +165,84 @@ function M(e) {
162
165
  tokenBalancesData: e.tokenBalancesData
163
166
  };
164
167
  }
165
- async function Q(e) {
166
- const { trailsClient: t, intentData: r, selected: n, effectiveRefundAddress: o, signature: a, payload: s } = e;
167
- O(o);
168
+ function me(e) {
169
+ if (e.length !== 132) throw new Error(`Invalid recovery signature length: expected 65 bytes, got ${(e.length - 2) / 2} bytes`);
170
+ const t = BigInt(`0x${e.slice(2, 66)}`), n = BigInt(`0x${e.slice(66, 130)}`), r = +`0x${e.slice(130)}`;
171
+ if (Number.isNaN(r)) throw new Error("Invalid recovery signature parity byte");
172
+ if (r === 0 || r === 27) return {
173
+ r: t,
174
+ s: n,
175
+ yParity: 0
176
+ };
177
+ if (r === 1 || r === 28) return {
178
+ r: t,
179
+ s: n,
180
+ yParity: 1
181
+ };
182
+ const o = r >= 27 ? (r - 27) % 2 : r % 2;
183
+ return k.console.warn("[recover] Normalizing non-standard signature parity", {
184
+ yParityOrV: r,
185
+ yParity: o
186
+ }), {
187
+ r: t,
188
+ s: n,
189
+ yParity: o
190
+ };
191
+ }
192
+ function Ae(e, t) {
193
+ if (e.length !== 132) return {
194
+ type: "erc1271",
195
+ address: t,
196
+ data: e
197
+ };
198
+ const n = me(e);
199
+ return {
200
+ type: "hash",
201
+ r: n.r,
202
+ s: n.s,
203
+ yParity: n.yParity
204
+ };
205
+ }
206
+ async function Te(e) {
207
+ const { trailsClient: t, intentData: n, selected: r, effectiveRefundAddress: o, signature: a, payload: s } = e;
208
+ J(o);
168
209
  const i = {
169
210
  kind: "v3_calls_payload",
170
211
  encoding: "hex",
171
- encoded: B.toHex(b.encode(s)),
172
- intentAddress: n.intentAddress,
173
- chainId: n.chainId
212
+ encoded: H.toHex(R.encode(s)),
213
+ intentAddress: r.intentAddress,
214
+ chainId: r.chainId
174
215
  }, d = await t.buildIntentRecoveryTransaction({
175
- intentId: r.intentId,
216
+ intentId: n.intentId,
176
217
  payload: i,
177
218
  signature: a,
178
- intentAddress: n.intentAddress,
219
+ intentAddress: r.intentAddress,
179
220
  refundToAddress: o
180
- }), l = Number(d.chainId), c = f.from(d.to);
181
- if (!c || f.isEqual(c, R)) throw new Error("Refund transaction 'to' address cannot be zero address");
182
- const h = P(l);
183
- if (!h) throw new Error(`Chain info not found for chain ID: ${l}`);
221
+ }), c = Number(d.chainId), u = f.from(d.to);
222
+ if (!u || f.isEqual(u, E)) throw new Error("Refund transaction 'to' address cannot be zero address");
223
+ const l = L(c);
224
+ if (!l) throw new Error(`Chain info not found for chain ID: ${c}`);
184
225
  return {
185
- to: c,
226
+ to: u,
186
227
  data: d.data,
187
- chainId: l,
188
- chain: h
228
+ chainId: c,
229
+ chain: l
189
230
  };
190
231
  }
191
- function fe() {
232
+ function be() {
192
233
  if (!globalThis.crypto?.getRandomValues) throw new Error("Secure random generator not available for recovery space");
193
234
  const e = new Uint8Array(20);
194
235
  globalThis.crypto.getRandomValues(e);
195
- const t = BigInt(`0x${Array.from(e, (r) => r.toString(16).padStart(2, "0")).join("")}`);
236
+ const t = BigInt(`0x${Array.from(e, (n) => n.toString(16).padStart(2, "0")).join("")}`);
196
237
  if (t <= 1n) throw new Error("Generated reserved recovery space");
197
238
  return t;
198
239
  }
199
- async function Y(e) {
200
- const { trailsClient: t, intentData: r, selectedIntentAddress: n, effectiveRefundAddress: o } = e, a = await t.prepareIntentRecovery({
201
- intentId: r.intentId,
202
- intentAddress: n.intentAddress,
240
+ async function ke(e) {
241
+ const { trailsClient: t, intentData: n, selectedIntentAddress: r, effectiveRefundAddress: o } = e, a = await t.prepareIntentRecovery({
242
+ intentId: n.intentId,
243
+ intentAddress: r.intentAddress,
203
244
  refundToAddress: o
204
- }), { decodedPayload: s, refundCall: i } = ue(a.payload, n.intentAddress);
245
+ }), { decodedPayload: s, refundCall: i } = Ie(a.payload, r.intentAddress);
205
246
  return {
206
247
  payload: s,
207
248
  refundCall: i,
@@ -209,55 +250,155 @@ async function Y(e) {
209
250
  typedData: a.typedData
210
251
  };
211
252
  }
212
- async function U(e) {
213
- const { walletClient: t, prepared: r } = e;
253
+ async function ee(e) {
254
+ const { walletClient: t, prepared: n } = e;
214
255
  if (!t.account) throw new Error("Wallet client account is required for signing");
215
- return await ne({
256
+ return await ie({
216
257
  walletClient: t,
217
- desiredChainId: r.chainId
218
- }), await X(t), {
219
- signature: await J(t.signTypedData({
258
+ desiredChainId: n.chainId
259
+ }), await K(t), {
260
+ signature: await V(t.signTypedData({
220
261
  account: t.account,
221
- ...r.typedData
262
+ ...n.typedData
222
263
  }), "Recovery signature"),
223
264
  signerAddress: t.account.address,
224
- payload: r.payload,
225
- refundCall: r.refundCall
265
+ payload: n.payload,
266
+ refundCall: n.refundCall
226
267
  };
227
268
  }
228
- async function D(e) {
229
- const { trailsWalletClient: t, recoverTx: r, trailsClient: n, intentId: o } = e;
269
+ var Re = 2e3, Ee = 6e4;
270
+ function Ce(e) {
271
+ return new Promise((t) => setTimeout(t, e));
272
+ }
273
+ function xe(e) {
274
+ return typeof e == "string" && /^0x[0-9a-fA-F]{64}$/.test(e);
275
+ }
276
+ function _(e) {
277
+ if (xe(e)) return e;
278
+ if (typeof e == "string" && /^[0-9a-fA-F]{64}$/.test(e)) return `0x${e}`;
279
+ }
280
+ function P(e, t) {
281
+ let n = e;
282
+ for (const r of t) {
283
+ if (!n || typeof n != "object" || !(r in n)) return;
284
+ n = n[r];
285
+ }
286
+ return n;
287
+ }
288
+ function ne(e) {
289
+ const t = _(e);
290
+ if (t) return t;
291
+ const n = e, r = [
292
+ n?.data?.txHash,
293
+ n?.data?.nativeReceipt && P(n.data.nativeReceipt, ["transactionHash"]),
294
+ n?.data?.nativeReceipt && P(n.data.nativeReceipt, ["hash"]),
295
+ n?.data?.receipt && P(n.data.receipt, ["txHash"]),
296
+ n?.data?.receipt && P(n.data.receipt, ["transactionHash"]),
297
+ n?.response && ne(n.response)
298
+ ];
299
+ for (const o of r) {
300
+ const a = _(o);
301
+ if (a) return a;
302
+ }
303
+ }
304
+ function te(e) {
305
+ if (!e || typeof e != "object") return;
306
+ const t = e;
307
+ return t.data?.metaTxHash ?? (t.response ? te(t.response) : void 0);
308
+ }
309
+ async function He(e) {
310
+ const { accountAddress: t, chainId: n, sentAtMs: r, pollingConfig: o } = e, a = o?.apiKey, s = o?.indexerUrl;
311
+ if (!a || !s) return null;
312
+ const i = de(n, s);
313
+ if (!i) return null;
314
+ const d = new we(i, a), c = D(n), u = Date.now();
315
+ for (; Date.now() - u < Ee; ) {
316
+ try {
317
+ const l = await d.getTransactionHistory({
318
+ filter: { accountAddress: B(t) },
319
+ includeMetadata: !1,
320
+ page: {
321
+ page: 1,
322
+ pageSize: 20
323
+ }
324
+ });
325
+ for (const v of l.transactions ?? []) {
326
+ const y = _(v.txnHash);
327
+ if (!y) continue;
328
+ const w = await c.getTransactionReceipt({ hash: y }).catch(() => null);
329
+ if (!w) continue;
330
+ const T = await c.getBlock({ blockNumber: w.blockNumber }).catch(() => null), g = T ? Number(T.timestamp) * 1e3 : 0;
331
+ if (!(g && g < r - 3e4))
332
+ return {
333
+ txHash: y,
334
+ receipt: w
335
+ };
336
+ }
337
+ } catch (l) {
338
+ k.console.warn("[recover] Failed to poll Sequence indexer for recovery tx hash", { error: l instanceof Error ? l.message : String(l) });
339
+ }
340
+ await Ce(Re);
341
+ }
342
+ return null;
343
+ }
344
+ async function Se(e) {
345
+ const { sendResult: t, trailsWalletClient: n, recoverTx: r, sentAtMs: o, pollingConfig: a } = e, s = D(r.chainId), i = ne(t);
346
+ if (i) return {
347
+ txHash: i,
348
+ receipt: await s.waitForTransactionReceipt({ hash: i })
349
+ };
350
+ const d = te(t);
351
+ k.console.warn("[recover] Recovery send returned no transaction hash; polling for mined transaction", { metaTxHash: d });
352
+ const c = await He({
353
+ accountAddress: n.account.address,
354
+ chainId: r.chainId,
355
+ sentAtMs: o,
356
+ pollingConfig: a
357
+ });
358
+ if (c) return c;
359
+ throw new Error("Recovery transaction was submitted, but the final transaction hash was not available yet. Please refresh transaction history in a moment.");
360
+ }
361
+ async function re(e) {
362
+ const { trailsWalletClient: t, recoverTx: n, trailsClient: r, intentId: o, pollingConfig: a, onTransactionSubmitted: s } = e;
230
363
  if (!t.account) throw new Error("Wallet client with account is required for recovery");
231
- const a = await t.sendTransaction({
364
+ const i = Date.now(), d = await t.sendTransaction({
232
365
  account: t.account,
233
- to: r.to,
234
- data: r.data,
235
- chain: r.chain
236
- }), s = await F(r.chainId).waitForTransactionReceipt({ hash: a });
237
- if (o && n) try {
238
- await te(n, o, r.chainId, a);
239
- } catch (i) {
240
- q.console.error("[recover] Failed to abort intent after recovery:", {
241
- error: i instanceof Error ? i.message : String(i),
366
+ to: n.to,
367
+ data: n.data,
368
+ chain: n.chain
369
+ });
370
+ s?.();
371
+ const { txHash: c, receipt: u } = await Se({
372
+ sendResult: d,
373
+ trailsWalletClient: t,
374
+ recoverTx: n,
375
+ sentAtMs: i,
376
+ pollingConfig: a
377
+ });
378
+ if (o && r) try {
379
+ await ce(r, o, n.chainId, c);
380
+ } catch (l) {
381
+ k.console.error("[recover] Failed to abort intent after recovery:", {
382
+ error: l instanceof Error ? l.message : String(l),
242
383
  intentId: o,
243
- chainId: r.chainId,
244
- txHash: a
384
+ chainId: n.chainId,
385
+ txHash: c
245
386
  });
246
387
  }
247
388
  return {
248
- txHash: a,
249
- receipt: s,
250
- chainId: r.chainId
389
+ txHash: c,
390
+ receipt: u,
391
+ chainId: n.chainId
251
392
  };
252
393
  }
253
- function he(e) {
254
- const { selectedIntentAddress: t, refundToAddress: r } = e, n = [], o = t.tokenBalancesData?.tokens ?? [];
394
+ function Be(e) {
395
+ const { selectedIntentAddress: t, refundToAddress: n } = e, r = [], o = t.tokenBalancesData?.tokens ?? [];
255
396
  for (const a of o) {
256
397
  const s = a.balance ?? "0", i = BigInt(s);
257
398
  if (i <= 0n) continue;
258
- const d = a.isNativeToken === !0, l = a.contractAddress;
259
- if (!d && !l) {
260
- q.console.warn("[recover] Skipping invalid recovery token: missing contractAddress for non-native token", {
399
+ const d = a.isNativeToken === !0, c = a.contractAddress;
400
+ if (!d && !c) {
401
+ k.console.warn("[recover] Skipping invalid recovery token: missing contractAddress for non-native token", {
261
402
  symbol: a.symbol || "unknown",
262
403
  balance: s,
263
404
  chainId: t.chainId,
@@ -265,10 +406,10 @@ function he(e) {
265
406
  });
266
407
  continue;
267
408
  }
268
- const c = l || R;
409
+ const u = c || E;
269
410
  if (d) {
270
- n.push({
271
- to: r,
411
+ r.push({
412
+ to: n,
272
413
  value: i,
273
414
  data: "0x",
274
415
  gasLimit: 0n,
@@ -278,12 +419,12 @@ function he(e) {
278
419
  });
279
420
  continue;
280
421
  }
281
- let h;
422
+ let l;
282
423
  try {
283
- h = f.from(c);
424
+ l = f.from(u);
284
425
  } catch {
285
- q.console.warn("[recover] Skipping invalid recovery token: malformed contractAddress", {
286
- contractAddress: c,
426
+ k.console.warn("[recover] Skipping invalid recovery token: malformed contractAddress", {
427
+ contractAddress: u,
287
428
  symbol: a.symbol || "unknown",
288
429
  balance: s,
289
430
  chainId: t.chainId,
@@ -291,67 +432,62 @@ function he(e) {
291
432
  });
292
433
  continue;
293
434
  }
294
- n.push({
295
- to: h,
435
+ r.push({
436
+ to: l,
296
437
  value: 0n,
297
- data: ee(r, i),
438
+ data: se(n, i),
298
439
  gasLimit: 0n,
299
440
  delegateCall: !1,
300
441
  onlyFallback: !1,
301
442
  behaviorOnError: "revert"
302
443
  });
303
444
  }
304
- if (n.length === 0) throw new Error("No recoverable token balances found on selected intent");
445
+ if (r.length === 0) throw new Error("No recoverable token balances found on selected intent");
305
446
  return {
306
447
  payload: {
307
448
  type: "call",
308
- space: fe(),
449
+ space: be(),
309
450
  nonce: 0n,
310
- calls: n
451
+ calls: r
311
452
  },
312
- refundCall: n[0]
453
+ refundCall: r[0]
313
454
  };
314
455
  }
315
- async function ge(e) {
316
- const { keyMachineClient: t, selected: r, signerAddress: n, signature: o, payload: a } = e, s = r.intentAddress, i = r.chainId, d = P(i);
456
+ async function Pe(e) {
457
+ const { keyMachineClient: t, selected: n, signerAddress: r, signature: o, payload: a } = e, s = n.intentAddress, i = n.chainId, d = L(i);
317
458
  if (!d) throw new Error(`Chain info not found for chain ID: ${i}`);
318
- const l = await le(s, i), c = await t.getDeploy(s);
319
- if (!c) throw new Error("Deploy config not found for intent address");
320
- const h = await t.getConfiguration(c.imageHash);
321
- if (!h) throw new Error("Configuration not found for intent address");
322
- const k = ce(o);
323
- let u = !1;
324
- const p = K.fillLeaves(h.topology, (A) => {
325
- if (f.isEqual(f.from(A.address), f.from(n)))
326
- return u = !0, {
327
- type: "hash",
328
- r: BigInt(k.r),
329
- s: BigInt(k.s),
330
- yParity: k.yParity
331
- };
459
+ const c = await ve(s, i), u = await t.getDeploy(s);
460
+ if (!u) throw new Error("Deploy config not found for intent address");
461
+ const l = await t.getConfiguration(u.imageHash);
462
+ if (!l) throw new Error("Configuration not found for intent address");
463
+ const v = Ae(o, r);
464
+ let y = !1;
465
+ const w = U.fillLeaves(l.topology, (h) => {
466
+ if (f.isEqual(f.from(h.address), f.from(r)))
467
+ return y = !0, v;
332
468
  });
333
- if (!u) throw new Error(`Signer ${n} is not an authorized signer for intent wallet ${s}. Connect the correct wallet to recover.`);
334
- const T = K.encodeSignature({
469
+ if (!y) throw new Error(`Signer ${r} is not an authorized signer for intent wallet ${s}. Connect the correct wallet to recover.`);
470
+ const T = U.encodeSignature({
335
471
  noChainId: !1,
336
472
  configuration: {
337
- threshold: h.threshold,
338
- checkpoint: h.checkpoint,
339
- checkpointer: h.checkpointer,
340
- topology: p
473
+ threshold: l.threshold,
474
+ checkpoint: l.checkpoint,
475
+ checkpointer: l.checkpointer,
476
+ topology: w
341
477
  }
342
- }), v = de.encodeData(G.EXECUTE, [B.toHex(b.encode(a)), B.toHex(T)]);
343
- if (!l) {
344
- const A = ie.deploy(c.imageHash, c.context), g = G.DefaultGuestAddress;
478
+ }), g = pe.encodeData(O.EXECUTE, [H.toHex(R.encode(a)), H.toHex(T)]);
479
+ if (!c) {
480
+ const h = ye.deploy(u.imageHash, u.context), x = O.DefaultGuestAddress;
345
481
  return {
346
- to: g,
347
- data: B.toHex(b.encode({
482
+ to: x,
483
+ data: H.toHex(R.encode({
348
484
  type: "call",
349
485
  space: 0n,
350
486
  nonce: 0n,
351
487
  calls: [{
352
- to: A.to,
488
+ to: h.to,
353
489
  value: 0n,
354
- data: A.data,
490
+ data: h.data,
355
491
  gasLimit: 0n,
356
492
  delegateCall: !1,
357
493
  onlyFallback: !1,
@@ -359,119 +495,126 @@ async function ge(e) {
359
495
  }, {
360
496
  to: s,
361
497
  value: 0n,
362
- data: v,
498
+ data: g,
363
499
  gasLimit: 0n,
364
500
  delegateCall: !1,
365
501
  onlyFallback: !1,
366
502
  behaviorOnError: "revert"
367
503
  }]
368
- }, g)),
504
+ }, x)),
369
505
  chainId: i,
370
506
  chain: d
371
507
  };
372
508
  }
373
509
  return {
374
510
  to: s,
375
- data: v,
511
+ data: g,
376
512
  chainId: i,
377
513
  chain: d
378
514
  };
379
515
  }
380
- function We({ intent: e, balancesByIntentAddress: t, walletClient: r, refundToAddress: n, keyMachineClient: o }) {
381
- const a = se(), s = o ?? a, i = H(), d = w(() => r ? W(r) : void 0, [r]), l = e?.originIntentAddress ? t[x(e.originIntentAddress)]?.tokenBalancesData : void 0, c = e?.destinationIntentAddress ? t[x(e.destinationIntentAddress)]?.tokenBalancesData : void 0, h = e?.originIntentAddress ? f.from(e.originIntentAddress) : null, k = e?.destinationIntentAddress ? f.from(e.destinationIntentAddress) : null, u = w(() => e ? N(e, l, c, h, k) : null, [
516
+ function tn({ intent: e, balancesByIntentAddress: t, walletClient: n, refundToAddress: r, keyMachineClient: o, onTransactionSubmitted: a }) {
517
+ const s = ge(), i = o ?? s, d = M(), { sequenceIndexerUrl: c, trailsApiKey: u } = fe(), l = I(() => ({
518
+ apiKey: u,
519
+ indexerUrl: c
520
+ }), [u, c]), v = I(() => n ? G(n) : void 0, [n]), y = e?.originIntentAddress ? t[B(e.originIntentAddress)]?.tokenBalancesData : void 0, w = e?.destinationIntentAddress ? t[B(e.destinationIntentAddress)]?.tokenBalancesData : void 0, T = e?.originIntentAddress ? f.from(e.originIntentAddress) : null, g = e?.destinationIntentAddress ? f.from(e.destinationIntentAddress) : null, h = I(() => e ? j(e, y, w, T, g) : null, [
382
521
  e,
383
- l,
384
- c,
385
- h,
386
- k
387
- ]), p = w(() => L(l, c), [l, c]), T = w(() => _(u), [u]), v = w(() => M(u), [u]), A = w(() => {
388
- const y = n || d?.account?.address;
389
- return !y || f.isEqual(y, R) ? null : y;
390
- }, [n, d?.account?.address]), g = I(async () => {
391
- if (!u) throw new Error("No intent address selected for recovery");
392
- O(A);
393
- const { payload: y, refundCall: S } = he({
394
- selectedIntentAddress: u,
395
- refundToAddress: A
522
+ y,
523
+ w,
524
+ T,
525
+ g
526
+ ]), x = I(() => X(y, w), [y, w]), b = I(() => Y(h), [h]), q = I(() => Z(h), [h]), S = I(() => {
527
+ const p = r || v?.account?.address;
528
+ return !p || f.isEqual(p, E) ? null : p;
529
+ }, [r, v?.account?.address]), C = A(async () => {
530
+ if (!h) throw new Error("No intent address selected for recovery");
531
+ J(S);
532
+ const { payload: p, refundCall: N } = Be({
533
+ selectedIntentAddress: h,
534
+ refundToAddress: S
396
535
  });
397
536
  return {
398
- payload: y,
399
- refundCall: S,
400
- chainId: u.chainId,
401
- typedData: b.toTyped(u.intentAddress, u.chainId, y)
537
+ payload: p,
538
+ refundCall: N,
539
+ chainId: h.chainId,
540
+ typedData: R.toTyped(h.intentAddress, h.chainId, p)
402
541
  };
403
- }, [u, A]), m = I(async (y) => {
404
- if (!d?.account) throw new Error("Wallet client with account is required for recovery");
405
- return U({
406
- walletClient: d,
407
- prepared: y ?? await g()
542
+ }, [h, S]), m = A(async (p) => {
543
+ if (!v?.account) throw new Error("Wallet client with account is required for recovery");
544
+ return ee({
545
+ walletClient: v,
546
+ prepared: p ?? await C()
408
547
  });
409
- }, [d, g]), C = I(async (y) => {
410
- if (!u) throw new Error("No intent address selected for recovery");
411
- return ge({
412
- keyMachineClient: s,
413
- selected: u,
414
- signerAddress: y.signerAddress,
415
- signature: y.signature,
416
- payload: y.payload
548
+ }, [v, C]), F = A(async (p) => {
549
+ if (!h) throw new Error("No intent address selected for recovery");
550
+ return Pe({
551
+ keyMachineClient: i,
552
+ selected: h,
553
+ signerAddress: p.signerAddress,
554
+ signature: p.signature,
555
+ payload: p.payload
417
556
  });
418
- }, [u, s]);
557
+ }, [h, i]);
419
558
  return {
420
559
  intent: e,
421
- hasIntentBalance: p,
422
- recoverToken: T,
423
- selectedRecoveryTarget: v,
560
+ hasIntentBalance: x,
561
+ recoverToken: b,
562
+ selectedRecoveryTarget: q,
424
563
  signPayload: m,
425
- getDataToSign: g,
426
- getRecoverTx: C,
427
- recover: I(async () => {
428
- if (!d?.account) throw new Error("Wallet client with account is required for recovery");
429
- const { signature: y, signerAddress: S, payload: E } = await m(await g());
430
- return D({
431
- trailsWalletClient: d,
432
- recoverTx: await C({
433
- signature: y,
434
- signerAddress: S,
435
- payload: E
564
+ getDataToSign: C,
565
+ getRecoverTx: F,
566
+ recover: A(async () => {
567
+ if (!v?.account) throw new Error("Wallet client with account is required for recovery");
568
+ const { signature: p, signerAddress: N, payload: oe } = await m(await C());
569
+ return re({
570
+ trailsWalletClient: v,
571
+ recoverTx: await F({
572
+ signature: p,
573
+ signerAddress: N,
574
+ payload: oe
436
575
  }),
437
- trailsClient: i,
438
- intentId: e?.intentId
576
+ trailsClient: d,
577
+ intentId: e?.intentId,
578
+ pollingConfig: l,
579
+ onTransactionSubmitted: a
439
580
  });
440
581
  }, [
441
- d,
442
- g,
443
- m,
582
+ v,
444
583
  C,
445
- i,
446
- e?.intentId
584
+ m,
585
+ F,
586
+ d,
587
+ e?.intentId,
588
+ l,
589
+ a
447
590
  ]),
448
- getRecoverStatus: I((y) => z(y), [])
591
+ getRecoverStatus: A((p) => Q(p), [])
449
592
  };
450
593
  }
451
- function Ne({ intentId: e, walletClient: t, refundToAddress: r }) {
452
- const n = H(), o = w(() => t ? W(t) : void 0, [t]), a = o?.account?.address, s = ye({
594
+ function rn({ intentId: e, walletClient: t, refundToAddress: n }) {
595
+ const r = M(), o = I(() => t ? G(t) : void 0, [t]), a = o?.account?.address, s = De({
453
596
  intentId: e,
454
597
  walletAddress: a,
455
- refundToAddress: r || a
456
- }), i = I(async () => {
598
+ refundToAddress: n || a
599
+ }), i = A(async () => {
457
600
  if (!o?.account) throw new Error("Intent data and wallet client with account required");
458
- return U({
601
+ return ee({
459
602
  walletClient: o,
460
603
  prepared: await s.getDataToSign()
461
604
  });
462
- }, [o, s]), d = I(async (c) => s.getRecoverTx({
463
- signature: c.signedHash,
464
- payload: c.payload
465
- }), [s]), l = I(async () => {
605
+ }, [o, s]), d = A(async (u) => s.getRecoverTx({
606
+ signature: u.signedHash,
607
+ payload: u.payload
608
+ }), [s]), c = A(async () => {
466
609
  if (!o?.account) throw new Error("Intent data and wallet client with account required");
467
- const { signature: c, payload: h } = await i();
468
- return D({
610
+ const { signature: u, payload: l } = await i();
611
+ return re({
469
612
  trailsWalletClient: o,
470
613
  recoverTx: await d({
471
- signedHash: c,
472
- payload: h
614
+ signedHash: u,
615
+ payload: l
473
616
  }),
474
- trailsClient: n,
617
+ trailsClient: r,
475
618
  intentId: e
476
619
  });
477
620
  }, [
@@ -479,159 +622,82 @@ function Ne({ intentId: e, walletClient: t, refundToAddress: r }) {
479
622
  d,
480
623
  o,
481
624
  e,
482
- n
625
+ r
483
626
  ]);
484
627
  return {
485
628
  ...s,
486
629
  signPayload: i,
487
630
  getRecoverTx: d,
488
- recover: l
631
+ recover: c
489
632
  };
490
633
  }
491
- function ye({ intentId: e, walletAddress: t, refundToAddress: r }) {
492
- const { intent: n, isLoading: o, error: a, refetch: s } = ae({
634
+ function De({ intentId: e, walletAddress: t, refundToAddress: n }) {
635
+ const { intent: r, isLoading: o, error: a, refetch: s } = he({
493
636
  intentId: e,
494
637
  enabled: !!e
495
- }), i = H(), d = n?.originIntentAddress ? f.from(n.originIntentAddress) : null, l = n?.destinationIntentAddress ? f.from(n.destinationIntentAddress) : null, { balancesByAccount: c, isLoading: h, error: k } = oe([d, l], { refetchInterval: re }), u = d ? c[x(d)]?.tokenBalancesData : void 0, p = l ? c[x(l)]?.tokenBalancesData : void 0, T = k, v = w(() => n ? N(n, u, p, d, l) : null, [
496
- n,
497
- u,
498
- p,
638
+ }), i = M(), d = r?.originIntentAddress ? f.from(r.originIntentAddress) : null, c = r?.destinationIntentAddress ? f.from(r.destinationIntentAddress) : null, { balancesByAccount: u, isLoading: l, error: v } = ue([d, c], { refetchInterval: le }), y = d ? u[B(d)]?.tokenBalancesData : void 0, w = c ? u[B(c)]?.tokenBalancesData : void 0, T = v, g = I(() => r ? j(r, y, w, d, c) : null, [
639
+ r,
640
+ y,
641
+ w,
499
642
  d,
500
- l
501
- ]), A = w(() => L(u, p), [u, p]), g = w(() => _(v), [v]), m = w(() => {
502
- const E = r ?? t;
503
- return !E || f.isEqual(E, R) ? null : E;
504
- }, [r, t]), C = I(async () => {
505
- if (!n) throw new Error("Intent data required");
506
- if (!m) throw new Error("Refund address required. Provide refundToAddress or walletAddress");
507
- if (f.isEqual(m, R)) throw new Error("Refund address cannot be zero address");
508
- if (!v) throw new Error("No intent address selected for recovery");
509
- return Y({
643
+ c
644
+ ]), h = I(() => X(y, w), [y, w]), x = I(() => Y(g), [g]), b = I(() => {
645
+ const m = n ?? t;
646
+ return !m || f.isEqual(m, E) ? null : m;
647
+ }, [n, t]), q = A(async () => {
648
+ if (!r) throw new Error("Intent data required");
649
+ if (!b) throw new Error("Refund address required. Provide refundToAddress or walletAddress");
650
+ if (f.isEqual(b, E)) throw new Error("Refund address cannot be zero address");
651
+ if (!g) throw new Error("No intent address selected for recovery");
652
+ return ke({
510
653
  trailsClient: i,
511
- intentData: n,
512
- selectedIntentAddress: v,
513
- effectiveRefundAddress: m
654
+ intentData: r,
655
+ selectedIntentAddress: g,
656
+ effectiveRefundAddress: b
514
657
  });
515
658
  }, [
516
- n,
517
- v,
518
- m,
659
+ r,
660
+ g,
661
+ b,
519
662
  i
520
- ]), y = I(async (E) => {
521
- if (!n) throw new Error("Intent data required");
522
- if (!v) throw new Error("No intent address selected for recovery");
523
- return Q({
663
+ ]), S = A(async (m) => {
664
+ if (!r) throw new Error("Intent data required");
665
+ if (!g) throw new Error("No intent address selected for recovery");
666
+ return Te({
524
667
  trailsClient: i,
525
- intentData: n,
526
- selected: v,
527
- effectiveRefundAddress: m,
528
- signature: E.signature,
529
- payload: E.payload
668
+ intentData: r,
669
+ selected: g,
670
+ effectiveRefundAddress: b,
671
+ signature: m.signature,
672
+ payload: m.payload
530
673
  });
531
674
  }, [
532
- n,
533
- v,
534
- m,
675
+ r,
676
+ g,
677
+ b,
535
678
  i
536
- ]), S = I((E) => z(E), []);
679
+ ]), C = A((m) => Q(m), []);
537
680
  return {
538
- intent: n,
681
+ intent: r,
539
682
  isLoadingIntent: o,
540
- isLoadingBalances: h,
683
+ isLoadingBalances: l,
541
684
  intentError: a,
542
685
  balancesError: T,
543
- hasIntentBalance: A,
544
- recoverToken: g,
545
- selectedRecoveryTarget: w(() => M(v), [v]),
546
- refetchIntent: s,
547
- getDataToSign: C,
548
- getRecoverTx: y,
549
- getRecoverStatus: S
550
- };
551
- }
552
- function Le({ intent: e, balancesByIntentAddress: t, walletClient: r, refundToAddress: n }) {
553
- const o = H(), a = w(() => r ? W(r) : void 0, [r]), s = e?.originIntentAddress ? t[x(e.originIntentAddress)]?.tokenBalancesData : void 0, i = e?.destinationIntentAddress ? t[x(e.destinationIntentAddress)]?.tokenBalancesData : void 0, d = e?.originIntentAddress ? f.from(e.originIntentAddress) : null, l = e?.destinationIntentAddress ? f.from(e.destinationIntentAddress) : null, c = w(() => e ? N(e, s, i, d, l) : null, [
554
- e,
555
- s,
556
- i,
557
- d,
558
- l
559
- ]), h = w(() => L(s, i), [s, i]), k = w(() => _(c), [c]), u = w(() => {
560
- const g = n || a?.account?.address;
561
- return !g || f.isEqual(g, R) ? null : g;
562
- }, [n, a?.account?.address]), p = I(async () => {
563
- if (!e || !a?.account) throw new Error("Intent data and wallet client with account required");
564
- if (O(u), !c) throw new Error("No intent address selected for recovery");
565
- return U({
566
- walletClient: a,
567
- prepared: await Y({
568
- trailsClient: o,
569
- intentData: e,
570
- selectedIntentAddress: c,
571
- effectiveRefundAddress: u
572
- })
573
- });
574
- }, [
575
- e,
576
- a,
577
- c,
578
- u,
579
- o
580
- ]), T = I(async (g) => {
581
- if (!e) throw new Error("Intent data required");
582
- if (!c) throw new Error("No intent address selected for recovery");
583
- return Q({
584
- trailsClient: o,
585
- intentData: e,
586
- selected: c,
587
- effectiveRefundAddress: u,
588
- signature: g.signedHash,
589
- payload: g.payload
590
- });
591
- }, [
592
- e,
593
- c,
594
- u,
595
- o
596
- ]), v = I(async () => {
597
- if (!a?.account) throw new Error("Wallet client with account is required for recovery");
598
- if (!e) throw new Error("Intent data is required for recovery");
599
- const { signature: g, payload: m } = await p(), { txHash: C } = await D({
600
- trailsWalletClient: a,
601
- recoverTx: await T({
602
- signedHash: g,
603
- payload: m
604
- }),
605
- trailsClient: o,
606
- intentId: e.intentId
607
- });
608
- return { txHash: C };
609
- }, [
610
- a,
611
- e,
612
- p,
613
- T,
614
- o
615
- ]), A = I((g) => z(g), []);
616
- return {
617
- intent: e,
618
686
  hasIntentBalance: h,
619
- recoverToken: k,
620
- selectedRecoveryTarget: w(() => M(c), [c]),
621
- refetchIntent: () => {
622
- },
623
- signPayload: p,
624
- getRecoverTx: T,
625
- recover: v,
626
- getRecoverStatus: A
687
+ recoverToken: x,
688
+ selectedRecoveryTarget: I(() => Z(g), [g]),
689
+ refetchIntent: s,
690
+ getDataToSign: q,
691
+ getRecoverTx: S,
692
+ getRecoverStatus: C
627
693
  };
628
694
  }
629
695
  export {
630
- Fe as buildRefundTransactionWithSignature,
631
- le as isAddressDeployed,
632
- Pe as signPayload,
633
- Ne as useIntentRecover,
634
- ye as useIntentRecoverWithAddress,
635
- Le as useIntentRecoverWithBalances,
636
- We as useKeymachineIntentRecovery
696
+ nn as buildRefundTransactionWithSignature,
697
+ ve as isAddressDeployed,
698
+ Ae as recoverySignatureForSigner,
699
+ en as signPayload,
700
+ rn as useIntentRecover,
701
+ De as useIntentRecoverWithAddress,
702
+ tn as useKeymachineIntentRecovery
637
703
  };