From 427c3e9e324fac2c3659d0090c95676854c4c342 Mon Sep 17 00:00:00 2001 From: Nam Hoang Date: Tue, 10 Dec 2024 16:48:12 +0700 Subject: [PATCH] refactor: extension configuration data --- .../__tests__/lib/credentialService.test.ts | 12 ++--- .../src/lib/schemaValidation.ts | 44 ++++++++++--------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/packages/untp-playground/__tests__/lib/credentialService.test.ts b/packages/untp-playground/__tests__/lib/credentialService.test.ts index e08aa720..2cc19830 100644 --- a/packages/untp-playground/__tests__/lib/credentialService.test.ts +++ b/packages/untp-playground/__tests__/lib/credentialService.test.ts @@ -35,18 +35,18 @@ describe('credentialService', () => { const envelopedCredential = { type: 'EnvelopedVerifiableCredential', - id: 'did:example:123,eyJhbGciOiJFUzI1NksifQ', + id: 'data:application/vc-ld+jwt,eyJhbGciOiJFZERTQSIsIm', }; const result = decodeEnvelopedCredential(envelopedCredential); expect(result).toEqual(mockDecodedCredential); - expect(jwtDecode).toHaveBeenCalledWith('eyJhbGciOiJFUzI1NksifQ'); + expect(jwtDecode).toHaveBeenCalledWith('eyJhbGciOiJFZERTQSIsIm'); }); it('should handle missing JWT part', () => { const envelopedCredential = { type: 'EnvelopedVerifiableCredential', - id: 'did:example:123', + id: 'data:application/vc+jwt', }; const result = decodeEnvelopedCredential(envelopedCredential); @@ -60,7 +60,7 @@ describe('credentialService', () => { const envelopedCredential = { type: 'EnvelopedVerifiableCredential', - id: 'did:example:123,invalid-jwt', + id: 'data:application/vc+jwt,invalid-jwt', }; const result = decodeEnvelopedCredential(envelopedCredential); @@ -139,7 +139,7 @@ describe('credentialService', () => { it('should detect enveloped proof', () => { const credential = { type: 'EnvelopedVerifiableCredential', - id: 'did:example:123,jwt', + id: 'data:application/vc+jwt,eyJhbGciOiJFZERTQSIsIm', }; expect(isEnvelopedProof(credential)).toBe(true); @@ -149,7 +149,7 @@ describe('credentialService', () => { const credential = { verifiableCredential: { type: 'EnvelopedVerifiableCredential', - id: 'did:example:123,jwt', + id: 'data:application/vc+jwt,eyJhbGciOiJFZERTQSIsIm', }, }; diff --git a/packages/untp-playground/src/lib/schemaValidation.ts b/packages/untp-playground/src/lib/schemaValidation.ts index 750ff4cd..bebfd61d 100644 --- a/packages/untp-playground/src/lib/schemaValidation.ts +++ b/packages/untp-playground/src/lib/schemaValidation.ts @@ -11,23 +11,29 @@ addFormats(ajv); const schemaCache = new Map(); -const SCHEMA_URLS = { - DigitalProductPassport: 'https://test.uncefact.org/vocabulary/untp/dpp/untp-dpp-schema-0.5.0.json', - DigitalConformityCredential: 'https://test.uncefact.org/vocabulary/untp/dcc/untp-dcc-schema-0.5.0.json', - DigitalTraceabilityEvent: 'https://test.uncefact.org/vocabulary/untp/dte/untp-dte-schema-0.5.0.json', - DigitalFacilityRecord: 'https://test.uncefact.org/vocabulary/untp/dfr/untp-dfr-schema-0.5.0.json', - DigitalIdentityAnchor: 'https://test.uncefact.org/vocabulary/untp/dia/untp-dia-schema-0.2.1.json', -}; +interface CoreVersion { + type: string; + version: string; +} + +interface ExtensionVersion { + version: string; + schema: string; + core: CoreVersion; +} + +interface ExtensionConfig { + domain: string; + versions: ExtensionVersion[]; +} -const EXTENSION_VERSIONS: Record< - string, - { domain: string; versions: { version: string; core: { type: string; version: string } }[] } -> = { +const EXTENSION_VERSIONS: Record = { DigitalLivestockPassport: { domain: 'aatp.foodagility.com', versions: [ { version: '0.4.0', + schema: 'https://aatp.foodagility.com/assets/files/aatp-dlp-schema-0.4.0-9c0ad2b1ca6a9e497dedcfd8b87f35f1.json', core: { type: 'DigitalProductPassport', version: '0.5.0' }, }, ], @@ -45,14 +51,8 @@ const schemaURLConstructor = (type: string, version: string) => { return `https://test.uncefact.org/vocabulary/untp/${shortCredentialTypes[type]}/untp-${shortCredentialTypes[type]}-schema-${version}.json`; }; -const extensionSchemaURLConstructor = (type: string, version: string) => { - const shortCredentialTypes: Record = { - DigitalLivestockPassport: 'dlp', - }; - if (type === 'DigitalLivestockPassport' && version === '0.4.0') { - return 'https://aatp.foodagility.com/assets/files/aatp-dlp-schema-0.4.0-9c0ad2b1ca6a9e497dedcfd8b87f35f1.json'; - } - return `https://aatp.foodagility.com/vocabulary/aatp/${shortCredentialTypes[type]}/aatp-${shortCredentialTypes[type]}-schema-${version}.json`; +const findExtensionSchemaURL = (type: string, version: string) => { + return EXTENSION_VERSIONS[type].versions.find((v) => v.version === version)?.schema; }; export async function validateCredentialSchema(credential: any): Promise<{ @@ -97,7 +97,11 @@ export async function validateExtension(credential: any): Promise<{ throw new Error('Unknown extension'); } - const schemaUrl = extensionSchemaURLConstructor(extension.extension.type, extension.extension.version); + const schemaUrl = findExtensionSchemaURL(extension.extension.type, extension.extension.version); + + if (!schemaUrl) { + throw new Error('Unsupported extension version'); + } return validateCredentialOnSchemaUrl(credential, schemaUrl); }