@@ -7,20 +7,16 @@ use crate::{
7
7
use async_trait:: async_trait;
8
8
use chrono:: Utc ;
9
9
use create2:: calc_addr;
10
- use ethstore:: {
11
- ethkey:: { verify_address, Message , Signature } ,
12
- SafeAccount ,
13
- } ;
10
+ use ethsign:: { KeyFile , Signature } ;
14
11
use primitives:: { Address , BigNum , Chain , ChainId , ChainOf , Channel , Config , ValidatorId } ;
15
12
16
13
use super :: {
17
14
channel:: EthereumChannel ,
18
15
error:: { Error , EwtSigningError , KeystoreError , VerifyError } ,
19
16
ewt:: { self , Payload } ,
20
- to_ethereum_signed, LockedWallet , UnlockedWallet , WalletState , DEPOSITOR_BYTECODE_DECODED ,
21
- ERC20_ABI , IDENTITY_ABI , OUTPACE_ABI , SWEEPER_ABI ,
17
+ to_ethereum_signed, Electrum , LockedWallet , UnlockedWallet , WalletState ,
18
+ DEPOSITOR_BYTECODE_DECODED , ERC20_ABI , IDENTITY_ABI , OUTPACE_ABI , SWEEPER_ABI ,
22
19
} ;
23
- use serde_json:: Value ;
24
20
use web3:: {
25
21
contract:: { Contract , Options as ContractOptions } ,
26
22
ethabi:: { encode, Token } ,
@@ -80,22 +76,18 @@ impl Ethereum<LockedWallet> {
80
76
pub fn init ( opts : Options , config : & Config ) -> Result < Self , Error > {
81
77
let keystore_contents =
82
78
fs:: read_to_string ( & opts. keystore_file ) . map_err ( KeystoreError :: ReadingFile ) ?;
83
- let keystore_json: Value =
79
+ let keystore_json: KeyFile =
84
80
serde_json:: from_str ( & keystore_contents) . map_err ( KeystoreError :: Deserialization ) ?;
85
81
86
- let address = {
87
- let keystore_address = keystore_json
88
- . get ( "address" )
89
- . and_then ( |value| value. as_str ( ) )
90
- . ok_or ( KeystoreError :: AddressMissing ) ?;
82
+ let address_bytes = keystore_json
83
+ . address
84
+ . clone ( )
85
+ . ok_or ( KeystoreError :: AddressMissing ) ?;
91
86
92
- keystore_address
93
- . parse ( )
94
- . map_err ( KeystoreError :: AddressInvalid )
95
- } ?;
87
+ let address = Address :: from_slice ( & address_bytes. 0 ) . ok_or ( KeystoreError :: AddressLength ) ?;
96
88
97
89
Ok ( Self {
98
- address,
90
+ address : ValidatorId :: from ( address ) ,
99
91
config : config. to_owned ( ) ,
100
92
state : LockedWallet :: KeyStore {
101
93
keystore : keystore_json,
@@ -160,16 +152,9 @@ impl Unlockable for Ethereum<LockedWallet> {
160
152
fn unlock ( & self ) -> Result < Ethereum < UnlockedWallet > , <Self :: Unlocked as Locked >:: Error > {
161
153
let unlocked_wallet = match & self . state {
162
154
LockedWallet :: KeyStore { keystore, password } => {
163
- let json = serde_json:: from_value ( keystore. clone ( ) )
164
- . map_err ( KeystoreError :: Deserialization ) ?;
165
-
166
- let wallet = SafeAccount :: from_file ( json, None , & Some ( password. clone ( ) ) )
167
- . map_err ( |err| Error :: WalletUnlock ( err. to_string ( ) ) ) ?;
155
+ let wallet = keystore. to_secret_key ( password) ?;
168
156
169
- UnlockedWallet {
170
- wallet,
171
- password : password. clone ( ) ,
172
- }
157
+ UnlockedWallet { wallet }
173
158
}
174
159
LockedWallet :: PrivateKey ( _priv_key) => todo ! ( ) ,
175
160
} ;
@@ -201,15 +186,19 @@ impl<S: WalletState> Locked for Ethereum<S> {
201
186
}
202
187
let decoded_signature =
203
188
hex:: decode ( & signature[ 2 ..] ) . map_err ( VerifyError :: SignatureDecoding ) ?;
204
- let address = ethstore:: ethkey:: Address :: from ( * signer. as_bytes ( ) ) ;
205
- let signature = Signature :: from_electrum ( & decoded_signature) ;
189
+
190
+ let signature =
191
+ Signature :: from_electrum ( & decoded_signature) . ok_or ( VerifyError :: SignatureInvalid ) ?;
206
192
let state_root = hex:: decode ( state_root) . map_err ( VerifyError :: StateRootDecoding ) ?;
207
- let message = Message :: from ( to_ethereum_signed ( & state_root) ) ;
208
193
209
- let verify_address = verify_address ( & address, & signature, & message)
210
- . map_err ( VerifyError :: PublicKeyRecovery ) ?;
194
+ let message = to_ethereum_signed ( & state_root) ;
211
195
212
- Ok ( verify_address)
196
+ // recover the public key using the signature and the eth sign message
197
+ let public_key = signature
198
+ . recover ( & message)
199
+ . map_err ( |ec_err| VerifyError :: PublicKeyRecovery ( ec_err. to_string ( ) ) ) ?;
200
+
201
+ Ok ( public_key. address ( ) == signer. as_bytes ( ) )
213
202
}
214
203
215
204
/// Creates a `Session` from a provided Token by calling the Contract.
@@ -358,16 +347,16 @@ impl<S: WalletState> Locked for Ethereum<S> {
358
347
impl Unlocked for Ethereum < UnlockedWallet > {
359
348
fn sign ( & self , state_root : & str ) -> Result < String , Error > {
360
349
let state_root = hex:: decode ( state_root) . map_err ( VerifyError :: StateRootDecoding ) ?;
361
- let message = Message :: from ( to_ethereum_signed ( & state_root) ) ;
350
+ let message = to_ethereum_signed ( & state_root) ;
351
+
362
352
let wallet_sign = self
363
353
. state
364
354
. wallet
365
- . sign ( & self . state . password , & message)
355
+ . sign ( & message)
366
356
// TODO: This is not entirely true, we do not sign an Ethereum Web Token but Outpace state_root
367
357
. map_err ( |err| EwtSigningError :: SigningMessage ( err. to_string ( ) ) ) ?;
368
- let signature: Signature = wallet_sign. into_electrum ( ) . into ( ) ;
369
358
370
- Ok ( format ! ( "0x{}" , signature ) )
359
+ Ok ( format ! ( "0x{}" , hex :: encode ( wallet_sign . to_electrum ( ) ) ) )
371
360
}
372
361
373
362
fn get_auth ( & self , for_chain : ChainId , intended_for : ValidatorId ) -> Result < String , Error > {
@@ -380,8 +369,7 @@ impl Unlocked for Ethereum<UnlockedWallet> {
380
369
chain_id : for_chain,
381
370
} ;
382
371
383
- let token = ewt:: Token :: sign ( & self . state . wallet , & self . state . password , payload)
384
- . map_err ( Error :: SignMessage ) ?;
372
+ let token = ewt:: Token :: sign ( & self . state . wallet , payload) . map_err ( Error :: SignMessage ) ?;
385
373
386
374
Ok ( token. to_string ( ) )
387
375
}
@@ -395,15 +383,14 @@ mod test {
395
383
ewt:: { self , Payload } ,
396
384
get_counterfactual_address,
397
385
test_util:: * ,
398
- to_ethereum_signed,
386
+ to_ethereum_signed, Electrum ,
399
387
} ;
400
388
401
389
use crate :: {
402
390
prelude:: * ,
403
391
primitives:: { Deposit , Session } ,
404
392
} ;
405
393
use chrono:: Utc ;
406
- use ethstore:: ethkey:: Message ;
407
394
408
395
use primitives:: {
409
396
channel:: Nonce ,
@@ -509,14 +496,13 @@ mod test {
509
496
510
497
let signature_actual = {
511
498
let ethers_sign_message = to_ethereum_signed ( & msg_hash_actual) ;
512
- let message = Message :: from ( ethers_sign_message) ;
513
499
514
500
let mut signature = user_adapter
515
501
. state
516
502
. wallet
517
- . sign ( & user_adapter . state . password , & message )
503
+ . sign ( & ethers_sign_message )
518
504
. expect ( "Should sign message" )
519
- . into_electrum ( )
505
+ . to_electrum ( )
520
506
. to_vec ( ) ;
521
507
522
508
signature. extend ( ETH_SIGN_SUFFIX . as_slice ( ) ) ;
@@ -549,14 +535,13 @@ mod test {
549
535
550
536
let signature_actual = {
551
537
let ethers_sign_message = to_ethereum_signed ( & msg_hash_actual) ;
552
- let message = Message :: from ( ethers_sign_message) ;
553
538
554
539
let mut signature = evil_adapter
555
540
. state
556
541
. wallet
557
- . sign ( & evil_adapter . state . password , & message )
542
+ . sign ( & ethers_sign_message )
558
543
. expect ( "Should sign message" )
559
- . into_electrum ( )
544
+ . to_electrum ( )
560
545
. to_vec ( ) ;
561
546
562
547
signature. extend ( ETH_SIGN_SUFFIX . as_slice ( ) ) ;
@@ -631,7 +616,7 @@ mod test {
631
616
chain_id : ganache_chain. chain_id ,
632
617
} ;
633
618
634
- let auth_token = ewt:: Token :: sign ( & adapter. state . wallet , & adapter . state . password , payload)
619
+ let auth_token = ewt:: Token :: sign ( & adapter. state . wallet , payload)
635
620
. expect ( "Should sign successfully the Payload" ) ;
636
621
637
622
let has_privileges = adapter
@@ -684,12 +669,8 @@ mod test {
684
669
chain_id : ganache_chain. chain_id ,
685
670
} ;
686
671
687
- let token = ewt:: Token :: sign (
688
- & signer_adapter. state . wallet ,
689
- & signer_adapter. state . password ,
690
- payload,
691
- )
692
- . expect ( "Should sign successfully the Payload" ) ;
672
+ let token = ewt:: Token :: sign ( & signer_adapter. state . wallet , payload)
673
+ . expect ( "Should sign successfully the Payload" ) ;
693
674
694
675
// double check that we have privileges for _Who Am I_
695
676
assert ! ( adapter
0 commit comments