@@ -238,17 +238,22 @@ impl SqliteConn {
238
238
// Update the address to derivation index mapping.
239
239
// TODO: have this as a helper in descriptors.rs
240
240
let next_la_index = next_index + LOOK_AHEAD_LIMIT - 1 ;
241
- let next_la_address = db_wallet
241
+ let next_receive_address = db_wallet
242
242
. main_descriptor
243
243
. receive_descriptor ( )
244
244
. derive ( next_la_index. into ( ) , secp)
245
245
. address ( network) ;
246
- db_tx
247
- . execute (
248
- "INSERT INTO addresses (address, derivation_index) VALUES (?1, ?2)" ,
249
- rusqlite:: params![ next_la_address. to_string( ) , next_la_index] ,
250
- )
251
- . map ( |_| ( ) )
246
+ let next_change_address = db_wallet
247
+ . main_descriptor
248
+ . change_descriptor ( )
249
+ . derive ( next_la_index. into ( ) , secp)
250
+ . address ( network) ;
251
+ db_tx. execute (
252
+ "INSERT INTO addresses (receive_address, change_address, derivation_index) VALUES (?1, ?2, ?3)" ,
253
+ rusqlite:: params![ next_receive_address. to_string( ) , next_change_address. to_string( ) , next_la_index] ,
254
+ ) ?;
255
+
256
+ Ok ( ( ) )
252
257
} )
253
258
. expect ( "Database must be available" )
254
259
}
@@ -363,7 +368,7 @@ impl SqliteConn {
363
368
pub fn db_address ( & mut self , address : & bitcoin:: Address ) -> Option < DbAddress > {
364
369
db_query (
365
370
& mut self . conn ,
366
- "SELECT * FROM addresses WHERE address = ?1" ,
371
+ "SELECT * FROM addresses WHERE receive_address = ?1 OR change_address = ?1" ,
367
372
rusqlite:: params![ address. to_string( ) ] ,
368
373
|row| row. try_into ( ) ,
369
374
)
@@ -721,6 +726,15 @@ mod tests {
721
726
let db_addr = conn. db_address ( & addr) . unwrap ( ) ;
722
727
assert_eq ! ( db_addr. derivation_index, 0 . into( ) ) ;
723
728
729
+ // And also for the change address
730
+ let addr = options
731
+ . main_descriptor
732
+ . change_descriptor ( )
733
+ . derive ( 0 . into ( ) , & secp)
734
+ . address ( options. bitcoind_network ) ;
735
+ let db_addr = conn. db_address ( & addr) . unwrap ( ) ;
736
+ assert_eq ! ( db_addr. derivation_index, 0 . into( ) ) ;
737
+
724
738
// There is the index for the 199th index (look-ahead limit)
725
739
let addr = options
726
740
. main_descriptor
@@ -742,6 +756,15 @@ mod tests {
742
756
conn. increment_derivation_index ( & secp) ;
743
757
let db_addr = conn. db_address ( & addr) . unwrap ( ) ;
744
758
assert_eq ! ( db_addr. derivation_index, 200 . into( ) ) ;
759
+
760
+ // Same for the change descriptor.
761
+ let addr = options
762
+ . main_descriptor
763
+ . change_descriptor ( )
764
+ . derive ( 200 . into ( ) , & secp)
765
+ . address ( options. bitcoind_network ) ;
766
+ let db_addr = conn. db_address ( & addr) . unwrap ( ) ;
767
+ assert_eq ! ( db_addr. derivation_index, 200 . into( ) ) ;
745
768
}
746
769
747
770
fs:: remove_dir_all ( & tmp_dir) . unwrap ( ) ;
0 commit comments