@@ -261,7 +261,7 @@ impl DaemonControl {
261
261
addr_info : & Option < AddrInfo > ,
262
262
) {
263
263
if let Some ( AddrInfo { index, is_change } ) = addr_info {
264
- if * is_change && db_conn. change_index ( ) < * index {
264
+ if * is_change && db_conn. change_index ( ) <= * index {
265
265
let next_index = index
266
266
. increment ( )
267
267
. expect ( "Must not get into hardened territory" ) ;
@@ -1699,9 +1699,46 @@ mod tests {
1699
1699
panic ! ( "expect successful spend creation" )
1700
1700
} ;
1701
1701
let tx_manual = psbt. unsigned_tx ;
1702
- // Check that manual and auto selection give same outputs (including change).
1703
- assert_eq ! ( tx_auto. output, tx_manual. output) ;
1702
+ // Check that manual and auto selection give same outputs (except change address).
1703
+ assert_ne ! ( tx_auto. output, tx_manual. output) ;
1704
+ assert_eq ! ( tx_auto. output. len( ) , tx_manual. output. len( ) ) ;
1705
+ assert_eq ! ( tx_auto. output[ 0 ] , tx_manual. output[ 0 ] ) ;
1706
+ assert_eq ! ( tx_auto. output[ 1 ] . value, tx_manual. output[ 1 ] . value) ;
1707
+ assert_ne ! (
1708
+ tx_auto. output[ 1 ] . script_pubkey,
1709
+ tx_manual. output[ 1 ] . script_pubkey
1710
+ ) ;
1704
1711
// Check inputs are also the same. Need to sort as order is not guaranteed by `create_spend`.
1712
+ let mut auto_input = tx_auto. clone ( ) . input ;
1713
+ let mut manual_input = tx_manual. input ;
1714
+ auto_input. sort ( ) ;
1715
+ manual_input. sort ( ) ;
1716
+ assert_eq ! ( auto_input, manual_input) ;
1717
+
1718
+ // Now do the same again, but this time specifying the change address to be the same
1719
+ // as for the auto spend.
1720
+ let change_address = bitcoin:: Address :: from_script (
1721
+ tx_auto. output [ 1 ] . script_pubkey . as_script ( ) ,
1722
+ bitcoin:: Network :: Bitcoin ,
1723
+ )
1724
+ . unwrap ( ) ;
1725
+ let psbt = if let CreateSpendResult :: Success { psbt, .. } = control
1726
+ . create_spend (
1727
+ & destinations,
1728
+ & [ confirmed_op_1, confirmed_op_2] ,
1729
+ 1 ,
1730
+ Some ( change_address. as_unchecked ( ) . clone ( ) ) ,
1731
+ )
1732
+ . unwrap ( )
1733
+ {
1734
+ psbt
1735
+ } else {
1736
+ panic ! ( "expect successful spend creation" )
1737
+ } ;
1738
+ let tx_manual = psbt. unsigned_tx ;
1739
+ // Now the outputs of each transaction are the same.
1740
+ assert_eq ! ( tx_auto. output, tx_manual. output) ;
1741
+ // Check again that inputs are still the same.
1705
1742
let mut auto_input = tx_auto. input ;
1706
1743
let mut manual_input = tx_manual. input ;
1707
1744
auto_input. sort ( ) ;
0 commit comments