-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathabi.rs
More file actions
922 lines (809 loc) · 34.8 KB
/
abi.rs
File metadata and controls
922 lines (809 loc) · 34.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
//! ABI bindings for the Tempo Zone protocol contracts.
//!
//! These bindings cover the two main contracts the sequencer interacts with:
//!
//! - **ZonePortal** — deployed on Tempo L1. Escrows gas tokens, manages the deposit queue,
//! accepts batch proofs, and processes withdrawals back to L1 recipients.
//!
//! - **ZoneOutbox** — deployed on the Zone L2. Collects user withdrawal requests, builds
//! withdrawal hash chains, and exposes [`LastBatch`] state for proof generation.
// The `sol!` macro generates functions whose arity we don't control.
#![allow(clippy::too_many_arguments)]
use alloc::vec::Vec;
use alloy_primitives::{Address, B256, Bytes, U256, keccak256};
use alloy_sol_types::SolValue;
pub use crate::constants::{
EMPTY_SENTINEL, PORTAL_PENDING_SEQUENCER_SLOT, PORTAL_SEQUENCER_SLOT, TEMPO_BLOCK_HASH_SLOT,
TEMPO_PACKED_SLOT, TEMPO_STATE_ADDRESS, TEMPO_STATE_READER_ADDRESS, ZONE_CONFIG_ADDRESS,
ZONE_INBOX_ADDRESS, ZONE_OUTBOX_ADDRESS, ZONE_TOKEN_ADDRESS, ZONE_TX_CONTEXT_ADDRESS,
};
/// Internal macro that emits the full `sol!` block, placing `$($rpc_attr)*`
/// before every `contract` declaration. Called twice: once with `#[sol(rpc)]`
/// (when the `rpc` feature is active) and once with nothing.
macro_rules! define_abi {
($($rpc_attr:tt)*) => {
alloy_sol_types::sol! {
// ---------------------------------------------------------------
// Shared types
// ---------------------------------------------------------------
#[derive(Debug)]
struct Withdrawal {
address token;
bytes32 senderTag;
address to;
uint128 amount;
uint128 fee;
bytes32 memo;
uint64 gasLimit;
address fallbackRecipient;
bytes callbackData;
bytes encryptedSender;
}
#[derive(Debug)]
struct Deposit {
address token;
address sender;
address to;
uint128 amount;
bytes32 memo;
address bouncebackRecipient;
}
/// Encrypted deposit payload (ECIES encrypted recipient and memo)
#[derive(Debug)]
struct EncryptedDepositPayload {
bytes32 ephemeralPubkeyX;
uint8 ephemeralPubkeyYParity;
bytes ciphertext;
bytes12 nonce;
bytes16 tag;
}
/// Encrypted deposit stored in the queue
#[derive(Debug)]
struct EncryptedDeposit {
address token;
address sender;
uint128 amount;
uint256 keyIndex;
EncryptedDepositPayload encrypted;
address bouncebackRecipient;
}
#[derive(Debug)]
struct BlockTransition {
bytes32 prevBlockHash;
bytes32 nextBlockHash;
}
#[derive(Debug)]
struct DepositQueueTransition {
bytes32 prevProcessedHash;
bytes32 nextProcessedHash;
uint64 prevDepositNumber;
uint64 nextDepositNumber;
}
#[derive(Debug)]
struct LastBatch {
bytes32 withdrawalQueueHash;
uint64 withdrawalBatchIndex;
}
/// A TIP-20 token enabled on L1 for bridging to the zone.
#[derive(Debug)]
struct EnabledToken {
address token;
string name;
string symbol;
string currency;
}
/// Generic unauthorized access error used by zone wrapper logic.
error Unauthorized();
// ---------------------------------------------------------------
// ZonePortal — deployed on Tempo L1
// ---------------------------------------------------------------
$($rpc_attr)*
contract ZonePortal {
// -- Events --
#[derive(Debug)]
event DepositMade(
bytes32 indexed newCurrentDepositQueueHash,
address indexed sender,
address token,
address to,
uint128 netAmount,
uint128 fee,
bytes32 memo,
address bouncebackRecipient,
uint64 depositNumber
);
#[derive(Debug)]
event EncryptedDepositMade(
bytes32 indexed newCurrentDepositQueueHash,
address indexed sender,
address token,
uint128 netAmount,
uint128 fee,
uint256 keyIndex,
bytes32 ephemeralPubkeyX,
uint8 ephemeralPubkeyYParity,
bytes ciphertext,
bytes12 nonce,
bytes16 tag,
uint64 depositNumber
);
/// Event emitted when a new TIP-20 token is enabled for bridging.
/// Includes token metadata so the zone can create a matching TIP-20.
#[derive(Debug)]
event TokenEnabled(address indexed token, string name, string symbol, string currency);
#[derive(Debug)]
event BatchSubmitted(
uint64 indexed withdrawalBatchIndex,
bytes32 nextProcessedDepositQueueHash,
bytes32 nextBlockHash,
bytes32 withdrawalQueueHash,
uint64 lastProcessedDepositNumber
);
#[derive(Debug)]
event WithdrawalProcessed(address indexed to, address token, uint128 amount, bool callbackSuccess);
#[derive(Debug)]
event WithdrawalBounceBack(
bytes32 indexed newCurrentDepositQueueHash,
address indexed fallbackRecipient,
address token,
uint128 amount,
uint64 depositNumber
);
#[derive(Debug)]
event DepositBounceBack(
address indexed bouncebackRecipient,
address token,
uint128 amount
);
#[derive(Debug)]
event SequencerTransferStarted(
address indexed currentSequencer,
address indexed pendingSequencer
);
#[derive(Debug)]
event SequencerTransferred(
address indexed previousSequencer,
address indexed newSequencer
);
// -- Errors --
#[derive(Debug)]
error NotSequencer();
#[derive(Debug)]
error InvalidProof();
#[derive(Debug)]
error InvalidTempoBlockNumber();
#[derive(Debug)]
error DepositPolicyForbids();
// -- View functions --
function zoneId() external view returns (uint32);
function sequencer() external view returns (address);
function verifier() external view returns (address);
function sequencerPubkey() external view returns (bytes32);
function withdrawalBatchIndex() external view returns (uint64);
function blockHash() external view returns (bytes32);
function currentDepositQueueHash() external view returns (bytes32);
function lastSyncedTempoBlockNumber() external view returns (uint64);
function withdrawalQueueHead() external view returns (uint256);
function withdrawalQueueTail() external view returns (uint256);
function withdrawalQueueMaxSize() external view returns (uint256);
function withdrawalQueueSlot(uint256 slot) external view returns (bytes32);
function genesisTempoBlockNumber() external view returns (uint64);
function calculateDepositFee() external view returns (uint128 fee);
function depositCount() external view returns (uint64);
function lastProcessedDepositNumber() external view returns (uint64);
// -- State-changing functions --
function deposit(address token, address to, uint128 amount, bytes32 memo, address bouncebackRecipient)
external
returns (bytes32 newCurrentDepositQueueHash);
function processWithdrawal(Withdrawal calldata withdrawal, bytes32 remainingQueue) external;
function submitBatch(
uint64 tempoBlockNumber,
uint64 recentTempoBlockNumber,
BlockTransition calldata blockTransition,
DepositQueueTransition calldata depositQueueTransition,
bytes32 withdrawalQueueHash,
bytes calldata verifierConfig,
bytes calldata proof
) external;
function enableToken(address token) external;
function depositEncrypted(
address token,
uint128 amount,
uint256 keyIndex,
EncryptedDepositPayload calldata encrypted,
address bouncebackRecipient
) external returns (bytes32 newCurrentDepositQueueHash);
function setSequencerEncryptionKey(
bytes32 x,
uint8 yParity,
uint8 popV,
bytes32 popR,
bytes32 popS
) external;
// -- View functions (token management) --
function isTokenEnabled(address token) external view returns (bool);
function enabledTokenCount() external view returns (uint256);
function enabledTokenAt(uint256 index) external view returns (address);
function zoneGasRate() external view returns (uint128);
function pendingSequencer() external view returns (address);
function sequencerEncryptionKey() external view returns (bytes32 x, uint8 yParity);
function encryptionKeyCount() external view returns (uint256);
}
// ---------------------------------------------------------------
// ZoneOutbox — deployed on Zone L2
// ---------------------------------------------------------------
$($rpc_attr)*
contract ZoneOutbox {
// -- Events --
event WithdrawalRequested(
uint64 indexed withdrawalIndex,
address indexed sender,
address token,
address to,
uint128 amount,
uint128 fee,
bytes32 memo,
uint64 gasLimit,
address fallbackRecipient,
bytes data,
bytes revealTo
);
#[derive(Debug)]
event BatchFinalized(bytes32 indexed withdrawalQueueHash, uint64 withdrawalBatchIndex);
// -- Errors --
error OnlySequencer();
// -- View functions --
function lastBatch() external view returns (LastBatch memory);
function withdrawalBatchIndex() external view returns (uint64);
function nextWithdrawalIndex() external view returns (uint64);
function pendingWithdrawalsCount() external view returns (uint256);
function calculateWithdrawalFee(uint64 gasLimit) external view returns (uint128 fee);
// -- State-changing functions --
function requestWithdrawal(
address token,
address to,
uint128 amount,
bytes32 memo,
uint64 gasLimit,
address fallbackRecipient,
bytes calldata data,
bytes calldata revealTo
) external;
function finalizeWithdrawalBatch(uint256 count, uint64 blockNumber, bytes[] calldata encryptedSenders) external returns (bytes32 withdrawalQueueHash);
}
// ---------------------------------------------------------------
// TempoState — Zone L2 predeploy (0x1c00...0000)
// ---------------------------------------------------------------
$($rpc_attr)*
contract TempoState {
#[derive(Debug)]
event TempoBlockFinalized(bytes32 indexed blockHash, uint64 indexed blockNumber, bytes32 stateRoot);
error InvalidParentHash();
error InvalidBlockNumber();
error InvalidRlpData();
error OnlyZoneInbox();
function tempoBlockHash() external view returns (bytes32);
function tempoBlockNumber() external view returns (uint64);
function tempoStateRoot() external view returns (bytes32);
function tempoParentHash() external view returns (bytes32);
function tempoBeneficiary() external view returns (address);
function tempoTransactionsRoot() external view returns (bytes32);
function tempoReceiptsRoot() external view returns (bytes32);
function tempoGasLimit() external view returns (uint64);
function tempoGasUsed() external view returns (uint64);
function tempoTimestamp() external view returns (uint64);
function tempoTimestampMillis() external view returns (uint64);
function tempoPrevRandao() external view returns (bytes32);
function generalGasLimit() external view returns (uint64);
function sharedGasLimit() external view returns (uint64);
function finalizeTempo(bytes calldata header) external;
}
// ---------------------------------------------------------------
// TempoStateReader — Zone L2 standalone precompile
// Separate from TempoState; reads Tempo L1 storage at a caller-specified block.
// ---------------------------------------------------------------
$($rpc_attr)*
contract TempoStateReader {
error DelegateCallNotAllowed();
function readStorageAt(address account, bytes32 slot, uint64 blockNumber) external view returns (bytes32);
function readStorageBatchAt(address account, bytes32[] calldata slots, uint64 blockNumber) external view returns (bytes32[] memory);
}
$($rpc_attr)*
contract ZoneTxContext {
function currentTxHash() external returns (bytes32);
}
// ---------------------------------------------------------------
// ZoneInbox — Zone L2 system contract (0x1c00...0001)
// ---------------------------------------------------------------
/// Deposit types for the unified deposit queue.
#[derive(Debug, PartialEq, Eq)]
enum DepositType {
Regular,
Encrypted,
}
/// A queued deposit (regular or encrypted) passed to `advanceTempo`.
#[derive(Debug)]
struct QueuedDeposit {
DepositType depositType;
bytes depositData;
}
/// Chaum-Pedersen proof for ECDH shared secret derivation.
#[derive(Debug)]
struct ChaumPedersenProof {
bytes32 s;
bytes32 c;
}
/// Decryption data provided by the sequencer for encrypted deposits.
#[derive(Debug)]
struct DecryptionData {
bytes32 sharedSecret;
uint8 sharedSecretYParity;
address to;
bytes32 memo;
ChaumPedersenProof cpProof;
}
// ---------------------------------------------------------------
// ZoneFactory — deployed on Tempo L1
// ---------------------------------------------------------------
#[derive(Debug)]
struct ZoneInfo {
uint32 zoneId;
address portal;
address messenger;
address initialToken;
address sequencer;
address verifier;
bytes32 genesisBlockHash;
bytes32 genesisTempoBlockHash;
uint64 genesisTempoBlockNumber;
}
$($rpc_attr)*
contract ZoneFactory {
struct ZoneParams {
bytes32 genesisBlockHash;
bytes32 genesisTempoBlockHash;
uint64 genesisTempoBlockNumber;
}
struct CreateZoneParams {
address token;
address sequencer;
address verifier;
ZoneParams zoneParams;
}
#[derive(Debug)]
event ZoneCreated(
uint32 indexed zoneId,
address indexed portal,
address indexed messenger,
address token,
address sequencer,
address verifier,
bytes32 genesisBlockHash,
bytes32 genesisTempoBlockHash,
uint64 genesisTempoBlockNumber
);
function createZone(CreateZoneParams calldata params) external returns (uint32 zoneId, address portal);
function verifier() external view returns (address);
function zones(uint32 zoneId) external view returns (ZoneInfo memory);
function zoneCount() external view returns (uint32);
function isZonePortal(address portal) external view returns (bool);
function isZoneMessenger(address messenger) external view returns (bool);
}
// ---------------------------------------------------------------
// ZoneInbox — Zone L2 system contract (0x1c00...0001)
// ---------------------------------------------------------------
$($rpc_attr)*
contract ZoneInbox {
#[derive(Debug)]
event TempoAdvanced(
bytes32 indexed tempoBlockHash,
uint64 indexed tempoBlockNumber,
uint256 depositsProcessed,
bytes32 newProcessedDepositQueueHash,
uint64 lastProcessedDepositNumber
);
#[derive(Debug)]
event DepositProcessed(
bytes32 indexed depositHash,
address indexed sender,
address indexed to,
address token,
uint128 amount,
bytes32 memo
);
#[derive(Debug)]
event EncryptedDepositProcessed(
bytes32 indexed depositHash,
address indexed sender,
address indexed to,
address token,
uint128 amount,
bytes32 memo
);
#[derive(Debug)]
event EncryptedDepositFailed(
bytes32 indexed depositHash,
address indexed sender,
address token,
uint128 amount
);
/// Emitted when a TIP-20 token is enabled on the zone via advanceTempo.
#[derive(Debug)]
event TokenEnabled(address indexed token, string name, string symbol, string currency);
error OnlySequencer();
error InvalidDepositQueueHash();
error MissingDecryptionData();
error ExtraDecryptionData();
error InvalidSharedSecretProof();
function processedDepositQueueHash() external view returns (bytes32);
function processedDepositNumber() external view returns (uint64);
function tempoPortal() external view returns (address);
function tempoState() external view returns (address);
function config() external view returns (address);
function advanceTempo(
bytes calldata header,
QueuedDeposit[] calldata deposits,
DecryptionData[] calldata decryptions,
EnabledToken[] calldata enabledTokens
) external;
}
// ---------------------------------------------------------------
// SwapAndDepositRouter — deployed on Tempo L1
// ---------------------------------------------------------------
$($rpc_attr)*
contract SwapAndDepositRouter {
function onWithdrawalReceived(
bytes32 senderTag,
address tokenIn,
uint128 amount,
bytes calldata data
) external returns (bytes4);
}
}
};
}
#[cfg(feature = "rpc")]
define_abi!(#[sol(rpc)]);
#[cfg(not(feature = "rpc"))]
define_abi!();
impl ZonePortal::sequencerEncryptionKeyReturn {
/// Normalize `yParity` to SEC1 compressed prefix (`0x02` or `0x03`).
///
/// The contract may return `0`/`1` (parity bit) or `0x02`/`0x03` (SEC1 prefix).
pub fn normalized_y_parity(&self) -> Option<u8> {
match self.yParity {
0x02 | 0x03 => Some(self.yParity),
0 | 1 => Some(0x02 + self.yParity),
_ => None,
}
}
}
/// Plaintext callback payload for `SwapAndDepositRouter.onWithdrawalReceived`.
///
/// This payload tells the router to optionally swap the withdrawn token on L1
/// and then perform a regular `ZonePortal.deposit(...)`.
#[derive(Debug, Clone)]
pub struct SwapAndDepositRouterPlaintextCallback {
/// Token that should be deposited after the optional L1 swap.
pub token_out: Address,
/// Target zone portal that receives the downstream deposit.
pub target_portal: Address,
/// Zone recipient for the downstream plaintext deposit.
pub recipient: Address,
/// Memo recorded on the downstream plaintext deposit.
pub memo: B256,
/// Minimum acceptable output from the optional swap.
///
/// Ignored when `tokenIn == token_out` and the router can deposit directly.
pub min_amount_out: u128,
}
impl SwapAndDepositRouterPlaintextCallback {
/// ABI-encode the router callback data expected by the Solidity router.
pub fn abi_encode(&self) -> Vec<u8> {
(
false,
self.token_out,
self.target_portal,
self.recipient,
self.memo,
self.min_amount_out,
)
.abi_encode_params()
}
}
/// Encrypted callback payload for `SwapAndDepositRouter.onWithdrawalReceived`.
///
/// This payload tells the router to optionally swap the withdrawn token on L1
/// and then call `ZonePortal.depositEncrypted(...)` with an ECIES-encrypted
/// `(recipient, memo)` payload.
#[derive(Debug, Clone)]
pub struct SwapAndDepositRouterEncryptedCallback {
/// Token that should be deposited after the optional L1 swap.
pub token_out: Address,
/// Target zone portal that receives the downstream encrypted deposit.
pub target_portal: Address,
/// Portal encryption key index used to build [`Self::encrypted`].
pub key_index: U256,
/// ECIES-encrypted `(recipient, memo)` payload for `depositEncrypted`.
pub encrypted: EncryptedDepositPayload,
/// Minimum acceptable output from the optional swap.
///
/// Ignored when `tokenIn == token_out` and the router can deposit directly.
pub min_amount_out: u128,
}
impl SwapAndDepositRouterEncryptedCallback {
/// ABI-encode the router callback data expected by the Solidity router.
pub fn abi_encode(&self) -> Vec<u8> {
(
true,
self.token_out,
self.target_portal,
self.key_index,
self.encrypted.clone(),
self.min_amount_out,
)
.abi_encode_params()
}
}
#[cfg(feature = "rpc")]
impl<P: alloy_provider::Provider<N>, N: alloy_network::Network>
ZonePortal::ZonePortalInstance<P, N>
{
/// Returns all token addresses currently enabled for bridging on this [`ZonePortal`].
///
/// Calls [`enabledTokenCount`](ZonePortal::enabledTokenCountCall) followed by
/// [`enabledTokenAt`](ZonePortal::enabledTokenAtCall) for each index concurrently.
pub async fn enabled_tokens(
&self,
) -> Result<alloc::vec::Vec<alloy_primitives::Address>, alloy_contract::Error> {
let count = self.enabledTokenCount().call().await?;
let futs: alloc::vec::Vec<_> = (0..count.to::<u64>())
.map(|i| async move {
self.enabledTokenAt(alloy_primitives::U256::from(i))
.call()
.await
})
.collect();
futures::future::try_join_all(futs).await
}
/// Fetches the active sequencer encryption key and its index.
///
/// Returns `(key, key_index)` where `key` is the
/// [`sequencerEncryptionKeyReturn`](ZonePortal::sequencerEncryptionKeyReturn) and
/// `key_index` is the zero-based index of the current key.
pub async fn encryption_key(
&self,
) -> Result<
(
ZonePortal::sequencerEncryptionKeyReturn,
alloy_primitives::U256,
),
alloy_contract::Error,
> {
let key_call = self.sequencerEncryptionKey();
let count_call = self.encryptionKeyCount();
let (key, count) = tokio::try_join!(key_call.call(), count_call.call())?;
let key_index = count.saturating_sub(alloy_primitives::U256::from(1));
Ok((key, key_index))
}
}
impl core::fmt::Display for ZonePortal::ZonePortalErrors {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::NotSequencer(_) => f.write_str("NotSequencer"),
Self::InvalidProof(_) => f.write_str("InvalidProof"),
Self::InvalidTempoBlockNumber(_) => f.write_str("InvalidTempoBlockNumber"),
Self::DepositPolicyForbids(_) => f.write_str("DepositPolicyForbids"),
}
}
}
impl Withdrawal {
/// Build the authenticated-withdrawal sender plaintext `[sender(20) | tx_hash(32)]`.
pub fn authenticated_sender_plaintext(sender: Address, tx_hash: B256) -> [u8; 52] {
let mut plaintext = [0u8; 52];
plaintext[..20].copy_from_slice(sender.as_slice());
plaintext[20..].copy_from_slice(tx_hash.as_slice());
plaintext
}
/// Compute the authenticated sender tag `keccak256(sender || tx_hash)`.
pub fn sender_tag(sender: Address, tx_hash: B256) -> B256 {
keccak256(Self::authenticated_sender_plaintext(sender, tx_hash))
}
/// Reconstruct the public L1-facing withdrawal from a zone-side withdrawal request event.
pub fn from_requested_event(
event: &ZoneOutbox::WithdrawalRequested,
tx_hash: B256,
encrypted_sender: Bytes,
) -> Self {
Self {
token: event.token,
senderTag: Self::sender_tag(event.sender, tx_hash),
to: event.to,
amount: event.amount,
fee: event.fee,
memo: event.memo,
gasLimit: event.gasLimit,
fallbackRecipient: event.fallbackRecipient,
callbackData: event.data.clone(),
encryptedSender: encrypted_sender,
}
}
/// Compute the withdrawal queue hash for a slice of withdrawals.
///
/// The hash chain has the oldest withdrawal at the outermost layer for efficient FIFO removal:
///
/// ```text
/// hash = keccak256(encode(w[0], keccak256(encode(w[1], keccak256(encode(w[2], EMPTY_SENTINEL))))))
/// ```
///
/// Building proceeds from the newest (innermost) to the oldest (outermost).
/// Returns `B256::ZERO` if `withdrawals` is empty.
pub fn queue_hash(withdrawals: &[Self]) -> B256 {
if withdrawals.is_empty() {
return B256::ZERO;
}
let mut hash = EMPTY_SENTINEL;
for w in withdrawals.iter().rev() {
hash = keccak256((w.clone(), hash).abi_encode_params());
}
hash
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::{Bytes, address};
use alloy_sol_types::SolCall;
#[test]
fn test_deposit_abi_encode_vs_params() {
let d = Deposit {
token: address!("0x0000000000000000000000000000000000001000"),
sender: address!("0x0000000000000000000000000000000000000001"),
to: address!("0x0000000000000000000000000000000000000002"),
amount: 1000u128,
memo: B256::ZERO,
bouncebackRecipient: Address::ZERO,
};
let encoded = d.abi_encode();
let encoded_params = d.abi_encode_params();
println!("abi_encode length: {}", encoded.len());
println!("abi_encode_params length: {}", encoded_params.len());
println!("abi_encode hex:\n{}", const_hex::encode(&encoded));
println!(
"abi_encode_params hex:\n{}",
const_hex::encode(&encoded_params)
);
println!("Are they equal: {}", encoded == encoded_params);
}
#[test]
fn test_queued_deposit_encoding() {
let deposit = Deposit {
token: address!("0x0000000000000000000000000000000000001000"),
sender: address!("0x0000000000000000000000000000000000000001"),
to: address!("0x0000000000000000000000000000000000000002"),
amount: 1000u128,
memo: B256::ZERO,
bouncebackRecipient: Address::ZERO,
};
let deposit_data = Bytes::from(deposit.abi_encode());
let qd = QueuedDeposit {
depositType: DepositType::Regular,
depositData: deposit_data,
};
println!(
"DepositType::Regular abi_encode: {}",
const_hex::encode(DepositType::Regular.abi_encode())
);
println!(
"deposit.abi_encode() length: {}",
deposit.abi_encode().len()
);
println!(
"deposit.abi_encode(): {}",
const_hex::encode(deposit.abi_encode())
);
println!(
"QueuedDeposit.abi_encode() length: {}",
qd.abi_encode().len()
);
println!(
"QueuedDeposit.abi_encode(): {}",
const_hex::encode(qd.abi_encode())
);
// Now test the full advanceTempo call encoding
let header_bytes = Bytes::from(vec![0xc0]); // minimal RLP empty list
let calldata = ZoneInbox::advanceTempoCall {
header: header_bytes,
deposits: vec![qd],
decryptions: vec![],
enabledTokens: vec![],
}
.abi_encode();
println!("\nadvanceTempo calldata length: {}", calldata.len());
println!(
"advanceTempo selector: 0x{}",
const_hex::encode(&calldata[..4])
);
println!(
"advanceTempo full calldata:\n{}",
const_hex::encode(&calldata)
);
}
#[test]
fn test_deposit_hash_chain_matches_solidity() {
let deposit = Deposit {
token: address!("0x0000000000000000000000000000000000001000"),
sender: address!("0x0000000000000000000000000000000000000001"),
to: address!("0x0000000000000000000000000000000000000002"),
amount: 1000u128,
memo: B256::ZERO,
bouncebackRecipient: Address::ZERO,
};
let prev_hash = B256::ZERO;
let solidity_encoding = (DepositType::Regular, deposit.clone(), prev_hash).abi_encode();
let solidity_hash = keccak256(&solidity_encoding);
let rust_encoding = (DepositType::Regular, deposit, prev_hash).abi_encode();
let rust_hash = keccak256(&rust_encoding);
assert_eq!(solidity_encoding, rust_encoding, "ABI encodings must match");
assert_eq!(solidity_hash, rust_hash, "Deposit hash chains must match");
}
#[test]
fn test_router_plaintext_callback_encoding_matches_tuple() {
let callback = SwapAndDepositRouterPlaintextCallback {
token_out: address!("0x0000000000000000000000000000000000001001"),
target_portal: address!("0x0000000000000000000000000000000000002001"),
recipient: address!("0x0000000000000000000000000000000000003001"),
memo: B256::from([0x11; 32]),
min_amount_out: 1234,
};
let tuple_encoding = (
false,
callback.token_out,
callback.target_portal,
callback.recipient,
callback.memo,
callback.min_amount_out,
)
.abi_encode_params();
assert_eq!(callback.abi_encode(), tuple_encoding);
}
#[test]
fn test_sender_tag_matches_plaintext_hash() {
let sender = address!("0x0000000000000000000000000000000000000001");
let tx_hash = B256::repeat_byte(0x22);
let plaintext = Withdrawal::authenticated_sender_plaintext(sender, tx_hash);
assert_eq!(&plaintext[..20], sender.as_slice());
assert_eq!(&plaintext[20..], tx_hash.as_slice());
assert_eq!(
Withdrawal::sender_tag(sender, tx_hash),
keccak256(plaintext)
);
}
#[test]
fn test_router_encrypted_callback_encoding_matches_tuple() {
let encrypted = EncryptedDepositPayload {
ephemeralPubkeyX: B256::from([0x22; 32]),
ephemeralPubkeyYParity: 0x02,
ciphertext: Bytes::from(vec![0xaa, 0xbb, 0xcc, 0xdd]),
nonce: [0x33; 12].into(),
tag: [0x44; 16].into(),
};
let callback = SwapAndDepositRouterEncryptedCallback {
token_out: address!("0x0000000000000000000000000000000000001002"),
target_portal: address!("0x0000000000000000000000000000000000002002"),
key_index: U256::from(7),
encrypted: encrypted.clone(),
min_amount_out: 5678,
};
let tuple_encoding = (
true,
callback.token_out,
callback.target_portal,
callback.key_index,
encrypted,
callback.min_amount_out,
)
.abi_encode_params();
assert_eq!(callback.abi_encode(), tuple_encoding);
}
}