Use the latest version of the DFX Auth Api to minimize the number of API calls (#1410)

Konstantin Ullrich committed Apr 25, 2024 at 06:06 UTC 190c8e06b9c2121e4694ccdd048b3daf61f99270
1 file changed +23 -57
lib/buy/dfx/dfx_buy_provider.dart
+23 -57
@@ -17,9 +17,8 @@ class DFXBuyProvider extends BuyProvider {
17 : super(wallet: wallet, isTestEnvironment: isTestEnvironment);
18
19 static const _baseUrl = 'api.dfx.swiss';
20 - static const _authPath = '/v1/auth/signMessage';
21 - static const _signUpPath = '/v1/auth/signUp';
22 - static const _signInPath = '/v1/auth/signIn';
20 + // static const _signMessagePath = '/v1/auth/signMessage';
21 + static const _authPath = '/v1/auth';
22 static const walletName = 'CakeWallet';
23
24 @override
@@ -73,21 +72,25 @@ class DFXBuyProvider extends BuyProvider {
72 String get walletAddress =>
73 wallet.walletAddresses.primaryAddress ?? wallet.walletAddresses.address;
74
76 - Future<String> getSignMessage() async {
77 - final uri = Uri.https(_baseUrl, _authPath, {'address': walletAddress});
78 -
79 - var response = await http.get(uri, headers: {'accept': 'application/json'});
80 -
81 - if (response.statusCode == 200) {
82 - final responseBody = jsonDecode(response.body);
83 - return responseBody['message'] as String;
84 - } else {
85 - throw Exception(
86 - 'Failed to get sign message. Status: ${response.statusCode} ${response.body}');
87 - }
88 - }
89 -
90 - Future<String> signUp() async {
75 + Future<String> getSignMessage() async =>
76 + "By_signing_this_message,_you_confirm_that_you_are_the_sole_owner_of_the_provided_Blockchain_address._Your_ID:_$walletAddress";
77 +
78 + // // Lets keep this just in case, but we can avoid this API Call
79 + // Future<String> getSignMessage() async {
80 + // final uri = Uri.https(_baseUrl, _signMessagePath, {'address': walletAddress});
81 + //
82 + // final response = await http.get(uri, headers: {'accept': 'application/json'});
83 + //
84 + // if (response.statusCode == 200) {
85 + // final responseBody = jsonDecode(response.body);
86 + // return responseBody['message'] as String;
87 + // } else {
88 + // throw Exception(
89 + // 'Failed to get sign message. Status: ${response.statusCode} ${response.body}');
90 + // }
91 + // }
92 +
93 + Future<String> auth() async {
94 final signMessage = getSignature(await getSignMessage());
95
96 final requestBody = jsonEncode({
@@ -96,7 +99,7 @@ class DFXBuyProvider extends BuyProvider {
99 'signature': signMessage,
100 });
101
99 - final uri = Uri.https(_baseUrl, _signUpPath);
102 + final uri = Uri.https(_baseUrl, _authPath);
103 var response = await http.post(
104 uri,
105 headers: {'Content-Type': 'application/json'},
@@ -115,33 +118,6 @@ class DFXBuyProvider extends BuyProvider {
118 }
119 }
120
118 - Future<String> signIn() async {
119 - final signMessage = getSignature(await getSignMessage());
120 -
121 - final requestBody = jsonEncode({
122 - 'address': walletAddress,
123 - 'signature': signMessage,
124 - });
125 -
126 - final uri = Uri.https(_baseUrl, _signInPath);
127 - var response = await http.post(
128 - uri,
129 - headers: {'Content-Type': 'application/json'},
130 - body: requestBody,
131 - );
132 -
133 - if (response.statusCode == 201) {
134 - final responseBody = jsonDecode(response.body);
135 - return responseBody['accessToken'] as String;
136 - } else if (response.statusCode == 403) {
137 - final responseBody = jsonDecode(response.body);
138 - final message = responseBody['message'] ?? 'Service unavailable in your country';
139 - throw Exception(message);
140 - } else {
141 - throw Exception('Failed to sign in. Status: ${response.statusCode} ${response.body}');
142 - }
143 - }
144 -
121 String getSignature(String message) {
122 switch (wallet.type) {
123 case WalletType.ethereum:
@@ -164,17 +140,7 @@ class DFXBuyProvider extends BuyProvider {
140 final blockchain = this.blockchain;
141 final actionType = isBuyAction == true ? '/buy' : '/sell';
142
167 - String accessToken;
168 -
169 - try {
170 - accessToken = await signUp();
171 - } on Exception catch (e) {
172 - if (e.toString().contains('409')) {
173 - accessToken = await signIn();
174 - } else {
175 - rethrow;
176 - }
177 - }
143 + final accessToken = await auth();
144
145 final uri = Uri.https('services.dfx.swiss', actionType, {
146 'session': accessToken,