From 9ed235e2dc388d729bf0bffa28301234e16a5bb5 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sun, 5 May 2024 14:23:36 +0100 Subject: [PATCH] fix: use Provider instead of URL --- blockchain_api/Cargo.toml | 2 ++ blockchain_api/src/lib.rs | 15 ++++++++++----- relay_client/src/websocket/stream.rs | 4 ++-- relay_rpc/Cargo.toml | 6 +++++- relay_rpc/src/auth/cacao.rs | 4 ++-- .../src/auth/cacao/signature/get_provider.rs | 9 +++++++++ .../src/auth/cacao/signature/get_rpc_url.rs | 6 ------ relay_rpc/src/auth/cacao/signature/mod.rs | 17 +++++++---------- relay_rpc/src/auth/cacao/tests.rs | 14 +++++++++++--- 9 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 relay_rpc/src/auth/cacao/signature/get_provider.rs delete mode 100644 relay_rpc/src/auth/cacao/signature/get_rpc_url.rs diff --git a/blockchain_api/Cargo.toml b/blockchain_api/Cargo.toml index 13eedc1..dfc7879 100644 --- a/blockchain_api/Cargo.toml +++ b/blockchain_api/Cargo.toml @@ -5,6 +5,8 @@ edition = "2021" license = "Apache-2.0" [dependencies] +alloy-provider = { git = "https://github.com/alloy-rs/alloy.git", rev = "d68a6b7" } +alloy-transport-http = { git = "https://github.com/alloy-rs/alloy.git", rev = "d68a6b7" } relay_rpc = { path = "../relay_rpc", features = ["cacao"] } reqwest = { version = "0.12.2", features = ["json"] } serde = "1.0" diff --git a/blockchain_api/src/lib.rs b/blockchain_api/src/lib.rs index 4c70fb0..feaaddf 100644 --- a/blockchain_api/src/lib.rs +++ b/blockchain_api/src/lib.rs @@ -1,6 +1,7 @@ pub use reqwest::Error; use { - relay_rpc::{auth::cacao::signature::get_rpc_url::GetRpcUrl, domain::ProjectId}, + alloy_provider::ReqwestProvider, + relay_rpc::{auth::cacao::signature::get_provider::GetProvider, domain::ProjectId}, serde::Deserialize, std::{collections::HashSet, convert::Infallible, sync::Arc, time::Duration}, tokio::{sync::RwLock, task::JoinHandle}, @@ -98,18 +99,22 @@ fn build_rpc_url(blockchain_api_rpc_endpoint: Url, chain_id: &str, project_id: & url } -impl GetRpcUrl for BlockchainApiProvider { - async fn get_rpc_url(&self, chain_id: String) -> Option { +impl GetProvider for BlockchainApiProvider { + type Provider = ReqwestProvider; + type Transport = alloy_transport_http::Http; + + async fn get_provider(&self, chain_id: String) -> Option { self.supported_chains .read() .await .contains(&chain_id) .then(|| { - build_rpc_url( + let url = build_rpc_url( self.blockchain_api_rpc_endpoint.clone(), &chain_id, self.project_id.as_ref(), - ) + ); + ReqwestProvider::new_http(url) }) } } diff --git a/relay_client/src/websocket/stream.rs b/relay_client/src/websocket/stream.rs index 28a5e1e..d2b8cab 100644 --- a/relay_client/src/websocket/stream.rs +++ b/relay_client/src/websocket/stream.rs @@ -141,7 +141,7 @@ impl ClientStream { /// Closes the connection. pub async fn close(&mut self, frame: Option>) -> Result<(), ClientError> { - self.close_frame = frame.clone(); + self.close_frame.clone_from(&frame); self.socket .close(frame) .await @@ -220,7 +220,7 @@ impl ClientStream { } Message::Close(frame) => { - self.close_frame = frame.clone(); + self.close_frame.clone_from(frame); Some(StreamEvent::ConnectionClosed(frame.clone())) } diff --git a/relay_rpc/Cargo.toml b/relay_rpc/Cargo.toml index 0473b69..96e1256 100644 --- a/relay_rpc/Cargo.toml +++ b/relay_rpc/Cargo.toml @@ -7,9 +7,10 @@ license = "Apache-2.0" [features] default = ["cacao"] cacao = [ + "dep:erc6492", + "dep:alloy-transport", "dep:alloy-provider", "dep:alloy-primitives", - "dep:erc6492", ] [dependencies] @@ -36,6 +37,7 @@ once_cell = "1.16" jsonwebtoken = "8.1" sha2 = { version = "0.10.6" } url = "2" +alloy-transport = { git = "https://github.com/alloy-rs/alloy.git", rev = "d68a6b7", optional = true } alloy-provider = { git = "https://github.com/alloy-rs/alloy.git", rev = "d68a6b7", optional = true } alloy-primitives = { version = "0.7.0", optional = true } erc6492 = { git = "https://github.com/WalletConnect/erc6492.git", optional = true } @@ -44,6 +46,8 @@ strum = { version = "0.26", features = ["strum_macros", "derive"] } [dev-dependencies] k256 = "0.13" tokio = { version = "1.35.1", features = ["test-util", "macros"] } +alloy-transport-http = { git = "https://github.com/alloy-rs/alloy.git", rev = "d68a6b7" } +reqwest = "0.12.4" [lints.clippy] indexing_slicing = "deny" diff --git a/relay_rpc/src/auth/cacao.rs b/relay_rpc/src/auth/cacao.rs index 9f888bc..ddbf461 100644 --- a/relay_rpc/src/auth/cacao.rs +++ b/relay_rpc/src/auth/cacao.rs @@ -2,7 +2,7 @@ use { self::{ header::Header, payload::Payload, - signature::{get_rpc_url::GetRpcUrl, Signature}, + signature::{get_provider::GetProvider, Signature}, }, alloy_primitives::AddressError, core::fmt::Debug, @@ -98,7 +98,7 @@ pub struct Cacao { impl Cacao { const ETHEREUM: &'static str = "Ethereum"; - pub async fn verify(&self, provider: Option<&impl GetRpcUrl>) -> Result<(), CacaoError> { + pub async fn verify(&self, provider: Option<&impl GetProvider>) -> Result<(), CacaoError> { self.p.validate()?; self.h.validate()?; self.s.verify(self, provider).await diff --git a/relay_rpc/src/auth/cacao/signature/get_provider.rs b/relay_rpc/src/auth/cacao/signature/get_provider.rs new file mode 100644 index 0000000..903f950 --- /dev/null +++ b/relay_rpc/src/auth/cacao/signature/get_provider.rs @@ -0,0 +1,9 @@ +use {alloy_provider::Provider, alloy_transport::Transport}; + +pub trait GetProvider { + type Transport: Transport + Clone; + type Provider: Provider; + + #[allow(async_fn_in_trait)] + async fn get_provider(&self, chain_id: String) -> Option; +} diff --git a/relay_rpc/src/auth/cacao/signature/get_rpc_url.rs b/relay_rpc/src/auth/cacao/signature/get_rpc_url.rs deleted file mode 100644 index fb712b9..0000000 --- a/relay_rpc/src/auth/cacao/signature/get_rpc_url.rs +++ /dev/null @@ -1,6 +0,0 @@ -use url::Url; - -pub trait GetRpcUrl { - #[allow(async_fn_in_trait)] - async fn get_rpc_url(&self, chain_id: String) -> Option; -} diff --git a/relay_rpc/src/auth/cacao/signature/mod.rs b/relay_rpc/src/auth/cacao/signature/mod.rs index c04b077..c5bfd21 100644 --- a/relay_rpc/src/auth/cacao/signature/mod.rs +++ b/relay_rpc/src/auth/cacao/signature/mod.rs @@ -1,17 +1,16 @@ use { self::{ eip191::{verify_eip191, EIP191}, - get_rpc_url::GetRpcUrl, + get_provider::GetProvider, }, super::{Cacao, CacaoError}, alloy_primitives::{hex::FromHex, Address, Bytes}, - alloy_provider::{network::Ethereum, ReqwestProvider}, erc6492::verify_signature, serde::{Deserialize, Serialize}, }; pub mod eip191; -pub mod get_rpc_url; +pub mod get_provider; #[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Hash)] pub struct Signature { @@ -26,7 +25,7 @@ impl Signature { pub async fn verify( &self, cacao: &Cacao, - provider: Option<&impl GetRpcUrl>, + provider: Option<&impl GetProvider>, ) -> Result<(), CacaoError> { let chain_id = cacao.p.chain_id_reference()?; let address = cacao.p.address()?; @@ -43,12 +42,10 @@ impl Signature { } EIP1271 | EIP6492 => { if let Some(provider) = provider { - let provider = ReqwestProvider::::new_http( - provider - .get_rpc_url(chain_id) - .await - .ok_or(CacaoError::ProviderNotAvailable)?, - ); + let provider = provider + .get_provider(chain_id) + .await + .ok_or(CacaoError::ProviderNotAvailable)?; let result = verify_signature(signature, address, message, provider) .await .map_err(CacaoError::Rpc)?; diff --git a/relay_rpc/src/auth/cacao/tests.rs b/relay_rpc/src/auth/cacao/tests.rs index 0b5dcea..0ccdd00 100644 --- a/relay_rpc/src/auth/cacao/tests.rs +++ b/relay_rpc/src/auth/cacao/tests.rs @@ -1,9 +1,17 @@ -use {super::signature::get_rpc_url::GetRpcUrl, crate::auth::cacao::Cacao, url::Url}; +use { + super::signature::get_provider::GetProvider, + crate::auth::cacao::Cacao, + alloy_provider::ReqwestProvider, +}; struct MockGetRpcUrl; -impl GetRpcUrl for MockGetRpcUrl { - async fn get_rpc_url(&self, _: String) -> Option { +// https://github.com/alloy-rs/alloy/issues/568 +impl GetProvider for MockGetRpcUrl { + type Provider = ReqwestProvider; + type Transport = alloy_transport_http::Http; + + async fn get_provider(&self, _: String) -> Option { None } }