Skip to content

Commit

Permalink
fix: dynamic provider and test
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 committed Jan 30, 2024
1 parent f7a74e0 commit 81cc254
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 26 deletions.
16 changes: 8 additions & 8 deletions relay_rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ jsonwebtoken = "8.1"
k256 = { version = "0.13", optional = true }
sha3 = { version = "0.10", optional = true }
sha2 = { version = "0.10.6" }
alloy-providers = { git = "https://github.com/alloy-rs/alloy.git", rev = "5ed60f8" }
alloy-transport = { git = "https://github.com/alloy-rs/alloy.git", rev = "5ed60f8" }
alloy-transport-http = { git = "https://github.com/alloy-rs/alloy.git", rev = "5ed60f8" }
reqwest = { version = "0.11", features = ["default-tls"] }
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy.git", rev = "5ed60f8" }
alloy-json-rpc = { git = "https://github.com/alloy-rs/alloy.git", rev = "5ed60f8" }
alloy-json-abi = "0.6"
alloy-sol-types = "0.6"
alloy-primitives = "0.6"
url = "2"
alloy-providers = { git = "https://github.com/alloy-rs/alloy.git" }
alloy-transport = { git = "https://github.com/alloy-rs/alloy.git" }
alloy-transport-http = { git = "https://github.com/alloy-rs/alloy.git" }
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy.git" }
alloy-json-rpc = { git = "https://github.com/alloy-rs/alloy.git" }
alloy-json-abi = "0.6.2"
alloy-sol-types = "0.6.2"
alloy-primitives = "0.6.2"

[dev-dependencies]
tokio = { version = "1.35.1", features = ["test-util", "macros"] }
12 changes: 9 additions & 3 deletions relay_rpc/src/auth/cacao.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use {
self::{header::Header, payload::Payload, signature::Signature},
self::{
header::Header,
payload::Payload,
signature::{GetProvider, Signature},
},
core::fmt::Debug,
serde::{Deserialize, Serialize},
serde_json::value::RawValue,
std::fmt::{Display, Write},
url::Url,
};

pub mod header;
Expand All @@ -29,6 +32,9 @@ pub enum CacaoError {
#[error("Unsupported signature type")]
UnsupportedSignature,

#[error("Provider not available for that chain")]
ProviderNotAvailable,

#[error("Unable to verify")]
Verification,

Expand Down Expand Up @@ -85,7 +91,7 @@ pub struct Cacao {
impl Cacao {
const ETHEREUM: &'static str = "Ethereum";

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

pub type GetProvider = fn(chain_id: String) -> Option<Url>;

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

let signature = data_encoding::HEXLOWER_PERMISSIVE
Expand All @@ -32,17 +38,23 @@ impl Signature {

match self.t.as_str() {
EIP191 => Eip191.verify(&signature, &address, hash),
EIP1271 if provider.is_some() => {
Eip1271
.verify(
signature,
Address::from_str(&address).map_err(|_| CacaoError::AddressInvalid)?,
&hash.finalize()[..]
.try_into()
.expect("hash length is 32 bytes"),
provider.expect("provider is some"),
)
.await
EIP1271 => {
let chain_id = cacao.p.chain_id_reference()?;
let provider = get_provider(chain_id);
if let Some(provider) = provider {
Eip1271
.verify(
signature,
Address::from_str(&address).map_err(|_| CacaoError::AddressInvalid)?,
&hash.finalize()[..]
.try_into()
.expect("hash length is 32 bytes"),
provider,
)
.await
} else {
Err(CacaoError::ProviderNotAvailable)
}
}
_ => Err(CacaoError::UnsupportedSignature),
}
Expand Down Expand Up @@ -145,3 +157,31 @@ impl Eip1271 {
}
}
}

#[cfg(test)]
mod test {
use {super::*, alloy_primitives::address};

// Manual test. Paste address, signature, message, and project ID to verify function
#[tokio::test]
#[ignore]
async fn test_eip1271() {
let address = address!("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
let signature = "xxx";
let signature = data_encoding::HEXLOWER_PERMISSIVE
.decode(strip_hex_prefix(signature).as_bytes())
.map_err(|_| CacaoError::Verification)
.unwrap();
let message = "xxx";
let hash = &Keccak256::new_with_prefix(eip191_bytes(message)).finalize()[..]
.try_into()
.unwrap();
let provider = "https://rpc.walletconnect.com/v1?chainId=eip155:1&projectId=xxx"
.parse()
.unwrap();
assert!(Eip1271
.verify(signature, address, hash, provider)
.await
.unwrap());
}
}
6 changes: 3 additions & 3 deletions relay_rpc/src/auth/cacao/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn cacao_verify_success() {
}
}"#;
let cacao: Cacao = serde_json::from_str(cacao_serialized).unwrap();
let result = cacao.verify(None).await;
let result = cacao.verify(|_| None).await;
assert!(result.is_ok());
assert!(result.map_err(|_| false).unwrap());

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

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

0 comments on commit 81cc254

Please sign in to comment.