Skip to content

Commit

Permalink
Merge branch 'TALAO' into TALAO_android
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkbee1 committed Apr 18, 2024
2 parents ec53815 + 474d311 commit 00ae429
Show file tree
Hide file tree
Showing 33 changed files with 800 additions and 425 deletions.
64 changes: 54 additions & 10 deletions lib/app/shared/dio_client/dio_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ import 'dart:convert';
import 'package:altme/app/app.dart';
import 'package:dio/dio.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:secure_storage/secure_storage.dart';
part 'logging.dart';

const _defaultConnectTimeout = Duration(minutes: 1);
const _defaultReceiveTimeout = Duration(minutes: 1);

class DioClient {
DioClient(this.baseUrl, this.dio) {
DioClient({
this.baseUrl,
required this.secureStorageProvider,
required this.dio,
}) {
if (baseUrl != null) {
dio.options.baseUrl = baseUrl!;
}

dio
..options.baseUrl = baseUrl
..options.connectTimeout = _defaultConnectTimeout
..options.receiveTimeout = _defaultReceiveTimeout
..httpClientAdapter
Expand All @@ -31,8 +39,9 @@ class DioClient {

final log = getLogger('DioClient');

final String baseUrl;
final SecureStorageProvider secureStorageProvider;
final Dio dio;
String? baseUrl;

Future<dynamic> get(
String uri, {
Expand All @@ -43,6 +52,7 @@ class DioClient {
Map<String, dynamic> headers = const <String, dynamic>{
'Content-Type': 'application/json; charset=UTF-8',
},
bool isCachingEnabled = false,
}) async {
try {
final isInternetAvailable = await isConnected();
Expand All @@ -54,13 +64,47 @@ class DioClient {

final stopwatch = Stopwatch()..start();
await getSpecificHeader(uri, headers);
final response = await dio.get<dynamic>(
uri,
queryParameters: queryParameters,
options: options,
cancelToken: cancelToken,
onReceiveProgress: onReceiveProgress,
);
log.i('uri - $uri');

final cachedData = await secureStorageProvider.get(uri);
dynamic response;

if (!isCachingEnabled || cachedData == null) {
response = await dio.get<dynamic>(
uri,
queryParameters: queryParameters,
options: options,
cancelToken: cancelToken,
onReceiveProgress: onReceiveProgress,
);
} else {
final cachedDataJson = jsonDecode(cachedData);
final expiry = int.parse(cachedDataJson['expiry'].toString());

final isExpired = DateTime.now().millisecondsSinceEpoch > expiry;

if (isExpired) {
response = await dio.get<dynamic>(
uri,
queryParameters: queryParameters,
options: options,
cancelToken: cancelToken,
onReceiveProgress: onReceiveProgress,
);
} else {
/// directly return cached data
/// returned here to avoid the caching override everytime
final response = await cachedDataJson['data'];
log.i('Time - ${stopwatch.elapsed}');
return response;
}
}
final expiry =
DateTime.now().add(const Duration(days: 2)).millisecondsSinceEpoch;

final value = {'expiry': expiry, 'data': response.data};
await secureStorageProvider.set(uri, jsonEncode(value));

log.i('Time - ${stopwatch.elapsed}');
return response.data;
} on FormatException catch (_) {
Expand Down
104 changes: 61 additions & 43 deletions lib/app/shared/helper_functions/helper_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:io';
import 'package:altme/app/app.dart';
import 'package:altme/dashboard/dashboard.dart';
import 'package:altme/oidc4vc/oidc4vc.dart';
import 'package:altme/selective_disclosure/selective_disclosure.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:convert/convert.dart';
import 'package:credential_manifest/credential_manifest.dart';
Expand Down Expand Up @@ -693,7 +694,6 @@ Future<
final OpenIdConfiguration openIdConfiguration = await oidc4vc.getOpenIdConfig(
baseUrl: issuer,
isAuthorizationServer: false,
oidc4vciDraftType: oidc4vciDraftType,
);

if (preAuthorizedCode == null) {
Expand All @@ -718,7 +718,6 @@ Future<
authorizationServerConfiguration = await oidc4vc.getOpenIdConfig(
baseUrl: authorizationServer,
isAuthorizationServer: true,
oidc4vciDraftType: oidc4vciDraftType,
);
}

Expand Down Expand Up @@ -969,7 +968,6 @@ Future<bool?> isEBSIV3ForVerifiers({
await oidc4vc.getOpenIdConfig(
baseUrl: clientId,
isAuthorizationServer: false,
oidc4vciDraftType: oidc4vciDraftType,
);

final subjectTrustFrameworksSupported =
Expand Down Expand Up @@ -1798,6 +1796,66 @@ List<dynamic> collectSdValues(Map<String, dynamic> data) {
return result;
}

Map<String, dynamic> createJsonByDecryptingSDValues({
required Map<String, dynamic> encryptedJson,
required SelectiveDisclosure selectiveDisclosure,
}) {
final json = <String, dynamic>{};

final sh256HashToContent = selectiveDisclosure.sh256HashToContent;

encryptedJson.forEach((key, value) {
if (key == '_sd') {
final sd = encryptedJson['_sd'];

if (sd is List<dynamic>) {
for (final sdValue in sd) {
if (sh256HashToContent.containsKey(sdValue)) {
final content = sh256HashToContent[sdValue];
if (content is Map) {
content.forEach((key, value) {
json[key.toString()] = value;
});
}
}
}
}
} else {
if (value is Map<String, dynamic>) {
final nestedJson = createJsonByDecryptingSDValues(
selectiveDisclosure: selectiveDisclosure,
encryptedJson: value,
);
json[key] = nestedJson;
} else if (value is List<dynamic>) {
final list = <String>[];

for (final ele in value) {
if (ele is Map) {
final threeDotValue = ele['...'];
if (sh256HashToContent.containsKey(threeDotValue)) {
final content = sh256HashToContent[threeDotValue];
if (content is Map) {
content.forEach((key, value) {
list.add(value.toString());
});
}
}
} else {
list.add(ele.toString());
}
}

json[key] = list;
} else {
json[key] = value;
}
}
});

return json;
}

Future<Map<String, dynamic>?> checkX509({
required String encodedData,
required Map<String, dynamic> header,
Expand Down Expand Up @@ -1931,43 +1989,3 @@ String? getWalletAddress(CredentialSubjectModel credentialSubjectModel) {
}
return null;
}

Future<String> getCatchedGetData({
required SecureStorageProvider secureStorageProvider,
required String url,
required Map<String, dynamic> headers,
required DioClient client,
required bool isCachingEnabled,
}) async {
final cachedData = await secureStorageProvider.get(url);

dynamic response;

if (!isCachingEnabled) {
response = await client.get(url, headers: headers);
} else if (cachedData == null) {
response = await client.get(url, headers: headers);
} else {
final cachedDataJson = jsonDecode(cachedData);
final expiry = int.parse(cachedDataJson['expiry'].toString());

final isExpired = DateTime.now().millisecondsSinceEpoch > expiry;

if (isExpired) {
response = await client.get(url, headers: headers);
} else {
/// directly return cached data
/// returned here to avoid the caching override everytime
final response = await cachedDataJson['data'];
return response.toString();
}
}

final expiry =
DateTime.now().add(const Duration(days: 2)).millisecondsSinceEpoch;

final value = {'expiry': expiry, 'data': response};
await secureStorageProvider.set(url, jsonEncode(value));

return response.toString();
}
58 changes: 43 additions & 15 deletions lib/app/view/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,18 @@ class App extends StatelessWidget {
create: (context) => KycVerificationCubit(
profileCubit: context.read<ProfileCubit>(),
client: DioClient(
'',
Dio(),
secureStorageProvider: secureStorageProvider,
dio: Dio(),
),
),
),
BlocProvider<HomeCubit>(
create: (context) => HomeCubit(
client: DioClient(Urls.issuerBaseUrl, Dio()),
client: DioClient(
baseUrl: Urls.issuerBaseUrl,
secureStorageProvider: secureStorageProvider,
dio: Dio(),
),
secureStorageProvider: secureStorageProvider,
oidc4vc: OIDC4VC(),
didKitProvider: DIDKitProvider(),
Expand Down Expand Up @@ -139,7 +143,10 @@ class App extends StatelessWidget {
),
BlocProvider<PolygonIdCubit>(
create: (context) => PolygonIdCubit(
client: DioClient('', Dio()),
client: DioClient(
secureStorageProvider: secureStorageProvider,
dio: Dio(),
),
secureStorageProvider: secureStorageProvider,
polygonId: PolygonId(),
credentialsCubit: context.read<CredentialsCubit>(),
Expand All @@ -149,14 +156,21 @@ class App extends StatelessWidget {
),
BlocProvider<EnterpriseCubit>(
create: (context) => EnterpriseCubit(
client: DioClient('', Dio()),
client: DioClient(
secureStorageProvider: secureStorageProvider,
dio: Dio(),
),
profileCubit: context.read<ProfileCubit>(),
credentialsCubit: context.read<CredentialsCubit>(),
),
),
BlocProvider<ScanCubit>(
create: (context) => ScanCubit(
client: DioClient(Urls.checkIssuerTalaoUrl, Dio()),
client: DioClient(
baseUrl: Urls.checkIssuerTalaoUrl,
secureStorageProvider: secureStorageProvider,
dio: Dio(),
),
credentialsCubit: context.read<CredentialsCubit>(),
didKitProvider: DIDKitProvider(),
secureStorageProvider: secureStorageProvider,
Expand All @@ -168,8 +182,15 @@ class App extends StatelessWidget {
),
BlocProvider<QRCodeScanCubit>(
create: (context) => QRCodeScanCubit(
client: DioClient(Urls.checkIssuerTalaoUrl, Dio()),
requestClient: DioClient('', Dio()),
client: DioClient(
baseUrl: Urls.checkIssuerTalaoUrl,
secureStorageProvider: secureStorageProvider,
dio: Dio(),
),
requestClient: DioClient(
secureStorageProvider: secureStorageProvider,
dio: Dio(),
),
scanCubit: context.read<ScanCubit>(),
queryByExampleCubit: context.read<QueryByExampleCubit>(),
deepLinkCubit: context.read<DeepLinkCubit>(),
Expand All @@ -190,8 +211,9 @@ class App extends StatelessWidget {
create: (context) => AllTokensCubit(
secureStorageProvider: secureStorageProvider,
client: DioClient(
Urls.coinGeckoBase,
Dio(),
baseUrl: Urls.coinGeckoBase,
secureStorageProvider: secureStorageProvider,
dio: Dio(),
),
),
),
Expand All @@ -206,17 +228,19 @@ class App extends StatelessWidget {
context.read<MnemonicNeedVerificationCubit>(),
secureStorageProvider: secureStorageProvider,
client: DioClient(
context.read<ManageNetworkCubit>().state.network.apiUrl,
Dio(),
baseUrl: context.read<ManageNetworkCubit>().state.network.apiUrl,
secureStorageProvider: secureStorageProvider,
dio: Dio(),
),
walletCubit: context.read<WalletCubit>(),
),
),
BlocProvider<NftCubit>(
create: (context) => NftCubit(
client: DioClient(
context.read<ManageNetworkCubit>().state.network.apiUrl,
Dio(),
baseUrl: context.read<ManageNetworkCubit>().state.network.apiUrl,
secureStorageProvider: secureStorageProvider,
dio: Dio(),
),
walletCubit: context.read<WalletCubit>(),
manageNetworkCubit: context.read<ManageNetworkCubit>(),
Expand All @@ -236,7 +260,11 @@ class App extends StatelessWidget {
homeCubit: context.read<HomeCubit>(),
walletCubit: context.read<WalletCubit>(),
credentialsCubit: context.read<CredentialsCubit>(),
client: DioClient(Urls.checkIssuerTalaoUrl, Dio()),
client: DioClient(
baseUrl: Urls.checkIssuerTalaoUrl,
secureStorageProvider: secureStorageProvider,
dio: Dio(),
),
altmeChatSupportCubit: context.read<AltmeChatSupportCubit>(),
profileCubit: context.read<ProfileCubit>(),
),
Expand Down
5 changes: 4 additions & 1 deletion lib/chat_room/matrix_chat/matrix_chat_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ class MatrixChatImpl extends MatrixChatInterface {
factory MatrixChatImpl() {
_instance ??= MatrixChatImpl._(
didKitProvider: DIDKitProvider(),
dioClient: DioClient('', Dio()),
dioClient: DioClient(
secureStorageProvider: getSecureStorage,
dio: Dio(),
),
secureStorageProvider: getSecureStorage,
oidc4vc: OIDC4VC(),
);
Expand Down
Loading

0 comments on commit 00ae429

Please sign in to comment.