@@ -10,11 +10,11 @@ use primitives::{
10
10
Channel , ValidatorDesc ,
11
11
} ;
12
12
use serde:: { Deserialize , Serialize } ;
13
+ use serde_json:: Value ;
13
14
use std:: cell:: RefCell ;
14
15
use std:: collections:: HashMap ;
15
16
use std:: error:: Error ;
16
- use std:: fs:: File ;
17
- use std:: path:: Path ;
17
+ use std:: fs;
18
18
use tiny_keccak:: Keccak ;
19
19
use web3:: transports:: Http ;
20
20
use web3:: {
@@ -32,7 +32,8 @@ lazy_static! {
32
32
33
33
#[ derive( Debug , Clone ) ]
34
34
pub struct EthereumAdapter {
35
- keystore_json : String ,
35
+ address : String ,
36
+ keystore_json : Value ,
36
37
keystore_pwd : Password ,
37
38
config : Config ,
38
39
// Auth tokens that we have verified (tokenId => session)
@@ -50,7 +51,7 @@ impl Adapter for EthereumAdapter {
50
51
type Output = EthereumAdapter ;
51
52
52
53
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 ) {
54
55
( Some ( file) , Some ( pwd) ) => ( file, pwd) ,
55
56
( _, _) => {
56
57
return Err ( AdapterError :: Configuration (
@@ -59,7 +60,23 @@ impl Adapter for EthereumAdapter {
59
60
}
60
61
} ;
61
62
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
+
62
78
Ok ( Self {
79
+ address,
63
80
keystore_json,
64
81
keystore_pwd : keystore_pwd. into ( ) ,
65
82
session_tokens : RefCell :: new ( HashMap :: new ( ) ) ,
@@ -70,11 +87,8 @@ impl Adapter for EthereumAdapter {
70
87
}
71
88
72
89
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
-
76
90
let account = SafeAccount :: from_file (
77
- serde_json:: from_reader ( json_file )
91
+ serde_json:: from_value ( self . keystore_json . clone ( ) )
78
92
. map_err ( |_| map_error ( "Invalid keystore json provided" ) ) ?,
79
93
None ,
80
94
& Some ( self . keystore_pwd . clone ( ) ) ,
@@ -87,19 +101,8 @@ impl Adapter for EthereumAdapter {
87
101
Ok ( true )
88
102
}
89
103
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 ( )
103
106
}
104
107
105
108
fn sign ( & self , state_root : & str ) -> AdapterResult < String > {
@@ -188,7 +191,7 @@ impl Adapter for EthereumAdapter {
188
191
if token. len ( ) < 16 {
189
192
return Err ( AdapterError :: Failed ( "invaild token id" . to_string ( ) ) ) ;
190
193
}
191
-
194
+
192
195
let token_id = token. to_owned ( ) [ token. len ( ) - 16 ..] . to_string ( ) ;
193
196
194
197
let mut session_tokens = self . session_tokens . borrow_mut ( ) ;
@@ -216,7 +219,7 @@ impl Adapter for EthereumAdapter {
216
219
let verified = ewt_verify ( header_encoded, payload_encoded, token_encoded)
217
220
. map_err ( |e| map_error ( & e. to_string ( ) ) ) ?;
218
221
219
- if self . whoami ( ) ? != verified. payload . id {
222
+ if self . whoami ( ) != verified. payload . id {
220
223
return Err ( AdapterError :: Configuration (
221
224
"token payload.id !== whoami(): token was not intended for us" . to_string ( ) ,
222
225
) ) ;
@@ -445,14 +448,14 @@ mod test {
445
448
fn should_get_whoami_sign_and_verify_messages ( ) {
446
449
// whoami
447
450
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 ( ) ;
451
452
assert_eq ! (
452
453
whoami, "0x2bDeAFAE53940669DaA6F519373f686c1f3d3393" ,
453
454
"failed to get correct whoami"
454
455
) ;
455
456
457
+ eth_adapter. unlock ( ) . expect ( "should unlock eth adapter" ) ;
458
+
456
459
// Sign
457
460
let expected_response =
458
461
"0xce654de0b3d14d63e1cb3181eee7a7a37ef4a06c9fabc204faf96f26357441b625b1be460fbe8f5278cc02aa88a5d0ac2f238e9e3b8e4893760d33bccf77e47f1b" ;
@@ -481,7 +484,7 @@ mod test {
481
484
let payload = Payload {
482
485
id : "awesomeValidator" . to_string ( ) ,
483
486
era : 10_0000 ,
484
- address : Some ( eth_adapter. whoami ( ) . expect ( "should get whoami ewt sign" ) ) ,
487
+ address : Some ( eth_adapter. whoami ( ) ) ,
485
488
identity : None ,
486
489
} ;
487
490
let wallet = eth_adapter. wallet . borrow ( ) . clone ( ) ;
0 commit comments