From 5b9ade5f156a8649b0c8155cddb5398cb83902ab Mon Sep 17 00:00:00 2001 From: julian Date: Fri, 20 Oct 2023 10:53:31 -0600 Subject: [PATCH] add wallet owns flag to outputv2 --- .../models/blockchain_data/v2/output_v2.dart | 21 +++++++++++++++++++ .../coins/bitcoincash/bitcoincash_wallet.dart | 10 ++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/models/isar/models/blockchain_data/v2/output_v2.dart b/lib/models/isar/models/blockchain_data/v2/output_v2.dart index b65dc4b23..a94c8dedb 100644 --- a/lib/models/isar/models/blockchain_data/v2/output_v2.dart +++ b/lib/models/isar/models/blockchain_data/v2/output_v2.dart @@ -9,6 +9,8 @@ class OutputV2 { late final String valueStringSats; late final List addresses; + late final bool walletOwns; + @ignore BigInt get value => BigInt.parse(valueStringSats); @@ -18,14 +20,31 @@ class OutputV2 { required String scriptPubKeyHex, required String valueStringSats, required List addresses, + required bool walletOwns, }) => OutputV2() ..scriptPubKeyHex = scriptPubKeyHex ..valueStringSats = valueStringSats + ..walletOwns = walletOwns ..addresses = List.unmodifiable(addresses); + OutputV2 copyWith({ + String? scriptPubKeyHex, + String? valueStringSats, + List? addresses, + bool? walletOwns, + }) { + return OutputV2.isarCantDoRequiredInDefaultConstructor( + scriptPubKeyHex: scriptPubKeyHex ?? this.scriptPubKeyHex, + valueStringSats: valueStringSats ?? this.valueStringSats, + addresses: addresses ?? this.addresses, + walletOwns: walletOwns ?? this.walletOwns, + ); + } + static OutputV2 fromElectrumXJson( Map json, { + required bool walletOwns, required int decimalPlaces, }) { try { @@ -46,6 +65,7 @@ class OutputV2 { decimalPlaces: decimalPlaces, ), addresses: addresses, + walletOwns: walletOwns, ); } catch (e) { throw Exception("Failed to parse OutputV2 from $json"); @@ -84,6 +104,7 @@ class OutputV2 { @override int get hashCode => Object.hash( scriptPubKeyHex, + addresses, valueStringSats, ); diff --git a/lib/services/coins/bitcoincash/bitcoincash_wallet.dart b/lib/services/coins/bitcoincash/bitcoincash_wallet.dart index 10bf44a49..88f59ec42 100644 --- a/lib/services/coins/bitcoincash/bitcoincash_wallet.dart +++ b/lib/services/coins/bitcoincash/bitcoincash_wallet.dart @@ -2099,6 +2099,7 @@ class BitcoinCashWallet extends CoinServiceAPI final prevOut = OutputV2.fromElectrumXJson( prevOutJson, decimalPlaces: coin.decimals, + walletOwns: false, // doesn't matter here as this is not saved ); outpoint = OutpointV2.isarCantDoRequiredInDefaultConstructor( @@ -2136,11 +2137,10 @@ class BitcoinCashWallet extends CoinServiceAPI final output = OutputV2.fromElectrumXJson( Map.from(outputJson as Map), decimalPlaces: coin.decimals, + // don't know yet if wallet owns. Need addresses first + walletOwns: false, ); - outputs.add(output); - } - for (final output in outputs) { // if output was to my wallet, add value to amount received if (receivingAddresses .intersection(output.addresses.toSet()) @@ -2153,6 +2153,10 @@ class BitcoinCashWallet extends CoinServiceAPI wasReceivedInThisWallet = true; changeAmountReceivedInThisWallet += output.value; } + + outputs.add(wasReceivedInThisWallet + ? output.copyWith(walletOwns: true) + : output); } final totalIn = inputs