UNPKG

@govtechsg/open-attestation

Version:
135 lines (134 loc) 5.37 kB
"use strict"; 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)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.diagnose = void 0; var logger_1 = require("@ethersproject/logger"); var document_1 = require("../@types/document"); var validate_1 = require("../validate"); var types_1 = require("../../3.0/types"); var types_2 = require("../../2.0/types"); var ajv_1 = require("../ajv"); //https://github.com/ethers-io/ethers.js/blob/ec1b9583039a14a0e0fa15d0a2a6082a2f41cf5b/packages/ethers/src.ts/ethers.ts#L36 var ethersLogger = new logger_1.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 */ 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 = (0, validate_1.validateSchema)(document, (0, ajv_1.getSchema)(version === "3.0" ? document_1.SchemaId.v3 : document_1.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 }); } }; exports.diagnose = diagnose; 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" ? types_2.SignatureStrict.check(document.signature) : types_2.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 { types_2.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 !== document_1.SchemaId.v3) { return handleError(debug, "The document schema version is wrong. Expected ".concat(document_1.SchemaId.v3, ", received ").concat(document.version)); } try { // eslint-disable-next-line @typescript-eslint/no-unused-expressions mode === "strict" ? types_1.VerifiableCredentialWrappedProofStrict.check(document.proof) : types_1.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 { types_1.VerifiableCredentialSignedProof.check(document.proof); } catch (e) { if (e instanceof Error) { return handleError(debug, e.message); } else { console.error(e); } } } return []; };