Skip to content

Commit

Permalink
feat: Add DEFAULT credential
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed Aug 2, 2023
1 parent 5785731 commit 2fbd4ca
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 36 deletions.
59 changes: 39 additions & 20 deletions lib/ebsi/add_ebsi_credential.dart
Original file line number Diff line number Diff line change
@@ -1,41 +1,59 @@
import 'dart:convert';

import 'package:altme/app/app.dart';
import 'package:altme/credentials/credentials.dart';
import 'package:altme/dashboard/dashboard.dart';
import 'package:altme/dashboard/home/tab_bar/credentials/models/activity/activity.dart';
import 'package:credential_manifest/credential_manifest.dart';
import 'package:dio/dio.dart';
import 'package:jose/jose.dart';

Future<void> addEbsiCredential(
dynamic encodedCredentialFromEbsi,
Future<void> addOIDC4VCCredential(
dynamic encodedCredentialFromOIDC4VC,
Uri uri,
CredentialsCubit credentialsCubit,
OIDC4VCType oidc4vcType,
String issuer,
String credentialTypeOrId,
) async {
//jwt_vc
final jws = JsonWebSignature.fromCompactSerialization(
encodedCredentialFromEbsi['credential'] as String,
);
late Map<String, dynamic> credentialFromOIDC4VC;
if (oidc4vcType.issuerVcType == 'jwt_vc') {
//jwt_vc_json
final jws = JsonWebSignature.fromCompactSerialization(
encodedCredentialFromOIDC4VC['credential'] as String,
);

credentialFromOIDC4VC =
jws.unverifiedPayload.jsonContent['vc'] as Map<String, dynamic>;
} else if (oidc4vcType.issuerVcType == 'ldp_vc') {
//ldp_vc

credentialFromOIDC4VC =
jsonDecode(encodedCredentialFromOIDC4VC['credential'].toString())
as Map<String, dynamic>;
} else {
throw Exception();
}

final credentialFromEbsi =
jws.unverifiedPayload.jsonContent['vc'] as Map<String, dynamic>;
final Map<String, dynamic> newCredential =
Map<String, dynamic>.from(credentialFromEbsi);
newCredential['jwt'] = encodedCredentialFromEbsi['credential'];
newCredential['credentialPreview'] = credentialFromEbsi;
Map<String, dynamic>.from(credentialFromOIDC4VC);

if (oidc4vcType.issuerVcType == 'jwt_vc') {
//jwt_vc_json
newCredential['jwt'] = encodedCredentialFromOIDC4VC['credential'];
}

newCredential['credentialPreview'] = credentialFromOIDC4VC;

/// added id as type to recognise the card
newCredential['credentialPreview']['credentialSubject']['type'] =
credentialFromEbsi['credentialSchema']['id'];

final String credentialSchema = uri.queryParameters['credential_type'] ?? '';
final issuerAndCode = uri.queryParameters['issuer'];
final issuerAndCodeUri = Uri.parse(issuerAndCode!);
final issuer =
'${issuerAndCodeUri.scheme}://${issuerAndCodeUri.authority}${issuerAndCodeUri.path}';
credentialFromOIDC4VC['credentialSchema']['id'];

final CredentialManifest credentialManifest = await getCredentialManifest(
Dio(),
issuer,
credentialSchema,
credentialTypeOrId,
oidc4vcType.schemaForType,
);

if (credentialManifest.outputDescriptors!.isNotEmpty) {
Expand All @@ -51,9 +69,10 @@ Future<void> addEbsiCredential(

final credentialModel = CredentialModel.copyWithData(
oldCredentialModel: newCredentialModel,
newData: credentialFromEbsi,
newData: credentialFromOIDC4VC,
activities: [Activity(acquisitionAt: DateTime.now())],
);

// insert the credential in the wallet
await credentialsCubit.insertCredential(credential: credentialModel);
}
20 changes: 11 additions & 9 deletions lib/ebsi/initiate_ebsi_credential_issuance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Future<void> initiateEbsiCredentialIssuance(

String? preAuthorizedCode;
late String issuer;
late String type;
late String credentialTypeOrId;
late String did;
late String kid;

Expand All @@ -43,17 +43,18 @@ Future<void> initiateEbsiCredentialIssuance(
['pre-authorized_code']
.toString();
issuer = credentialOfferJson['credential_issuer'].toString();
type = credentialOfferJson['credentials'][0].toString();
credentialTypeOrId = credentialOfferJson['credentials'][0].toString();

const didMethod = AltMeStrings.defaultDIDMethod;
did = didKitProvider.keyToDID(didMethod, privateKey);
kid = await didKitProvider.keyToVerificationMethod(didMethod, privateKey);

break;
case OIDC4VCType.EBSIV2:
preAuthorizedCode =
uriFromScannedResponse.queryParameters['pre-authorized_code'];
issuer = uriFromScannedResponse.queryParameters['issuer'].toString();
type =
credentialTypeOrId =
uriFromScannedResponse.queryParameters['credential_type'].toString();

final private = await oidc4vc.getPrivateKey(mnemonic, privateKey);
Expand All @@ -70,23 +71,24 @@ Future<void> initiateEbsiCredentialIssuance(
}

if (preAuthorizedCode != null) {
final dynamic encodedCredentialFromEbsi = await oidc4vc.getCredential(
final dynamic encodedCredentialFromOIDC4VC = await oidc4vc.getCredential(
preAuthorizedCode,
issuer,
type,
credentialTypeOrId,
did,
kid,
uriFromScannedResponse,
null,
privateKey,
);

print(encodedCredentialFromEbsi);

await addEbsiCredential(
encodedCredentialFromEbsi,
await addOIDC4VCCredential(
encodedCredentialFromOIDC4VC,
uriFromScannedResponse,
credentialsCubit,
oidc4vcType,
issuer,
credentialTypeOrId,
);
} else {
final Uri ebsiAuthenticationUri =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import 'package:json_path/json_path.dart';
Future<CredentialManifest> getCredentialManifest(
Dio client,
String baseUrl,
String type,
String credentialTypeOrId,
bool schemaForType,
) async {
final dynamic wellKnown = await client.get<String>(
'$baseUrl/.well-known/openid-configuration',
Expand All @@ -27,10 +28,12 @@ Future<CredentialManifest> getCredentialManifest(
credentialManifestMap,
);

final String key = schemaForType ? 'schema' : 'id';

/// select wanted output desciptor
final JsonPath outputDescriptorPath = JsonPath(
// ignore: prefer_interpolation_to_compose_strings
r'$..output_descriptors[?(@.schema=="' + type + '")]',
r'$..output_descriptors[?(@.' + key + '=="' + credentialTypeOrId + '")]',
);

/// There are some possible issues with this way of filtering :-/
Expand Down
12 changes: 7 additions & 5 deletions packages/oidc4vc/lib/src/oidc4vc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class OIDC4VC {
Future<dynamic> getCredential(
String? preAuthorizedCode,
String issuer,
String type,
String credentialTypeOrId,
String did,
String kid,
Uri credentialRequestUri,
Expand Down Expand Up @@ -224,9 +224,11 @@ class OIDC4VC {
issuerTokenParameters,
credentialRequestUri,
openidConfigurationResponse,
type,
credentialTypeOrId,
);

/// sign proof
final credentialEndpoint =
readCredentialEndpoint(openidConfigurationResponse);

Expand Down Expand Up @@ -357,7 +359,7 @@ class OIDC4VC {
IssuerTokenParameters issuerTokenParameters,
Uri credentialRequestUri,
Response<Map<String, dynamic>> openidConfigurationResponse,
String type,
String credentialTypeOrId,
) async {
final nonce = response['c_nonce'] as String;

Expand All @@ -376,8 +378,8 @@ class OIDC4VC {
// }

final credentialData = <String, dynamic>{
'type': type,
'format': 'jwt_vc_json',
'type': credentialTypeOrId,
'format': oidc4vcModel.issuerVcType,
'proof': {
'proof_type': 'jwt',
'jwt': vcJwt,
Expand Down

0 comments on commit 2fbd4ca

Please sign in to comment.