Cw 523 robinhood enhancements (#1176)

* CW-523 Add BCH support to Robinhood * CW-523 Only show popup on Robinhood error * CW-523 Add BitcoinCash to Robinhood Provider

Konstantin Ullrich committed Nov 17, 2023 at 19:37 UTC 539eb9b3eb27306c5b3b578f17408ecf0dd4e242
4 files changed +28 -9
cw_bitcoin_cash/lib/src/bitcoin_cash_address_utils.dart
+1
@@ -2,4 +2,5 @@ import 'package:bitbox/bitbox.dart' as bitbox;
2
3 class AddressUtils {
4 static String getCashAddrFormat(String address) => bitbox.Address.toCashAddress(address);
5 + static String toLegacyAddress(String address) => bitbox.Address.toLegacyAddress(address);
6 }
cw_bitcoin_cash/lib/src/bitcoin_cash_wallet.dart
+14
@@ -1,3 +1,5 @@
1 +import 'dart:convert';
2 +
3 import 'package:bitbox/bitbox.dart' as bitbox;
4 import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
5 import 'package:cw_bitcoin/bitcoin_address_record.dart';
@@ -294,4 +296,16 @@ abstract class BitcoinCashWalletBase extends ElectrumWallet with Store {
296
297 return 0;
298 }
299 +
300 + @override
301 + String signMessage(String message, {String? address = null}) {
302 + final index = address != null
303 + ? walletAddresses.addresses
304 + .firstWhere((element) => element.address == AddressUtils.toLegacyAddress(address))
305 + .index
306 + : null;
307 + return index == null
308 + ? base64Encode(hd.sign(message))
309 + : base64Encode(hd.derive(index).sign(message));
310 + }
311 }
lib/buy/robinhood/robinhood_buy_provider.dart
+12 -8
@@ -3,7 +3,6 @@ import 'dart:convert';
3 import 'package:cake_wallet/.secrets.g.dart' as secrets;
4 import 'package:cake_wallet/generated/i18n.dart';
5 import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
6 -import 'package:cake_wallet/utils/exception_handler.dart';
6 import 'package:cake_wallet/utils/show_pop_up.dart';
7 import 'package:cw_core/wallet_base.dart';
8 import 'package:cw_core/wallet_type.dart';
@@ -12,8 +11,7 @@ import 'package:http/http.dart' as http;
11 import 'package:url_launcher/url_launcher.dart';
12
13 class RobinhoodBuyProvider {
15 - RobinhoodBuyProvider({required WalletBase wallet})
16 - : this._wallet = wallet;
14 + RobinhoodBuyProvider({required WalletBase wallet}) : this._wallet = wallet;
15
16 final WalletBase _wallet;
17
@@ -21,10 +19,15 @@ class RobinhoodBuyProvider {
19 static const _cIdBaseUrl = 'exchange-helper.cakewallet.com';
20
21 String get _applicationId => secrets.robinhoodApplicationId;
22 +
23 String get _apiSecret => secrets.robinhoodCIdApiSecret;
24
26 - bool get isAvailable =>
27 - [WalletType.bitcoin, WalletType.litecoin, WalletType.ethereum].contains(_wallet.type);
25 + bool get isAvailable => [
26 + WalletType.bitcoin,
27 + WalletType.bitcoinCash,
28 + WalletType.litecoin,
29 + WalletType.ethereum
30 + ].contains(_wallet.type);
31
32 String getSignature(String message) {
33 switch (_wallet.type) {
@@ -32,6 +35,7 @@ class RobinhoodBuyProvider {
35 return _wallet.signMessage(message);
36 case WalletType.litecoin:
37 case WalletType.bitcoin:
38 + case WalletType.bitcoinCash:
39 return _wallet.signMessage(message, address: _wallet.walletAddresses.address);
40 default:
41 throw Exception("WalletType is not available for Robinhood ${_wallet.type}");
@@ -55,7 +59,8 @@ class RobinhoodBuyProvider {
59 if (response.statusCode == 200) {
60 return (jsonDecode(response.body) as Map<String, dynamic>)['connectId'] as String;
61 } else {
58 - throw Exception('Provider currently unavailable. Status: ${response.statusCode} ${response.body}');
62 + throw Exception(
63 + 'Provider currently unavailable. Status: ${response.statusCode} ${response.body}');
64 }
65 }
66
@@ -76,8 +81,7 @@ class RobinhoodBuyProvider {
81 try {
82 final uri = await requestUrl();
83 await launchUrl(uri, mode: LaunchMode.externalApplication);
79 - } catch (e, s) {
80 - ExceptionHandler.onError(FlutterErrorDetails(exception: e, stack: s));
84 + } catch (_) {
85 await showPopUp<void>(
86 context: context,
87 builder: (BuildContext context) {
lib/entities/main_actions.dart
+1 -1
@@ -52,7 +52,7 @@ class MainActions {
52 case WalletType.bitcoin:
53 case WalletType.litecoin:
54 case WalletType.ethereum:
55 - // case WalletType.bitcoinCash: // TODO: add sign message function to BCH first
55 + case WalletType.bitcoinCash:
56 switch (defaultBuyProvider) {
57 case BuyProviderType.AskEachTime:
58 Navigator.pushNamed(context, Routes.buy);