@@ -28,6 +28,8 @@ lazy_static! {
28
28
include_bytes!( "../../lib/protocol-eth/abi/AdExCore.json" ) ;
29
29
static ref IDENTITY_ABI : & ' static [ u8 ] =
30
30
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( ) ;
31
33
}
32
34
33
35
#[ derive( Debug , Clone ) ]
@@ -86,7 +88,7 @@ impl Adapter for EthereumAdapter {
86
88
} )
87
89
}
88
90
89
- fn unlock ( & self ) -> AdapterResult < bool > {
91
+ fn unlock ( & self ) -> AdapterResult < ( ) > {
90
92
let account = SafeAccount :: from_file (
91
93
serde_json:: from_value ( self . keystore_json . clone ( ) )
92
94
. map_err ( |_| map_error ( "Invalid keystore json provided" ) ) ?,
@@ -97,8 +99,7 @@ impl Adapter for EthereumAdapter {
97
99
98
100
self . wallet . replace ( Some ( account) ) ;
99
101
100
- // wallet has been unlocked
101
- Ok ( true )
102
+ Ok ( ( ) )
102
103
}
103
104
104
105
fn whoami ( & self ) -> String {
@@ -124,7 +125,7 @@ impl Adapter for EthereumAdapter {
124
125
fn verify ( & self , signer : & str , state_root : & str , sig : & str ) -> AdapterResult < bool > {
125
126
let ( decoded_adress, decoded_signature) = match ( hex:: decode ( signer) , hex:: decode ( sig) ) {
126
127
( Ok ( address) , Ok ( sig) ) => ( address, sig) ,
127
- ( _ , _ ) => {
128
+ _ => {
128
129
return Err ( AdapterError :: Signature (
129
130
"invalid signature or address" . to_string ( ) ,
130
131
) )
@@ -135,10 +136,7 @@ impl Adapter for EthereumAdapter {
135
136
let signature = Signature :: from_electrum ( & decoded_signature) ;
136
137
let message = Message :: from_slice ( & hash_message ( state_root) ) ;
137
138
138
- match verify_address ( & address, & signature, & message) {
139
- Ok ( result) => Ok ( result) ,
140
- Err ( _) => Ok ( false ) ,
141
- }
139
+ verify_address ( & address, & signature, & message) . or ( Ok ( false ) )
142
140
}
143
141
144
142
fn validate_channel ( & self , channel : & Channel ) -> AdapterResult < bool > {
@@ -159,14 +157,8 @@ impl Adapter for EthereumAdapter {
159
157
) ) ;
160
158
}
161
159
// 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
- ) ) ;
160
+ if let Err ( error) = EthereumAdapter :: is_channel_valid ( & self . config , channel) {
161
+ return Err ( AdapterError :: InvalidChannel ( error. to_string ( ) ) ) ;
170
162
}
171
163
// query the blockchain for the channel status
172
164
let contract_address = Address :: from_slice ( self . config . ethereum_core_address . as_bytes ( ) ) ;
@@ -178,7 +170,7 @@ impl Adapter for EthereumAdapter {
178
170
. wait ( )
179
171
. map_err ( |_| map_error ( "contract channel status query failed" ) ) ?;
180
172
181
- if channel_status != 1 . into ( ) {
173
+ if channel_status != * CHANNEL_STATE_ACTIVE {
182
174
return Err ( AdapterError :: Configuration (
183
175
"channel is not Active on the ethereum network" . to_string ( ) ,
184
176
) ) ;
@@ -192,14 +184,11 @@ impl Adapter for EthereumAdapter {
192
184
return Err ( AdapterError :: Failed ( "invaild token id" . to_string ( ) ) ) ;
193
185
}
194
186
195
- let token_id = token. to_owned ( ) [ token. len ( ) - 16 ..] . to_string ( ) ;
187
+ let token_id = token[ token. len ( ) - 16 ..] . to_string ( ) ;
196
188
197
189
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 ( ) ) ;
190
+ if let Some ( token) = session_tokens. get ( & token_id) {
191
+ return Ok ( token. to_owned ( ) ) ;
203
192
}
204
193
205
194
let parts: Vec < & str > = token. split ( '.' ) . collect ( ) ;
@@ -208,7 +197,7 @@ impl Adapter for EthereumAdapter {
208
197
( Some ( header_encoded) , Some ( payload_encoded) , Some ( token_encoded) ) => {
209
198
( header_encoded, payload_encoded, token_encoded)
210
199
}
211
- ( _ , _ , _ ) => {
200
+ _ => {
212
201
return Err ( AdapterError :: Failed ( format ! (
213
202
"{} token string is incorrect" ,
214
203
token
@@ -236,7 +225,7 @@ impl Adapter for EthereumAdapter {
236
225
. wait ( )
237
226
. map_err ( |_| map_error ( "failed query priviledge level on contract" ) ) ?;
238
227
239
- if priviledge_level == 0 . into ( ) {
228
+ if priviledge_level == * PRIVILEGE_LEVEL_NONE {
240
229
return Err ( AdapterError :: Authorization (
241
230
"insufficient privilege" . to_string ( ) ,
242
231
) ) ;
@@ -286,14 +275,11 @@ impl Adapter for EthereumAdapter {
286
275
}
287
276
288
277
fn check_validator_id_checksum ( channel : & Channel ) -> bool {
289
- let invalid_address_checkum : Vec < & ValidatorDesc > = channel
278
+ channel
290
279
. spec
291
280
. validators
292
281
. into_iter ( )
293
- . filter ( |v| v. id != eth_checksum:: checksum ( & v. id ) )
294
- . collect ( ) ;
295
-
296
- invalid_address_checkum. is_empty ( )
282
+ . any ( |v| v. id != eth_checksum:: checksum ( & v. id ) )
297
283
}
298
284
299
285
fn hash_message ( message : & str ) -> [ u8 ; 32 ] {
@@ -361,15 +347,11 @@ pub fn ewt_sign(
361
347
alg : "ETH" . to_string ( ) ,
362
348
} ;
363
349
364
- let header_encoded = base64:: encode_config (
365
- & serde_json:: to_string ( & header) ?. as_bytes ( ) ,
366
- base64:: URL_SAFE_NO_PAD ,
367
- ) ;
350
+ let header_encoded =
351
+ base64:: encode_config ( & serde_json:: to_string ( & header) ?, base64:: URL_SAFE_NO_PAD ) ;
368
352
369
- let payload_encoded = base64:: encode_config (
370
- & serde_json:: to_string ( payload) ?. as_bytes ( ) ,
371
- base64:: URL_SAFE_NO_PAD ,
372
- ) ;
353
+ let payload_encoded =
354
+ base64:: encode_config ( & serde_json:: to_string ( payload) ?, base64:: URL_SAFE_NO_PAD ) ;
373
355
374
356
let message = Message :: from_slice ( & hash_message ( & format ! (
375
357
"{}.{}" ,
@@ -441,7 +423,7 @@ mod test {
441
423
let eth_adapter = setup_eth_adapter ( ) ;
442
424
let unlock = eth_adapter. unlock ( ) . expect ( "should unlock eth adapter" ) ;
443
425
444
- assert_eq ! ( true , unlock, "failed to unlock eth adapter" ) ;
426
+ assert_eq ! ( ( ) , unlock, "failed to unlock eth adapter" ) ;
445
427
}
446
428
447
429
#[ test]
@@ -483,7 +465,7 @@ mod test {
483
465
484
466
let payload = Payload {
485
467
id : "awesomeValidator" . to_string ( ) ,
486
- era : 10_0000 ,
468
+ era : 100_000 ,
487
469
address : Some ( eth_adapter. whoami ( ) ) ,
488
470
identity : None ,
489
471
} ;
0 commit comments