UNPKG

@kubiklabs/wasmkit

Version:

Wasmkit is a development environment to compile, deploy, test, run cosmwasm contracts on different networks.

131 lines (130 loc) 5.17 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (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.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getValidationErrors = exports.validateConfig = void 0; const z = __importStar(require("zod")); const zod_errors_1 = require("../../util/zod-errors"); const errors_1 = require("../errors"); const errors_list_1 = require("../errors-list"); const config_errors_1 = __importDefault(require("./config-errors")); const AccountType = z.object({ name: z.string(), address: z.string(), mnemonic: z.string() }); const HttpNetworkType = z.object({ accounts: z.array(AccountType).optional(), endpoint: z.string().optional(), nodeId: z.string().optional(), chainId: z.string().optional(), keyringBackend: z.string().optional() }).nonstrict(); const NetworksType = z.record(HttpNetworkType); const ProjectPaths = z.object({ root: z.string().optional(), cache: z.string().optional(), artifacts: z.string().optional(), sources: z.string().optional(), tests: z.string().optional() }).nonstrict(); const Config = z.object({ networks: NetworksType.optional(), paths: ProjectPaths.optional() }).nonstrict(); /** * Validates the config, throwing a BuilderError if invalid. * @param config */ function validateConfig(config) { const errors = getValidationErrors(config); if (errors.isEmpty()) { return; } const errorList = ` * ${errors.toString()}`; throw new errors_1.WasmkitError(errors_list_1.ERRORS.GENERAL.INVALID_CONFIG, { errors: errorList }); } exports.validateConfig = validateConfig; function getValidationErrors(config) { const errors = new config_errors_1.default(); if (config !== undefined && typeof config.networks === "object") { for (const [net, ncfg] of Object.entries(config.networks)) { const accountsMap = new Map(); // {} as ([key: string]: number); let j; for (let i = 0; i < (ncfg.accounts || []).length; ++i) { const a = ncfg.accounts[i]; const p = errors.putter(net + ".accounts", i.toString()); if (a.name === undefined) { const errorMessage = `Account with index ${i} does not have name specified`; p.push('name', errorMessage, 'string'); } if (a.address === undefined) { const errorMessage = `Account with index ${i} does not have address specified`; p.push('address', errorMessage, 'string'); } if (a.mnemonic === undefined) { const errorMessage = `Account with index ${i} does not have mnemonic specified`; p.push('mnemonic', errorMessage, 'string'); } if ((j = accountsMap.get(a.name)) !== undefined) { const errorMessage = `Account name ${String(a.name)} already exists at index ${String(j)}`; p.push('name', errorMessage, 'string'); } else { accountsMap.set(a.name, i); } } try { HttpNetworkType.parse(ncfg); } catch (e) { if (e instanceof z.ZodError) { errors.appendErrors([(0, zod_errors_1.parseZodError)(e)]); } } } } if (!errors.isEmpty()) { return errors; } try { Config.parse(config); } catch (e) { if (e instanceof z.ZodError) { errors.appendErrors([(0, zod_errors_1.parseZodError)(e)]); } } return errors; } exports.getValidationErrors = getValidationErrors; // Reference: https://stackoverflow.com/questions/5717093 // eslint-disable-next-line const exp = new RegExp('^(https?:\\/\\/)?' + // protocol '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name '(localhost)|' + // localhost '((\\d{1,3}\\.){3}\\d{1,3}))'); // OR ip (v4) address