diff --git a/cw_bitcoin/lib/litecoin_wallet.dart b/cw_bitcoin/lib/litecoin_wallet.dart index 8fa5af63b..547a2dda5 100644 --- a/cw_bitcoin/lib/litecoin_wallet.dart +++ b/cw_bitcoin/lib/litecoin_wallet.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'package:convert/convert.dart'; +import 'package:crypto/crypto.dart'; import 'package:bitcoin_base/bitcoin_base.dart'; import 'package:cw_bitcoin/bitcoin_mnemonic.dart'; import 'package:cw_bitcoin/bitcoin_transaction_priority.dart'; @@ -137,9 +138,20 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { syncStatus = SyncedSyncStatus(); } }); + processMwebUtxos(); + } + + final Map mwebUtxos = {}; + + Future processMwebUtxos() async { + final stub = await CwMweb.stub(); final scanSecret = mwebHd.derive(0x80000000).privKey!; final req = UtxosRequest(scanSecret: hex.decode(scanSecret)); await for (var utxo in stub.utxos(req)) { + final mwebAddrs = (walletAddresses as LitecoinWalletAddresses).mwebAddrs; + if (!mwebAddrs.contains(utxo.address)) continue; + mwebUtxos[utxo.outputId] = utxo; + final status = await stub.status(StatusRequest()); var date = DateTime.now(); var confirmations = 0; @@ -160,6 +172,44 @@ abstract class LitecoinWalletBase extends ElectrumWallet with Store { } } + Future checkMwebUtxosSpent() async { + final List outputIds = []; + mwebUtxos.forEach((outputId, utxo) { + if (utxo.height > 0) + outputIds.add(outputId); + }); + final stub = await CwMweb.stub(); + final resp = await stub.spent(SpentRequest(outputId: outputIds)); + final spent = resp.outputId; + if (spent.isEmpty) return; + final status = await stub.status(StatusRequest()); + final height = await electrumClient.getCurrentBlockChainTip(); + if (height == null || status.mwebUtxosHeight != height) return; + final date = await electrumClient.getBlockTime(height: height); + int amount = 0; + Set inputAddresses = {}; + var output = AccumulatorSink(); + var input = sha256.startChunkedConversion(output); + for (final outputId in spent) { + input.add(hex.decode(outputId)); + amount += mwebUtxos[outputId]!.value.toInt(); + inputAddresses.add(mwebUtxos[outputId]!.address); + mwebUtxos.remove(outputId); + } + input.close(); + var digest = output.events.single; + final tx = ElectrumTransactionInfo(WalletType.litecoin, + id: digest.toString(), height: height, + amount: amount, fee: 0, + direction: TransactionDirection.outgoing, + isPending: false, + date: date, confirmations: 1, + inputAddresses: inputAddresses.toList(), + outputAddresses: []); + transactionHistory.addOne(tx); + await transactionHistory.save(); + } + @override int feeRate(TransactionPriority priority) { if (priority is LitecoinTransactionPriority) {