diff --git a/src/__tests__/converters.test.ts b/src/__tests__/converters.test.ts index 212f871..8515d17 100644 --- a/src/__tests__/converters.test.ts +++ b/src/__tests__/converters.test.ts @@ -425,6 +425,38 @@ describe('credential', () => { }) }) + describe('other W3C fields', () => { + it('uses evidence from vc', () => { + const result = normalizeCredential({ vc: { evidence: 'foo'} }) + expect(result).toMatchObject({ evidence: 'foo' }) + }) + + it('uses evidence from vc, keeping originals', () => { + const result = normalizeCredential({ vc: { evidence: 'foo' } }, false) + expect(result).toMatchObject({ evidence: 'foo', vc: { evidence: 'foo'} }) + }) + + it('uses credentialStatus from vc', () => { + const result = normalizeCredential({ vc: { credentialStatus: 'foo'} }) + expect(result).toMatchObject({ credentialStatus: 'foo' }) + }) + + it('uses credentialStatus from vc, keeping originals', () => { + const result = normalizeCredential({ vc: { credentialStatus: 'foo' } }, false) + expect(result).toMatchObject({ credentialStatus: 'foo', vc: { credentialStatus: 'foo'} }) + }) + + it('uses termsOfUse from vc', () => { + const result = normalizeCredential({ vc: { termsOfUse: 'foo'} }) + expect(result).toMatchObject({ termsOfUse: 'foo' }) + }) + + it('uses termsOfUse from vc, keeping originals', () => { + const result = normalizeCredential({ vc: { termsOfUse: 'foo' } }, false) + expect(result).toMatchObject({ termsOfUse: 'foo', vc: { termsOfUse: 'foo'} }) + }) + }) + describe('JWT payload', () => { it('rejects unknown JSON string payload', () => { expect(() => { @@ -924,6 +956,41 @@ describe('credential', () => { expect(input.issuer.id).toEqual('did:example:567') }) }) + + describe('other fields W3C fields', () => { + it('maps evidence to vc', () => { + const result = transformCredentialInput({ evidence: 'foo' }) + expect(result).toMatchObject({ vc: { evidence: 'foo' } }) + expect(result).not.toHaveProperty('evidence') + }) + + it('maps evidence to vc, keeping originals', () => { + const result = transformCredentialInput({ evidence: 'foo' }, false) + expect(result).toMatchObject({ evidence: 'foo', vc: { evidence: 'foo' } }) + }) + + it('maps credentialStatus to vc', () => { + const result = transformCredentialInput({ credentialStatus: 'foo' }) + expect(result).toMatchObject({ vc: { credentialStatus: 'foo' } }) + expect(result).not.toHaveProperty('credentialStatus') + }) + + it('maps credentialStatus to vc, keeping originals', () => { + const result = transformCredentialInput({ credentialStatus: 'foo' }, false) + expect(result).toMatchObject({ credentialStatus: 'foo', vc: { credentialStatus: 'foo' } }) + }) + + it('maps termsOfUse to vc', () => { + const result = transformCredentialInput({ termsOfUse: 'foo' }) + expect(result).toMatchObject({ vc: { termsOfUse: 'foo' } }) + expect(result).not.toHaveProperty('termsOfUse') + }) + + it('maps termsOfUse to vc, keeping originals', () => { + const result = transformCredentialInput({ termsOfUse: 'foo' }, false) + expect(result).toMatchObject({ termsOfUse: 'foo', vc: { termsOfUse: 'foo' } }) + }) + }) }) }) diff --git a/src/converters.ts b/src/converters.ts index ae7d41b..08e1d6a 100644 --- a/src/converters.ts +++ b/src/converters.ts @@ -122,6 +122,21 @@ function normalizeJwtCredentialPayload( delete result.vc?.type } + result.evidence = input.vc?.evidence + if (removeOriginalFields) { + delete result.vc?.evidence + } + + result.credentialStatus = input.vc?.credentialStatus + if (removeOriginalFields) { + delete result.vc?.credentialStatus + } + + result.termsOfUse = input.vc?.termsOfUse + if (removeOriginalFields) { + delete result.vc?.termsOfUse + } + const contextArray: string[] = [ ...asArray(input.context), ...asArray(input['@context']), @@ -236,9 +251,10 @@ export function transformCredentialInput( delete credentialSubject.id } } - result.vc.credentialSubject = credentialSubject + + result.vc.credentialStatus = input.credentialStatus if (removeOriginalFields) { - delete result.credentialSubject + delete result.credentialStatus } const contextEntries = [ @@ -304,6 +320,22 @@ export function transformCredentialInput( } } + // additional W3C VC fields to map: + result.vc.credentialSubject = credentialSubject + if (removeOriginalFields) { + delete result.credentialSubject + } + + result.vc.evidence = input.evidence + if (removeOriginalFields) { + delete result.evidence + } + + result.vc.termsOfUse = input.termsOfUse + if (removeOriginalFields) { + delete result.termsOfUse + } + return result as JwtCredentialPayload } diff --git a/src/types.ts b/src/types.ts index cb0b200..406b379 100644 --- a/src/types.ts +++ b/src/types.ts @@ -20,6 +20,9 @@ export interface JwtCredentialPayload { '@context': string[] | string type: string[] | string credentialSubject: JwtCredentialSubject + credentialStatus?: CredentialStatus + evidence?: any + termsOfUse?: any [x: string]: any } nbf?: number @@ -66,6 +69,8 @@ interface FixedCredentialPayload { [x: string]: any } credentialStatus?: CredentialStatus + evidence?: any + termsOfUse?: any } /**