Skip to content

Commit

Permalink
feat: Add "file info" command to CLI (iden3#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
xavi-pinsach authored Jul 29, 2022
1 parent 2575607 commit b607029
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import path from "path";
import bfj from "bfj";

import Logger from "logplease";
import * as binFileUtils from "@iden3/binfileutils";
const logger = Logger.create("snarkJS", {showTimestamp:false});
Logger.setLogLevel("INFO");

Expand Down Expand Up @@ -297,6 +298,12 @@ const commands = [
alias: ["pkv"],
options: "-verbose|v",
action: plonkVerify
},
{
cmd: "file info [binary.file]",
description: "Check info of a binary file",
alias: ["fi"],
action: fileInfo
}
];

Expand Down Expand Up @@ -1091,3 +1098,53 @@ async function plonkVerify(params, options) {
return 1;
}
}

async function fileInfo(params) {
const filename = params[0];
const extension = filename.split(".").pop();

if (!["zkey", "r1cs", "ptau", "wtns"].includes(extension)) {
console.error(`Extension ${extension} is not allowed.`);
return;
}

try {
const {
fd: fd,
sections: sections
} = await binFileUtils.readBinFile(filename, extension, 2, 1 << 25, 1 << 23);

console.log(`File info for ${filename}`);
console.log();
console.log(`File size: ${fd.totalSize} bytes`);
console.log(`File type: ${extension}`);
console.log(`Version: ${fd.version}`);
console.log(`Bin version: ${fd.binVersion}`);
console.log("");

sections.forEach((section, index) => {
let errors = [];
if (section.length > 1) errors.push(`Section ${index} has more than one section definition`);
else {
if (section[0].size === 0) {
errors.push(`Section ${index} size is zero. This could cause false errors in other sections.`);
}
}
if(section[0].p + section[0].size > fd.totalSize) {
errors.push(`Section ${index} is out of bounds of the file.`);
}

const color = errors.length === 0 ? "%s%s%s" : "%s\x1b[31m%s\x1b[0m%s";
const text0 = "section " + ("#" + index).padStart(5, " ");
const text1 = errors.length === 0 ? " " : " !!";
const text2 = ` size: ${section[0].size}\toffset: 0x${(section[0].p - 12).toString(16)}`;
console.log(color, text0, text1, text2);
errors.forEach((error) => {
console.error("\x1b[31m%s\x1b[0m", " > " + error);
});
});
} catch (error)
{
console.error(error.message);
}
}

0 comments on commit b607029

Please sign in to comment.