@@ -27,7 +27,9 @@ pub struct VM {
27
27
pub payload : ByteArray ,
28
28
}
29
29
30
- pub mod error_codes {
30
+ pub mod errors {
31
+ pub use pyth :: reader :: errors as reader;
32
+
31
33
pub const NO_GUARDIANS_SPECIFIED : felt252 = ' no guardians specified' ;
32
34
pub const TOO_MANY_GUARDIANS : felt252 = ' too many guardians' ;
33
35
pub const INVALID_GUARDIAN_KEY : felt252 = ' invalid guardian key' ;
@@ -45,15 +47,15 @@ pub mod error_codes {
45
47
}
46
48
47
49
pub fn quorum (num_guardians : usize ) -> usize {
48
- assert (num_guardians < 256 , error_codes :: TOO_MANY_GUARDIANS );
50
+ assert (num_guardians < 256 , errors :: TOO_MANY_GUARDIANS );
49
51
((num_guardians * 2 ) / 3 ) + 1
50
52
}
51
53
52
54
#[starknet:: contract]
53
55
mod wormhole {
54
56
use core :: box :: BoxTrait ;
55
57
use core :: array :: ArrayTrait ;
56
- use super :: {VM , IWormhole , GuardianSignature , error_codes , quorum};
58
+ use super :: {VM , IWormhole , GuardianSignature , errors , quorum};
57
59
use pyth :: reader :: {Reader , ReaderImpl , ByteArray };
58
60
use core :: starknet :: secp256_trait :: {Signature , recover_public_key, Secp256PointTrait };
59
61
use core :: starknet :: secp256k1 :: Secp256k1Point ;
@@ -92,14 +94,14 @@ mod wormhole {
92
94
}
93
95
94
96
fn store_guardian_set (ref self : ContractState , set_index : u32 , guardians : Array <felt252 >) {
95
- assert (guardians . len () > 0 , error_codes :: NO_GUARDIANS_SPECIFIED );
96
- assert (guardians . len () < 256 , error_codes :: TOO_MANY_GUARDIANS );
97
+ assert (guardians . len () > 0 , errors :: NO_GUARDIANS_SPECIFIED );
98
+ assert (guardians . len () < 256 , errors :: TOO_MANY_GUARDIANS );
97
99
let set = GuardianSet { num_guardians : guardians . len (), expiration_time : 0 };
98
100
self . guardian_sets. write (set_index , set );
99
101
let mut i = 0 ;
100
102
while i < guardians . len () {
101
103
let key = * guardians . at (i );
102
- assert (key != 0 , error_codes :: INVALID_GUARDIAN_KEY );
104
+ assert (key != 0 , errors :: INVALID_GUARDIAN_KEY );
103
105
// i < 256
104
106
self
105
107
. guardian_keys
@@ -121,10 +123,10 @@ mod wormhole {
121
123
ref self : ContractState , set_index : u32 , guardians : Array <felt252 >
122
124
) {
123
125
let execution_info = get_execution_info (). unbox ();
124
- assert (self . owner. read () == execution_info . caller_address, error_codes :: ACCESS_DENIED );
126
+ assert (self . owner. read () == execution_info . caller_address, errors :: ACCESS_DENIED );
125
127
126
128
let current_set_index = self . current_guardian_set_index. read ();
127
- assert (set_index == current_set_index + 1 , error_codes :: INVALID_GUARDIAN_SET_SEQUENCE );
129
+ assert (set_index == current_set_index + 1 , errors :: INVALID_GUARDIAN_SET_SEQUENCE );
128
130
expire_guardian_set (
129
131
ref self , current_set_index , execution_info . block_info. unbox (). block_timestamp
130
132
);
@@ -137,14 +139,14 @@ mod wormhole {
137
139
let (vm , body_hash ) = parse_vm (encoded_vm )? ;
138
140
let guardian_set = self . guardian_sets. read (vm . guardian_set_index);
139
141
if guardian_set . num_guardians == 0 {
140
- return Result :: Err (error_codes :: INVALID_GUARDIAN_SET_INDEX );
142
+ return Result :: Err (errors :: INVALID_GUARDIAN_SET_INDEX );
141
143
}
142
144
if vm . guardian_set_index != self . current_guardian_set_index. read ()
143
145
&& guardian_set . expiration_time < get_block_timestamp () {
144
- return Result :: Err (error_codes :: GUARDIAN_SET_EXPIRED );
146
+ return Result :: Err (errors :: GUARDIAN_SET_EXPIRED );
145
147
}
146
148
if vm . signatures. len () < quorum (guardian_set . num_guardians) {
147
- return Result :: Err (error_codes :: NO_QUORUM );
149
+ return Result :: Err (errors :: NO_QUORUM );
148
150
}
149
151
let mut signatures_clone = vm . signatures. clone ();
150
152
let mut last_index = Option :: None ;
@@ -159,7 +161,7 @@ mod wormhole {
159
161
match last_index {
160
162
Option :: Some (last_index ) => {
161
163
if * (@ signature ). guardian_index <= last_index {
162
- result = Result :: Err (error_codes :: INVALID_SIGNATURE_ORDER );
164
+ result = Result :: Err (errors :: INVALID_SIGNATURE_ORDER );
163
165
break ;
164
166
}
165
167
},
@@ -168,7 +170,7 @@ mod wormhole {
168
170
last_index = Option :: Some (* (@ signature ). guardian_index);
169
171
170
172
if signature . guardian_index. into () >= guardian_set . num_guardians {
171
- result = Result :: Err (error_codes :: INVALID_GUARDIAN_INDEX );
173
+ result = Result :: Err (errors :: INVALID_GUARDIAN_INDEX );
172
174
break ;
173
175
}
174
176
@@ -204,7 +206,7 @@ mod wormhole {
204
206
let mut reader = ReaderImpl :: new (encoded_vm );
205
207
let version = reader . read_u8 ()? ;
206
208
if version != 1 {
207
- return Result :: Err (error_codes :: VM_VERSION_INCOMPATIBLE );
209
+ return Result :: Err (errors :: VM_VERSION_INCOMPATIBLE );
208
210
}
209
211
let guardian_set_index = reader . read_u32 ()? ;
210
212
@@ -261,21 +263,21 @@ mod wormhole {
261
263
body_hash : u256 , signature : Signature , guardian_key : u256 ,
262
264
) -> Result <(), felt252 > {
263
265
let point : Secp256k1Point = recover_public_key (body_hash , signature )
264
- . ok_or (error_codes :: INVALID_SIGNATURE )? ;
266
+ . ok_or (errors :: INVALID_SIGNATURE )? ;
265
267
let address = eth_address (point )? ;
266
268
if guardian_key == 0 {
267
- return Result :: Err (error_codes :: INVALID_GUARDIAN_KEY );
269
+ return Result :: Err (errors :: INVALID_GUARDIAN_KEY );
268
270
}
269
271
if address != guardian_key {
270
- return Result :: Err (error_codes :: INVALID_SIGNATURE );
272
+ return Result :: Err (errors :: INVALID_SIGNATURE );
271
273
}
272
274
Result :: Ok (())
273
275
}
274
276
275
277
fn eth_address (point : Secp256k1Point ) -> Result <u256 , felt252 > {
276
278
let (x , y ) = match point . get_coordinates () {
277
279
Result :: Ok (v ) => { v },
278
- Result :: Err (_ ) => { return Result :: Err (error_codes :: INVALID_SIGNATURE ); },
280
+ Result :: Err (_ ) => { return Result :: Err (errors :: INVALID_SIGNATURE ); },
279
281
};
280
282
281
283
let mut hasher = HasherImpl :: new ();
0 commit comments