-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1881 from blockchain-certificates/feat/vc-v2
Add date format validation to comply with VC v2 spec
- Loading branch information
Showing
33 changed files
with
675 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { BlockcertsV3 } from '../../../models/BlockcertsV3'; | ||
|
||
export interface DatesToValidate { | ||
property: string; | ||
dateTimeStamp: string; | ||
} | ||
|
||
export default function getDatesToValidate (credential: BlockcertsV3): DatesToValidate[] { | ||
const dates: DatesToValidate[] = []; | ||
if (credential.validFrom) { | ||
dates.push({ | ||
property: 'validFrom', | ||
dateTimeStamp: credential.validFrom | ||
}); | ||
} | ||
|
||
if (credential.validUntil) { | ||
dates.push({ | ||
property: 'validUntil', | ||
dateTimeStamp: credential.validUntil | ||
}); | ||
} | ||
|
||
const proof = !Array.isArray(credential.proof) ? [credential.proof] : credential.proof; | ||
for (const proofItem of proof) { | ||
if (proofItem.created) { | ||
dates.push({ | ||
property: `proof ${proofItem.cryptosuite ?? proofItem.type} created`, | ||
dateTimeStamp: proofItem.created | ||
}); | ||
} | ||
} | ||
|
||
return dates; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { validateDateTimeStamp } from '../helpers/date'; | ||
import { VerifierError } from '../models'; | ||
import { SUB_STEPS } from '../domain/verifier/entities/verificationSteps'; | ||
import { getText } from '../domain/i18n/useCases'; | ||
import type { DatesToValidate } from '../domain/verifier/useCases/getDatesToValidate'; | ||
|
||
export default function validateDateFormat (dates: DatesToValidate[]): void { | ||
for (const { dateTimeStamp, property } of dates) { | ||
if (!validateDateTimeStamp(dateTimeStamp)) { | ||
console.error('Date', dateTimeStamp, 'is not valid:', property); | ||
throw new VerifierError(SUB_STEPS.validateDateFormat, `${getText('errors', 'validateDateFormat')} ${property}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { CONTEXT_URLS } from '@blockcerts/schemas'; | ||
import { isString } from '../../helpers/string'; | ||
import type { JsonLDContext } from '../../models/Blockcerts'; | ||
|
||
export interface VCVersion { | ||
versionNumber: number; | ||
} | ||
|
||
export function isVCV2 (context: JsonLDContext | string): boolean { | ||
return retrieveVCVersion(context).versionNumber === 2; | ||
} | ||
|
||
export default function retrieveVCVersion (context: JsonLDContext | string): VCVersion { | ||
if (typeof context === 'string') { | ||
context = [context]; | ||
} | ||
|
||
const VCContextsUrls = [CONTEXT_URLS.VERIFIABLE_CREDENTIAL_V1_CONTEXT, CONTEXT_URLS.VERIFIABLE_CREDENTIAL_V2_CONTEXT]; | ||
|
||
const VCContext: string = context.filter(isString).find((ctx: string) => VCContextsUrls.includes(ctx)); | ||
|
||
let versionNumber: number = -1; | ||
|
||
if (VCContext?.includes('v1')) { | ||
versionNumber = 1; | ||
} | ||
|
||
if (VCContext?.includes('v2')) { | ||
versionNumber = 2; | ||
} | ||
|
||
return { | ||
versionNumber | ||
}; | ||
} |
Oops, something went wrong.