Skip to content

Commit 6ff6641

Browse files
committed
feat(client): Remove Auth::None and update Error
1 parent 160a130 commit 6ff6641

3 files changed

Lines changed: 30 additions & 47 deletions

File tree

src/client.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ pub mod v28;
2525
/// Client authentication methods for the Bitcoin Core JSON-RPC server
2626
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
2727
pub enum Auth {
28-
/// No authentication (not recommended)
29-
None,
3028
/// Username and password authentication (RPC user/pass)
3129
UserPass(String, String),
3230
/// Authentication via a cookie file
@@ -42,7 +40,6 @@ impl Auth {
4240
/// Returns an error if the `CookieFile` cannot be read or invalid
4341
pub fn get_user_pass(self) -> Result<(Option<String>, Option<String>), Error> {
4442
match self {
45-
Auth::None => Ok((None, None)),
4643
Auth::UserPass(u, p) => Ok((Some(u), Some(p))),
4744
Auth::CookieFile(path) => {
4845
let line = BufReader::new(File::open(path)?)
@@ -78,21 +75,15 @@ impl Client {
7875
///
7976
/// # Errors
8077
///
81-
/// * Returns `Error::MissingAuthentication` if `Auth::None` is provided.
82-
/// * Returns `Error::InvalidResponse` if the URL is invalid.
78+
/// * Returns `Error::InvalidUrl` if the URL is invalid.
8379
/// * Returns errors related to reading the cookie file.
8480
pub fn with_auth(url: &str, auth: Auth) -> Result<Self, Error> {
85-
if matches!(auth, Auth::None) {
86-
return Err(Error::MissingAuthentication);
87-
}
88-
8981
let mut builder = Builder::new()
9082
.url(url)
91-
.map_err(|e| Error::InvalidResponse(format!("Invalid URL: {e}")))?
83+
.map_err(|e| Error::InvalidUrl(format!("{e}")))?
9284
.timeout(std::time::Duration::from_secs(60));
9385

9486
builder = match auth {
95-
Auth::None => unreachable!(),
9687
Auth::UserPass(user, pass) => builder.basic_auth(user, Some(pass)),
9788
Auth::CookieFile(path) => {
9889
let cookie = std::fs::read_to_string(path)
@@ -148,7 +139,7 @@ impl Client {
148139
/// The deserialized `Block` struct.
149140
pub fn get_block(&self, block_hash: &BlockHash) -> Result<Block, Error> {
150141
self.call::<String>("getblock", &[json!(block_hash), json!(0)])
151-
.and_then(|blockhash_hex| deserialize_hex(&blockhash_hex).map_err(Error::DecodeHex))
142+
.and_then(|block_hex| deserialize_hex(&block_hex).map_err(Error::DecodeHex))
152143
}
153144

154145
/// Retrieves the hash of the tip of the best block chain.
@@ -294,14 +285,6 @@ mod test_auth {
294285
assert_eq!(result, (Some("user".to_string()), Some("pass".to_string())));
295286
}
296287

297-
#[test]
298-
fn test_auth_none_get_user_pass() {
299-
let auth = Auth::None;
300-
let result = auth.get_user_pass().expect("failed to get user pass");
301-
302-
assert_eq!(result, (None, None));
303-
}
304-
305288
#[test]
306289
fn test_auth_cookie_file_get_user_pass() {
307290
let temp_dir = std::env::temp_dir();

src/error.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,11 @@ pub enum Error {
2727
/// Error modeling [`GetBlockFilter`](corepc_types::model::GetBlockFilter)
2828
GetBlockFilter(GetBlockFilterError),
2929

30-
/// Missing authentication credentials.
31-
MissingAuthentication,
32-
3330
/// Invalid or corrupted cookie file.
3431
InvalidCookieFile,
3532

36-
/// Invalid response from the RPC server.
37-
InvalidResponse(String),
33+
/// The provided URL is syntactically incorrect
34+
InvalidUrl(String),
3835

3936
/// JSON-RPC error from the server.
4037
JsonRpc(jsonrpc::Error),
@@ -55,20 +52,34 @@ pub enum Error {
5552
impl fmt::Display for Error {
5653
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5754
match self {
58-
Error::MissingAuthentication => {
59-
write!(f, "authentication is required but none was provided")
60-
}
61-
Error::InvalidCookieFile => write!(f, "invalid cookie file"),
62-
Error::InvalidResponse(e) => write!(f, "invalid response: {e}"),
63-
Error::HexToArray(e) => write!(f, "Hash parsing eror: {e}"),
55+
Error::DecodeHex(e) => write!(f, "hex deserialization error: {e}"),
56+
Error::GetBlockVerboseOne(e) => write!(f, "block verbose error: {e}"),
57+
Error::GetBlockHeaderVerbose(e) => write!(f, "block header verbose error: {e}"),
58+
Error::GetBlockFilter(e) => write!(f, "block filter error: {e}"),
59+
Error::InvalidCookieFile => write!(f, "invalid or missing cookie file"),
60+
Error::InvalidUrl(e) => write!(f, "invalid RPC URL: {e}"),
61+
Error::HexToArray(e) => write!(f, "hash parsing error: {e}"),
6462
Error::JsonRpc(e) => write!(f, "JSON-RPC error: {e}"),
6563
Error::Json(e) => write!(f, "JSON error: {e}"),
6664
Error::Io(e) => write!(f, "I/O error: {e}"),
67-
Error::DecodeHex(e) => write!(f, "Hex deserialization error: {e}"),
68-
Error::GetBlockHeaderVerbose(e) => write!(f, "{e}"),
69-
Error::GetBlockVerboseOne(e) => write!(f, "{e}"),
70-
Error::TryFromInt(e) => write!(f, "Integer conversion overflow error: {e}"),
71-
Error::GetBlockFilter(e) => write!(f, "{e}"),
65+
Error::TryFromInt(e) => write!(f, "integer conversion overflow: {e}"),
66+
}
67+
}
68+
}
69+
70+
impl std::error::Error for Error {
71+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
72+
match self {
73+
Error::DecodeHex(e) => Some(e),
74+
Error::JsonRpc(e) => Some(e),
75+
Error::HexToArray(e) => Some(e),
76+
Error::Json(e) => Some(e),
77+
Error::Io(e) => Some(e),
78+
Error::TryFromInt(e) => Some(e),
79+
Error::GetBlockVerboseOne(e) => Some(e),
80+
Error::GetBlockHeaderVerbose(e) => Some(e),
81+
Error::GetBlockFilter(e) => Some(e),
82+
_ => None,
7283
}
7384
}
7485
}

tests/test_rpc_client.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,6 @@ fn test_client_with_user_pass() {
7373
node.stop().expect("failed to stop node");
7474
}
7575

76-
#[test]
77-
fn test_auth_none_returns_error() {
78-
let result = Client::with_auth("http://invalid-url", Auth::None);
79-
80-
assert!(result.is_err());
81-
match result {
82-
Err(Error::MissingAuthentication) => (),
83-
_ => panic!("expected MissingAuthentication error"),
84-
}
85-
}
86-
8776
#[test]
8877
fn test_invalid_credentials() {
8978
let (_, mut node) = setup();

0 commit comments

Comments
 (0)