add wallet owns flag to outputv2

This commit is contained in:
julian 2023-10-20 10:53:31 -06:00
parent dc64a06698
commit 5b9ade5f15
2 changed files with 28 additions and 3 deletions

View file

@ -9,6 +9,8 @@ class OutputV2 {
late final String valueStringSats;
late final List<String> 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<String> addresses,
required bool walletOwns,
}) =>
OutputV2()
..scriptPubKeyHex = scriptPubKeyHex
..valueStringSats = valueStringSats
..walletOwns = walletOwns
..addresses = List.unmodifiable(addresses);
OutputV2 copyWith({
String? scriptPubKeyHex,
String? valueStringSats,
List<String>? 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<String, dynamic> 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,
);

View file

@ -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<String, dynamic>.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