@@ -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,8 @@ lazy_static! {
3232
3333#[ derive( Debug , Clone ) ]
3434pub struct EthereumAdapter {
35- keystore_json : String ,
35+ address : String ,
36+ keystore_json : Value ,
3637 keystore_pwd : Password ,
3738 config : Config ,
3839 // Auth tokens that we have verified (tokenId => session)
@@ -50,7 +51,7 @@ impl Adapter for EthereumAdapter {
5051 type Output = EthereumAdapter ;
5152
5253 fn init ( opts : AdapterOptions , config : & Config ) -> AdapterResult < EthereumAdapter > {
53- let ( keystore_json , keystore_pwd) = match ( opts. keystore_file , opts. keystore_pwd ) {
54+ let ( keystore_file , keystore_pwd) = match ( opts. keystore_file , opts. keystore_pwd ) {
5455 ( Some ( file) , Some ( pwd) ) => ( file, pwd) ,
5556 ( _, _) => {
5657 return Err ( AdapterError :: Configuration (
@@ -59,7 +60,23 @@ impl Adapter for EthereumAdapter {
5960 }
6061 } ;
6162
63+ let keystore_contents = fs:: read_to_string ( & keystore_file)
64+ . map_err ( |_| map_error ( "Invalid keystore location provided" ) ) ?;
65+
66+ let keystore_json: Value = serde_json:: from_str ( & keystore_contents)
67+ . map_err ( |_| map_error ( "Invalid keystore json provided" ) ) ?;
68+
69+ let address = match keystore_json[ "address" ] . as_str ( ) {
70+ Some ( addr) => eth_checksum:: checksum ( & addr) ,
71+ None => {
72+ return Err ( AdapterError :: Failed (
73+ "address missing in keystore json" . to_string ( ) ,
74+ ) )
75+ }
76+ } ;
77+
6278 Ok ( Self {
79+ address,
6380 keystore_json,
6481 keystore_pwd : keystore_pwd. into ( ) ,
6582 session_tokens : RefCell :: new ( HashMap :: new ( ) ) ,
@@ -70,11 +87,8 @@ impl Adapter for EthereumAdapter {
7087 }
7188
7289 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-
7690 let account = SafeAccount :: from_file (
77- serde_json:: from_reader ( json_file )
91+ serde_json:: from_value ( self . keystore_json . clone ( ) )
7892 . map_err ( |_| map_error ( "Invalid keystore json provided" ) ) ?,
7993 None ,
8094 & Some ( self . keystore_pwd . clone ( ) ) ,
@@ -87,19 +101,8 @@ impl Adapter for EthereumAdapter {
87101 Ok ( true )
88102 }
89103
90- 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 ( ) ,
101- ) ) ,
102- }
104+ fn whoami ( & self ) -> String {
105+ self . address . clone ( )
103106 }
104107
105108 fn sign ( & self , state_root : & str ) -> AdapterResult < String > {
@@ -188,7 +191,7 @@ impl Adapter for EthereumAdapter {
188191 if token. len ( ) < 16 {
189192 return Err ( AdapterError :: Failed ( "invaild token id" . to_string ( ) ) ) ;
190193 }
191-
194+
192195 let token_id = token. to_owned ( ) [ token. len ( ) - 16 ..] . to_string ( ) ;
193196
194197 let mut session_tokens = self . session_tokens . borrow_mut ( ) ;
@@ -216,7 +219,7 @@ impl Adapter for EthereumAdapter {
216219 let verified = ewt_verify ( header_encoded, payload_encoded, token_encoded)
217220 . map_err ( |e| map_error ( & e. to_string ( ) ) ) ?;
218221
219- if self . whoami ( ) ? != verified. payload . id {
222+ if self . whoami ( ) != verified. payload . id {
220223 return Err ( AdapterError :: Configuration (
221224 "token payload.id !== whoami(): token was not intended for us" . to_string ( ) ,
222225 ) ) ;
@@ -445,14 +448,14 @@ mod test {
445448 fn should_get_whoami_sign_and_verify_messages ( ) {
446449 // whoami
447450 let eth_adapter = setup_eth_adapter ( ) ;
448- eth_adapter. unlock ( ) . expect ( "should unlock eth adapter" ) ;
449-
450- let whoami = eth_adapter. whoami ( ) . expect ( "failed to get whoami" ) ;
451+ let whoami = eth_adapter. whoami ( ) ;
451452 assert_eq ! (
452453 whoami, "0x2bDeAFAE53940669DaA6F519373f686c1f3d3393" ,
453454 "failed to get correct whoami"
454455 ) ;
455456
457+ eth_adapter. unlock ( ) . expect ( "should unlock eth adapter" ) ;
458+
456459 // Sign
457460 let expected_response =
458461 "0xce654de0b3d14d63e1cb3181eee7a7a37ef4a06c9fabc204faf96f26357441b625b1be460fbe8f5278cc02aa88a5d0ac2f238e9e3b8e4893760d33bccf77e47f1b" ;
@@ -481,7 +484,7 @@ mod test {
481484 let payload = Payload {
482485 id : "awesomeValidator" . to_string ( ) ,
483486 era : 10_0000 ,
484- address : Some ( eth_adapter. whoami ( ) . expect ( "should get whoami ewt sign" ) ) ,
487+ address : Some ( eth_adapter. whoami ( ) ) ,
485488 identity : None ,
486489 } ;
487490 let wallet = eth_adapter. wallet . borrow ( ) . clone ( ) ;
0 commit comments