Skip to content

Commit

Permalink
update usages on proximity presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmanos committed Dec 4, 2024
1 parent 0f5ce28 commit 3a446f7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
39 changes: 30 additions & 9 deletions src/context/ContainerContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import StatusContext from "./StatusContext";
import { getSdJwtVcMetadata } from "../lib/utils/getSdJwtVcMetadata";
import { CredentialBatchHelper } from "../lib/services/CredentialBatchHelper";
import { MDoc } from "@auth0/mdl";
import {deviceResponseParser, mdocPIDParser } from "../lib/utils/mdocPIDParser";
import {PidParser } from "../lib/utils/PidParser";
import { deviceResponseParser, mdocPIDParser } from "../lib/utils/mdocPIDParser";
import { PidParser } from "../lib/utils/PidParser";

export type ContainerContextValue = {
httpProxy: IHttpProxy,
Expand Down Expand Up @@ -91,10 +91,37 @@ export const ContainerContextProvider = ({ children }) => {
const userData = userResponse.data;

cont.register<IHttpProxy>('HttpProxy', HttpProxy);

cont.register<CredentialBatchHelper>('CredentialBatchHelper', CredentialBatchHelper,
async function updateCredential(storableCredential: StorableCredential) {
await api.post('/storage/vc/update', {
credential: storableCredential
});
}
)

cont.register<IMdocAppCommunication>('MdocAppCommunication', MdocAppCommunication,
cont.resolve<CredentialBatchHelper>('CredentialBatchHelper'),

async function getAllStoredVerifiableCredentials() {
const fetchAllCredentials = await api.get('/storage/vc');
return { verifiableCredentials: fetchAllCredentials.data.vc_list };
},

async function generateDeviceResponse(mdocCredential: MDoc, presentationDefinition: any, sessionTranscriptBytes: any) {
return keystore.generateDeviceResponseWithProximity(mdocCredential, presentationDefinition, sessionTranscriptBytes);
},

async function storeVerifiablePresentation(presentation: string, format: string, identifiersOfIncludedCredentials: string[], presentationSubmission: any, audience: string) {
await api.post('/storage/vp', {
presentationIdentifier: generateRandomIdentifier(32),
presentation,
presentationSubmission,
includedVerifiableCredentialIdentifiers: identifiersOfIncludedCredentials,
audience,
issuanceDate: new Date().toISOString(),
});
},
);
cont.register<IOpenID4VPRelyingPartyStateRepository>('OpenID4VPRelyingPartyStateRepository', OpenID4VPRelyingPartyStateRepository);

Expand All @@ -103,13 +130,7 @@ export const ContainerContextProvider = ({ children }) => {
cont.register<IOpenID4VCIClientStateRepository>('OpenID4VCIClientStateRepository', OpenID4VCIClientStateRepository, userData.settings.openidRefreshTokenMaxAgeInSeconds);
cont.register<IOpenID4VCIHelper>('OpenID4VCIHelper', OpenID4VCIHelper, cont.resolve<IHttpProxy>('HttpProxy'));

cont.register<CredentialBatchHelper>('CredentialBatchHelper', CredentialBatchHelper,
async function updateCredential(storableCredential: StorableCredential) {
await api.post('/storage/vc/update', {
credential: storableCredential
});
}
)

const credentialParserRegistry = cont.resolve<ICredentialParserRegistry>('CredentialParserRegistry');

credentialParserRegistry.addParser(PidParser);
Expand Down
14 changes: 14 additions & 0 deletions src/lib/services/MdocAppCommunication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import { cborDecode, cborEncode } from "../utils/cbor";
import { v4 as uuidv4 } from 'uuid';
import { encryptMessage, decryptMessage, hexToUint8Array, uint8ArrayToBase64Url, deriveSharedSecret, getKey, uint8ArraytoHexString, getSessionTranscriptBytes, getDeviceEngagement } from "../utils/mdocProtocol";
import { base64url } from "jose";
import { CredentialBatchHelper } from "./CredentialBatchHelper";
import { StorableCredential } from "../types/StorableCredential";


export class MdocAppCommunication implements IMdocAppCommunication {
constructor(
private credentialBatchHelper: CredentialBatchHelper,
private getAllStoredVerifiableCredentials: () => Promise<{ verifiableCredentials: StorableCredential[] }>,
private generateDeviceResponseFn: (mdocCredential: MDoc, presentationDefinition: any, sessionTranscripBytes: any) => Promise<{ deviceResponseMDoc: MDoc }>,
private storeVerifiablePresentation: (presentation: string, format: string, identifiersOfIncludedCredentials: string[], presentationSubmission: any, audience: string) => Promise<void>,
) { }

ephemeralKey: CryptoKeyPair;
Expand Down Expand Up @@ -183,6 +188,15 @@ export class MdocAppCommunication implements IMdocAppCommunication {

const { deviceResponseMDoc } = await this.generateDeviceResponseFn(mdoc, fullPEX, sessionTranscriptBytes);

const storePresentation = async () => {
const encodedDeviceResponse = base64url.encode(deviceResponseMDoc.encode());
const creds = await this.getAllStoredVerifiableCredentials();
const storableCredential = creds.verifiableCredentials.filter((cred) => cred.credential == this.credential)[0];
this.storeVerifiablePresentation(encodedDeviceResponse, "", [storableCredential.credentialIdentifier], {}, "proximity");
this.credentialBatchHelper.useCredential(storableCredential);
}
storePresentation().catch(() => console.log("Failed to store"));

// encrypt mdoc response
const iv = new Uint8Array([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // identifier
Expand Down

0 comments on commit 3a446f7

Please sign in to comment.