corepc_client/client_sync/v21/
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.21`.
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_v21__create_wallet {
15    () => {
16        impl Client {
17            /// Calls `createwallet` with `wallet` as the only argument.
18            ///
19            /// In v21 and v22 this creates a legacy wallet. Use `create_descriptor_wallet` to create
20            /// a descriptor wallet.
21            pub fn create_wallet(&self, wallet: &str) -> Result<CreateWallet> {
22                self.call("createwallet", &[wallet.into()])
23            }
24
25            /// Creates a wallet with descriptors=true (descriptor wallet).
26            ///
27            /// > createwallet "wallet_name" ( disable_private_keys blank "passphrase" avoid_reuse descriptors load_on_startup )
28            /// >
29            /// > Creates and loads a new wallet.
30            /// >
31            /// > Arguments:
32            /// > 1. wallet_name             (string, required) The name for the new wallet. If this is a path, the wallet will be created at the path location.
33            /// > 2. disable_private_keys    (boolean, optional, default=false) Disable the possibility of private keys (only watchonlys are possible in this mode).
34            /// > 3. blank                   (boolean, optional, default=false) Create a blank wallet. A blank wallet has no keys or HD seed. One can be set using sethdseed.
35            /// > 4. passphrase              (string, optional) Encrypt the wallet with this passphrase.
36            /// > 5. avoid_reuse             (boolean, optional, default=false) Keep track of coin reuse, and treat dirty and clean coins differently with privacy considerations in mind.
37            /// > 6. descriptors             (boolean, optional, default=true) Create a native descriptor wallet. The wallet will use descriptors internally to handle address creation
38            /// > 7. load_on_startup         (boolean, optional) Save wallet name to persistent settings and load on startup. True to add wallet to startup list, false to remove, null to leave unchanged.
39            pub fn create_descriptor_wallet(&self, wallet: &str) -> Result<CreateWallet> {
40                let disable_private_keys = false;
41                let blank = false;
42                let passphrase = String::new();
43                let avoid_reuse = false;
44                let descriptors = true;
45
46                self.call(
47                    "createwallet",
48                    &[
49                        wallet.into(),
50                        disable_private_keys.into(),
51                        blank.into(),
52                        passphrase.into(),
53                        avoid_reuse.into(),
54                        descriptors.into(),
55                    ],
56                )
57            }
58        }
59    };
60}
61
62/// Implements Bitcoin Core JSON-RPC API method `importdescriptors`.
63#[macro_export]
64macro_rules! impl_client_v21__import_descriptors {
65    () => {
66        impl Client {
67            pub fn import_descriptors(
68                &self,
69                requests: &[ImportDescriptorsRequest],
70            ) -> Result<ImportDescriptors> {
71                self.call("importdescriptors", &[into_json(requests)?])
72            }
73        }
74    };
75}
76
77/// Implements Bitcoin Core JSON-RPC API method `psbtbumpfee`.
78#[macro_export]
79macro_rules! impl_client_v21__psbt_bump_fee {
80    () => {
81        impl Client {
82            pub fn psbt_bump_fee(&self, txid: &bitcoin::Txid) -> Result<PsbtBumpFee> {
83                self.call("psbtbumpfee", &[into_json(txid)?])
84            }
85        }
86    };
87}
88
89/// Implements Bitcoin Core JSON-RPC API method `send`.
90#[macro_export]
91macro_rules! impl_client_v21__send {
92    () => {
93        impl Client {
94            pub fn send(&self, outputs: &BTreeMap<String, f64>) -> Result<Send> {
95                self.call("send", &[into_json(outputs)?])
96            }
97        }
98    };
99}
100
101/// Implements Bitcoin Core JSON-RPC API method `sendmany` with `verbose=true` (v21+).
102#[macro_export]
103macro_rules! impl_client_v21__send_many_verbose {
104    () => {
105        impl Client {
106            pub fn send_many_verbose(
107                &self,
108                amounts: BTreeMap<Address, Amount>,
109            ) -> Result<SendManyVerbose> {
110                let dummy = ""; // Backwards compatibility dummy.
111                let amount_btc: BTreeMap<String, f64> = amounts
112                    .into_iter()
113                    .map(|(addr, amount)| (addr.to_string(), amount.to_btc()))
114                    .collect();
115                let minconf = 1u64;
116                let comment = "";
117                let subtract_fee_from: Vec<String> = Vec::new();
118                let replaceable = true;
119                let conf_target = 1u64;
120                let estimate_mode = "unset";
121                let fee_rate = serde_json::Value::Null;
122                let verbose = true;
123                self.call(
124                    "sendmany",
125                    &[
126                        into_json(dummy)?,
127                        into_json(amount_btc)?,
128                        minconf.into(),
129                        comment.into(),
130                        into_json(subtract_fee_from)?,
131                        replaceable.into(),
132                        conf_target.into(),
133                        estimate_mode.into(),
134                        fee_rate,
135                        verbose.into(),
136                    ],
137                )
138            }
139        }
140    };
141}
142
143/// Implements Bitcoin Core JSON-RPC API method `unloadwallet`.
144#[macro_export]
145macro_rules! impl_client_v21__unload_wallet {
146    () => {
147        impl Client {
148            pub fn unload_wallet(&self, wallet: &str) -> Result<UnloadWallet> {
149                self.call("unloadwallet", &[wallet.into()])
150            }
151        }
152    };
153}
154
155/// Implements Bitcoin Core JSON-RPC API method `upgradewallet`.
156#[macro_export]
157macro_rules! impl_client_v21__upgrade_wallet {
158    () => {
159        impl Client {
160            pub fn upgrade_wallet(&self) -> Result<UpgradeWallet> {
161                self.call("upgradewallet", &[])
162            }
163        }
164    };
165}