diff --git a/CHANGELOG.md b/CHANGELOG.md index 065ed3d9..c237286d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 1.0.1 +- only bitcoinsv-rpc - added new() method for Client which accepts a URI with username and password included + # 1.0.0 - cargo update - add timeout parameter for Client diff --git a/Cargo.lock b/Cargo.lock index 34e6a261..287ce9af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -93,7 +93,7 @@ dependencies = [ [[package]] name = "bitcoinsv-rpc" -version = "1.0.0" +version = "1.0.1" dependencies = [ "async-trait", "bitcoinsv", @@ -105,6 +105,7 @@ dependencies = [ "serde_json", "tempfile", "tokio", + "url", ] [[package]] @@ -177,6 +178,15 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + [[package]] name = "futures" version = "0.3.30" @@ -301,6 +311,16 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" +[[package]] +name = "idna" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "integration_test" version = "0.1.0" @@ -465,6 +485,12 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + [[package]] name = "pin-project-lite" version = "0.2.14" @@ -718,6 +744,21 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "tinyvec" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "tokio" version = "1.38.0" @@ -759,18 +800,44 @@ dependencies = [ "tokio", ] +[[package]] +name = "unicode-bidi" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" + [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-normalization" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5" +dependencies = [ + "tinyvec", +] + [[package]] name = "untrusted" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "url" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + [[package]] name = "uuid" version = "1.9.1" diff --git a/client/Cargo.toml b/client/Cargo.toml index 8d8f927b..04ae3248 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bitcoinsv-rpc" -version = "1.0.0" +version = "1.0.1" authors = [ "Daniel Connolly " ] @@ -26,6 +26,7 @@ serde_json = "1" hex = "0.4.3" bitcoinsv = "0.2.5" async-trait = "0.1.75" +url = "2.5.0" [dev-dependencies] tempfile = "3.3.0" diff --git a/client/src/client.rs b/client/src/client.rs index a62689aa..38f61278 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -4,6 +4,7 @@ use std::io::{BufRead, BufReader, Cursor}; use std::iter::FromIterator; use std::path::PathBuf; use std::{fmt, result}; +use url::Url; use async_trait::async_trait; use hex::{FromHex, ToHex}; use jsonrpc; @@ -538,6 +539,24 @@ impl Client { }; Ok(Client {client: jsonrpc::client::Client::with_transport(b.build())}) } + + pub fn new_from_uri(uri: &str, timeout: Option) -> Result { + let url; + let username; + let password; + match Url::parse(&*uri) { + Err(_e) => { + println!("could not parse RPC URI"); + return Err(Error::InvalidUri); + } + Ok(result) => { + url = format!("{}://{}:{}/", result.scheme(), result.host_str().unwrap(), result.port().unwrap()); + username = String::from(result.username()); + password = String::from(result.password().unwrap()); + } + } + Client::new(&url, Auth::UserPass(username, password), timeout) + } } impl RpcApi for Client { diff --git a/client/src/error.rs b/client/src/error.rs index 3e8f08e7..e4c98b3a 100644 --- a/client/src/error.rs +++ b/client/src/error.rs @@ -15,6 +15,8 @@ pub enum Error { UnexpectedStructure, /// The daemon returned an error string. ReturnedError(String), + /// The URI could not be parsed. + InvalidUri, MinReqError(jsonrpc::minreq_http::Error), SVJsonError(bitcoinsv::Error), } @@ -43,12 +45,6 @@ impl From for Error { } } -// impl From for Error { -// fn from(e: bitcoinsv::Error) -> Error { -// Error::BitcoinSVError(e) -// } -// } -// impl From for Error { fn from(e: jsonrpc::minreq_http::Error) -> Error { Error::MinReqError(e) @@ -71,7 +67,7 @@ impl fmt::Display for Error { Error::InvalidCookieFile => write!(f, "invalid cookie file"), Error::UnexpectedStructure => write!(f, "the JSON result had an unexpected structure"), Error::ReturnedError(ref s) => write!(f, "the daemon returned an error string: {}", s), - // Error::BitcoinSVError(ref e) => write!(f, "BSV error: {}", e), + Error::InvalidUri => write!(f, "the URI could not be parsed"), Error::MinReqError(ref e) => write!(f, "HTTPMinReq: {}", e), Error::SVJsonError(ref e) => write!(f, "SVJson: {}", e), }