Skip to content

Commit e1c20b8

Browse files
committed
Merge #74: Add raw_call to ElectrumApi
3d96644 Add raw_call to ElectrumApi (Zoe Faltibà) Pull request description: In order to give more flexibility to the users of this library I added a `raw_call` method to the `ElectrumApi` trait that will allow to call any arbitrary electrum API call. ACKs for top commit: afilini: ACK 3d96644 Tree-SHA512: b88ece7bd9ff8121272c3b3528430e9ccb8778661b15cce276c6f67a8797a4dbf4c52a8521191942c21e8b67ebde8187fd75ac49189440b0fbb71274974b4b41
2 parents ca48628 + 3d96644 commit e1c20b8

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

src/api.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ pub trait ElectrumApi {
6565
self.transaction_broadcast_raw(&buffer)
6666
}
6767

68+
/// Executes the requested API call returning the raw answer.
69+
fn raw_call(&self, call: &Call) -> Result<serde_json::Value, Error>;
70+
6871
/// Execute a queue of calls stored in a [`Batch`](../batch/struct.Batch.html) struct. Returns
6972
/// `Ok()` **only if** all of the calls are successful. The order of the JSON `Value`s returned
7073
/// reflects the order in which the calls were made on the `Batch` struct.

src/batch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use bitcoin::hashes::hex::ToHex;
66
use bitcoin::{Script, Txid};
77

8-
use types::{Param, ToElectrumScriptHash};
8+
use types::{Call, Param, ToElectrumScriptHash};
99

1010
/// Helper structure that caches all the requests before they are actually sent to the server.
1111
///
@@ -18,7 +18,7 @@ use types::{Param, ToElectrumScriptHash};
1818
/// [`batch_script_get_balance`](../client/struct.Client.html#method.batch_script_get_balance) to ask the
1919
/// server for the balance of multiple scripts with a single request.
2020
pub struct Batch {
21-
calls: Vec<(String, Vec<Param>)>,
21+
calls: Vec<Call>,
2222
}
2323

2424
impl Batch {

src/client.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ impl Client {
160160
}
161161

162162
impl ElectrumApi for Client {
163+
#[inline]
164+
fn raw_call(&self, call: &Call) -> Result<serde_json::Value, Error> {
165+
impl_inner_call!(self, raw_call, call)
166+
}
167+
163168
#[inline]
164169
fn batch_call(&self, batch: &Batch) -> Result<Vec<serde_json::Value>, Error> {
165170
impl_inner_call!(self, batch_call, batch)

src/raw_client.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,17 @@ impl<S: Read + Write> RawClient<S> {
666666
}
667667

668668
impl<T: Read + Write> ElectrumApi for RawClient<T> {
669+
fn raw_call(&self, call: &Call) -> Result<serde_json::Value, Error> {
670+
let req = Request::new_id(
671+
self.last_id.fetch_add(1, Ordering::SeqCst),
672+
&call.0,
673+
call.1.to_vec(),
674+
);
675+
let result = self.call(req)?;
676+
677+
Ok(result)
678+
}
679+
669680
fn batch_call(&self, batch: &Batch) -> Result<Vec<serde_json::Value>, Error> {
670681
let mut raw = Vec::new();
671682

@@ -1283,4 +1294,35 @@ mod test {
12831294
assert!(client.transaction_broadcast_raw(&[0x00]).is_err());
12841295
assert!(client.server_features().is_ok());
12851296
}
1297+
1298+
#[test]
1299+
fn test_raw_call() {
1300+
use types::Param;
1301+
1302+
let client = RawClient::new(get_test_server(), None).unwrap();
1303+
1304+
let params = vec![
1305+
Param::String(
1306+
"cc2ca076fd04c2aeed6d02151c447ced3d09be6fb4d4ef36cb5ed4e7a3260566".to_string(),
1307+
),
1308+
Param::Bool(false),
1309+
];
1310+
let call = ("blockchain.transaction.get".to_string(), params);
1311+
1312+
let resp = client.raw_call(&call).unwrap();
1313+
1314+
assert_eq!(
1315+
resp,
1316+
"01000000000101000000000000000000000000000000000000000000000000000\
1317+
0000000000000ffffffff5403f09c091b4d696e656420627920416e74506f6f6c3\
1318+
13139ae006f20074d6528fabe6d6d2ab1948d50b3d991e2a0821df74358ed9c255\
1319+
3af00c7a61f97771ca0acee106e0400000000000000cbec00802461f905fffffff\
1320+
f0354ceac2a000000001976a91411dbe48cc6b617f9c6adaf4d9ed5f625b1c7cb5\
1321+
988ac0000000000000000266a24aa21a9ed2e578bce2ca6c6bc9359377345d8e98\
1322+
5dd5f90c78421ffa6efa5eb60428e698c0000000000000000266a24b9e11b6d2f6\
1323+
21d7ec3f45a5eca89d3ea6a294cdf3a042e973009584470a12916111e2caa01200\
1324+
000000000000000000000000000000000000000000000000000000000000000000\
1325+
00000"
1326+
)
1327+
}
12861328
}

src/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use serde::{de, Deserialize, Serialize};
1717

1818
static JSONRPC_2_0: &str = "2.0";
1919

20+
pub type Call = (String, Vec<Param>);
21+
2022
#[derive(Serialize, Clone)]
2123
#[serde(untagged)]
2224
/// A single parameter of a [`Request`](struct.Request.html)

0 commit comments

Comments
 (0)