Skip to content

Commit

Permalink
feat: Choose VC among several type of VC's #1817
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed Aug 28, 2023
1 parent cc2e0b9 commit 465e80d
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,5 @@ enum ResponseString {
RESPONSE_STRING_pleaseSwitchPolygonNetwork,
RESPONSE_STRING_pleaseSwitchToCorrectOIDC4VCProfile,
RESPONSE_STRING_authenticationSuccess,
RESPONSE_STRING_youcanSelectOnlyXCredential,
}
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ extension ResponseStringX on ResponseString {

case ResponseString.RESPONSE_STRING_authenticationSuccess:
return globalMessage.RESPONSE_STRING_authenticationSuccess;

case ResponseString.RESPONSE_STRING_youcanSelectOnlyXCredential:
return globalMessage.RESPONSE_STRING_youcanSelectOnlyXCredential(
injectedMessage ?? '',
);
}
}
}
3 changes: 3 additions & 0 deletions lib/app/shared/message_handler/global_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,7 @@ class GlobalMessage {
l10n.pleaseSwitchToCorrectOIDC4VCProfile;
String get RESPONSE_STRING_authenticationSuccess =>
l10n.authenticationSuccess;

String RESPONSE_STRING_youcanSelectOnlyXCredential(String value) =>
l10n.youcanSelectOnlyXCredential(value);
}
7 changes: 7 additions & 0 deletions lib/app/shared/message_handler/response_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,13 @@ class ResponseMessage with MessageHandler {
return ResponseString.RESPONSE_STRING_authenticationSuccess.localise(
context,
);

case ResponseString.RESPONSE_STRING_youcanSelectOnlyXCredential:
return ResponseString.RESPONSE_STRING_youcanSelectOnlyXCredential
.localise(
context,
injectedMessage: injectedMessage,
);
}
}
return '';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:math';

import 'package:altme/app/shared/shared.dart';
import 'package:altme/dashboard/dashboard.dart';
import 'package:credential_manifest/credential_manifest.dart';
import 'package:equatable/equatable.dart';
Expand Down Expand Up @@ -30,6 +33,45 @@ class CredentialManifestPickCubit extends Cubit<CredentialManifestPickState> {
required int inputDescriptorIndex,
required bool? isJwtVpInJwtVCRequired,
}) {
if (presentationDefinition.submissionRequirements != null) {
final inputDescriptors = List.of(presentationDefinition.inputDescriptors);

final newInputDescriptor = <InputDescriptor>[];

/// grouping
while (inputDescriptors.isNotEmpty) {
final currentFirst = inputDescriptors.removeAt(0);
final group = currentFirst.group.toString();

final descriptorsWithSameGroup = inputDescriptors
.where((descriptor) => descriptor.group.toString() == group)
.toList();

if (descriptorsWithSameGroup.isNotEmpty) {
final mergedDescriptor = InputDescriptor(
id: '${currentFirst.id},${descriptorsWithSameGroup.map((e) => e.id).join(",")}', // ignore: lines_longer_than_80_chars
name:
'${currentFirst.name},${descriptorsWithSameGroup.map((e) => e.name).join(",")}', // ignore: lines_longer_than_80_chars
constraints: Constraints([
...?currentFirst.constraints?.fields,
for (final descriptor in descriptorsWithSameGroup)
...?descriptor.constraints?.fields,
]),
group: currentFirst.group,
purpose:
'${currentFirst.purpose},${descriptorsWithSameGroup.map((e) => e.purpose).join(",")}', // ignore: lines_longer_than_80_chars
);
newInputDescriptor.add(mergedDescriptor);
inputDescriptors.removeWhere(
(descriptor) => descriptor.group.toString() == group);
} else {
newInputDescriptor.add(currentFirst);
}
}

presentationDefinition.inputDescriptors = newInputDescriptor;
}

/// Get instruction to filter credentials of the wallet
final filteredCredentialList = getCredentialsFromPresentationDefinition(
presentationDefinition: presentationDefinition,
Expand All @@ -38,26 +80,84 @@ class CredentialManifestPickCubit extends Cubit<CredentialManifestPickState> {
isJwtVpInJwtVCRequired: isJwtVpInJwtVCRequired,
);

emit(state.copyWith(filteredCredentialList: filteredCredentialList));
emit(
state.copyWith(
filteredCredentialList: filteredCredentialList,
presentationDefinition: presentationDefinition,
),
);
}

void toggle(int index) {
if (state.selected.contains(index)) {
emit(
state.copyWith(
selected: List<int>.from(state.selected)
..removeWhere((element) => element == index),
),
);
void toggle({
required int index,
required InputDescriptor inputDescriptor,
}) {
final bool isSelected = state.selected.contains(index);

late List<int> selected;

if (isSelected) {
/// deSelecting the credential
selected = List<int>.from(state.selected)
..removeWhere((element) => element == index);
} else {
/// selecting the credential
selected = [
...state.selected,
...[index]
];
}

bool isButtonEnabled = selected.isNotEmpty;

if (state.presentationDefinition!.submissionRequirements != null) {
final requirement = state.presentationDefinition!.submissionRequirements!
.where((element) => element.from == inputDescriptor.group?[0])
.firstOrNull;

if (requirement != null) {
final count = requirement.count;
final atLeast = requirement.min;
if (count != null) {
if (!isSelected) {
/// selecting the credential
if (state.selected.length >= count) {
/// show message that limit is (count)
emit(
state.copyWith(
message: StateMessage.info(
messageHandler: ResponseMessage(
ResponseString
.RESPONSE_STRING_youcanSelectOnlyXCredential,
),
injectedMessage: count.toString(),
),
),
);
return;
}
}

isButtonEnabled = selected.length == count;
} else if (atLeast != null) {
isButtonEnabled = selected.length < atLeast;
} else {
throw Exception();
}
} else {
throw Exception();
}
} else {
emit(
state.copyWith(
selected: [
...state.selected,
...[index],
],
),
);
/// normal case
isButtonEnabled = selected.isNotEmpty;
}

emit(
state.copyWith(
selected: selected,
isButtonEnabled: isButtonEnabled,
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,37 @@ part of 'credential_manifest_pick_cubit.dart';
@JsonSerializable()
class CredentialManifestPickState extends Equatable {
const CredentialManifestPickState({
this.message,
this.selected = const [],
required this.filteredCredentialList,
this.presentationDefinition,
this.isButtonEnabled = false,
});

factory CredentialManifestPickState.fromJson(Map<String, dynamic> json) =>
_$CredentialManifestPickStateFromJson(json);

final StateMessage? message;
final List<int> selected;
final List<CredentialModel> filteredCredentialList;
final PresentationDefinition? presentationDefinition;
final bool isButtonEnabled;

CredentialManifestPickState copyWith({
List<int>? selected,
List<CredentialModel>? filteredCredentialList,
PresentationDefinition? presentationDefinition,
bool? isButtonEnabled,
StateMessage? message,
}) {
return CredentialManifestPickState(
selected: selected ?? this.selected,
filteredCredentialList:
filteredCredentialList ?? this.filteredCredentialList,
presentationDefinition:
presentationDefinition ?? this.presentationDefinition,
isButtonEnabled: isButtonEnabled ?? this.isButtonEnabled,
message: message,
);
}

Expand All @@ -30,5 +43,8 @@ class CredentialManifestPickState extends Equatable {
List<Object?> get props => [
selected,
filteredCredentialList,
presentationDefinition,
isButtonEnabled,
message,
];
}
Loading

0 comments on commit 465e80d

Please sign in to comment.