UNPKG

vc-notarization

Version:

Notarize verifiable credentials on DLT for provability and order

51 lines (50 loc) 2.67 kB
import { DLT } from './enums.js'; import { notarizeVC, getNotarizedVCTimestamp, notarizeHash, getNotarizedTimestamp } from './notarization/index.js'; /** * Takes a hash string or verifiabel credential object and notarizes it on the desired DLT specified by the DLT enum * * @param object - Hash string or verifiabel credential object which shall be notarized * @param dlt - The DLT enum of the DLT where the credential * @returns The DLT transaction id */ export async function notarize(object, dlt = DLT.IOTA) { if (typeof object == 'string') return await notarizeHash(object, dlt); return await notarizeVC(object, dlt); } ; /** * Takes a hash string or verifiable credential object and tries to look up the timestamp of the notarization on the DLT specified by the DLT enum * * @param object - Verifiable credential or hash string for which the timestamp shall be retrieved * @param dlt - The DLT enum of the DLT where the credential shall be notarized * @param proof - Optional proof of inlclushion if the transaction can not be retrieved from the DLT * @returns The timestamp of the latest possible event creation */ export async function getTimestamp(object, dlt = DLT.IOTA, proof) { if (typeof object == 'string') return await getNotarizedTimestamp(object, dlt, proof); return await getNotarizedVCTimestamp(object, dlt, proof); } ; /** * Takes a verifiable credential and verifies its issuance date using the notarized timestamp on the DLT specified by the DLT enum. * Throws an error if the tolerance is exceeded or the notarization happened before the issuance * * @param credential - Verifiable credential for which the timestamp shall be verified * @param dlt - The DLT enum of the DLT where the hash is notarized * @param proof - Optional proof of inlclusion if the transaction can not be retrieved from the DLT * @param tolerance - Optional time tolerance between issuance and notarization in milliseconds - Default is 300 seconds * @returns The timestamp of the latest possible event creation */ export async function verifyTimestamp(credential, dlt = DLT.IOTA, proof, tolerance = 300000) { const notarizedTimestamp = await getTimestamp(credential, dlt, proof); const deviation = notarizedTimestamp.getTime() - new Date(credential.issuanceDate).getTime(); if (deviation < 0) throw new Error(`Notarization happended ${(-deviation) / 1000} seconds before issuance!`); if (deviation > tolerance) throw new Error(`Tolerance exceeded! Notarization happended ${(deviation) / 1000} seconds after issuance!`); return notarizedTimestamp; } ; export { DLT } from './enums.js';