From 1dc1ad99f3f22066cc51c75a9ace6deeb6444616 Mon Sep 17 00:00:00 2001 From: sanderPostma Date: Thu, 31 Oct 2024 01:13:14 +0100 Subject: [PATCH] chore: sdJwtDecodedCredentialToUniformCredential fix --- packages/ssi-types/src/types/sd-jwt-vc.ts | 43 ++++++++++++----------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/packages/ssi-types/src/types/sd-jwt-vc.ts b/packages/ssi-types/src/types/sd-jwt-vc.ts index bcee095dc..f4cdc0dcb 100644 --- a/packages/ssi-types/src/types/sd-jwt-vc.ts +++ b/packages/ssi-types/src/types/sd-jwt-vc.ts @@ -282,28 +282,13 @@ export async function decodeSdJwtVcAsync(compactSdJwtVc: CompactSdJwtVc, hasher: } } -// TODO naive implementation of mapping a sd-jwt onto a IVerifiableCredential. Needs some fixes and further implementation and needs to be moved out of ssi-types export const sdJwtDecodedCredentialToUniformCredential = ( decoded: SdJwtDecodedVerifiableCredential, - opts?: { maxTimeSkewInMS?: number }, + opts?: { maxTimeSkewInMS?: number } ): IVerifiableCredential => { - const { decodedPayload } = decoded // fixme: other params and proof + const { decodedPayload } = decoded const { exp, nbf, iss, iat, vct, cnf, status, sub, jti } = decodedPayload - type DisclosuresAccumulator = { - [key: string]: any - } - - const credentialSubject = decoded.disclosures.reduce( - (acc: DisclosuresAccumulator, item: { decoded: Array; digest: string; encoded: string }) => { - const key = item.decoded[1] - acc[key] = item.decoded[2] - - return acc - }, - {}, - ) - const maxSkewInMS = opts?.maxTimeSkewInMS ?? 1500 const expirationDate = jwtDateToISOString({ jwtClaim: exp, claimName: 'exp' }) @@ -325,13 +310,29 @@ export const sdJwtDecodedCredentialToUniformCredential = ( throw Error(`JWT issuance date is required but was not present`) } + // Filter out the fields we don't want in credentialSubject + const excludedFields = new Set(['vct', 'cnf', 'iss', 'iat', 'exp', 'nbf', 'jti', 'sub']) + const credentialSubject = Object.entries(decodedPayload).reduce((acc, [key, value]) => { + if ( + !excludedFields.has(key) && + value !== undefined && + value !== '' && + !(typeof value === 'object' && value !== null && Object.keys(value).length === 0) + ) { + acc[key] = value + } + return acc + }, {} as Record) + + // Add id to credentialSubject if applicable + if (sub || jti) { + credentialSubject.id = sub ?? jti + } + const credential: Omit = { type: [vct], // SDJwt is not a W3C VC, so no VerifiableCredential '@context': [], // SDJwt has no JSON-LD by default. Certainly not the VC DM1 default context for JSON-LD - credentialSubject: { - ...credentialSubject, - id: credentialSubject.id ?? sub ?? jti, - }, + credentialSubject, issuanceDate, expirationDate, issuer: iss,