Skip to content

Commit

Permalink
Remove leftovers, rearrange code and fix wasm support
Browse files Browse the repository at this point in the history
  • Loading branch information
Antony1060 committed Nov 22, 2023
1 parent e377429 commit 4393e75
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 48 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ ethers-core = "2.0.11"
ethers-providers = "2.0.11"
futures-util = "0.3.29"

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }

[dev-dependencies]
tokio = { version = "1.34.0", features = ["macros", "rt-multi-thread"] }
ethers = "2.0.11"
Expand Down
89 changes: 41 additions & 48 deletions src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use reqwest::Url;
use serde_json::Value;

use crate::ccip::handle_ccip;
use crate::utils::{decode_bytes, dns_encode};
use crate::utils::{build_reqwest, decode_bytes, dns_encode};
use crate::CCIPReadMiddlewareError;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -67,11 +67,7 @@ impl<M: Middleware> CCIPReadMiddlewareBuilder<M> {
Ok(CCIPReadMiddleware {
provider: self.provider.ok_or("provider is required".to_string())?,
ens: self.ens.unwrap_or(ens::ENS_ADDRESS),
reqwest_client: reqwest::Client::builder()
// TODO: discuss an appropriate default timeout, 4 seconds is a very random decision
.timeout(self.timeout.unwrap_or(Duration::from_secs(4)))
.build()
.expect("should be a valid reqwest client"),
reqwest_client: build_reqwest(self.timeout.unwrap_or(Duration::from_secs(10))),
max_redirect_attempt: self.max_redirect_attempt.unwrap_or(10),
})
}
Expand Down Expand Up @@ -226,7 +222,8 @@ impl<M: Middleware> CCIPReadMiddleware<M> {
Ok(H160::zero())
}

#[async_recursion]
#[cfg_attr(target_arch = "wasm32", async_recursion(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_recursion)]
async fn _call(
&self,
transaction: &TypedTransaction,
Expand Down Expand Up @@ -350,32 +347,24 @@ where
&self.provider
}

/// Call the underlying middleware with the provided transaction and block
async fn call(
&self,
tx: &TypedTransaction,
block: Option<BlockId>,
) -> Result<Bytes, Self::Error> {
return self._call(tx, block, 0).await;
/// Resolve an ENS name to an address
async fn resolve_name(&self, ens_name: &str) -> Result<Address, Self::Error> {
self.query_resolver(ParamType::Address, ens_name, ens::ADDR_SELECTOR)
.await
}

/**
The following couple of methods were copied from ethers-rs, and modified to work with ENSIP-10
**/

/// Resolve a field of an ENS name
async fn resolve_field(&self, ens_name: &str, field: &str) -> Result<String, Self::Error> {
println!("calling resolve field");

let field: String = self
.query_resolver_parameters(
ParamType::String,
ens_name,
ens::FIELD_SELECTOR,
Some(&ens::parameterhash(field)),
)
/// Look up an address to find its primary ENS name
async fn lookup_address(&self, address: Address) -> Result<String, Self::Error> {
let ens_name = ens::reverse_address(address);
let domain: String = self
.query_resolver(ParamType::String, &ens_name, ens::NAME_SELECTOR)
.await?;
Ok(field)
let reverse_address = self.resolve_name(&domain).await?;
if address != reverse_address {
Err(CCIPReadMiddlewareError::EnsNotOwned(domain))
} else {
Ok(domain)
}
}

/// Resolve avatar field of an ENS name
Expand Down Expand Up @@ -444,24 +433,30 @@ where
}
}

/// Resolve an ENS name to an address
async fn resolve_name(&self, ens_name: &str) -> Result<Address, Self::Error> {
self.query_resolver(ParamType::Address, ens_name, ens::ADDR_SELECTOR)
.await
}
/**
The following couple of methods were copied from ethers-rs, and modified to work with ENSIP-10
**/

/// Look up an address to find its primary ENS name
async fn lookup_address(&self, address: Address) -> Result<String, Self::Error> {
let ens_name = ens::reverse_address(address);
let domain: String = self
.query_resolver(ParamType::String, &ens_name, ens::NAME_SELECTOR)
/// Resolve a field of an ENS name
async fn resolve_field(&self, ens_name: &str, field: &str) -> Result<String, Self::Error> {
let field: String = self
.query_resolver_parameters(
ParamType::String,
ens_name,
ens::FIELD_SELECTOR,
Some(&ens::parameterhash(field)),
)
.await?;
let reverse_address = self.resolve_name(&domain).await?;
if address != reverse_address {
Err(CCIPReadMiddlewareError::EnsNotOwned(domain))
} else {
Ok(domain)
}
Ok(field)
}

/// Call the underlying middleware with the provided transaction and block
async fn call(
&self,
tx: &TypedTransaction,
block: Option<BlockId>,
) -> Result<Bytes, Self::Error> {
return self._call(tx, block, 0).await;
}
}

Expand Down Expand Up @@ -524,8 +519,6 @@ mod tests {
assert_eq!(record, email);
}

// todo: requires modification in ethers-rs, uncomment when merged
//
#[tokio::test]
async fn test_mismatched_sender() {
let resolver_address = "0xC1735677a60884ABbCF72295E88d47764BeDa282";
Expand Down
15 changes: 15 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ethers_core::abi;
use ethers_core::abi::{Detokenize, ParamType};
use std::time::Duration;

pub(crate) fn truncate_str(src: &str, side: usize) -> String {
if src.len() < side * 2 + 3 {
Expand All @@ -14,6 +15,20 @@ pub(crate) fn decode_bytes<T: Detokenize>(param: ParamType, bytes: &[u8]) -> Res
T::from_tokens(tokens).map_err(|err| abi::Error::Other(err.to_string().into()))
}

#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn build_reqwest(timeout: Duration) -> reqwest::Client {
reqwest::Client::builder()
.timeout(timeout)
.build()
.expect("should be a valid reqwest client")
}

#[cfg(target_arch = "wasm32")]
pub(crate) fn build_reqwest(_timeout: Duration) -> reqwest::Client {
// reqwest doesn't support timeouts on wasm
reqwest::Client::new()
}

/// Encodes a domain name into its binary representation according to the DNS
/// wire format. Each label (i.e., substring separated by dots) in the domain
/// is prefixed with its length, and the encoded domain name is terminated
Expand Down

0 comments on commit 4393e75

Please sign in to comment.