@tezwell/michelson-sdk
Version:
Michelson SDK is a framework for generating Michelson values and types from Javascript.
563 lines (556 loc) • 18.1 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
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 __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/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 TOr = (leftType, rightType) => new Michelson_Type("or" /* or */, leftType, rightType);
// 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);
var Literals = {
Unit,
Nat,
Int,
Mutez,
String: String2,
Bool,
Address,
Contract,
Timestamp,
Chain_id,
Bytes,
Bls12_381_fr,
Bls12_381_g1,
Bls12_381_g2,
Key,
Key_hash,
Signature,
List,
Set,
None,
Some,
Pair,
Map,
Big_map,
Lambda,
Record,
Variant
};
var literal_default = Literals;
export {
Address,
Big_map,
Bls12_381_fr,
Bls12_381_g1,
Bls12_381_g2,
Bool,
Bytes,
Chain_id,
Contract,
Int,
Key,
Key_hash,
Lambda,
Left,
List,
Map,
Michelson_Literal,
Michelson_Literal_C1,
Michelson_Map,
Mutez,
Nat,
None,
Pair,
Record,
Right,
Set,
Signature,
Some,
String2 as String,
Timestamp,
Unit,
Variant,
literal_default as default
};