Skip to content

Commit bd6c130

Browse files
committed
fix(wallet): advance internal keychain when needed
i.e. when processing a transaction that uses the next internal address
1 parent 0443509 commit bd6c130

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

wallet/src/actors/app/routes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ macro_rules! routes {
1515
{
1616
let api_addr = $api.clone();
1717
$io.add_method($method_jsonrpc, move |params: Params| {
18-
log::debug!("Handling request for method: {}", $method_jsonrpc);
18+
log::debug!("Handling request for method {}: {:?}", $method_jsonrpc, params);
1919
let addr = api_addr.clone();
2020
// Try to parse the request params into the actor message
2121
let fut03 = future::ready(params.parse::<$actor_msg>())

wallet/src/repository/wallet/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1342,10 +1342,15 @@ where
13421342
let index = state.next_internal_index;
13431343
let parent_key = &state.keychains[keychain as usize];
13441344

1345+
// `preview` is negated because it turns into `persist_db`
13451346
let (address, next_index) =
13461347
self.derive_and_persist_address(label, parent_key, account, keychain, index, !preview)?;
13471348

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+
}
13491354

13501355
Ok(address)
13511356
}
@@ -1461,6 +1466,18 @@ where
14611466
// - Cannot borrow `state` as mutable because it is also borrowed as immutable
14621467
let state = &mut *state;
14631468

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+
14641481
// Mark UTXOs as used so we don't double spend
14651482
// Save the timestamp to after which the UTXO can be spent again
14661483
let tx_pending_timeout = self.params.pending_transactions_timeout_seconds;

0 commit comments

Comments
 (0)