Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new error handling #110

Merged
merged 20 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
node_modules/
.npmrc
dist/
#dist/
.idea
*.iml
15 changes: 15 additions & 0 deletions dist/cjs/common/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLogger = void 0;
var debug_1 = __importDefault(require("debug"));
var logger = debug_1.default("oa-verify");
exports.getLogger = function (namespace) { return ({
trace: logger.extend("trace:" + namespace),
debug: logger.extend("debug:" + namespace),
info: logger.extend("info:" + namespace),
warn: logger.extend("warn:" + namespace),
error: logger.extend("error:" + namespace),
}); };
69 changes: 69 additions & 0 deletions dist/cjs/common/smartContract/documentStoreErrors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getErrorReason = exports.badResponse = exports.contractRevoked = exports.contractNotIssued = void 0;
var ethers_1 = require("ethers");
var __1 = require("../..");
var contractNotFound = function (address) {
return {
code: __1.OpenAttestationEthereumDocumentStoreStatusCode.CONTRACT_NOT_FOUND,
codeString: __1.OpenAttestationEthereumDocumentStoreStatusCode[__1.OpenAttestationEthereumDocumentStoreStatusCode.CONTRACT_NOT_FOUND],
message: "Contract " + address + " was not found",
};
};
var contractAddressInvalid = function (address) {
return {
code: __1.OpenAttestationEthereumDocumentStoreStatusCode.CONTRACT_ADDRESS_INVALID,
codeString: __1.OpenAttestationEthereumDocumentStoreStatusCode[__1.OpenAttestationEthereumDocumentStoreStatusCode.CONTRACT_ADDRESS_INVALID],
message: "Contract address " + address + " is invalid",
};
};
exports.contractNotIssued = function (merkleRoot, address) {
return {
code: __1.OpenAttestationEthereumDocumentStoreStatusCode.DOCUMENT_NOT_ISSUED,
codeString: __1.OpenAttestationEthereumDocumentStoreStatusCode[__1.OpenAttestationEthereumDocumentStoreStatusCode.DOCUMENT_NOT_ISSUED],
message: "Document " + merkleRoot + " has not been issued under contract " + address,
};
};
exports.contractRevoked = function (merkleRoot, address) {
return {
code: __1.OpenAttestationEthereumDocumentStoreStatusCode.DOCUMENT_REVOKED,
codeString: __1.OpenAttestationEthereumDocumentStoreStatusCode[__1.OpenAttestationEthereumDocumentStoreStatusCode.DOCUMENT_REVOKED],
message: "Document " + merkleRoot + " has been revoked under contract " + address,
};
};
/**
* This function handles all non-200 HTTP response codes (e.g. Infura/Cloudflare rate limits, Cloudflare's random 502)
* @param address the document store address
* TODO: Add the same for tokenStore
*/
exports.badResponse = function () {
return {
code: __1.OpenAttestationEthereumDocumentStoreStatusCode.BAD_RESPONSE,
codeString: __1.OpenAttestationEthereumDocumentStoreStatusCode[__1.OpenAttestationEthereumDocumentStoreStatusCode.BAD_RESPONSE],
message: "Unable to connect to Ethereum, please try again later",
};
};
exports.getErrorReason = function (error, address) {
var _a, _b, _c, _d;
var reason = error.reason && Array.isArray(error.reason) ? error.reason[0] : (_a = error.reason) !== null && _a !== void 0 ? _a : "";
if (!error.reason &&
(((_b = error.method) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === "isRevoked(bytes32)".toLowerCase() ||
((_c = error.method) === null || _c === void 0 ? void 0 : _c.toLowerCase()) === "isIssued(bytes32)".toLowerCase()) &&
error.code === ethers_1.errors.CALL_EXCEPTION) {
return contractNotFound(address);
}
else if ((reason.toLowerCase() === "ENS name not configured".toLowerCase() && error.code === ethers_1.errors.UNSUPPORTED_OPERATION) ||
(reason.toLowerCase() === "bad address checksum".toLowerCase() && error.code === ethers_1.errors.INVALID_ARGUMENT) ||
((_d = error.message) === null || _d === void 0 ? void 0 : _d.toLowerCase()) === "name not found".toLowerCase() ||
(reason.toLowerCase() === "invalid address".toLowerCase() && error.code === ethers_1.errors.INVALID_ARGUMENT)) {
return contractAddressInvalid(address);
}
else if (reason.toLowerCase() === "bad response".toLowerCase()) {
return exports.badResponse();
}
return {
message: "Error with smart contract " + address + ": " + error.reason,
code: __1.OpenAttestationEthereumDocumentStoreStatusCode.ETHERS_UNHANDLED_ERROR,
codeString: __1.OpenAttestationEthereumDocumentStoreStatusCode[__1.OpenAttestationEthereumDocumentStoreStatusCode.ETHERS_UNHANDLED_ERROR],
};
};
44 changes: 44 additions & 0 deletions dist/cjs/common/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getIssuersTokenRegistry = exports.getIssuersDocumentStore = exports.getProvider = void 0;
var ethers = __importStar(require("ethers"));
var open_attestation_1 = require("@govtechsg/open-attestation");
var config_1 = require("../config");
exports.getProvider = function (options) {
return process.env.ETHEREUM_PROVIDER === "cloudflare"
? new ethers.providers.CloudflareProvider()
: new ethers.providers.InfuraProvider(options.network, process.env.INFURA_API_KEY || config_1.INFURA_API_KEY);
};
exports.getIssuersDocumentStore = function (document) {
if (open_attestation_1.utils.isWrappedV2Document(document)) {
var data = open_attestation_1.getData(document);
return data.issuers.map(function (issuer) { return issuer.documentStore || issuer.certificateStore || ""; });
}
return [open_attestation_1.getData(document).proof.value];
};
exports.getIssuersTokenRegistry = function (document) {
if (open_attestation_1.utils.isWrappedV2Document(document)) {
var data = open_attestation_1.getData(document);
return data.issuers.map(function (issuer) { return issuer.tokenRegistry || ""; });
}
return [open_attestation_1.getData(document).proof.value];
};
4 changes: 4 additions & 0 deletions dist/cjs/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.INFURA_API_KEY = void 0;
exports.INFURA_API_KEY = "bb46da3f80e040e8ab73c0a9ff365d18";
39 changes: 39 additions & 0 deletions dist/cjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.openAttestationEthereumTokenRegistryStatus = exports.openAttestationEthereumDocumentStoreStatus = exports.openAttestationDnsTxt = exports.openAttestationSignedProof = exports.openAttestationHash = exports.verify = exports.isValid = exports.openAttestationVerifiers = exports.verificationBuilder = void 0;
var verificationBuilder_1 = require("./verifiers/verificationBuilder");
Object.defineProperty(exports, "verificationBuilder", { enumerable: true, get: function () { return verificationBuilder_1.verificationBuilder; } });
var openAttestationHash_1 = require("./verifiers/hash/openAttestationHash");
Object.defineProperty(exports, "openAttestationHash", { enumerable: true, get: function () { return openAttestationHash_1.openAttestationHash; } });
var openAttestationDnsTxt_1 = require("./verifiers/dnsText/openAttestationDnsTxt");
Object.defineProperty(exports, "openAttestationDnsTxt", { enumerable: true, get: function () { return openAttestationDnsTxt_1.openAttestationDnsTxt; } });
var openAttestationSignedProof_1 = require("./verifiers/signedProof/openAttestationSignedProof");
Object.defineProperty(exports, "openAttestationSignedProof", { enumerable: true, get: function () { return openAttestationSignedProof_1.openAttestationSignedProof; } });
var validator_1 = require("./validator");
Object.defineProperty(exports, "isValid", { enumerable: true, get: function () { return validator_1.isValid; } });
var openAttestationEthereumTokenRegistryStatus_1 = require("./verifiers/tokenRegistryStatus/openAttestationEthereumTokenRegistryStatus");
Object.defineProperty(exports, "openAttestationEthereumTokenRegistryStatus", { enumerable: true, get: function () { return openAttestationEthereumTokenRegistryStatus_1.openAttestationEthereumTokenRegistryStatus; } });
var openAttestationEthereumDocumentStoreStatus_1 = require("./verifiers/documentStoreStatus/openAttestationEthereumDocumentStoreStatus");
Object.defineProperty(exports, "openAttestationEthereumDocumentStoreStatus", { enumerable: true, get: function () { return openAttestationEthereumDocumentStoreStatus_1.openAttestationEthereumDocumentStoreStatus; } });
var openAttestationVerifiers = [
openAttestationHash_1.openAttestationHash,
openAttestationSignedProof_1.openAttestationSignedProof,
openAttestationEthereumTokenRegistryStatus_1.openAttestationEthereumTokenRegistryStatus,
openAttestationEthereumDocumentStoreStatus_1.openAttestationEthereumDocumentStoreStatus,
openAttestationDnsTxt_1.openAttestationDnsTxt,
];
exports.openAttestationVerifiers = openAttestationVerifiers;
var verify = verificationBuilder_1.verificationBuilder(openAttestationVerifiers);
exports.verify = verify;
__exportStar(require("./types/core"), exports);
__exportStar(require("./types/error"), exports);
2 changes: 2 additions & 0 deletions dist/cjs/types/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
43 changes: 43 additions & 0 deletions dist/cjs/types/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenAttestationHashCode = exports.OpenAttestationDnsTxtCode = exports.OpenAttestationEthereumTokenRegistryStatusCode = exports.OpenAttestationDocumentSignedCode = exports.OpenAttestationEthereumDocumentStoreStatusCode = void 0;
// NEVER EVER REPLACE OR CHANGE A VALUE :)
// code for errors and invalid fragment
var OpenAttestationEthereumDocumentStoreStatusCode;
(function (OpenAttestationEthereumDocumentStoreStatusCode) {
OpenAttestationEthereumDocumentStoreStatusCode[OpenAttestationEthereumDocumentStoreStatusCode["UNEXPECTED_ERROR"] = 0] = "UNEXPECTED_ERROR";
OpenAttestationEthereumDocumentStoreStatusCode[OpenAttestationEthereumDocumentStoreStatusCode["DOCUMENT_NOT_ISSUED"] = 1] = "DOCUMENT_NOT_ISSUED";
OpenAttestationEthereumDocumentStoreStatusCode[OpenAttestationEthereumDocumentStoreStatusCode["CONTRACT_ADDRESS_INVALID"] = 2] = "CONTRACT_ADDRESS_INVALID";
OpenAttestationEthereumDocumentStoreStatusCode[OpenAttestationEthereumDocumentStoreStatusCode["ETHERS_UNHANDLED_ERROR"] = 3] = "ETHERS_UNHANDLED_ERROR";
OpenAttestationEthereumDocumentStoreStatusCode[OpenAttestationEthereumDocumentStoreStatusCode["SKIPPED"] = 4] = "SKIPPED";
OpenAttestationEthereumDocumentStoreStatusCode[OpenAttestationEthereumDocumentStoreStatusCode["DOCUMENT_REVOKED"] = 5] = "DOCUMENT_REVOKED";
OpenAttestationEthereumDocumentStoreStatusCode[OpenAttestationEthereumDocumentStoreStatusCode["CONTRACT_NOT_FOUND"] = 404] = "CONTRACT_NOT_FOUND";
OpenAttestationEthereumDocumentStoreStatusCode[OpenAttestationEthereumDocumentStoreStatusCode["BAD_RESPONSE"] = 500] = "BAD_RESPONSE";
})(OpenAttestationEthereumDocumentStoreStatusCode = exports.OpenAttestationEthereumDocumentStoreStatusCode || (exports.OpenAttestationEthereumDocumentStoreStatusCode = {}));
var OpenAttestationDocumentSignedCode;
(function (OpenAttestationDocumentSignedCode) {
OpenAttestationDocumentSignedCode[OpenAttestationDocumentSignedCode["UNEXPECTED_ERROR"] = 0] = "UNEXPECTED_ERROR";
OpenAttestationDocumentSignedCode[OpenAttestationDocumentSignedCode["DOCUMENT_PROOF_INVALID"] = 1] = "DOCUMENT_PROOF_INVALID";
OpenAttestationDocumentSignedCode[OpenAttestationDocumentSignedCode["DOCUMENT_PROOF_ERROR"] = 2] = "DOCUMENT_PROOF_ERROR";
OpenAttestationDocumentSignedCode[OpenAttestationDocumentSignedCode["SKIPPED"] = 4] = "SKIPPED";
})(OpenAttestationDocumentSignedCode = exports.OpenAttestationDocumentSignedCode || (exports.OpenAttestationDocumentSignedCode = {}));
var OpenAttestationEthereumTokenRegistryStatusCode;
(function (OpenAttestationEthereumTokenRegistryStatusCode) {
OpenAttestationEthereumTokenRegistryStatusCode[OpenAttestationEthereumTokenRegistryStatusCode["UNEXPECTED_ERROR"] = 0] = "UNEXPECTED_ERROR";
OpenAttestationEthereumTokenRegistryStatusCode[OpenAttestationEthereumTokenRegistryStatusCode["DOCUMENT_NOT_MINTED"] = 1] = "DOCUMENT_NOT_MINTED";
OpenAttestationEthereumTokenRegistryStatusCode[OpenAttestationEthereumTokenRegistryStatusCode["CONTRACT_ADDRESS_INVALID"] = 2] = "CONTRACT_ADDRESS_INVALID";
OpenAttestationEthereumTokenRegistryStatusCode[OpenAttestationEthereumTokenRegistryStatusCode["ETHERS_UNHANDLED_ERROR"] = 3] = "ETHERS_UNHANDLED_ERROR";
OpenAttestationEthereumTokenRegistryStatusCode[OpenAttestationEthereumTokenRegistryStatusCode["SKIPPED"] = 4] = "SKIPPED";
OpenAttestationEthereumTokenRegistryStatusCode[OpenAttestationEthereumTokenRegistryStatusCode["CONTRACT_NOT_FOUND"] = 404] = "CONTRACT_NOT_FOUND";
OpenAttestationEthereumTokenRegistryStatusCode[OpenAttestationEthereumTokenRegistryStatusCode["BAD_RESPONSE"] = 500] = "BAD_RESPONSE";
})(OpenAttestationEthereumTokenRegistryStatusCode = exports.OpenAttestationEthereumTokenRegistryStatusCode || (exports.OpenAttestationEthereumTokenRegistryStatusCode = {}));
var OpenAttestationDnsTxtCode;
(function (OpenAttestationDnsTxtCode) {
OpenAttestationDnsTxtCode[OpenAttestationDnsTxtCode["UNEXPECTED_ERROR"] = 0] = "UNEXPECTED_ERROR";
OpenAttestationDnsTxtCode[OpenAttestationDnsTxtCode["INVALID_IDENTITY"] = 1] = "INVALID_IDENTITY";
OpenAttestationDnsTxtCode[OpenAttestationDnsTxtCode["SKIPPED"] = 2] = "SKIPPED";
})(OpenAttestationDnsTxtCode = exports.OpenAttestationDnsTxtCode || (exports.OpenAttestationDnsTxtCode = {}));
var OpenAttestationHashCode;
(function (OpenAttestationHashCode) {
OpenAttestationHashCode[OpenAttestationHashCode["DOCUMENT_TAMPERED"] = 0] = "DOCUMENT_TAMPERED";
})(OpenAttestationHashCode = exports.OpenAttestationHashCode || (exports.OpenAttestationHashCode = {}));
19 changes: 19 additions & 0 deletions dist/cjs/validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isValid = void 0;
exports.isValid = function (verificationFragments, types) {
if (types === void 0) { types = ["DOCUMENT_STATUS", "DOCUMENT_INTEGRITY", "ISSUER_IDENTITY"]; }
if (verificationFragments.length < 1) {
throw new Error("Please provide at least one verification fragment to check");
}
if (types.length < 1) {
throw new Error("Please provide at least one type to check");
}
return types.every(function (type) {
var verificationFragmentsForType = verificationFragments.filter(function (fragment) { return fragment.type === type; });
// return true if at least one fragment is valid
// and all fragments are valid or skipped
return (verificationFragmentsForType.some(function (fragment) { return fragment.status === "VALID"; }) &&
verificationFragmentsForType.every(function (fragment) { return fragment.status === "VALID" || fragment.status === "SKIPPED"; }));
});
};
Loading