@sphereon/ssi-sdk.data-store
Version:
139 lines • 6.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.digitalCredentialsFrom = exports.digitalCredentialFrom = exports.nonPersistedDigitalCredentialEntityFromAddArgs = void 0;
exports.isHex = isHex;
exports.parseRawDocument = parseRawDocument;
exports.ensureRawDocument = ensureRawDocument;
const ssi_types_1 = require("@sphereon/ssi-types");
const utils_1 = require("@veramo/utils");
const types_1 = require("../../types");
const FormattingUtils_1 = require("../FormattingUtils");
const ssi_sdk_core_1 = require("@sphereon/ssi-sdk.core");
function determineDocumentType(raw) {
const rawDocument = parseRawDocument(raw);
if (!rawDocument) {
throw new Error(`Couldn't parse the credential: ${raw}`);
}
const hasProof = ssi_types_1.CredentialMapper.hasProof(rawDocument);
const isCredential = isHex(raw) || ssi_types_1.ObjectUtils.isBase64(raw) || ssi_types_1.CredentialMapper.isCredential(rawDocument);
const isPresentation = ssi_types_1.CredentialMapper.isPresentation(rawDocument);
if (isCredential) {
return hasProof || isHex(raw) || ssi_types_1.ObjectUtils.isBase64(raw) ? types_1.DocumentType.VC : types_1.DocumentType.C;
}
else if (isPresentation) {
return hasProof ? types_1.DocumentType.VP : types_1.DocumentType.P;
}
throw new Error(`Couldn't determine the type of the credential: ${raw}`);
}
function isHex(input) {
return input.match(/^([0-9A-Fa-f])+$/g) !== null;
}
function parseRawDocument(raw) {
if (isHex(raw) || ssi_types_1.ObjectUtils.isBase64(raw)) {
// mso_mdoc
return raw;
}
else if (ssi_types_1.CredentialMapper.isJwtEncoded(raw) || ssi_types_1.CredentialMapper.isSdJwtEncoded(raw)) {
return raw;
}
try {
return JSON.parse(raw);
}
catch (e) {
throw new Error(`Can't parse the raw credential: ${raw}`);
}
}
function ensureRawDocument(input) {
if (typeof input === 'string') {
if (isHex(input) || ssi_types_1.ObjectUtils.isBase64(input)) {
// mso_mdoc
return input;
}
else if (ssi_types_1.CredentialMapper.isJwtEncoded(input) || ssi_types_1.CredentialMapper.isSdJwtEncoded(input)) {
return input;
}
throw Error('Unknown input to be mapped as rawDocument');
}
try {
return JSON.stringify(input);
}
catch (e) {
throw new Error(`Can't stringify to a raw credential: ${input}`);
}
}
function determineCredentialDocumentFormat(documentFormat) {
switch (documentFormat) {
case 1 /* DocumentFormat.JSONLD */:
return types_1.CredentialDocumentFormat.JSON_LD;
case 0 /* DocumentFormat.JWT */:
return types_1.CredentialDocumentFormat.JWT;
case 2 /* DocumentFormat.SD_JWT_VC */:
return types_1.CredentialDocumentFormat.SD_JWT;
case 4 /* DocumentFormat.MSO_MDOC */:
return types_1.CredentialDocumentFormat.MSO_MDOC;
default:
throw new Error(`Not supported document format: ${documentFormat}`);
}
}
function getValidUntil(uniformDocument) {
if ('expirationDate' in uniformDocument && uniformDocument.expirationDate) {
return new Date(uniformDocument.expirationDate);
}
else if ('validUntil' in uniformDocument && uniformDocument.validUntil) {
return new Date(uniformDocument.validUntil);
}
else if ('exp' in uniformDocument && uniformDocument.exp) {
return new Date(uniformDocument.exp * 1000);
}
return undefined;
}
function getValidFrom(uniformDocument) {
if ('issuanceDate' in uniformDocument && uniformDocument.issuanceDate) {
return new Date(uniformDocument.issuanceDate);
}
else if ('validFrom' in uniformDocument && uniformDocument.validFrom) {
return new Date(uniformDocument['validFrom']);
}
else if ('nbf' in uniformDocument && uniformDocument.nbf) {
return new Date(uniformDocument['nbf'] * 1000);
}
else if ('iat' in uniformDocument && uniformDocument.iat) {
return new Date(uniformDocument['iat'] * 1000);
}
return undefined;
}
const safeStringify = (object) => {
if (typeof object === 'string') {
return object;
}
return JSON.stringify(object);
};
const nonPersistedDigitalCredentialEntityFromAddArgs = (addCredentialArgs) => {
var _a, _b, _c, _d, _e;
const documentType = determineDocumentType(addCredentialArgs.rawDocument);
const documentFormat = ssi_types_1.CredentialMapper.detectDocumentType(addCredentialArgs.rawDocument);
const hasher = (_b = (_a = addCredentialArgs === null || addCredentialArgs === void 0 ? void 0 : addCredentialArgs.opts) === null || _a === void 0 ? void 0 : _a.hasher) !== null && _b !== void 0 ? _b : ssi_sdk_core_1.defaultHasher;
if (documentFormat === 2 /* DocumentFormat.SD_JWT_VC */ && !((_c = addCredentialArgs.opts) === null || _c === void 0 ? void 0 : _c.hasher)) {
throw new Error('No hasher function is provided for SD_JWT credential.');
}
const uniformDocument = documentType === types_1.DocumentType.VC || documentType === types_1.DocumentType.C
? ssi_types_1.CredentialMapper.toUniformCredential(addCredentialArgs.rawDocument, { hasher })
: ssi_types_1.CredentialMapper.toUniformPresentation(addCredentialArgs.rawDocument, { hasher });
const validFrom = getValidFrom(uniformDocument);
const validUntil = getValidUntil(uniformDocument);
const hash = (0, utils_1.computeEntryHash)(addCredentialArgs.rawDocument);
const regulationType = (_d = addCredentialArgs.regulationType) !== null && _d !== void 0 ? _d : types_1.RegulationType.NON_REGULATED;
return Object.assign(Object.assign(Object.assign(Object.assign({}, addCredentialArgs), { regulationType,
documentType, documentFormat: determineCredentialDocumentFormat(documentFormat), createdAt: new Date(), credentialId: (_e = uniformDocument.id) !== null && _e !== void 0 ? _e : hash, hash, uniformDocument: safeStringify(uniformDocument), validFrom }), (validUntil && { validUntil })), { lastUpdatedAt: new Date() });
};
exports.nonPersistedDigitalCredentialEntityFromAddArgs = nonPersistedDigitalCredentialEntityFromAddArgs;
const digitalCredentialFrom = (credentialEntity) => {
const result = Object.assign({}, credentialEntity);
return (0, FormattingUtils_1.replaceNullWithUndefined)(result);
};
exports.digitalCredentialFrom = digitalCredentialFrom;
const digitalCredentialsFrom = (credentialEntities) => {
return credentialEntities.map((credentialEntity) => (0, exports.digitalCredentialFrom)(credentialEntity));
};
exports.digitalCredentialsFrom = digitalCredentialsFrom;
//# sourceMappingURL=MappingUtils.js.map