diff --git a/lib/electrumx_rpc/cached_electrumx.dart b/lib/electrumx_rpc/cached_electrumx.dart index a063ff00b..7a90c2343 100644 --- a/lib/electrumx_rpc/cached_electrumx.dart +++ b/lib/electrumx_rpc/cached_electrumx.dart @@ -1,5 +1,6 @@ import 'package:stackwallet/electrumx_rpc/electrumx.dart'; import 'package:stackwallet/hive/db.dart'; +import 'package:stackwallet/services/coins/firo/firo_wallet.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/prefs.dart'; @@ -59,6 +60,20 @@ class CachedElectrumX { "setHash": "", "coins": [], }; + + // try up to 3 times + for (int i = 0; i < 3; i++) { + final result = await getInitialAnonymitySetCache(groupId); + if (result != null) { + set["setHash"] = result["setHash"]; + set["blockHash"] = result["blockHash"]; + set["coins"] = result["coins"]; + Logging.instance.log( + "Populated initial anon set cache for group $groupId", + level: LogLevel.Info); + break; + } + } } else { set = Map.from(cachedSet); } diff --git a/lib/services/coins/firo/firo_wallet.dart b/lib/services/coins/firo/firo_wallet.dart index fb858b4ce..006655cf2 100644 --- a/lib/services/coins/firo/firo_wallet.dart +++ b/lib/services/coins/firo/firo_wallet.dart @@ -731,8 +731,10 @@ Future _setTestnetWrapper(bool isTestnet) async { // setTestnet(isTestnet); } -Future getAnonymity(int groupID) async { - Logging.instance.log("getAnonymity", level: LogLevel.Info); +Future?> getInitialAnonymitySetCache( + String groupID, +) async { + Logging.instance.log("getInitialAnonymitySetCache", level: LogLevel.Info); final Client client = Client(); try { final uri = Uri.parse("$kStackCommunityNodesEndpoint/getAnonymity"); @@ -743,26 +745,22 @@ Future getAnonymity(int groupID) async { body: jsonEncode({ "jsonrpc": "2.0", "id": "0", - 'aset': groupID.toString(), + 'aset': groupID, }), ); - // TODO: should the following be removed for security reasons in production? - Logging.instance - .log(anonSetResult.statusCode.toString(), level: LogLevel.Info); - Logging.instance.log(anonSetResult.body.toString(), level: LogLevel.Info); final response = jsonDecode(anonSetResult.body.toString()); if (response['status'] == 'success') { final anonResponse = jsonDecode(response['result'] as String); - Logging.instance.log(anonResponse, level: LogLevel.Info); - return response; + final setData = Map.from(anonResponse["result"] as Map); + return setData; } else { - return false; + return null; } } catch (e, s) { Logging.instance.log("$e $s", level: LogLevel.Error); - return false; + return null; } }