ton-assembly
Version:
TON assembler and disassembler
127 lines • 4.86 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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const cac_1 = require("cac");
const fs = __importStar(require("node:fs/promises"));
const core_1 = require("@ton/core");
const runtime_1 = require("../runtime");
const text_1 = require("../text");
async function getBocBuffer(options, file) {
if (options.string !== undefined) {
const trimmedContent = options.string.trim();
if (!trimmedContent) {
throw new Error("Provided string is empty");
}
if (options.format === "hex") {
return Buffer.from(trimmedContent, "hex");
}
if (options.format === "base64") {
return Buffer.from(trimmedContent, "base64");
}
throw new Error(`Unsupported format for string input: ${options.format}. Use 'hex' or 'base64'`);
}
if (file === undefined) {
throw new Error("Either file or string input is required");
}
if (options.format === "binary") {
return fs.readFile(file);
}
const content = await fs.readFile(file, "utf8");
const trimmedContent = content.trim();
if (!trimmedContent) {
throw new Error("File is empty");
}
if (options.format === "hex") {
return Buffer.from(trimmedContent, "hex");
}
if (options.format === "base64") {
return Buffer.from(trimmedContent, "base64");
}
throw new Error(`Unsupported format: ${options.format}`);
}
const cli = (0, cac_1.cac)("tdisasm");
cli.command("[file]", "Disassemble a BOC file or string to TASM")
.option("-o, --output <file>", "Output file path")
.option("-f, --format <format>", "Input format (binary|hex|base64)", { default: "binary" })
.option("-s, --string <data>", "Input data as hex or base64 string instead of file")
.option("--verbose", "Verbose output")
.action(async (file, options) => {
if (!file && options.string === undefined) {
console.error("Error: Either input file or string data is required");
process.exit(1);
}
try {
if (options.verbose) {
if (options.string === undefined) {
console.log(`Reading file: ${file}`);
}
else {
console.log(`Processing string data (${options.format} format)`);
}
}
const bocBuffer = await getBocBuffer(options, file);
if (options.verbose) {
console.log(`Parsed BOC buffer of ${bocBuffer.length} bytes`);
}
const [rootCell] = core_1.Cell.fromBoc(bocBuffer);
if (!rootCell) {
throw new Error("No cells found in BOC");
}
const instructions = (0, runtime_1.decompileCell)(rootCell);
if (options.verbose) {
console.log(`Decompiled ${instructions.length} instructions`);
}
const assemblyText = (0, text_1.print)(instructions);
if (options.output) {
await fs.writeFile(options.output, assemblyText);
if (options.verbose) {
console.log(`Written to: ${options.output}`);
}
}
else {
console.log(assemblyText);
}
}
catch (error) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
console.error(`Error: ${error instanceof Error ? error.message : error}`);
process.exit(1);
}
});
cli.help();
cli.version("0.0.1");
cli.parse();
//# sourceMappingURL=disassembler.js.map