Skip to content

Commit

Permalink
fix: use Provider instead of URL
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 committed May 5, 2024
1 parent 1ff884f commit 9ed235e
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 29 deletions.
2 changes: 2 additions & 0 deletions blockchain_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 10 additions & 5 deletions blockchain_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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<Url> {
impl GetProvider for BlockchainApiProvider {
type Provider = ReqwestProvider;
type Transport = alloy_transport_http::Http<reqwest::Client>;

async fn get_provider(&self, chain_id: String) -> Option<Self::Provider> {
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)
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions relay_client/src/websocket/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl ClientStream {

/// Closes the connection.
pub async fn close(&mut self, frame: Option<CloseFrame<'static>>) -> Result<(), ClientError> {
self.close_frame = frame.clone();
self.close_frame.clone_from(&frame);
self.socket
.close(frame)
.await
Expand Down Expand Up @@ -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()))
}

Expand Down
6 changes: 5 additions & 1 deletion relay_rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 }
Expand All @@ -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"
4 changes: 2 additions & 2 deletions relay_rpc/src/auth/cacao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions relay_rpc/src/auth/cacao/signature/get_provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use {alloy_provider::Provider, alloy_transport::Transport};

pub trait GetProvider {
type Transport: Transport + Clone;
type Provider: Provider<Self::Transport>;

#[allow(async_fn_in_trait)]
async fn get_provider(&self, chain_id: String) -> Option<Self::Provider>;
}
6 changes: 0 additions & 6 deletions relay_rpc/src/auth/cacao/signature/get_rpc_url.rs

This file was deleted.

17 changes: 7 additions & 10 deletions relay_rpc/src/auth/cacao/signature/mod.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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()?;
Expand All @@ -43,12 +42,10 @@ impl Signature {
}
EIP1271 | EIP6492 => {
if let Some(provider) = provider {
let provider = ReqwestProvider::<Ethereum>::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)?;
Expand Down
14 changes: 11 additions & 3 deletions relay_rpc/src/auth/cacao/tests.rs
Original file line number Diff line number Diff line change
@@ -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<Url> {
// https://github.com/alloy-rs/alloy/issues/568
impl GetProvider for MockGetRpcUrl {
type Provider = ReqwestProvider;
type Transport = alloy_transport_http::Http<reqwest::Client>;

async fn get_provider(&self, _: String) -> Option<Self::Provider> {
None
}
}
Expand Down

0 comments on commit 9ed235e

Please sign in to comment.