Skip to content

Commit

Permalink
Added a feature flag "rpc_authentication" so that diqwest dependency …
Browse files Browse the repository at this point in the history
…is only compiled when the feature is needed
  • Loading branch information
refring committed Nov 13, 2022
1 parent c7308a7 commit df26564
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ description = "RPC client for Monero daemon and wallet"
[dependencies]
anyhow = "1"
chrono = { version = "0.4", features = ["serde"] }
diqwest = "1.1"
fixed-hash = "0.7"
diqwest = { version = "1.1", optional = true }
fixed-hash = "0.8"
hex = "0.4"
http = "0.2"
jsonrpc-core = "18"
Expand All @@ -31,3 +31,6 @@ rand = "0.8.4"
rustc-hex = "2.1"
serde_test = "1.0"
tokio = { version = "1.12.0", features = ["full"] }

[features]
rpc_authentication = ["dep:diqwest"]
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ use std::{
use tracing::*;
use uuid::Uuid;

#[cfg(feature = "rpc_authentication")]
use crate::RpcAuthentication::Credentials;
#[cfg(feature = "rpc_authentication")]
use diqwest::WithDigestAuth;

enum RpcParams {
Expand Down Expand Up @@ -106,6 +108,7 @@ impl From<RpcParams> for Params {
struct RemoteCaller {
http_client: reqwest::Client,
addr: String,
#[allow(dead_code)]
rpc_auth: RpcAuthentication,
}

Expand All @@ -129,6 +132,10 @@ impl RemoteCaller {

let req = client.post(&uri).json(&method_call);

#[cfg(not(feature = "rpc_authentication"))]
let rsp = req.send().await?.json::<response::Output>().await?;

#[cfg(feature = "rpc_authentication")]
let rsp = if let Credentials { username, password } = &self.rpc_auth {
req.send_with_digest_auth(username, password)
.await?
Expand Down Expand Up @@ -160,6 +167,10 @@ impl RemoteCaller {

let req = client.post(uri).json(&json_params);

#[cfg(not(feature = "rpc_authentication"))]
let rsp = req.send().await?.json::<T>().await?;

#[cfg(feature = "rpc_authentication")]
let rsp = if let Credentials { username, password } = &self.rpc_auth {
req.send_with_digest_auth(username, password)
.await?
Expand Down
8 changes: 4 additions & 4 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.7"
services:
monerod:
image: ghcr.io/farcaster-project/containers/monerod:${MONERO_VERSION:-0.18.1.2}
image: ghcr.io/farcaster-project/containers/monerod:${MONERO_VERSION:-0.18.0.0}
environment:
NETWORK: regtest
MONEROD_RPC_PORT: 18081
Expand All @@ -12,7 +12,7 @@ services:
- 18081:18081

monero-wallet-rpc:
image: ghcr.io/farcaster-project/containers/monero-wallet-rpc:${MONERO_VERSION:-0.18.1.2}
image: ghcr.io/farcaster-project/containers/monero-wallet-rpc:${MONERO_VERSION:-0.18.0.0}
environment:
MONERO_DAEMON_ADDRESS: monerod:18081
MONERO_DAEMON_HOST: monerod:18081
Expand All @@ -23,7 +23,7 @@ services:
- 18083:18083

monerod-rpc-authentication:
image: ghcr.io/farcaster-project/containers/monerod:${MONERO_VERSION:-0.18.1.2}
image: ghcr.io/farcaster-project/containers/monerod:${MONERO_VERSION:-0.18.0.0}
depends_on:
- "monerod"
ports:
Expand All @@ -39,7 +39,7 @@ services:
--fixed-difficulty 1
monero-wallet-rpc-authentication:
image: ghcr.io/farcaster-project/containers/monero-wallet-rpc:${MONERO_VERSION:-0.18.1.2}
image: ghcr.io/farcaster-project/containers/monero-wallet-rpc:${MONERO_VERSION:-0.18.0.0}
depends_on:
- "monerod"
ports:
Expand Down
6 changes: 6 additions & 0 deletions tests/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async fn main_functional_test() {
clients_tests::all_clients_interaction::run().await;
}

#[cfg(feature = "rpc_authentication")]
fn setup_rpc_auth_client(username: &str, password: &str, port: u32) -> RpcClient {
let whost = env::var("MONERO_WALLET_HOST_1").unwrap_or_else(|_| "localhost".into());
let rpc_credentials = RpcAuthentication::Credentials {
Expand All @@ -76,6 +77,7 @@ fn setup_rpc_auth_client(username: &str, password: &str, port: u32) -> RpcClient
}

#[tokio::test]
#[cfg(feature = "rpc_authentication")]
async fn test_daemon_rpc_auth() {
let rpc_client = setup_rpc_auth_client("foo", "bar", 18085).daemon();
let daemon_transactions = rpc_client.get_block_count().await;
Expand All @@ -84,6 +86,7 @@ async fn test_daemon_rpc_auth() {
}

#[tokio::test]
#[cfg(feature = "rpc_authentication")]
async fn test_daemon_rpc_auth_fail() {
let rpc_client = setup_rpc_auth_client("invalid", "bar", 18085).daemon();
let daemon_transactions = rpc_client.get_block_count().await;
Expand All @@ -92,6 +95,7 @@ async fn test_daemon_rpc_auth_fail() {
}

#[tokio::test]
#[cfg(feature = "rpc_authentication")]
async fn test_daemon_rpc_rpc_auth() {
let rpc_client = setup_rpc_auth_client("foo", "bar", 18085).daemon_rpc();
let transactions = vec![Hash::from_low_u64_be(1)];
Expand All @@ -103,6 +107,7 @@ async fn test_daemon_rpc_rpc_auth() {
}

#[tokio::test]
#[cfg(feature = "rpc_authentication")]
async fn test_rpc_auth() {
let rpc_client = setup_rpc_auth_client("foo", "bar", 18084).wallet();
assert!(rpc_client.get_version().await.is_ok());
Expand All @@ -112,6 +117,7 @@ async fn test_rpc_auth() {
}

#[tokio::test]
#[cfg(feature = "rpc_authentication")]
async fn test_rpc_auth_fail() {
let rpc_client = setup_rpc_auth_client("invalid", "auth", 18084).wallet();

Expand Down

0 comments on commit df26564

Please sign in to comment.