Skip to content

Commit

Permalink
Use the latest version of the DFX Auth Api to minimize the number of …
Browse files Browse the repository at this point in the history
…API calls (#1410)
  • Loading branch information
konstantinullrich authored Apr 25, 2024
1 parent 3732a4c commit 190c8e0
Showing 1 changed file with 23 additions and 57 deletions.
80 changes: 23 additions & 57 deletions lib/buy/dfx/dfx_buy_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class DFXBuyProvider extends BuyProvider {
: super(wallet: wallet, isTestEnvironment: isTestEnvironment);

static const _baseUrl = 'api.dfx.swiss';
static const _authPath = '/v1/auth/signMessage';
static const _signUpPath = '/v1/auth/signUp';
static const _signInPath = '/v1/auth/signIn';
// static const _signMessagePath = '/v1/auth/signMessage';
static const _authPath = '/v1/auth';
static const walletName = 'CakeWallet';

@override
Expand Down Expand Up @@ -73,21 +72,25 @@ class DFXBuyProvider extends BuyProvider {
String get walletAddress =>
wallet.walletAddresses.primaryAddress ?? wallet.walletAddresses.address;

Future<String> getSignMessage() async {
final uri = Uri.https(_baseUrl, _authPath, {'address': walletAddress});

var response = await http.get(uri, headers: {'accept': 'application/json'});

if (response.statusCode == 200) {
final responseBody = jsonDecode(response.body);
return responseBody['message'] as String;
} else {
throw Exception(
'Failed to get sign message. Status: ${response.statusCode} ${response.body}');
}
}

Future<String> signUp() async {
Future<String> getSignMessage() async =>
"By_signing_this_message,_you_confirm_that_you_are_the_sole_owner_of_the_provided_Blockchain_address._Your_ID:_$walletAddress";

// // Lets keep this just in case, but we can avoid this API Call
// Future<String> getSignMessage() async {
// final uri = Uri.https(_baseUrl, _signMessagePath, {'address': walletAddress});
//
// final response = await http.get(uri, headers: {'accept': 'application/json'});
//
// if (response.statusCode == 200) {
// final responseBody = jsonDecode(response.body);
// return responseBody['message'] as String;
// } else {
// throw Exception(
// 'Failed to get sign message. Status: ${response.statusCode} ${response.body}');
// }
// }

Future<String> auth() async {
final signMessage = getSignature(await getSignMessage());

final requestBody = jsonEncode({
Expand All @@ -96,7 +99,7 @@ class DFXBuyProvider extends BuyProvider {
'signature': signMessage,
});

final uri = Uri.https(_baseUrl, _signUpPath);
final uri = Uri.https(_baseUrl, _authPath);
var response = await http.post(
uri,
headers: {'Content-Type': 'application/json'},
Expand All @@ -115,33 +118,6 @@ class DFXBuyProvider extends BuyProvider {
}
}

Future<String> signIn() async {
final signMessage = getSignature(await getSignMessage());

final requestBody = jsonEncode({
'address': walletAddress,
'signature': signMessage,
});

final uri = Uri.https(_baseUrl, _signInPath);
var response = await http.post(
uri,
headers: {'Content-Type': 'application/json'},
body: requestBody,
);

if (response.statusCode == 201) {
final responseBody = jsonDecode(response.body);
return responseBody['accessToken'] as String;
} else if (response.statusCode == 403) {
final responseBody = jsonDecode(response.body);
final message = responseBody['message'] ?? 'Service unavailable in your country';
throw Exception(message);
} else {
throw Exception('Failed to sign in. Status: ${response.statusCode} ${response.body}');
}
}

String getSignature(String message) {
switch (wallet.type) {
case WalletType.ethereum:
Expand All @@ -164,17 +140,7 @@ class DFXBuyProvider extends BuyProvider {
final blockchain = this.blockchain;
final actionType = isBuyAction == true ? '/buy' : '/sell';

String accessToken;

try {
accessToken = await signUp();
} on Exception catch (e) {
if (e.toString().contains('409')) {
accessToken = await signIn();
} else {
rethrow;
}
}
final accessToken = await auth();

final uri = Uri.https('services.dfx.swiss', actionType, {
'session': accessToken,
Expand Down

0 comments on commit 190c8e0

Please sign in to comment.