Skip to content

Commit

Permalink
fix: trait objects work better than boxed function traits
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 committed Jan 31, 2024
1 parent ed663fc commit 6be145b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
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::{GetProvider, Signature},
signature::{GetRpcUrl, Signature},
},
core::fmt::Debug,
serde::{Deserialize, Serialize},
Expand Down Expand Up @@ -91,7 +91,7 @@ pub struct Cacao {
impl Cacao {
const ETHEREUM: &'static str = "Ethereum";

pub async fn verify(&self, provider: GetProvider) -> Result<bool, CacaoError> {
pub async fn verify(&self, provider: &impl GetRpcUrl) -> Result<bool, CacaoError> {
self.p.validate()?;
self.h.validate()?;
self.s.verify(self, provider).await
Expand Down
8 changes: 5 additions & 3 deletions relay_rpc/src/auth/cacao/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ pub struct Signature {
pub s: String,
}

pub type GetProvider = Box<dyn Fn(String) -> Option<Url>>;
pub trait GetRpcUrl {
fn get_rpc_url(&self, chain_id: String) -> Option<Url>;
}

impl Signature {
pub async fn verify(
&self,
cacao: &Cacao,
get_provider: GetProvider,
get_provider: &impl GetRpcUrl,
) -> Result<bool, CacaoError> {
let address = cacao.p.address()?;

Expand All @@ -40,7 +42,7 @@ impl Signature {
EIP191 => Eip191.verify(&signature, &address, hash),
EIP1271 => {
let chain_id = cacao.p.chain_id_reference()?;
let provider = get_provider(chain_id);
let provider = get_provider.get_rpc_url(chain_id);
if let Some(provider) = provider {
Eip1271
.verify(
Expand Down
16 changes: 12 additions & 4 deletions relay_rpc/src/auth/cacao/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
use crate::auth::cacao::Cacao;
use {super::signature::GetRpcUrl, crate::auth::cacao::Cacao, url::Url};

struct MockGetRpcUrl;

impl GetRpcUrl for MockGetRpcUrl {
fn get_rpc_url(&self, _: String) -> Option<Url> {
None
}
}

/// Test that we can verify a deprecated Cacao.
#[tokio::test]
Expand All @@ -24,7 +32,7 @@ async fn cacao_verify_success() {
}
}"#;
let cacao: Cacao = serde_json::from_str(cacao_serialized).unwrap();
let result = cacao.verify(Box::new(|_| None)).await;
let result = cacao.verify(&MockGetRpcUrl).await;
assert!(result.is_ok());
assert!(result.map_err(|_| false).unwrap());

Expand Down Expand Up @@ -61,7 +69,7 @@ async fn cacao_verify_success_identity_in_audience() {
}
}"#;
let cacao: Cacao = serde_json::from_str(cacao_serialized).unwrap();
let result = cacao.verify(Box::new(|_| None)).await;
let result = cacao.verify(&MockGetRpcUrl).await;
assert!(result.is_ok());
assert!(result.map_err(|_| false).unwrap());

Expand Down Expand Up @@ -97,6 +105,6 @@ async fn cacao_verify_failure() {
}
}"#;
let cacao: Cacao = serde_json::from_str(cacao_serialized).unwrap();
let result = cacao.verify(Box::new(|_| None)).await;
let result = cacao.verify(&MockGetRpcUrl).await;
assert!(result.is_err());
}

0 comments on commit 6be145b

Please sign in to comment.