1
1
use super :: * ;
2
2
3
3
use anyhow:: { anyhow as err, ensure, Error } ;
4
+ use base64:: prelude:: * ;
4
5
use dydx_proto:: dydxprotocol:: accountplus:: {
5
6
AccountAuthenticator , GetAuthenticatorsRequest , MsgAddAuthenticator , MsgRemoveAuthenticator ,
6
7
} ;
7
- use serde:: { Deserialize , Serialize } ;
8
+ use serde:: ser ;
8
9
9
10
/// [`NodeClient`] Authenticator requests dispatcher.
10
11
pub struct Authenticators < ' a > {
@@ -13,20 +14,19 @@ pub struct Authenticators<'a> {
13
14
14
15
/// [`Authenticator`] type.
15
16
/// An authenticator can be composed by a single or multiple types.
16
- #[ derive( Debug , Clone , Eq , Hash , PartialEq , Serialize , Deserialize ) ]
17
- #[ serde( tag = "type" , content = "config" ) ]
17
+ #[ derive( Debug , Clone , Eq , Hash , PartialEq ) ]
18
18
pub enum Authenticator {
19
19
/// Enables authentication via a specific key.
20
20
SignatureVerification ( Vec < u8 > ) ,
21
21
/// Restricts authentication to certain message types. Configured using string bytes, with
22
22
/// different message types separated by commas.
23
- MessageFilter ( Vec < u8 > ) ,
23
+ MessageFilter ( String ) ,
24
24
/// Restricts authentication to certain subaccount constraints. Configured using string bytes,
25
25
/// with different IDs separated by commas.
26
- SubaccountFilter ( Vec < u8 > ) ,
26
+ SubaccountFilter ( String ) ,
27
27
/// Restricts transactions to specific CLOB pair IDs. Configured using string bytes, with
28
28
/// different subaccount numbers separated by commas.
29
- ClobPairIdFilter ( Vec < u8 > ) ,
29
+ ClobPairIdFilter ( String ) ,
30
30
/// Composable type, restricts authentication if any sub-authenticator is valid.
31
31
AnyOf ( Vec < Authenticator > ) ,
32
32
/// Composable type, restricts authentication if all sub-authenticators are valid.
@@ -125,10 +125,10 @@ impl Authenticator {
125
125
Authenticator :: AllOf ( types) | Authenticator :: AnyOf ( types) => {
126
126
Ok ( serde_json:: to_string ( & types) ?. into_bytes ( ) )
127
127
}
128
- Authenticator :: SignatureVerification ( v)
129
- | Authenticator :: MessageFilter ( v)
128
+ Authenticator :: SignatureVerification ( v) => Ok ( v . clone ( ) ) ,
129
+ Authenticator :: MessageFilter ( v)
130
130
| Authenticator :: ClobPairIdFilter ( v)
131
- | Authenticator :: SubaccountFilter ( v) => Ok ( v. clone ( ) ) ,
131
+ | Authenticator :: SubaccountFilter ( v) => Ok ( v. clone ( ) . into_bytes ( ) ) ,
132
132
}
133
133
}
134
134
@@ -172,6 +172,38 @@ impl Authenticator {
172
172
}
173
173
}
174
174
175
+ impl serde:: Serialize for Authenticator {
176
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
177
+ where
178
+ S : serde:: Serializer ,
179
+ {
180
+ let mut json_obj = serde_json:: Map :: new ( ) ;
181
+
182
+ // type (tag)
183
+ json_obj. insert (
184
+ "type" . to_string ( ) ,
185
+ serde_json:: Value :: String ( self . type_to_str ( ) . to_string ( ) ) ,
186
+ ) ;
187
+
188
+ // config (content)
189
+ let config_bytes = match self {
190
+ Authenticator :: SignatureVerification ( bytes) => bytes. to_owned ( ) ,
191
+ Authenticator :: MessageFilter ( string)
192
+ | Authenticator :: SubaccountFilter ( string)
193
+ | Authenticator :: ClobPairIdFilter ( string) => string. clone ( ) . into_bytes ( ) ,
194
+ Authenticator :: AnyOf ( auths) | Authenticator :: AllOf ( auths) => {
195
+ serde_json:: to_string ( auths)
196
+ . map_err ( |e| ser:: Error :: custom ( format ! ( "JSON serialization error: {}" , e) ) ) ?
197
+ . into_bytes ( )
198
+ }
199
+ } ;
200
+ let base64 = BASE64_STANDARD . encode ( config_bytes) ;
201
+ json_obj. insert ( "config" . to_string ( ) , serde_json:: Value :: String ( base64) ) ;
202
+
203
+ serde_json:: Value :: Object ( json_obj) . serialize ( serializer)
204
+ }
205
+ }
206
+
175
207
#[ cfg( test) ]
176
208
mod tests {
177
209
use super :: * ;
0 commit comments