From 5d419dd4ec864426b70bd88a28c805f4ee234498 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Sat, 19 Feb 2022 17:45:24 +0000 Subject: [PATCH] Handle case where funds are sent to and from the same bitcoiny deterministic wallet. --- src/main/java/org/qortal/crosschain/Bitcoiny.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/qortal/crosschain/Bitcoiny.java b/src/main/java/org/qortal/crosschain/Bitcoiny.java index cce20c84..bc6f79e1 100644 --- a/src/main/java/org/qortal/crosschain/Bitcoiny.java +++ b/src/main/java/org/qortal/crosschain/Bitcoiny.java @@ -476,6 +476,7 @@ public abstract class Bitcoiny implements ForeignBlockchain { List outputs = new ArrayList<>(); boolean anyOutputAddressInWallet = false; + boolean transactionInvolvesExternalWallet = false; for (BitcoinyTransaction.Input input : t.inputs) { try { @@ -490,6 +491,9 @@ public abstract class Bitcoiny implements ForeignBlockchain { total += inputAmount; addressInWallet = true; } + else { + transactionInvolvesExternalWallet = true; + } inputs.add(new SimpleTransaction.Input(sender, inputAmount, addressInWallet)); } } @@ -503,14 +507,17 @@ public abstract class Bitcoiny implements ForeignBlockchain { for (String address : output.addresses) { boolean addressInWallet = false; if (keySet.contains(address)) { - if (total > 0L) { + if (total > 0L) { // Change returned from sent amount amount -= (total - output.value); - } else { + } else { // Amount received amount += output.value; } addressInWallet = true; anyOutputAddressInWallet = true; } + else { + transactionInvolvesExternalWallet = true; + } outputs.add(new SimpleTransaction.Output(address, output.value, addressInWallet)); } } @@ -525,6 +532,10 @@ public abstract class Bitcoiny implements ForeignBlockchain { amount = total * -1; } } + else if (!transactionInvolvesExternalWallet) { + // All inputs and outputs relate to this wallet, so the balance should be unaffected + amount = 0; + } return new SimpleTransaction(t.txHash, t.timestamp, amount, fee, inputs, outputs); }