@@ -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,7 @@ lazy_static! {
32
32
33
33
#[ derive( Debug , Clone ) ]
34
34
pub struct EthereumAdapter {
35
- keystore_json : String ,
35
+ keystore_json : Value ,
36
36
keystore_pwd : Password ,
37
37
config : Config ,
38
38
// Auth tokens that we have verified (tokenId => session)
@@ -59,8 +59,12 @@ impl Adapter for EthereumAdapter {
59
59
}
60
60
} ;
61
61
62
+ let keystore_contents = fs:: read_to_string ( & keystore_json)
63
+ . map_err ( |_| map_error ( "Invalid keystore location provided" ) ) ?;
64
+
62
65
Ok ( Self {
63
- keystore_json,
66
+ keystore_json : serde_json:: from_str ( & keystore_contents)
67
+ . map_err ( |_| map_error ( "Invalid keystore json provided" ) ) ?,
64
68
keystore_pwd : keystore_pwd. into ( ) ,
65
69
session_tokens : RefCell :: new ( HashMap :: new ( ) ) ,
66
70
authorization_tokens : RefCell :: new ( HashMap :: new ( ) ) ,
@@ -70,11 +74,8 @@ impl Adapter for EthereumAdapter {
70
74
}
71
75
72
76
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
77
let account = SafeAccount :: from_file (
77
- serde_json:: from_reader ( json_file )
78
+ serde_json:: from_value ( self . keystore_json . clone ( ) )
78
79
. map_err ( |_| map_error ( "Invalid keystore json provided" ) ) ?,
79
80
None ,
80
81
& Some ( self . keystore_pwd . clone ( ) ) ,
@@ -88,16 +89,10 @@ impl Adapter for EthereumAdapter {
88
89
}
89
90
90
91
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 ( ) ,
101
96
) ) ,
102
97
}
103
98
}
@@ -188,7 +183,7 @@ impl Adapter for EthereumAdapter {
188
183
if token. len ( ) < 16 {
189
184
return Err ( AdapterError :: Failed ( "invaild token id" . to_string ( ) ) ) ;
190
185
}
191
-
186
+
192
187
let token_id = token. to_owned ( ) [ token. len ( ) - 16 ..] . to_string ( ) ;
193
188
194
189
let mut session_tokens = self . session_tokens . borrow_mut ( ) ;
@@ -445,14 +440,14 @@ mod test {
445
440
fn should_get_whoami_sign_and_verify_messages ( ) {
446
441
// whoami
447
442
let eth_adapter = setup_eth_adapter ( ) ;
448
- eth_adapter. unlock ( ) . expect ( "should unlock eth adapter" ) ;
449
-
450
443
let whoami = eth_adapter. whoami ( ) . expect ( "failed to get whoami" ) ;
451
444
assert_eq ! (
452
445
whoami, "0x2bDeAFAE53940669DaA6F519373f686c1f3d3393" ,
453
446
"failed to get correct whoami"
454
447
) ;
455
448
449
+ eth_adapter. unlock ( ) . expect ( "should unlock eth adapter" ) ;
450
+
456
451
// Sign
457
452
let expected_response =
458
453
"0xce654de0b3d14d63e1cb3181eee7a7a37ef4a06c9fabc204faf96f26357441b625b1be460fbe8f5278cc02aa88a5d0ac2f238e9e3b8e4893760d33bccf77e47f1b" ;
0 commit comments