| 1 | import 'dart:convert'; |
| 2 | |
| 3 | import 'package:cake_wallet/core/address_resolver/yat/yat_record.dart'; |
| 4 | import 'package:cw_core/utils/proxy_wrapper.dart'; |
| 5 | |
| 6 | class YatService { |
| 7 | static bool isDevMode = false; |
| 8 | |
| 9 | static String get apiUrl => |
| 10 | YatService.isDevMode ? YatService.apiDevUrl : YatService.apiReleaseUrl; |
| 11 | static const apiReleaseUrl = "https://a.y.at"; |
| 12 | static const apiDevUrl = 'https://a.yat.fyi'; |
| 13 | |
| 14 | static String lookupEmojiUrl(String emojiId) => "$apiUrl/emoji_id/$emojiId/payment"; |
| 15 | |
| 16 | static const String MONERO_SUB_ADDRESS = '0x1002'; |
| 17 | static const String MONERO_STD_ADDRESS = '0x1001'; |
| 18 | static const tags = { |
| 19 | 'XMR': "$MONERO_STD_ADDRESS,$MONERO_SUB_ADDRESS", |
| 20 | 'BTC': '0x1003', |
| 21 | 'LTC': '0x1019' |
| 22 | }; |
| 23 | |
| 24 | static Future<List<YatRecord>> fetchYatAddress( |
| 25 | String emojiId, |
| 26 | String ticker, |
| 27 | ) async { |
| 28 | final tagQuery = tags[ticker.toUpperCase()]; |
| 29 | if (tagQuery == null) return const []; |
| 30 | |
| 31 | final uri = Uri.parse( |
| 32 | lookupEmojiUrl(emojiId.replaceAll(' ', '')), |
| 33 | ).replace(queryParameters: {'tags': tagQuery}); |
| 34 | |
| 35 | try { |
| 36 | final response = await ProxyWrapper().get(clearnetUri: uri); |
| 37 | final body = json.decode(response.body) as Map<String, dynamic>; |
| 38 | |
| 39 | final res = body['result']; |
| 40 | if (res == null) return const []; |
| 41 | |
| 42 | final records = <YatRecord>[]; |
| 43 | |
| 44 | if (res is Map) { |
| 45 | res.forEach((tag, data) { |
| 46 | if (data is Map<String, dynamic>) { |
| 47 | records.add(YatRecord.fromJson(data, tag.toString())); |
| 48 | } |
| 49 | }); |
| 50 | } else if (res is List) { |
| 51 | for (final item in res) { |
| 52 | if (item is Map<String, dynamic>) { |
| 53 | final tag = item['tag']?.toString() ?? ''; |
| 54 | records.add(YatRecord.fromJson(item, tag)); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return records; |
| 60 | } catch (_) { |
| 61 | return const []; |
| 62 | } |
| 63 | } |
| 64 | } |