@@ -1342,10 +1342,15 @@ where
1342
1342
let index = state. next_internal_index ;
1343
1343
let parent_key = & state. keychains [ keychain as usize ] ;
1344
1344
1345
+ // `preview` is negated because it turns into `persist_db`
1345
1346
let ( address, next_index) =
1346
1347
self . derive_and_persist_address ( label, parent_key, account, keychain, index, !preview) ?;
1347
1348
1348
- state. next_internal_index = next_index;
1349
+ // Don't advance the internal index if we are simply previewing
1350
+ if !preview {
1351
+ state. next_internal_index = next_index;
1352
+ log:: debug!( "Internal keychain advanced to index #{next_index}" ) ;
1353
+ }
1349
1354
1350
1355
Ok ( address)
1351
1356
}
@@ -1461,6 +1466,18 @@ where
1461
1466
// - Cannot borrow `state` as mutable because it is also borrowed as immutable
1462
1467
let state = & mut * state;
1463
1468
1469
+ // Move internal keychain forward if we used a new change address
1470
+ let next = self . _gen_internal_address ( state, None , true ) ?;
1471
+ if match & txn. transaction {
1472
+ Transaction :: ValueTransfer ( vtt) => {
1473
+ vtt. body . outputs . iter ( ) . any ( |vto| vto. pkh == next. pkh )
1474
+ }
1475
+ Transaction :: DataRequest ( dr) => dr. body . outputs . iter ( ) . any ( |vto| vto. pkh == next. pkh ) ,
1476
+ _ => false ,
1477
+ } {
1478
+ let _ = self . _gen_internal_address ( state, None , false ) ;
1479
+ }
1480
+
1464
1481
// Mark UTXOs as used so we don't double spend
1465
1482
// Save the timestamp to after which the UTXO can be spent again
1466
1483
let tx_pending_timeout = self . params . pending_transactions_timeout_seconds ;
0 commit comments