Fixes for electrum
M committed
Dec 8, 2021 at 12:46 UTC
c76ffd63272e50a401a4c9d33236635172b506ab
5 files changed
+72
-21
lib/bitcoin/electrum.dart
+23
-15
@@ -318,11 +318,15 @@ class ElectrumClient {
318
{@required String id,
319
@required String method,
320
List<Object> params = const []}) {
321
- final subscription = BehaviorSubject<T>();
322
- _regisrySubscription(id, subscription);
323
- socket.write(jsonrpc(method: method, id: _id, params: params));
321
+ try {
322
+ final subscription = BehaviorSubject<T>();
323
+ _regisrySubscription(id, subscription);
324
+ socket.write(jsonrpc(method: method, id: _id, params: params));
325
325
- return subscription;
326
+ return subscription;
327
+ } catch(e) {
328
+ print(e.toString());
329
+ }
330
}
331
332
Future<dynamic> call({String method, List<Object> params = const []}) async {
@@ -339,18 +343,22 @@ class ElectrumClient {
343
{String method,
344
List<Object> params = const [],
345
int timeout = 2000}) async {
342
- final completer = Completer<dynamic>();
343
- _id += 1;
344
- final id = _id;
345
- _registryTask(id, completer);
346
- socket.write(jsonrpc(method: method, id: id, params: params));
347
- Timer(Duration(milliseconds: timeout), () {
348
- if (!completer.isCompleted) {
349
- completer.completeError(RequestFailedTimeoutException(method, id));
350
- }
351
- });
346
+ try {
347
+ final completer = Completer<dynamic>();
348
+ _id += 1;
349
+ final id = _id;
350
+ _registryTask(id, completer);
351
+ socket.write(jsonrpc(method: method, id: id, params: params));
352
+ Timer(Duration(milliseconds: timeout), () {
353
+ if (!completer.isCompleted) {
354
+ completer.completeError(RequestFailedTimeoutException(method, id));
355
+ }
356
+ });
357
353
- return completer.future;
358
+ return completer.future;
359
+ } catch(e) {
360
+ print(e.toString());
361
+ }
362
}
363
364
Future<void> close() async {
lib/bitcoin/electrum_wallet.dart
+6
-1
@@ -90,6 +90,11 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
90
.map((addr) => scriptHash(addr.address, networkType: networkType))
91
.toList();
92
93
+ List<String> get publicScriptHashes => walletAddresses.addresses
94
+ .where((addr) => !addr.isHidden)
95
+ .map((addr) => scriptHash(addr.address, networkType: networkType))
96
+ .toList();
97
+
98
String get xpub => hd.base58;
99
100
@override
@@ -476,7 +481,7 @@ abstract class ElectrumWalletBase extends WalletBase<ElectrumBalance,
481
@override
482
Future<Map<String, ElectrumTransactionInfo>> fetchTransactions() async {
483
final histories =
479
- scriptHashes.map((scriptHash) => electrumClient.getHistory(scriptHash));
484
+ publicScriptHashes.map((scriptHash) => electrumClient.getHistory(scriptHash));
485
final _historiesWithDetails = await Future.wait(histories)
486
.then((histories) => histories.expand((i) => i).toList())
487
.then((histories) => histories.map((tx) => fetchTransactionInfo(
lib/bitcoin/electrum_wallet_addresses.dart
-1
@@ -100,7 +100,6 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
100
for (var i = startIndex; i < count + startIndex; i++) {
101
final address = BitcoinAddressRecord(getAddress(index: i, hd: hd),
102
index: i, isHidden: isHidden);
103
- print(address.address);
103
list.add(address);
104
}
105
lib/buy/moonpay/moonpay_buy_provider.dart
+36
@@ -10,6 +10,42 @@ import 'package:cake_wallet/core/wallet_base.dart';
10
import 'package:cake_wallet/entities/wallet_type.dart';
11
import 'package:cake_wallet/exchange/trade_state.dart';
12
import 'package:cake_wallet/.secrets.g.dart' as secrets;
13
+import 'package:cake_wallet/entities/crypto_currency.dart';
14
+
15
+class MoonPaySellProvider {
16
+ MoonPaySellProvider({this.isTest = false})
17
+ : baseUrl = isTest ? _baseTestUrl : _baseProductUrl;
18
+
19
+ static const _baseTestUrl = 'sell-staging.moonpay.com';
20
+ static const _baseProductUrl = 'sell.moonpay.com';
21
+ static String get _apiKey => secrets.moonPayApiKey;
22
+ static String get _secretKey => secrets.moonPaySecretKey;
23
+ final bool isTest;
24
+ final String baseUrl;
25
+
26
+ Future<String> requestUrl({CryptoCurrency currency, String refundWalletAddress}) async {
27
+ final originalUri = Uri.https(
28
+ baseUrl, '', <String, dynamic>{
29
+ 'apiKey': _apiKey,
30
+ 'defaultBaseCurrencyCode': currency.toString().toLowerCase(),
31
+ 'refundWalletAddress': refundWalletAddress
32
+ });
33
+ final messageBytes = utf8.encode('?${originalUri.query}');
34
+ final key = utf8.encode(_secretKey);
35
+ final hmac = Hmac(sha256, key);
36
+ final digest = hmac.convert(messageBytes);
37
+ final signature = base64.encode(digest.bytes);
38
+
39
+ if (isTest) {
40
+ return originalUri.toString();
41
+ }
42
+
43
+ final query = Map<String, dynamic>.from(originalUri.queryParameters);
44
+ query['signature'] = signature;
45
+ final signedUri = originalUri.replace(queryParameters: query);
46
+ return signedUri.toString();
47
+ }
48
+}
49
50
class MoonPayBuyProvider extends BuyProvider {
51
MoonPayBuyProvider({WalletBase wallet, bool isTestEnvironment = false})
lib/src/screens/dashboard/dashboard_page.dart
+7
-4
@@ -24,6 +24,8 @@ import 'package:smooth_page_indicator/smooth_page_indicator.dart';
24
import 'package:flutter_spinkit/flutter_spinkit.dart';
25
import 'package:cake_wallet/main.dart';
26
import 'package:cake_wallet/router.dart';
27
+import 'package:cake_wallet/buy/moonpay/moonpay_buy_provider.dart';
28
+import 'package:url_launcher/url_launcher.dart';
29
30
class DashboardPage extends BasePage {
31
DashboardPage({
@@ -261,10 +263,11 @@ class DashboardPage extends BasePage {
263
264
switch (walletType) {
265
case WalletType.bitcoin:
264
- Navigator.of(context).pushNamed(Routes.preOrder);
265
- break;
266
- case WalletType.litecoin:
267
- Navigator.of(context).pushNamed(Routes.preOrder);
266
+ final moonPaySellProvider = MoonPaySellProvider();
267
+ final uri = await moonPaySellProvider.requestUrl(
268
+ currency: walletViewModel.wallet.currency,
269
+ refundWalletAddress: walletViewModel.wallet.walletAddresses.address);
270
+ await launch(uri);
271
break;
272
default:
273
await showPopUp<void>(