corepc_client/client_sync/v17/
raw_transactions.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! Macros for implementing JSON-RPC methods on a client.
4//!
5//! Specifically this is methods found under the `== Rawtransactions ==` section of the
6//! API docs of Bitcoin Core `v0.17`.
7//!
8//! All macros require `Client` to be in scope.
9//!
10//! See or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.
11
12/// Implements Bitcoin Core JSON-RPC API method `combinepsbt`.
13#[macro_export]
14macro_rules! impl_client_v17__combine_psbt {
15    () => {
16        impl Client {
17            pub fn combine_psbt(&self, txs: &[bitcoin::Psbt]) -> Result<CombinePsbt> {
18                let txs = txs.iter().map(|psbt| format!("{}", psbt)).collect::<Vec<String>>();
19                self.call("combinepsbt", &[txs.into()])
20            }
21        }
22    };
23}
24
25/// Implements Bitcoin Core JSON-RPC API method `combinerawtransaction`.
26#[macro_export]
27macro_rules! impl_client_v17__combine_raw_transaction {
28    () => {
29        impl Client {
30            pub fn combine_raw_transaction(
31                &self,
32                txs: &[bitcoin::Transaction],
33            ) -> Result<CombineRawTransaction> {
34                let encoded = txs
35                    .iter()
36                    .map(|tx| bitcoin::consensus::encode::serialize_hex(tx))
37                    .collect::<Vec<String>>();
38                self.call("combinerawtransaction", &[into_json(encoded)?])
39            }
40        }
41    };
42}
43
44/// Implements Bitcoin Core JSON-RPC API method `converttopsbt`.
45#[macro_export]
46macro_rules! impl_client_v17__convert_to_psbt {
47    () => {
48        impl Client {
49            pub fn convert_to_psbt(&self, tx: &bitcoin::Transaction) -> Result<ConvertToPsbt> {
50                let hex = bitcoin::consensus::encode::serialize_hex(tx);
51                self.call("converttopsbt", &[hex.into()])
52            }
53        }
54    };
55}
56
57/// Implements Bitcoin Core JSON-RPC API method `createpsbt`.
58#[macro_export]
59macro_rules! impl_client_v17__create_psbt {
60    () => {
61        impl Client {
62            pub fn create_psbt(&self, inputs: &[Input], outputs: &[Output]) -> Result<CreatePsbt> {
63                self.call("createpsbt", &[into_json(inputs)?, into_json(outputs)?])
64            }
65        }
66    };
67}
68
69/// Implements Bitcoin Core JSON-RPC API method `createrawtransaction`.
70#[macro_export]
71macro_rules! impl_client_v17__create_raw_transaction {
72    () => {
73        impl Client {
74            pub fn create_raw_transaction(
75                &self,
76                inputs: &[Input],
77                outputs: &[Output],
78            ) -> Result<CreateRawTransaction> {
79                self.call("createrawtransaction", &[into_json(inputs)?, into_json(outputs)?])
80            }
81        }
82    };
83}
84
85/// Implements Bitcoin Core JSON-RPC API method `decodepsbt`.
86#[macro_export]
87macro_rules! impl_client_v17__decode_psbt {
88    () => {
89        impl Client {
90            pub fn decode_psbt(&self, psbt: &str) -> Result<DecodePsbt> {
91                self.call("decodepsbt", &[psbt.into()])
92            }
93        }
94    };
95}
96
97/// Implements Bitcoin Core JSON-RPC API method `finalizepsbt`.
98#[macro_export]
99macro_rules! impl_client_v17__finalize_psbt {
100    () => {
101        impl Client {
102            pub fn finalize_psbt(&self, psbt: &bitcoin::Psbt) -> Result<FinalizePsbt> {
103                let psbt = format!("{}", psbt);
104                // Pass extract=false so Core returns the PSBT field in the response.
105                self.call("finalizepsbt", &[psbt.into(), false.into()])
106            }
107        }
108    };
109}
110
111/// Implements Bitcoin Core JSON-RPC API method `decoderawtransaction`.
112#[macro_export]
113macro_rules! impl_client_v17__decode_raw_transaction {
114    () => {
115        impl Client {
116            pub fn decode_raw_transaction(
117                &self,
118                tx: &bitcoin::Transaction,
119            ) -> Result<DecodeRawTransaction> {
120                let hex = bitcoin::consensus::encode::serialize_hex(tx);
121                self.call("decoderawtransaction", &[hex.into()])
122            }
123        }
124    };
125}
126
127/// Implements Bitcoin Core JSON-RPC API method `decodescript`.
128#[macro_export]
129macro_rules! impl_client_v17__decode_script {
130    () => {
131        impl Client {
132            // Arg is the hex encoded script we want to decode.
133            pub fn decode_script(&self, script: &str) -> Result<DecodeScript> {
134                self.call("decodescript", &[script.into()])
135            }
136        }
137    };
138}
139
140/// Implements Bitcoin Core JSON-RPC API method `fundrawtransaction`.
141#[macro_export]
142macro_rules! impl_client_v17__fund_raw_transaction {
143    () => {
144        impl Client {
145            pub fn fund_raw_transaction(
146                &self,
147                tx: &bitcoin::Transaction,
148            ) -> Result<FundRawTransaction> {
149                let hex = bitcoin::consensus::encode::serialize_hex(tx);
150                self.call("fundrawtransaction", &[hex.into()])
151            }
152        }
153    };
154}
155
156/// Implements Bitcoin Core JSON-RPC API method `getrawtransaction`.
157#[macro_export]
158macro_rules! impl_client_v17__get_raw_transaction {
159    () => {
160        impl Client {
161            pub fn get_raw_transaction(&self, txid: bitcoin::Txid) -> Result<GetRawTransaction> {
162                self.call("getrawtransaction", &[into_json(&txid)?, false.into()])
163            }
164
165            pub fn get_raw_transaction_verbose(
166                &self,
167                txid: Txid,
168            ) -> Result<GetRawTransactionVerbose> {
169                self.call("getrawtransaction", &[into_json(&txid)?, true.into()])
170            }
171        }
172    };
173}
174
175/// Implements Bitcoin Core JSON-RPC API method `sendrawtransaction`.
176#[macro_export]
177macro_rules! impl_client_v17__send_raw_transaction {
178    () => {
179        impl Client {
180            pub fn send_raw_transaction(
181                &self,
182                tx: &bitcoin::Transaction,
183            ) -> Result<SendRawTransaction> {
184                let hex = bitcoin::consensus::encode::serialize_hex(tx);
185                self.call("sendrawtransaction", &[hex.into()])
186            }
187        }
188    };
189}
190
191/// Implements Bitcoin Core JSON-RPC API method `signrawtransaction`.
192#[macro_export]
193macro_rules! impl_client_v17__sign_raw_transaction {
194    () => {
195        impl Client {
196            pub fn sign_raw_transaction(
197                &self,
198                tx: &bitcoin::Transaction,
199            ) -> Result<SignRawTransaction> {
200                let hex = bitcoin::consensus::encode::serialize_hex(tx);
201                self.call("signrawtransaction", &[hex.into()])
202            }
203        }
204    };
205}
206
207/// Implements Bitcoin Core JSON-RPC API method `signrawtransactionwithkey`.
208#[macro_export]
209macro_rules! impl_client_v17__sign_raw_transaction_with_key {
210    () => {
211        impl Client {
212            pub fn sign_raw_transaction_with_key(
213                &self,
214                tx: &bitcoin::Transaction,
215                keys: &[bitcoin::PrivateKey],
216            ) -> Result<SignRawTransactionWithKey> {
217                let hex = bitcoin::consensus::encode::serialize_hex(tx);
218                let keys = keys.iter().map(|k| format!("{}", k)).collect::<Vec<String>>();
219                self.call("signrawtransactionwithkey", &[hex.into(), into_json(keys)?])
220            }
221        }
222    };
223}
224
225/// Implements Bitcoin Core JSON-RPC API method `testmempoolaccept`.
226#[macro_export]
227macro_rules! impl_client_v17__test_mempool_accept {
228    () => {
229        impl Client {
230            pub fn test_mempool_accept(
231                &self,
232                txs: &[bitcoin::Transaction],
233            ) -> Result<TestMempoolAccept> {
234                let encoded = txs
235                    .iter()
236                    .map(|tx| bitcoin::consensus::encode::serialize_hex(tx))
237                    .collect::<Vec<String>>();
238                self.call("testmempoolaccept", &[into_json(encoded)?])
239            }
240        }
241    };
242}