UNPKG

@tezwell/michelson-sdk

Version:

Michelson SDK is a framework for generating Michelson values and types from Javascript.

143 lines (138 loc) 4.86 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __reExport = (target, module2, copyDefault, desc) => { if (module2 && typeof module2 === "object" || typeof module2 === "function") { for (let key of __getOwnPropNames(module2)) if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default")) __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable }); } return target; }; var __toCommonJS = /* @__PURE__ */ ((cache) => { return (module2, temp) => { return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp); }; })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0); // src/converter/index.ts var converter_exports = {}; __export(converter_exports, { default: () => converter_default, michelineOfJSON: () => michelineOfJSON }); // src/misc/guards.ts var isPrim = (michelson) => "prim" in michelson; var isInt = (michelson) => "int" in michelson; var isString = (michelson) => "string" in michelson; var isBytes = (michelson) => "bytes" in michelson; var Guards = { isPrim, isInt, isString, isBytes }; var guards_default = Guards; // src/converter/json.ts var toMichelineType = (michelson) => { var _a; const args = ((_a = michelson.args) == null ? void 0 : _a.map((m) => toMicheline(m, ""))) || []; const annot = michelson.annots || []; if (args.length || annot.length) { return `(${[michelson.prim, ...annot, ...args].join(" ")})`; } return michelson.prim; }; var toMichelineSeq = (michelson, identation) => { const innerIdentation = identation + " ".repeat(2); return `${identation ? ` ${identation}` : identation}{ ${michelson.map((m) => toMicheline(m, innerIdentation)).join(` `)} ${identation}}`; }; var toMicheline = (michelson, padding = "") => { var _a; if (Array.isArray(michelson)) { return toMichelineSeq(michelson, padding); } if (guards_default.isInt(michelson)) { return michelson.int; } if (guards_default.isString(michelson)) { return `"${michelson.string}"`; } if (guards_default.isBytes(michelson)) { return michelson.bytes; } const prim = michelson.prim; const args = michelson.args || []; switch (michelson.prim) { case "storage" /* storage */: case "parameter" /* parameter */: return `${padding}${prim} ${args.map(toMichelineType).join("")};`; case "code" /* code */: return `${padding}${prim} ${toMicheline(((_a = michelson.args) == null ? void 0 : _a[0]) || [], padding).trim()};`; case "unit" /* unit */: case "nat" /* nat */: case "int" /* int */: case "mutez" /* mutez */: case "timestamp" /* timestamp */: case "string" /* string */: case "address" /* address */: case "bytes" /* bytes */: case "chain_id" /* chain_id */: case "bool" /* bool */: case "bls12_381_fr" /* bls12_381_fr */: case "bls12_381_g1" /* bls12_381_g1 */: case "bls12_381_g2" /* bls12_381_g2 */: case "key" /* key */: case "key_hash" /* key_hash */: case "signature" /* signature */: case "operation" /* operation */: case "never" /* never */: case "list" /* list */: case "set" /* set */: case "option" /* option */: case "pair" /* pair */: case "or" /* or */: case "map" /* map */: case "big_map" /* big_map */: case "lambda" /* lambda */: case "ticket" /* ticket */: case "contract" /* contract */: case "sapling_state" /* sapling_state */: case "sapling_transaction" /* sapling_transaction */: return toMichelineType(michelson); case "None" /* None */: case "True" /* True */: case "False" /* False */: case "Unit" /* Unit */: return prim; case "Some" /* Some */: case "Pair" /* Pair */: case "Left" /* Left */: case "Right" /* Right */: return `(${[prim, ...(args == null ? void 0 : args.map((m) => toMicheline(m, padding))) || []].join(" ")})`; default: return `${padding}${[prim, ...(args == null ? void 0 : args.map((m) => toMicheline(m, padding))) || []].join(" ")};`; } }; var Michelson_JSON = { toMichelineType, toMichelineSeq, toMicheline }; var json_default = Michelson_JSON; // src/converter/index.ts var michelineOfJSON = (json) => json_default.toMicheline(json, ""); var Converter = { michelineOfJSON }; var converter_default = Converter; module.exports = __toCommonJS(converter_exports);