create-tezos-smart-contract
Version:
Node.js toolset to write, test and deploy Tezos smart contracts
85 lines (84 loc) • 3.33 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContractsBundle = exports.BuildErrorCodes = void 0;
const console_1 = require("../../console");
const config_1 = require("../config");
const bundle_1 = require("./bundle");
const crypto_1 = __importDefault(require("crypto"));
var BuildErrorCodes;
(function (BuildErrorCodes) {
BuildErrorCodes["MICHELSON_MISSING"] = "MICHELSON_MISSING";
BuildErrorCodes["INVALID_SOURCE_PATH"] = "INVALID_SOURCE_PATH";
BuildErrorCodes["INVALID_HASH"] = "INVALID_HASH";
})(BuildErrorCodes = exports.BuildErrorCodes || (exports.BuildErrorCodes = {}));
class ContractsBundle extends bundle_1.Bundle {
constructor() {
super(...arguments);
this.config = config_1.defaultConfig;
this.generateHash = (contractData) => {
const hash = crypto_1.default.createHash('sha256');
hash.update(contractData);
return hash.digest('hex');
};
}
async readConfigFile() {
const fileName = config_1.ConfigFile.getName();
if (!this.exists(fileName)) {
(0, console_1.warn)("This smart contract repository it's missing \"config.json\" file, will proceed with default config instead.");
}
else {
const config = await this.readJSONFile(fileName);
const configFile = new config_1.ConfigFile(config);
try {
configFile.validate();
}
catch (validationError) {
(0, console_1.error)(`Could not read smart contract repository configuration:\n\n${validationError}`);
process.exit(1);
}
this.config = config;
}
return this.config;
}
async getContractsFiles() {
return await this.listFiles(this.config.contractsDirectory);
}
getContractFile(fileName) {
return this.getPath(this.config.contractsDirectory, fileName);
}
async readContract(contractName) {
const contractFile = this.getContractFile(contractName);
return await this.readTextFile(contractFile);
}
getBuildFile(contractName) {
return this.getPath(this.config.outputDirectory, `${contractName}.json`);
}
async writeBuildFile(contractName, data) {
const buildFile = this.getBuildFile(contractName);
return await this.writeJSONFile(buildFile, data, true);
}
async readBuildFile(contractName) {
const buildFile = this.getBuildFile(contractName);
return await this.readJSONFile(buildFile);
}
buildFileExists(contractName) {
const buildFile = this.getBuildFile(contractName);
return this.exists(buildFile);
}
isBuildValid(sourcePath, hash, buildFile) {
if (!buildFile.michelson || buildFile.michelson === '') {
return BuildErrorCodes.MICHELSON_MISSING;
}
if (buildFile.sourcePath !== sourcePath) {
return BuildErrorCodes.INVALID_SOURCE_PATH;
}
if (buildFile.hash !== hash) {
return BuildErrorCodes.INVALID_HASH;
}
return true;
}
}
exports.ContractsBundle = ContractsBundle;