UNPKG

@taquito/michel-codec

Version:

Michelson parser, validator, and formatter for Taquito.

94 lines (93 loc) 3.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dummyContract = exports.Contract = void 0; const michelson_typecheck_1 = require("./michelson-typecheck"); const micheline_parser_1 = require("./micheline-parser"); const michelson_validator_1 = require("./michelson-validator"); const errors_1 = require("./errors"); class Contract { constructor(contract, opt) { this.contract = contract; this.ctx = { contract, ...opt }; this.output = (0, michelson_typecheck_1.assertContractValid)(contract, this.ctx); } static parse(src, opt) { const p = new micheline_parser_1.Parser(opt); const expr = typeof src === 'string' ? p.parseScript(src) : p.parseJSON(src); if (expr === null) { throw new errors_1.InvalidMichelsonError('empty Michelson'); } if ((0, michelson_validator_1.assertMichelsonContract)(expr)) { return new Contract(expr, opt); } throw undefined; } static parseTypeExpression(src, opt) { const p = new micheline_parser_1.Parser(opt); const expr = typeof src === 'string' ? p.parseScript(src) : p.parseJSON(src); if (expr === null) { throw new errors_1.InvalidTypeExpressionError('empty type expression'); } // remove assertTypeAnnotationsValid from if block because: () => void || throw error if ((0, michelson_validator_1.assertMichelsonType)(expr)) { (0, michelson_typecheck_1.assertTypeAnnotationsValid)(expr); return expr; } throw undefined; } static parseDataExpression(src, opt) { const p = new micheline_parser_1.Parser(opt); const expr = typeof src === 'string' ? p.parseScript(src) : p.parseJSON(src); if (expr === null) { throw new errors_1.InvalidDataExpressionError('empty data expression'); } if ((0, michelson_validator_1.assertMichelsonData)(expr)) { return expr; } throw undefined; } section(section) { return (0, michelson_typecheck_1.contractSection)(this.contract, section); } entryPoints() { return (0, michelson_typecheck_1.contractEntryPoints)(this.contract); } entryPoint(ep) { return (0, michelson_typecheck_1.contractEntryPoint)(this.contract, ep); } assertDataValid(d, t) { (0, michelson_typecheck_1.assertDataValid)(d, t, this.ctx); } isDataValid(d, t) { return (0, michelson_typecheck_1.isDataValid)(d, t, this.ctx); } assertParameterValid(ep, d) { const t = this.entryPoint(ep || undefined); if (t === null) { throw new errors_1.InvalidEntrypointError(ep?.toString()); } this.assertDataValid(d, t); } isParameterValid(ep, d) { try { this.assertParameterValid(ep, d); return true; } catch { return false; } } functionType(inst, stack) { return (0, michelson_typecheck_1.functionType)(inst, stack, this.ctx); } } exports.Contract = Contract; // TODO: dummyContract not used anywhere in the codebase can be deleted? exports.dummyContract = new Contract([ { prim: 'parameter', args: [{ prim: 'unit' }] }, { prim: 'storage', args: [{ prim: 'unit' }] }, { prim: 'code', args: [[{ prim: 'CAR' }, { prim: 'NIL', args: [{ prim: 'operation' }] }, { prim: 'PAIR' }]], }, ]);