diff --git a/lib/wallets/models/tx_data.dart b/lib/wallets/models/tx_data.dart index f8b3f6803..22101003c 100644 --- a/lib/wallets/models/tx_data.dart +++ b/lib/wallets/models/tx_data.dart @@ -5,6 +5,7 @@ import 'package:stackwallet/models/isar/models/isar_models.dart'; import 'package:stackwallet/models/paynym/paynym_account_lite.dart'; import 'package:stackwallet/utilities/amount/amount.dart'; import 'package:stackwallet/utilities/enums/fee_rate_type_enum.dart'; +import 'package:stackwallet/wallets/isar/models/spark_coin.dart'; import 'package:tezart/tezart.dart' as tezart; import 'package:web3dart/web3dart.dart' as web3dart; @@ -69,6 +70,7 @@ class TxData { bool isChange, })>? sparkRecipients; final List? sparkMints; + final List? usedSparkCoins; final TransactionV2? tempTx; @@ -105,6 +107,7 @@ class TxData { this.tezosOperationsList, this.sparkRecipients, this.sparkMints, + this.usedSparkCoins, this.tempTx, }); @@ -187,6 +190,7 @@ class TxData { })>? sparkRecipients, List? sparkMints, + List? usedSparkCoins, TransactionV2? tempTx, }) { return TxData( @@ -224,6 +228,7 @@ class TxData { tezosOperationsList: tezosOperationsList ?? this.tezosOperationsList, sparkRecipients: sparkRecipients ?? this.sparkRecipients, sparkMints: sparkMints ?? this.sparkMints, + usedSparkCoins: usedSparkCoins ?? this.usedSparkCoins, tempTx: tempTx ?? this.tempTx, ); } @@ -262,6 +267,7 @@ class TxData { 'tezosOperationsList: $tezosOperationsList, ' 'sparkRecipients: $sparkRecipients, ' 'sparkMints: $sparkMints, ' + 'usedSparkCoins: $usedSparkCoins, ' 'tempTx: $tempTx, ' '}'; } diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/spark_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/spark_interface.dart index b60e8c8a8..74848182e 100644 --- a/lib/wallets/wallet/wallet_mixin_interfaces/spark_interface.dart +++ b/lib/wallets/wallet/wallet_mixin_interfaces/spark_interface.dart @@ -499,6 +499,27 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface { ), ); + final List usedSparkCoins = []; + + for (final usedCoin in spend.usedCoins) { + try { + usedSparkCoins.add(coins + .firstWhere((e) => + usedCoin.height == e.height && + usedCoin.groupId == e.groupId && + base64Decode(e.serializedCoinB64!) + .toHex + .startsWith(base64Decode(usedCoin.serializedCoin).toHex)) + .copyWith( + isUsed: true, + )); + } catch (_) { + throw Exception( + "Unexpectedly did not find used spark coin. This should never happen.", + ); + } + } + return txData.copyWith( raw: rawTxHex, vSize: extractedTx.virtualSize(), @@ -523,7 +544,7 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface { height: null, version: 3, ), - // TODO used coins + usedSparkCoins: usedSparkCoins, ); } @@ -540,17 +561,17 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface { Logging.instance.log("Sent txHash: $txHash", level: LogLevel.Info); txData = txData.copyWith( - // TODO mark spark coins as spent locally and update balance before waiting to check via electrumx? - - // usedUTXOs: - // txData.usedUTXOs!.map((e) => e.copyWith(used: true)).toList(), - // TODO revisit setting these both txHash: txHash, txid: txHash, ); - // // mark utxos as used - // await mainDB.putUTXOs(txData.usedUTXOs!); + + // Update used spark coins as used in database. They should already have + // been marked as isUsed. + // TODO: [prio=med] Could (probably should) throw an exception here if txData.usedSparkCoins is null or empty + if (txData.usedSparkCoins != null && txData.usedSparkCoins!.isNotEmpty) { + await _addOrUpdateSparkCoins(txData.usedSparkCoins!); + } return await updateSentCachedTxData(txData: txData); } catch (e, s) { @@ -1499,6 +1520,13 @@ Future< Uint8List serializedSpendPayload, List outputScripts, int fee, + List< + ({ + int groupId, + int height, + String serializedCoin, + String serializedCoinContext + })> usedCoins, })> _createSparkSend( ({ String privateKeyHex, diff --git a/pubspec.lock b/pubspec.lock index 07ee80f19..423323325 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -674,8 +674,8 @@ packages: dependency: "direct main" description: path: "." - ref: fb50031056fbea0326f7dd76ad59d165c1e5eee5 - resolved-ref: fb50031056fbea0326f7dd76ad59d165c1e5eee5 + ref: "3f986ca1a94bdac5d31373454c989cc2f5842de8" + resolved-ref: "3f986ca1a94bdac5d31373454c989cc2f5842de8" url: "https://github.com/cypherstack/flutter_libsparkmobile.git" source: git version: "0.0.1" diff --git a/pubspec.yaml b/pubspec.yaml index 13b50ee5e..44dc98d34 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -30,7 +30,7 @@ dependencies: flutter_libsparkmobile: git: url: https://github.com/cypherstack/flutter_libsparkmobile.git - ref: fb50031056fbea0326f7dd76ad59d165c1e5eee5 + ref: 3f986ca1a94bdac5d31373454c989cc2f5842de8 flutter_libmonero: path: ./crypto_plugins/flutter_libmonero