Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

LL-8115 Additional fix for correct handling of change output #1524

Merged
merged 1 commit into from
Nov 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions src/families/bitcoin/js-synchronisation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,50 @@ const mapTxToOperations = (
const transactionSequenceNumber =
(accountInputs.length > 0 && accountInputs[0].sequence) || undefined;

// Change output is the last one
const hasSpentNothing = value.eq(0);
// Change output is always the last one
const changeOutputIndex = tx.outputs
.map((o) => o.output_index)
.reduce((p, c) => (p > c ? p : c));

for (const output of tx.outputs) {
if (output.address) {
if (accountAddresses.includes(output.address)) {
if (!accountAddresses.includes(output.address)) {
// The output doesn't belong to this account
if (output.output_index < changeOutputIndex) {
// The output isn't the change output
recipients.push(
syncReplaceAddress
? syncReplaceAddress(output.address)
: output.address
);
}
} else {
// The output belongs to this account
accountOutputs.push(output);
} else if (
!changeAddresses.includes(output.address) ||
output.output_index !== changeOutputIndex
) {
recipients.push(
syncReplaceAddress
? syncReplaceAddress(output.address)
: output.address
);

if (!changeAddresses.includes(output.address)) {
// The output isn't a change output of this account
recipients.push(
syncReplaceAddress
? syncReplaceAddress(output.address)
: output.address
);
} else {
// The output is a change output of this account,
// we count it as a recipient only in some special cases
if (
(recipients.length === 0 &&
output.output_index >= changeOutputIndex) ||
hasSpentNothing
) {
recipients.push(
syncReplaceAddress
? syncReplaceAddress(output.address)
: output.address
);
}
}
}
}
}
Expand Down