@wagmi/core 3.4.3 → 3.4.4

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.
@@ -0,0 +1,894 @@
1
+ import * as Bytes from 'ox/Bytes';
2
+ import * as PublicKey from 'ox/PublicKey';
3
+ import * as Secp256k1 from 'ox/Secp256k1';
4
+ import { TokenId } from 'ox/tempo';
5
+ import { encodeAbiParameters, encodeFunctionData, zeroHash, } from 'viem';
6
+ import { readContract as viem_readContract, sendTransaction as viem_sendTransaction, sendTransactionSync as viem_sendTransactionSync, } from 'viem/actions';
7
+ import { Abis, Actions } from 'viem/tempo';
8
+ import { Abis as ZoneAbis } from 'viem/tempo/zones';
9
+ import { parseAccount } from 'viem/utils';
10
+ import { getConnectorClient } from '../../actions/getConnectorClient.js';
11
+ import { filterQueryOptions } from './utils.js';
12
+ /**
13
+ * Gets information about the currently stored zone authorization token.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { createConfig } from '@wagmi/core'
18
+ * import { Actions, dangerous_secp256k1 } from '@wagmi/core/tempo'
19
+ * import { Account } from 'viem/tempo'
20
+ * import { http as zoneHttp, zone } from 'viem/tempo/zones'
21
+ *
22
+ * const zoneChain = zone(7)
23
+ * const account = Account.fromSecp256k1('0x...')
24
+ * const config = createConfig({
25
+ * chains: [zoneChain],
26
+ * connectors: [dangerous_secp256k1({ account })],
27
+ * transports: {
28
+ * [zoneChain.id]: zoneHttp(),
29
+ * },
30
+ * })
31
+ *
32
+ * await Actions.zone.signAuthorizationToken(config, {
33
+ * chainId: zoneChain.id,
34
+ * })
35
+ *
36
+ * const info = await Actions.zone.getAuthorizationTokenInfo(config, {
37
+ * chainId: zoneChain.id,
38
+ * })
39
+ *
40
+ * console.log(info.expiresAt)
41
+ * ```
42
+ *
43
+ * @param config - Config.
44
+ * @param parameters - Parameters.
45
+ * @returns The authorization token info.
46
+ */
47
+ export function getAuthorizationTokenInfo(config, parameters) {
48
+ const client = config.getClient({ chainId: parameters.chainId });
49
+ return Actions.zone.getAuthorizationTokenInfo(client);
50
+ }
51
+ (function (getAuthorizationTokenInfo) {
52
+ function queryKey(parameters) {
53
+ return [
54
+ 'getAuthorizationTokenInfo',
55
+ filterQueryOptions(parameters),
56
+ ];
57
+ }
58
+ getAuthorizationTokenInfo.queryKey = queryKey;
59
+ function queryOptions(config, parameters) {
60
+ const { query, ...rest } = parameters;
61
+ return {
62
+ ...query,
63
+ enabled: Boolean(query?.enabled ?? true),
64
+ queryKey: queryKey(rest),
65
+ async queryFn(context) {
66
+ const [, parameters] = context.queryKey;
67
+ return await getAuthorizationTokenInfo(config, parameters);
68
+ },
69
+ };
70
+ }
71
+ getAuthorizationTokenInfo.queryOptions = queryOptions;
72
+ })(getAuthorizationTokenInfo || (getAuthorizationTokenInfo = {}));
73
+ /**
74
+ * Gets deposit processing status for a Tempo block number.
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * import { createConfig } from '@wagmi/core'
79
+ * import { Actions, dangerous_secp256k1 } from '@wagmi/core/tempo'
80
+ * import { Account } from 'viem/tempo'
81
+ * import { http as zoneHttp, zone } from 'viem/tempo/zones'
82
+ *
83
+ * const zoneChain = zone(7)
84
+ * const account = Account.fromSecp256k1('0x...')
85
+ * const config = createConfig({
86
+ * chains: [zoneChain],
87
+ * connectors: [dangerous_secp256k1({ account })],
88
+ * transports: {
89
+ * [zoneChain.id]: zoneHttp(),
90
+ * },
91
+ * })
92
+ *
93
+ * await Actions.zone.signAuthorizationToken(config, {
94
+ * chainId: zoneChain.id,
95
+ * })
96
+ *
97
+ * const status = await Actions.zone.getDepositStatus(config, {
98
+ * chainId: zoneChain.id,
99
+ * tempoBlockNumber: 42n,
100
+ * })
101
+ *
102
+ * console.log(status.processed)
103
+ * ```
104
+ *
105
+ * @param config - Config.
106
+ * @param parameters - Parameters.
107
+ * @returns The deposit status.
108
+ */
109
+ export function getDepositStatus(config, parameters) {
110
+ const { chainId, ...rest } = parameters;
111
+ const client = config.getClient({ chainId });
112
+ return Actions.zone.getDepositStatus(client, rest);
113
+ }
114
+ (function (getDepositStatus) {
115
+ function queryKey(parameters) {
116
+ return ['getDepositStatus', filterQueryOptions(parameters)];
117
+ }
118
+ getDepositStatus.queryKey = queryKey;
119
+ function queryOptions(config, parameters) {
120
+ const { query, ...rest } = parameters;
121
+ return {
122
+ ...query,
123
+ enabled: Boolean(rest.tempoBlockNumber !== undefined && (query?.enabled ?? true)),
124
+ queryKey: queryKey(rest),
125
+ async queryFn(context) {
126
+ const [, { tempoBlockNumber, ...parameters }] = context.queryKey;
127
+ if (tempoBlockNumber === undefined)
128
+ throw new Error('tempoBlockNumber is required.');
129
+ return await getDepositStatus(config, {
130
+ ...parameters,
131
+ tempoBlockNumber,
132
+ });
133
+ },
134
+ };
135
+ }
136
+ getDepositStatus.queryOptions = queryOptions;
137
+ })(getDepositStatus || (getDepositStatus = {}));
138
+ /**
139
+ * Gets the withdrawal fee for a given gas limit.
140
+ *
141
+ * @example
142
+ * ```ts
143
+ * import { createConfig } from '@wagmi/core'
144
+ * import { Actions } from '@wagmi/core/tempo'
145
+ * import { http as zoneHttp, zone } from 'viem/tempo/zones'
146
+ *
147
+ * const zoneChain = zone(7)
148
+ * const config = createConfig({
149
+ * chains: [zoneChain],
150
+ * transports: {
151
+ * [zoneChain.id]: zoneHttp(),
152
+ * },
153
+ * })
154
+ *
155
+ * const fee = await Actions.zone.getWithdrawalFee(config, {
156
+ * chainId: zoneChain.id,
157
+ * gas: 21_000n,
158
+ * })
159
+ *
160
+ * console.log(fee)
161
+ * ```
162
+ *
163
+ * @param config - Config.
164
+ * @param parameters - Parameters.
165
+ * @returns The withdrawal fee.
166
+ */
167
+ export function getWithdrawalFee(config, parameters) {
168
+ const { chainId, ...rest } = parameters;
169
+ const client = config.getClient({ chainId });
170
+ return Actions.zone.getWithdrawalFee(client, rest);
171
+ }
172
+ (function (getWithdrawalFee) {
173
+ function queryKey(parameters) {
174
+ return ['getWithdrawalFee', filterQueryOptions(parameters)];
175
+ }
176
+ getWithdrawalFee.queryKey = queryKey;
177
+ function queryOptions(config, parameters) {
178
+ const { query, ...rest } = parameters;
179
+ return {
180
+ ...query,
181
+ enabled: Boolean(query?.enabled ?? true),
182
+ queryKey: queryKey(rest),
183
+ async queryFn(context) {
184
+ const [, parameters] = context.queryKey;
185
+ return await getWithdrawalFee(config, parameters);
186
+ },
187
+ };
188
+ }
189
+ getWithdrawalFee.queryOptions = queryOptions;
190
+ })(getWithdrawalFee || (getWithdrawalFee = {}));
191
+ /**
192
+ * Gets the current zone metadata.
193
+ *
194
+ * @example
195
+ * ```ts
196
+ * import { createConfig } from '@wagmi/core'
197
+ * import { Actions } from '@wagmi/core/tempo'
198
+ * import { http as zoneHttp, zone } from 'viem/tempo/zones'
199
+ *
200
+ * const zoneChain = zone(7)
201
+ * const config = createConfig({
202
+ * chains: [zoneChain],
203
+ * transports: {
204
+ * [zoneChain.id]: zoneHttp(),
205
+ * },
206
+ * })
207
+ *
208
+ * const info = await Actions.zone.getZoneInfo(config, {
209
+ * chainId: zoneChain.id,
210
+ * })
211
+ *
212
+ * console.log(info.zoneId)
213
+ * ```
214
+ *
215
+ * @param config - Config.
216
+ * @param parameters - Parameters.
217
+ * @returns The zone metadata.
218
+ */
219
+ export function getZoneInfo(config, parameters) {
220
+ const client = config.getClient({ chainId: parameters.chainId });
221
+ return Actions.zone.getZoneInfo(client);
222
+ }
223
+ (function (getZoneInfo) {
224
+ function queryKey(parameters) {
225
+ return ['getZoneInfo', filterQueryOptions(parameters)];
226
+ }
227
+ getZoneInfo.queryKey = queryKey;
228
+ function queryOptions(config, parameters) {
229
+ const { query, ...rest } = parameters;
230
+ return {
231
+ ...query,
232
+ enabled: Boolean(query?.enabled ?? true),
233
+ queryKey: queryKey(rest),
234
+ async queryFn(context) {
235
+ const [, parameters] = context.queryKey;
236
+ return await getZoneInfo(config, parameters);
237
+ },
238
+ };
239
+ }
240
+ getZoneInfo.queryOptions = queryOptions;
241
+ })(getZoneInfo || (getZoneInfo = {}));
242
+ /**
243
+ * Signs and stores a zone authorization token for the configured zone transport.
244
+ *
245
+ * @example
246
+ * ```ts
247
+ * import { createConfig } from '@wagmi/core'
248
+ * import { Actions, dangerous_secp256k1 } from '@wagmi/core/tempo'
249
+ * import { Account } from 'viem/tempo'
250
+ * import { http as zoneHttp, zone } from 'viem/tempo/zones'
251
+ *
252
+ * const zoneChain = zone(7)
253
+ * const account = Account.fromSecp256k1('0x...')
254
+ * const config = createConfig({
255
+ * chains: [zoneChain],
256
+ * connectors: [dangerous_secp256k1({ account })],
257
+ * transports: {
258
+ * [zoneChain.id]: zoneHttp(),
259
+ * },
260
+ * })
261
+ *
262
+ * const result = await Actions.zone.signAuthorizationToken(config, {
263
+ * chainId: zoneChain.id,
264
+ * })
265
+ *
266
+ * console.log(result.token)
267
+ * ```
268
+ *
269
+ * @param config - Config.
270
+ * @param parameters - Parameters.
271
+ * @returns The authentication payload and serialized token.
272
+ */
273
+ export async function signAuthorizationToken(config, parameters) {
274
+ const { account, chainId, connector, ...rest } = parameters;
275
+ const client = await getZoneWalletClient(config, {
276
+ account,
277
+ chainId,
278
+ connector,
279
+ });
280
+ return Actions.zone.signAuthorizationToken(client, rest);
281
+ }
282
+ /**
283
+ * Deposits tokens into a zone on the parent Tempo chain.
284
+ *
285
+ * @example
286
+ * ```ts
287
+ * import { createConfig, http } from '@wagmi/core'
288
+ * import { tempoModerato } from '@wagmi/core/chains'
289
+ * import { Actions } from '@wagmi/core/tempo'
290
+ *
291
+ * const config = createConfig({
292
+ * chains: [tempoModerato],
293
+ * transports: {
294
+ * [tempoModerato.id]: http(),
295
+ * },
296
+ * })
297
+ *
298
+ * const hash = await Actions.zone.deposit(config, {
299
+ * amount: 1_000_000n,
300
+ * token: '0x20c0000000000000000000000000000000000001',
301
+ * zoneId: 7,
302
+ * })
303
+ * ```
304
+ *
305
+ * @param config - Config.
306
+ * @param parameters - Parameters.
307
+ * @returns Transaction hash.
308
+ */
309
+ export async function deposit(config, parameters) {
310
+ const { account, chainId, connector, ...rest } = parameters;
311
+ const client = await getConnectorClient(config, {
312
+ account,
313
+ assertChainId: false,
314
+ chainId,
315
+ connector,
316
+ });
317
+ const resolvedChainId = chainId ?? client.chain?.id;
318
+ if (!resolvedChainId)
319
+ throw new Error('`chainId` is required.');
320
+ const account_ = account ?? client.account;
321
+ if (!account_)
322
+ throw new Error('`account` is required.');
323
+ const accountAddress = parseAccount(account_).address;
324
+ const { amount, memo = zeroHash, recipient = accountAddress, token, zoneId, ...tx } = rest;
325
+ const { address: portalAddress } = resolvePortal(config, resolvedChainId, zoneId);
326
+ const tokenAddress = TokenId.toAddress(token);
327
+ return viem_sendTransaction(client, {
328
+ ...tx,
329
+ calls: [
330
+ {
331
+ data: encodeFunctionData({
332
+ abi: Abis.tip20,
333
+ functionName: 'approve',
334
+ args: [portalAddress, amount],
335
+ }),
336
+ to: tokenAddress,
337
+ },
338
+ {
339
+ data: encodeFunctionData({
340
+ abi: ZoneAbis.zonePortal,
341
+ functionName: 'deposit',
342
+ args: [tokenAddress, recipient, amount, memo],
343
+ }),
344
+ to: portalAddress,
345
+ },
346
+ ],
347
+ });
348
+ }
349
+ /**
350
+ * Deposits tokens into a zone on the parent Tempo chain.
351
+ *
352
+ * Note: This is a synchronous action that waits for the transaction to
353
+ * be included on a block before returning a response.
354
+ *
355
+ * @example
356
+ * ```ts
357
+ * import { createConfig, http } from '@wagmi/core'
358
+ * import { tempoModerato } from '@wagmi/core/chains'
359
+ * import { Actions } from '@wagmi/core/tempo'
360
+ *
361
+ * const config = createConfig({
362
+ * chains: [tempoModerato],
363
+ * transports: {
364
+ * [tempoModerato.id]: http(),
365
+ * },
366
+ * })
367
+ *
368
+ * const result = await Actions.zone.depositSync(config, {
369
+ * amount: 1_000_000n,
370
+ * token: '0x20c0000000000000000000000000000000000001',
371
+ * zoneId: 7,
372
+ * })
373
+ *
374
+ * console.log(result.receipt.transactionHash)
375
+ * ```
376
+ *
377
+ * @param config - Config.
378
+ * @param parameters - Parameters.
379
+ * @returns The transaction receipt.
380
+ */
381
+ export async function depositSync(config, parameters) {
382
+ const { account, chainId, connector, throwOnReceiptRevert = true, ...rest } = parameters;
383
+ const client = await getConnectorClient(config, {
384
+ account,
385
+ assertChainId: false,
386
+ chainId,
387
+ connector,
388
+ });
389
+ const resolvedChainId = chainId ?? client.chain?.id;
390
+ if (!resolvedChainId)
391
+ throw new Error('`chainId` is required.');
392
+ const account_ = account ?? client.account;
393
+ if (!account_)
394
+ throw new Error('`account` is required.');
395
+ const accountAddress = parseAccount(account_).address;
396
+ const { amount, memo = zeroHash, recipient = accountAddress, token, zoneId, ...tx } = rest;
397
+ const { address: portalAddress } = resolvePortal(config, resolvedChainId, zoneId);
398
+ const tokenAddress = TokenId.toAddress(token);
399
+ const receipt = await viem_sendTransactionSync(client, {
400
+ ...tx,
401
+ calls: [
402
+ {
403
+ data: encodeFunctionData({
404
+ abi: Abis.tip20,
405
+ functionName: 'approve',
406
+ args: [portalAddress, amount],
407
+ }),
408
+ to: tokenAddress,
409
+ },
410
+ {
411
+ data: encodeFunctionData({
412
+ abi: ZoneAbis.zonePortal,
413
+ functionName: 'deposit',
414
+ args: [tokenAddress, recipient, amount, memo],
415
+ }),
416
+ to: portalAddress,
417
+ },
418
+ ],
419
+ throwOnReceiptRevert,
420
+ });
421
+ return { receipt };
422
+ }
423
+ /**
424
+ * Deposits tokens into a zone on the parent Tempo chain with an encrypted
425
+ * recipient and memo.
426
+ *
427
+ * @example
428
+ * ```ts
429
+ * import { createConfig, http } from '@wagmi/core'
430
+ * import { tempoModerato } from '@wagmi/core/chains'
431
+ * import { Actions } from '@wagmi/core/tempo'
432
+ *
433
+ * const config = createConfig({
434
+ * chains: [tempoModerato],
435
+ * transports: {
436
+ * [tempoModerato.id]: http(),
437
+ * },
438
+ * })
439
+ *
440
+ * const hash = await Actions.zone.encryptedDeposit(config, {
441
+ * amount: 1_000_000n,
442
+ * token: '0x20c0000000000000000000000000000000000001',
443
+ * zoneId: 7,
444
+ * })
445
+ * ```
446
+ *
447
+ * @param config - Config.
448
+ * @param parameters - Parameters.
449
+ * @returns Transaction hash.
450
+ */
451
+ export async function encryptedDeposit(config, parameters) {
452
+ const { account, chainId, connector, ...rest } = parameters;
453
+ const client = await getConnectorClient(config, {
454
+ account,
455
+ assertChainId: false,
456
+ chainId,
457
+ connector,
458
+ });
459
+ const resolvedChainId = chainId ?? client.chain?.id;
460
+ if (!resolvedChainId)
461
+ throw new Error('`chainId` is required.');
462
+ const account_ = account ?? client.account;
463
+ if (!account_)
464
+ throw new Error('`account` is required.');
465
+ const accountAddress = parseAccount(account_).address;
466
+ const { amount, memo, recipient = accountAddress, token, zoneId, ...tx } = rest;
467
+ const portal = resolvePortal(config, resolvedChainId, zoneId);
468
+ const portalAddress = portal.address;
469
+ const tokenAddress = TokenId.toAddress(token);
470
+ const [publicKey, keyIndex] = portal.sequencerEncryptionKey && portal.encryptionKeyCount !== undefined
471
+ ? [portal.sequencerEncryptionKey, portal.encryptionKeyCount]
472
+ : await Promise.all([
473
+ viem_readContract(client, {
474
+ address: portalAddress,
475
+ abi: ZoneAbis.zonePortal,
476
+ functionName: 'sequencerEncryptionKey',
477
+ }).then(([x, yParity]) => ({ x, yParity: Number(yParity) })),
478
+ viem_readContract(client, {
479
+ address: portalAddress,
480
+ abi: ZoneAbis.zonePortal,
481
+ functionName: 'encryptionKeyCount',
482
+ }),
483
+ ]);
484
+ if (keyIndex === 0n)
485
+ throw new Error('No sequencer encryption key configured.');
486
+ const encrypted = await encryptDepositPayload(publicKey, recipient, memo);
487
+ return viem_sendTransaction(client, {
488
+ ...tx,
489
+ calls: [
490
+ {
491
+ data: encodeFunctionData({
492
+ abi: Abis.tip20,
493
+ functionName: 'approve',
494
+ args: [portalAddress, amount],
495
+ }),
496
+ to: tokenAddress,
497
+ },
498
+ {
499
+ data: encodeFunctionData({
500
+ abi: ZoneAbis.zonePortal,
501
+ functionName: 'depositEncrypted',
502
+ args: [tokenAddress, amount, keyIndex - 1n, encrypted],
503
+ }),
504
+ to: portalAddress,
505
+ },
506
+ ],
507
+ });
508
+ }
509
+ /**
510
+ * Deposits tokens into a zone on the parent Tempo chain with an encrypted
511
+ * recipient and memo.
512
+ *
513
+ * Note: This is a synchronous action that waits for the transaction to
514
+ * be included on a block before returning a response.
515
+ *
516
+ * @example
517
+ * ```ts
518
+ * import { createConfig, http } from '@wagmi/core'
519
+ * import { tempoModerato } from '@wagmi/core/chains'
520
+ * import { Actions } from '@wagmi/core/tempo'
521
+ *
522
+ * const config = createConfig({
523
+ * chains: [tempoModerato],
524
+ * transports: {
525
+ * [tempoModerato.id]: http(),
526
+ * },
527
+ * })
528
+ *
529
+ * const result = await Actions.zone.encryptedDepositSync(config, {
530
+ * amount: 1_000_000n,
531
+ * token: '0x20c0000000000000000000000000000000000001',
532
+ * zoneId: 7,
533
+ * })
534
+ *
535
+ * console.log(result.receipt.transactionHash)
536
+ * ```
537
+ *
538
+ * @param config - Config.
539
+ * @param parameters - Parameters.
540
+ * @returns The transaction receipt.
541
+ */
542
+ export async function encryptedDepositSync(config, parameters) {
543
+ const { account, chainId, connector, throwOnReceiptRevert = true, ...rest } = parameters;
544
+ const client = await getConnectorClient(config, {
545
+ account,
546
+ assertChainId: false,
547
+ chainId,
548
+ connector,
549
+ });
550
+ const resolvedChainId = chainId ?? client.chain?.id;
551
+ if (!resolvedChainId)
552
+ throw new Error('`chainId` is required.');
553
+ const account_ = account ?? client.account;
554
+ if (!account_)
555
+ throw new Error('`account` is required.');
556
+ const accountAddress = parseAccount(account_).address;
557
+ const { amount, memo, recipient = accountAddress, token, zoneId, ...tx } = rest;
558
+ const portal = resolvePortal(config, resolvedChainId, zoneId);
559
+ const portalAddress = portal.address;
560
+ const tokenAddress = TokenId.toAddress(token);
561
+ const [publicKey, keyIndex] = portal.sequencerEncryptionKey && portal.encryptionKeyCount !== undefined
562
+ ? [portal.sequencerEncryptionKey, portal.encryptionKeyCount]
563
+ : await Promise.all([
564
+ viem_readContract(client, {
565
+ address: portalAddress,
566
+ abi: ZoneAbis.zonePortal,
567
+ functionName: 'sequencerEncryptionKey',
568
+ }).then(([x, yParity]) => ({ x, yParity: Number(yParity) })),
569
+ viem_readContract(client, {
570
+ address: portalAddress,
571
+ abi: ZoneAbis.zonePortal,
572
+ functionName: 'encryptionKeyCount',
573
+ }),
574
+ ]);
575
+ if (keyIndex === 0n)
576
+ throw new Error('No sequencer encryption key configured.');
577
+ const encrypted = await encryptDepositPayload(publicKey, recipient, memo);
578
+ const receipt = await viem_sendTransactionSync(client, {
579
+ ...tx,
580
+ calls: [
581
+ {
582
+ data: encodeFunctionData({
583
+ abi: Abis.tip20,
584
+ functionName: 'approve',
585
+ args: [portalAddress, amount],
586
+ }),
587
+ to: tokenAddress,
588
+ },
589
+ {
590
+ data: encodeFunctionData({
591
+ abi: ZoneAbis.zonePortal,
592
+ functionName: 'depositEncrypted',
593
+ args: [tokenAddress, amount, keyIndex - 1n, encrypted],
594
+ }),
595
+ to: portalAddress,
596
+ },
597
+ ],
598
+ throwOnReceiptRevert,
599
+ });
600
+ return { receipt };
601
+ }
602
+ /**
603
+ * Requests a withdrawal from a zone to the parent Tempo chain.
604
+ *
605
+ * @example
606
+ * ```ts
607
+ * import { createConfig } from '@wagmi/core'
608
+ * import { Actions, dangerous_secp256k1 } from '@wagmi/core/tempo'
609
+ * import { Account } from 'viem/tempo'
610
+ * import { http as zoneHttp, zone } from 'viem/tempo/zones'
611
+ *
612
+ * const zoneChain = zone(7)
613
+ * const account = Account.fromSecp256k1('0x...')
614
+ * const config = createConfig({
615
+ * chains: [zoneChain],
616
+ * connectors: [dangerous_secp256k1({ account })],
617
+ * transports: {
618
+ * [zoneChain.id]: zoneHttp(),
619
+ * },
620
+ * })
621
+ *
622
+ * const hash = await Actions.zone.requestWithdrawal(config, {
623
+ * amount: 1_000_000n,
624
+ * chainId: zoneChain.id,
625
+ * token: '0x20c0000000000000000000000000000000000001',
626
+ * })
627
+ *
628
+ * console.log(hash)
629
+ * ```
630
+ *
631
+ * @param config - Config.
632
+ * @param parameters - Parameters.
633
+ * @returns Transaction hash.
634
+ */
635
+ export async function requestWithdrawal(config, parameters) {
636
+ const { account, chainId, connector } = parameters;
637
+ const client = await getZoneWalletClient(config, {
638
+ account,
639
+ chainId,
640
+ connector,
641
+ });
642
+ return Actions.zone.requestWithdrawal(client, parameters);
643
+ }
644
+ /**
645
+ * Requests a withdrawal from a zone to the parent Tempo chain.
646
+ *
647
+ * Note: This is a synchronous action that waits for the transaction to
648
+ * be included on a block before returning a response.
649
+ *
650
+ * @example
651
+ * ```ts
652
+ * import { createConfig } from '@wagmi/core'
653
+ * import { Actions, dangerous_secp256k1 } from '@wagmi/core/tempo'
654
+ * import { Account } from 'viem/tempo'
655
+ * import { http as zoneHttp, zone } from 'viem/tempo/zones'
656
+ *
657
+ * const zoneChain = zone(7)
658
+ * const account = Account.fromSecp256k1('0x...')
659
+ * const config = createConfig({
660
+ * chains: [zoneChain],
661
+ * connectors: [dangerous_secp256k1({ account })],
662
+ * transports: {
663
+ * [zoneChain.id]: zoneHttp(),
664
+ * },
665
+ * })
666
+ *
667
+ * const result = await Actions.zone.requestWithdrawalSync(config, {
668
+ * amount: 1_000_000n,
669
+ * chainId: zoneChain.id,
670
+ * token: '0x20c0000000000000000000000000000000000001',
671
+ * })
672
+ *
673
+ * console.log(result.receipt.transactionHash)
674
+ * ```
675
+ *
676
+ * @param config - Config.
677
+ * @param parameters - Parameters.
678
+ * @returns The transaction receipt.
679
+ */
680
+ export async function requestWithdrawalSync(config, parameters) {
681
+ const { account, chainId, connector } = parameters;
682
+ const client = await getZoneWalletClient(config, {
683
+ account,
684
+ chainId,
685
+ connector,
686
+ });
687
+ return Actions.zone.requestWithdrawalSync(client, parameters);
688
+ }
689
+ /**
690
+ * Requests a verifiable withdrawal from a zone to the parent Tempo chain.
691
+ *
692
+ * @example
693
+ * ```ts
694
+ * import { createConfig } from '@wagmi/core'
695
+ * import { Actions, dangerous_secp256k1 } from '@wagmi/core/tempo'
696
+ * import { Account } from 'viem/tempo'
697
+ * import { http as zoneHttp, zone } from 'viem/tempo/zones'
698
+ *
699
+ * const zoneChain = zone(7)
700
+ * const account = Account.fromSecp256k1('0x...')
701
+ * const config = createConfig({
702
+ * chains: [zoneChain],
703
+ * connectors: [dangerous_secp256k1({ account })],
704
+ * transports: {
705
+ * [zoneChain.id]: zoneHttp(),
706
+ * },
707
+ * })
708
+ *
709
+ * const hash = await Actions.zone.requestVerifiableWithdrawal(config, {
710
+ * amount: 1_000_000n,
711
+ * chainId: zoneChain.id,
712
+ * revealTo:
713
+ * '0x0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
714
+ * token: '0x20c0000000000000000000000000000000000001',
715
+ * })
716
+ *
717
+ * console.log(hash)
718
+ * ```
719
+ *
720
+ * @param config - Config.
721
+ * @param parameters - Parameters.
722
+ * @returns Transaction hash.
723
+ */
724
+ export async function requestVerifiableWithdrawal(config, parameters) {
725
+ const { account, chainId, connector } = parameters;
726
+ const client = await getZoneWalletClient(config, {
727
+ account,
728
+ chainId,
729
+ connector,
730
+ });
731
+ return Actions.zone.requestVerifiableWithdrawal(client, parameters);
732
+ }
733
+ /**
734
+ * Requests a verifiable withdrawal from a zone to the parent Tempo chain.
735
+ *
736
+ * Note: This is a synchronous action that waits for the transaction to
737
+ * be included on a block before returning a response.
738
+ *
739
+ * @example
740
+ * ```ts
741
+ * import { createConfig } from '@wagmi/core'
742
+ * import { Actions, dangerous_secp256k1 } from '@wagmi/core/tempo'
743
+ * import { Account } from 'viem/tempo'
744
+ * import { http as zoneHttp, zone } from 'viem/tempo/zones'
745
+ *
746
+ * const zoneChain = zone(7)
747
+ * const account = Account.fromSecp256k1('0x...')
748
+ * const config = createConfig({
749
+ * chains: [zoneChain],
750
+ * connectors: [dangerous_secp256k1({ account })],
751
+ * transports: {
752
+ * [zoneChain.id]: zoneHttp(),
753
+ * },
754
+ * })
755
+ *
756
+ * const result = await Actions.zone.requestVerifiableWithdrawalSync(config, {
757
+ * amount: 1_000_000n,
758
+ * chainId: zoneChain.id,
759
+ * revealTo:
760
+ * '0x0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
761
+ * token: '0x20c0000000000000000000000000000000000001',
762
+ * })
763
+ *
764
+ * console.log(result.receipt.transactionHash)
765
+ * ```
766
+ *
767
+ * @param config - Config.
768
+ * @param parameters - Parameters.
769
+ * @returns The transaction receipt.
770
+ */
771
+ export async function requestVerifiableWithdrawalSync(config, parameters) {
772
+ const { account, chainId, connector } = parameters;
773
+ const client = await getZoneWalletClient(config, {
774
+ account,
775
+ chainId,
776
+ connector,
777
+ });
778
+ return Actions.zone.requestVerifiableWithdrawalSync(client, parameters);
779
+ }
780
+ const portalAddresses = {
781
+ 42431: {
782
+ 6: '0x7069DeC4E64Fd07334A0933eDe836C17259c9B23',
783
+ 7: '0x3F5296303400B56271b476F5A0B9cBF74350D6Ac',
784
+ },
785
+ };
786
+ async function getZoneWalletClient(config, parameters) {
787
+ const client = await getConnectorClient(config, {
788
+ ...parameters,
789
+ assertChainId: false,
790
+ });
791
+ const resolvedChainId = parameters.chainId ?? client.chain?.id;
792
+ const account = resolveSignableAccount(parameters.account) ??
793
+ (await getSignableConnectorAccount(config, {
794
+ account: parameters.account ?? client.account,
795
+ chainId: resolvedChainId,
796
+ connector: parameters.connector,
797
+ }));
798
+ if (!account || resolvedChainId === undefined)
799
+ return client;
800
+ // Local accounts can sign against the zone transport directly without
801
+ // depending on the connector provider's currently selected chain.
802
+ return Object.assign(config.getClient({ chainId: resolvedChainId }), {
803
+ account,
804
+ });
805
+ }
806
+ async function getSignableConnectorAccount(config, parameters) {
807
+ const connector = parameters.connector ??
808
+ config.state.connections.get(config.state.current)?.connector;
809
+ const provider = (await connector?.getProvider?.({
810
+ chainId: parameters.chainId,
811
+ }));
812
+ if (typeof provider?.getAccount !== 'function')
813
+ return;
814
+ try {
815
+ return provider.getAccount({
816
+ address: parameters.account
817
+ ? parseAccount(parameters.account).address
818
+ : undefined,
819
+ signable: true,
820
+ });
821
+ }
822
+ catch {
823
+ return;
824
+ }
825
+ }
826
+ function resolveSignableAccount(account) {
827
+ if (typeof account !== 'object' || account === null)
828
+ return;
829
+ return 'sign' in account ? account : undefined;
830
+ }
831
+ function resolvePortal(config, chainId, zoneId) {
832
+ const chain = config.chains.find((chain) => chain.id === chainId);
833
+ const zonePortal = chain?.contracts?.zonePortal;
834
+ // Allow custom chains to supply portal addresses until viem exposes a
835
+ // generic resolver for non-hardcoded Tempo networks.
836
+ if (typeof zonePortal === 'string')
837
+ return { address: zonePortal };
838
+ if (zonePortal && typeof zonePortal === 'object') {
839
+ const portal = 'address' in zonePortal && typeof zonePortal.address === 'string'
840
+ ? zonePortal
841
+ : zonePortal[zoneId];
842
+ if (typeof portal === 'string')
843
+ return { address: portal };
844
+ if (portal &&
845
+ typeof portal === 'object' &&
846
+ typeof portal.address === 'string') {
847
+ return {
848
+ address: portal.address,
849
+ encryptionKeyCount: portal.encryptionKeyCount,
850
+ sequencerEncryptionKey: portal.sequencerEncryptionKey,
851
+ };
852
+ }
853
+ }
854
+ const address = portalAddresses[chainId]?.[zoneId];
855
+ if (address)
856
+ return { address };
857
+ throw new Error(`No portal address configured for zone ${zoneId} on chain ${chainId}.`);
858
+ }
859
+ async function encryptDepositPayload(publicKey, recipient, memo = zeroHash) {
860
+ const sequencerPublicKey = PublicKey.from({
861
+ prefix: publicKey.yParity,
862
+ x: BigInt(publicKey.x),
863
+ });
864
+ const { privateKey: ephemeralPrivateKey, publicKey: ephemeralPublicKey } = Secp256k1.createKeyPair();
865
+ const sharedSecret = Secp256k1.getSharedSecret({
866
+ privateKey: ephemeralPrivateKey,
867
+ publicKey: sequencerPublicKey,
868
+ as: 'Bytes',
869
+ });
870
+ const hkdfKey = await globalThis.crypto.subtle.importKey('raw', sharedSecret.buffer, 'HKDF', false, ['deriveKey']);
871
+ const aesKey = await globalThis.crypto.subtle.deriveKey({
872
+ name: 'HKDF',
873
+ hash: 'SHA-256',
874
+ salt: new Uint8Array(12),
875
+ info: new TextEncoder().encode('ecies-aes-key'),
876
+ }, hkdfKey, { name: 'AES-GCM', length: 256 }, false, ['encrypt']);
877
+ const nonce = Bytes.random(12);
878
+ const plaintext = encodeAbiParameters([{ type: 'address' }, { type: 'bytes32' }], [recipient, memo]);
879
+ const ciphertextWithTag = new Uint8Array(await globalThis.crypto.subtle.encrypt({ name: 'AES-GCM', iv: nonce, tagLength: 128 }, aesKey, Bytes.from(plaintext)));
880
+ const ciphertext = ciphertextWithTag.slice(0, -16);
881
+ const tag = ciphertextWithTag.slice(-16);
882
+ const compressedEphemeral = PublicKey.compress(ephemeralPublicKey);
883
+ return {
884
+ ciphertext: bytesToHex(ciphertext),
885
+ ephemeralPubkeyX: `0x${compressedEphemeral.x.toString(16).padStart(64, '0')}`,
886
+ ephemeralPubkeyYParity: compressedEphemeral.prefix,
887
+ nonce: bytesToHex(nonce),
888
+ tag: bytesToHex(tag),
889
+ };
890
+ }
891
+ function bytesToHex(bytes) {
892
+ return `0x${Array.from(bytes, (value) => value.toString(16).padStart(2, '0')).join('')}`;
893
+ }
894
+ //# sourceMappingURL=zone.js.map