refactor: optimize wallet JWT management, update security checks for debug builds, and improve pricing data parsing

This commit is contained in:
Hamza-Ayed
2026-06-27 22:50:28 +03:00
parent 43e3f8c939
commit a87cb7c082
7 changed files with 93 additions and 18 deletions

View File

@@ -142,11 +142,55 @@ class LoginDriverController extends GetxController {
}
var dev = '';
Future<String>? _walletJwtFuture;
getJwtWallet() async {
if (_walletJwtFuture != null) {
Log.print('⏳ getJwtWallet: Request already in progress, reusing future.');
return _walletJwtFuture!;
}
_walletJwtFuture = _getJwtWalletInternal();
try {
return await _walletJwtFuture!;
} finally {
_walletJwtFuture = null;
}
}
Future<String> _getJwtWalletInternal() async {
if (box.read(BoxName.security_check).toString() != 'passed') {
Log.print('Security check failed');
return;
return '';
}
// 1. Check GetStorage first to avoid redundant API calls
String? cachedJwt = box.read(BoxName.walletJwt)?.toString();
if (cachedJwt != null && cachedJwt.isNotEmpty) {
bool isTokenValid = false;
try {
final parts = cachedJwt.split('.');
if (parts.length == 3) {
var payload = parts[1];
switch (payload.length % 4) {
case 2: payload += '=='; break;
case 3: payload += '='; break;
}
final decoded = jsonDecode(utf8.decode(base64Url.decode(payload)));
final exp = decoded['exp'];
if (exp != null) {
// Check if token has at least 10 seconds remaining
isTokenValid = DateTime.now().millisecondsSinceEpoch < (exp * 1000 - 10000);
}
}
} catch (_) {}
if (isTokenValid) {
Log.print('🔑 Valid Wallet JWT found in storage. Skipping generation.');
return cachedJwt;
}
}
Log.print('Security check passed');
dev = Platform.isAndroid ? 'android' : 'ios';
var fingerPrint = box.read(BoxName.deviceFingerprint)?.toString() ??
@@ -172,8 +216,12 @@ class LoginDriverController extends GetxController {
: decoded['hmac'];
Log.print('payment["jwt"]: $jwt');
await box.write(BoxName.hmac, hmac);
return jwt.toString();
if (jwt != null) {
await box.write(BoxName.walletJwt, jwt.toString());
await box.write(BoxName.hmac, hmac);
return jwt.toString();
}
return '';
}
getJWT() async {