find SparkCoins that correspond to the usedCoins returned from spark lib

instead of translating used coins to UTXOs, we find which SparkCoins in isar match the usedCoins returned from sparkmobile and update them as isUsed: true in db.
This commit is contained in:
sneurlax 2024-02-27 19:01:53 -06:00
parent 01881aae4f
commit 2ac1558266
2 changed files with 39 additions and 33 deletions

View file

@ -69,6 +69,13 @@ class TxData {
bool isChange,
})>? sparkRecipients;
final List<TxData>? sparkMints;
final List<
({
String serializedCoin,
String serializedCoinContext,
int groupId,
int height,
})>? usedCoins;
final TransactionV2? tempTx;
@ -105,6 +112,7 @@ class TxData {
this.tezosOperationsList,
this.sparkRecipients,
this.sparkMints,
this.usedCoins,
this.tempTx,
});
@ -187,6 +195,14 @@ class TxData {
})>?
sparkRecipients,
List<TxData>? sparkMints,
List<
({
String serializedCoin,
String serializedCoinContext,
int groupId,
int height,
})>?
usedCoins,
TransactionV2? tempTx,
}) {
return TxData(
@ -224,6 +240,7 @@ class TxData {
tezosOperationsList: tezosOperationsList ?? this.tezosOperationsList,
sparkRecipients: sparkRecipients ?? this.sparkRecipients,
sparkMints: sparkMints ?? this.sparkMints,
usedCoins: usedCoins ?? this.usedCoins,
tempTx: tempTx ?? this.tempTx,
);
}
@ -262,6 +279,7 @@ class TxData {
'tezosOperationsList: $tezosOperationsList, '
'sparkRecipients: $sparkRecipients, '
'sparkMints: $sparkMints, '
'usedCoins: $usedCoins, '
'tempTx: $tempTx, '
'}';
}

View file

@ -499,35 +499,6 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface {
),
);
// Find out which coins were used and translate them into UTXOs.
final usedUTXOs = coins.where((coin) {
return spend.usedCoins.any((usedCoin) {
return usedCoin.serializedCoin == coin.serializedCoinB64 &&
usedCoin.serializedCoinContext == coin.contextB64;
});
}).map((coin) {
return UTXO(
walletId: walletId,
txid: extractedTx.getId(),
vout: coin.groupId,
value: coin.value.toInt(),
name: '',
isBlocked: false, // true?
blockedReason: null, // "Used in Spark spend."?
isCoinbase: false,
blockHash: null,
blockHeight: coin.height,
blockTime: null,
address: null,
used: true,
otherData: jsonEncode((
groupId: coin.groupId,
serializedCoin: coin.serializedCoinB64,
serializedCoinContext: coin.contextB64,
)),
);
}).toList();
return txData.copyWith(
raw: rawTxHex,
vSize: extractedTx.virtualSize(),
@ -552,7 +523,7 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface {
height: null,
version: 3,
),
usedUTXOs: usedUTXOs,
usedCoins: spend.usedCoins,
);
}
@ -569,13 +540,30 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface {
Logging.instance.log("Sent txHash: $txHash", level: LogLevel.Info);
txData = txData.copyWith(
usedUTXOs: txData.usedUTXOs,
// TODO revisit setting these both
txHash: txHash,
txid: txHash,
);
// mark utxos as used
await mainDB.putUTXOs(txData.usedUTXOs!);
// Update coins as used in database.
final List<SparkCoin> usedCoins = [];
for (final usedCoin in txData.usedCoins!) {
// Find the SparkCoin that matches the usedCoin.
final sparkCoin = await mainDB.isar.sparkCoins
.where()
.walletIdEqualToAnyLTagHash(walletId)
.filter()
.serializedCoinB64EqualTo(usedCoin.serializedCoin)
.findFirst();
// Add the SparkCoin to usedCoins if it exists.
if (sparkCoin != null) {
usedCoins.add(sparkCoin.copyWith(isUsed: true));
}
}
// Update the SparkCoins in the database.
await _addOrUpdateSparkCoins(usedCoins);
return await updateSentCachedTxData(txData: txData);
} catch (e, s) {