@xorddotcom/shield
Version:
p align="center" > <img src="https://xord.notion.site/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F283b98b7-fdae-4e5a-acaf-248242084e4a%2FICON.png?table=block&id=5306223c-a4f7-45d1-9f54-b9a5f4004cd6&spaceId=49976899-64a1-40f
161 lines (157 loc) • 6.27 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.arrayLogger = exports.bumpSolidityVersion = exports.createInterface = exports.updateCopyProjectName = exports.fileExists = exports.getEmptyDir = exports.getEmptyDirByPath = exports.indexDist = void 0;
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const fs_1 = __importDefault(require("fs"));
const logger_1 = require("./logger");
exports.indexDist = "node ../../dist/src/index.js";
const groth16InterfaceContent = (inputVariable, CIRCUIT_NAME, SOLIDITY_VERSION) => {
return `
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ${SOLIDITY_VERSION};
interface I${CIRCUIT_NAME}Verifier {
function verifyProof(
uint256[2] calldata a,
uint256[2][2] calldata b,
uint256[2] calldata c,
${inputVariable}
) external view returns (bool);
}`;
};
const plonkInterfaceContent = (inputVariable, CIRCUIT_NAME, SOLIDITY_VERSION) => {
return `
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ${SOLIDITY_VERSION};
interface I${CIRCUIT_NAME}Verifier {
function verifyProof(bytes memory proof, ${inputVariable})
external
view
returns (bool);
}`;
};
const getEmptyDirByPath = async (path, exitCode) => {
const dir = await fs_extra_1.default.ensureDir(path);
if (dir === undefined) {
(0, logger_1.log)(`"${path}" dir already exist`, "info");
if (exitCode === 1) {
process.exit(1);
}
return;
}
(0, logger_1.log)(`✓ "${path}" dir created`, "success");
await fs_extra_1.default.emptyDir(path);
return path;
};
exports.getEmptyDirByPath = getEmptyDirByPath;
const getEmptyDir = async (name, exitCode) => {
try {
const tmpDir = path_1.default.join(process.cwd(), `/${name}`);
const dir = await fs_extra_1.default.ensureDir(tmpDir);
if (dir === undefined) {
(0, logger_1.log)(`A folder named "${name}" already exist, delete or move it to somewhere else and try again!!`, "error");
if (exitCode === 1) {
process.exit(1);
}
}
await fs_extra_1.default.emptyDir(tmpDir);
return tmpDir;
}
catch (error) {
(0, logger_1.log)(`${error}`, "error");
throw error;
}
};
exports.getEmptyDir = getEmptyDir;
const fileExists = async (file) => {
return fs_1.default.promises
.access(file, fs_1.default.constants.F_OK)
.then(() => true)
.catch(() => false);
};
exports.fileExists = fileExists;
const updateCopyProjectName = async (name, projectPath) => {
try {
const packageJsonPath = path_1.default.join(projectPath, `/package.json`);
const packageJson = fs_extra_1.default.readJsonSync(packageJsonPath);
packageJson.name = name;
const res = await fs_extra_1.default.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 3));
return res;
}
catch (error) {
(0, logger_1.log)("unable to locate the package.json file or rewrite the project name", "error");
return null;
}
};
exports.updateCopyProjectName = updateCopyProjectName;
const createInterface = async (CIRCUIT_NAME, protocol, content, SOLIDITY_VERSION) => {
try {
const tmpDir = path_1.default.join(process.cwd(), `/contracts/interfaces`);
await fs_extra_1.default.ensureDir(tmpDir);
await fs_extra_1.default.createFileSync(`./contracts/interfaces/I${CIRCUIT_NAME}Verifier.sol`);
let inputVariable = "";
let interfaceBumped = "";
if (protocol === "groth16") {
inputVariable = content
.split("uint[2] memory c,")[1]
.split(")")[0]
.trim();
interfaceBumped = groth16InterfaceContent(inputVariable, CIRCUIT_NAME, SOLIDITY_VERSION);
}
else {
inputVariable = content
.split("bytes memory proof,")[1]
.split(")")[0]
.trim();
interfaceBumped = plonkInterfaceContent(inputVariable, CIRCUIT_NAME, SOLIDITY_VERSION);
}
await fs_extra_1.default.writeFileSync(`./contracts/interfaces/I${CIRCUIT_NAME}Verifier.sol`, interfaceBumped);
}
catch (error) {
(0, logger_1.log)(`${error}`, "error");
throw error;
}
};
exports.createInterface = createInterface;
const bumpSolidityVersion = async (SOLIDITY_VERSION, CIRCUIT_NAME, PROTOCOL) => {
try {
const solidityRegex = /pragma solidity \^\d+\.\d+\.\d+/;
const content = fs_extra_1.default.readFileSync(`./contracts/${CIRCUIT_NAME}_Verifier.sol`, {
encoding: "utf-8",
});
await (0, exports.createInterface)(CIRCUIT_NAME, PROTOCOL, content, SOLIDITY_VERSION);
const bumped = content.replace(solidityRegex, `pragma solidity ${SOLIDITY_VERSION}`);
let bumpedContractName = "";
if (PROTOCOL === "groth16") {
bumpedContractName = bumped.replace("contract Verifier", `contract ${CIRCUIT_NAME}Verifier`);
}
else {
bumpedContractName = bumped.replace("contract PlonkVerifier", `contract ${CIRCUIT_NAME}Verifier`);
}
fs_extra_1.default.writeFileSync(`./contracts/${CIRCUIT_NAME}_Verifier.sol`, bumpedContractName);
}
catch (error) {
(0, logger_1.log)(`${error}`, "error");
throw error;
}
};
exports.bumpSolidityVersion = bumpSolidityVersion;
const arrayLogger = (key, array) => {
let prefixes = {};
for (let index = 0; index < array.length; index++) {
const element = array[index];
if (typeof element === "object") {
const subPrefixes = (0, exports.arrayLogger)(`${key}[${index}]`, element);
prefixes = { ...prefixes, ...subPrefixes };
}
else {
prefixes[`${key}[${index}]`] = element;
}
}
return prefixes;
};
exports.arrayLogger = arrayLogger;
//# sourceMappingURL=utils.js.map