use std::{
fs::File,
io::{BufRead, BufReader},
path::PathBuf,
time::Duration,
};
use corepc_types::{
bitcoin::{
Block, BlockHash, Transaction, Txid,
block::Header,
consensus::encode::{deserialize_hex, serialize_hex},
},
model, v30,
};
use jsonrpc::{
Transport, bitreq_http, serde,
serde_json::{Value, json},
};
use crate::{Error, Rpc};
#[cfg(all(feature = "28_0", not(feature = "29_0")))]
pub mod v28;
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(15);
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub enum Auth {
UserPass(String, String),
CookieFile(PathBuf),
}
impl Auth {
pub fn get_user_pass(self) -> Result<(Option<String>, Option<String>), Error> {
match self {
Auth::UserPass(u, p) => Ok((Some(u), Some(p))),
Auth::CookieFile(path) => {
let line = BufReader::new(File::open(path)?)
.lines()
.next()
.ok_or(Error::InvalidCookieFile)??;
let colon = line.find(':').ok_or(Error::InvalidCookieFile)?;
Ok((Some(line[..colon].into()), Some(line[colon + 1..].into())))
}
}
}
}
pub struct Client {
inner: crate::Client,
transport: Box<dyn Transport>,
}
impl std::fmt::Debug for Client {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Client")
.field("inner", &self.inner)
.finish_non_exhaustive()
}
}
impl Client {
pub fn with_auth(url: &str, auth: Auth) -> Result<Self, Error> {
Self::with_auth_timeout(url, auth, DEFAULT_TIMEOUT)
}
pub fn with_auth_timeout(url: &str, auth: Auth, timeout: Duration) -> Result<Self, Error> {
let mut builder = bitreq_http::Builder::new()
.url(url)
.map_err(|e| Error::InvalidUrl(format!("{e}")))?
.timeout(timeout);
let (user, pass) = auth.get_user_pass()?;
if let Some(username) = user {
builder = builder.basic_auth(username, pass);
}
Ok(Self {
inner: crate::Client::new(),
transport: Box::new(builder.build()),
})
}
pub fn with_transport<T>(transport: T) -> Self
where
T: Transport + 'static,
{
Self {
inner: crate::Client::new(),
transport: Box::new(transport),
}
}
fn call<T>(&self, rpc: Rpc, params: &[Value]) -> Result<T, Error>
where
T: for<'de> serde::Deserialize<'de>,
{
let method = rpc.to_string();
self.inner
.call(&method, params, |req| self.transport.send_request(req))
}
}
impl Client {
pub fn get_block(&self, block_hash: &BlockHash) -> Result<Block, Error> {
self.call::<String>(Rpc::GetBlock, &[json!(block_hash), json!(0)])
.and_then(|block_hex| deserialize_hex(&block_hex).map_err(Error::DecodeHex))
}
pub fn get_best_block_hash(&self) -> Result<BlockHash, Error> {
self.call::<String>(Rpc::GetBestBlockHash, &[])
.and_then(|blockhash_hex| blockhash_hex.parse().map_err(Error::HexToArray))
}
pub fn get_block_count(&self) -> Result<u32, Error> {
self.call::<v30::GetBlockCount>(Rpc::GetBlockCount, &[])?
.0
.try_into()
.map_err(Error::TryFromInt)
}
pub fn get_block_hash(&self, height: u32) -> Result<BlockHash, Error> {
self.call::<String>(Rpc::GetBlockHash, &[json!(height)])
.and_then(|blockhash_hex| blockhash_hex.parse().map_err(Error::HexToArray))
}
pub fn get_block_filter(&self, block_hash: &BlockHash) -> Result<model::GetBlockFilter, Error> {
let block_filter: v30::GetBlockFilter =
self.call(Rpc::GetBlockFilter, &[json!(block_hash)])?;
block_filter.into_model().map_err(Error::model)
}
pub fn get_block_header(&self, block_hash: &BlockHash) -> Result<Header, Error> {
self.call::<String>(Rpc::GetBlockHeader, &[json!(block_hash), json!(false)])
.and_then(|header_hex: String| deserialize_hex(&header_hex).map_err(Error::DecodeHex))
}
pub fn get_raw_mempool(&self) -> Result<Vec<Txid>, Error> {
self.call::<model::GetRawMempool>(Rpc::GetRawMempool, &[])
.map(|txids| txids.0)
}
pub fn get_raw_transaction(&self, txid: &Txid) -> Result<Transaction, Error> {
self.call::<String>(Rpc::GetRawTransaction, &[json!(txid)])
.and_then(|tx_hex| deserialize_hex(&tx_hex).map_err(Error::DecodeHex))
}
pub fn send_raw_transaction(&self, tx: &Transaction) -> Result<Txid, Error> {
let hex_tx = serialize_hex(tx);
let txid: Txid = self.call(Rpc::SendRawTransaction, &[json!(hex_tx)])?;
Ok(txid)
}
}
#[cfg(feature = "29_0")]
use corepc_types::model::{GetBlockHeaderVerbose, GetBlockVerboseOne};
#[cfg(feature = "29_0")]
impl Client {
pub fn get_block_header_verbose(
&self,
block_hash: &BlockHash,
) -> Result<GetBlockHeaderVerbose, Error> {
let header_info: v30::GetBlockHeaderVerbose =
self.call(Rpc::GetBlockHeader, &[json!(block_hash)])?;
header_info.into_model().map_err(Error::model)
}
pub fn get_block_verbose(&self, block_hash: &BlockHash) -> Result<GetBlockVerboseOne, Error> {
let block_info: v30::GetBlockVerboseOne =
self.call(Rpc::GetBlock, &[json!(block_hash), json!(1)])?;
block_info.into_model().map_err(Error::model)
}
pub fn get_blockchain_info(&self) -> Result<model::GetBlockchainInfo, Error> {
let info: v30::GetBlockchainInfo = self.call(Rpc::GetBlockchainInfo, &[])?;
info.into_model().map_err(Error::model)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_auth_user_pass_get_user_pass() {
let auth = Auth::UserPass("user".to_string(), "pass".to_string());
let result = auth.get_user_pass().expect("failed to get user pass");
assert_eq!(result, (Some("user".to_string()), Some("pass".to_string())));
}
#[test]
#[ignore = "modifies the local filesystem"]
fn test_auth_cookie_file_get_user_pass() {
let temp_dir = std::env::temp_dir();
let cookie_path = temp_dir.join("test_auth_cookie");
std::fs::write(&cookie_path, "testuser:testpass").expect("failed to write cookie");
let auth = Auth::CookieFile(cookie_path.clone());
let result = auth.get_user_pass().expect("failed to get user pass");
assert_eq!(
result,
(Some("testuser".to_string()), Some("testpass".to_string()))
);
std::fs::remove_file(cookie_path).ok();
}
#[test]
fn test_auth_invalid_cookie_file() {
let cookie_path = PathBuf::from("/nonexistent/path/to/cookie");
let auth = Auth::CookieFile(cookie_path);
let result = auth.get_user_pass();
assert!(matches!(result, Err(Error::Io(_))));
}
}