corepc_client/client_sync/v17/
wallet.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 `== Wallet ==` 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 `createwallet`.
13#[macro_export]
14macro_rules! impl_client_v17__addmultisigaddress {
15    () => {
16        impl Client {
17            pub fn add_multisig_address_with_keys(
18                &self,
19                nrequired: u32,
20                keys: Vec<PublicKey>,
21            ) -> Result<AddMultisigAddress> {
22                self.call("addmultisigaddress", &[nrequired.into(), into_json(keys)?])
23            }
24
25            pub fn add_multisig_address_with_addresses(
26                &self,
27                nrequired: u32,
28                keys: Vec<Address>,
29            ) -> Result<AddMultisigAddress> {
30                self.call("addmultisigaddress", &[nrequired.into(), into_json(keys)?])
31            }
32        }
33    };
34}
35
36/// Implements Bitcoin Core JSON-RPC API method `bumpfee`.
37#[macro_export]
38macro_rules! impl_client_v17__bumpfee {
39    () => {
40        impl Client {
41            pub fn bump_fee(&self, txid: Txid) -> Result<BumpFee> {
42                self.call("bumpfee", &[into_json(txid)?])
43            }
44        }
45    };
46}
47
48/// Implements Bitcoin Core JSON-RPC API method `createwallet`.
49#[macro_export]
50macro_rules! impl_client_v17__createwallet {
51    () => {
52        impl Client {
53            pub fn create_wallet(&self, wallet: &str) -> Result<CreateWallet> {
54                self.call("createwallet", &[wallet.into()])
55            }
56        }
57    };
58}
59
60/// Implements Bitcoin Core JSON-RPC API method `dumpprivkey`.
61#[macro_export]
62macro_rules! impl_client_v17__dumpprivkey {
63    () => {
64        impl Client {
65            pub fn dump_priv_key(&self, address: &Address) -> Result<DumpPrivKey> {
66                self.call("dumpprivkey", &[into_json(address)?])
67            }
68        }
69    };
70}
71
72/// Implements Bitcoin Core JSON-RPC API method `dumpwallet`.
73#[macro_export]
74macro_rules! impl_client_v17__dumpwallet {
75    () => {
76        impl Client {
77            // filename is either absolute or relative to bitcoind.
78            pub fn dump_wallet(&self, filename: &Path) -> Result<DumpWallet> {
79                self.call("dumpwallet", &[into_json(filename)?])
80            }
81        }
82    };
83}
84
85/// Implements Bitcoin Core JSON-RPC API method `getaddressesbylabel`.
86#[macro_export]
87macro_rules! impl_client_v17__getaddressesbylabel {
88    () => {
89        impl Client {
90            pub fn get_addresses_by_label(&self, label: &str) -> Result<GetAddressesByLabel> {
91                self.call("getaddressesbylabel", &[label.into()])
92            }
93        }
94    };
95}
96
97/// Implements Bitcoin Core JSON-RPC API method `getaddressinfo`.
98#[macro_export]
99macro_rules! impl_client_v17__getaddressinfo {
100    () => {
101        impl Client {
102            pub fn get_address_info(&self, address: &Address) -> Result<GetAddressInfo> {
103                self.call("getaddressinfo", &[into_json(address)?])
104            }
105        }
106    };
107}
108
109/// Implements Bitcoin Core JSON-RPC API method `getbalance`.
110#[macro_export]
111macro_rules! impl_client_v17__getbalance {
112    () => {
113        impl Client {
114            pub fn get_balance(&self) -> Result<GetBalance> { self.call("getbalance", &[]) }
115        }
116    };
117}
118
119/// Implements Bitcoin Core JSON-RPC API method `getnewaddress`.
120#[macro_export]
121macro_rules! impl_client_v17__getnewaddress {
122    () => {
123        impl Client {
124            /// Gets a new address from `bitcoind` and parses it assuming its correct.
125            pub fn new_address(&self) -> Result<bitcoin::Address> {
126                use core::str::FromStr;
127
128                let json = self.get_new_address(None, None)?;
129                let address = bitcoin::Address::from_str(&json.0)
130                    .expect("assume the address is valid")
131                    .assume_checked(); // Assume bitcoind will return an valid address for the network its on.
132                Ok(address)
133            }
134
135            /// Gets a new address from `bitcoind` and parses it assuming its correct.
136            pub fn new_address_with_type(&self, ty: AddressType) -> Result<bitcoin::Address> {
137                use core::str::FromStr;
138
139                let json = self.get_new_address(None, Some(ty))?;
140                let address = bitcoin::Address::from_str(&json.0)
141                    .expect("assume the address is valid")
142                    .assume_checked(); // Assume bitcoind will return an valid address for the network its on.
143                Ok(address)
144            }
145
146            /// Gets a new address with label from `bitcoind` and parses it assuming its correct.
147            // FIXME: unchecked network here is ugly and not uniform with other functions.
148            pub fn new_address_with_label(
149                &self,
150                label: &str,
151            ) -> Result<bitcoin::Address<bitcoin::address::NetworkUnchecked>> {
152                use core::str::FromStr;
153
154                let json = self.get_new_address(Some(label), None)?;
155                let address =
156                    bitcoin::Address::from_str(&json.0).expect("assume the address is valid");
157                Ok(address)
158            }
159
160            fn get_new_address(
161                &self,
162                label: Option<&str>,
163                ty: Option<AddressType>,
164            ) -> Result<GetNewAddress> {
165                match (label, ty) {
166                    (Some(label), Some(ty)) =>
167                        self.call("getnewaddress", &[into_json(label)?, into_json(ty)?]),
168                    (Some(label), None) => self.call("getnewaddress", &[into_json(label)?]),
169                    (None, Some(ty)) => self.call("getnewaddress", &["".into(), into_json(ty)?]),
170                    (None, None) => self.call("getnewaddress", &[]),
171                }
172            }
173        }
174    };
175}
176
177/// Implements Bitcoin Core JSON-RPC API method `getrawchangeaddress`.
178#[macro_export]
179macro_rules! impl_client_v17__getrawchangeaddress {
180    () => {
181        impl Client {
182            pub fn get_raw_change_address(&self) -> Result<GetRawChangeAddress> {
183                self.call("getrawchangeaddress", &[])
184            }
185        }
186    };
187}
188
189/// Implements Bitcoin Core JSON-RPC API method `getreceivedbyaddress`.
190#[macro_export]
191macro_rules! impl_client_v17__getreceivedbyaddress {
192    () => {
193        impl Client {
194            pub fn get_received_by_address(
195                &self,
196                address: &Address<NetworkChecked>,
197            ) -> Result<GetReceivedByAddress> {
198                self.call("getreceivedbyaddress", &[address.to_string().into()])
199            }
200        }
201    };
202}
203
204/// Implements Bitcoin Core JSON-RPC API method `gettransaction`.
205#[macro_export]
206macro_rules! impl_client_v17__gettransaction {
207    () => {
208        impl Client {
209            pub fn get_transaction(&self, txid: Txid) -> Result<GetTransaction> {
210                self.call("gettransaction", &[into_json(txid)?])
211            }
212        }
213    };
214}
215
216/// Implements Bitcoin Core JSON-RPC API method `getunconfirmedbalance`.
217#[macro_export]
218macro_rules! impl_client_v17__getunconfirmedbalance {
219    () => {
220        impl Client {
221            pub fn get_unconfirmed_balance(&self) -> Result<GetUnconfirmedBalance> {
222                self.call("getunconfirmedbalance", &[])
223            }
224        }
225    };
226}
227
228/// Implements Bitcoin Core JSON-RPC API method `getwalletinfo`.
229#[macro_export]
230macro_rules! impl_client_v17__getwalletinfo {
231    () => {
232        impl Client {
233            pub fn get_wallet_info(&self) -> Result<GetWalletInfo> {
234                self.call("getwalletinfo", &[])
235            }
236        }
237    };
238}
239
240/// Implements Bitcoin Core JSON-RPC API method `listaddressgroupings`.
241#[macro_export]
242macro_rules! impl_client_v17__listaddressgroupings {
243    () => {
244        impl Client {
245            pub fn list_address_groupings(&self) -> Result<ListAddressGroupings> {
246                self.call("listaddressgroupings", &[])
247            }
248        }
249    };
250}
251
252/// Implements Bitcoin Core JSON-RPC API method `listlabels`.
253#[macro_export]
254macro_rules! impl_client_v17__listlabels {
255    () => {
256        impl Client {
257            pub fn list_labels(&self) -> Result<ListLabels> { self.call("listlabels", &[]) }
258        }
259    };
260}
261
262/// Implements Bitcoin Core JSON-RPC API method `listlockunspent`.
263#[macro_export]
264macro_rules! impl_client_v17__listlockunspent {
265    () => {
266        impl Client {
267            pub fn list_lock_unspent(&self) -> Result<ListLockUnspent> {
268                self.call("listlockunspent", &[])
269            }
270        }
271    };
272}
273
274/// Implements Bitcoin Core JSON-RPC API method `listreceivedbyaddress`.
275#[macro_export]
276macro_rules! impl_client_v17__listreceivedbyaddress {
277    () => {
278        impl Client {
279            pub fn list_received_by_address(&self) -> Result<ListReceivedByAddress> {
280                self.call("listreceivedbyaddress", &[])
281            }
282        }
283    };
284}
285
286/// Implements Bitcoin Core JSON-RPC API method `listsinceblock`.
287#[macro_export]
288macro_rules! impl_client_v17__listsinceblock {
289    () => {
290        impl Client {
291            pub fn list_since_block(&self) -> Result<ListSinceBlock> {
292                self.call("listsinceblock", &[])
293            }
294        }
295    };
296}
297
298/// Implements Bitcoin Core JSON-RPC API method `listtransactions`.
299#[macro_export]
300macro_rules! impl_client_v17__listtransactions {
301    () => {
302        impl Client {
303            pub fn list_transactions(&self) -> Result<ListTransactions> {
304                self.call("listtransactions", &[])
305            }
306        }
307    };
308}
309
310/// Implements Bitcoin Core JSON-RPC API method `listunspent`.
311#[macro_export]
312macro_rules! impl_client_v17__listunspent {
313    () => {
314        impl Client {
315            pub fn list_unspent(&self) -> Result<ListUnspent> { self.call("listunspent", &[]) }
316        }
317    };
318}
319
320/// Implements Bitcoin Core JSON-RPC API method `listwallets`.
321#[macro_export]
322macro_rules! impl_client_v17__listwallets {
323    () => {
324        impl Client {
325            pub fn list_wallets(&self) -> Result<ListWallets> { self.call("listwallets", &[]) }
326        }
327    };
328}
329
330/// Implements Bitcoin Core JSON-RPC API method `loadwallet`.
331#[macro_export]
332macro_rules! impl_client_v17__loadwallet {
333    () => {
334        impl Client {
335            pub fn load_wallet(&self, filename: &str) -> Result<LoadWallet> {
336                self.call("loadwallet", &[into_json(filename)?])
337            }
338        }
339    };
340}
341
342/// Implements Bitcoin Core JSON-RPC API method `rescanblockchain`.
343#[macro_export]
344macro_rules! impl_client_v17__rescanblockchain {
345    () => {
346        impl Client {
347            pub fn rescan_blockchain(&self) -> Result<RescanBlockchain> {
348                self.call("rescanblockchain", &[])
349            }
350        }
351    };
352}
353
354/// Implements Bitcoin Core JSON-RPC API method `sendmany`.
355#[macro_export]
356macro_rules! impl_client_v17__sendmany {
357    () => {
358        impl Client {
359            pub fn send_many(&self, amounts: BTreeMap<Address, Amount>) -> Result<SendMany> {
360                let dummy = ""; // Must be set to "" for backwards compatibility.
361                self.call("sendmany", &[into_json(dummy)?, into_json(amounts)?])
362            }
363        }
364    };
365}
366
367/// Implements Bitcoin Core JSON-RPC API method `sendtoaddress`.
368#[macro_export]
369macro_rules! impl_client_v17__sendtoaddress {
370    () => {
371        impl Client {
372            // Send to address - no RBF.
373            pub fn send_to_address(
374                &self,
375                address: &Address<NetworkChecked>,
376                amount: Amount,
377            ) -> Result<SendToAddress> {
378                let args = [address.to_string().into(), into_json(amount.to_btc())?];
379                self.call("sendtoaddress", &args)
380            }
381
382            // Send to address - with RBF.
383            pub fn send_to_address_rbf(
384                &self,
385                address: &Address<NetworkChecked>,
386                amount: Amount,
387            ) -> Result<SendToAddress> {
388                let comment = "";
389                let comment_to = "";
390                let subtract_fee_from_amount = false;
391                let replaceable = true;
392
393                let args = [
394                    address.to_string().into(),
395                    into_json(amount.to_btc())?,
396                    comment.into(),
397                    comment_to.into(),
398                    subtract_fee_from_amount.into(),
399                    replaceable.into(),
400                ];
401                self.call("sendtoaddress", &args)
402            }
403        }
404    };
405}
406
407/// Implements Bitcoin Core JSON-RPC API method `signmessage`.
408#[macro_export]
409macro_rules! impl_client_v17__signmessage {
410    () => {
411        impl Client {
412            pub fn sign_message(&self, address: &Address, message: &str) -> Result<SignMessage> {
413                self.call("signmessage", &[into_json(address)?, into_json(message)?])
414            }
415        }
416    };
417}
418
419/// Implements Bitcoin Core JSON-RPC API method `signrawtransactionwithwallet`.
420#[macro_export]
421macro_rules! impl_client_v17__signrawtransactionwithwallet {
422    () => {
423        impl Client {
424            // `hexstring`: The transaction hex string.
425            pub fn sign_raw_transaction_with_wallet(
426                &self,
427                hex: &str,
428            ) -> Result<SignRawTransactionWithWallet> {
429                self.call("signrawtransactionwithwallet", &[into_json(hex)?])
430            }
431        }
432    };
433}
434
435/// Implements Bitcoin Core JSON-RPC API method `walletcreatefundedpsbt`.
436#[macro_export]
437macro_rules! impl_client_v17__walletcreatefundedpsbt {
438    () => {
439        impl Client {
440            pub fn wallet_create_funded_psbt(
441                &self,
442                inputs: Vec<WalletCreateFundedPsbtInput>,
443                outputs: Vec<BTreeMap<Address, Amount>>,
444            ) -> Result<WalletCreateFundedPsbt> {
445                self.call("walletcreatefundedpsbt", &[into_json(inputs)?, into_json(outputs)?])
446            }
447        }
448    };
449}
450
451/// Implements Bitcoin Core JSON-RPC API method `walletprocesspsbt`.
452#[macro_export]
453macro_rules! impl_client_v17__walletprocesspsbt {
454    () => {
455        impl Client {
456            pub fn wallet_process_psbt(&self, psbt: &bitcoin::Psbt) -> Result<WalletProcessPsbt> {
457                self.call("walletprocesspsbt", &[into_json(psbt)?])
458            }
459        }
460    };
461}