Finish DFX auth flow with the primary address for Electrum (bitcoin) (#1264)

This commit is contained in:
Omar Hatem 2024-01-11 17:29:51 +02:00 committed by GitHub
parent cbaa299dda
commit 8d973cf919
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 15 deletions

View file

@ -62,6 +62,9 @@ abstract class ElectrumWalletAddressesBase extends WalletAddresses with Store {
return walletInfo.type == WalletType.bitcoinCash ? toCashAddr(receiveAddress) : receiveAddress; return walletInfo.type == WalletType.bitcoinCash ? toCashAddr(receiveAddress) : receiveAddress;
} }
@override
String get primaryAddress => getAddress(index: 0, hd: mainHd);
@override @override
set address(String addr) => null; set address(String addr) => null;

View file

@ -10,6 +10,8 @@ abstract class WalletAddresses {
String get address; String get address;
String? get primaryAddress => null;
set address(String address); set address(String address);
Map<String, String> addressesMap; Map<String, String> addressesMap;

View file

@ -66,8 +66,10 @@ class DFXBuyProvider extends BuyProvider {
} }
} }
String get walletAddress =>
wallet.walletAddresses.primaryAddress ?? wallet.walletAddresses.address;
Future<String> getSignMessage() async { Future<String> getSignMessage() async {
final walletAddress = wallet.walletAddresses.address;
final uri = Uri.https(_baseUrl, _authPath, {'address': walletAddress}); final uri = Uri.https(_baseUrl, _authPath, {'address': walletAddress});
var response = await http.get(uri, headers: {'accept': 'application/json'}); var response = await http.get(uri, headers: {'accept': 'application/json'});
@ -83,7 +85,6 @@ class DFXBuyProvider extends BuyProvider {
Future<String> signUp() async { Future<String> signUp() async {
final signMessage = getSignature(await getSignMessage()); final signMessage = getSignature(await getSignMessage());
final walletAddress = wallet.walletAddresses.address;
final requestBody = jsonEncode({ final requestBody = jsonEncode({
'wallet': walletName, 'wallet': walletName,
@ -92,8 +93,11 @@ class DFXBuyProvider extends BuyProvider {
}); });
final uri = Uri.https(_baseUrl, _signUpPath); final uri = Uri.https(_baseUrl, _signUpPath);
var response = await http.post(uri, var response = await http.post(
headers: {'Content-Type': 'application/json'}, body: requestBody); uri,
headers: {'Content-Type': 'application/json'},
body: requestBody,
);
if (response.statusCode == 201) { if (response.statusCode == 201) {
final responseBody = jsonDecode(response.body); final responseBody = jsonDecode(response.body);
@ -103,14 +107,12 @@ class DFXBuyProvider extends BuyProvider {
final message = responseBody['message'] ?? 'Service unavailable in your country'; final message = responseBody['message'] ?? 'Service unavailable in your country';
throw Exception(message); throw Exception(message);
} else { } else {
throw Exception( throw Exception('Failed to sign up. Status: ${response.statusCode} ${response.body}');
'Failed to sign up. Status: ${response.statusCode} ${response.body}');
} }
} }
Future<String> signIn() async { Future<String> signIn() async {
final signMessage = getSignature(await getSignMessage()); final signMessage = getSignature(await getSignMessage());
final walletAddress = wallet.walletAddresses.address;
final requestBody = jsonEncode({ final requestBody = jsonEncode({
'address': walletAddress, 'address': walletAddress,
@ -118,8 +120,11 @@ class DFXBuyProvider extends BuyProvider {
}); });
final uri = Uri.https(_baseUrl, _signInPath); final uri = Uri.https(_baseUrl, _signInPath);
var response = await http.post(uri, var response = await http.post(
headers: {'Content-Type': 'application/json'}, body: requestBody); uri,
headers: {'Content-Type': 'application/json'},
body: requestBody,
);
if (response.statusCode == 201) { if (response.statusCode == 201) {
final responseBody = jsonDecode(response.body); final responseBody = jsonDecode(response.body);
@ -129,8 +134,7 @@ class DFXBuyProvider extends BuyProvider {
final message = responseBody['message'] ?? 'Service unavailable in your country'; final message = responseBody['message'] ?? 'Service unavailable in your country';
throw Exception(message); throw Exception(message);
} else { } else {
throw Exception( throw Exception('Failed to sign in. Status: ${response.statusCode} ${response.body}');
'Failed to sign in. Status: ${response.statusCode} ${response.body}');
} }
} }
@ -142,8 +146,7 @@ class DFXBuyProvider extends BuyProvider {
case WalletType.litecoin: case WalletType.litecoin:
case WalletType.bitcoin: case WalletType.bitcoin:
case WalletType.bitcoinCash: case WalletType.bitcoinCash:
return wallet.signMessage(message, return wallet.signMessage(message, address: walletAddress);
address: wallet.walletAddresses.address);
default: default:
throw Exception("WalletType is not available for DFX ${wallet.type}"); throw Exception("WalletType is not available for DFX ${wallet.type}");
} }
@ -178,8 +181,7 @@ class DFXBuyProvider extends BuyProvider {
if (await canLaunchUrl(uri)) { if (await canLaunchUrl(uri)) {
if (DeviceInfo.instance.isMobile) { if (DeviceInfo.instance.isMobile) {
Navigator.of(context) Navigator.of(context).pushNamed(Routes.webViewPage, arguments: [title, uri]);
.pushNamed(Routes.webViewPage, arguments: [title, uri]);
} else { } else {
await launchUrl(uri, mode: LaunchMode.externalApplication); await launchUrl(uri, mode: LaunchMode.externalApplication);
} }