2023-05-26 21:21:16 +00:00
|
|
|
/*
|
|
|
|
* This file is part of Stack Wallet.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2023 Cypher Stack
|
|
|
|
* All Rights Reserved.
|
|
|
|
* The code is distributed under GPLv3 license, see LICENSE file for details.
|
|
|
|
* Generated by Cypher Stack on 2023-05-26
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-10-24 20:31:05 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-03-01 21:52:13 +00:00
|
|
|
import 'package:stackwallet/db/hive/db.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:stackwallet/electrumx_rpc/electrumx.dart';
|
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
2022-10-24 20:31:05 +00:00
|
|
|
import 'package:string_validator/string_validator.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
class CachedElectrumX {
|
2023-05-25 20:27:36 +00:00
|
|
|
final ElectrumX electrumXClient;
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
static const minCacheConfirms = 30;
|
|
|
|
|
|
|
|
const CachedElectrumX({
|
2023-05-25 20:27:36 +00:00
|
|
|
required this.electrumXClient,
|
2022-08-26 08:11:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
factory CachedElectrumX.from({
|
2023-05-25 20:27:36 +00:00
|
|
|
required ElectrumX electrumXClient,
|
2022-08-26 08:11:35 +00:00
|
|
|
}) =>
|
|
|
|
CachedElectrumX(
|
2023-05-25 20:27:36 +00:00
|
|
|
electrumXClient: electrumXClient,
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
Future<Map<String, dynamic>> getAnonymitySet({
|
|
|
|
required String groupId,
|
|
|
|
String blockhash = "",
|
|
|
|
required Coin coin,
|
|
|
|
}) async {
|
|
|
|
try {
|
2023-07-03 22:27:36 +00:00
|
|
|
final box = await DB.instance.getAnonymitySetCacheBox(coin: coin);
|
|
|
|
final cachedSet = box.get(groupId) as Map?;
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
Map<String, dynamic> set;
|
|
|
|
|
|
|
|
// null check to see if there is a cached set
|
|
|
|
if (cachedSet == null) {
|
|
|
|
set = {
|
|
|
|
"setId": groupId,
|
|
|
|
"blockHash": blockhash,
|
|
|
|
"setHash": "",
|
|
|
|
"coins": <dynamic>[],
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
set = Map<String, dynamic>.from(cachedSet);
|
|
|
|
}
|
|
|
|
|
2023-05-25 20:27:36 +00:00
|
|
|
final newSet = await electrumXClient.getAnonymitySet(
|
2022-08-26 08:11:35 +00:00
|
|
|
groupId: groupId,
|
|
|
|
blockhash: set["blockHash"] as String,
|
|
|
|
);
|
|
|
|
|
|
|
|
// update set with new data
|
|
|
|
if (newSet["setHash"] != "" && set["setHash"] != newSet["setHash"]) {
|
2022-10-24 20:31:05 +00:00
|
|
|
set["setHash"] = !isHexadecimal(newSet["setHash"] as String)
|
2022-12-14 15:05:47 +00:00
|
|
|
? base64ToHex(newSet["setHash"] as String)
|
2022-10-24 20:31:05 +00:00
|
|
|
: newSet["setHash"];
|
|
|
|
set["blockHash"] = !isHexadecimal(newSet["blockHash"] as String)
|
2022-12-14 15:05:47 +00:00
|
|
|
? base64ToReverseHex(newSet["blockHash"] as String)
|
2022-10-24 20:31:05 +00:00
|
|
|
: newSet["blockHash"];
|
2022-08-26 08:11:35 +00:00
|
|
|
for (int i = (newSet["coins"] as List).length - 1; i >= 0; i--) {
|
2022-10-24 20:31:05 +00:00
|
|
|
dynamic newCoin = newSet["coins"][i];
|
2023-07-03 22:27:36 +00:00
|
|
|
List<dynamic> translatedCoin = [];
|
2022-10-24 20:31:05 +00:00
|
|
|
translatedCoin.add(!isHexadecimal(newCoin[0] as String)
|
|
|
|
? base64ToHex(newCoin[0] as String)
|
|
|
|
: newCoin[0]);
|
|
|
|
translatedCoin.add(!isHexadecimal(newCoin[1] as String)
|
|
|
|
? base64ToReverseHex(newCoin[1] as String)
|
|
|
|
: newCoin[1]);
|
|
|
|
try {
|
|
|
|
translatedCoin.add(!isHexadecimal(newCoin[2] as String)
|
|
|
|
? base64ToHex(newCoin[2] as String)
|
|
|
|
: newCoin[2]);
|
|
|
|
} catch (e, s) {
|
|
|
|
translatedCoin.add(newCoin[2]);
|
|
|
|
}
|
|
|
|
translatedCoin.add(!isHexadecimal(newCoin[3] as String)
|
|
|
|
? base64ToReverseHex(newCoin[3] as String)
|
|
|
|
: newCoin[3]);
|
|
|
|
set["coins"].insert(0, translatedCoin);
|
2022-08-26 08:11:35 +00:00
|
|
|
}
|
|
|
|
// save set to db
|
2023-07-03 22:27:36 +00:00
|
|
|
await box.put(groupId, set);
|
2022-08-26 08:11:35 +00:00
|
|
|
Logging.instance.log(
|
2023-05-16 17:04:45 +00:00
|
|
|
"Updated current anonymity set for ${coin.name} with group ID $groupId",
|
|
|
|
level: LogLevel.Info,
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return set;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"Failed to process CachedElectrumX.getAnonymitySet(): $e\n$s",
|
|
|
|
level: LogLevel.Error);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 20:31:05 +00:00
|
|
|
String base64ToHex(String source) =>
|
|
|
|
base64Decode(LineSplitter.split(source).join())
|
|
|
|
.map((e) => e.toRadixString(16).padLeft(2, '0'))
|
|
|
|
.join();
|
|
|
|
|
|
|
|
String base64ToReverseHex(String source) =>
|
|
|
|
base64Decode(LineSplitter.split(source).join())
|
|
|
|
.reversed
|
|
|
|
.map((e) => e.toRadixString(16).padLeft(2, '0'))
|
|
|
|
.join();
|
|
|
|
|
2022-08-26 08:11:35 +00:00
|
|
|
/// Call electrumx getTransaction on a per coin basis, storing the result in local db if not already there.
|
|
|
|
///
|
|
|
|
/// ElectrumX api only called if the tx does not exist in local db
|
|
|
|
Future<Map<String, dynamic>> getTransaction({
|
|
|
|
required String txHash,
|
|
|
|
required Coin coin,
|
|
|
|
bool verbose = true,
|
|
|
|
}) async {
|
|
|
|
try {
|
2023-07-03 22:27:36 +00:00
|
|
|
final box = await DB.instance.getTxCacheBox(coin: coin);
|
|
|
|
|
|
|
|
final cachedTx = box.get(txHash) as Map?;
|
2022-08-26 08:11:35 +00:00
|
|
|
if (cachedTx == null) {
|
2023-05-25 20:27:36 +00:00
|
|
|
final Map<String, dynamic> result = await electrumXClient
|
|
|
|
.getTransaction(txHash: txHash, verbose: verbose);
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
result.remove("hex");
|
|
|
|
result.remove("lelantusData");
|
|
|
|
|
|
|
|
if (result["confirmations"] != null &&
|
|
|
|
result["confirmations"] as int > minCacheConfirms) {
|
2023-07-03 22:27:36 +00:00
|
|
|
await box.put(txHash, result);
|
2022-08-26 08:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Logging.instance.log("using fetched result", level: LogLevel.Info);
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
Logging.instance.log("using cached result", level: LogLevel.Info);
|
|
|
|
return Map<String, dynamic>.from(cachedTx);
|
|
|
|
}
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"Failed to process CachedElectrumX.getTransaction(): $e\n$s",
|
|
|
|
level: LogLevel.Error);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 19:29:48 +00:00
|
|
|
Future<List<String>> getUsedCoinSerials({
|
2022-08-26 08:11:35 +00:00
|
|
|
required Coin coin,
|
|
|
|
int startNumber = 0,
|
|
|
|
}) async {
|
|
|
|
try {
|
2023-07-03 22:27:36 +00:00
|
|
|
final box = await DB.instance.getUsedSerialsCacheBox(coin: coin);
|
|
|
|
|
|
|
|
final _list = box.get("serials") as List?;
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2023-07-26 02:28:27 +00:00
|
|
|
Set<String> cachedSerials =
|
|
|
|
_list == null ? {} : List<String>.from(_list).toSet();
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2023-07-26 02:28:27 +00:00
|
|
|
// startNumber is broken currently
|
|
|
|
final startNumber = 0; // cachedSerials.length;
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2023-07-26 02:28:27 +00:00
|
|
|
final serials = await electrumXClient.getUsedCoinSerials(
|
|
|
|
startNumber: startNumber,
|
|
|
|
);
|
|
|
|
Set<String> newSerials = {};
|
2023-05-16 19:29:48 +00:00
|
|
|
|
|
|
|
for (final element in (serials["serials"] as List)) {
|
2022-10-24 20:31:05 +00:00
|
|
|
if (!isHexadecimal(element as String)) {
|
|
|
|
newSerials.add(base64ToHex(element));
|
|
|
|
} else {
|
|
|
|
newSerials.add(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cachedSerials.addAll(newSerials);
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2023-07-26 02:28:27 +00:00
|
|
|
final resultingList = cachedSerials.toList();
|
|
|
|
|
2023-07-03 22:27:36 +00:00
|
|
|
await box.put(
|
|
|
|
"serials",
|
2023-07-26 02:28:27 +00:00
|
|
|
resultingList,
|
2023-05-16 19:29:48 +00:00
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
|
2023-07-26 02:28:27 +00:00
|
|
|
return resultingList;
|
2022-08-26 08:11:35 +00:00
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"Failed to process CachedElectrumX.getTransaction(): $e\n$s",
|
|
|
|
level: LogLevel.Error);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear all cached transactions for the specified coin
|
|
|
|
Future<void> clearSharedTransactionCache({required Coin coin}) async {
|
2023-07-03 22:27:36 +00:00
|
|
|
await DB.instance.closeAnonymitySetCacheBox(coin: coin);
|
2022-08-26 08:11:35 +00:00
|
|
|
}
|
|
|
|
}
|