UNPKG

@tezwell/michelson-sdk

Version:

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

622 lines (614 loc) 22.9 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); 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); var __accessCheck = (obj, member, msg) => { if (!member.has(obj)) throw TypeError("Cannot " + msg); }; var __privateGet = (obj, member, getter) => { __accessCheck(obj, member, "read from private field"); return getter ? getter.call(obj) : member.get(obj); }; var __privateAdd = (obj, member, value) => { if (member.has(obj)) throw TypeError("Cannot add the same private member more than once"); member instanceof WeakSet ? member.add(obj) : member.set(obj, value); }; var __privateSet = (obj, member, value, setter) => { __accessCheck(obj, member, "write to private field"); setter ? setter.call(obj, value) : member.set(obj, value); return value; }; // src/index.ts var src_exports = {}; __export(src_exports, { Address: () => Address, Big_map: () => Big_map, Bls12_381_fr: () => Bls12_381_fr, Bls12_381_g1: () => Bls12_381_g1, Bls12_381_g2: () => Bls12_381_g2, Bool: () => Bool, Bytes: () => Bytes, Chain_id: () => Chain_id, Contract: () => Contract, Int: () => Int, Key: () => Key, Key_hash: () => Key_hash, Lambda: () => Lambda, Left: () => Left, List: () => List, Map: () => Map, Michelson_Literal: () => Michelson_Literal, Michelson_Literal_C1: () => Michelson_Literal_C1, Michelson_Map: () => Michelson_Map, Michelson_Type: () => Michelson_Type, Michelson_Type_With_Param: () => Michelson_Type_With_Param, Mutez: () => Mutez, Nat: () => Nat, None: () => None, Pair: () => Pair, Record: () => Record, Right: () => Right, Set: () => Set, Signature: () => Signature, Some: () => Some, String: () => String2, TAddress: () => TAddress, TBig_map: () => TBig_map, TBls12_381_fr: () => TBls12_381_fr, TBls12_381_g1: () => TBls12_381_g1, TBls12_381_g2: () => TBls12_381_g2, TBool: () => TBool, TBytes: () => TBytes, TChain_id: () => TChain_id, TContract: () => TContract, TInt: () => TInt, TKey: () => TKey, TKey_hash: () => TKey_hash, TLambda: () => TLambda, TList: () => TList, TMap: () => TMap, TMutez: () => TMutez, TNat: () => TNat, TNever: () => TNever, TOperation: () => TOperation, TOption: () => TOption, TOr: () => TOr, TPair: () => TPair, TRecord: () => TRecord, TSapling_state: () => TSapling_state, TSapling_transaction: () => TSapling_transaction, TSet: () => TSet, TSignature: () => TSignature, TString: () => TString, TTicket: () => TTicket, TTimestamp: () => TTimestamp, TUnit: () => TUnit, TVariant: () => TVariant, Timestamp: () => Timestamp, Unit: () => Unit, Variant: () => Variant, buildRecordVariantType: () => buildRecordVariantType }); // src/misc/utils.ts var parenthesis = (str) => `(${str})`; var curlyBrackets = (str) => `{ ${str} }`; var capitalizeBoolean = (bool) => bool ? "True" /* True */ : "False" /* False */; var compressHexString = (str) => { return (str.slice(0, 2) === "0x" ? str.slice(2) : str).toLowerCase(); }; var composeRightCombLayout = (fields) => { if (fields.length > 2) { return [fields[0], composeRightCombLayout(fields.slice(1))]; } return fields; }; var Utils = { parenthesis, curlyBrackets, capitalizeBoolean, compressHexString, composeRightCombLayout }; var utils_default = Utils; // src/type.ts var _annotation; var Michelson_Type = class { constructor(type, ...innerTypes) { this.type = type; __privateAdd(this, _annotation, void 0); this.innerTypes = innerTypes; } setAnnotation(annotation) { __privateSet(this, _annotation, annotation); return this; } toMicheline() { const annot = __privateGet(this, _annotation) ? `%${__privateGet(this, _annotation)}` : ""; switch (this.type) { case "unit" /* unit */: case "int" /* int */: case "nat" /* nat */: case "mutez" /* mutez */: case "timestamp" /* timestamp */: case "string" /* string */: case "address" /* address */: case "chain_id" /* chain_id */: case "bool" /* bool */: case "bytes" /* bytes */: 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 */: return annot ? parenthesis(`${this.type} ${annot}`) : this.type; case "list" /* list */: case "set" /* set */: case "pair" /* pair */: case "or" /* or */: case "option" /* option */: 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 parenthesis([this.type, ...annot ? [annot] : [], ...this.innerTypes.map((t) => t.toMicheline())].join(" ")); } } toJSON() { const obj = __privateGet(this, _annotation) ? { annots: [`%${__privateGet(this, _annotation)}`] } : {}; switch (this.type) { case "unit" /* unit */: case "int" /* int */: case "nat" /* nat */: case "mutez" /* mutez */: case "timestamp" /* timestamp */: case "string" /* string */: case "address" /* address */: case "chain_id" /* chain_id */: case "bool" /* bool */: case "bytes" /* bytes */: 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 */: return __spreadProps(__spreadValues({}, obj), { prim: this.type }); case "list" /* list */: case "set" /* set */: case "pair" /* pair */: case "or" /* or */: case "option" /* option */: 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 __spreadProps(__spreadValues({}, obj), { prim: this.type, args: this.innerTypes.map((t) => t.toJSON()) }); } } [Symbol.toPrimitive]() { return this.toJSON(); } }; _annotation = new WeakMap(); var _annotation2, _params; var Michelson_Type_With_Param = class { constructor(type, ...params) { this.type = type; __privateAdd(this, _annotation2, void 0); __privateAdd(this, _params, void 0); __privateSet(this, _params, params); } setAnnotation(annotation) { __privateSet(this, _annotation2, annotation); return this; } toMicheline() { return `(${[this.type, ...__privateGet(this, _params).map(String)].join(" ")})`; } toJSON() { const obj = __privateGet(this, _annotation2) ? { annots: [`%${__privateGet(this, _annotation2)}`] } : {}; return __spreadProps(__spreadValues({}, obj), { prim: this.type, args: __privateGet(this, _params).map((p) => ({ int: String(p) })) }); } [Symbol.toPrimitive]() { return this.toJSON(); } }; _annotation2 = new WeakMap(); _params = new WeakMap(); var buildRecordVariantType = (fields, layout, container) => { const buildBranch = (branch) => { if (typeof branch === "string") { return fields[branch].setAnnotation(branch); } const [left, right] = branch; return container(buildBranch(left), buildBranch(right)); }; return Object.assign(buildBranch(layout), { fields, layout }); }; var TNat = () => new Michelson_Type("nat" /* nat */); var TInt = () => new Michelson_Type("int" /* int */); var TMutez = () => new Michelson_Type("mutez" /* mutez */); var TString = () => new Michelson_Type("string" /* string */); var TBool = () => new Michelson_Type("bool" /* bool */); var TAddress = () => new Michelson_Type("address" /* address */); var TTimestamp = () => new Michelson_Type("timestamp" /* timestamp */); var TChain_id = () => new Michelson_Type("chain_id" /* chain_id */); var TBytes = () => new Michelson_Type("bytes" /* bytes */); var TBls12_381_fr = () => new Michelson_Type("bls12_381_fr" /* bls12_381_fr */); var TBls12_381_g1 = () => new Michelson_Type("bls12_381_g1" /* bls12_381_g1 */); var TBls12_381_g2 = () => new Michelson_Type("bls12_381_g2" /* bls12_381_g2 */); var TKey = () => new Michelson_Type("key" /* key */); var TKey_hash = () => new Michelson_Type("key_hash" /* key_hash */); var TSignature = () => new Michelson_Type("signature" /* signature */); var TUnit = () => new Michelson_Type("unit" /* unit */); var TOperation = () => new Michelson_Type("operation" /* operation */); var TNever = () => new Michelson_Type("never" /* never */); var TList = (innerType) => new Michelson_Type("list" /* list */, innerType); var TSet = (innerType) => new Michelson_Type("set" /* set */, innerType); var TOption = (innerType) => new Michelson_Type("option" /* option */, innerType); var TPair = (leftType, rightType) => new Michelson_Type("pair" /* pair */, leftType, rightType); var TOr = (leftType, rightType) => new Michelson_Type("or" /* or */, leftType, rightType); var TMap = (keyType, valueType) => new Michelson_Type("map" /* map */, keyType, valueType); var TBig_map = (keyType, valueType) => new Michelson_Type("big_map" /* big_map */, keyType, valueType); var TLambda = (inType, outType) => new Michelson_Type("lambda" /* lambda */, inType, outType); var TTicket = (innerType) => new Michelson_Type("ticket" /* ticket */, innerType); var TContract = (innerType) => new Michelson_Type("contract" /* contract */, innerType); var TSapling_state = (memoSize) => new Michelson_Type_With_Param("sapling_state" /* sapling_state */, memoSize); var TSapling_transaction = (memoSize) => new Michelson_Type_With_Param("sapling_transaction" /* sapling_transaction */, memoSize); var TRecord = (fields, layout) => buildRecordVariantType(fields, layout || composeRightCombLayout(Object.keys(fields)), TPair); var TVariant = (fields, layout) => buildRecordVariantType(fields, layout || composeRightCombLayout(Object.keys(fields)), TOr); // 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; // src/literal.ts var Michelson_Literal = class { constructor(prim, value) { this.prim = prim; switch (prim) { case "Unit" /* Unit */: case "None" /* None */: case "False" /* False */: case "True" /* True */: this.value = prim; break; default: if (typeof value === "undefined") { throw new Error("Expected a value!"); } this.value = value; } } toMicheline() { switch (this.prim) { case "None" /* None */: case "False" /* False */: case "True" /* True */: case "Unit" /* Unit */: case "int" /* int */: case "bytes" /* bytes */: return `${this.value}`; case "string" /* string */: return `"${this.value}"`; } throw new Error(`Cannot produce michelson for literal of type: ${this.prim}`); } toJSON() { switch (this.prim) { case "None" /* None */: case "Unit" /* Unit */: case "False" /* False */: case "True" /* True */: return { prim: this.value }; case "int" /* int */: return { ["int" /* int */]: `${this.value}` }; case "string" /* string */: return { ["string" /* string */]: this.value }; case "bytes" /* bytes */: return { ["bytes" /* bytes */]: utils_default.compressHexString(`${this.value}`) }; } throw new Error(`Cannot produce michelson JSON for literal of type: ${this.prim}`); } }; var _prim, _elements; var Michelson_Literal_C1 = class { constructor(prim, elements) { __privateAdd(this, _prim, void 0); __privateAdd(this, _elements, void 0); __privateSet(this, _prim, prim); __privateSet(this, _elements, elements); } toMicheline(wrap = true) { switch (__privateGet(this, _prim)) { case "Some" /* Some */: case "Pair" /* Pair */: case "Left" /* Left */: case "Right" /* Right */: const prim = `${__privateGet(this, _prim)} ${__privateGet(this, _elements).map((v) => v.toMicheline()).join(" ")}`; return wrap ? parenthesis(prim) : prim; case "list" /* list */: return curlyBrackets(__privateGet(this, _elements).map((v) => v.toMicheline(false)).join(" ; ")); } throw new Error(`Cannot produce michelson for literal of type: ${__privateGet(this, _prim)}`); } toJSON() { switch (__privateGet(this, _prim)) { case "Some" /* Some */: case "Pair" /* Pair */: case "Left" /* Left */: case "Right" /* Right */: return { prim: __privateGet(this, _prim), args: __privateGet(this, _elements).map((v) => v.toJSON()) }; case "list" /* list */: return __privateGet(this, _elements).map((v) => v.toJSON()); } throw new Error(`Cannot produce michelson JSON for literal of type: ${__privateGet(this, _prim)}`); } }; _prim = new WeakMap(); _elements = new WeakMap(); var _elements2; var Michelson_Map = class { constructor(elements) { __privateAdd(this, _elements2, void 0); this.buildMichelineElt = (key, value) => { return `${"Elt" /* Elt */} ${key.toMicheline()} ${value.toMicheline()}`; }; __privateSet(this, _elements2, elements); } toMicheline() { return curlyBrackets(__privateGet(this, _elements2).map(([key, value]) => this.buildMichelineElt(key, value)).join(" ; ")); } toJSON() { return __privateGet(this, _elements2).map(([key, value]) => ({ prim: "Elt" /* Elt */, args: [key.toJSON(), value.toJSON()] })); } }; _elements2 = new WeakMap(); var buildRecord = (fields, layout) => { const buildBranch = (branch) => { if (typeof branch === "string") { return fields[branch]; } const [left, right] = branch; return Pair(buildBranch(left), buildBranch(right)); }; return buildBranch(layout || composeRightCombLayout(Object.keys(fields))); }; var buildVariant = (target, value, type) => { const [left, right] = type.layout; if (left === target) { return Left(value); } if (right === target) { return Right(value); } if (Array.isArray(left) && left.flat().includes(target)) { return Left(buildVariant(target, value, buildRecordVariantType(type.fields, left, TOr))); } if (Array.isArray(right) && right.flat().includes(target)) { return Right(buildVariant(target, value, buildRecordVariantType(type.fields, right, TOr))); } throw new Error(`Variant (${target}) is invalid.`); }; var buildLambda = (michelson) => { if (typeof michelson === "string") { return { toMicheline: () => michelson, toJSON: () => { throw new Error("Convertion from Micheline to JSON is not implemented."); } }; } return { toMicheline: () => converter_default.michelineOfJSON(michelson), toJSON: () => michelson }; }; var Nat = (value) => new Michelson_Literal("int" /* int */, value); var Int = (value) => new Michelson_Literal("int" /* int */, value); var Mutez = (value) => new Michelson_Literal("int" /* int */, value); var Timestamp = (value) => new Michelson_Literal(typeof value === "string" ? "string" /* string */ : "int" /* int */, value); var String2 = (value) => new Michelson_Literal("string" /* string */, value); var Address = (address) => new Michelson_Literal("string" /* string */, address); var Contract = (address, entry_point) => new Michelson_Literal("string" /* string */, `${address}%${entry_point}`); var Bytes = (value) => new Michelson_Literal("bytes" /* bytes */, value); var Chain_id = (value) => { return new Michelson_Literal(`${value}`.slice(0, 2) === "0x" ? "bytes" /* bytes */ : "string" /* string */, value); }; var Bls12_381_fr = (value) => new Michelson_Literal(typeof value === "string" ? "bytes" /* bytes */ : "int" /* int */, value); var Bls12_381_g1 = (value) => new Michelson_Literal("bytes" /* bytes */, value); var Bls12_381_g2 = (value) => new Michelson_Literal("bytes" /* bytes */, value); var Key = (key) => new Michelson_Literal("string" /* string */, key); var Key_hash = (key_hash) => new Michelson_Literal("string" /* string */, key_hash); var Signature = (signature) => new Michelson_Literal("string" /* string */, signature); var Bool = (bool) => new Michelson_Literal(bool ? "True" /* True */ : "False" /* False */); var Unit = () => new Michelson_Literal("Unit" /* Unit */); var List = (elements) => new Michelson_Literal_C1("list" /* list */, elements); var Set = List; var None = () => new Michelson_Literal("None" /* None */); var Some = (element) => new Michelson_Literal_C1("Some" /* Some */, [element]); var Pair = (left, right) => new Michelson_Literal_C1("Pair" /* Pair */, [left, right]); var Map = (elements = []) => new Michelson_Map(elements); var Big_map = Map; var Lambda = (code) => buildLambda(code); var Left = (value) => new Michelson_Literal_C1("Left" /* Left */, [value]); var Right = (value) => new Michelson_Literal_C1("Right" /* Right */, [value]); var Record = (fields, layout) => buildRecord(fields, layout); var Variant = (branch, value, type) => buildVariant(branch, value, type); module.exports = __toCommonJS(src_exports);