Skip to content

Commit

Permalink
fix(wallet): advance internal keychain when needed
Browse files Browse the repository at this point in the history
i.e. when processing a transaction that uses the next internal address
  • Loading branch information
aesedepece committed Aug 3, 2023
1 parent 0443509 commit bd6c130
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion wallet/src/actors/app/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ macro_rules! routes {
{
let api_addr = $api.clone();
$io.add_method($method_jsonrpc, move |params: Params| {
log::debug!("Handling request for method: {}", $method_jsonrpc);
log::debug!("Handling request for method {}: {:?}", $method_jsonrpc, params);
let addr = api_addr.clone();
// Try to parse the request params into the actor message
let fut03 = future::ready(params.parse::<$actor_msg>())
Expand Down
19 changes: 18 additions & 1 deletion wallet/src/repository/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,10 +1342,15 @@ where
let index = state.next_internal_index;
let parent_key = &state.keychains[keychain as usize];

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

state.next_internal_index = next_index;
// Don't advance the internal index if we are simply previewing
if !preview {
state.next_internal_index = next_index;
log::debug!("Internal keychain advanced to index #{next_index}");
}

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

// Move internal keychain forward if we used a new change address
let next = self._gen_internal_address(state, None, true)?;
if match &txn.transaction {
Transaction::ValueTransfer(vtt) => {
vtt.body.outputs.iter().any(|vto| vto.pkh == next.pkh)
}
Transaction::DataRequest(dr) => dr.body.outputs.iter().any(|vto| vto.pkh == next.pkh),
_ => false,
} {
let _ = self._gen_internal_address(state, None, false);
}

// Mark UTXOs as used so we don't double spend
// Save the timestamp to after which the UTXO can be spent again
let tx_pending_timeout = self.params.pending_transactions_timeout_seconds;
Expand Down

0 comments on commit bd6c130

Please sign in to comment.