From b0463f4df2b546cc9b2952cfb66e53c46e72d289 Mon Sep 17 00:00:00 2001 From: fosse Date: Wed, 22 Nov 2023 13:54:34 -0500 Subject: [PATCH] move moonpay providers into separate files --- lib/buy/moonpay/moonpay_buy_provider.dart | 130 ------------------ .../moonpay/moonpay_exchange_provider.dart | 95 +++++++++++++ lib/buy/moonpay/moonpay_sell_provider.dart | 79 +++++++++++ lib/di.dart | 1 + lib/entities/main_actions.dart | 1 + 5 files changed, 176 insertions(+), 130 deletions(-) create mode 100644 lib/buy/moonpay/moonpay_exchange_provider.dart create mode 100644 lib/buy/moonpay/moonpay_sell_provider.dart diff --git a/lib/buy/moonpay/moonpay_buy_provider.dart b/lib/buy/moonpay/moonpay_buy_provider.dart index c2db9e8ff..8d6af7775 100644 --- a/lib/buy/moonpay/moonpay_buy_provider.dart +++ b/lib/buy/moonpay/moonpay_buy_provider.dart @@ -20,64 +20,6 @@ import 'package:cake_wallet/.secrets.g.dart' as secrets; import 'package:cw_core/crypto_currency.dart'; import 'package:url_launcher/url_launcher.dart'; -class MoonPaySellProvider { - MoonPaySellProvider({this.isTest = false}) : baseUrl = isTest ? _baseTestUrl : _baseProductUrl; - - static const _baseTestUrl = 'sell-sandbox.moonpay.com'; - static const _baseProductUrl = 'sell.moonpay.com'; - static String themeToMoonPayTheme(ThemeBase theme) { - switch (theme.type) { - case ThemeType.bright: - return 'light'; - case ThemeType.light: - return 'light'; - case ThemeType.dark: - return 'dark'; - } - } - - static String get _apiKey => secrets.moonPayApiKey; - static String get _secretKey => secrets.moonPaySecretKey; - final bool isTest; - final String baseUrl; - - Future requestUrl( - {required CryptoCurrency currency, - required String refundWalletAddress, - required SettingsStore settingsStore}) async { - final customParams = { - 'theme': themeToMoonPayTheme(settingsStore.currentTheme), - 'language': settingsStore.languageCode, - 'colorCode': settingsStore.currentTheme.type == ThemeType.dark - ? '#${Palette.blueCraiola.value.toRadixString(16).substring(2, 8)}' - : '#${Palette.moderateSlateBlue.value.toRadixString(16).substring(2, 8)}', - }; - - final originalUri = Uri.https( - baseUrl, - '', - { - 'apiKey': _apiKey, - 'defaultBaseCurrencyCode': currency.toString().toLowerCase(), - 'refundWalletAddress': refundWalletAddress - }..addAll(customParams)); - final messageBytes = utf8.encode('?${originalUri.query}'); - final key = utf8.encode(_secretKey); - final hmac = Hmac(sha256, key); - final digest = hmac.convert(messageBytes); - final signature = base64.encode(digest.bytes); - - if (isTest) { - return originalUri; - } - - final query = Map.from(originalUri.queryParameters); - query['signature'] = signature; - final signedUri = originalUri.replace(queryParameters: query); - return signedUri; - } -} - class MoonPayBuyProvider extends BuyProvider { MoonPayBuyProvider({required WalletBase wallet, bool isTestEnvironment = false}) : baseUrl = isTestEnvironment ? _baseTestUrl : _baseProductUrl, @@ -213,75 +155,3 @@ class MoonPayBuyProvider extends BuyProvider { } } -class MoonPayExchangeProvider { - MoonPayExchangeProvider( - {required SettingsStore settingsStore, required WalletBase wallet, this.isTest = false}) - : this._settingsStore = settingsStore, - this._wallet = wallet, - baseUrl = isTest ? _baseTestUrl : _baseProductUrl; - - final SettingsStore _settingsStore; - final WalletBase _wallet; - - static const _baseTestUrl = 'buy-staging.moonpay.com'; - static const _apiKey = secrets.moonPayApiKey; - static const _baseProductUrl = 'buy.moonpay.com'; - static const _secretKey = secrets.moonPaySecretKey; - - static String themeToMoonPayTheme(ThemeBase theme) { - switch (theme.type) { - case ThemeType.bright: - return 'light'; - case ThemeType.light: - return 'light'; - case ThemeType.dark: - return 'dark'; - } - } - - final bool isTest; - final String baseUrl; - - Future requestUrl( - {required CryptoCurrency currency, required String refundWalletAddress}) async { - final customParams = { - 'theme': themeToMoonPayTheme(_settingsStore.currentTheme), - 'language': _settingsStore.languageCode, - 'colorCode': _settingsStore.currentTheme.type == ThemeType.dark - ? '#${Palette.blueCraiola.value.toRadixString(16).substring(2, 8)}' - : '#${Palette.moderateSlateBlue.value.toRadixString(16).substring(2, 8)}', - }; - - final originalUri = Uri.https( - baseUrl, - 'swaps', - { - 'apiKey': _apiKey, - 'baseCurrencyCode': currency.toString().toLowerCase(), - 'refundWalletAddress': refundWalletAddress - }..addAll(customParams)); - final messageBytes = utf8.encode('?${originalUri.query}'); - final key = utf8.encode(_secretKey); - final hmac = Hmac(sha256, key); - final digest = hmac.convert(messageBytes); - final signature = base64.encode(digest.bytes); - - if (isTest) { - return originalUri; - } - - final query = Map.from(originalUri.queryParameters); - query['signature'] = signature; - final signedUri = originalUri.replace(queryParameters: query); - return signedUri; - } - - Future launchProvider(BuildContext context) async { - final uri = await requestUrl(currency: _wallet.currency, refundWalletAddress: _wallet.walletAddresses.address); - if (DeviceInfo.instance.isMobile) { - Navigator.of(context).pushNamed(Routes.webViewPage, arguments: [S.of(context).exchange, uri]); - } else { - await launchUrl(uri); - } - } -} diff --git a/lib/buy/moonpay/moonpay_exchange_provider.dart b/lib/buy/moonpay/moonpay_exchange_provider.dart new file mode 100644 index 000000000..e5e6d096d --- /dev/null +++ b/lib/buy/moonpay/moonpay_exchange_provider.dart @@ -0,0 +1,95 @@ +import 'dart:convert'; +import 'package:cake_wallet/generated/i18n.dart'; +import 'package:cake_wallet/palette.dart'; +import 'package:cake_wallet/routes.dart'; +import 'package:cake_wallet/store/settings_store.dart'; +import 'package:cake_wallet/themes/theme_base.dart'; +import 'package:cake_wallet/utils/device_info.dart'; +import 'package:crypto/crypto.dart'; +import 'package:cake_wallet/buy/buy_exception.dart'; +import 'package:flutter/material.dart'; +import 'package:http/http.dart'; +import 'package:cake_wallet/buy/buy_amount.dart'; +import 'package:cake_wallet/buy/buy_provider.dart'; +import 'package:cake_wallet/buy/buy_provider_description.dart'; +import 'package:cake_wallet/buy/order.dart'; +import 'package:cw_core/wallet_base.dart'; +import 'package:cw_core/wallet_type.dart'; +import 'package:cake_wallet/exchange/trade_state.dart'; +import 'package:cake_wallet/.secrets.g.dart' as secrets; +import 'package:cw_core/crypto_currency.dart'; +import 'package:url_launcher/url_launcher.dart'; + +class MoonPayExchangeProvider { + MoonPayExchangeProvider( + {required SettingsStore settingsStore, required WalletBase wallet, this.isTest = false}) + : this._settingsStore = settingsStore, + this._wallet = wallet, + baseUrl = isTest ? _baseTestUrl : _baseProductUrl; + + final SettingsStore _settingsStore; + final WalletBase _wallet; + + static const _baseTestUrl = 'buy-staging.moonpay.com'; + static const _apiKey = secrets.moonPayApiKey; + static const _baseProductUrl = 'buy.moonpay.com'; + static const _secretKey = secrets.moonPaySecretKey; + + static String themeToMoonPayTheme(ThemeBase theme) { + switch (theme.type) { + case ThemeType.bright: + return 'light'; + case ThemeType.light: + return 'light'; + case ThemeType.dark: + return 'dark'; + } + } + + final bool isTest; + final String baseUrl; + + Future requestUrl( + {required CryptoCurrency currency, required String refundWalletAddress}) async { + final customParams = { + 'theme': themeToMoonPayTheme(_settingsStore.currentTheme), + 'language': _settingsStore.languageCode, + 'colorCode': _settingsStore.currentTheme.type == ThemeType.dark + ? '#${Palette.blueCraiola.value.toRadixString(16).substring(2, 8)}' + : '#${Palette.moderateSlateBlue.value.toRadixString(16).substring(2, 8)}', + }; + + final originalUri = Uri.https( + baseUrl, + 'swaps', + { + 'apiKey': _apiKey, + 'baseCurrencyCode': currency.toString().toLowerCase(), + 'refundWalletAddress': refundWalletAddress + }..addAll(customParams)); + final messageBytes = utf8.encode('?${originalUri.query}'); + final key = utf8.encode(_secretKey); + final hmac = Hmac(sha256, key); + final digest = hmac.convert(messageBytes); + final signature = base64.encode(digest.bytes); + + if (isTest) { + return originalUri; + } + + final query = Map.from(originalUri.queryParameters); + query['signature'] = signature; + final signedUri = originalUri.replace(queryParameters: query); + return signedUri; + } + + Future launchProvider(BuildContext context) async { + final uri = await requestUrl( + currency: _wallet.currency, refundWalletAddress: _wallet.walletAddresses.address); + if (DeviceInfo.instance.isMobile) { + Navigator.of(context).pushNamed(Routes.webViewPage, arguments: [S.of(context).exchange, uri]); + } else { + await launchUrl(uri); + } + } +} diff --git a/lib/buy/moonpay/moonpay_sell_provider.dart b/lib/buy/moonpay/moonpay_sell_provider.dart new file mode 100644 index 000000000..9aacc1ff5 --- /dev/null +++ b/lib/buy/moonpay/moonpay_sell_provider.dart @@ -0,0 +1,79 @@ +import 'dart:convert'; +import 'package:cake_wallet/generated/i18n.dart'; +import 'package:cake_wallet/palette.dart'; +import 'package:cake_wallet/routes.dart'; +import 'package:cake_wallet/store/settings_store.dart'; +import 'package:cake_wallet/themes/theme_base.dart'; +import 'package:cake_wallet/utils/device_info.dart'; +import 'package:crypto/crypto.dart'; +import 'package:cake_wallet/buy/buy_exception.dart'; +import 'package:flutter/material.dart'; +import 'package:http/http.dart'; +import 'package:cake_wallet/buy/buy_amount.dart'; +import 'package:cake_wallet/buy/buy_provider.dart'; +import 'package:cake_wallet/buy/buy_provider_description.dart'; +import 'package:cake_wallet/buy/order.dart'; +import 'package:cw_core/wallet_base.dart'; +import 'package:cw_core/wallet_type.dart'; +import 'package:cake_wallet/exchange/trade_state.dart'; +import 'package:cake_wallet/.secrets.g.dart' as secrets; +import 'package:cw_core/crypto_currency.dart'; +import 'package:url_launcher/url_launcher.dart'; + +class MoonPaySellProvider { + MoonPaySellProvider({this.isTest = false}) : baseUrl = isTest ? _baseTestUrl : _baseProductUrl; + + static const _baseTestUrl = 'sell-sandbox.moonpay.com'; + static const _baseProductUrl = 'sell.moonpay.com'; + static String themeToMoonPayTheme(ThemeBase theme) { + switch (theme.type) { + case ThemeType.bright: + return 'light'; + case ThemeType.light: + return 'light'; + case ThemeType.dark: + return 'dark'; + } + } + + static String get _apiKey => secrets.moonPayApiKey; + static String get _secretKey => secrets.moonPaySecretKey; + final bool isTest; + final String baseUrl; + + Future requestUrl( + {required CryptoCurrency currency, + required String refundWalletAddress, + required SettingsStore settingsStore}) async { + final customParams = { + 'theme': themeToMoonPayTheme(settingsStore.currentTheme), + 'language': settingsStore.languageCode, + 'colorCode': settingsStore.currentTheme.type == ThemeType.dark + ? '#${Palette.blueCraiola.value.toRadixString(16).substring(2, 8)}' + : '#${Palette.moderateSlateBlue.value.toRadixString(16).substring(2, 8)}', + }; + + final originalUri = Uri.https( + baseUrl, + '', + { + 'apiKey': _apiKey, + 'defaultBaseCurrencyCode': currency.toString().toLowerCase(), + 'refundWalletAddress': refundWalletAddress + }..addAll(customParams)); + final messageBytes = utf8.encode('?${originalUri.query}'); + final key = utf8.encode(_secretKey); + final hmac = Hmac(sha256, key); + final digest = hmac.convert(messageBytes); + final signature = base64.encode(digest.bytes); + + if (isTest) { + return originalUri; + } + + final query = Map.from(originalUri.queryParameters); + query['signature'] = signature; + final signedUri = originalUri.replace(queryParameters: query); + return signedUri; + } +} diff --git a/lib/di.dart b/lib/di.dart index e2214a688..362935fbc 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -2,6 +2,7 @@ import 'package:cake_wallet/anonpay/anonpay_api.dart'; import 'package:cake_wallet/anonpay/anonpay_info_base.dart'; import 'package:cake_wallet/anonpay/anonpay_invoice_info.dart'; import 'package:cake_wallet/buy/moonpay/moonpay_buy_provider.dart'; +import 'package:cake_wallet/buy/moonpay/moonpay_exchange_provider.dart'; import 'package:cake_wallet/buy/onramper/onramper_buy_provider.dart'; import 'package:cake_wallet/bitcoin_cash/bitcoin_cash.dart'; import 'package:cake_wallet/buy/payfura/payfura_buy_provider.dart'; diff --git a/lib/entities/main_actions.dart b/lib/entities/main_actions.dart index d342479d1..af393e711 100644 --- a/lib/entities/main_actions.dart +++ b/lib/entities/main_actions.dart @@ -1,4 +1,5 @@ import 'package:cake_wallet/buy/moonpay/moonpay_buy_provider.dart'; +import 'package:cake_wallet/buy/moonpay/moonpay_sell_provider.dart'; import 'package:cake_wallet/buy/onramper/onramper_buy_provider.dart'; import 'package:cake_wallet/buy/robinhood/robinhood_buy_provider.dart'; import 'package:cake_wallet/di.dart';