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 `addmultisigaddress`.
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                let json = self.get_new_address(None, None)?;
127                let model = json.into_model().unwrap();
128                Ok(model.0.assume_checked())
129            }
130
131            /// Gets a new address from `bitcoind` and parses it assuming its correct.
132            pub fn new_address_with_type(&self, ty: AddressType) -> Result<bitcoin::Address> {
133                let json = self.get_new_address(None, Some(ty))?;
134                let model = json.into_model().unwrap();
135                Ok(model.0.assume_checked())
136            }
137
138            /// Gets a new address with label from `bitcoind` and parses it assuming its correct.
139            // FIXME: unchecked network here is ugly and not uniform with other functions.
140            pub fn new_address_with_label(
141                &self,
142                label: &str,
143            ) -> Result<bitcoin::Address<bitcoin::address::NetworkUnchecked>> {
144                let json = self.get_new_address(Some(label), None)?;
145                let model = json.into_model().unwrap();
146                Ok(model.0)
147            }
148
149            /// Gets a new address - low level RPC call.
150            pub fn get_new_address(
151                &self,
152                label: Option<&str>,
153                ty: Option<AddressType>,
154            ) -> Result<GetNewAddress> {
155                match (label, ty) {
156                    (Some(label), Some(ty)) =>
157                        self.call("getnewaddress", &[into_json(label)?, into_json(ty)?]),
158                    (Some(label), None) => self.call("getnewaddress", &[into_json(label)?]),
159                    (None, Some(ty)) => self.call("getnewaddress", &["".into(), into_json(ty)?]),
160                    (None, None) => self.call("getnewaddress", &[]),
161                }
162            }
163        }
164    };
165}
166
167/// Implements Bitcoin Core JSON-RPC API method `getrawchangeaddress`.
168#[macro_export]
169macro_rules! impl_client_v17__getrawchangeaddress {
170    () => {
171        impl Client {
172            pub fn get_raw_change_address(&self) -> Result<GetRawChangeAddress> {
173                self.call("getrawchangeaddress", &[])
174            }
175        }
176    };
177}
178
179/// Implements Bitcoin Core JSON-RPC API method `getreceivedbyaddress`.
180#[macro_export]
181macro_rules! impl_client_v17__getreceivedbyaddress {
182    () => {
183        impl Client {
184            pub fn get_received_by_address(
185                &self,
186                address: &Address<NetworkChecked>,
187            ) -> Result<GetReceivedByAddress> {
188                self.call("getreceivedbyaddress", &[address.to_string().into()])
189            }
190        }
191    };
192}
193
194/// Implements Bitcoin Core JSON-RPC API method `gettransaction`.
195#[macro_export]
196macro_rules! impl_client_v17__gettransaction {
197    () => {
198        impl Client {
199            pub fn get_transaction(&self, txid: Txid) -> Result<GetTransaction> {
200                self.call("gettransaction", &[into_json(txid)?])
201            }
202        }
203    };
204}
205
206/// Implements Bitcoin Core JSON-RPC API method `getunconfirmedbalance`.
207#[macro_export]
208macro_rules! impl_client_v17__getunconfirmedbalance {
209    () => {
210        impl Client {
211            pub fn get_unconfirmed_balance(&self) -> Result<GetUnconfirmedBalance> {
212                self.call("getunconfirmedbalance", &[])
213            }
214        }
215    };
216}
217
218/// Implements Bitcoin Core JSON-RPC API method `getwalletinfo`.
219#[macro_export]
220macro_rules! impl_client_v17__getwalletinfo {
221    () => {
222        impl Client {
223            pub fn get_wallet_info(&self) -> Result<GetWalletInfo> {
224                self.call("getwalletinfo", &[])
225            }
226        }
227    };
228}
229
230/// Implements Bitcoin Core JSON-RPC API method `listaddressgroupings`.
231#[macro_export]
232macro_rules! impl_client_v17__listaddressgroupings {
233    () => {
234        impl Client {
235            pub fn list_address_groupings(&self) -> Result<ListAddressGroupings> {
236                self.call("listaddressgroupings", &[])
237            }
238        }
239    };
240}
241
242/// Implements Bitcoin Core JSON-RPC API method `listlabels`.
243#[macro_export]
244macro_rules! impl_client_v17__listlabels {
245    () => {
246        impl Client {
247            pub fn list_labels(&self) -> Result<ListLabels> { self.call("listlabels", &[]) }
248        }
249    };
250}
251
252/// Implements Bitcoin Core JSON-RPC API method `listlockunspent`.
253#[macro_export]
254macro_rules! impl_client_v17__listlockunspent {
255    () => {
256        impl Client {
257            pub fn list_lock_unspent(&self) -> Result<ListLockUnspent> {
258                self.call("listlockunspent", &[])
259            }
260        }
261    };
262}
263
264/// Implements Bitcoin Core JSON-RPC API method `listreceivedbyaddress`.
265#[macro_export]
266macro_rules! impl_client_v17__listreceivedbyaddress {
267    () => {
268        impl Client {
269            pub fn list_received_by_address(&self) -> Result<ListReceivedByAddress> {
270                self.call("listreceivedbyaddress", &[])
271            }
272        }
273    };
274}
275
276/// Implements Bitcoin Core JSON-RPC API method `listsinceblock`.
277#[macro_export]
278macro_rules! impl_client_v17__listsinceblock {
279    () => {
280        impl Client {
281            pub fn list_since_block(&self) -> Result<ListSinceBlock> {
282                self.call("listsinceblock", &[])
283            }
284        }
285    };
286}
287
288/// Implements Bitcoin Core JSON-RPC API method `listtransactions`.
289#[macro_export]
290macro_rules! impl_client_v17__listtransactions {
291    () => {
292        impl Client {
293            pub fn list_transactions(&self) -> Result<ListTransactions> {
294                self.call("listtransactions", &[])
295            }
296        }
297    };
298}
299
300/// Implements Bitcoin Core JSON-RPC API method `listunspent`.
301#[macro_export]
302macro_rules! impl_client_v17__listunspent {
303    () => {
304        impl Client {
305            pub fn list_unspent(&self) -> Result<ListUnspent> { self.call("listunspent", &[]) }
306        }
307    };
308}
309
310/// Implements Bitcoin Core JSON-RPC API method `listwallets`.
311#[macro_export]
312macro_rules! impl_client_v17__listwallets {
313    () => {
314        impl Client {
315            pub fn list_wallets(&self) -> Result<ListWallets> { self.call("listwallets", &[]) }
316        }
317    };
318}
319
320/// Implements Bitcoin Core JSON-RPC API method `loadwallet`.
321#[macro_export]
322macro_rules! impl_client_v17__loadwallet {
323    () => {
324        impl Client {
325            pub fn load_wallet(&self, filename: &str) -> Result<LoadWallet> {
326                self.call("loadwallet", &[into_json(filename)?])
327            }
328        }
329    };
330}
331
332/// Implements Bitcoin Core JSON-RPC API method `rescanblockchain`.
333#[macro_export]
334macro_rules! impl_client_v17__rescanblockchain {
335    () => {
336        impl Client {
337            pub fn rescan_blockchain(&self) -> Result<RescanBlockchain> {
338                self.call("rescanblockchain", &[])
339            }
340        }
341    };
342}
343
344/// Implements Bitcoin Core JSON-RPC API method `sendmany`.
345#[macro_export]
346macro_rules! impl_client_v17__sendmany {
347    () => {
348        impl Client {
349            pub fn send_many(&self, amounts: BTreeMap<Address, Amount>) -> Result<SendMany> {
350                let dummy = ""; // Must be set to "" for backwards compatibility.
351                self.call("sendmany", &[into_json(dummy)?, into_json(amounts)?])
352            }
353        }
354    };
355}
356
357/// Implements Bitcoin Core JSON-RPC API method `sendtoaddress`.
358#[macro_export]
359macro_rules! impl_client_v17__sendtoaddress {
360    () => {
361        impl Client {
362            // Send to address - no RBF.
363            pub fn send_to_address(
364                &self,
365                address: &Address<NetworkChecked>,
366                amount: Amount,
367            ) -> Result<SendToAddress> {
368                let args = [address.to_string().into(), into_json(amount.to_btc())?];
369                self.call("sendtoaddress", &args)
370            }
371
372            // Send to address - with RBF.
373            pub fn send_to_address_rbf(
374                &self,
375                address: &Address<NetworkChecked>,
376                amount: Amount,
377            ) -> Result<SendToAddress> {
378                let comment = "";
379                let comment_to = "";
380                let subtract_fee_from_amount = false;
381                let replaceable = true;
382
383                let args = [
384                    address.to_string().into(),
385                    into_json(amount.to_btc())?,
386                    comment.into(),
387                    comment_to.into(),
388                    subtract_fee_from_amount.into(),
389                    replaceable.into(),
390                ];
391                self.call("sendtoaddress", &args)
392            }
393        }
394    };
395}
396
397/// Implements Bitcoin Core JSON-RPC API method `signmessage`.
398#[macro_export]
399macro_rules! impl_client_v17__signmessage {
400    () => {
401        impl Client {
402            pub fn sign_message(&self, address: &Address, message: &str) -> Result<SignMessage> {
403                self.call("signmessage", &[into_json(address)?, into_json(message)?])
404            }
405        }
406    };
407}
408
409/// Implements Bitcoin Core JSON-RPC API method `signrawtransactionwithwallet`.
410#[macro_export]
411macro_rules! impl_client_v17__signrawtransactionwithwallet {
412    () => {
413        impl Client {
414            // `hexstring`: The transaction hex string.
415            pub fn sign_raw_transaction_with_wallet(
416                &self,
417                tx: &bitcoin::Transaction,
418            ) -> Result<SignRawTransaction> {
419                let hex = bitcoin::consensus::encode::serialize_hex(tx);
420                self.call("signrawtransactionwithwallet", &[into_json(hex)?])
421            }
422        }
423    };
424}
425
426/// Implements Bitcoin Core JSON-RPC API method `unloadwallet`.
427#[macro_export]
428macro_rules! impl_client_v17__unloadwallet {
429    () => {
430        impl Client {
431            pub fn unload_wallet(&self, wallet_name: &str) -> Result<()> {
432                match self.call("unloadwallet", &[into_json(wallet_name)?]) {
433                    Ok(serde_json::Value::Null) => Ok(()),
434                    Ok(res) => Err(Error::Returned(res.to_string())),
435                    Err(err) => Err(err.into()),
436                }
437            }
438        }
439    };
440}
441
442/// Implements Bitcoin Core JSON-RPC API method `walletcreatefundedpsbt`.
443#[macro_export]
444macro_rules! impl_client_v17__walletcreatefundedpsbt {
445    () => {
446        impl Client {
447            pub fn wallet_create_funded_psbt(
448                &self,
449                inputs: Vec<WalletCreateFundedPsbtInput>,
450                outputs: Vec<BTreeMap<Address, Amount>>,
451            ) -> Result<WalletCreateFundedPsbt> {
452                self.call("walletcreatefundedpsbt", &[into_json(inputs)?, into_json(outputs)?])
453            }
454        }
455    };
456}
457
458/// Implements Bitcoin Core JSON-RPC API method `walletprocesspsbt`.
459#[macro_export]
460macro_rules! impl_client_v17__walletprocesspsbt {
461    () => {
462        impl Client {
463            pub fn wallet_process_psbt(&self, psbt: &bitcoin::Psbt) -> Result<WalletProcessPsbt> {
464                self.call("walletprocesspsbt", &[into_json(psbt)?])
465            }
466        }
467    };
468}