1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::bitcoin;
use serde_json;
use crate::client::Result;
use crate::client::RpcApi;
pub trait Queryable<C: RpcApi>: Sized {
type Id;
fn query(rpc: &C, id: &Self::Id) -> Result<Self>;
}
impl<C: RpcApi> Queryable<C> for bitcoin::blockdata::block::Block {
type Id = bitcoin::BlockHash;
fn query(rpc: &C, id: &Self::Id) -> Result<Self> {
let rpc_name = "getblock";
let hex: String = rpc.call(rpc_name, &[serde_json::to_value(id)?, 0.into()])?;
let bytes: Vec<u8> = bitcoin::hashes::hex::FromHex::from_hex(&hex)?;
Ok(bitcoin::consensus::encode::deserialize(&bytes)?)
}
}
impl<C: RpcApi> Queryable<C> for bitcoin::blockdata::transaction::Transaction {
type Id = bitcoin::Txid;
fn query(rpc: &C, id: &Self::Id) -> Result<Self> {
let rpc_name = "getrawtransaction";
let hex: String = rpc.call(rpc_name, &[serde_json::to_value(id)?])?;
let bytes: Vec<u8> = bitcoin::hashes::hex::FromHex::from_hex(&hex)?;
Ok(bitcoin::consensus::encode::deserialize(&bytes)?)
}
}
impl<C: RpcApi> Queryable<C> for Option<crate::json::GetTxOutResult> {
type Id = bitcoin::OutPoint;
fn query(rpc: &C, id: &Self::Id) -> Result<Self> {
rpc.get_tx_out(&id.txid, id.vout, Some(true))
}
}