UNPKG

@govtechsg/open-attestation

Version:
131 lines (130 loc) 5.26 kB
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; import { Logger } from "@ethersproject/logger"; import { SchemaId } from "../@types/document"; import { validateSchema as validate } from "../validate"; import { VerifiableCredentialSignedProof, VerifiableCredentialWrappedProof, VerifiableCredentialWrappedProofStrict, } from "../../3.0/types"; import { ArrayProof, Signature, SignatureStrict } from "../../2.0/types"; import { getSchema } from "../ajv"; //https://github.com/ethers-io/ethers.js/blob/ec1b9583039a14a0e0fa15d0a2a6082a2f41cf5b/packages/ethers/src.ts/ethers.ts#L36 var ethersLogger = new Logger("ethers/5.7.0"); var handleError = function (debug) { var messages = []; for (var _i = 1; _i < arguments.length; _i++) { messages[_i - 1] = arguments[_i]; } if (debug) { for (var _a = 0, messages_1 = messages; _a < messages_1.length; _a++) { var message = messages_1[_a]; ethersLogger.info(message); } } return messages.map(function (message) { return ({ message: message }); }); }; /** * Tools to give information about the validity of a document. It will return and eventually output the errors found. * @param version 2.0 or 3.0 * @param kind raw, wrapped or signed * @param debug turn on to output in the console, the errors found * @param mode strict or non-strict. Strict will perform additional check on the data. For instance strict validation will ensure that a target hash is a 32 bytes hex string while non strict validation will just check that target hash is a string. * @param document the document to validate */ export var diagnose = function (_a) { var version = _a.version, kind = _a.kind, document = _a.document, _b = _a.debug, debug = _b === void 0 ? false : _b, mode = _a.mode; if (!document) { return handleError(debug, "The document must not be empty"); } if (typeof document !== "object") { return handleError(debug, "The document must be an object"); } var errors = validate(document, getSchema(version === "3.0" ? SchemaId.v3 : SchemaId.v2, mode), kind); if (errors.length > 0) { // TODO this can be improved later return handleError.apply(void 0, __spreadArray([debug, "The document does not match OpenAttestation schema ".concat(version === "3.0" ? "3.0" : "2.0")], errors.map(function (error) { return "".concat(error.instancePath || "document", " - ").concat(error.message); }), false)); } if (kind === "raw") { return []; } if (version === "3.0") { return diagnoseV3({ mode: mode, debug: debug, document: document, kind: kind }); } else { return diagnoseV2({ mode: mode, debug: debug, document: document, kind: kind }); } }; var diagnoseV2 = function (_a) { var kind = _a.kind, document = _a.document, debug = _a.debug, mode = _a.mode; try { // eslint-disable-next-line @typescript-eslint/no-unused-expressions mode === "strict" ? SignatureStrict.check(document.signature) : Signature.check(document.signature); } catch (e) { if (e instanceof Error) { return handleError(debug, e.message); } else { console.error(e); } } if (kind === "signed") { if (!document.proof || !(document.proof.length > 0)) { return handleError(debug, "The document does not have a proof"); } try { ArrayProof.check(document.proof); } catch (e) { if (e instanceof Error) { return handleError(debug, e.message); } else { console.error(e); } } } return []; }; var diagnoseV3 = function (_a) { var kind = _a.kind, document = _a.document, debug = _a.debug, mode = _a.mode; if (document.version !== SchemaId.v3) { return handleError(debug, "The document schema version is wrong. Expected ".concat(SchemaId.v3, ", received ").concat(document.version)); } try { // eslint-disable-next-line @typescript-eslint/no-unused-expressions mode === "strict" ? VerifiableCredentialWrappedProofStrict.check(document.proof) : VerifiableCredentialWrappedProof.check(document.proof); } catch (e) { if (e instanceof Error) { return handleError(debug, e.message); } else { console.error(e); } } if (kind === "signed") { if (!document.proof) { return handleError(debug, "The document does not have a proof"); } try { VerifiableCredentialSignedProof.check(document.proof); } catch (e) { if (e instanceof Error) { return handleError(debug, e.message); } else { console.error(e); } } } return []; };