From 859ec74c8fcd956bcde8479ca76b4851f2e32428 Mon Sep 17 00:00:00 2001 From: GideonBature Date: Wed, 18 Jun 2025 18:04:12 +0100 Subject: [PATCH] Implement importaddress method and test --- client/src/client_sync/v17/mod.rs | 1 + client/src/client_sync/v17/wallet.rs | 16 ++++++++++++++++ client/src/client_sync/v18/mod.rs | 1 + client/src/client_sync/v19/mod.rs | 1 + client/src/client_sync/v20/mod.rs | 1 + client/src/client_sync/v21/mod.rs | 1 + client/src/client_sync/v22/mod.rs | 1 + client/src/client_sync/v23/mod.rs | 1 + client/src/client_sync/v24/mod.rs | 1 + client/src/client_sync/v25/mod.rs | 1 + client/src/client_sync/v26/mod.rs | 1 + client/src/client_sync/v27/mod.rs | 1 + client/src/client_sync/v28/mod.rs | 1 + client/src/client_sync/v29/mod.rs | 1 + integration_test/tests/wallet.rs | 24 ++++++++++++++++++++++++ 15 files changed, 53 insertions(+) diff --git a/client/src/client_sync/v17/mod.rs b/client/src/client_sync/v17/mod.rs index 30e4830b..64b1c3ba 100644 --- a/client/src/client_sync/v17/mod.rs +++ b/client/src/client_sync/v17/mod.rs @@ -126,6 +126,7 @@ crate::impl_client_v17__get_received_by_address!(); crate::impl_client_v17__get_transaction!(); crate::impl_client_v17__get_unconfirmed_balance!(); crate::impl_client_v17__get_wallet_info!(); +crate::impl_client_v17__import_address!(); crate::impl_client_v17__import_privkey!(); crate::impl_client_v17__list_address_groupings!(); crate::impl_client_v17__list_labels!(); diff --git a/client/src/client_sync/v17/wallet.rs b/client/src/client_sync/v17/wallet.rs index 87f4d1ea..aff62dab 100644 --- a/client/src/client_sync/v17/wallet.rs +++ b/client/src/client_sync/v17/wallet.rs @@ -269,6 +269,22 @@ macro_rules! impl_client_v17__get_wallet_info { }; } +/// Implements Bitcoin Core JSON-RPC API method `importaddress`. +#[macro_export] +macro_rules! impl_client_v17__import_address { + () => { + impl Client { + pub fn import_address(&self, address: &Address) -> Result<()> { + match self.call("importaddress", &[into_json(address)?]) { + Ok(serde_json::Value::Null) => Ok(()), + Ok(res) => Err(Error::Returned(res.to_string())), + Err(err) => Err(err.into()), + } + } + } + }; +} + /// Implements Bitcoin Core JSON-RPC API method `importprivkey`. #[macro_export] macro_rules! impl_client_v17__import_privkey { diff --git a/client/src/client_sync/v18/mod.rs b/client/src/client_sync/v18/mod.rs index f618f13f..0e5e0a89 100644 --- a/client/src/client_sync/v18/mod.rs +++ b/client/src/client_sync/v18/mod.rs @@ -141,6 +141,7 @@ crate::impl_client_v18__get_received_by_label!(); crate::impl_client_v17__get_transaction!(); crate::impl_client_v17__get_unconfirmed_balance!(); crate::impl_client_v17__get_wallet_info!(); +crate::impl_client_v17__import_address!(); crate::impl_client_v17__import_privkey!(); crate::impl_client_v17__list_address_groupings!(); crate::impl_client_v17__list_labels!(); diff --git a/client/src/client_sync/v19/mod.rs b/client/src/client_sync/v19/mod.rs index 0d1b6cc6..236331c6 100644 --- a/client/src/client_sync/v19/mod.rs +++ b/client/src/client_sync/v19/mod.rs @@ -137,6 +137,7 @@ crate::impl_client_v17__get_received_by_address!(); crate::impl_client_v17__get_transaction!(); crate::impl_client_v17__get_unconfirmed_balance!(); crate::impl_client_v17__get_wallet_info!(); +crate::impl_client_v17__import_address!(); crate::impl_client_v17__import_privkey!(); crate::impl_client_v17__list_address_groupings!(); crate::impl_client_v18__list_received_by_label!(); diff --git a/client/src/client_sync/v20/mod.rs b/client/src/client_sync/v20/mod.rs index 267f087f..7387fb5f 100644 --- a/client/src/client_sync/v20/mod.rs +++ b/client/src/client_sync/v20/mod.rs @@ -134,6 +134,7 @@ crate::impl_client_v17__get_received_by_address!(); crate::impl_client_v17__get_transaction!(); crate::impl_client_v17__get_unconfirmed_balance!(); crate::impl_client_v17__get_wallet_info!(); +crate::impl_client_v17__import_address!(); crate::impl_client_v17__import_privkey!(); crate::impl_client_v17__list_address_groupings!(); crate::impl_client_v17__list_labels!(); diff --git a/client/src/client_sync/v21/mod.rs b/client/src/client_sync/v21/mod.rs index 6bc67669..7dae29e9 100644 --- a/client/src/client_sync/v21/mod.rs +++ b/client/src/client_sync/v21/mod.rs @@ -136,6 +136,7 @@ crate::impl_client_v17__get_received_by_address!(); crate::impl_client_v17__get_transaction!(); crate::impl_client_v17__get_unconfirmed_balance!(); crate::impl_client_v17__get_wallet_info!(); +crate::impl_client_v17__import_address!(); crate::impl_client_v17__import_privkey!(); crate::impl_client_v17__list_address_groupings!(); crate::impl_client_v17__list_labels!(); diff --git a/client/src/client_sync/v22/mod.rs b/client/src/client_sync/v22/mod.rs index 7e2bdc5c..589e629b 100644 --- a/client/src/client_sync/v22/mod.rs +++ b/client/src/client_sync/v22/mod.rs @@ -136,6 +136,7 @@ crate::impl_client_v17__get_received_by_address!(); crate::impl_client_v17__get_transaction!(); crate::impl_client_v17__get_unconfirmed_balance!(); crate::impl_client_v17__get_wallet_info!(); +crate::impl_client_v17__import_address!(); crate::impl_client_v17__import_privkey!(); crate::impl_client_v17__list_address_groupings!(); crate::impl_client_v18__list_received_by_label!(); diff --git a/client/src/client_sync/v23/mod.rs b/client/src/client_sync/v23/mod.rs index 1692c433..ad2ab53f 100644 --- a/client/src/client_sync/v23/mod.rs +++ b/client/src/client_sync/v23/mod.rs @@ -138,6 +138,7 @@ crate::impl_client_v17__get_received_by_address!(); crate::impl_client_v17__get_transaction!(); crate::impl_client_v17__get_unconfirmed_balance!(); crate::impl_client_v17__get_wallet_info!(); +crate::impl_client_v17__import_address!(); crate::impl_client_v17__import_privkey!(); crate::impl_client_v17__list_address_groupings!(); crate::impl_client_v18__list_received_by_label!(); diff --git a/client/src/client_sync/v24/mod.rs b/client/src/client_sync/v24/mod.rs index 825c2cda..9269e149 100644 --- a/client/src/client_sync/v24/mod.rs +++ b/client/src/client_sync/v24/mod.rs @@ -135,6 +135,7 @@ crate::impl_client_v17__get_received_by_address!(); crate::impl_client_v17__get_transaction!(); crate::impl_client_v17__get_unconfirmed_balance!(); crate::impl_client_v17__get_wallet_info!(); +crate::impl_client_v17__import_address!(); crate::impl_client_v17__import_privkey!(); crate::impl_client_v17__list_address_groupings!(); crate::impl_client_v18__list_received_by_label!(); diff --git a/client/src/client_sync/v25/mod.rs b/client/src/client_sync/v25/mod.rs index 7602fbe0..ea31f5c6 100644 --- a/client/src/client_sync/v25/mod.rs +++ b/client/src/client_sync/v25/mod.rs @@ -135,6 +135,7 @@ crate::impl_client_v17__get_received_by_address!(); crate::impl_client_v17__get_transaction!(); crate::impl_client_v17__get_unconfirmed_balance!(); crate::impl_client_v17__get_wallet_info!(); +crate::impl_client_v17__import_address!(); crate::impl_client_v17__import_privkey!(); crate::impl_client_v17__list_address_groupings!(); crate::impl_client_v18__list_received_by_label!(); diff --git a/client/src/client_sync/v26/mod.rs b/client/src/client_sync/v26/mod.rs index b29c83cc..fb05cbc8 100644 --- a/client/src/client_sync/v26/mod.rs +++ b/client/src/client_sync/v26/mod.rs @@ -141,6 +141,7 @@ crate::impl_client_v17__get_received_by_address!(); crate::impl_client_v17__get_transaction!(); crate::impl_client_v17__get_unconfirmed_balance!(); crate::impl_client_v17__get_wallet_info!(); +crate::impl_client_v17__import_address!(); crate::impl_client_v17__import_privkey!(); crate::impl_client_v17__list_address_groupings!(); crate::impl_client_v17__list_labels!(); diff --git a/client/src/client_sync/v27/mod.rs b/client/src/client_sync/v27/mod.rs index 8910b1d8..d5a05bb2 100644 --- a/client/src/client_sync/v27/mod.rs +++ b/client/src/client_sync/v27/mod.rs @@ -137,6 +137,7 @@ crate::impl_client_v17__get_received_by_address!(); crate::impl_client_v17__get_transaction!(); crate::impl_client_v17__get_unconfirmed_balance!(); crate::impl_client_v17__get_wallet_info!(); +crate::impl_client_v17__import_address!(); crate::impl_client_v17__import_privkey!(); crate::impl_client_v17__list_address_groupings!(); crate::impl_client_v18__list_received_by_label!(); diff --git a/client/src/client_sync/v28/mod.rs b/client/src/client_sync/v28/mod.rs index f240e34d..632e6467 100644 --- a/client/src/client_sync/v28/mod.rs +++ b/client/src/client_sync/v28/mod.rs @@ -139,6 +139,7 @@ crate::impl_client_v17__get_received_by_address!(); crate::impl_client_v17__get_transaction!(); crate::impl_client_v17__get_unconfirmed_balance!(); crate::impl_client_v17__get_wallet_info!(); +crate::impl_client_v17__import_address!(); crate::impl_client_v17__import_privkey!(); crate::impl_client_v17__list_address_groupings!(); crate::impl_client_v18__list_received_by_label!(); diff --git a/client/src/client_sync/v29/mod.rs b/client/src/client_sync/v29/mod.rs index 855a39bf..e1d2e271 100644 --- a/client/src/client_sync/v29/mod.rs +++ b/client/src/client_sync/v29/mod.rs @@ -139,6 +139,7 @@ crate::impl_client_v17__get_received_by_address!(); crate::impl_client_v17__get_transaction!(); crate::impl_client_v17__get_unconfirmed_balance!(); crate::impl_client_v17__get_wallet_info!(); +crate::impl_client_v17__import_address!(); crate::impl_client_v17__import_privkey!(); crate::impl_client_v17__list_address_groupings!(); crate::impl_client_v18__list_received_by_label!(); diff --git a/integration_test/tests/wallet.rs b/integration_test/tests/wallet.rs index 0a88f6da..6ac335a7 100644 --- a/integration_test/tests/wallet.rs +++ b/integration_test/tests/wallet.rs @@ -279,6 +279,30 @@ fn wallet__get_transaction__modelled() { model.unwrap(); } +#[test] +fn wallet__import_address() { + let node = match () { + #[cfg(feature = "v22_and_below")] + () => Node::with_wallet(Wallet::Default, &[]), + #[cfg(not(feature = "v22_and_below"))] + () => { + let node = Node::with_wallet(Wallet::None, &["-deprecatedrpc=create_bdb"]); + node.client.create_legacy_wallet("wallet_name").expect("createlegacywallet"); + node + } + }; + + let privkey = + PrivateKey::from_wif("cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy").unwrap(); + + // Derive the address from the private key + let secp = bitcoin::secp256k1::Secp256k1::new(); + let pubkey = privkey.public_key(&secp); + let addr = bitcoin::Address::p2pkh(&pubkey, privkey.network); + + node.client.import_address(&addr).expect("importaddress"); +} + #[cfg(not(feature = "v17"))] #[test] fn wallet__list_received_by_label__modelled() {