import fs from "fs"; import path from "path"; import commander from "commander"; import { ProviderRpcClient } from "everscale-inpage-provider"; import type { ConnectionData, Ed25519KeyPair } from "everscale-standalone-client"; import { ConnectionProperties, NETWORK_PRESETS } from "everscale-standalone-client"; import { Giver } from "../factory"; import Joi from "joi"; import { MessageProperties } from "everscale-standalone-client"; import semver from "semver/preload"; export enum ConfigState { EXTERNAL, INTERNAL, } export const LOCKLIFT_NETWORK_NAME = "locklift"; type LockliftNetworkName = typeof LOCKLIFT_NETWORK_NAME; export interface LockliftConfig { compiler: { includesPath?: string; externalContracts?: ExternalContracts; externalContractsArtifacts?: ExternalContracts; compilerParams?: Array; } & ({ path: string } | { version: string }); linker?: | { path: string; lib: string; } | { version: string }; networks: Networks; mocha: Mocha.MochaOptions & { tsconfig?: string; }; } export type KeysConfig = { path?: string; phrase?: string; amount: number; }; export type ForkContractsConfig = Array< { abi: { path: string; }; } & ({ codeHash: string | { deriveAddress: string } } | { address: string }) >; export type ForkSource = { type: "live"; connection: ConnectionProperties } | { type: "block"; block: number }; export type Networks = Record<"local" | string, NetworkValue> & { [key in LockliftNetworkName]: NetworkValue; }; export interface NetworkValue { giver: T extends ConfigState.EXTERNAL ? P extends LockliftNetworkName ? GiverConfig | undefined : GiverConfig : GiverConfig; keys: T extends ConfigState.EXTERNAL ? KeysConfig : Required; connection: T extends ConfigState.EXTERNAL ? ConnectionProperties : ConnectionData; fork?: { source: ForkSource; contracts: ForkContractsConfig; }; blockchainConfig?: "EVER" | "TON" | { custom: string } | undefined; clientConfig?: { message?: MessageProperties; initInput?: any | Promise; }; } export type ExternalContracts = Record>; export type GiverConfig = { address: string; giverFactory?: (ever: ProviderRpcClient, keyPair: Ed25519KeyPair, address: string) => Giver; } & ({ key: string } | { phrase: string; accountId: number } | { phrase: string; path: string }); export const JoiConfig = Joi.object({ compiler: Joi.alternatives([ Joi.object({ includesPath: Joi.string().optional(), compilerParams: Joi.array().items(Joi.string()).optional(), externalContracts: Joi.object().pattern(Joi.string(), Joi.array().items(Joi.string())), externalContractsArtifacts: Joi.object().pattern(Joi.string(), Joi.array().items(Joi.string())), path: Joi.string(), }), Joi.object({ includesPath: Joi.string().optional(), compilerParams: Joi.array().items(Joi.string()).optional(), externalContracts: Joi.object().pattern(Joi.string(), Joi.array().items(Joi.string())), externalContractsArtifacts: Joi.object().pattern(Joi.string(), Joi.array().items(Joi.string())), version: Joi.string().required(), }), ]), linker: Joi.when("compiler.version", { is: Joi.custom((value, helpers) => { if (semver.lte(value, "0.71.0")) { return value; } return helpers.message({ custom: "DUMMY MSG" }); }), then: Joi.alternatives([ Joi.object({ path: Joi.string(), lib: Joi.string(), }), Joi.object({ version: Joi.string(), }), ]).required(), otherwise: Joi.any(), }), networks: Joi.object().pattern( Joi.string(), Joi.object({ giver: Joi.alternatives().conditional(Joi.object({ phrase: Joi.string().required() }).unknown(), { then: Joi.alternatives([ Joi.object({ address: Joi.string(), giverFactory: Joi.any().optional(), phrase: Joi.string(), accountId: Joi.number(), }), Joi.object({ address: Joi.string(), giverFactory: Joi.any().optional(), phrase: Joi.string(), path: Joi.string().allow(""), }), ]), otherwise: Joi.object({ address: Joi.string(), giverFactory: Joi.any().optional(), key: Joi.string(), }), }), keys: Joi.object({ path: Joi.string().optional(), phrase: Joi.string().optional(), amount: Joi.number(), }), connection: Joi.alternatives([ ...Object.keys(NETWORK_PRESETS).map(el => el), Joi.object({ id: Joi.number().optional(), type: Joi.alternatives(["graphql", "jrpc", "proxy"]), group: Joi.string().optional(), data: Joi.alternatives().conditional("type", { is: "graphql", then: Joi.object({ endpoints: Joi.array().items(Joi.string()), local: Joi.boolean().optional(), latencyDetectionInterval: Joi.number().optional(), maxLatency: Joi.number().optional(), }), otherwise: Joi.alternatives().conditional("type", { is: "jrpc", then: Joi.object({ endpoint: Joi.string(), }), otherwise: Joi.object({ connectionFactory: Joi.object().custom(value => { return value; // if (value instanceof ConnectionFactory) { // return value; // } // return helpers.message({"custom": "Invalid proxy connection"}); }), }), // otherwise: Joi.object().custom((value, helpers) => { // if (value instanceof ConnectionFactory) { // return value; // } // return helpers.message({"custom": "Invalid proxy connection"}); // }), }), }), }), ]), clientConfig: Joi.object({ message: Joi.object({}).optional().unknown(), initInput: Joi.any().optional(), }).optional(), tracing: Joi.object({ endpoint: Joi.string(), }).optional(), }).unknown(), ), mocha: Joi.object({ tsconfig: Joi.string().optional(), }).unknown(true), }).unknown(); export function loadConfig(configPath: string): LockliftConfig { const resolvedConfigPath = path.resolve(process.cwd(), configPath); if (!fs.existsSync(resolvedConfigPath)) { throw new commander.InvalidOptionArgumentError(`Config at ${configPath} not found!`); } // eslint-disable-next-line @typescript-eslint/no-require-imports const configFile = require(resolvedConfigPath); const validationResult = JoiConfig.validate(configFile.default); if (validationResult.error) { if (validationResult.error instanceof Joi.ValidationError) { throw new Error(validationResult.error.annotate()); } throw validationResult.error; } const config = { ...validationResult.value } as unknown as LockliftConfig; for (const [key, value] of Object.entries(config.networks)) { if (value.keys != null) { value.keys = { ...value.keys, phrase: value.keys?.phrase || "maze turn choose industry beauty sweet panther valve double report upset mother", path: value.keys.path || "m/44'/396'/0'/0/INDEX", }; } if (typeof value.connection === "string") { value.connection = getPresetParams(value.connection) || value.connection; } config.networks[key] = { ...value, giver: value.giver || { address: "0:f2570313b1d6313db96f01ff3af1ebbae31111aa0bbf74f51397b67990bc2b73", key: "172af540e43a524763dd53b26a066d472a97c4de37d5498170564510608250c3", }, }; } return config as unknown as LockliftConfig; } const getPresetParams = (preset: string): ConnectionData | undefined => { if (isKeyofNetworkPreset(preset)) { return NETWORK_PRESETS[preset]; } }; const isKeyofNetworkPreset = (preset: string): preset is keyof typeof NETWORK_PRESETS => { return Object.keys(NETWORK_PRESETS).find(el => el === preset) !== undefined; };