odra-modules 2.8.0

Collection of reusable Odra modules.
Documentation
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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
#![allow(clippy::too_many_arguments)]
#![allow(missing_docs)]
//! CEP-3009 — an adaptation of [ERC-3009] for the Casper Network.
//!
//! ERC-3009 ("Transfer With Authorization") lets a token holder authorize a
//! transfer of their tokens via an off-chain EIP-712 signature. A third party
//! (a relayer, or the recipient themselves) submits the authorization
//! on-chain, so the token holder never has to spend gas to move their tokens.
//! Unlike ERC-2612, no allowance is granted: each authorization is a direct,
//! single-use mandate to move a specific `amount` to a specific `to` address.
//!
//! The module exposes three entry points:
//!
//! * [`CEP3009::transfer_with_authorization`] — anyone may relay; the funds
//!   move from `from` to `to` once the signature checks out.
//! * [`CEP3009::receive_with_authorization`] — only `to` may submit. On
//!   Ethereum this exists to prevent front-running of a relayed
//!   `to == contract` authorization; Casper has no public mempool, so
//!   front-running isn't a concern. The variant is still useful because it
//!   guarantees `caller == to`, which lets a receiving contract atomically
//!   accept the transfer and act on the deposit in the same call.
//! * [`CEP3009::cancel_authorization`] — the authorizer (or anyone relaying
//!   the authorizer's signed cancel message) can burn an unused nonce so a
//!   leaked authorization can never be redeemed.
//!
//! # Differences from EVM ERC-3009
//!
//! The EIP-712 typed-data digest construction (domain separator + the
//! `TransferWithAuthorization`, `ReceiveWithAuthorization`, and
//! `CancelAuthorization` struct hashes) follows the EVM specification exactly,
//! so signatures produced by standard EIP-712 tooling remain compatible. The
//! verification path differs because Casper does not provide `ecrecover`:
//!
//! * The caller must pass the signer's [`PublicKey`] explicitly alongside the
//!   signature. The contract checks that `Address::from(public_key) == from`
//!   (or `authorizer`, for cancellations) before verifying the signature, so
//!   a valid signature from a different keypair cannot be used to move
//!   someone else's tokens or burn someone else's nonce.
//! * Signature verification uses the host's [`verify_signature`] facility,
//!   which supports Casper's Ed25519 and Secp256k1 account keys.
//! * The EIP-712 `chainId` field (a `uint256` on Ethereum) is replaced by a
//!   `chain_name` string supplied at construction time, matching Casper's
//!   chain identification model.
//!
//! Replay protection follows ERC-3009: each authorization carries a 32-byte
//! `nonce` chosen by the signer (not a monotonic counter as in ERC-2612), and
//! the contract records `(authorizer, nonce) -> used` so the same nonce can
//! never be redeemed twice. Cancellation marks a nonce used without moving
//! funds.
//!
//! [ERC-3009]: https://eips.ethereum.org/EIPS/eip-3009
//! [`verify_signature`]: odra::ContractEnv::verify_signature

use crate::{cep18::events::Transfer, cep18_token::Cep18, eip712};
use casper_eip_712::DomainSeparator;
use odra::{
    casper_types::{bytesrepr::Bytes, PublicKey, U256},
    named_keys::{compound_key_value_storage, single_value_storage},
    prelude::*
};

// keccak256("TransferWithAuthorization(address from,address to,uint256 value,uint256 validAfter,uint256 validBefore,bytes32 nonce)")
const TRANSFER_WITH_AUTHORIZATION_TYPEHASH: [u8; 32] = [
    0x7c, 0x7c, 0x6c, 0xdb, 0x67, 0xa1, 0x87, 0x43, 0xf4, 0x9e, 0xc6, 0xfa, 0x9b, 0x35, 0xf5, 0x0d,
    0x52, 0xed, 0x05, 0xcb, 0xed, 0x4c, 0xc5, 0x92, 0xe1, 0x3b, 0x44, 0x50, 0x1c, 0x1a, 0x22, 0x67
];

// keccak256("ReceiveWithAuthorization(address from,address to,uint256 value,uint256 validAfter,uint256 validBefore,bytes32 nonce)")
const RECEIVE_WITH_AUTHORIZATION_TYPEHASH: [u8; 32] = [
    0xd0, 0x99, 0xcc, 0x98, 0xef, 0x71, 0x10, 0x7a, 0x61, 0x6c, 0x4f, 0x0f, 0x94, 0x1f, 0x04, 0xc3,
    0x22, 0xd8, 0xe2, 0x54, 0xfe, 0x26, 0xb3, 0xc6, 0x66, 0x8d, 0xb8, 0x7a, 0xae, 0x41, 0x3d, 0xe8
];

// keccak256("CancelAuthorization(address authorizer,bytes32 nonce)")
const CANCEL_AUTHORIZATION_TYPEHASH: [u8; 32] = [
    0x15, 0x8b, 0x0a, 0x9e, 0xdf, 0x7a, 0x82, 0x8a, 0xad, 0x02, 0xf6, 0x3c, 0xd5, 0x15, 0xc6, 0x8e,
    0xf2, 0xf5, 0x0b, 0xa8, 0x07, 0x39, 0x6f, 0x6d, 0x12, 0x84, 0x28, 0x33, 0xa1, 0x59, 0x74, 0x29
];

/// Emitted when an authorization is consumed by
/// [`CEP3009::transfer_with_authorization`] or
/// [`CEP3009::receive_with_authorization`]. The `(authorizer, nonce)` pair
/// is now marked used and cannot be redeemed again.
#[odra::event]
pub struct AuthorizationUsed {
    authorizer: Address,
    nonce: Bytes
}

/// Emitted when an authorization nonce is burned by
/// [`CEP3009::cancel_authorization`] before being used. The `(authorizer,
/// nonce)` pair is marked used without any funds moving.
#[odra::event]
pub struct AuthorizationCanceled {
    authorizer: Address,
    nonce: Bytes
}

/// Errors raised by CEP-3009 authorization operations.
#[odra::odra_error]
pub enum Error {
    /// The `(from, nonce)` pair has already been consumed by a transfer or
    /// burned by a cancel. Authorizations are single-use.
    NonceAlreadyUsed = 37_000,
    /// The current block time is past `valid_before` — the authorization has
    /// expired.
    AuthorizationExpired = 37_001,
    /// The current block time is at or before `valid_after` — the
    /// authorization is not yet in its validity window.
    AuthorizationNotYetValid = 37_002,
    /// Signature verification against the supplied public key failed, or the
    /// recomputed digest does not match what the signer signed (e.g. wrong
    /// `amount`, `to`, or `nonce`).
    InvalidSignature = 37_003,
    /// The supplied `public_key` does not hash to the declared `from` /
    /// `authorizer` address. Guards against using a valid signature from a
    /// different keypair to move someone else's tokens or burn their nonce.
    InvalidPublicKey = 37_004,
    /// `receive_with_authorization` was submitted by an account other than
    /// `to`. See the module-level docs for why this restriction exists.
    InvalidCaller = 37_005,
    /// Attempted to cancel an authorization whose `(authorizer, nonce)` pair
    /// has already been consumed.
    AuthorizationUsed = 37_006,
    /// The supplied `nonce` is not exactly 32 bytes. ERC-3009 nonces are
    /// `bytes32`; the EIP-712 digest only covers 32 bytes, so accepting a
    /// shorter or longer nonce would let an attacker craft length aliases
    /// that share a digest (and thus a signature) while occupying distinct
    /// replay-protection storage keys.
    InvalidNonceLength = 37_007
}

/// Storage defined as named keys.
const CHAIN_NAME_KEY: &str = "chain_name";
const USED_NONCES_KEY: &str = "used_nonces";

/// Domain separator version.
const DOMAIN_VERSION: &str = "1";

single_value_storage!(
    CEP3009ChainNameStorage,
    String,
    CHAIN_NAME_KEY,
    ExecutionError::KeyNotFound
);

compound_key_value_storage!(
    CEP3009UsedNoncesStorage,
    USED_NONCES_KEY,
    Address,
    Bytes,
    bool
);

/// CEP-3009 authorization module — adds off-chain signed transfers to a
/// CEP-18 token.
///
/// The module is meant to be composed with a [`Cep18`] sub-module (see
/// [`CEP3009Wrapper`] for a deployable composition used in tests).
#[odra::module(events = [AuthorizationUsed, AuthorizationCanceled, Transfer], errors = Error)]
pub struct CEP3009 {
    /// Per-(authorizer, nonce) flag recording whether that authorization has
    /// been consumed or cancelled. Provides single-use semantics.
    used_nonces: SubModule<CEP3009UsedNoncesStorage>,
    /// CAIP-2 Chain name used as the EIP-712 domain's `chainId` substitute.
    chain_name: SubModule<CEP3009ChainNameStorage>,
    /// The CEP-18 token whose balances are mutated by authorized transfers.
    token: SubModule<Cep18>
}

#[odra::module]
impl CEP3009 {
    /// Initializes the module by storing the EIP-712 chain ID and the
    /// used-nonces storage.
    pub fn init(&mut self, chain_name: String) {
        self.chain_name.set(chain_name);
        self.used_nonces.init();
    }

    /// Returns `true` if `(authorizer, nonce)` has been consumed by a
    /// previous transfer or burned by [`Self::cancel_authorization`]. Useful
    /// for off-chain clients to check before submitting a relayed transfer.
    pub fn authorization_state(&self, authorizer: Address, nonce: Bytes) -> bool {
        self.used_nonces.get_or_default(&authorizer, &nonce)
    }

    /// Consumes a signed `TransferWithAuthorization` and moves `amount`
    /// tokens from `from` to `to`. Any account may submit this call — the
    /// signature, not the caller, is what authorizes the transfer.
    ///
    /// Reverts with [`Error::InvalidNonceLength`], [`Error::NonceAlreadyUsed`],
    /// [`Error::AuthorizationNotYetValid`], [`Error::AuthorizationExpired`],
    /// [`Error::InvalidPublicKey`], or [`Error::InvalidSignature`] if the
    /// corresponding precondition fails.
    /// On success, marks the nonce used, emits [`AuthorizationUsed`], and
    /// performs the transfer via [`Cep18::raw_transfer`].
    pub fn transfer_with_authorization(
        &mut self,
        from: Address,
        to: Address,
        amount: U256,
        valid_after: u64,
        valid_before: u64,
        nonce: Bytes,
        public_key: PublicKey,
        signature: Bytes
    ) {
        self.raw_transfer_with_authorization(
            TRANSFER_WITH_AUTHORIZATION_TYPEHASH,
            from,
            to,
            amount,
            valid_after,
            valid_before,
            nonce,
            public_key,
            signature
        );
    }

    /// Like [`Self::transfer_with_authorization`], but requires
    /// `caller == to`. This lets a receiving contract atomically accept a
    /// pre-signed transfer and act on it within the same call (e.g. a
    /// "deposit then mint LP token" flow), while preventing any other
    /// account from triggering the transfer at a moment of their choosing.
    ///
    /// Reverts with [`Error::InvalidCaller`] if `caller != to`. All other
    /// checks and effects mirror `transfer_with_authorization`.
    pub fn receive_with_authorization(
        &mut self,
        from: Address,
        to: Address,
        amount: U256,
        valid_after: u64,
        valid_before: u64,
        nonce: Bytes,
        public_key: PublicKey,
        signature: Bytes
    ) {
        let caller = self.env().caller();
        if caller != to {
            self.env().revert(Error::InvalidCaller);
        }

        self.raw_transfer_with_authorization(
            RECEIVE_WITH_AUTHORIZATION_TYPEHASH,
            from,
            to,
            amount,
            valid_after,
            valid_before,
            nonce,
            public_key,
            signature
        );
    }

    /// Burns an unused authorization nonce by consuming a signed
    /// `CancelAuthorization` message. Once a nonce is cancelled, any
    /// previously distributed signature for the same `(authorizer, nonce)`
    /// pair becomes unredeemable — the canonical way for an authorizer to
    /// revoke a leaked or otherwise unwanted authorization.
    ///
    /// Reverts with [`Error::InvalidNonceLength`] if `nonce` is not 32 bytes,
    /// with [`Error::AuthorizationUsed`] if the nonce has already been consumed
    /// (a cancelled nonce cannot be re-cancelled either), with
    /// [`Error::InvalidPublicKey`] if `public_key` does not hash to
    /// `authorizer`, or with [`Error::InvalidSignature`] if signature
    /// verification fails. On success, marks the nonce used and emits
    /// [`AuthorizationCanceled`].
    pub fn cancel_authorization(
        &mut self,
        authorizer: Address,
        nonce: Bytes,
        public_key: PublicKey,
        signature: Bytes
    ) {
        let Ok(nonce_bytes) = <[u8; 32]>::try_from(nonce.as_slice()) else {
            self.env().revert(Error::InvalidNonceLength)
        };

        if self.authorization_state(authorizer, nonce.clone()) {
            self.env().revert(Error::AuthorizationUsed);
        }

        if Address::from(public_key.clone()) != authorizer {
            self.env().revert(Error::InvalidPublicKey);
        }

        let message = self.build_cancel_message(authorizer, nonce_bytes);
        if !self
            .env()
            .verify_signature(&message, &signature, &public_key)
        {
            self.env().revert(Error::InvalidSignature);
        }

        self.used_nonces.set(&authorizer, &nonce, true);
        self.env()
            .emit_event(AuthorizationCanceled { authorizer, nonce });
    }
}

impl CEP3009 {
    /// Shared body of [`Self::transfer_with_authorization`] and
    /// [`Self::receive_with_authorization`]. They differ only in the EIP-712
    /// typehash used to compute the digest (and in the caller restriction
    /// applied by the receive variant before calling this).
    fn raw_transfer_with_authorization(
        &mut self,
        typehash: [u8; 32],
        from: Address,
        to: Address,
        amount: U256,
        valid_after: u64,
        valid_before: u64,
        nonce: Bytes,
        public_key: PublicKey,
        signature: Bytes
    ) {
        // 1. Enforce a 32-byte nonce so the replay-protection key and the
        // signed digest cover the exact same bytes (no length aliasing).
        let Ok(nonce_bytes) = <[u8; 32]>::try_from(nonce.as_slice()) else {
            self.env().revert(Error::InvalidNonceLength)
        };

        // 2. Replay protection
        if self.used_nonces.get_or_default(&from, &nonce) {
            self.env().revert(Error::NonceAlreadyUsed);
        }

        // 3. block_time
        let now_secs = self.env().get_block_time_secs();

        // 4. Check valid_after
        if now_secs <= valid_after {
            self.env().revert(Error::AuthorizationNotYetValid);
        }

        // 5. Check valid_before
        if now_secs >= valid_before {
            self.env().revert(Error::AuthorizationExpired);
        }

        // 6. Verify that public_key matches the `from` address
        let derived_address = Address::from(public_key.clone());
        if derived_address != from {
            self.env().revert(Error::InvalidPublicKey);
        }

        // 7. Build message and verify signature
        let message = self.build_authorization_message(
            typehash,
            from,
            to,
            &amount,
            valid_after,
            valid_before,
            nonce_bytes
        );

        if !self
            .env()
            .verify_signature(&message, &signature, &public_key)
        {
            self.env().revert(Error::InvalidSignature);
        }

        // 8. Mark nonce as used
        self.used_nonces.set(&from, &nonce, true);

        // 9. Emit event
        self.env().emit_event(AuthorizationUsed {
            authorizer: from,
            nonce
        });

        // 10. Execute transfer (raw_transfer takes refs)
        self.token.raw_transfer(&from, &to, &amount);

        // 11. Emit event.
        self.env().emit_event(Transfer {
            sender: from,
            recipient: to,
            amount
        });
    }

    /// Builds the EIP-712 digest for a transfer authorization. `amount` is
    /// big-endian encoded to match the EVM `uint256` layout, and `nonce` is the
    /// 32-byte `bytes32` value (enforced at every entry point).
    fn build_authorization_message(
        &self,
        typehash: [u8; 32],
        from: Address,
        to: Address,
        amount: &U256,
        valid_after: u64,
        valid_before: u64,
        nonce: [u8; 32]
    ) -> Bytes {
        let mut value_bytes = [0u8; 32];
        amount.to_big_endian(&mut value_bytes);

        let mut encoded_data = Vec::with_capacity(6 * 32);
        encoded_data.extend(eip712::encode_address(from));
        encoded_data.extend(eip712::encode_address(to));
        encoded_data.extend(casper_eip_712::encode_uint256(value_bytes));
        encoded_data.extend(casper_eip_712::encode_uint64(valid_after));
        encoded_data.extend(casper_eip_712::encode_uint64(valid_before));
        encoded_data.extend(casper_eip_712::encode_bytes32(nonce));
        let domain = self.domain_separator();

        Bytes::from(crate::eip712::hash_typed_data(domain, typehash, encoded_data).to_vec())
    }

    /// Builds the EIP-712 digest for a `CancelAuthorization` message. `nonce` is
    /// the 32-byte `bytes32` value (enforced at every entry point).
    fn build_cancel_message(&self, authorizer: Address, nonce: [u8; 32]) -> Bytes {
        let mut encoded_data = Vec::with_capacity(64);
        encoded_data.extend(eip712::encode_address(authorizer));
        encoded_data.extend(casper_eip_712::encode_bytes32(nonce));
        let domain = self.domain_separator();

        Bytes::from(
            eip712::hash_typed_data(domain, CANCEL_AUTHORIZATION_TYPEHASH, encoded_data).to_vec()
        )
    }

    /// Builds the EIP-712 domain separator. Bound to the token `name`, the
    /// fixed `DOMAIN_VERSION`, the configured `chain_name`, and the
    /// deployed contract's own address (the EIP-712 `verifyingContract`).
    fn domain_separator(&self) -> DomainSeparator {
        let self_address = self.env().self_address();
        let name = self.token.name();
        let chain_name = self.chain_name.get();
        eip712::domain_separator(&name, DOMAIN_VERSION, chain_name, self_address)
    }
}

/// Wrapper contract that combines ERC-3009 functionality with a CEP-18 token for testing purposes.
#[odra::module]
pub struct CEP3009Wrapper {
    cep3009: SubModule<CEP3009>,
    token: SubModule<Cep18>
}

#[odra::module]
impl CEP3009Wrapper {
    /// Initializes both the authorization module (storing the EIP-712 chain
    /// id) and the underlying CEP-18 token (symbol, name, decimals, supply).
    pub fn init(
        &mut self,
        chain_name: String,
        symbol: String,
        name: String,
        decimals: u8,
        initial_supply: U256
    ) {
        self.cep3009.init(chain_name);
        self.token.init(symbol, name, decimals, initial_supply);
    }

    delegate! {
        to self.cep3009 {
            fn authorization_state(&self, authorizer: Address, nonce: Bytes) -> bool;
            fn transfer_with_authorization(
                &mut self,
                from: Address,
                to: Address,
                amount: U256,
                valid_after: u64,
                valid_before: u64,
                nonce: Bytes,
                public_key: PublicKey,
                signature: Bytes
            );
            fn receive_with_authorization(
                &mut self,
                from: Address,
                to: Address,
                amount: U256,
                valid_after: u64,
                valid_before: u64,
                nonce: Bytes,
                public_key: PublicKey,
                signature: Bytes
            );
            fn cancel_authorization(
                &mut self,
                authorizer: Address,
                nonce: Bytes,
                public_key: PublicKey,
                signature: Bytes
            );
        }

        to self.token {
            fn balance_of(&self, address: &Address) -> U256;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use odra::host::{Deployer, HostEnv};

    const TOKEN_NAME: &str = "Test Token";
    const TOKEN_SYMBOL: &str = "TEST";
    const TOKEN_DECIMALS: u8 = 8;
    const INITIAL_SUPPLY: u64 = 1_000_000;
    const CHAIN_NAME: &str = "casper:casper";

    struct Setup {
        env: HostEnv,
        wrapper: CEP3009WrapperHostRef,
        alice: Address,
        bob: Address,
        charlie: Address,
        alice_pubkey: PublicKey
    }

    /// Deploys the wrapper with alice as deployer (so she gets the initial supply)
    /// and advances block time past `valid_after = 0`.
    fn setup() -> Setup {
        let env = odra_test::env();
        let alice = env.get_account(0);
        let bob = env.get_account(1);
        let charlie = env.get_account(2);
        let alice_pubkey = env.public_key(&alice);

        let wrapper = CEP3009Wrapper::deploy(
            &env,
            CEP3009WrapperInitArgs {
                chain_name: CHAIN_NAME.to_string(),
                symbol: TOKEN_SYMBOL.to_string(),
                name: TOKEN_NAME.to_string(),
                decimals: TOKEN_DECIMALS,
                initial_supply: INITIAL_SUPPLY.into()
            }
        );

        env.advance_block_time(1_000);

        Setup {
            env,
            wrapper,
            alice,
            bob,
            charlie,
            alice_pubkey
        }
    }

    fn fresh_nonce(seed: u8) -> Bytes {
        Bytes::from(vec![seed; 32])
    }

    #[test]
    fn transfer_with_authorization_happy_path() {
        let Setup {
            env,
            mut wrapper,
            alice,
            bob,
            charlie,
            alice_pubkey
        } = setup();

        assert_eq!(wrapper.balance_of(&alice), INITIAL_SUPPLY.into());
        assert_eq!(wrapper.balance_of(&bob), U256::zero());

        let amount: U256 = 100u64.into();
        let valid_after: u64 = 0;
        let valid_before: u64 = u64::MAX;
        let nonce = fresh_nonce(1);

        let signature = sign_transfer_authorization(
            &env,
            &alice,
            wrapper.address(),
            TRANSFER_WITH_AUTHORIZATION_TYPEHASH,
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            &nonce
        );

        // Charlie relays the authorization on alice's behalf.
        env.set_caller(charlie);
        wrapper.transfer_with_authorization(
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            nonce.clone(),
            alice_pubkey,
            signature
        );

        assert_eq!(
            wrapper.balance_of(&alice),
            U256::from(INITIAL_SUPPLY) - amount
        );
        assert_eq!(wrapper.balance_of(&bob), amount);
        assert!(wrapper.authorization_state(alice, nonce.clone()));
        assert!(env.emitted_event(
            &wrapper,
            AuthorizationUsed {
                authorizer: alice,
                nonce
            }
        ));
    }

    #[test]
    fn transfer_with_authorization_replay_protection() {
        let Setup {
            env,
            mut wrapper,
            alice,
            bob,
            charlie,
            alice_pubkey
        } = setup();

        let amount: U256 = 100u64.into();
        let valid_after: u64 = 0;
        let valid_before: u64 = u64::MAX;
        let nonce = fresh_nonce(2);

        let signature = sign_transfer_authorization(
            &env,
            &alice,
            wrapper.address(),
            TRANSFER_WITH_AUTHORIZATION_TYPEHASH,
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            &nonce
        );

        env.set_caller(charlie);
        wrapper.transfer_with_authorization(
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            nonce.clone(),
            alice_pubkey.clone(),
            signature.clone()
        );

        // Submitting the same authorization a second time reverts.
        assert_eq!(
            wrapper.try_transfer_with_authorization(
                alice,
                bob,
                amount,
                valid_after,
                valid_before,
                nonce,
                alice_pubkey,
                signature
            ),
            Err(Error::NonceAlreadyUsed.into())
        );
    }

    #[test]
    fn transfer_with_authorization_not_yet_valid() {
        let Setup {
            env,
            mut wrapper,
            alice,
            bob,
            charlie,
            alice_pubkey
        } = setup();

        // Block time is at 1 second; require it to be in the future.
        let valid_after: u64 = 60;
        let valid_before: u64 = u64::MAX;
        let amount: U256 = 100u64.into();
        let nonce = fresh_nonce(3);

        let signature = sign_transfer_authorization(
            &env,
            &alice,
            wrapper.address(),
            TRANSFER_WITH_AUTHORIZATION_TYPEHASH,
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            &nonce
        );

        env.set_caller(charlie);
        assert_eq!(
            wrapper.try_transfer_with_authorization(
                alice,
                bob,
                amount,
                valid_after,
                valid_before,
                nonce,
                alice_pubkey,
                signature
            ),
            Err(Error::AuthorizationNotYetValid.into())
        );
    }

    #[test]
    fn transfer_with_authorization_expired() {
        let Setup {
            env,
            mut wrapper,
            alice,
            bob,
            charlie,
            alice_pubkey
        } = setup();

        // Move further into the future so that valid_before is in the past.
        env.advance_block_time(60_000);

        let valid_after: u64 = 0;
        let valid_before: u64 = 10;
        let amount: U256 = 100u64.into();
        let nonce = fresh_nonce(4);

        let signature = sign_transfer_authorization(
            &env,
            &alice,
            wrapper.address(),
            TRANSFER_WITH_AUTHORIZATION_TYPEHASH,
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            &nonce
        );

        env.set_caller(charlie);
        assert_eq!(
            wrapper.try_transfer_with_authorization(
                alice,
                bob,
                amount,
                valid_after,
                valid_before,
                nonce,
                alice_pubkey,
                signature
            ),
            Err(Error::AuthorizationExpired.into())
        );
    }

    #[test]
    fn transfer_with_authorization_invalid_signature() {
        let Setup {
            env,
            mut wrapper,
            alice,
            bob,
            charlie,
            alice_pubkey
        } = setup();

        let valid_after: u64 = 0;
        let valid_before: u64 = u64::MAX;
        let nonce = fresh_nonce(5);

        // Alice signs for an amount of 100 ...
        let signature = sign_transfer_authorization(
            &env,
            &alice,
            wrapper.address(),
            TRANSFER_WITH_AUTHORIZATION_TYPEHASH,
            alice,
            bob,
            U256::from(100u64),
            valid_after,
            valid_before,
            &nonce
        );

        // ... but charlie submits with a different amount, so the digest the
        // contract recomputes won't match the signature.
        env.set_caller(charlie);
        assert_eq!(
            wrapper.try_transfer_with_authorization(
                alice,
                bob,
                U256::from(200u64),
                valid_after,
                valid_before,
                nonce,
                alice_pubkey,
                signature
            ),
            Err(Error::InvalidSignature.into())
        );
    }

    #[test]
    fn transfer_with_authorization_public_key_mismatch() {
        let Setup {
            env,
            mut wrapper,
            alice,
            bob,
            charlie,
            ..
        } = setup();

        let valid_after: u64 = 0;
        let valid_before: u64 = u64::MAX;
        let amount: U256 = 100u64.into();
        let nonce = fresh_nonce(6);

        // Sign with bob's key while declaring alice as `from`.
        let bob_pubkey = env.public_key(&bob);
        let signature = sign_transfer_authorization(
            &env,
            &bob,
            wrapper.address(),
            TRANSFER_WITH_AUTHORIZATION_TYPEHASH,
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            &nonce
        );

        env.set_caller(charlie);
        assert_eq!(
            wrapper.try_transfer_with_authorization(
                alice,
                bob,
                amount,
                valid_after,
                valid_before,
                nonce,
                bob_pubkey,
                signature
            ),
            Err(Error::InvalidPublicKey.into())
        );
    }

    #[test]
    fn receive_with_authorization_happy_path() {
        let Setup {
            env,
            mut wrapper,
            alice,
            bob,
            alice_pubkey,
            ..
        } = setup();

        let amount: U256 = 250u64.into();
        let valid_after: u64 = 0;
        let valid_before: u64 = u64::MAX;
        let nonce = fresh_nonce(7);

        let signature = sign_transfer_authorization(
            &env,
            &alice,
            wrapper.address(),
            RECEIVE_WITH_AUTHORIZATION_TYPEHASH,
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            &nonce
        );

        // Bob (the recipient) must be the caller for receive_with_authorization.
        env.set_caller(bob);
        wrapper.receive_with_authorization(
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            nonce.clone(),
            alice_pubkey,
            signature
        );

        assert_eq!(
            wrapper.balance_of(&alice),
            U256::from(INITIAL_SUPPLY) - amount
        );
        assert_eq!(wrapper.balance_of(&bob), amount);
        assert!(wrapper.authorization_state(alice, nonce));
    }

    #[test]
    fn receive_with_authorization_wrong_caller_reverts() {
        let Setup {
            env,
            mut wrapper,
            alice,
            bob,
            charlie,
            alice_pubkey
        } = setup();

        let amount: U256 = 250u64.into();
        let valid_after: u64 = 0;
        let valid_before: u64 = u64::MAX;
        let nonce = fresh_nonce(8);

        let signature = sign_transfer_authorization(
            &env,
            &alice,
            wrapper.address(),
            RECEIVE_WITH_AUTHORIZATION_TYPEHASH,
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            &nonce
        );

        // Charlie tries to relay even though `to` is bob.
        env.set_caller(charlie);
        assert_eq!(
            wrapper.try_receive_with_authorization(
                alice,
                bob,
                amount,
                valid_after,
                valid_before,
                nonce,
                alice_pubkey,
                signature
            ),
            Err(Error::InvalidCaller.into())
        );
    }

    #[test]
    fn cancel_authorization_happy_path() {
        let Setup {
            env,
            mut wrapper,
            alice,
            bob,
            charlie,
            alice_pubkey
        } = setup();

        let nonce = fresh_nonce(9);
        let cancel_signature =
            sign_cancel_authorization(&env, &alice, wrapper.address(), alice, &nonce);

        env.set_caller(charlie);
        wrapper.cancel_authorization(alice, nonce.clone(), alice_pubkey.clone(), cancel_signature);

        assert!(wrapper.authorization_state(alice, nonce.clone()));
        assert!(env.emitted_event(
            &wrapper,
            AuthorizationCanceled {
                authorizer: alice,
                nonce: nonce.clone()
            }
        ));

        // A subsequent transfer reusing the cancelled nonce is rejected.
        let amount: U256 = 1u64.into();
        let valid_after: u64 = 0;
        let valid_before: u64 = u64::MAX;
        let transfer_signature = sign_transfer_authorization(
            &env,
            &alice,
            wrapper.address(),
            TRANSFER_WITH_AUTHORIZATION_TYPEHASH,
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            &nonce
        );
        assert_eq!(
            wrapper.try_transfer_with_authorization(
                alice,
                bob,
                amount,
                valid_after,
                valid_before,
                nonce,
                alice_pubkey,
                transfer_signature
            ),
            Err(Error::NonceAlreadyUsed.into())
        );
    }

    #[test]
    fn cancel_authorization_already_used_reverts() {
        let Setup {
            env,
            mut wrapper,
            alice,
            bob,
            charlie,
            alice_pubkey
        } = setup();

        let amount: U256 = 100u64.into();
        let valid_after: u64 = 0;
        let valid_before: u64 = u64::MAX;
        let nonce = fresh_nonce(10);

        // Consume the nonce via a successful transfer first.
        let transfer_signature = sign_transfer_authorization(
            &env,
            &alice,
            wrapper.address(),
            TRANSFER_WITH_AUTHORIZATION_TYPEHASH,
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            &nonce
        );
        env.set_caller(charlie);
        wrapper.transfer_with_authorization(
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            nonce.clone(),
            alice_pubkey.clone(),
            transfer_signature
        );

        // Cancelling an already-consumed nonce reverts.
        let cancel_signature =
            sign_cancel_authorization(&env, &alice, wrapper.address(), alice, &nonce);
        assert_eq!(
            wrapper.try_cancel_authorization(alice, nonce, alice_pubkey, cancel_signature),
            Err(Error::AuthorizationUsed.into())
        );
    }

    #[test]
    fn cancel_authorization_with_mismatched_public_key_reverts() {
        let Setup {
            env,
            mut wrapper,
            alice,
            bob,
            charlie,
            ..
        } = setup();

        let nonce = fresh_nonce(11);

        // Bob signs a cancel message that names alice as the authorizer,
        // using his own keypair.
        let bob_pubkey = env.public_key(&bob);
        let cancel_signature =
            sign_cancel_authorization(&env, &bob, wrapper.address(), alice, &nonce);

        // Submitting with authorizer=alice but public_key=bob_pubkey must fail,
        // otherwise an attacker could burn alice's nonces.
        env.set_caller(charlie);
        assert_eq!(
            wrapper.try_cancel_authorization(alice, nonce.clone(), bob_pubkey, cancel_signature),
            Err(Error::InvalidPublicKey.into())
        );

        // The nonce must remain unused so alice's pending authorization is not bricked.
        assert!(!wrapper.authorization_state(alice, nonce));
    }

    #[test]
    fn transfer_with_authorization_rejects_appended_byte_nonce_alias() {
        let Setup {
            env,
            mut wrapper,
            alice,
            bob,
            charlie,
            alice_pubkey
        } = setup();

        let amount: U256 = 100u64.into();
        let valid_after: u64 = 0;
        let valid_before: u64 = u64::MAX;
        let nonce = fresh_nonce(20);

        let signature = sign_transfer_authorization(
            &env,
            &alice,
            wrapper.address(),
            TRANSFER_WITH_AUTHORIZATION_TYPEHASH,
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            &nonce
        );

        // First redemption with the canonical 32-byte nonce succeeds.
        env.set_caller(charlie);
        wrapper.transfer_with_authorization(
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            nonce.clone(),
            alice_pubkey.clone(),
            signature.clone()
        );

        // The same signature with a byte appended to the nonce hashes to the
        // same 32-byte digest (the digest only covers 32 bytes) but produces a
        // distinct replay-protection storage key. It must be rejected outright
        // rather than allowing a second redemption.
        let mut aliased = nonce.to_vec();
        aliased.push(0);
        let aliased_nonce = Bytes::from(aliased);

        assert_eq!(
            wrapper.try_transfer_with_authorization(
                alice,
                bob,
                amount,
                valid_after,
                valid_before,
                aliased_nonce,
                alice_pubkey,
                signature
            ),
            Err(Error::InvalidNonceLength.into())
        );

        // The funds moved exactly once.
        assert_eq!(wrapper.balance_of(&bob), amount);
    }

    #[test]
    fn transfer_with_authorization_rejects_short_nonce() {
        let Setup {
            env,
            mut wrapper,
            alice,
            bob,
            charlie,
            alice_pubkey
        } = setup();

        let amount: U256 = 100u64.into();
        let valid_after: u64 = 0;
        let valid_before: u64 = u64::MAX;
        // A 31-byte nonce that the digest would right-pad to 32 bytes.
        let nonce = Bytes::from(vec![21u8; 31]);

        let signature = sign_transfer_authorization(
            &env,
            &alice,
            wrapper.address(),
            TRANSFER_WITH_AUTHORIZATION_TYPEHASH,
            alice,
            bob,
            amount,
            valid_after,
            valid_before,
            &nonce
        );

        env.set_caller(charlie);
        assert_eq!(
            wrapper.try_transfer_with_authorization(
                alice,
                bob,
                amount,
                valid_after,
                valid_before,
                nonce,
                alice_pubkey,
                signature
            ),
            Err(Error::InvalidNonceLength.into())
        );
    }

    #[test]
    fn cancel_authorization_rejects_nonstandard_nonce() {
        let Setup {
            env,
            mut wrapper,
            alice,
            charlie,
            alice_pubkey,
            ..
        } = setup();

        // An oversized (33-byte) nonce aliases a 32-byte cancel digest.
        let nonce = Bytes::from(vec![22u8; 33]);
        let cancel_signature =
            sign_cancel_authorization(&env, &alice, wrapper.address(), alice, &nonce);

        env.set_caller(charlie);
        assert_eq!(
            wrapper.try_cancel_authorization(alice, nonce.clone(), alice_pubkey, cancel_signature),
            Err(Error::InvalidNonceLength.into())
        );

        // The aliased nonce must not be recorded as used.
        assert!(!wrapper.authorization_state(alice, nonce));
    }

    fn sign_transfer_authorization(
        env: &HostEnv,
        signer: &Address,
        contract_address: Address,
        typehash: [u8; 32],
        from: Address,
        to: Address,
        amount: U256,
        valid_after: u64,
        valid_before: u64,
        nonce: &Bytes
    ) -> Bytes {
        let mut value_bytes = [0u8; 32];
        amount.to_big_endian(&mut value_bytes);

        let mut nonce_padded = [0u8; 32];
        let len = nonce.len().min(32);
        nonce_padded[..len].copy_from_slice(&nonce[..len]);

        let mut encoded_data = Vec::with_capacity(6 * 32);
        encoded_data.extend(crate::eip712::encode_address(from));
        encoded_data.extend(crate::eip712::encode_address(to));
        encoded_data.extend(casper_eip_712::encode_uint256(value_bytes));
        encoded_data.extend(casper_eip_712::encode_uint64(valid_after));
        encoded_data.extend(casper_eip_712::encode_uint64(valid_before));
        encoded_data.extend(casper_eip_712::encode_bytes32(nonce_padded));

        let domain = crate::eip712::domain_separator(
            TOKEN_NAME,
            DOMAIN_VERSION,
            CHAIN_NAME.to_string(),
            contract_address
        );
        let message_hash = crate::eip712::hash_typed_data(domain, typehash, encoded_data);
        let message = Bytes::from(message_hash.to_vec());

        env.sign_message(&message, signer)
    }

    fn sign_cancel_authorization(
        env: &HostEnv,
        signer: &Address,
        contract_address: Address,
        authorizer: Address,
        nonce: &Bytes
    ) -> Bytes {
        let mut nonce_padded = [0u8; 32];
        let len = nonce.len().min(32);
        nonce_padded[..len].copy_from_slice(&nonce[..len]);

        let mut encoded_data = Vec::with_capacity(64);
        encoded_data.extend(crate::eip712::encode_address(authorizer));
        encoded_data.extend(casper_eip_712::encode_bytes32(nonce_padded));

        let domain = crate::eip712::domain_separator(
            TOKEN_NAME,
            DOMAIN_VERSION,
            CHAIN_NAME.to_string(),
            contract_address
        );
        let message_hash =
            crate::eip712::hash_typed_data(domain, CANCEL_AUTHORIZATION_TYPEHASH, encoded_data);
        let message = Bytes::from(message_hash.to_vec());

        env.sign_message(&message, signer)
    }
}