sgf-gstin-validator
Version:
Module to validate GSTIN and verify Signed QR of GST eInvoice
78 lines (68 loc) • 12.6 kB
JavaScript
/*
* ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
* This devtool is neither made for production nor for readable output files.
* It uses "eval()" calls to create a separate source file in the browser devtools.
* If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
* or disable the default devtool with "devtool: false".
* If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
*/
/******/ (() => { // webpackBootstrap
/******/ var __webpack_modules__ = ({
/***/ "?44a0":
/*!******************************!*\
!*** jsonwebtoken (ignored) ***!
\******************************/
/***/ (() => {
eval("/* (ignored) */\n\n//# sourceURL=webpack://sgf-gstin-validator/jsonwebtoken_(ignored)?");
/***/ }),
/***/ "./dist/certs.js":
/*!***********************!*\
!*** ./dist/certs.js ***!
\***********************/
/***/ ((module) => {
"use strict";
eval("\nmodule.exports = {\n \"einv_prod\": \"LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUE3bEpkNjRHM3pkNFdnZmJpTDI2SwpUTGpZNU11WGpQc0dTc2ZPTm13WDlaMUhiSVRLZUZFYVhLSllDZS8yV0xOdnJjV2VmeWsyc1AzVGl0V21YRlRSCkpyQXZ6bG0vRjl0S2ZHbEhpRUZ6aDY1b3NvbEdFL0tkT0l2QnZMWEZ3SjI5dWwzelNtMTdYMS9jbHF4YTZNb3YKaXlzNzNoRHhIYlpaL21wWUxEYXRiNjZ6emdlL0l6c09qZno0enlVRTJWdXM2c2loVUpadzJhN0JwSVBXT1BvTQowVEdTa2Y5L2RxOU9QektCYWtZZlhadkwvaFU4V1lZOXdPSVM3OUdPcXlUY01vN2NBYnN0VHAvZFJabHNldG1FClArZjFQeWk4Q21tNlJlS1UwRzNtdVRUUGdXdE01cjlTei90S2Y0N3hzRkxBZTY5UTJXT21ibEZSW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WHk3SldMeFdsajBPdUl2SzFSb2o5QgpKemJNRjVFbjhGRC9LV2RyVlM1TzB5S3VuZ2ljNDRvL0FWc0RuRUF6dkgwRnJqUzJqRDBVeHZnQktFWEZMRmtjCnhRTnFrV0wzbEJUWnJteVpLTkxRUnRISmlFVG9ueFlTTWFUS1FBcnhpSFdnbHM0N1o3Z0hOZm9zeG9LQi9yMy8KSlFJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg==\"\n};\n//# sourceMappingURL=certs.js.map\n\n//# sourceURL=webpack://sgf-gstin-validator/./dist/certs.js?");
/***/ }),
/***/ "./dist/index.js":
/*!***********************!*\
!*** ./dist/index.js ***!
\***********************/
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
"use strict";
eval("\nvar jwt = __webpack_require__(/*! jsonwebtoken */ \"?44a0\");\nvar certs = __webpack_require__(/*! ./certs.js */ \"./dist/certs.js\");\nfunction calcCheckSum(gstin) {\n var GSTN_CODEPOINT_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';\n var factor = 2;\n var sum = 0;\n var checkCodePoint = 0;\n var mod = GSTN_CODEPOINT_CHARS.length;\n var i;\n for (i = gstin.length - 2; i >= 0; i--) {\n var codePoint = -1;\n for (var j = 0; j < GSTN_CODEPOINT_CHARS.length; j++) {\n if (GSTN_CODEPOINT_CHARS[j] === gstin[i]) {\n codePoint = j;\n }\n }\n var digit = factor * codePoint;\n factor = factor === 2 ? 1 : 2;\n digit = Math.floor(digit / mod) + (digit % mod);\n sum += digit;\n }\n checkCodePoint = (mod - (sum % mod)) % mod;\n return GSTN_CODEPOINT_CHARS[checkCodePoint];\n}\n// GSTIN Regex validation result\nfunction validatePattern(gstin) {\n // eslint-disable-next-line max-len\n var gstinRegexPattern = /^([0-2][0-9]|[3][0-8])[A-Z]{3}[ABCFGHLJPTK][A-Z]\\d{4}[A-Z][A-Z0-9][Z][A-Z0-9]$/;\n return gstinRegexPattern.test(gstin);\n}\nfunction isValidGSTNumber(gstin) {\n gstin = gstin.toUpperCase();\n if (gstin.length !== 15) {\n return false;\n }\n if (validatePattern(gstin)) {\n return gstin[14] === calcCheckSum(gstin.toUpperCase());\n }\n return false;\n}\nfunction getGSTINInfo(gstin) {\n var _a;\n var states = [\n {\n state_name: 'Andaman and Nicobar Islands',\n state_code: '35',\n state_shortcode: 'AN',\n },\n { state_name: 'Andhra Pradesh', state_code: '28', state_shortcode: 'AP' },\n {\n state_name: 'Andhra Pradesh (New)',\n state_code: '37',\n state_shortcode: 'AD',\n },\n {\n state_name: 'Arunachal Pradesh',\n state_code: '12',\n state_shortcode: 'AR',\n },\n { state_name: 'Assam', state_code: '18', state_shortcode: 'AS' },\n { state_name: 'Bihar', state_code: '10', state_shortcode: 'BH' },\n { state_name: 'Chandigarh', state_code: '04', state_shortcode: 'CH' },\n { state_name: 'Chattisgarh', state_code: '22', state_shortcode: 'CT' },\n {\n state_name: 'Dadra and Nagar Haveli',\n state_code: '26',\n state_shortcode: 'DN',\n },\n { state_name: 'Daman and Diu', state_code: '25', state_shortcode: 'DD' },\n { state_name: 'Delhi', state_code: '07', state_shortcode: 'DL' },\n { state_name: 'Goa', state_code: '30', state_shortcode: 'GA' },\n { state_name: 'Gujarat', state_code: '24', state_shortcode: 'GJ' },\n { state_name: 'Haryana', state_code: '06', state_shortcode: 'HR' },\n { state_name: 'Himachal Pradesh', state_code: '02', state_shortcode: 'HP' },\n {\n state_name: 'Jammu and Kashmir',\n state_code: '01',\n state_shortcode: 'JK',\n },\n { state_name: 'Jharkhand', state_code: '20', state_shortcode: 'JH' },\n { state_name: 'Karnataka', state_code: '29', state_shortcode: 'KA' },\n { state_name: 'Kerala', state_code: '32', state_shortcode: 'KL' },\n { state_name: 'Ladakh', state_code: '38', state_shortcode: 'LA' },\n {\n state_name: 'Lakshadweep Islands',\n state_code: '31',\n state_shortcode: 'LD',\n },\n { state_name: 'Madhya Pradesh', state_code: '23', state_shortcode: 'MP' },\n { state_name: 'Maharashtra', state_code: '27', state_shortcode: 'MH' },\n { state_name: 'Manipur', state_code: '14', state_shortcode: 'MN' },\n { state_name: 'Meghalaya', state_code: '17', state_shortcode: 'ME' },\n { state_name: 'Mizoram', state_code: '15', state_shortcode: 'MI' },\n { state_name: 'Nagaland', state_code: '13', state_shortcode: 'NL' },\n { state_name: 'Odisha', state_code: '21', state_shortcode: 'OR' },\n { state_name: 'Pondicherry', state_code: '34', state_shortcode: 'PY' },\n { state_name: 'Punjab', state_code: '03', state_shortcode: 'PB' },\n { state_name: 'Rajasthan', state_code: '08', state_shortcode: 'RJ' },\n { state_name: 'Sikkim', state_code: '11', state_shortcode: 'SK' },\n { state_name: 'Tamil Nadu', state_code: '33', state_shortcode: 'TN' },\n { state_name: 'Telangana', state_code: '36', state_shortcode: 'TS' },\n { state_name: 'Tripura', state_code: '16', state_shortcode: 'TR' },\n { state_name: 'Uttar Pradesh', state_code: '09', state_shortcode: 'UP' },\n { state_name: 'Uttarakhand', state_code: '05', state_shortcode: 'UT' },\n { state_name: 'West Bengal', state_code: '19', state_shortcode: 'WB' },\n ];\n var panTypes = [\n { code: 'C', pan_type: 'Company' },\n { code: 'P', pan_type: 'Person' },\n { code: 'H', pan_type: 'HUF (Hindu Undivided Family)' },\n { code: 'F', pan_type: 'Firm' },\n { code: 'A', pan_type: 'Association of Persons (AOP)' },\n { code: 'T', pan_type: 'AOP (Trust)' },\n { code: 'B', pan_type: 'Body of Individuals (BOI)' },\n { code: 'L', pan_type: 'Local Authority' },\n { code: 'J', pan_type: 'Artificial Juridical Person ' },\n { code: 'G', pan_type: 'Government' },\n ];\n gstin = gstin.toUpperCase();\n if (!isValidGSTNumber(gstin))\n return 'Invalid GSTIN';\n var state = states.find(function (o) {\n return o.state_code === gstin.slice(0, 2);\n });\n var info_msg = 'The GSTIN ' +\n gstin +\n ' is entity #' +\n parseInt(gstin.slice(12, 13), 36) +\n ' belonging to ' +\n ((_a = panTypes.find(function (o) {\n return o.code === gstin[5];\n })) === null || _a === void 0 ? void 0 : _a.pan_type) +\n ' whose PAN is ' +\n gstin.slice(2, 12) +\n ' registered in ' +\n (state === null || state === void 0 ? void 0 : state.state_name) +\n ' (' +\n (state === null || state === void 0 ? void 0 : state.state_shortcode) +\n ')';\n return info_msg;\n}\nfunction getCert(certname) {\n if (certname === undefined) {\n certname = 'einv_prod';\n }\n if (certname.endsWith('.pem')) {\n certname = certname.replace('.pem', '');\n }\n // get public key\n var buf = Buffer.from(certs[certname], 'base64');\n return buf.toString();\n}\n// This function is to validate a eInvoice QR\nfunction validateEInvoiceSignedQR(qrText, publickey) {\n var cert = getCert(publickey);\n try {\n var decodedQR = jwt.verify(qrText, cert, { issuer: 'NIC' });\n }\n catch (err) {\n throw new Error('Signature Verification Failed!');\n }\n return decodedQR;\n}\nfunction validateSignedInvoice(signedInvoiceJWT, publickey) {\n var cert = getCert(publickey);\n try {\n var invoice = jwt.verify(signedInvoiceJWT, cert, { issuer: 'NIC' });\n }\n catch (err) {\n throw new Error('Signature Verification Failed!');\n }\n return invoice;\n}\nfunction ValidateGSTIN(gstin) {\n gstin = gstin.toUpperCase();\n if (gstin.length !== 15) {\n return 'Enter a valid 15 character GSTIN';\n }\n if (!validatePattern(gstin)) {\n return 'Invalid GSTIN format';\n }\n else {\n if (gstin.toUpperCase()[14] !== calcCheckSum(gstin.toUpperCase())) {\n return 'Invalid checksum character in GSTIN';\n }\n else\n return 'Valid GSTIN';\n }\n}\nmodule.exports = {\n isValidGSTNumber: isValidGSTNumber,\n ValidateGSTIN: ValidateGSTIN,\n validateEInvoiceSignedQR: validateEInvoiceSignedQR,\n validateSignedInvoice: validateSignedInvoice,\n getGSTINInfo: getGSTINInfo,\n};\n//# sourceMappingURL=index.js.map\n\n//# sourceURL=webpack://sgf-gstin-validator/./dist/index.js?");
/***/ })
/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/
/******/ // startup
/******/ // Load entry module and return exports
/******/ // This entry module is referenced by other modules so it can't be inlined
/******/ var __webpack_exports__ = __webpack_require__("./dist/index.js");
/******/
/******/ })()
;