From c640d3e4cccd5daff480d60021b4c6fadb75f603 Mon Sep 17 00:00:00 2001 From: julian Date: Thu, 21 Dec 2023 16:18:12 -0600 Subject: [PATCH] run createSparkSend in isolate --- .../spark_interface.dart | 113 ++++++++++++++---- 1 file changed, 87 insertions(+), 26 deletions(-) diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/spark_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/spark_interface.dart index e38cf0f73..20bba6013 100644 --- a/lib/wallets/wallet/wallet_mixin_interfaces/spark_interface.dart +++ b/lib/wallets/wallet/wallet_mixin_interfaces/spark_interface.dart @@ -304,32 +304,36 @@ mixin SparkInterface on Bip39HDWallet, ElectrumXInterface { ); extractedTx.setPayload(Uint8List(0)); - final spend = LibSpark.createSparkSendTransaction( - privateKeyHex: privateKey.toHex, - index: kDefaultSparkIndex, - recipients: txData.recipients - ?.map((e) => ( - address: e.address, - amount: e.amount.raw.toInt(), - subtractFeeFromAmount: isSendAll, - )) - .toList() ?? - [], - privateRecipients: txData.sparkRecipients - ?.map((e) => ( - sparkAddress: e.address, - amount: e.amount.raw.toInt(), - subtractFeeFromAmount: isSendAll, - memo: e.memo, - )) - .toList() ?? - [], - serializedCoins: serializedCoins, - allAnonymitySets: allAnonymitySets, - idAndBlockHashes: idAndBlockHashes - .map((e) => (setId: e.groupId, blockHash: base64Decode(e.blockHash))) - .toList(), - txHash: extractedTx.getHash(), + final spend = await compute( + _createSparkSend, + ( + privateKeyHex: privateKey.toHex, + index: kDefaultSparkIndex, + recipients: txData.recipients + ?.map((e) => ( + address: e.address, + amount: e.amount.raw.toInt(), + subtractFeeFromAmount: isSendAll, + )) + .toList() ?? + [], + privateRecipients: txData.sparkRecipients + ?.map((e) => ( + sparkAddress: e.address, + amount: e.amount.raw.toInt(), + subtractFeeFromAmount: isSendAll, + memo: e.memo, + )) + .toList() ?? + [], + serializedCoins: serializedCoins, + allAnonymitySets: allAnonymitySets, + idAndBlockHashes: idAndBlockHashes + .map( + (e) => (setId: e.groupId, blockHash: base64Decode(e.blockHash))) + .toList(), + txHash: extractedTx.getHash(), + ), ); for (final outputScript in spend.outputScripts) { @@ -853,6 +857,63 @@ String base64ToReverseHex(String source) => .map((e) => e.toRadixString(16).padLeft(2, '0')) .join(); +/// Top level function which should be called wrapped in [compute] +Future< + ({ + Uint8List serializedSpendPayload, + List outputScripts, + int fee, + })> _createSparkSend( + ({ + String privateKeyHex, + int index, + List< + ({ + String address, + int amount, + bool subtractFeeFromAmount + })> recipients, + List< + ({ + String sparkAddress, + int amount, + bool subtractFeeFromAmount, + String memo + })> privateRecipients, + List< + ({ + String serializedCoin, + String serializedCoinContext, + int groupId, + int height, + })> serializedCoins, + List< + ({ + int setId, + String setHash, + List<({String serializedCoin, String txHash})> set + })> allAnonymitySets, + List< + ({ + int setId, + Uint8List blockHash, + })> idAndBlockHashes, + Uint8List txHash, + }) args) async { + final spend = LibSpark.createSparkSendTransaction( + privateKeyHex: args.privateKeyHex, + index: args.index, + recipients: args.recipients, + privateRecipients: args.privateRecipients, + serializedCoins: args.serializedCoins, + allAnonymitySets: args.allAnonymitySets, + idAndBlockHashes: args.idAndBlockHashes, + txHash: args.txHash, + ); + + return spend; +} + /// Top level function which should be called wrapped in [compute] Future> _identifyCoins( ({