@@ -7,11 +7,12 @@ use primitives::{
77 adapter:: { Adapter , AdapterError , AdapterOptions , AdapterResult , Session } ,
88 channel_validator:: ChannelValidator ,
99 config:: Config ,
10- Channel , ValidatorDesc ,
10+ Channel ,
1111} ;
1212use serde:: { Deserialize , Serialize } ;
1313use serde_json:: Value ;
1414use std:: collections:: HashMap ;
15+ use std:: convert:: TryFrom ;
1516use std:: error:: Error ;
1617use std:: fs;
1718use tiny_keccak:: Keccak ;
@@ -52,9 +53,11 @@ impl Adapter for EthereumAdapter {
5253 type Output = EthereumAdapter ;
5354
5455 fn init ( opts : AdapterOptions , config : & Config ) -> AdapterResult < EthereumAdapter > {
55- let ( keystore_file, keystore_pwd) = match ( opts. keystore_file , opts. keystore_pwd ) {
56- ( Some ( file) , Some ( pwd) ) => ( file, pwd) ,
57- ( _, _) => {
56+ let ( keystore_file, keystore_pwd) = match opts {
57+ AdapterOptions :: EthereumAdapter ( keystore_opts) => {
58+ ( keystore_opts. keystore_file , keystore_opts. keystore_pwd )
59+ }
60+ _ => {
5861 return Err ( AdapterError :: Configuration (
5962 "Missing keystore json file or password" . to_string ( ) ,
6063 ) )
@@ -139,7 +142,19 @@ impl Adapter for EthereumAdapter {
139142 }
140143
141144 fn validate_channel ( & self , channel : & Channel ) -> AdapterResult < bool > {
142- let eth_channel: EthereumChannel = channel. into ( ) ;
145+ if check_validator_id_checksum ( channel) {
146+ return Err ( AdapterError :: Configuration (
147+ "channel.validators: all addresses are checksummed" . to_string ( ) ,
148+ ) ) ;
149+ }
150+ // check if channel is valid
151+ if let Err ( e) = EthereumAdapter :: is_channel_valid ( & self . config , channel) {
152+ return Err ( AdapterError :: InvalidChannel ( e. to_string ( ) ) ) ;
153+ }
154+
155+ let eth_channel = EthereumChannel :: try_from ( channel)
156+ . map_err ( |e| AdapterError :: InvalidChannel ( e. to_string ( ) ) ) ?;
157+
143158 let channel_id = eth_channel
144159 . hash_hex ( & self . config . ethereum_core_address )
145160 . map_err ( |_| map_error ( "Failed to hash the channel id" ) ) ?;
@@ -150,15 +165,6 @@ impl Adapter for EthereumAdapter {
150165 ) ) ;
151166 }
152167
153- if check_validator_id_checksum ( channel) {
154- return Err ( AdapterError :: Configuration (
155- "channel.validators: all addresses are checksummed" . to_string ( ) ,
156- ) ) ;
157- }
158- // check if channel is valid
159- if let Err ( error) = EthereumAdapter :: is_channel_valid ( & self . config , channel) {
160- return Err ( AdapterError :: InvalidChannel ( error. to_string ( ) ) ) ;
161- }
162168 // query the blockchain for the channel status
163169 let contract_address = Address :: from_slice ( self . config . ethereum_core_address . as_bytes ( ) ) ;
164170 let contract = get_contract ( & self . config , contract_address, & ADEXCORE_ABI )
@@ -399,17 +405,16 @@ pub fn ewt_verify(
399405#[ cfg( test) ]
400406mod test {
401407 use super :: * ;
408+ use primitives:: adapter:: KeystoreOptions ;
402409 use primitives:: config:: configuration;
403410
404411 fn setup_eth_adapter ( ) -> EthereumAdapter {
405412 let config = configuration ( "development" , None ) . expect ( "failed parse config" ) ;
406- let adapter_options = AdapterOptions {
407- keystore_file : Some ( "./test/resources/keystore.json" . to_string ( ) ) ,
408- keystore_pwd : Some ( "adexvalidator" . to_string ( ) ) ,
409- dummy_identity : None ,
410- dummy_auth : None ,
411- dummy_auth_tokens : None ,
413+ let keystore_options = KeystoreOptions {
414+ keystore_file : "./test/resources/keystore.json" . to_string ( ) ,
415+ keystore_pwd : "adexvalidator" . to_string ( ) ,
412416 } ;
417+ let adapter_options = AdapterOptions :: EthereumAdapter ( keystore_options) ;
413418
414419 EthereumAdapter :: init ( adapter_options, & config) . expect ( "should init ethereum adapter" )
415420 }
0 commit comments