From 01de393521f3eed4c6143e6c901312fc745d6f84 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Thu, 25 Apr 2024 15:35:04 -0500 Subject: [PATCH] fetch paynym bot image over Tor hard to test, as I cannot claim/generate nor detect a previously-claimed/generated nym over Tor due to ``` flutter: Log: [Info][2024-04-25 20:28:22.609Z]: HTTP.post() rethrew: Exception: Command handling failed. With error: serverError flutter: #0 SocksSocket._handleCommandResponse (package:socks5_proxy/src/client/socks_client.dart:196:7) flutter: flutter: #1 SocksSocket.initialize (package:socks5_proxy/src/client/socks_client.dart:74:22) flutter: flutter: #2 SocksTCPClient.connect (package:socks5_proxy/src/client/socks_tcp_client.dart:70:20) flutter: flutter: #3 SocksTCPClient.assignToHttpClientWithSecureOptions. (package:socks5_proxy/src/client/socks_tcp_client.dart:46:40) flutter: flutter: #4 _ConnectionTarget.connect. (dart:_http/http_impl.dart:2490:32) flutter: flutter: #5 _HttpClient._openUrl. (dart:_http/http_impl.dart:2787:15) flutter: flutter: #6 HTTP.post (package:stackwallet/networking/http.dart:85:41) flutter: flutter: #7 PaynymIsApi._post (package:stackwallet/utilities/paynym_is_api.dart:54:22) flutter: flutter: #8 PaynymIsApi.nym (package:stackwallet/utilities/paynym_is_api.dart:267:22) flutter: flutter: #9 Wallet.refresh (package:stackwallet/wallets/wallet/wallet.dart:511:21) flutter: ``` --- lib/pages/paynym/subwidgets/paynym_bot.dart | 43 ++++++++++++++++----- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/lib/pages/paynym/subwidgets/paynym_bot.dart b/lib/pages/paynym/subwidgets/paynym_bot.dart index d8f645da3..082f7034e 100644 --- a/lib/pages/paynym/subwidgets/paynym_bot.dart +++ b/lib/pages/paynym/subwidgets/paynym_bot.dart @@ -8,8 +8,12 @@ * */ +import 'dart:typed_data'; + import 'package:flutter/material.dart'; -import 'package:stackwallet/widgets/loading_indicator.dart'; +import 'package:stackwallet/networking/http.dart'; +import 'package:stackwallet/services/tor_service.dart'; +import 'package:stackwallet/utilities/prefs.dart'; class PayNymBot extends StatelessWidget { const PayNymBot({ @@ -28,16 +32,37 @@ class PayNymBot extends StatelessWidget { child: SizedBox( width: size, height: size, - child: Image.network( - "https://paynym.is/$paymentCodeString/avatar", - loadingBuilder: (context, child, loadingProgress) => - loadingProgress == null - ? child - : const Center( - child: LoadingIndicator(), - ), + child: FutureBuilder( + future: _fetchImage(), + builder: (context, snapshot) { + if (snapshot.hasData) { + return Image.memory(snapshot.data!); + } else if (snapshot.hasError) { + return const Center(child: Icon(Icons.error)); + } else { + return const Center(); // TODO [prio=low]: Make better loading indicator. + } + }, ), ), ); } + + Future _fetchImage() async { + final HTTP client = HTTP(); + final Uri uri = Uri.parse("https://paynym.is/$paymentCodeString/avatar"); + + final response = await client.get( + url: uri, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.getProxyInfo() + : null, + ); + + if (response.code == 200) { + return Uint8List.fromList(response.bodyBytes); + } else { + throw Exception('Failed to load image'); + } + } }