@@ -5,6 +5,7 @@ use ethkey::Password;
5
5
use ethstore:: SafeAccount ;
6
6
use futures:: compat:: Future01CompatExt ;
7
7
use futures:: future:: { BoxFuture , FutureExt } ;
8
+ use futures:: TryFutureExt ;
8
9
use lazy_static:: lazy_static;
9
10
use parity_crypto:: publickey:: {
10
11
public_to_address, recover, verify_address, Address , Message , Signature ,
@@ -15,6 +16,7 @@ use primitives::{
15
16
config:: Config ,
16
17
Channel , ToETHChecksum , ValidatorId ,
17
18
} ;
19
+ use reqwest:: Client ;
18
20
use serde:: { Deserialize , Serialize } ;
19
21
use serde_hex:: { SerHexOpt , StrictPfx } ;
20
22
use serde_json:: Value ;
@@ -34,10 +36,8 @@ use web3::{
34
36
lazy_static ! {
35
37
static ref ADEXCORE_ABI : & ' static [ u8 ] =
36
38
include_bytes!( "../../lib/protocol-eth/abi/AdExCore.json" ) ;
37
- static ref IDENTITY_ABI : & ' static [ u8 ] =
38
- include_bytes!( "../../lib/protocol-eth/abi/Identity.json" ) ;
39
39
static ref CHANNEL_STATE_ACTIVE : U256 = 1 . into( ) ;
40
- static ref PRIVILEGE_LEVEL_NONE : U256 = 0 . into ( ) ;
40
+ static ref PRIVILEGE_LEVEL_NONE : u8 = 0 ;
41
41
}
42
42
43
43
#[ derive( Debug , Clone ) ]
@@ -49,6 +49,7 @@ pub struct EthereumAdapter {
49
49
wallet : Option < SafeAccount > ,
50
50
event_loop : Arc < EventLoopHandle > ,
51
51
web3 : Web3 < Http > ,
52
+ relayer : RelayerClient ,
52
53
}
53
54
54
55
// Enables EthereumAdapter to be able to
@@ -78,6 +79,8 @@ impl EthereumAdapter {
78
79
. map_err ( |_| map_error ( "failed to init http transport" ) ) ?;
79
80
let event_loop = Arc :: new ( eloop) ;
80
81
let web3 = web3:: Web3 :: new ( transport) ;
82
+ let relayer = RelayerClient :: new ( & config. ethereum_adapter_relayer )
83
+ . map_err ( |_| map_error ( "Client for Relayer couldn't be built" ) ) ?;
81
84
82
85
Ok ( Self {
83
86
address,
@@ -87,6 +90,7 @@ impl EthereumAdapter {
87
90
config : config. to_owned ( ) ,
88
91
event_loop,
89
92
web3,
93
+ relayer,
90
94
} )
91
95
}
92
96
}
@@ -219,31 +223,17 @@ impl Adapter for EthereumAdapter {
219
223
220
224
let sess = match & verified. payload . identity {
221
225
Some ( identity) => {
222
- let contract_address: Address = identity. into ( ) ;
223
- let contract =
224
- Contract :: from_json ( self . web3 . eth ( ) , contract_address, & IDENTITY_ABI )
225
- . map_err ( |_| map_error ( "failed to init identity contract" ) ) ?;
226
-
227
- let privilege_level: U256 = contract
228
- . query (
229
- "privileges" ,
230
- ( Token :: Address ( Address :: from_slice ( verified. from . inner ( ) ) ) , ) ,
231
- None ,
232
- Options :: default ( ) ,
233
- None ,
234
- )
235
- . compat ( )
236
- . await
237
- . map_err ( |_| map_error ( "failed query priviledge level on contract" ) ) ?;
238
-
239
- if privilege_level == * PRIVILEGE_LEVEL_NONE {
226
+ let privilege = self . relayer . get_privilege ( identity) . await ?;
227
+
228
+ if privilege > * PRIVILEGE_LEVEL_NONE {
240
229
return Err ( AdapterError :: Authorization (
241
230
"insufficient privilege" . to_string ( ) ,
242
231
) ) ;
243
- }
244
- Session {
245
- era : verified. payload . era ,
246
- uid : identity. into ( ) ,
232
+ } else {
233
+ Session {
234
+ era : verified. payload . era ,
235
+ uid : identity. into ( ) ,
236
+ }
247
237
}
248
238
}
249
239
None => Session {
@@ -276,6 +266,49 @@ impl Adapter for EthereumAdapter {
276
266
}
277
267
}
278
268
269
+ #[ derive( Debug , Clone ) ]
270
+ struct RelayerClient {
271
+ client : Client ,
272
+ relayer_url : String ,
273
+ }
274
+
275
+ impl RelayerClient {
276
+ pub fn new ( relayer_url : & str ) -> Result < Self , reqwest:: Error > {
277
+ let client = Client :: builder ( ) . build ( ) ?;
278
+
279
+ Ok ( Self {
280
+ relayer_url : relayer_url. to_string ( ) ,
281
+ client,
282
+ } )
283
+ }
284
+
285
+ pub async fn get_privilege ( & self , identity : & [ u8 ; 20 ] ) -> Result < u8 , AdapterError > {
286
+ use reqwest:: Response ;
287
+ use std:: collections:: HashMap ;
288
+
289
+ let relay_url = format ! (
290
+ "{}/identity/by-owner/{}" ,
291
+ self . relayer_url,
292
+ hex:: encode( identity)
293
+ ) ;
294
+
295
+ let identities_owned: HashMap < [ u8 ; 20 ] , u8 > = self
296
+ . client
297
+ . get ( & relay_url)
298
+ . send ( )
299
+ . and_then ( |res : Response | res. json ( ) )
300
+ . await
301
+ . map_err ( |_| map_error ( "Fetching privileges failed" ) ) ?;
302
+
303
+ let privilege = identities_owned
304
+ . get ( identity)
305
+ . copied ( )
306
+ . unwrap_or_else ( || 0_u8 ) ;
307
+
308
+ Ok ( privilege)
309
+ }
310
+ }
311
+
279
312
fn hash_message ( message : & str ) -> [ u8 ; 32 ] {
280
313
let eth = "\x19 Ethereum Signed Message:\n " ;
281
314
let message_length = message. len ( ) ;
@@ -377,7 +410,7 @@ pub fn ewt_verify(
377
410
let payload: Payload = serde_json:: from_str ( & payload_string) ?;
378
411
379
412
let verified_payload = VerifyPayload {
380
- from : ValidatorId :: try_from ( & format ! ( "{:?}" , address) ) ? ,
413
+ from : ValidatorId :: from ( address. as_fixed_bytes ( ) ) ,
381
414
payload,
382
415
} ;
383
416
@@ -604,13 +637,10 @@ mod test {
604
637
. await
605
638
. expect ( "open channel" ) ;
606
639
607
- let contract_addr = <[ u8 ; 20 ] >:: from_hex ( & format ! ( "{:?}" , adex_contract. address( ) ) [ 2 ..] )
608
- . expect ( "failed to deserialize contract addr" ) ;
609
-
640
+ let contract_addr = adex_contract. address ( ) . to_fixed_bytes ( ) ;
610
641
let channel_id = eth_channel. hash ( & contract_addr) . expect ( "hash hex" ) ;
611
642
// set id to proper id
612
- valid_channel. id = ChannelId :: from_hex ( hex:: encode ( channel_id) )
613
- . expect ( "prep_db: failed to deserialize channel id" ) ;
643
+ valid_channel. id = ChannelId :: from ( channel_id) ;
614
644
615
645
// eth adapter
616
646
let mut eth_adapter = setup_eth_adapter ( Some ( contract_addr) ) ;
@@ -623,68 +653,4 @@ mod test {
623
653
624
654
assert_eq ! ( result, true , "should validate valid channel correctly" ) ;
625
655
}
626
-
627
- #[ tokio:: test]
628
- async fn should_generate_session_from_token_with_identity ( ) {
629
- // setup test payload
630
- let mut eth_adapter = setup_eth_adapter ( None ) ;
631
- eth_adapter. unlock ( ) . expect ( "should unlock eth adapter" ) ;
632
-
633
- // part of address used in initializing ganache-cli
634
- let leader_account: Address = "Df08F82De32B8d460adbE8D72043E3a7e25A3B39"
635
- . parse ( )
636
- . expect ( "failed to parse leader account" ) ;
637
-
638
- let eth_adapter_address: Address = eth_adapter
639
- . whoami ( )
640
- . to_hex_non_prefix_string ( )
641
- . parse ( )
642
- . expect ( "failed to parse eth adapter address" ) ;
643
-
644
- let identity_bytecode = include_str ! ( "../test/resources/identitybytecode.json" ) ;
645
-
646
- // deploy identity contract
647
- let identity_contract = Contract :: deploy ( eth_adapter. web3 . eth ( ) , & IDENTITY_ABI )
648
- . expect ( "invalid token token contract" )
649
- . confirmations ( 0 )
650
- . options ( Options :: with ( |opt| {
651
- opt. gas_price = Some ( 1 . into ( ) ) ;
652
- opt. gas = Some ( 6_721_975 . into ( ) ) ;
653
- } ) )
654
- . execute (
655
- identity_bytecode,
656
- (
657
- Token :: Array ( vec ! [ Token :: Address ( eth_adapter_address) ] ) ,
658
- Token :: Array ( vec ! [ Token :: Uint ( 1 . into( ) ) ] ) ,
659
- ) ,
660
- leader_account,
661
- )
662
- . expect ( "Correct parameters are passed to the constructor." )
663
- . compat ( )
664
- . await
665
- . expect ( "failed to initialize identity contract" ) ;
666
-
667
- // identity contract address
668
- let identity = <[ u8 ; 20 ] >:: from_hex ( & format ! ( "{:?}" , identity_contract. address( ) ) [ 2 ..] )
669
- . expect ( "failed to deserialize address" ) ;
670
-
671
- let payload = Payload {
672
- id : eth_adapter. whoami ( ) . to_checksum ( ) ,
673
- era : 100_000 ,
674
- address : format ! ( "{:?}" , leader_account) ,
675
- identity : Some ( identity) ,
676
- } ;
677
-
678
- let wallet = eth_adapter. wallet . clone ( ) ;
679
- let response = ewt_sign ( & wallet. unwrap ( ) , & eth_adapter. keystore_pwd , & payload)
680
- . expect ( "failed to generate ewt signature" ) ;
681
-
682
- // verify since its with identity
683
- let session = eth_adapter
684
- . session_from_token ( & response)
685
- . await
686
- . expect ( "failed generate session" ) ;
687
-
688
- assert_eq ! ( session. uid. inner( ) , & identity) ;
689
- }
690
656
}
0 commit comments