1
1
use primitives:: adapter:: { Adapter , AdapterError , AdapterResult , DummyAdapterOptions , Session } ;
2
2
use primitives:: channel_validator:: ChannelValidator ;
3
3
use primitives:: config:: Config ;
4
- use primitives:: Channel ;
4
+ use primitives:: { Channel , ValidatorId } ;
5
5
use std:: collections:: HashMap ;
6
6
7
7
#[ derive( Debug , Clone ) ]
8
8
pub struct DummyAdapter {
9
- identity : String ,
9
+ identity : ValidatorId ,
10
10
config : Config ,
11
11
// Auth tokens that we have verified (tokenId => session)
12
- session_tokens : HashMap < String , String > ,
12
+ session_tokens : HashMap < String , ValidatorId > ,
13
13
// Auth tokens that we've generated to authenticate with someone (address => token)
14
14
authorization_tokens : HashMap < String , String > ,
15
15
}
@@ -23,8 +23,8 @@ impl DummyAdapter {
23
23
Self {
24
24
identity : opts. dummy_identity ,
25
25
config : config. to_owned ( ) ,
26
- session_tokens : opts. dummy_auth_tokens ,
27
- authorization_tokens : opts. dummy_auth ,
26
+ session_tokens : opts. dummy_auth ,
27
+ authorization_tokens : opts. dummy_auth_tokens ,
28
28
}
29
29
}
30
30
}
@@ -34,8 +34,8 @@ impl Adapter for DummyAdapter {
34
34
Ok ( ( ) )
35
35
}
36
36
37
- fn whoami ( & self ) -> String {
38
- self . identity . clone ( )
37
+ fn whoami ( & self ) -> & ValidatorId {
38
+ & self . identity
39
39
}
40
40
41
41
fn sign ( & self , state_root : & str ) -> AdapterResult < String > {
@@ -47,11 +47,16 @@ impl Adapter for DummyAdapter {
47
47
Ok ( signature)
48
48
}
49
49
50
- fn verify ( & self , signer : & str , _state_root : & str , signature : & str ) -> AdapterResult < bool > {
50
+ fn verify (
51
+ & self ,
52
+ signer : & ValidatorId ,
53
+ _state_root : & str ,
54
+ signature : & str ,
55
+ ) -> AdapterResult < bool > {
51
56
// select the `identity` and compare it to the signer
52
57
// for empty string this will return array with 1 element - an empty string `[""]`
53
58
let is_same = match signature. rsplit ( ' ' ) . take ( 1 ) . next ( ) {
54
- Some ( from) => from == signer,
59
+ Some ( from) => from == signer. to_string ( ) ,
55
60
None => false ,
56
61
} ;
57
62
@@ -73,7 +78,7 @@ impl Adapter for DummyAdapter {
73
78
74
79
match identity {
75
80
Some ( ( id, _) ) => Ok ( Session {
76
- uid : id . to_owned ( ) ,
81
+ uid : self . session_tokens [ id ] . clone ( ) ,
77
82
era : 0 ,
78
83
} ) ,
79
84
None => Err ( AdapterError :: Authentication ( format ! (
@@ -83,12 +88,11 @@ impl Adapter for DummyAdapter {
83
88
}
84
89
}
85
90
86
- fn get_auth ( & mut self , _validator : & str ) -> AdapterResult < String > {
91
+ fn get_auth ( & mut self , _validator : & ValidatorId ) -> AdapterResult < String > {
87
92
let who = self
88
93
. session_tokens
89
94
. iter ( )
90
95
. find ( |( _, id) | * id == & self . identity ) ;
91
-
92
96
match who {
93
97
Some ( ( id, _) ) => {
94
98
let auth = self . authorization_tokens . get ( id) . expect ( "id should exist" ) ;
0 commit comments