From 3d96644a0f7e5823b8b9bd712640ddfbb253061d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zoe=20Faltib=C3=A0?= Date: Wed, 27 Jul 2022 14:51:08 +0200 Subject: [PATCH] Add raw_call to ElectrumApi --- src/api.rs | 3 +++ src/batch.rs | 4 ++-- src/client.rs | 5 +++++ src/raw_client.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/types.rs | 2 ++ 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/api.rs b/src/api.rs index 87c2140..1781847 100644 --- a/src/api.rs +++ b/src/api.rs @@ -65,6 +65,9 @@ pub trait ElectrumApi { self.transaction_broadcast_raw(&buffer) } + /// Executes the requested API call returning the raw answer. + fn raw_call(&self, call: &Call) -> Result; + /// Execute a queue of calls stored in a [`Batch`](../batch/struct.Batch.html) struct. Returns /// `Ok()` **only if** all of the calls are successful. The order of the JSON `Value`s returned /// reflects the order in which the calls were made on the `Batch` struct. diff --git a/src/batch.rs b/src/batch.rs index aa24c0d..5d47bcd 100644 --- a/src/batch.rs +++ b/src/batch.rs @@ -5,7 +5,7 @@ use bitcoin::hashes::hex::ToHex; use bitcoin::{Script, Txid}; -use types::{Param, ToElectrumScriptHash}; +use types::{Call, Param, ToElectrumScriptHash}; /// Helper structure that caches all the requests before they are actually sent to the server. /// @@ -18,7 +18,7 @@ use types::{Param, ToElectrumScriptHash}; /// [`batch_script_get_balance`](../client/struct.Client.html#method.batch_script_get_balance) to ask the /// server for the balance of multiple scripts with a single request. pub struct Batch { - calls: Vec<(String, Vec)>, + calls: Vec, } impl Batch { diff --git a/src/client.rs b/src/client.rs index d7bc127..2113711 100644 --- a/src/client.rs +++ b/src/client.rs @@ -160,6 +160,11 @@ impl Client { } impl ElectrumApi for Client { + #[inline] + fn raw_call(&self, call: &Call) -> Result { + impl_inner_call!(self, raw_call, call) + } + #[inline] fn batch_call(&self, batch: &Batch) -> Result, Error> { impl_inner_call!(self, batch_call, batch) diff --git a/src/raw_client.rs b/src/raw_client.rs index 7e54845..b2f65fd 100644 --- a/src/raw_client.rs +++ b/src/raw_client.rs @@ -666,6 +666,17 @@ impl RawClient { } impl ElectrumApi for RawClient { + fn raw_call(&self, call: &Call) -> Result { + let req = Request::new_id( + self.last_id.fetch_add(1, Ordering::SeqCst), + &call.0, + call.1.to_vec(), + ); + let result = self.call(req)?; + + Ok(result) + } + fn batch_call(&self, batch: &Batch) -> Result, Error> { let mut raw = Vec::new(); @@ -1283,4 +1294,35 @@ mod test { assert!(client.transaction_broadcast_raw(&[0x00]).is_err()); assert!(client.server_features().is_ok()); } + + #[test] + fn test_raw_call() { + use types::Param; + + let client = RawClient::new(get_test_server(), None).unwrap(); + + let params = vec![ + Param::String( + "cc2ca076fd04c2aeed6d02151c447ced3d09be6fb4d4ef36cb5ed4e7a3260566".to_string(), + ), + Param::Bool(false), + ]; + let call = ("blockchain.transaction.get".to_string(), params); + + let resp = client.raw_call(&call).unwrap(); + + assert_eq!( + resp, + "01000000000101000000000000000000000000000000000000000000000000000\ + 0000000000000ffffffff5403f09c091b4d696e656420627920416e74506f6f6c3\ + 13139ae006f20074d6528fabe6d6d2ab1948d50b3d991e2a0821df74358ed9c255\ + 3af00c7a61f97771ca0acee106e0400000000000000cbec00802461f905fffffff\ + f0354ceac2a000000001976a91411dbe48cc6b617f9c6adaf4d9ed5f625b1c7cb5\ + 988ac0000000000000000266a24aa21a9ed2e578bce2ca6c6bc9359377345d8e98\ + 5dd5f90c78421ffa6efa5eb60428e698c0000000000000000266a24b9e11b6d2f6\ + 21d7ec3f45a5eca89d3ea6a294cdf3a042e973009584470a12916111e2caa01200\ + 000000000000000000000000000000000000000000000000000000000000000000\ + 00000" + ) + } } diff --git a/src/types.rs b/src/types.rs index 906b0e0..ff6652d 100644 --- a/src/types.rs +++ b/src/types.rs @@ -17,6 +17,8 @@ use serde::{de, Deserialize, Serialize}; static JSONRPC_2_0: &str = "2.0"; +pub type Call = (String, Vec); + #[derive(Serialize, Clone)] #[serde(untagged)] /// A single parameter of a [`Request`](struct.Request.html)