Skip to content

Commit

Permalink
feat(example app): qr code parser
Browse files Browse the repository at this point in the history
parser to read iden3message from qrCode, currently the iden3message can be in three formats, encoded base64, raw json and url from where to retrieve the json
  • Loading branch information
emuroni committed Nov 27, 2023
1 parent a076457 commit 15750e3
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:polygonid_flutter_sdk_example/src/presentation/ui/home/home_bloc
import 'package:polygonid_flutter_sdk_example/src/presentation/ui/restore_identity/bloc/restore_identity_bloc.dart';
import 'package:polygonid_flutter_sdk_example/src/presentation/ui/sign/sign_bloc.dart';
import 'package:polygonid_flutter_sdk_example/src/presentation/ui/splash/splash_bloc.dart';
import 'package:polygonid_flutter_sdk_example/utils/qr_code_parser_utils.dart';

final getIt = GetIt.instance;

Expand All @@ -34,6 +35,7 @@ Future<void> init() async {
registerIdentityDependencies();
registerBackupIdentityDependencies();
registerRestoreIdentityDependencies();
registerUtilities();
}

void registerEnv() {
Expand Down Expand Up @@ -83,7 +85,11 @@ void registerHomeDependencies() {

///
void registerClaimsDependencies() {
getIt.registerFactory(() => ClaimsBloc(getIt(), getIt()));
getIt.registerFactory(() => ClaimsBloc(
getIt(),
getIt(),
getIt(),
));
}

///
Expand All @@ -93,7 +99,7 @@ void registerClaimDetailDependencies() {

///
void registerAuthDependencies() {
getIt.registerFactory(() => AuthBloc(getIt()));
getIt.registerFactory(() => AuthBloc(getIt(), getIt()));
}

///
Expand Down Expand Up @@ -125,3 +131,9 @@ void registerRestoreIdentityDependencies() {
getIt
.registerFactory<RestoreIdentityBloc>(() => RestoreIdentityBloc(getIt()));
}

/// Register utilities
void registerUtilities() {
getIt.registerLazySingleton<QrcodeParserUtils>(
() => QrcodeParserUtils(getIt()));
}
11 changes: 8 additions & 3 deletions example/lib/src/presentation/ui/auth/auth_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ import 'package:polygonid_flutter_sdk_example/src/presentation/ui/auth/auth_even
import 'package:polygonid_flutter_sdk_example/src/presentation/ui/auth/auth_state.dart';
import 'package:polygonid_flutter_sdk_example/src/presentation/ui/common/widgets/profile_radio_button.dart';
import 'package:polygonid_flutter_sdk_example/utils/nonce_utils.dart';
import 'package:polygonid_flutter_sdk_example/utils/qr_code_parser_utils.dart';
import 'package:polygonid_flutter_sdk_example/utils/secure_storage_keys.dart';

class AuthBloc extends Bloc<AuthEvent, AuthState> {
final PolygonIdSdk _polygonIdSdk;
final QrcodeParserUtils _qrcodeParserUtils;

static const SelectedProfile _defaultProfile = SelectedProfile.public;
SelectedProfile selectedProfile = _defaultProfile;

AuthBloc(this._polygonIdSdk) : super(const AuthState.initial()) {
AuthBloc(
this._polygonIdSdk,
this._qrcodeParserUtils,
) : super(const AuthState.initial()) {
on<ClickScanQrCodeEvent>(_handleClickScanQrCode);
on<ScanQrCodeResponse>(_handleScanQrCodeResponse);
on<ProfileSelectedEvent>(_handleProfileSelected);
Expand Down Expand Up @@ -53,8 +58,8 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
}

try {
final Iden3MessageEntity iden3message = await _polygonIdSdk.iden3comm
.getIden3Message(message: qrCodeResponse);
final Iden3MessageEntity iden3message = await _qrcodeParserUtils
.getIden3MessageFromQrCode(qrCodeResponse);
emit(AuthState.loaded(iden3message));

String? privateKey =
Expand Down
7 changes: 5 additions & 2 deletions example/lib/src/presentation/ui/claims/claims_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ import 'package:polygonid_flutter_sdk_example/src/presentation/ui/claims/mappers
import 'package:polygonid_flutter_sdk_example/src/presentation/ui/claims/models/claim_model.dart';
import 'package:polygonid_flutter_sdk_example/utils/custom_strings.dart';
import 'package:polygonid_flutter_sdk_example/utils/nonce_utils.dart';
import 'package:polygonid_flutter_sdk_example/utils/qr_code_parser_utils.dart';
import 'package:polygonid_flutter_sdk_example/utils/secure_storage_keys.dart';

class ClaimsBloc extends Bloc<ClaimsEvent, ClaimsState> {
final ClaimModelMapper _mapper;
final PolygonIdSdk _polygonIdSdk;
final QrcodeParserUtils _qrcodeParserUtils;

ClaimsBloc(
this._mapper,
this._polygonIdSdk,
this._qrcodeParserUtils,
) : super(const ClaimsState.initial()) {
on<FetchAndSaveClaimsEvent>(_fetchAndSaveClaims);
on<GetClaimsEvent>(_getClaims);
Expand Down Expand Up @@ -322,8 +325,8 @@ class ClaimsBloc extends Bloc<ClaimsEvent, ClaimsState> {
}

try {
final Iden3MessageEntity iden3message = await _polygonIdSdk.iden3comm
.getIden3Message(message: qrCodeResponse!);
final Iden3MessageEntity iden3message = await _qrcodeParserUtils
.getIden3MessageFromQrCode(qrCodeResponse!);
emit(ClaimsState.qrCodeScanned(iden3message));
} catch (error) {
emit(const ClaimsState.error("Scanned code is not valid"));
Expand Down
56 changes: 56 additions & 0 deletions example/lib/utils/qr_code_parser_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'dart:convert';

import 'package:http/http.dart';
import 'package:polygonid_flutter_sdk/iden3comm/domain/entities/common/iden3_message_entity.dart';
import 'package:polygonid_flutter_sdk/sdk/polygon_id_sdk.dart';

class QrcodeParserUtils {
final PolygonIdSdk _polygonIdSdk;

QrcodeParserUtils(this._polygonIdSdk);

///
Future<Iden3MessageEntity> getIden3MessageFromQrCode(String message) async {
try {
String rawMessage = message;
if (message.startsWith("iden3comm://?i_m")) {
rawMessage = await _getMessageFromBase64(message);
}

if (message.startsWith("iden3comm://?request_uri")) {
rawMessage = await _getMessageFromRemote(message);
}

Iden3MessageEntity? _iden3Message =
await _polygonIdSdk.iden3comm.getIden3Message(message: rawMessage);
return _iden3Message;
} catch (error) {
throw Exception("Error while processing the QR code");
}
}

///
Future<String> _getMessageFromRemote(String message) async {
try {
message = message.replaceAll("iden3comm://?request_uri=", "");
Response response = await get(Uri.parse(message));
if (response.statusCode != 200) {
throw Exception("Error while getting the message from the remote");
}
return response.body;
} catch (error) {
throw Exception("Error while getting the message from the remote");
}
}

///
Future<String> _getMessageFromBase64(String message) async {
try {
message = message.replaceAll("iden3comm://?i_m=", "");
String decodedMessage = String.fromCharCodes(base64.decode(message));
return decodedMessage;
} catch (error) {
throw Exception("Error while getting the message from the base64");
}
}
}

0 comments on commit 15750e3

Please sign in to comment.