update frost wallet for electrum_adapter functionality

pulled from electrumx_interface, might consider using those methods instead
This commit is contained in:
sneurlax 2024-02-23 17:46:34 -06:00
parent 7d5cc8d8be
commit 73276ba676

View file

@ -1,6 +1,8 @@
import 'dart:async'; import 'dart:async';
import 'dart:ffi'; import 'dart:ffi';
import 'package:electrum_adapter/electrum_adapter.dart' as electrum_adapter;
import 'package:electrum_adapter/electrum_adapter.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:frostdart/frostdart.dart' as frost; import 'package:frostdart/frostdart.dart' as frost;
import 'package:frostdart/frostdart_bindings_generated.dart'; import 'package:frostdart/frostdart_bindings_generated.dart';
@ -18,15 +20,19 @@ import 'package:stackwallet/models/paymint/fee_object_model.dart';
import 'package:stackwallet/services/event_bus/events/global/wallet_sync_status_changed_event.dart'; import 'package:stackwallet/services/event_bus/events/global/wallet_sync_status_changed_event.dart';
import 'package:stackwallet/services/event_bus/global_event_bus.dart'; import 'package:stackwallet/services/event_bus/global_event_bus.dart';
import 'package:stackwallet/services/frost.dart'; import 'package:stackwallet/services/frost.dart';
import 'package:stackwallet/services/tor_service.dart';
import 'package:stackwallet/utilities/amount/amount.dart'; import 'package:stackwallet/utilities/amount/amount.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/extensions/extensions.dart'; import 'package:stackwallet/utilities/extensions/extensions.dart';
import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:stackwallet/wallets/crypto_currency/coins/bitcoin_frost.dart'; import 'package:stackwallet/wallets/crypto_currency/coins/bitcoin_frost.dart';
import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart'; import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart';
import 'package:stackwallet/wallets/crypto_currency/intermediate/private_key_currency.dart'; import 'package:stackwallet/wallets/crypto_currency/intermediate/private_key_currency.dart';
import 'package:stackwallet/wallets/isar/models/frost_wallet_info.dart'; import 'package:stackwallet/wallets/isar/models/frost_wallet_info.dart';
import 'package:stackwallet/wallets/models/tx_data.dart'; import 'package:stackwallet/wallets/models/tx_data.dart';
import 'package:stackwallet/wallets/wallet/wallet.dart'; import 'package:stackwallet/wallets/wallet/wallet.dart';
import 'package:stream_channel/stream_channel.dart';
class BitcoinFrostWallet<T extends FrostCurrency> extends Wallet<T> { class BitcoinFrostWallet<T extends FrostCurrency> extends Wallet<T> {
BitcoinFrostWallet(CryptoCurrencyNetwork network) BitcoinFrostWallet(CryptoCurrencyNetwork network)
@ -38,6 +44,8 @@ class BitcoinFrostWallet<T extends FrostCurrency> extends Wallet<T> {
.findFirstSync()!; .findFirstSync()!;
late ElectrumXClient electrumXClient; late ElectrumXClient electrumXClient;
late StreamChannel electrumAdapterChannel;
late ElectrumClient electrumAdapterClient;
late CachedElectrumXClient electrumXCachedClient; late CachedElectrumXClient electrumXCachedClient;
Future<void> initializeNewFrost({ Future<void> initializeNewFrost({
@ -1075,6 +1083,7 @@ class BitcoinFrostWallet<T extends FrostCurrency> extends Wallet<T> {
); );
} }
// TODO [prio=low]: Use ElectrumXInterface method.
Future<void> _updateElectrumX() async { Future<void> _updateElectrumX() async {
final failovers = nodeService final failovers = nodeService
.failoverNodesFor(coin: cryptoCurrency.coin) .failoverNodesFor(coin: cryptoCurrency.coin)
@ -1088,16 +1097,67 @@ class BitcoinFrostWallet<T extends FrostCurrency> extends Wallet<T> {
.toList(); .toList();
final newNode = await _getCurrentElectrumXNode(); final newNode = await _getCurrentElectrumXNode();
try {
await electrumXClient.electrumAdapterClient?.close();
} catch (e, s) {
if (e.toString().contains("initialized")) {
// Ignore. This should happen every first time the wallet is opened.
} else {
Logging.instance
.log("Error closing electrumXClient: $e", level: LogLevel.Error);
}
}
electrumXClient = ElectrumXClient.from( electrumXClient = ElectrumXClient.from(
node: newNode, node: newNode,
prefs: prefs, prefs: prefs,
failovers: failovers, failovers: failovers,
coin: cryptoCurrency.coin,
); );
electrumAdapterChannel = await electrum_adapter.connect(
newNode.address,
port: newNode.port,
acceptUnverified: true,
useSSL: newNode.useSSL,
proxyInfo: Prefs.instance.useTor
? TorService.sharedInstance.getProxyInfo()
: null,
);
if (electrumXClient.coin == Coin.firo ||
electrumXClient.coin == Coin.firoTestNet) {
electrumAdapterClient = FiroElectrumClient(
electrumAdapterChannel,
newNode.address,
newNode.port,
newNode.useSSL,
Prefs.instance.useTor
? TorService.sharedInstance.getProxyInfo()
: null);
} else {
electrumAdapterClient = ElectrumClient(
electrumAdapterChannel,
newNode.address,
newNode.port,
newNode.useSSL,
Prefs.instance.useTor
? TorService.sharedInstance.getProxyInfo()
: null);
}
electrumXCachedClient = CachedElectrumXClient.from( electrumXCachedClient = CachedElectrumXClient.from(
electrumXClient: electrumXClient, electrumXClient: electrumXClient,
electrumAdapterClient: electrumAdapterClient,
electrumAdapterUpdateCallback: updateClient,
); );
} }
// TODO [prio=low]: Use ElectrumXInterface method.
Future<ElectrumClient> updateClient() async {
Logging.instance.log(
"Updating electrum node and ElectrumAdapterClient from Frost wallet.",
level: LogLevel.Info);
await updateNode();
return electrumAdapterClient;
}
bool _duplicateTxCheck( bool _duplicateTxCheck(
List<Map<String, dynamic>> allTransactions, String txid) { List<Map<String, dynamic>> allTransactions, String txid) {
for (int i = 0; i < allTransactions.length; i++) { for (int i = 0; i < allTransactions.length; i++) {