@@ -28,6 +28,8 @@ lazy_static! {
2828 include_bytes!( "../../lib/protocol-eth/abi/AdExCore.json" ) ;
2929 static ref IDENTITY_ABI : & ' static [ u8 ] =
3030 include_bytes!( "../../lib/protocol-eth/abi/Identity.json" ) ;
31+ static ref CHANNEL_STATE_ACTIVE : U256 = 1 . into( ) ;
32+ static ref PRIVILEGE_LEVEL_NONE : U256 = 0 . into( ) ;
3133}
3234
3335#[ derive( Debug , Clone ) ]
@@ -124,7 +126,7 @@ impl Adapter for EthereumAdapter {
124126 fn verify ( & self , signer : & str , state_root : & str , sig : & str ) -> AdapterResult < bool > {
125127 let ( decoded_adress, decoded_signature) = match ( hex:: decode ( signer) , hex:: decode ( sig) ) {
126128 ( Ok ( address) , Ok ( sig) ) => ( address, sig) ,
127- ( _ , _ ) => {
129+ _ => {
128130 return Err ( AdapterError :: Signature (
129131 "invalid signature or address" . to_string ( ) ,
130132 ) )
@@ -135,10 +137,7 @@ impl Adapter for EthereumAdapter {
135137 let signature = Signature :: from_electrum ( & decoded_signature) ;
136138 let message = Message :: from_slice ( & hash_message ( state_root) ) ;
137139
138- match verify_address ( & address, & signature, & message) {
139- Ok ( result) => Ok ( result) ,
140- Err ( _) => Ok ( false ) ,
141- }
140+ verify_address ( & address, & signature, & message) . or ( Ok ( false ) )
142141 }
143142
144143 fn validate_channel ( & self , channel : & Channel ) -> AdapterResult < bool > {
@@ -159,14 +158,8 @@ impl Adapter for EthereumAdapter {
159158 ) ) ;
160159 }
161160 // check if channel is valid
162- let is_channel_valid = EthereumAdapter :: is_channel_valid ( & self . config , channel) ;
163- if is_channel_valid. is_err ( ) {
164- return Err ( AdapterError :: InvalidChannel (
165- is_channel_valid
166- . err ( )
167- . expect ( "failed to get channel error" )
168- . to_string ( ) ,
169- ) ) ;
161+ if let Err ( error) = EthereumAdapter :: is_channel_valid ( & self . config , channel) {
162+ return Err ( AdapterError :: InvalidChannel ( error. to_string ( ) ) ) ;
170163 }
171164 // query the blockchain for the channel status
172165 let contract_address = Address :: from_slice ( self . config . ethereum_core_address . as_bytes ( ) ) ;
@@ -178,7 +171,7 @@ impl Adapter for EthereumAdapter {
178171 . wait ( )
179172 . map_err ( |_| map_error ( "contract channel status query failed" ) ) ?;
180173
181- if channel_status != 1 . into ( ) {
174+ if channel_status != * CHANNEL_STATE_ACTIVE {
182175 return Err ( AdapterError :: Configuration (
183176 "channel is not Active on the ethereum network" . to_string ( ) ,
184177 ) ) ;
@@ -192,14 +185,11 @@ impl Adapter for EthereumAdapter {
192185 return Err ( AdapterError :: Failed ( "invaild token id" . to_string ( ) ) ) ;
193186 }
194187
195- let token_id = token. to_owned ( ) [ token. len ( ) - 16 ..] . to_string ( ) ;
188+ let token_id = token[ token. len ( ) - 16 ..] . to_string ( ) ;
196189
197190 let mut session_tokens = self . session_tokens . borrow_mut ( ) ;
198- if session_tokens. get ( & token_id) . is_some ( ) {
199- return Ok ( session_tokens
200- . get ( & token_id)
201- . expect ( "failed to get session" )
202- . to_owned ( ) ) ;
191+ if let Some ( token) = session_tokens. get ( & token_id) {
192+ return Ok ( token. to_owned ( ) ) ;
203193 }
204194
205195 let parts: Vec < & str > = token. split ( '.' ) . collect ( ) ;
@@ -208,7 +198,7 @@ impl Adapter for EthereumAdapter {
208198 ( Some ( header_encoded) , Some ( payload_encoded) , Some ( token_encoded) ) => {
209199 ( header_encoded, payload_encoded, token_encoded)
210200 }
211- ( _ , _ , _ ) => {
201+ _ => {
212202 return Err ( AdapterError :: Failed ( format ! (
213203 "{} token string is incorrect" ,
214204 token
@@ -236,7 +226,7 @@ impl Adapter for EthereumAdapter {
236226 . wait ( )
237227 . map_err ( |_| map_error ( "failed query priviledge level on contract" ) ) ?;
238228
239- if priviledge_level == 0 . into ( ) {
229+ if priviledge_level == * PRIVILEGE_LEVEL_NONE {
240230 return Err ( AdapterError :: Authorization (
241231 "insufficient privilege" . to_string ( ) ,
242232 ) ) ;
@@ -286,14 +276,11 @@ impl Adapter for EthereumAdapter {
286276}
287277
288278fn check_validator_id_checksum ( channel : & Channel ) -> bool {
289- let invalid_address_checkum : Vec < & ValidatorDesc > = channel
279+ channel
290280 . spec
291281 . validators
292282 . into_iter ( )
293- . filter ( |v| v. id != eth_checksum:: checksum ( & v. id ) )
294- . collect ( ) ;
295-
296- invalid_address_checkum. is_empty ( )
283+ . any ( |v| v. id != eth_checksum:: checksum ( & v. id ) )
297284}
298285
299286fn hash_message ( message : & str ) -> [ u8 ; 32 ] {
@@ -361,15 +348,11 @@ pub fn ewt_sign(
361348 alg : "ETH" . to_string ( ) ,
362349 } ;
363350
364- let header_encoded = base64:: encode_config (
365- & serde_json:: to_string ( & header) ?. as_bytes ( ) ,
366- base64:: URL_SAFE_NO_PAD ,
367- ) ;
351+ let header_encoded =
352+ base64:: encode_config ( & serde_json:: to_string ( & header) ?, base64:: URL_SAFE_NO_PAD ) ;
368353
369- let payload_encoded = base64:: encode_config (
370- & serde_json:: to_string ( payload) ?. as_bytes ( ) ,
371- base64:: URL_SAFE_NO_PAD ,
372- ) ;
354+ let payload_encoded =
355+ base64:: encode_config ( & serde_json:: to_string ( payload) ?, base64:: URL_SAFE_NO_PAD ) ;
373356
374357 let message = Message :: from_slice ( & hash_message ( & format ! (
375358 "{}.{}" ,
@@ -483,7 +466,7 @@ mod test {
483466
484467 let payload = Payload {
485468 id : "awesomeValidator" . to_string ( ) ,
486- era : 10_0000 ,
469+ era : 100_000 ,
487470 address : Some ( eth_adapter. whoami ( ) ) ,
488471 identity : None ,
489472 } ;
0 commit comments