corepc_client/client_sync/v23/
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 `v23`.
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_v23__create_wallet {
15    () => {
16        impl Client {
17            /// Calls `createwallet` with `wallet` as the only argument.
18            ///
19            /// In v23 and later this creates a descriptor wallet. Use `create_legacy_wallet` to create
20            /// a legacy wallet.
21            pub fn create_wallet(&self, wallet: &str) -> Result<CreateWallet> {
22                self.call("createwallet", &[wallet.into()])
23            }
24
25            /// Creates a legacy wallet (i.e not a native descriptor wallet).
26            ///
27            /// > createwallet "wallet_name" ( disable_private_keys blank "passphrase" avoid_reuse descriptors load_on_startup external_signer )
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            /// > 8. external_signer         (boolean, optional, default=false) Use an external signer such as a hardware wallet. Requires -signer to be configured. Wallet creation will fail if keys cannot be fetched. Requires disable_private_keys and descriptors set to true.
40            pub fn create_legacy_wallet(&self, wallet: &str) -> Result<CreateWallet> {
41                let disable_private_keys = false;
42                let blank = false;
43                let passphrase = String::new();
44                let avoid_reuse = false;
45                let descriptors = false;
46
47                self.call(
48                    "createwallet",
49                    &[
50                        wallet.into(),
51                        disable_private_keys.into(),
52                        blank.into(),
53                        passphrase.into(),
54                        avoid_reuse.into(),
55                        descriptors.into(),
56                    ],
57                )
58            }
59        }
60    };
61}
62
63/// Implements Bitcoin Core JSON-RPC API method `newkeypool`.
64#[macro_export]
65macro_rules! impl_client_v23__new_keypool {
66    () => {
67        impl Client {
68            /// Calls `newkeypool` for the loaded wallet.
69            ///
70            /// > newkeypool
71            /// >
72            /// > Entirely clears and refills the keypool.
73            /// > Requires wallet passphrase to be set if wallet is encrypted.
74            pub fn new_keypool(&self) -> Result<()> {
75                match self.call("newkeypool", &[]) {
76                    Ok(serde_json::Value::Null) => Ok(()),
77                    Ok(res) => Err(Error::Returned(res.to_string())),
78                    Err(err) => Err(err.into()),
79                }
80            }
81        }
82    };
83}
84
85/// Implements Bitcoin Core JSON-RPC API method `restorewallet`.
86#[macro_export]
87macro_rules! impl_client_v23__restore_wallet {
88    () => {
89        impl Client {
90            /// Calls `restorewallet` with required and optional arguments.
91            ///
92            /// > restorewallet "wallet_name" "backup_file" ( load_on_startup )
93            pub fn restore_wallet(
94                &self,
95                wallet_name: &str,
96                backup_file: &Path,
97            ) -> Result<RestoreWallet> {
98                self.call("restorewallet", &[wallet_name.into(), into_json(backup_file)?])
99            }
100        }
101    };
102}