@@ -103,10 +103,14 @@ pub struct Wallet<D> {
103103
104104/// The address index selection strategy to use to derived an address from the wallet's external
105105/// descriptor. See [`Wallet::get_address`]. If you're unsure which one to use use `WalletIndex::New`.
106- #[ derive( Debug ) ]
106+ #[ derive( Debug , Clone ) ]
107107pub enum AddressIndex {
108108 /// Return a new address after incrementing the current descriptor index.
109109 New ,
110+ /// Return address for the current descriptor index.
111+ ///
112+ /// Use with caution, address may have already been used.
113+ Current ,
110114 /// Return the address for the current descriptor index if it has not been used in a received
111115 /// transaction. Otherwise return a new address as with [`AddressIndex::New`].
112116 ///
@@ -268,6 +272,24 @@ where
268272 . map_err ( |_| Error :: ScriptDoesntHaveAddressForm )
269273 }
270274
275+ // Return derived address at the current index (ie. without incrementing the index) for the specified `keychain`.
276+ fn get_current_address ( & self , keychain : KeychainKind ) -> Result < AddressInfo , Error > {
277+ let current_index = self . fetch_index ( keychain) ?;
278+
279+ let address_result = self
280+ . get_descriptor_for_keychain ( keychain)
281+ . at_derivation_index ( current_index)
282+ . address ( self . network ) ;
283+
284+ address_result
285+ . map ( |address| AddressInfo {
286+ address,
287+ index : current_index,
288+ keychain,
289+ } )
290+ . map_err ( |_| Error :: ScriptDoesntHaveAddressForm )
291+ }
292+
271293 // Return the the last previously derived address for `keychain` if it has not been used in a
272294 // received transaction. Otherwise return a new address using [`Wallet::get_new_address`].
273295 fn get_unused_address ( & self , keychain : KeychainKind ) -> Result < AddressInfo , Error > {
@@ -354,6 +376,7 @@ where
354376 ) -> Result < AddressInfo , Error > {
355377 match address_index {
356378 AddressIndex :: New => self . get_new_address ( keychain) ,
379+ AddressIndex :: Current => self . get_current_address ( keychain) ,
357380 AddressIndex :: LastUnused => self . get_unused_address ( keychain) ,
358381 AddressIndex :: Peek ( index) => self . peek_address ( index, keychain) ,
359382 AddressIndex :: Reset ( index) => self . reset_address ( index, keychain) ,
@@ -839,7 +862,12 @@ where
839862 let drain_script = match params. drain_to {
840863 Some ( ref drain_recipient) => drain_recipient. clone ( ) ,
841864 None => self
842- . get_internal_address ( AddressIndex :: New ) ?
865+ . get_internal_address (
866+ params
867+ . change_address_index
868+ . clone ( )
869+ . unwrap_or ( AddressIndex :: New ) ,
870+ ) ?
843871 . address
844872 . script_pubkey ( ) ,
845873 } ;
0 commit comments