Skip to content

Commit

Permalink
fix: get url from rpc and add openssl
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Aug 4, 2023
1 parent 2da6148 commit e7be58a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ tracing-appender = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
cargo_metadata = "0.15.4"
pathdiff = "0.2.1"
# For hyper-tls
openssl = { version = "0.10.55", features = ["vendored"] }

[build-dependencies]
crate-git-revision = "0.0.4"
Expand Down
39 changes: 8 additions & 31 deletions cmd/soroban-cli/src/commands/config/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use serde_json::Value;
use stellar_strkey::ed25519::PublicKey;

use crate::{commands::HEADING_RPC, rpc};
use crate::{commands::HEADING_RPC, rpc::{self, Client}};

use super::locator;

Expand Down Expand Up @@ -81,14 +81,6 @@ pub struct Args {
help_heading = HEADING_RPC,
)]
pub network_passphrase: Option<String>,
/// Helper URL to use for funding accounts on test networks
#[arg(
long = "helper-url",
requires = "rpc_url",
env = "SOROBAN_NETWORK_PASSPHRASE",
help_heading = HEADING_RPC,
)]
pub helper_url: Option<String>,
/// Name of network to use from config
#[arg(
long,
Expand All @@ -110,7 +102,6 @@ impl Args {
Ok(Network {
rpc_url,
network_passphrase,
helper_url: self.helper_url.clone(),
})
} else {
Err(Error::Network)
Expand Down Expand Up @@ -139,34 +130,21 @@ pub struct Network {
help_heading = HEADING_RPC,
)]
pub network_passphrase: String,

/// Network passphrase to sign the transaction sent to the rpc server
#[arg(
long,
env = "SOROBAN_HELPER_URL",
help_heading = HEADING_RPC,
)]
pub helper_url: Option<String>,
}

impl Network {
pub fn helper_url(&self, addr: &str) -> Result<http::Uri, Error> {
pub async fn helper_url(&self, addr: &str) -> Result<http::Uri, Error> {
tracing::debug!("address {addr:?}");
let (url_root, path) = if let Some(helper_url) = &self.helper_url {
(helper_url, "")
} else {
(&self.rpc_url, "/friendbot")
};
tracing::debug!("helper url {url_root:?}\npath: {path:?}");
let uri =
http::Uri::from_str(url_root).map_err(|_| Error::InvalidUrl(url_root.to_string()))?;
let client = Client::new(&self.rpc_url)?;
let helper_url_root = client.friendbot_url().await?;
let uri = http::Uri::from_str(&helper_url_root).map_err(|_|Error::InvalidUrl(helper_url_root.to_string()))?;
http::Uri::from_str(&format!("{uri:?}?addr={addr}"))
.map_err(|_| Error::InvalidUrl(url_root.to_string()))
.map_err(|_| Error::InvalidUrl(helper_url_root.to_string()))
}

#[allow(clippy::similar_names)]
pub async fn fund_address(&self, addr: &PublicKey) -> Result<(), Error> {
let uri = self.helper_url(&addr.to_string())?;
let uri = self.helper_url(&addr.to_string()).await?;
tracing::debug!("URL {uri:?}");
let response = match uri.scheme_str() {
Some("http") => hyper::Client::new().get(uri.clone()).await?,
Expand Down Expand Up @@ -200,8 +178,7 @@ impl Network {
pub fn futurenet() -> Self {
Network {
rpc_url: "https://rpc-futurenet.stellar.org:443".to_owned(),
network_passphrase: "Test SDF Future Network ; October 2022".to_owned(),
helper_url: Some("https://friendbot-futurenet.stellar.org".to_owned()),
network_passphrase: "Test SDF Future Network ; October 2022".to_owned()
}
}
}
11 changes: 11 additions & 0 deletions cmd/soroban-cli/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,17 @@ impl Client {
.build(url)?)
}

pub async fn friendbot_url(&self) -> Result<String, Error> {
let network = self.get_network().await?;
tracing::trace!("{network:#?}");
network.friendbot_url.ok_or_else(|| {
Error::NotFound(
"Friendbot".to_string(),
"Friendbot is not available on this network".to_string(),
)
})
}

pub async fn verify_network_passphrase(&self, expected: Option<&str>) -> Result<String, Error> {
let server = self.get_network().await?.passphrase;
if expected.is_some() && expected != Some(&server) {
Expand Down

0 comments on commit e7be58a

Please sign in to comment.