From 39a6739146e3c7f7477cd720f5ba37db862eba53 Mon Sep 17 00:00:00 2001 From: Sander Bosma Date: Fri, 17 Feb 2023 13:07:31 +0100 Subject: [PATCH] Add the descriptor argument to createwallet --- client/src/client.rs | 58 +++++++++++++++++++++++++++++------- client/src/error.rs | 5 ++++ integration_test/src/main.rs | 9 +++++- 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/client/src/client.rs b/client/src/client.rs index e254af35..680d9b12 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -278,18 +278,54 @@ pub trait RpcApi: Sized { blank: Option, passphrase: Option<&str>, avoid_reuse: Option, + descriptors: Option, ) -> Result { - let mut args = [ - wallet.into(), - opt_into_json(disable_private_keys)?, - opt_into_json(blank)?, - opt_into_json(passphrase)?, - opt_into_json(avoid_reuse)?, - ]; - self.call( - "createwallet", - handle_defaults(&mut args, &[false.into(), false.into(), into_json("")?, false.into()]), - ) + // the descriptors argument was added in version 21 + if self.version()? < 210000 { + // note: we allow Some(false) since it's the default behavior + if let Some(true) = descriptors { + return Err(Error::Unsupported); + } + // no descriptors argument yet + let mut args = [ + wallet.into(), + opt_into_json(disable_private_keys)?, + opt_into_json(blank)?, + opt_into_json(passphrase)?, + opt_into_json(avoid_reuse)?, + ]; + self.call( + "createwallet", + handle_defaults( + &mut args, + &[false.into(), false.into(), into_json("")?, false.into()], + ), + ) + } else { + let mut args = [ + wallet.into(), + opt_into_json(disable_private_keys)?, + opt_into_json(blank)?, + opt_into_json(passphrase)?, + opt_into_json(avoid_reuse)?, + opt_into_json(descriptors)?, + ]; + // from 23 on, the default value of the descriptors argument is true + let default_descriptors = self.version()? >= 230000; + self.call( + "createwallet", + handle_defaults( + &mut args, + &[ + false.into(), + false.into(), + into_json("")?, + false.into(), + default_descriptors.into(), + ], + ), + ) + } } fn list_wallets(&self) -> Result> { diff --git a/client/src/error.rs b/client/src/error.rs index f874fc21..b4e7da54 100644 --- a/client/src/error.rs +++ b/client/src/error.rs @@ -31,6 +31,8 @@ pub enum Error { UnexpectedStructure, /// The daemon returned an error string. ReturnedError(String), + /// Feature not supported by the connected bitcoin version. + Unsupported, } impl From for Error { @@ -88,6 +90,9 @@ impl fmt::Display for Error { Error::InvalidCookieFile => write!(f, "invalid cookie file"), Error::UnexpectedStructure => write!(f, "the JSON result had an unexpected structure"), Error::ReturnedError(ref s) => write!(f, "the daemon returned an error string: {}", s), + Error::Unsupported => { + write!(f, "the daemon version does not support the accessed feature") + } } } } diff --git a/integration_test/src/main.rs b/integration_test/src/main.rs index 118a9ba4..1ca5c4bb 100644 --- a/integration_test/src/main.rs +++ b/integration_test/src/main.rs @@ -131,7 +131,7 @@ fn main() { unsafe { VERSION = cl.version().unwrap() }; println!("Version: {}", version()); - cl.create_wallet("testwallet", None, None, None, None).unwrap(); + cl.create_wallet("testwallet", None, None, None, None, None).unwrap(); test_get_mining_info(&cl); test_get_blockchain_info(&cl); @@ -1062,6 +1062,7 @@ fn test_create_wallet(cl: &Client) { blank: Option, passphrase: Option<&'a str>, avoid_reuse: Option, + descriptor: Option, } let mut wallet_params = vec![ @@ -1071,6 +1072,7 @@ fn test_create_wallet(cl: &Client) { blank: None, passphrase: None, avoid_reuse: None, + descriptor: None, }, WalletParams { name: wallet_names[1], @@ -1078,6 +1080,7 @@ fn test_create_wallet(cl: &Client) { blank: None, passphrase: None, avoid_reuse: None, + descriptor: None, }, WalletParams { name: wallet_names[2], @@ -1085,6 +1088,7 @@ fn test_create_wallet(cl: &Client) { blank: Some(true), passphrase: None, avoid_reuse: None, + descriptor: None, }, ]; @@ -1095,6 +1099,7 @@ fn test_create_wallet(cl: &Client) { blank: None, passphrase: Some("pass"), avoid_reuse: None, + descriptor: None, }); wallet_params.push(WalletParams { name: wallet_names[4], @@ -1102,6 +1107,7 @@ fn test_create_wallet(cl: &Client) { blank: None, passphrase: None, avoid_reuse: Some(true), + descriptor: Some(false), }); } @@ -1113,6 +1119,7 @@ fn test_create_wallet(cl: &Client) { wallet_param.blank, wallet_param.passphrase, wallet_param.avoid_reuse, + wallet_param.descriptor, ) .unwrap();