@@ -11,7 +11,6 @@ use primitives::{
1111} ;
1212use serde:: { Deserialize , Serialize } ;
1313use serde_json:: Value ;
14- use std:: cell:: RefCell ;
1514use std:: collections:: HashMap ;
1615use std:: error:: Error ;
1716use std:: fs;
@@ -39,10 +38,10 @@ pub struct EthereumAdapter {
3938 keystore_pwd : Password ,
4039 config : Config ,
4140 // Auth tokens that we have verified (tokenId => session)
42- session_tokens : RefCell < HashMap < String , Session > > ,
41+ session_tokens : HashMap < String , Session > ,
4342 // 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 > ,
4645}
4746
4847// Enables EthereumAdapter to be able to
@@ -81,14 +80,14 @@ impl Adapter for EthereumAdapter {
8180 address,
8281 keystore_json,
8382 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 ,
8786 config : config. to_owned ( ) ,
8887 } )
8988 }
9089
91- fn unlock ( & self ) -> AdapterResult < ( ) > {
90+ fn unlock ( & mut self ) -> AdapterResult < ( ) > {
9291 let account = SafeAccount :: from_file (
9392 serde_json:: from_value ( self . keystore_json . clone ( ) )
9493 . map_err ( |_| map_error ( "Invalid keystore json provided" ) ) ?,
@@ -97,7 +96,7 @@ impl Adapter for EthereumAdapter {
9796 )
9897 . map_err ( |_| map_error ( "Failed to create account" ) ) ?;
9998
100- self . wallet . replace ( Some ( account) ) ;
99+ self . wallet = Some ( account) ;
101100
102101 Ok ( ( ) )
103102 }
@@ -107,18 +106,18 @@ impl Adapter for EthereumAdapter {
107106 }
108107
109108 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 (
120119 "Unlock the wallet before signing" . to_string ( ) ,
121- ) ) ,
120+ ) )
122121 }
123122 }
124123
@@ -136,7 +135,7 @@ impl Adapter for EthereumAdapter {
136135 let signature = Signature :: from_electrum ( & decoded_signature) ;
137136 let message = Message :: from_slice ( & hash_message ( state_root) ) ;
138137
139- verify_address ( & address, & signature, & message) . or ( Ok ( false ) )
138+ verify_address ( & address, & signature, & message) . or_else ( |_| Ok ( false ) )
140139 }
141140
142141 fn validate_channel ( & self , channel : & Channel ) -> AdapterResult < bool > {
@@ -179,15 +178,14 @@ impl Adapter for EthereumAdapter {
179178 Ok ( true )
180179 }
181180
182- fn session_from_token ( & self , token : & str ) -> AdapterResult < Session > {
181+ fn session_from_token ( & mut self , token : & str ) -> AdapterResult < Session > {
183182 if token. len ( ) < 16 {
184183 return Err ( AdapterError :: Failed ( "invaild token id" . to_string ( ) ) ) ;
185184 }
186185
187186 let token_id = token[ token. len ( ) - 16 ..] . to_string ( ) ;
188187
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) {
191189 return Ok ( token. to_owned ( ) ) ;
192190 }
193191
@@ -241,16 +239,12 @@ impl Adapter for EthereumAdapter {
241239 } ,
242240 } ;
243241
244- session_tokens. insert ( token_id, sess. clone ( ) ) ;
242+ self . session_tokens . insert ( token_id, sess. clone ( ) ) ;
245243 Ok ( sess)
246244 }
247245
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 ) ) {
254248 ( Some ( _) , Some ( token) ) => Ok ( token. to_owned ( ) ) ,
255249 ( Some ( wallet) , None ) => {
256250 let era = Utc :: now ( ) . timestamp ( ) as f64 / 60000.0 ;
@@ -260,10 +254,11 @@ impl Adapter for EthereumAdapter {
260254 identity : None ,
261255 address : None ,
262256 } ;
263- let token = ewt_sign ( & wallet, & self . keystore_pwd , & payload)
257+ let token = ewt_sign ( wallet, & self . keystore_pwd , & payload)
264258 . map_err ( |_| map_error ( "Failed to sign token" ) ) ?;
265259
266- authorization_tokens. insert ( validator. id . clone ( ) , token. clone ( ) ) ;
260+ self . authorization_tokens
261+ . insert ( validator. id . clone ( ) , token. clone ( ) ) ;
267262
268263 Ok ( token)
269264 }
@@ -420,7 +415,7 @@ mod test {
420415
421416 #[ test]
422417 fn should_init_and_unlock_ethereum_adapter ( ) {
423- let eth_adapter = setup_eth_adapter ( ) ;
418+ let mut eth_adapter = setup_eth_adapter ( ) ;
424419 let unlock = eth_adapter. unlock ( ) . expect ( "should unlock eth adapter" ) ;
425420
426421 assert_eq ! ( ( ) , unlock, "failed to unlock eth adapter" ) ;
@@ -429,7 +424,7 @@ mod test {
429424 #[ test]
430425 fn should_get_whoami_sign_and_verify_messages ( ) {
431426 // whoami
432- let eth_adapter = setup_eth_adapter ( ) ;
427+ let mut eth_adapter = setup_eth_adapter ( ) ;
433428 let whoami = eth_adapter. whoami ( ) ;
434429 assert_eq ! (
435430 whoami, "0x2bDeAFAE53940669DaA6F519373f686c1f3d3393" ,
@@ -460,7 +455,7 @@ mod test {
460455
461456 #[ test]
462457 fn should_generate_correct_ewt_sign_and_verify ( ) {
463- let eth_adapter = setup_eth_adapter ( ) ;
458+ let mut eth_adapter = setup_eth_adapter ( ) ;
464459 eth_adapter. unlock ( ) . expect ( "should unlock eth adapter" ) ;
465460
466461 let payload = Payload {
@@ -469,7 +464,7 @@ mod test {
469464 address : Some ( eth_adapter. whoami ( ) ) ,
470465 identity : None ,
471466 } ;
472- let wallet = eth_adapter. wallet . borrow ( ) . clone ( ) ;
467+ let wallet = eth_adapter. wallet . clone ( ) ;
473468 let response = ewt_sign ( & wallet. unwrap ( ) , & eth_adapter. keystore_pwd , & payload)
474469 . expect ( "failed to generate ewt signature" ) ;
475470 let expected =
0 commit comments