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;
}
@override
String get primaryAddress => getAddress(index: 0, hd: mainHd);
@override
set address(String addr) => null;

View file

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