@@ -10,11 +10,11 @@ use primitives::{
1010 Channel , ValidatorDesc ,
1111} ;
1212use serde:: { Deserialize , Serialize } ;
13+ use serde_json:: Value ;
1314use std:: cell:: RefCell ;
1415use std:: collections:: HashMap ;
1516use std:: error:: Error ;
16- use std:: fs:: File ;
17- use std:: path:: Path ;
17+ use std:: fs;
1818use tiny_keccak:: Keccak ;
1919use web3:: transports:: Http ;
2020use web3:: {
@@ -32,7 +32,7 @@ lazy_static! {
3232
3333#[ derive( Debug , Clone ) ]
3434pub struct EthereumAdapter {
35- keystore_json : String ,
35+ keystore_json : Value ,
3636 keystore_pwd : Password ,
3737 config : Config ,
3838 // Auth tokens that we have verified (tokenId => session)
@@ -59,8 +59,12 @@ impl Adapter for EthereumAdapter {
5959 }
6060 } ;
6161
62+ let keystore_contents = fs:: read_to_string ( & keystore_json)
63+ . map_err ( |_| map_error ( "Invalid keystore location provided" ) ) ?;
64+
6265 Ok ( Self {
63- keystore_json,
66+ keystore_json : serde_json:: from_str ( & keystore_contents)
67+ . map_err ( |_| map_error ( "Invalid keystore json provided" ) ) ?,
6468 keystore_pwd : keystore_pwd. into ( ) ,
6569 session_tokens : RefCell :: new ( HashMap :: new ( ) ) ,
6670 authorization_tokens : RefCell :: new ( HashMap :: new ( ) ) ,
@@ -70,11 +74,8 @@ impl Adapter for EthereumAdapter {
7074 }
7175
7276 fn unlock ( & self ) -> AdapterResult < bool > {
73- let json_file = File :: open ( & Path :: new ( & self . keystore_json ) . to_path_buf ( ) )
74- . map_err ( |_| map_error ( "Invalid keystore location provided" ) ) ?;
75-
7677 let account = SafeAccount :: from_file (
77- serde_json:: from_reader ( json_file )
78+ serde_json:: from_value ( self . keystore_json . clone ( ) )
7879 . map_err ( |_| map_error ( "Invalid keystore json provided" ) ) ?,
7980 None ,
8081 & Some ( self . keystore_pwd . clone ( ) ) ,
@@ -88,16 +89,10 @@ impl Adapter for EthereumAdapter {
8889 }
8990
9091 fn whoami ( & self ) -> AdapterResult < String > {
91- match self . wallet . borrow ( ) . clone ( ) {
92- Some ( wallet) => {
93- let public = wallet
94- . public ( & self . keystore_pwd )
95- . map_err ( |_| map_error ( "Failed to get public key" ) ) ?;
96- let address = format ! ( "{:?}" , public_to_address( & public) ) ;
97- Ok ( eth_checksum:: checksum ( & address) )
98- }
99- None => Err ( AdapterError :: Configuration (
100- "Unlock wallet before use" . to_string ( ) ,
92+ match self . keystore_json [ "address" ] . as_str ( ) {
93+ Some ( addr) => Ok ( eth_checksum:: checksum ( & addr) ) ,
94+ None => Err ( AdapterError :: Failed (
95+ "address missing in keystore json" . to_string ( ) ,
10196 ) ) ,
10297 }
10398 }
@@ -188,7 +183,7 @@ impl Adapter for EthereumAdapter {
188183 if token. len ( ) < 16 {
189184 return Err ( AdapterError :: Failed ( "invaild token id" . to_string ( ) ) ) ;
190185 }
191-
186+
192187 let token_id = token. to_owned ( ) [ token. len ( ) - 16 ..] . to_string ( ) ;
193188
194189 let mut session_tokens = self . session_tokens . borrow_mut ( ) ;
@@ -445,14 +440,14 @@ mod test {
445440 fn should_get_whoami_sign_and_verify_messages ( ) {
446441 // whoami
447442 let eth_adapter = setup_eth_adapter ( ) ;
448- eth_adapter. unlock ( ) . expect ( "should unlock eth adapter" ) ;
449-
450443 let whoami = eth_adapter. whoami ( ) . expect ( "failed to get whoami" ) ;
451444 assert_eq ! (
452445 whoami, "0x2bDeAFAE53940669DaA6F519373f686c1f3d3393" ,
453446 "failed to get correct whoami"
454447 ) ;
455448
449+ eth_adapter. unlock ( ) . expect ( "should unlock eth adapter" ) ;
450+
456451 // Sign
457452 let expected_response =
458453 "0xce654de0b3d14d63e1cb3181eee7a7a37ef4a06c9fabc204faf96f26357441b625b1be460fbe8f5278cc02aa88a5d0ac2f238e9e3b8e4893760d33bccf77e47f1b" ;
0 commit comments