1
+ #ifdef HAVE_EIP7702
2
+
3
+ #include "auth_7702.h"
4
+ #include "utils.h"
5
+ #include "read.h"
6
+
7
+ enum {
8
+ TAG_STRUCT_VERSION = 0x00 ,
9
+ TAG_DERIVATION_IDX = 0x01 ,
10
+ TAG_DELEGATE_ADDR = 0x02 ,
11
+ TAG_CHAIN_ID = 0x03 ,
12
+ TAG_NONCE = 0x04 ,
13
+ };
14
+
15
+ enum {
16
+ BIT_VERSION ,
17
+ BIT_DERIVATION_IDX ,
18
+ BIT_DELEGATE_ADDR ,
19
+ BIT_CHAIN_ID ,
20
+ BIT_NONCE ,
21
+ };
22
+
23
+ #define STRUCT_VERSION 0x01
24
+ #define MASK_ALL \
25
+ (SET_BIT(BIT_VERSION) | SET_BIT(BIT_DERIVATION_IDX) | SET_BIT(BIT_DELEGATE_ADDR) | \
26
+ SET_BIT(BIT_CHAIN_ID) | SET_BIT(BIT_NONCE))
27
+
28
+ static bool handle_version (const s_tlv_data * data , s_auth_7702_ctx * context ) {
29
+ if (data -> length != sizeof (uint8_t )) {
30
+ return false;
31
+ }
32
+ context -> mask_parsed |= SET_BIT (BIT_VERSION );
33
+ return (data -> value [0 ] == STRUCT_VERSION );
34
+ }
35
+
36
+ static bool handle_derivation_idx (const s_tlv_data * data , s_auth_7702_ctx * context ) {
37
+ uint32_t idx ;
38
+ if (data -> length != sizeof (uint32_t )) {
39
+ return false;
40
+ }
41
+ if (context -> auth_7702 .bip32 .length == MAX_BIP32_PATH ) {
42
+ return false;
43
+ }
44
+ idx = read_u32_be (data -> value , 0 );
45
+ context -> auth_7702 .bip32 .path [context -> auth_7702 .bip32 .length ] = idx ;
46
+ context -> auth_7702 .bip32 .length ++ ;
47
+ context -> mask_parsed |= SET_BIT (BIT_DERIVATION_IDX );
48
+ return true;
49
+ }
50
+
51
+ static bool handle_delegate_addr (const s_tlv_data * data , s_auth_7702_ctx * context ) {
52
+ if (data -> length != sizeof (context -> auth_7702 .delegate )) {
53
+ return false;
54
+ }
55
+ memmove (context -> auth_7702 .delegate , data -> value , sizeof (context -> auth_7702 .delegate ));
56
+ context -> mask_parsed |= SET_BIT (BIT_DELEGATE_ADDR );
57
+ return true;
58
+ }
59
+
60
+ static bool handle_chain_id (const s_tlv_data * data , s_auth_7702_ctx * context ) {
61
+ if (data -> length != sizeof (uint64_t )) {
62
+ return false;
63
+ }
64
+ context -> auth_7702 .chainId = read_u64_be (data -> value , 0 );
65
+ context -> mask_parsed |= SET_BIT (BIT_CHAIN_ID );
66
+ return true;
67
+ }
68
+
69
+ static bool handle_nonce (const s_tlv_data * data , s_auth_7702_ctx * context ) {
70
+ if (data -> length != sizeof (uint64_t )) {
71
+ return false;
72
+ }
73
+ context -> auth_7702 .nonce = read_u64_be (data -> value , 0 );
74
+ context -> mask_parsed |= SET_BIT (BIT_NONCE );
75
+ return true;
76
+ }
77
+
78
+ bool handle_auth_7702_struct (const s_tlv_data * data , s_auth_7702_ctx * context ) {
79
+ bool ret ;
80
+
81
+ switch (data -> tag ) {
82
+ case TAG_STRUCT_VERSION :
83
+ ret = handle_version (data , context );
84
+ break ;
85
+ case TAG_DERIVATION_IDX :
86
+ ret = handle_derivation_idx (data , context );
87
+ break ;
88
+ case TAG_DELEGATE_ADDR :
89
+ ret = handle_delegate_addr (data , context );
90
+ break ;
91
+ case TAG_CHAIN_ID :
92
+ ret = handle_chain_id (data , context );
93
+ break ;
94
+ case TAG_NONCE :
95
+ ret = handle_nonce (data , context );
96
+ break ;
97
+ default :
98
+ PRINTF (TLV_TAG_ERROR_MSG , data -> tag );
99
+ ret = false;
100
+ }
101
+ return ret ;
102
+ }
103
+
104
+ bool verify_auth_7702_struct (s_auth_7702_ctx * context ) {
105
+ return ((context -> mask_parsed & MASK_ALL ) == MASK_ALL );
106
+ }
107
+
108
+ #endif // HAVE_EIP7702
0 commit comments