]> Untitled Git - bdk/commitdiff
chore(wallet)!: Remove enum AddressIndex
authorvalued mammal <valuedmammal@protonmail.com>
Sun, 14 Apr 2024 14:58:59 +0000 (10:58 -0400)
committer志宇 <hello@evanlinjin.me>
Sat, 20 Apr 2024 07:12:39 +0000 (15:12 +0800)
crates/bdk/src/wallet/mod.rs
crates/hwi/src/lib.rs
example-crates/wallet_electrum/src/main.rs
example-crates/wallet_esplora_async/src/main.rs
example-crates/wallet_esplora_blocking/src/main.rs

index 00f0c46150ef33664b38914a5fba158863dc478d..a03d7ac7f9d78aa80f2412c3919b96c15046cc3b 100644 (file)
@@ -178,28 +178,6 @@ impl
     }
 }
 
-/// The address index selection strategy to use to derived an address from the wallet's external
-/// descriptor.
-#[derive(Debug)]
-pub enum AddressIndex {
-    /// Return a new address after incrementing the current descriptor index.
-    New,
-    /// Return the address for the current descriptor index if it has not been used in a received
-    /// transaction. Otherwise return a new address as with [`AddressIndex::New`].
-    ///
-    /// Use with caution, if the wallet has not yet detected an address has been used it could
-    /// return an already used address. This function is primarily meant for situations where the
-    /// caller is untrusted; for example when deriving donation addresses on-demand for a public
-    /// web page.
-    LastUnused,
-    /// Return the address for a specific descriptor index. Does not change the current descriptor
-    /// index used by `AddressIndex::New` and `AddressIndex::LastUsed`.
-    ///
-    /// Use with caution, if an index is given that is less than the current descriptor index
-    /// then the returned address may have already been used.
-    Peek(u32),
-}
-
 /// A derived address and the index it was found at.
 /// For convenience this automatically derefs to `Address`
 #[derive(Debug, PartialEq, Eq)]
@@ -640,104 +618,6 @@ impl Wallet {
         self.indexed_graph.index.keychains()
     }
 
-    /// Return a derived address using the external descriptor, see [`AddressIndex`] for
-    /// available address index selection strategies. If none of the keys in the descriptor are derivable
-    /// (i.e. does not end with /*) then the same address will always be returned for any [`AddressIndex`].
-    ///
-    /// A `PersistBackend<ChangeSet>::WriteError` will result if unable to persist the new address
-    /// to the `PersistBackend`.
-    ///
-    /// # Panics
-    ///
-    /// This panics when the caller requests for an address of derivation index greater than the
-    /// BIP32 max index.
-    pub fn try_get_address(&mut self, address_index: AddressIndex) -> anyhow::Result<AddressInfo> {
-        self._get_address(KeychainKind::External, address_index)
-    }
-
-    /// Return a derived address using the internal (change) descriptor.
-    ///
-    /// If the wallet doesn't have an internal descriptor it will use the external descriptor.
-    ///
-    /// A `PersistBackend<ChangeSet>::WriteError` will result if unable to persist the new address
-    /// to the `PersistBackend`.
-    ///
-    /// see [`AddressIndex`] for available address index selection strategies. If none of the keys
-    /// in the descriptor are derivable (i.e. does not end with /*) then the same address will always
-    /// be returned for any [`AddressIndex`].
-    ///
-    /// # Panics
-    ///
-    /// This panics when the caller requests for an address of derivation index greater than the
-    /// BIP32 max index.
-    pub fn try_get_internal_address(
-        &mut self,
-        address_index: AddressIndex,
-    ) -> anyhow::Result<AddressInfo> {
-        self._get_address(KeychainKind::Internal, address_index)
-    }
-
-    /// Return a derived address using the specified `keychain` (external/internal).
-    ///
-    /// If `keychain` is [`KeychainKind::External`], external addresses will be derived (used for
-    /// receiving funds).
-    ///
-    /// If `keychain` is [`KeychainKind::Internal`], internal addresses will be derived (used for
-    /// creating change outputs). If the wallet does not have an internal keychain, it will use the
-    /// external keychain to derive change outputs.
-    ///
-    /// See [`AddressIndex`] for available address index selection strategies. If none of the keys
-    /// in the descriptor are derivable (i.e. does not end with /*) then the same address will
-    /// always be returned for any [`AddressIndex`].
-    ///
-    /// # Panics
-    ///
-    /// This panics when the caller requests for an address of derivation index greater than the
-    /// BIP32 max index.
-    fn _get_address(
-        &mut self,
-        keychain: KeychainKind,
-        address_index: AddressIndex,
-    ) -> anyhow::Result<AddressInfo> {
-        let keychain = self.map_keychain(keychain);
-        let txout_index = &mut self.indexed_graph.index;
-        let (index, spk, changeset) = match address_index {
-            AddressIndex::New => {
-                let ((index, spk), index_changeset) = txout_index.reveal_next_spk(&keychain);
-                (index, spk.into(), Some(index_changeset))
-            }
-            AddressIndex::LastUnused => {
-                let ((index, spk), index_changeset) = txout_index.next_unused_spk(&keychain);
-                (index, spk.into(), Some(index_changeset))
-            }
-            AddressIndex::Peek(mut peek_index) => {
-                let mut spk_iter = txout_index.unbounded_spk_iter(&keychain);
-                if !spk_iter.descriptor().has_wildcard() {
-                    peek_index = 0;
-                }
-                let (index, spk) = spk_iter
-                    .nth(peek_index as usize)
-                    .expect("derivation index is out of bounds");
-                (index, spk, None)
-            }
-        };
-
-        if let Some(changeset) = changeset {
-            self.persist
-                .stage(ChangeSet::from(indexed_tx_graph::ChangeSet::from(
-                    changeset,
-                )));
-            self.persist.commit()?;
-        }
-
-        Ok(AddressInfo {
-            index,
-            address: Address::from_script(&spk, self.network)
-                .expect("descriptor must have address form"),
-            keychain,
-        })
-    }
-
     /// Peek an address of the given `keychain` at `index` without revealing it.
     ///
     /// For non-wildcard descriptors this returns the same address at every provided index.
index 4e2cd0c0f5a7a1d69c99ea491ca88bf312dd0bd8..ab87e8a8719f97ff5080f086875e1d2aa606cc4d 100644 (file)
@@ -6,7 +6,6 @@
 //! # use bdk::bitcoin::Network;
 //! # use bdk::signer::SignerOrdering;
 //! # use bdk_hwi::HWISigner;
-//! # use bdk::wallet::AddressIndex::New;
 //! # use bdk::{KeychainKind, SignOptions, Wallet};
 //! # use hwi::HWIClient;
 //! # use std::sync::Arc;
index a1a628c78e5bed484290f320f7eab06bc3f8223f..4af9e71dec7cb5f79eeb3d84597c09829ac105aa 100644 (file)
@@ -8,8 +8,8 @@ use std::str::FromStr;
 
 use bdk::bitcoin::Address;
 use bdk::wallet::Update;
-use bdk::SignOptions;
 use bdk::{bitcoin::Network, Wallet};
+use bdk::{KeychainKind, SignOptions};
 use bdk_electrum::{
     electrum_client::{self, ElectrumApi},
     ElectrumExt, ElectrumUpdate,
@@ -29,7 +29,7 @@ fn main() -> Result<(), anyhow::Error> {
         Network::Testnet,
     )?;
 
-    let address = wallet.try_get_address(bdk::wallet::AddressIndex::New)?;
+    let address = wallet.next_unused_address(KeychainKind::External)?;
     println!("Generated Address: {}", address);
 
     let balance = wallet.get_balance();
index 14e9e38dd03b456bf06be2c409a77c29694d9ad3..734b3491141a3aaaec8ff8fd0981d0395440421c 100644 (file)
@@ -2,8 +2,8 @@ use std::{io::Write, str::FromStr};
 
 use bdk::{
     bitcoin::{Address, Network},
-    wallet::{AddressIndex, Update},
-    SignOptions, Wallet,
+    wallet::Update,
+    KeychainKind, SignOptions, Wallet,
 };
 use bdk_esplora::{esplora_client, EsploraAsyncExt};
 use bdk_file_store::Store;
@@ -27,7 +27,7 @@ async fn main() -> Result<(), anyhow::Error> {
         Network::Testnet,
     )?;
 
-    let address = wallet.try_get_address(AddressIndex::New)?;
+    let address = wallet.next_unused_address(KeychainKind::External)?;
     println!("Generated Address: {}", address);
 
     let balance = wallet.get_balance();
index 1815650d4377333c12c87b751bc21b341f2411e0..cb8fa729e7e8389a11ef2b8e9fb9177c5d87cf0d 100644 (file)
@@ -7,8 +7,8 @@ use std::{io::Write, str::FromStr};
 
 use bdk::{
     bitcoin::{Address, Network},
-    wallet::{AddressIndex, Update},
-    SignOptions, Wallet,
+    wallet::Update,
+    KeychainKind, SignOptions, Wallet,
 };
 use bdk_esplora::{esplora_client, EsploraExt};
 use bdk_file_store::Store;
@@ -26,7 +26,7 @@ fn main() -> Result<(), anyhow::Error> {
         Network::Testnet,
     )?;
 
-    let address = wallet.try_get_address(AddressIndex::New)?;
+    let address = wallet.next_unused_address(KeychainKind::External)?;
     println!("Generated Address: {}", address);
 
     let balance = wallet.get_balance();