Skip to content

Commit

Permalink
Added display type capability.
Browse files Browse the repository at this point in the history
Added basic display type.
  • Loading branch information
5eeman committed Jan 12, 2024
1 parent 33ddc83 commit e65c92a
Show file tree
Hide file tree
Showing 20 changed files with 1,370 additions and 1,038 deletions.
46 changes: 24 additions & 22 deletions lib/credential/data/data_sources/storage_claim_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class ClaimStoreRefWrapper {
return _store.record(key).get(database);
}

Future<Map<String, Object?>> put(
DatabaseClient database, String key, Map<String, Object?> value,
Future<Map<String, Object?>> put(DatabaseClient database, String key,
Map<String, Object?> value,
{bool? merge}) {
return _store.record(key).put(database, value, merge: merge);
}
Expand All @@ -48,44 +48,41 @@ class StorageClaimDataSource extends SecureIdentityStorageDataSource {

/// Store all claims in a single transaction
/// If one storing fails, they will all be reverted
Future<void> storeClaims(
{required List<ClaimDTO> claims,
required String did,
required String privateKey}) {
Future<void> storeClaims({required List<ClaimDTO> claims,
required String did,
required String privateKey}) {
// TODO check if identifiers inside each claim are from privateKey
return getDatabase(did: did, privateKey: privateKey).then((database) =>
database
.transaction((transaction) =>
storeClaimsTransact(transaction: transaction, claims: claims))
storeClaimsTransact(transaction: transaction, claims: claims))
.whenComplete(() => database.close()));
}

// For UT purpose
Future<void> storeClaimsTransact(
{required DatabaseClient transaction,
required List<ClaimDTO> claims}) async {
Future<void> storeClaimsTransact({required DatabaseClient transaction,
required List<ClaimDTO> claims}) async {
for (ClaimDTO claim in claims) {
await _storeRefWrapper.put(transaction, claim.id, claim.toJson());
}
}

/// Remove all claims in a single transaction
/// If one removing fails, they will all be reverted
Future<void> removeClaims(
{required List<String> claimIds,
required String did,
required String privateKey}) {
Future<void> removeClaims({required List<String> claimIds,
required String did,
required String privateKey}) {
return getDatabase(did: did, privateKey: privateKey).then((database) =>
database
.transaction((transaction) => removeClaimsTransact(
.transaction((transaction) =>
removeClaimsTransact(
transaction: transaction, claimIds: claimIds))
.whenComplete(() => database.close()));
}

// For UT purpose
Future<void> removeClaimsTransact(
{required DatabaseClient transaction,
required List<String> claimIds}) async {
Future<void> removeClaimsTransact({required DatabaseClient transaction,
required List<String> claimIds}) async {
for (String claimId in claimIds) {
// TODO check if identifiers inside each claim are from privateKey
await _storeRefWrapper.remove(transaction, claimId);
Expand All @@ -99,7 +96,7 @@ class StorageClaimDataSource extends SecureIdentityStorageDataSource {
return getDatabase(did: did, privateKey: privateKey).then((database) =>
database
.transaction((transaction) =>
removeAllClaimsTransact(transaction: transaction))
removeAllClaimsTransact(transaction: transaction))
.whenComplete(() => database.close()));
}

Expand All @@ -117,9 +114,14 @@ class StorageClaimDataSource extends SecureIdentityStorageDataSource {
return getDatabase(did: did, privateKey: privateKey).then((database) =>
_storeRefWrapper
.find(database, finder: Finder(filter: filter))
.then((snapshots) => snapshots
.map((snapshot) => ClaimDTO.fromJson(snapshot.value))
.toList())
.then((snapshots) {
return snapshots
.map((snapshot) {
return ClaimDTO.fromJson(snapshot.value);
})
.toList();
}
)
.whenComplete(() => database.close()));
}

Expand Down
22 changes: 13 additions & 9 deletions lib/credential/data/dtos/claim_dto.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:polygonid_flutter_sdk/credential/data/dtos/display_type/display_type.dart';

import 'claim_info_dto.dart';

Expand All @@ -16,16 +17,19 @@ class ClaimDTO extends Equatable {
final String? expiration;
final String type;
Map<String, dynamic>? schema;
Map<String, dynamic>? displayType;

ClaimDTO(
{required this.id,
required this.issuer,
required this.did,
required this.type,
this.state = '',
this.expiration,
required this.info,
this.schema});
ClaimDTO({
required this.id,
required this.issuer,
required this.did,
required this.type,
this.state = '',
this.expiration,
required this.info,
this.schema,
this.displayType,
});

factory ClaimDTO.fromJson(Map<String, dynamic> json) =>
_$ClaimDTOFromJson(json);
Expand Down
2 changes: 2 additions & 0 deletions lib/credential/data/dtos/claim_dto.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions lib/credential/data/dtos/claim_info_dto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ClaimInfoDTO extends Equatable {
@JsonKey(name: 'proof')
final List<ClaimProofDTO> proofs;
final RefreshServiceDTO? refreshService;
final DisplayMethodDTO? displayMethod;

const ClaimInfoDTO(
this.id,
Expand All @@ -35,6 +36,7 @@ class ClaimInfoDTO extends Equatable {
this.credentialSchema,
this.proofs,
this.refreshService,
this.displayMethod,
);

factory ClaimInfoDTO.fromJson(Map<String, dynamic> json) =>
Expand Down Expand Up @@ -172,3 +174,20 @@ class CredentialSchemaDTO extends Equatable {
@override
List<Object?> get props => [id, type];
}

@JsonSerializable()
class DisplayMethodDTO {
/// Contains url.
final String id;
final String type;

DisplayMethodDTO(this.id, this.type);

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

Map<String, dynamic> toJson() => _$DisplayMethodDTOToJson(this);

@override
List<Object?> get props => [id, type];
}
17 changes: 17 additions & 0 deletions lib/credential/data/dtos/claim_info_dto.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions lib/credential/data/dtos/display_type/basic_display_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:polygonid_flutter_sdk/credential/data/dtos/display_type/display_type.dart';

part 'basic_display_type.g.dart';

@JsonSerializable()
class Iden3BasicDisplayType extends DisplayType {
static const name = "Iden3BasicDisplayMethodV1";

final String? title;
final String? description;
final String? issuerName;
final String? titleTextColor;
final String? descriptionTextColor;
final String? issuerTextColor;
final String? backgroundImageUrl;
final String? backgroundColor;
@JsonKey(toJson: _logoToJson, fromJson: _logoFromJson)
final Iden3BasicDisplayTypeLogo? logo;

Iden3BasicDisplayType(
this.title,
this.description,
this.issuerName,
this.titleTextColor,
this.descriptionTextColor,
this.issuerTextColor,
this.backgroundImageUrl,
this.backgroundColor,
this.logo,
);

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

@override
Map<String, dynamic> toJson() => _$Iden3BasicDisplayTypeToJson(this);

@override
String get typeName => name;

@override
List<Object?> get props => [
title,
description,
issuerName,
titleTextColor,
descriptionTextColor,
issuerTextColor,
backgroundImageUrl,
backgroundColor,
logo,
];
}

@JsonSerializable()
class Iden3BasicDisplayTypeLogo {
final String? uri;
final String? alt;

Iden3BasicDisplayTypeLogo(this.uri, this.alt);

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

Map<String, dynamic> toJson() => _$Iden3BasicDisplayTypeLogoToJson(this);

@override
List<Object?> get props => [uri, alt];
}

Map<String, dynamic>? _logoToJson(Iden3BasicDisplayTypeLogo? logo) {
return logo?.toJson();
}

Iden3BasicDisplayTypeLogo? _logoFromJson(Map<String, dynamic>? json) {
return json == null ? null : Iden3BasicDisplayTypeLogo.fromJson(json);
}
49 changes: 49 additions & 0 deletions lib/credential/data/dtos/display_type/basic_display_type.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions lib/credential/data/dtos/display_type/display_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:polygonid_flutter_sdk/credential/data/dtos/display_type/basic_display_type.dart';
import 'package:polygonid_flutter_sdk/credential/data/dtos/display_type/unknown_display_type.dart';

abstract class DisplayType {
const DisplayType();

String get typeName;

factory DisplayType.fromJson(Map<String, dynamic> json) {
switch (json['type']) {
case Iden3BasicDisplayType.name:
return Iden3BasicDisplayType.fromJson(json);
default:
return UnknownDisplayType.fromJson(json);
}
}

Map<String, dynamic> toJson();
}
17 changes: 17 additions & 0 deletions lib/credential/data/dtos/display_type/unknown_display_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:polygonid_flutter_sdk/credential/data/dtos/display_type/display_type.dart';

class UnknownDisplayType extends DisplayType {
final Map<String, dynamic> displayType;

@override
String get typeName => "UnknownDisplayType";

UnknownDisplayType(this.displayType);

factory UnknownDisplayType.fromJson(Map<String, dynamic> json) {
return UnknownDisplayType(json);
}

@override
Map<String, dynamic> toJson() => displayType;
}
Loading

0 comments on commit e65c92a

Please sign in to comment.