Skip to content

Commit

Permalink
refactor: improve debug of RpcApi (#307)
Browse files Browse the repository at this point in the history
Improve debug representation of `RpcApi` to display the host name if
available.
  • Loading branch information
gregorydemay authored Oct 14, 2024
1 parent f353ed3 commit 7a4695e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ serde = { workspace = true }
serde_bytes = { workspace = true }
serde_json = { workspace = true }
thousands = "0.2"
url = "2.5"
url = { workspace = true }
hex = "0.4"
ethers-core = "2.0"
zeroize = { version = "1.8", features = ["zeroize_derive"] }
Expand Down Expand Up @@ -81,6 +81,7 @@ serde_json = "1.0"
serde_bytes = "0.11.15"
strum = { version = "0.26", features = ["derive"] }
thiserror = "1.0.64"
url = "2.5"

[workspace]
members = ["e2e/rust", "evm_rpc_types"]
3 changes: 3 additions & 0 deletions evm_rpc_types/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.1.0] - 2024-10-14

### Changed

- v1.1.0 Improve Debug and Display implementations for `HexByte`, `Hex20`, `Hex32`, `Hex256`, `Hex` and `Nat256`.
- v1.1.0 Improve Debug implementation of `RpcApi`.

## [1.0.0] - 2024-10-07

Expand Down
1 change: 1 addition & 0 deletions evm_rpc_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ num-bigint = { workspace = true }
serde = { workspace = true }
strum = { workspace = true }
thiserror = { workspace = true }
url = { workspace = true }

[dev-dependencies]
proptest = { workspace = true }
14 changes: 13 additions & 1 deletion evm_rpc_types/src/rpc_client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(test)]
mod tests;

pub use ic_cdk::api::management_canister::http_request::HttpHeader;
use std::fmt::Debug;

Expand Down Expand Up @@ -53,9 +56,18 @@ pub struct RpcApi {
pub headers: Option<Vec<HttpHeader>>,
}

impl RpcApi {
pub fn host_str(&self) -> Option<String> {
url::Url::parse(&self.url)
.ok()
.and_then(|u| u.host_str().map(|host| host.to_string()))
}
}

impl Debug for RpcApi {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "RpcApi {{ url: ***, headers: *** }}",) //URL or header value could contain API keys
let host = self.host_str().unwrap_or("N/A".to_string());
write!(f, "RpcApi {{ host: {}, url/headers: *** }}", host) //URL or header value could contain API keys
}
}

Expand Down
29 changes: 29 additions & 0 deletions evm_rpc_types/src/rpc_client/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::RpcApi;
use ic_cdk::api::management_canister::http_request::HttpHeader;

#[test]
fn should_contain_host_without_sensitive_information() {
for provider in [
RpcApi {
url: "https://eth-mainnet.g.alchemy.com/v2".to_string(),
headers: None,
},
RpcApi {
url: "https://eth-mainnet.g.alchemy.com/v2/key".to_string(),
headers: None,
},
RpcApi {
url: "https://eth-mainnet.g.alchemy.com/v2".to_string(),
headers: Some(vec![HttpHeader {
name: "authorization".to_string(),
value: "Bearer key".to_string(),
}]),
},
] {
let debug = format!("{:?}", provider);
assert_eq!(
debug,
"RpcApi { host: eth-mainnet.g.alchemy.com, url/headers: *** }"
);
}
}

0 comments on commit 7a4695e

Please sign in to comment.