@@ -192,12 +192,14 @@ impl BMPWallet<Connection> {
192192 let unused = self . list_unused_addresses ( key_chain) . collect :: < Vec < _ > > ( ) ;
193193
194194 let addr = if unused. len ( ) >= STOP_GAP {
195- unused
196- . first ( )
195+ let rand_index = rand:: random :: < u8 > ( ) as usize % unused. len ( ) ;
196+
197+ println ! ( "Rand {rand_index}" ) ;
198+ unused. get ( rand_index)
197199 . expect ( "Unused addresses should not be empty" )
198200 . clone ( )
199201 } else {
200- self . next_unused_address ( key_chain)
202+ self . reveal_next_address ( key_chain)
201203 } ;
202204
203205 // Persist the revealed address to avoid address reuse
@@ -767,6 +769,7 @@ impl DerefMut for BMPWallet<Connection> {
767769#[ cfg( test) ]
768770mod tests {
769771 use std:: str:: FromStr ;
772+
770773 use bdk_kyoto:: FeeRate ;
771774 use bdk_wallet:: bitcoin:: hashes:: Hash as _;
772775 use bdk_wallet:: bitcoin:: { Address , AddressType , Amount , BlockHash , Network , Weight , psbt} ;
@@ -776,9 +779,9 @@ mod tests {
776779 use bmp_tracing:: tracing;
777780 use rand:: RngCore as _;
778781 use secp:: Scalar ;
779- use tempfile:: { tempdir , TempDir } ;
782+ use tempfile:: { TempDir , tempdir } ;
780783
781- use crate :: bmp_wallet:: { BMPWallet , WalletApi as _ } ;
784+ use crate :: bmp_wallet:: { BMPWallet , STOP_GAP , WalletApi } ;
782785 use crate :: test_utils:: { MockedBDKElectrum , derive_public_key, load_imported_wallet} ;
783786
784787 fn get_dir ( ) -> TempDir {
@@ -1169,4 +1172,29 @@ mod tests {
11691172 assert_eq ! ( w2. balance( ) , Amount :: ZERO ) ;
11701173 Ok ( ( ) )
11711174 }
1175+
1176+ #[ test]
1177+ fn test_address_generation ( ) -> anyhow:: Result < ( ) > {
1178+ let dir = get_dir ( ) ;
1179+ let mut wallet = BMPWallet :: new ( dir. path ( ) , "" , Network :: Regtest ) ?;
1180+
1181+ let mut add_vec: Vec < AddressInfo > = vec ! [ ] ;
1182+
1183+ loop {
1184+ add_vec. push ( wallet. next_address ( KeychainKind :: External ) ?) ;
1185+ if add_vec. len ( ) >= STOP_GAP {
1186+ break ;
1187+ }
1188+ }
1189+
1190+ // Since we reached STOP_GAP, next_address should return one address from the add_vec list
1191+ let new_addr = wallet. next_address ( KeychainKind :: External ) ?;
1192+ assert ! ( add_vec. contains( & new_addr) ) ;
1193+
1194+ // Returned next address should be different from previous one but still exist in the list
1195+ let new_addr2 = wallet. next_address ( KeychainKind :: External ) ?;
1196+ assert ! ( add_vec. contains( & new_addr2) && new_addr != new_addr2) ;
1197+
1198+ Ok ( ( ) )
1199+ }
11721200}
0 commit comments