convert cached electrumx into a simple wrapper

This commit is contained in:
julian 2023-05-25 14:27:36 -06:00
parent ba41094ed2
commit 7718a93059

View file

@ -4,43 +4,23 @@ import 'package:stackwallet/db/hive/db.dart';
import 'package:stackwallet/electrumx_rpc/electrumx.dart'; import 'package:stackwallet/electrumx_rpc/electrumx.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:string_validator/string_validator.dart'; import 'package:string_validator/string_validator.dart';
class CachedElectrumX { class CachedElectrumX {
final ElectrumX? electrumXClient; final ElectrumX electrumXClient;
final String server;
final int port;
final bool useSSL;
final Prefs prefs;
final List<ElectrumXNode> failovers;
static const minCacheConfirms = 30; static const minCacheConfirms = 30;
const CachedElectrumX({ const CachedElectrumX({
required this.server, required this.electrumXClient,
required this.port,
required this.useSSL,
required this.prefs,
required this.failovers,
this.electrumXClient,
}); });
factory CachedElectrumX.from({ factory CachedElectrumX.from({
required ElectrumXNode node, required ElectrumX electrumXClient,
required Prefs prefs,
required List<ElectrumXNode> failovers,
ElectrumX? electrumXClient,
}) => }) =>
CachedElectrumX( CachedElectrumX(
server: node.address, electrumXClient: electrumXClient,
port: node.port, );
useSSL: node.useSSL,
prefs: prefs,
failovers: failovers,
electrumXClient: electrumXClient);
Future<Map<String, dynamic>> getAnonymitySet({ Future<Map<String, dynamic>> getAnonymitySet({
required String groupId, required String groupId,
@ -66,16 +46,7 @@ class CachedElectrumX {
set = Map<String, dynamic>.from(cachedSet); set = Map<String, dynamic>.from(cachedSet);
} }
final client = electrumXClient ?? final newSet = await electrumXClient.getAnonymitySet(
ElectrumX(
host: server,
port: port,
useSSL: useSSL,
prefs: prefs,
failovers: failovers,
);
final newSet = await client.getAnonymitySet(
groupId: groupId, groupId: groupId,
blockhash: set["blockHash"] as String, blockhash: set["blockHash"] as String,
); );
@ -152,16 +123,8 @@ class CachedElectrumX {
final cachedTx = DB.instance.get<dynamic>( final cachedTx = DB.instance.get<dynamic>(
boxName: DB.instance.boxNameTxCache(coin: coin), key: txHash) as Map?; boxName: DB.instance.boxNameTxCache(coin: coin), key: txHash) as Map?;
if (cachedTx == null) { if (cachedTx == null) {
final client = electrumXClient ?? final Map<String, dynamic> result = await electrumXClient
ElectrumX( .getTransaction(txHash: txHash, verbose: verbose);
host: server,
port: port,
useSSL: useSSL,
prefs: prefs,
failovers: failovers,
);
final Map<String, dynamic> result =
await client.getTransaction(txHash: txHash, verbose: verbose);
result.remove("hex"); result.remove("hex");
result.remove("lelantusData"); result.remove("lelantusData");
@ -202,16 +165,8 @@ class CachedElectrumX {
final startNumber = cachedSerials.length; final startNumber = cachedSerials.length;
final client = electrumXClient ?? final serials =
ElectrumX( await electrumXClient.getUsedCoinSerials(startNumber: startNumber);
host: server,
port: port,
useSSL: useSSL,
prefs: prefs,
failovers: failovers,
);
final serials = await client.getUsedCoinSerials(startNumber: startNumber);
List<String> newSerials = []; List<String> newSerials = [];
for (final element in (serials["serials"] as List)) { for (final element in (serials["serials"] as List)) {