@@ -7,6 +7,7 @@ pub use alloy::rpc::types::{
77 Block , BlockId , BlockNumberOrTag , FeeHistory , Filter , FilterBlockOption , Log , Transaction ,
88 TransactionReceipt ,
99} ;
10+ pub use alloy:: transports:: Authorization as AlloyAuthorization ;
1011pub use alloy_primitives:: { Address , BlockHash , BlockNumber , Bytes , TxHash , U128 , U256 , U64 , U8 } ;
1112use serde:: { Deserialize , Serialize } ;
1213use std:: collections:: { HashMap , HashSet } ;
@@ -220,23 +221,85 @@ pub struct ProviderConfig {
220221}
221222
222223#[ derive( Clone , Debug , Deserialize , Serialize , Hash , Eq , PartialEq ) ]
224+ pub enum Authorization {
225+ Basic ( String ) ,
226+ Bearer ( String ) ,
227+ Raw ( String ) ,
228+ }
229+
230+ impl From < Authorization > for AlloyAuthorization {
231+ fn from ( auth : Authorization ) -> AlloyAuthorization {
232+ match auth {
233+ Authorization :: Basic ( value) => AlloyAuthorization :: Basic ( value) ,
234+ Authorization :: Bearer ( value) => AlloyAuthorization :: Bearer ( value) ,
235+ Authorization :: Raw ( value) => AlloyAuthorization :: Raw ( value) ,
236+ }
237+ }
238+ }
239+
240+ #[ derive( Clone , Debug , Serialize , Hash , Eq , PartialEq ) ]
223241pub enum NodeOrRpcUrl {
224242 Node {
225243 kns_update : crate :: net:: KnsUpdate ,
226244 use_as_provider : bool , // false for just-routers inside saved config
227245 } ,
228- RpcUrl ( String ) ,
246+ RpcUrl {
247+ url : String ,
248+ auth : Option < Authorization > ,
249+ } ,
229250}
230251
231252impl std:: cmp:: PartialEq < str > for NodeOrRpcUrl {
232253 fn eq ( & self , other : & str ) -> bool {
233254 match self {
234255 NodeOrRpcUrl :: Node { kns_update, .. } => kns_update. name == other,
235- NodeOrRpcUrl :: RpcUrl ( url) => url == other,
256+ NodeOrRpcUrl :: RpcUrl { url, .. } => url == other,
236257 }
237258 }
238259}
239260
261+ impl < ' de > Deserialize < ' de > for NodeOrRpcUrl {
262+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
263+ where
264+ D : serde:: Deserializer < ' de > ,
265+ {
266+ #[ derive( Deserialize ) ]
267+ #[ serde( untagged) ]
268+ enum RpcUrlHelper {
269+ String ( String ) ,
270+ Struct {
271+ url : String ,
272+ auth : Option < Authorization > ,
273+ } ,
274+ }
275+
276+ #[ derive( Deserialize ) ]
277+ enum Helper {
278+ Node {
279+ kns_update : crate :: net:: KnsUpdate ,
280+ use_as_provider : bool ,
281+ } ,
282+ RpcUrl ( RpcUrlHelper ) ,
283+ }
284+
285+ let helper = Helper :: deserialize ( deserializer) ?;
286+
287+ Ok ( match helper {
288+ Helper :: Node {
289+ kns_update,
290+ use_as_provider,
291+ } => NodeOrRpcUrl :: Node {
292+ kns_update,
293+ use_as_provider,
294+ } ,
295+ Helper :: RpcUrl ( url_helper) => match url_helper {
296+ RpcUrlHelper :: String ( url) => NodeOrRpcUrl :: RpcUrl { url, auth : None } ,
297+ RpcUrlHelper :: Struct { url, auth } => NodeOrRpcUrl :: RpcUrl { url, auth } ,
298+ } ,
299+ } )
300+ }
301+ }
302+
240303/// An EVM chain provider. Create this object to start making RPC calls.
241304/// Set the chain_id to determine which chain to call: requests will fail
242305/// unless the node this process is running on has access to a provider
0 commit comments