bitcoin_harness/
bitcoind_rpc_api.rs

1use bitcoin::{Address, BlockHash, Transaction, Txid};
2use bitcoincore_rpc_json::{
3    FinalizePsbtResult, GetAddressInfoResult, GetBlockResult, GetBlockchainInfoResult,
4    GetDescriptorInfoResult, GetTransactionResult, GetWalletInfoResult, ListUnspentResultEntry,
5    LoadWalletResult, WalletCreateFundedPsbtResult,
6};
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9
10#[jsonrpc_client::api]
11pub trait BitcoindRpcApi {
12    async fn createwallet(
13        &self,
14        wallet_name: &str,
15        disable_private_keys: Option<bool>,
16        blank: Option<bool>,
17        passphrase: Option<String>,
18        avoid_reuse: Option<bool>,
19    ) -> LoadWalletResult;
20
21    async fn deriveaddresses(&self, descriptor: &str, range: Option<[u64; 2]>) -> Vec<Address>;
22
23    async fn dumpwallet(&self, filename: &std::path::Path) -> DumpWalletResponse;
24
25    async fn finalizepsbt(&self, psbt: PsbtBase64) -> FinalizePsbtResult;
26
27    async fn generatetoaddress(&self, nblocks: u32, address: Address) -> Vec<BlockHash>;
28
29    async fn getaddressinfo(&self, address: &Address) -> GetAddressInfoResult;
30
31    async fn getbalance(
32        &self,
33        minconf: Option<u32>,
34        include_watchonly: Option<bool>,
35        avoid_reuse: Option<bool>,
36    ) -> f64;
37
38    async fn getblock(&self, blockhash: &bitcoin::BlockHash) -> GetBlockResult;
39
40    async fn getblockchaininfo(&self) -> GetBlockchainInfoResult;
41
42    async fn getblockcount(&self) -> u32;
43
44    async fn getdescriptorinfo(&self, descriptor: &str) -> GetDescriptorInfoResult;
45
46    async fn getnewaddress(&self, label: Option<String>, address_type: Option<String>) -> Address;
47
48    async fn gettransaction(&self, txid: Txid) -> GetTransactionResult;
49
50    async fn getwalletinfo(&self) -> GetWalletInfoResult;
51
52    async fn joinpsbts(&self, txs: &[String]) -> PsbtBase64;
53
54    async fn listunspent(
55        &self,
56        minconf: Option<u32>,
57        maxconf: Option<u32>,
58        addresses: Option<Vec<Address>>,
59        include_unsafe: Option<bool>,
60    ) -> Vec<ListUnspentResultEntry>;
61
62    async fn listwallets(&self) -> Vec<String>;
63
64    async fn sendrawtransaction(&self, hexstring: TransactionHex) -> String;
65
66    /// amount is btc
67    async fn sendtoaddress(&self, address: Address, amount: f64) -> String;
68
69    async fn sethdseed(&self, new_key_pool: Option<bool>, wif_private_key: Option<String>) -> ();
70
71    /// Outputs are {address, btc amount}
72    async fn walletcreatefundedpsbt(
73        &self,
74        inputs: &[bitcoincore_rpc_json::CreateRawTransactionInput],
75        outputs: HashMap<String, f64>,
76    ) -> WalletCreateFundedPsbtResult;
77
78    async fn walletprocesspsbt(&self, psbt: PsbtBase64) -> WalletProcessPsbtResponse;
79}
80
81#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
82pub struct DumpWalletResponse {
83    pub filename: String,
84}
85
86#[derive(Debug, Deserialize, Serialize)]
87pub struct PsbtBase64(pub String);
88
89#[derive(Debug, Deserialize, Serialize)]
90pub struct WalletProcessPsbtResponse {
91    psbt: String,
92    complete: bool,
93}
94
95#[derive(Clone, Copy, Debug, Serialize)]
96#[serde(rename = "*")]
97pub struct Account;
98
99impl From<WalletProcessPsbtResponse> for PsbtBase64 {
100    fn from(processed_psbt: WalletProcessPsbtResponse) -> Self {
101        Self(processed_psbt.psbt)
102    }
103}
104
105impl From<String> for PsbtBase64 {
106    fn from(base64_string: String) -> Self {
107        Self(base64_string)
108    }
109}
110
111#[derive(Debug, Serialize)]
112pub struct TransactionHex(String);
113
114impl From<Transaction> for TransactionHex {
115    fn from(tx: Transaction) -> Self {
116        Self(bitcoin::consensus::encode::serialize_hex(&tx))
117    }
118}