Skip to content

Commit

Permalink
chore: sdJwtDecodedCredentialToUniformCredential fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderPostma committed Oct 31, 2024
1 parent df3f6ea commit 1dc1ad9
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions packages/ssi-types/src/types/sd-jwt-vc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>; 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' })
Expand All @@ -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<string, any>)

// Add id to credentialSubject if applicable
if (sub || jti) {
credentialSubject.id = sub ?? jti
}

const credential: Omit<IVerifiableCredential, 'issuer' | 'issuanceDate'> = {
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,
Expand Down

0 comments on commit 1dc1ad9

Please sign in to comment.