@@ -4,7 +4,7 @@ use stellar_axelar_std::events::Event;
4
4
5
5
use crate :: error:: ContractError ;
6
6
use crate :: event:: SignersRotatedEvent ;
7
- use crate :: storage_types :: DataKey ;
7
+ use crate :: storage ;
8
8
use crate :: types:: { Proof , ProofSignature , ProofSigner , WeightedSigner , WeightedSigners } ;
9
9
10
10
pub fn initialize_auth (
@@ -14,20 +14,10 @@ pub fn initialize_auth(
14
14
previous_signer_retention : u64 ,
15
15
initial_signers : Vec < WeightedSigners > ,
16
16
) -> Result < ( ) , ContractError > {
17
- env. storage ( ) . instance ( ) . set ( & DataKey :: Epoch , & 0_u64 ) ;
18
-
19
- env. storage ( ) . instance ( ) . set (
20
- & DataKey :: PreviousSignerRetention ,
21
- & previous_signer_retention,
22
- ) ;
23
-
24
- env. storage ( )
25
- . instance ( )
26
- . set ( & DataKey :: DomainSeparator , & domain_separator) ;
27
-
28
- env. storage ( )
29
- . instance ( )
30
- . set ( & DataKey :: MinimumRotationDelay , & minimum_rotation_delay) ;
17
+ storage:: set_epoch ( & env, & 0_u64 ) ;
18
+ storage:: set_previous_signer_retention ( & env, & previous_signer_retention) ;
19
+ storage:: set_domain_separator ( & env, & domain_separator) ;
20
+ storage:: set_minimum_rotation_delay ( & env, & minimum_rotation_delay) ;
31
21
32
22
ensure ! ( !initial_signers. is_empty( ) , ContractError :: EmptySigners ) ;
33
23
@@ -38,27 +28,6 @@ pub fn initialize_auth(
38
28
Ok ( ( ) )
39
29
}
40
30
41
- pub fn domain_separator ( env : & Env ) -> BytesN < 32 > {
42
- env. storage ( )
43
- . instance ( )
44
- . get ( & DataKey :: DomainSeparator )
45
- . expect ( "domain_separator not found" )
46
- }
47
-
48
- pub fn minimum_rotation_delay ( env : & Env ) -> u64 {
49
- env. storage ( )
50
- . instance ( )
51
- . get ( & DataKey :: MinimumRotationDelay )
52
- . expect ( "minimum_rotation_delay not found" )
53
- }
54
-
55
- pub fn previous_signers_retention ( env : & Env ) -> u64 {
56
- env. storage ( )
57
- . instance ( )
58
- . get ( & DataKey :: PreviousSignerRetention )
59
- . expect ( "previous_signers_retention not found" )
60
- }
61
-
62
31
pub fn validate_proof (
63
32
env : & Env ,
64
33
data_hash : & BytesN < 32 > ,
@@ -68,14 +37,15 @@ pub fn validate_proof(
68
37
69
38
let signers_hash = signers_set. hash ( env) ;
70
39
71
- let signers_epoch = epoch_by_signers_hash ( env, signers_hash. clone ( ) ) ?;
40
+ let signers_epoch = storage:: try_epoch_by_signers_hash ( env, signers_hash. clone ( ) )
41
+ . ok_or ( ContractError :: InvalidSignersHash ) ?;
72
42
73
- let current_epoch = epoch ( env) ;
43
+ let current_epoch = storage :: epoch ( env) ;
74
44
75
45
let is_latest_signers: bool = signers_epoch == current_epoch;
76
46
77
47
ensure ! (
78
- current_epoch - signers_epoch <= previous_signers_retention ( env) ,
48
+ current_epoch - signers_epoch <= storage :: previous_signer_retention ( env) ,
79
49
ContractError :: OutdatedSigners
80
50
) ;
81
51
@@ -100,23 +70,20 @@ pub fn rotate_signers(
100
70
101
71
let new_signers_hash = new_signers. hash ( env) ;
102
72
103
- let new_epoch: u64 = epoch ( env) + 1 ;
73
+ let new_epoch: u64 = storage :: epoch ( env) + 1 ;
104
74
105
- env . storage ( ) . instance ( ) . set ( & DataKey :: Epoch , & new_epoch) ;
75
+ storage:: set_epoch ( env , & new_epoch) ;
106
76
107
- env. storage ( )
108
- . persistent ( )
109
- . set ( & DataKey :: SignersHashByEpoch ( new_epoch) , & new_signers_hash) ;
77
+ storage:: set_signers_hash_by_epoch ( env, new_epoch, & new_signers_hash) ;
110
78
111
79
ensure ! (
112
- epoch_by_signers_hash( env, new_signers_hash. clone( ) ) . is_err( ) ,
80
+ storage:: try_epoch_by_signers_hash( env, new_signers_hash. clone( ) )
81
+ . ok_or( ContractError :: InvalidSignersHash )
82
+ . is_err( ) ,
113
83
ContractError :: DuplicateSigners
114
84
) ;
115
85
116
- env. storage ( ) . persistent ( ) . set (
117
- & DataKey :: EpochBySignersHash ( new_signers_hash. clone ( ) ) ,
118
- & new_epoch,
119
- ) ;
86
+ storage:: set_epoch_by_signers_hash ( env, new_signers_hash. clone ( ) , & new_epoch) ;
120
87
121
88
SignersRotatedEvent {
122
89
epoch : new_epoch,
@@ -128,54 +95,26 @@ pub fn rotate_signers(
128
95
Ok ( ( ) )
129
96
}
130
97
131
- pub fn epoch ( env : & Env ) -> u64 {
132
- env. storage ( )
133
- . instance ( )
134
- . get ( & DataKey :: Epoch )
135
- . expect ( "epoch not found" )
136
- }
137
-
138
- pub fn epoch_by_signers_hash ( env : & Env , signers_hash : BytesN < 32 > ) -> Result < u64 , ContractError > {
139
- env. storage ( )
140
- . persistent ( )
141
- . get ( & DataKey :: EpochBySignersHash ( signers_hash) )
142
- . ok_or ( ContractError :: InvalidSignersHash )
143
- }
144
-
145
- pub fn signers_hash_by_epoch ( env : & Env , epoch : u64 ) -> Result < BytesN < 32 > , ContractError > {
146
- env. storage ( )
147
- . persistent ( )
148
- . get ( & DataKey :: SignersHashByEpoch ( epoch) )
149
- . ok_or ( ContractError :: InvalidEpoch )
150
- }
151
-
152
98
fn message_hash_to_sign ( env : & Env , signers_hash : BytesN < 32 > , data_hash : & BytesN < 32 > ) -> BytesN < 32 > {
153
- let mut msg: Bytes = domain_separator ( env) . into ( ) ;
99
+ let mut msg: Bytes = storage :: domain_separator ( env) . into ( ) ;
154
100
msg. extend_from_array ( & signers_hash. to_array ( ) ) ;
155
101
msg. extend_from_array ( & data_hash. to_array ( ) ) ;
156
102
157
103
env. crypto ( ) . keccak256 ( & msg) . into ( )
158
104
}
159
105
160
106
fn update_rotation_timestamp ( env : & Env , enforce_rotation_delay : bool ) -> Result < ( ) , ContractError > {
161
- let last_rotation_timestamp: u64 = env
162
- . storage ( )
163
- . instance ( )
164
- . get ( & DataKey :: LastRotationTimestamp )
165
- . unwrap_or ( 0 ) ;
166
-
167
107
let current_timestamp = env. ledger ( ) . timestamp ( ) ;
168
108
169
109
if enforce_rotation_delay {
170
110
ensure ! (
171
- current_timestamp - last_rotation_timestamp >= minimum_rotation_delay( env) ,
111
+ current_timestamp - storage:: last_rotation_timestamp( env)
112
+ >= storage:: minimum_rotation_delay( env) ,
172
113
ContractError :: InsufficientRotationDelay
173
114
) ;
174
115
}
175
116
176
- env. storage ( )
177
- . instance ( )
178
- . set ( & DataKey :: LastRotationTimestamp , & current_timestamp) ;
117
+ storage:: set_last_rotation_timestamp ( env, & current_timestamp) ;
179
118
180
119
Ok ( ( ) )
181
120
}
0 commit comments