Cw 45 implement yat sending (#269)

* resolve YAT emoji

* remove animation in route builder

change YAT api

* remove yat sending page

* fix crypto address resolving

* check if text is emoji

* use getter for string extension hasOnlyEmojis

* refactor parsed domain from address

* update PR based on changes from code review

* import missing dependencies
This commit is contained in:
Godwin Asuquo 2022-03-15 10:11:53 +01:00 committed by GitHub
parent 23913c01dc
commit d2cc812884
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 178 additions and 363 deletions

47
lib/core/yat_service.dart Normal file
View file

@ -0,0 +1,47 @@
import 'dart:convert';
import 'package:cake_wallet/entities/yat_record.dart';
import 'package:http/http.dart';
class YatService {
static bool isDevMode = false;
static String get apiUrl =>
YatService.isDevMode ? YatService.apiDevUrl : YatService.apiReleaseUrl;
static const apiReleaseUrl = "https://a.y.at";
static const apiDevUrl = 'https://yat.fyi';
static String lookupEmojiUrl(String emojiId) =>
"$apiUrl/emoji_id/$emojiId/payment";
static const tags = {
'XMR': '0x1001,0x1002',
'BTC': '0x1003',
'LTC': '0x3fff'
};
Future<List<YatRecord>> fetchYatAddress(String emojiId, String ticker) async {
final formattedTicker = ticker.toUpperCase();
final formattedEmojiId = emojiId.replaceAll(' ', '');
final uri = Uri.parse(lookupEmojiUrl(formattedEmojiId)).replace(
queryParameters: <String, dynamic>{
"tags": tags[formattedTicker]
});
final yatRecords = <YatRecord>[];
try {
final response = await get(uri);
final resBody = json.decode(response.body) as Map<String, dynamic>;
final results = resBody["result"] as Map<dynamic, dynamic>;
results.forEach((dynamic key, dynamic value) {
yatRecords.add(YatRecord.fromJson(value as Map<String, dynamic>));
});
return yatRecords;
} catch (_) {
return yatRecords;
}
}
}

View file

@ -1,3 +1,5 @@
import 'package:cake_wallet/core/yat_service.dart';
import 'package:cake_wallet/entities/parse_address_from_domain.dart';
import 'package:cake_wallet/entities/wake_lock.dart';
import 'package:cake_wallet/monero/monero.dart';
import 'package:cake_wallet/bitcoin/bitcoin.dart';
@ -623,5 +625,9 @@ Future setup(
getIt.registerFactory(() => WakeLock());
getIt.registerFactory(() => YatService());
getIt.registerFactory(() => AddressResolver(yatService: getIt.get<YatService>()));
_isSetupFinished = true;
}

View file

@ -0,0 +1,25 @@
import 'package:flutter/material.dart';
extension Emoji on String {
static final REGEX_EMOJI = RegExp(
r'[\u{1f300}-\u{1f5ff}\u{1f900}-\u{1f9ff}\u{1f600}-\u{1f64f}'
r'\u{1f680}-\u{1f6ff}\u{2600}-\u{26ff}\u{2700}'
r'-\u{27bf}\u{1f1e6}-\u{1f1ff}\u{1f191}-\u{1f251}'
r'\u{1f004}\u{1f0cf}\u{1f170}-\u{1f171}\u{1f17e}'
r'-\u{1f17f}\u{1f18e}\u{3030}\u{2b50}\u{2b55}'
r'\u{2934}-\u{2935}\u{2b05}-\u{2b07}\u{2b1b}'
r'-\u{2b1c}\u{3297}\u{3299}\u{303d}\u{00a9}'
r'\u{00ae}\u{2122}\u{23f3}\u{24c2}\u{23e9}'
r'-\u{23ef}\u{25b6}\u{23f8}-\u{23fa}\u{200d}]+',
unicode: true,
);
bool _hasOnlyEmojis() {
final parsedText = this.replaceAll(' ', '');
for (final c in Characters(parsedText))
if (!REGEX_EMOJI.hasMatch(c)) return false;
return true;
}
/// Returns true if the given text contains only emojis.
bool get hasOnlyEmojis => _hasOnlyEmojis();
}

View file

@ -1,10 +1,17 @@
import 'package:cake_wallet/core/yat_service.dart';
import 'package:cake_wallet/entities/openalias_record.dart';
import 'package:cake_wallet/entities/parsed_address.dart';
import 'package:cake_wallet/entities/unstoppable_domain_address.dart';
import 'package:cake_wallet/store/yat/yat_store.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:cake_wallet/entities/emoji_string_extension.dart';
import 'package:flutter/foundation.dart';
const unstoppableDomains = [
class AddressResolver {
AddressResolver({@required this.yatService});
final YatService yatService;
static const unstoppableDomains = [
'crypto',
'zil',
'x',
@ -17,57 +24,33 @@ const unstoppableDomains = [
'blockchain'
];
Future<ParsedAddress> parseAddressFromDomain(
String domain, String ticker) async {
try {
final formattedName = OpenaliasRecord.formatDomainName(domain);
final domainParts = formattedName.split('.');
final name = domainParts.last;
if (domainParts.length <= 1 || domainParts.first.isEmpty || name.isEmpty) {
try {
final addresses = await fetchYatAddress(domain, ticker);
if (addresses?.isEmpty ?? true) {
return ParsedAddress(
addresses: [domain], parseFrom: ParseFrom.yatRecord);
}
return ParsedAddress(
addresses: addresses, name: domain, parseFrom: ParseFrom.yatRecord);
} catch (e) {
return ParsedAddress(addresses: [domain]);
Future<ParsedAddress> resolve(String text, String ticker) async {
try {
if (text.hasOnlyEmojis) {
final addresses = await yatService.fetchYatAddress(text, ticker);
return ParsedAddress.fetchEmojiAddress(addresses: addresses, name: text);
}
}
final formattedName = OpenaliasRecord.formatDomainName(text);
final domainParts = formattedName.split('.');
final name = domainParts.last;
if (unstoppableDomains.any((domain) => name.contains(domain))) {
final address = await fetchUnstoppableDomainAddress(domain, ticker);
if (address?.isEmpty ?? true) {
return ParsedAddress(addresses: [domain]);
if (domainParts.length <= 1 || domainParts.first.isEmpty || name.isEmpty) {
return ParsedAddress(addresses: [text]);
}
return ParsedAddress(
addresses: [address],
name: domain,
parseFrom: ParseFrom.unstoppableDomains);
if (unstoppableDomains.any((domain) => name.contains(domain))) {
final address = await fetchUnstoppableDomainAddress(text, ticker);
return ParsedAddress.fetchUnstoppableDomainAddress(address: address, name: text);
}
final record = await OpenaliasRecord.fetchAddressAndName(
formattedName: formattedName, ticker: ticker);
return ParsedAddress.fetchOpenAliasAddress(record: record, name: text);
} catch (e) {
print(e.toString());
}
final record = await OpenaliasRecord.fetchAddressAndName(
formattedName: formattedName, ticker: ticker);
if (record == null || record.address.contains(formattedName)) {
return ParsedAddress(addresses: [domain]);
}
return ParsedAddress(
addresses: [record.address],
name: record.name,
description: record.description,
parseFrom: ParseFrom.openAlias);
} catch (e) {
print(e.toString());
return ParsedAddress(addresses: [text]);
}
return ParsedAddress(addresses: [domain]);
}

View file

@ -1,3 +1,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 }
class ParsedAddress {
@ -12,4 +16,46 @@ class ParsedAddress {
final String name;
final String description;
final ParseFrom parseFrom;
factory ParsedAddress.fetchEmojiAddress({
@required List<YatRecord> addresses,
@required String name,
}){
if (addresses?.isEmpty ?? true) {
return ParsedAddress(
addresses: [name], parseFrom: ParseFrom.yatRecord);
}
return ParsedAddress(
addresses: addresses.map((e) => e.address).toList(),
name: name,
parseFrom: ParseFrom.yatRecord,
);
}
factory ParsedAddress.fetchUnstoppableDomainAddress({
@required String address,
@required String name,
}){
if (address?.isEmpty ?? true) {
return ParsedAddress(addresses: [name]);
}
return ParsedAddress(
addresses: [address],
name: name,
parseFrom: ParseFrom.unstoppableDomains,
);
}
factory ParsedAddress.fetchOpenAliasAddress({@required OpenaliasRecord record, @required String name}){
final formattedName = OpenaliasRecord.formatDomainName(name);
if (record == null || record.address.contains(formattedName)) {
return ParsedAddress(addresses: [name]);
}
return ParsedAddress(
addresses: [record.address],
name: record.name,
description: record.description,
parseFrom: ParseFrom.openAlias,
);
}
}

View file

@ -0,0 +1,16 @@
class YatRecord {
String category;
String address;
YatRecord({
this.category,
this.address,
});
YatRecord.fromJson(Map<String, dynamic> json) {
address = json['address'] as String;
category = json['category'] as String;
}
}

View file

@ -2,9 +2,7 @@ import 'dart:async';
import 'package:cake_wallet/bitcoin/bitcoin.dart';
import 'package:cake_wallet/entities/language_service.dart';
import 'package:cake_wallet/buy/order.dart';
import 'package:cake_wallet/src/screens/yat_emoji_id.dart';
import 'package:cake_wallet/store/yat/yat_store.dart';
import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

View file

@ -178,24 +178,6 @@ class DashboardPage extends BasePage {
pages.add(TransactionsPage(dashboardViewModel: walletViewModel));
_isEffectsInstalled = true;
//if (walletViewModel.shouldShowYatPopup) {
// await Future<void>.delayed(Duration(seconds: 1));
// if (currentRouteSettings.name == Routes.preSeed
// || currentRouteSettings.name == Routes.seed) {
// return;
// }
// await showPopUp<void>(
// context: context,
// builder: (BuildContext context) {
// return YatPopup(
// dashboardViewModel: walletViewModel,
// onClose: () => Navigator.of(context).pop());
// });
// walletViewModel.furtherShowYatPopup(false);
//}
autorun((_) async {
if (!walletViewModel.isOutdatedElectrumWallet) {
return;

View file

@ -1,5 +1,5 @@
import 'dart:ui';
import 'package:cake_wallet/entities/parsed_address.dart';
import 'package:cake_wallet/di.dart';
import 'package:cake_wallet/utils/debounce.dart';
import 'package:cw_core/sync_status.dart';
import 'package:cw_core/wallet_type.dart';
@ -790,7 +790,7 @@ class ExchangePage extends BasePage {
Future<String> fetchParsedAddress(
BuildContext context, String domain, String ticker) async {
final parsedAddress = await parseAddressFromDomain(domain, ticker);
final parsedAddress = await getIt.get<AddressResolver>().resolve(domain, ticker);
final address = await extractAddressFromParsed(context, parsedAddress);
return address;
}

View file

@ -1,6 +1,6 @@
import 'dart:ui';
import 'package:cake_wallet/src/screens/dashboard/widgets/sync_indicator_icon.dart';
import 'package:cake_wallet/src/screens/send/widgets/send_card.dart';
import 'package:cake_wallet/src/screens/yat/widgets/yat_close_button.dart';
import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
import 'package:cake_wallet/src/widgets/template_tile.dart';
import 'package:cake_wallet/view_model/send/output.dart';
@ -22,9 +22,6 @@ import 'package:dotted_border/dotted_border.dart';
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
import 'package:cake_wallet/src/screens/send/widgets/confirm_sending_alert.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';
import 'package:cake_wallet/store/yat/yat_store.dart';
import 'package:cake_wallet/src/screens/yat/yat_sending.dart';
import 'package:cake_wallet/src/screens/dashboard/widgets/sync_indicator_icon.dart';
class SendPage extends BasePage {
SendPage({@required this.sendViewModel}) : _formKey = GlobalKey<FormState>();
@ -304,11 +301,6 @@ class SendPage extends BasePage {
await sendViewModel.createTransaction();
if (!sendViewModel.isBatchSending &&
sendViewModel.hasYat) {
Navigator.of(context)
.push<void>(YatSending.createRoute(sendViewModel));
}
},
text: S.of(context).send,
color: Theme.of(context).accentTextTheme.body2.color,
@ -345,7 +337,7 @@ class SendPage extends BasePage {
}
if (state is ExecutedSuccessfullyState &&
!(!sendViewModel.isBatchSending && sendViewModel.hasYat)) {
!sendViewModel.isBatchSending) {
WidgetsBinding.instance.addPostFrameCallback((_) {
showPopUp<void>(
context: context,

View file

@ -1,15 +0,0 @@
import 'package:flutter/material.dart';
class CircleClipper extends CustomClipper<Path> {
CircleClipper(this.center, this.radius);
final Offset center;
final double radius;
@override
Path getClip(Size size) =>
Path()..addOval(Rect.fromCircle(radius: radius, center: center));
@override
bool shouldReclip(covariant CustomClipper<Path> oldClipper) => true;
}

View file

@ -1,124 +0,0 @@
import 'package:cake_wallet/src/screens/yat/widgets/yat_bar.dart';
import 'package:cake_wallet/src/widgets/primary_button.dart';
import 'package:cake_wallet/src/widgets/scollable_with_bottom_section.dart';
import 'package:cake_wallet/store/yat/yat_store.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:cake_wallet/palette.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:lottie/lottie.dart';
class YatAlert extends StatelessWidget {
YatAlert(this.yatStore);
final YatStore yatStore;
static const aspectRatioImage = 1.133;
final animation = Lottie.asset('assets/animation/anim1.json');
@override
Widget build(BuildContext context) {
final screenHeight = MediaQuery.of(context).size.height;
final screenWidth = MediaQuery.of(context).size.width;
return Container(
height: screenHeight,
width: screenWidth,
color: Colors.white,
child: ScrollableWithBottomSection(
contentPadding: EdgeInsets.only(top: 40, bottom: 40),
content: Column(
children: [
Container(
height: 45,
padding: EdgeInsets.only(left: 24, right: 24),
child: YatBar(onClose: () => Navigator.of(context).pop())
),
animation,
Container(
padding: EdgeInsets.only(left: 30, right: 30),
child: Column(
children: [
Text(
S.of(context).yat_alert_title,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
fontFamily: 'Lato',
color: Colors.black,
decoration: TextDecoration.none,
)
),
Padding(
padding: EdgeInsets.only(top: 20),
child: Text(
S.of(context).yat_alert_content,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.normal,
fontFamily: 'Lato',
color: Colors.black,
decoration: TextDecoration.none,
)
)
)
]
)
)
]
),
bottomSectionPadding: EdgeInsets.fromLTRB(24, 0, 24, 40),
bottomSection: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
PrimaryIconButton(
text: S.of(context).get_your_yat,
textColor: Colors.white,
color: Palette.protectiveBlue,
borderColor: Palette.protectiveBlue,
iconColor: Colors.white,
iconBackgroundColor: Colors.transparent,
iconData: CupertinoIcons
.arrow_up_right_square,
mainAxisAlignment: MainAxisAlignment.end,
onPressed: () {
var createNewYatUrl = YatLink.startFlowUrl;
final createNewYatUrlParameters =
yatStore.defineQueryParameters();
if (createNewYatUrlParameters.isNotEmpty) {
createNewYatUrl += '?sub1=' + createNewYatUrlParameters;
}
launch(createNewYatUrl, forceSafariVC: false);
}),
Padding(
padding: EdgeInsets.only(top: 24),
child: PrimaryIconButton(
text: S.of(context).connect_an_existing_yat,
textColor: Colors.black,
color: Palette.blueAlice,
borderColor: Palette.blueAlice,
iconColor: Colors.black,
iconBackgroundColor: Colors.transparent,
iconData: CupertinoIcons
.arrow_up_right_square,
mainAxisAlignment: MainAxisAlignment.end,
onPressed: () {
String url = YatLink.baseUrl + YatLink.signInSuffix;
final parameters =
yatStore.defineQueryParameters();
if (parameters.isNotEmpty) {
url += YatLink.queryParameter + parameters;
}
launch(url, forceSafariVC: false);
})
)
]
),
)
);
}
}

View file

@ -1,141 +0,0 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:mobx/mobx.dart';
import 'package:cake_wallet/src/screens/yat/widgets/yat_close_button.dart';
import 'package:cake_wallet/view_model/send/send_view_model_state.dart';
import 'package:cake_wallet/src/screens/yat/circle_clipper.dart';
import 'package:cake_wallet/src/screens/base_page.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:cake_wallet/src/widgets/primary_button.dart';
import 'package:cake_wallet/store/yat/yat_store.dart';
import 'package:cake_wallet/core/execution_state.dart';
import 'package:cake_wallet/view_model/send/send_view_model.dart';
class YatSending extends BasePage {
YatSending(this.sendViewModel);
static Route createRoute(SendViewModel sendViewModel) {
return PageRouteBuilder<void>(
transitionDuration: Duration(seconds: 1),
reverseTransitionDuration: Duration(seconds: 1),
opaque: false,
barrierDismissible: false,
pageBuilder: (context, animation, secondaryAnimation) => YatSending(sendViewModel),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
final screenSize = MediaQuery.of(context).size;
final center = Offset(screenSize.width / 2, screenSize.height / 2);
final endRadius = screenSize.height * 1.2;
final tween = Tween(begin: 0.0, end: endRadius);
return ClipPath(
clipper: CircleClipper(center, animation.drive(tween).value),
child: child,
);
},
);
}
final SendViewModel sendViewModel;
@override
Color get titleColor => Colors.white;
@override
bool get resizeToAvoidBottomInset => false;
@override
bool get extendBodyBehindAppBar => true;
@override
AppBarStyle get appBarStyle => AppBarStyle.transparent;
@override
Widget trailing(context) =>
YatCloseButton(onClose: () => Navigator.of(context).pop());
@override
Widget leading(BuildContext context) => Container();
@override
Widget body(BuildContext context) {
final screenWidth = MediaQuery.of(context).size.width;
return Container(
color: Colors.black,
child: Stack(
children: [
//Center(
// child:FutureBuilder<String>(
// future: visualisationForEmojiId(sendViewModel.outputs.first.address),
// builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
// switch (snapshot.connectionState) {
// case ConnectionState.done:
// if (snapshot.hasError || snapshot.data.isEmpty) {
// return Image.asset('assets/images/yat_logo.png', width: screenWidth, color: Colors.white);
// }
// return Image.network(
// snapshot.data,
// scale: 0.7,
// loadingBuilder: (Object z, Widget child, ImageChunkEvent loading)
// => loading != null
// ? CupertinoActivityIndicator(animating: true)
// : child);
// default:
// return Image.asset('assets/images/yat_logo.png', width: screenWidth, color: Colors.white);
// }
// }),
// ),
Positioned(
bottom: 20,
child: Container(
width: screenWidth,
padding: EdgeInsets.fromLTRB(20, 0, 20, 10),
child: Column(children: [
Text(
'You are sending ${sendViewModel.outputs.first.cryptoAmount} ${sendViewModel.currency.title} to ${sendViewModel.outputs.first.address}'.toUpperCase(),
style: TextStyle(
fontSize: 28,
decoration: TextDecoration.none,
color: Theme.of(context).accentTextTheme.display3.backgroundColor),
textAlign: TextAlign.center),
Container(height: 30),
LoadingPrimaryButton(
onPressed: () {
sendViewModel.commitTransaction();
showPopUp<void>(
context: context,
builder: (BuildContext popContext) {
return Observer(builder: (_) {
final state = sendViewModel.state;
if (state is FailureState) {
Navigator.of(context).pop();
}
if (state is TransactionCommitted) {
return AlertWithOneAction(
alertTitle: '',
alertContent: S.of(popContext).send_success(
sendViewModel.currency
.toString()),
buttonText: S.of(popContext).ok,
buttonAction: () {
Navigator.of(popContext).pop();
Navigator.of(context).pop();
});
}
return Offstage();
});
});
},
text: S.of(context).confirm_sending,
color: Theme.of(context).accentTextTheme.body2.color,
textColor: Colors.white,
isLoading: sendViewModel.state is IsExecutingState ||
sendViewModel.state is TransactionCommitting)])))]));
}
}

View file

@ -1,3 +1,4 @@
import 'package:cake_wallet/di.dart';
import 'package:cake_wallet/entities/calculate_fiat_amount_raw.dart';
import 'package:cake_wallet/entities/parse_address_from_domain.dart';
import 'package:cake_wallet/entities/parsed_address.dart';
@ -216,7 +217,7 @@ abstract class OutputBase with Store {
Future<void> fetchParsedAddress(BuildContext context) async {
final domain = address;
final ticker = _wallet.currency.title.toLowerCase();
parsedAddress = await parseAddressFromDomain(domain, ticker);
parsedAddress = await getIt.get<AddressResolver>().resolve(domain, ticker);
extractedAddress = await extractAddressFromParsed(context, parsedAddress);
note = parsedAddress.description;
}

View file

@ -1,4 +1,3 @@
import 'package:cake_wallet/src/screens/yat/yat_alert.dart';
import 'package:cake_wallet/store/yat/yat_store.dart';
import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:cake_wallet/view_model/settings/link_list_item.dart';