Updates wyre.

This commit is contained in:
M 2021-03-23 13:05:01 +02:00
parent 23f73df391
commit 5cad3a9612
6 changed files with 91 additions and 56 deletions

View file

@ -88,6 +88,7 @@ import 'package:cake_wallet/view_model/wallet_list/wallet_list_view_model.dart';
import 'package:cake_wallet/view_model/wallet_restore_view_model.dart';
import 'package:cake_wallet/view_model/wallet_seed_view_model.dart';
import 'package:cake_wallet/view_model/exchange/exchange_view_model.dart';
import 'package:cake_wallet/view_model/wyre_view_model.dart';
import 'package:flutter/widgets.dart';
import 'package:get_it/get_it.dart';
import 'package:hive/hive.dart';
@ -142,7 +143,7 @@ Future setup(
if (!_isSetupFinished) {
getIt.registerSingletonAsync<SharedPreferences>(
() => SharedPreferences.getInstance());
() => SharedPreferences.getInstance());
}
final settingsStore = await SettingsStoreBase.load(nodeSource: _nodeSource);
@ -231,7 +232,8 @@ Future setup(
tradeFilterStore: getIt.get<TradeFilterStore>(),
transactionFilterStore: getIt.get<TransactionFilterStore>(),
ordersSource: _ordersSource,
ordersStore: getIt.get<OrdersStore>()));
ordersStore: getIt.get<OrdersStore>(),
wyreViewModel: getIt.get<WyreViewModel>()));
getIt.registerFactory<AuthService>(() => AuthService(
secureStorage: getIt.get<FlutterSecureStorage>(),
@ -494,8 +496,8 @@ Future setup(
getIt.registerFactoryParam<TradeDetailsViewModel, Trade, void>((trade, _) =>
TradeDetailsViewModel(tradeForDetails: trade, trades: _tradesSource));
getIt.registerFactoryParam<OrderDetailsViewModel, Order, void>((order, _) =>
OrderDetailsViewModel(orderForDetails: order));
getIt.registerFactoryParam<OrderDetailsViewModel, Order, void>(
(order, _) => OrderDetailsViewModel(orderForDetails: order));
getIt.registerFactory(() => BackupService(
getIt.get<FlutterSecureStorage>(),
@ -529,8 +531,15 @@ Future setup(
getIt.registerFactoryParam<OrderDetailsPage, Order, void>((Order order, _) =>
OrderDetailsPage(getIt.get<OrderDetailsViewModel>(param1: order)));
getIt.registerFactory(() {
final wallet = getIt.get<AppStore>().wallet;
return WyreViewModel(ordersSource, getIt.get<OrdersStore>(),
walletId: wallet.id, address: wallet.address, type: wallet.type);
});
getIt.registerFactoryParam<WyrePage, String, void>((String url, _) =>
WyrePage(ordersStore: getIt.get<OrdersStore>(), url: url));
WyrePage(getIt.get<WyreViewModel>(),
ordersStore: getIt.get<OrdersStore>(), url: url));
getIt.registerFactory(() => SupportViewModel());

View file

@ -146,17 +146,10 @@ class DashboardPage extends BasePage {
: () async {
try {
walletViewModel.isRunningWebView = true;
final url = await walletViewModel.getWyreUrl();
final url = await walletViewModel.wyreViewModel.getWyreUrl();
if (url.isNotEmpty) {
await Navigator.of(context)
.pushNamed(Routes.wyre, arguments: url);
final orderId = walletViewModel.ordersStore
.orderId;
if (orderId.isNotEmpty) {
await walletViewModel.saveOrder(orderId);
}
}
walletViewModel.isRunningWebView = false;
} catch(e) {

View file

@ -3,14 +3,16 @@ import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/palette.dart';
import 'package:cake_wallet/src/screens/base_page.dart';
import 'package:cake_wallet/store/dashboard/orders_store.dart';
import 'package:cake_wallet/view_model/wyre_view_model.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WyrePage extends BasePage {
WyrePage({@required this.ordersStore, @required this.url});
WyrePage(this.wyreViewModel, {@required this.ordersStore, @required this.url});
final OrdersStore ordersStore;
final String url;
final WyreViewModel wyreViewModel;
@override
String get title => S.current.buy;
@ -23,14 +25,15 @@ class WyrePage extends BasePage {
@override
Widget body(BuildContext context) =>
WyrePageBody(ordersStore: ordersStore,url: url);
WyrePageBody(wyreViewModel, ordersStore: ordersStore,url: url);
}
class WyrePageBody extends StatefulWidget {
WyrePageBody({this.ordersStore, this.url});
WyrePageBody(this.wyreViewModel, {this.ordersStore, this.url});
final OrdersStore ordersStore;
final String url;
final WyreViewModel wyreViewModel;
@override
WyrePageBodyState createState() => WyrePageBodyState();
@ -66,6 +69,10 @@ class WyrePageBodyState extends State<WyrePageBody> {
final urlParts = currentUrl.split('/');
orderId = urlParts.last;
widget.ordersStore.orderId = orderId;
if (orderId.isNotEmpty) {
await widget.wyreViewModel.saveOrder(orderId);
}
}
return NavigationDecision.navigate;

View file

@ -28,6 +28,7 @@ import 'package:cake_wallet/view_model/dashboard/trade_list_item.dart';
import 'package:cake_wallet/view_model/dashboard/transaction_list_item.dart';
import 'package:cake_wallet/view_model/dashboard/action_list_item.dart';
import 'package:cake_wallet/view_model/dashboard/action_list_display_mode.dart';
import 'package:cake_wallet/view_model/wyre_view_model.dart';
import 'package:crypto/crypto.dart';
import 'package:flutter/services.dart';
import 'package:hive/hive.dart';
@ -43,7 +44,6 @@ import 'package:cake_wallet/store/dashboard/trade_filter_store.dart';
import 'package:cake_wallet/store/dashboard/transaction_filter_store.dart';
import 'package:cake_wallet/view_model/dashboard/formatted_item_list.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:cake_wallet/.secrets.g.dart' as secrets;
import 'package:convert/convert.dart';
part 'dashboard_view_model.g.dart';
@ -58,7 +58,8 @@ abstract class DashboardViewModelBase with Store {
this.tradeFilterStore,
this.transactionFilterStore,
this.ordersSource,
this.ordersStore}) {
this.ordersStore,
this.wyreViewModel}) {
filterItems = {
S.current.transactions: [
FilterItem(
@ -250,6 +251,8 @@ abstract class DashboardViewModelBase with Store {
TransactionFilterStore transactionFilterStore;
WyreViewModel wyreViewModel;
Map<String, List<FilterItem>> filterItems;
ReactionDisposer _reaction;
@ -331,42 +334,5 @@ abstract class DashboardViewModelBase with Store {
settingsStore: appStore.settingsStore)));
}
Future<String> getWyreUrl() async {
final timestamp = DateTime.now().millisecondsSinceEpoch.toString();
final url = 'https://api.sendwyre.com/v3/orders/reserve' + '?timestamp=' +
timestamp;
final secretKey = secrets.wyreSecretKey;
final accountId = secrets.wyreAccountId;
final body = {
'destCurrency' : walletTypeToCryptoCurrency(type).title,
'dest' : walletTypeToString(type).toLowerCase() + ':' + address,
'referrerAccountId' : accountId,
'lockFields' : ['destCurrency', 'dest']
};
final response = await post(url,
headers: {
'Authorization': 'Bearer $secretKey',
'Content-Type': 'application/json',
'cache-control': 'no-cache'
},
body: json.encode(body)
);
if (response.statusCode == 200) {
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final urlFromResponse = responseJSON['url'] as String;
return urlFromResponse;
} else {
return '';
}
}
Future<void> saveOrder(String orderId) async {
final order = await findOrderById(orderId);
order.receiveAddress = address;
order.walletId = wallet.id;
await ordersSource.add(order);
ordersStore.setOrder(order);
}
}

View file

@ -52,7 +52,7 @@ abstract class OrderDetailsViewModelBase with Store {
void _updateItems() {
final dateFormat = DateFormatter.withCurrentLocal();
final buildURL =
'https://api.sendwyre.com/v2/transfer/${order.transferId}/track';
'https://dash.sendwyre.com/track/${order.transferId}';
items?.clear();

View file

@ -0,0 +1,60 @@
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';
import 'package:http/http.dart';
import 'package:cake_wallet/.secrets.g.dart' as secrets;
import 'package:cake_wallet/entities/find_order_by_id.dart';
import 'package:cake_wallet/entities/order.dart';
import 'package:cake_wallet/entities/wallet_type.dart';
import 'package:cake_wallet/store/dashboard/orders_store.dart';
class WyreViewModel {
WyreViewModel(this.ordersSource, this.ordersStore,
{@required this.walletId, @required this.address, @required this.type});
final Box<Order> ordersSource;
final OrdersStore ordersStore;
final String walletId;
final WalletType type;
final String address;
Future<void> saveOrder(String orderId) async {
final order = await findOrderById(orderId);
order.receiveAddress = address;
order.walletId = walletId;
await ordersSource.add(order);
ordersStore.setOrder(order);
}
Future<String> getWyreUrl() async {
final timestamp = DateTime.now().millisecondsSinceEpoch.toString();
final url = 'https://api.sendwyre.com/v3/orders/reserve' +
'?timestamp=' +
timestamp;
final secretKey = secrets.wyreSecretKey;
final accountId = secrets.wyreAccountId;
final body = {
'destCurrency': walletTypeToCryptoCurrency(type).title,
'dest': walletTypeToString(type).toLowerCase() + ':' + address,
'referrerAccountId': accountId,
'lockFields': ['destCurrency', 'dest']
};
final response = await post(url,
headers: {
'Authorization': 'Bearer $secretKey',
'Content-Type': 'application/json',
'cache-control': 'no-cache'
},
body: json.encode(body));
if (response.statusCode == 200) {
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final urlFromResponse = responseJSON['url'] as String;
return urlFromResponse;
} else {
return '';
}
}
}