@@ -11,7 +11,6 @@ use primitives::{
11
11
} ;
12
12
use serde:: { Deserialize , Serialize } ;
13
13
use serde_json:: Value ;
14
- use std:: cell:: RefCell ;
15
14
use std:: collections:: HashMap ;
16
15
use std:: error:: Error ;
17
16
use std:: fs;
@@ -39,10 +38,10 @@ pub struct EthereumAdapter {
39
38
keystore_pwd : Password ,
40
39
config : Config ,
41
40
// Auth tokens that we have verified (tokenId => session)
42
- session_tokens : RefCell < HashMap < String , Session > > ,
41
+ session_tokens : HashMap < String , Session > ,
43
42
// Auth tokens that we've generated to authenticate with someone (address => token)
44
- authorization_tokens : RefCell < HashMap < String , String > > ,
45
- wallet : RefCell < Option < SafeAccount > > ,
43
+ authorization_tokens : HashMap < String , String > ,
44
+ wallet : Option < SafeAccount > ,
46
45
}
47
46
48
47
// Enables EthereumAdapter to be able to
@@ -81,14 +80,14 @@ impl Adapter for EthereumAdapter {
81
80
address,
82
81
keystore_json,
83
82
keystore_pwd : keystore_pwd. into ( ) ,
84
- session_tokens : RefCell :: new ( HashMap :: new ( ) ) ,
85
- authorization_tokens : RefCell :: new ( HashMap :: new ( ) ) ,
86
- wallet : RefCell :: new ( None ) ,
83
+ session_tokens : HashMap :: new ( ) ,
84
+ authorization_tokens : HashMap :: new ( ) ,
85
+ wallet : None ,
87
86
config : config. to_owned ( ) ,
88
87
} )
89
88
}
90
89
91
- fn unlock ( & self ) -> AdapterResult < ( ) > {
90
+ fn unlock ( & mut self ) -> AdapterResult < ( ) > {
92
91
let account = SafeAccount :: from_file (
93
92
serde_json:: from_value ( self . keystore_json . clone ( ) )
94
93
. map_err ( |_| map_error ( "Invalid keystore json provided" ) ) ?,
@@ -97,7 +96,7 @@ impl Adapter for EthereumAdapter {
97
96
)
98
97
. map_err ( |_| map_error ( "Failed to create account" ) ) ?;
99
98
100
- self . wallet . replace ( Some ( account) ) ;
99
+ self . wallet = Some ( account) ;
101
100
102
101
Ok ( ( ) )
103
102
}
@@ -107,18 +106,18 @@ impl Adapter for EthereumAdapter {
107
106
}
108
107
109
108
fn sign ( & self , state_root : & str ) -> AdapterResult < String > {
110
- let message = Message :: from_slice ( & hash_message ( state_root ) ) ;
111
- match self . wallet . borrow ( ) . clone ( ) {
112
- Some ( wallet ) => {
113
- let wallet_sign = wallet
114
- . sign ( & self . keystore_pwd , & message )
115
- . map_err ( |_| map_error ( "failed to sign messages" ) ) ? ;
116
- let signature : Signature = wallet_sign . into_electrum ( ) . into ( ) ;
117
- Ok ( format ! ( "0x{}" , signature) )
118
- }
119
- None => Err ( AdapterError :: Configuration (
109
+ if let Some ( wallet ) = & self . wallet {
110
+ let message = Message :: from_slice ( & hash_message ( state_root ) ) ;
111
+ let wallet_sign = wallet
112
+ . sign ( & self . keystore_pwd , & message )
113
+ . map_err ( |_| map_error ( "failed to sign messages" ) ) ? ;
114
+ let signature : Signature = wallet_sign . into_electrum ( ) . into ( ) ;
115
+
116
+ Ok ( format ! ( "0x{}" , signature) )
117
+ } else {
118
+ Err ( AdapterError :: Configuration (
120
119
"Unlock the wallet before signing" . to_string ( ) ,
121
- ) ) ,
120
+ ) )
122
121
}
123
122
}
124
123
@@ -136,7 +135,7 @@ impl Adapter for EthereumAdapter {
136
135
let signature = Signature :: from_electrum ( & decoded_signature) ;
137
136
let message = Message :: from_slice ( & hash_message ( state_root) ) ;
138
137
139
- verify_address ( & address, & signature, & message) . or ( Ok ( false ) )
138
+ verify_address ( & address, & signature, & message) . or_else ( |_| Ok ( false ) )
140
139
}
141
140
142
141
fn validate_channel ( & self , channel : & Channel ) -> AdapterResult < bool > {
@@ -179,15 +178,14 @@ impl Adapter for EthereumAdapter {
179
178
Ok ( true )
180
179
}
181
180
182
- fn session_from_token ( & self , token : & str ) -> AdapterResult < Session > {
181
+ fn session_from_token ( & mut self , token : & str ) -> AdapterResult < Session > {
183
182
if token. len ( ) < 16 {
184
183
return Err ( AdapterError :: Failed ( "invaild token id" . to_string ( ) ) ) ;
185
184
}
186
185
187
186
let token_id = token[ token. len ( ) - 16 ..] . to_string ( ) ;
188
187
189
- let mut session_tokens = self . session_tokens . borrow_mut ( ) ;
190
- if let Some ( token) = session_tokens. get ( & token_id) {
188
+ if let Some ( token) = self . session_tokens . get ( & token_id) {
191
189
return Ok ( token. to_owned ( ) ) ;
192
190
}
193
191
@@ -241,16 +239,12 @@ impl Adapter for EthereumAdapter {
241
239
} ,
242
240
} ;
243
241
244
- session_tokens. insert ( token_id, sess. clone ( ) ) ;
242
+ self . session_tokens . insert ( token_id, sess. clone ( ) ) ;
245
243
Ok ( sess)
246
244
}
247
245
248
- fn get_auth ( & self , validator : & ValidatorDesc ) -> AdapterResult < String > {
249
- let mut authorization_tokens = self . authorization_tokens . borrow_mut ( ) ;
250
- match (
251
- self . wallet . borrow ( ) . clone ( ) ,
252
- authorization_tokens. get ( & validator. id ) ,
253
- ) {
246
+ fn get_auth ( & mut self , validator : & ValidatorDesc ) -> AdapterResult < String > {
247
+ match ( & self . wallet , self . authorization_tokens . get ( & validator. id ) ) {
254
248
( Some ( _) , Some ( token) ) => Ok ( token. to_owned ( ) ) ,
255
249
( Some ( wallet) , None ) => {
256
250
let era = Utc :: now ( ) . timestamp ( ) as f64 / 60000.0 ;
@@ -260,10 +254,11 @@ impl Adapter for EthereumAdapter {
260
254
identity : None ,
261
255
address : None ,
262
256
} ;
263
- let token = ewt_sign ( & wallet, & self . keystore_pwd , & payload)
257
+ let token = ewt_sign ( wallet, & self . keystore_pwd , & payload)
264
258
. map_err ( |_| map_error ( "Failed to sign token" ) ) ?;
265
259
266
- authorization_tokens. insert ( validator. id . clone ( ) , token. clone ( ) ) ;
260
+ self . authorization_tokens
261
+ . insert ( validator. id . clone ( ) , token. clone ( ) ) ;
267
262
268
263
Ok ( token)
269
264
}
@@ -420,7 +415,7 @@ mod test {
420
415
421
416
#[ test]
422
417
fn should_init_and_unlock_ethereum_adapter ( ) {
423
- let eth_adapter = setup_eth_adapter ( ) ;
418
+ let mut eth_adapter = setup_eth_adapter ( ) ;
424
419
let unlock = eth_adapter. unlock ( ) . expect ( "should unlock eth adapter" ) ;
425
420
426
421
assert_eq ! ( ( ) , unlock, "failed to unlock eth adapter" ) ;
@@ -429,7 +424,7 @@ mod test {
429
424
#[ test]
430
425
fn should_get_whoami_sign_and_verify_messages ( ) {
431
426
// whoami
432
- let eth_adapter = setup_eth_adapter ( ) ;
427
+ let mut eth_adapter = setup_eth_adapter ( ) ;
433
428
let whoami = eth_adapter. whoami ( ) ;
434
429
assert_eq ! (
435
430
whoami, "0x2bDeAFAE53940669DaA6F519373f686c1f3d3393" ,
@@ -460,7 +455,7 @@ mod test {
460
455
461
456
#[ test]
462
457
fn should_generate_correct_ewt_sign_and_verify ( ) {
463
- let eth_adapter = setup_eth_adapter ( ) ;
458
+ let mut eth_adapter = setup_eth_adapter ( ) ;
464
459
eth_adapter. unlock ( ) . expect ( "should unlock eth adapter" ) ;
465
460
466
461
let payload = Payload {
@@ -469,7 +464,7 @@ mod test {
469
464
address : Some ( eth_adapter. whoami ( ) ) ,
470
465
identity : None ,
471
466
} ;
472
- let wallet = eth_adapter. wallet . borrow ( ) . clone ( ) ;
467
+ let wallet = eth_adapter. wallet . clone ( ) ;
473
468
let response = ewt_sign ( & wallet. unwrap ( ) , & eth_adapter. keystore_pwd , & payload)
474
469
. expect ( "failed to generate ewt signature" ) ;
475
470
let expected =
0 commit comments