Added ability sending to FIO address (#370)

* Added ability sending to FIO address

* remove apiKey property

* fix conditions for FIO address checking
This commit is contained in:
Serhii 2022-06-13 14:41:46 +03:00 committed by GitHub
parent 427efb44fa
commit fc58972bf9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 1 deletions

View file

@ -0,0 +1,57 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
class FioAddressProvider {
static const apiAuthority = 'fio.blockpane.com';
static const availCheck = '/v1/chain/avail_check';
static const getAddress = '/v1/chain/get_pub_address';
static Future<bool> checkAvail(String fioAddress) async {
bool isFioRegistered = false;
final headers = {'Content-Type': 'application/json'};
final body = <String, String>{"fio_name": fioAddress};
final uri = Uri.https(apiAuthority, availCheck);
final response =
await http.post(uri, headers: headers, body: json.encode(body));
if (response.statusCode != 200) {
return isFioRegistered;
}
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
isFioRegistered = responseJSON['is_registered'] as int == 1;
return isFioRegistered;
}
static Future<String> getPubAddress(String fioAddress, String token) async {
final headers = {'Content-Type': 'application/json'};
final body = <String, String>{
"fio_address": fioAddress,
"chain_code": token.toUpperCase(),
"token_code": token.toUpperCase(),
};
final uri = Uri.https(apiAuthority, getAddress);
final response =
await http.post(uri, headers: headers, body: json.encode(body));
if (response.statusCode == 400) {
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final error = responseJSON['error'] as String;
final message = responseJSON['message'] as String;
throw Exception('${error}\n$message');
}
if (response.statusCode != 200) {
return null;
}
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final String pubAddress = responseJSON['public_address'] as String;
return pubAddress;
}
}

View file

@ -4,6 +4,7 @@ import 'package:cake_wallet/entities/parsed_address.dart';
import 'package:cake_wallet/entities/unstoppable_domain_address.dart';
import 'package:cake_wallet/entities/emoji_string_extension.dart';
import 'package:flutter/foundation.dart';
import 'package:cake_wallet/entities/fio_address_provider.dart';
class AddressResolver {
@ -26,6 +27,14 @@ class AddressResolver {
Future<ParsedAddress> resolve(String text, String ticker) async {
try {
if (text.contains('@') && !text.contains('.')) {
final bool isFioRegistered = await FioAddressProvider.checkAvail(text);
if (isFioRegistered) {
final address = await FioAddressProvider.getPubAddress(text, ticker);
return ParsedAddress.fetchFioAddress(address: address, name: text);
}
}
if (text.hasOnlyEmojis) {
final addresses = await yatService.fetchYatAddress(text, ticker);
return ParsedAddress.fetchEmojiAddress(addresses: addresses, name: text);

View file

@ -2,7 +2,7 @@ import 'package:cake_wallet/entities/openalias_record.dart';
import 'package:cake_wallet/entities/yat_record.dart';
import 'package:flutter/material.dart';
enum ParseFrom { unstoppableDomains, openAlias, yatRecord, notParsed }
enum ParseFrom { unstoppableDomains, openAlias, yatRecord, fio, notParsed }
class ParsedAddress {
ParsedAddress({
@ -58,4 +58,13 @@ class ParsedAddress {
parseFrom: ParseFrom.openAlias,
);
}
factory ParsedAddress.fetchFioAddress({@required String address, @required String name}){
return ParsedAddress(
addresses: [address],
name: name,
parseFrom: ParseFrom.fio,
);
}
}

View file

@ -23,6 +23,11 @@ Future<String> extractAddressFromParsed(
content = S.of(context).openalias_alert_content(parsedAddress.name);
address = parsedAddress.addresses.first;
break;
case ParseFrom.fio:
title = S.of(context).address_detected;
content = S.of(context).openalias_alert_content(parsedAddress.name);
address = parsedAddress.addresses.first;
break;
case ParseFrom.yatRecord:
if (parsedAddress.name.isEmpty) {
title = S.of(context).yat_error;