@broxus/locklift-verifier
Version:
Locklift plugin for integration with Everscan contract verification service
166 lines (165 loc) • 7.05 kB
JavaScript
;
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.tryToGetNodeModules = exports.getCompilerHash = exports.getSupportedCompilers = exports.getPathToBinaries = exports.getVerificationAppReleases = exports.download = exports.getPlatform = void 0;
const os_1 = __importDefault(require("os"));
const constants_1 = require("./constants");
const axios_1 = __importDefault(require("axios"));
const fs = __importStar(require("fs-extra"));
const tar_1 = __importDefault(require("tar"));
const path_1 = __importStar(require("path"));
const getPlatform = () => {
const type = os_1.default.type();
const architecture = os_1.default.arch();
let rustTarget = constants_1.supportedPlatforms.find(({ TYPE, ARCHITECTURE }) => type === TYPE && ARCHITECTURE === architecture);
if (!rustTarget) {
throw new Error(`Verification plugin doesn't support ${type} Os with ${architecture} architecture`);
}
return rustTarget.RUST_TARGET;
};
exports.getPlatform = getPlatform;
async function download(fileUrl, outputLocationPath) {
const writer = fs.createWriteStream(outputLocationPath);
return (0, axios_1.default)({
method: "get",
url: fileUrl,
responseType: "stream",
})
.then((response) => {
return new Promise((resolve, reject) => {
response.data.pipe(writer);
let error;
writer.on("error", (err) => {
error = err;
writer.close();
reject(err);
});
writer.on("close", () => {
if (!error) {
resolve(true);
}
});
});
})
.catch(async (e) => {
await fs.unlink(outputLocationPath);
throw e;
});
}
exports.download = download;
const getVerificationAppReleases = () => {
const url = `https://api.github.com/repos/broxus/everscan-verify/releases`;
return axios_1.default.get(url).then((res) => res.data.map((el) => el.tag_name.replace("v", "")));
};
exports.getVerificationAppReleases = getVerificationAppReleases;
const getPathToBinaries = async ({ pathToVerificationApp, version, }) => {
const platform = (0, exports.getPlatform)();
const isWindows = os_1.default.platform() === "win32";
const pathWithExtension = `${pathToVerificationApp}${isWindows ? ".exe" : ""}`;
if (fs.existsSync(pathWithExtension)) {
return pathWithExtension;
}
console.log(`Downloading everscan-verify@${version} app...`);
const repo_url = "https://github.com/broxus/everscan-verify";
const appName = "everscan-verify";
const url = `${repo_url}/releases/download/v${version}/${appName}-v${version}-${platform}.tar.gz`;
const pathToGzippedFile = `${pathToVerificationApp}.tar.gz`;
await download(url, pathToGzippedFile).catch((e) => {
throw new Error(`Downloading everscan-verify error\n fetch url: ${url}\n ${e.message}`);
});
fs.ensureDirSync(pathToVerificationApp);
try {
await tar_1.default.x({
cwd: pathToVerificationApp,
file: pathToGzippedFile,
});
fs.rmSync(pathToGzippedFile);
debugger;
fs.moveSync(path_1.default.resolve(pathToVerificationApp, "dist", `everscan-verify${isWindows ? ".exe" : ""}`), pathToVerificationApp + "temp");
fs.rmSync(pathToVerificationApp, { recursive: true });
fs.moveSync(pathToVerificationApp + "temp", pathWithExtension);
fs.chmodSync(pathWithExtension, "755");
console.log(`Everscan-verify@${version} was downloaded`);
return pathWithExtension;
}
catch (e) {
console.log(`Downloading error`);
try {
fs.rmSync(pathToGzippedFile);
}
catch (e) { }
throw new Error(e);
}
};
exports.getPathToBinaries = getPathToBinaries;
const getSupportedCompilers = async () => {
const compilers = await axios_1.default
.get("https://verify.everscan.io/supported/solc")
.then((res) => Object.keys(res.data));
const linkers = await axios_1.default.get("https://verify.everscan.io/supported/linker").then((res) => res.data);
return {
compilers,
linkers,
};
};
exports.getSupportedCompilers = getSupportedCompilers;
const getHashToCompilerMap = async () => {
return axios_1.default
.get(`https://api.github.com/repos/tonlabs/TON-Solidity-Compiler/tags`)
.then((res) => res.data)
.then((res) => res.reduce((acc, { commit: { sha }, name }) => ({ ...acc, [name]: sha }), {}));
};
const getCompilerHash = async ({ compilerVersion, compilerToHashMapPath, }) => {
if (!fs.existsSync(compilerToHashMapPath)) {
fs.writeFileSync(compilerToHashMapPath, JSON.stringify({}));
}
let compilerToHashMap = JSON.parse(fs.readFileSync(compilerToHashMapPath, "utf-8"));
if (!compilerToHashMap[compilerVersion]) {
console.log(`Finding hash for compiler ${compilerVersion} ...`);
compilerToHashMap = await getHashToCompilerMap().catch((e) => {
throw new Error(`Fetch compiler hashes error ${e}`);
});
if (!compilerToHashMap[compilerVersion]) {
throw new Error("Compiler not exists, please check your locklift.config and the compiler version");
}
console.log(`Found hash ${compilerToHashMap[compilerVersion]} for compiler ${compilerVersion}`);
fs.writeFileSync(compilerToHashMapPath, JSON.stringify(compilerToHashMap));
}
return compilerToHashMap[compilerVersion];
};
exports.getCompilerHash = getCompilerHash;
const tryToGetNodeModules = () => {
const findNodeModules = require("find-node-modules");
try {
return (0, path_1.resolve)(findNodeModules()[0]);
}
catch (e) {
return undefined;
}
};
exports.tryToGetNodeModules = tryToGetNodeModules;