UNPKG

@taquito/michelson-encoder

Version:

converts michelson data and types into convenient JS/TS objects

104 lines (103 loc) 3.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ComparableToken = exports.Token = exports.TokenValidationError = void 0; const core_1 = require("@taquito/core"); /** * @category Error * @description Error that indicates a failure when encoding invalid or incorrect data (e.g. if an address is expected but a number is received) */ class TokenValidationError extends core_1.TaquitoError { constructor(value, token, baseMessage) { super(); this.value = value; this.token = token; this.name = 'TokenValidationError'; const annot = this.token.annot(); const annotText = annot ? `[${annot}] ` : ''; this.message = `${annotText}${baseMessage}`; } } exports.TokenValidationError = TokenValidationError; class Token { /** * @description Gets the strategy used for field numbering in Token execute/encode/decode to convert Michelson values to/from javascript objects, returns a value of type {@link FieldNumberingStrategy} that controls how field numbers are calculated */ static get fieldNumberingStrategy() { return Token._fieldNumberingStrategy; } /** * @description Sets the strategy used for field numbering in Token execute/encode/decode to convert Michelson values to/from javascript objects, accepts a value of type {@link FieldNumberingStrategy} that controls how field numbers are calculated */ static set fieldNumberingStrategy(value) { Token._fieldNumberingStrategy = value; } constructor(val, idx, fac, parentTokenType) { this.val = val; this.idx = idx; this.fac = fac; this.parentTokenType = parentTokenType; this.createToken = this.fac; } typeWithoutAnnotations() { const handleMichelsonExpression = (val) => { if (typeof val === 'object') { if (Array.isArray(val)) { const array = val; return array.map((item) => handleMichelsonExpression(item)); } const extended = val; if (extended.args) { return { prim: extended.prim, args: extended.args.map((x) => handleMichelsonExpression(x)), }; } else { return { prim: extended.prim, }; } } return val; }; const handleMichelsonExtended = (val) => { if (val.args) { return { prim: val.prim, args: val.args.map((x) => handleMichelsonExpression(x)), }; } else { return { prim: val.prim, }; } }; return handleMichelsonExtended(this.val); } annot() { return (Array.isArray(this.val.annots) && this.val.annots.length > 0 ? this.val.annots[0] : String(this.idx)).replace(/(%|:)(_Liq_entry_)?/, ''); } hasAnnotations() { return Array.isArray(this.val.annots) && this.val.annots.length; } get tokenVal() { return this.val; } ExtractSignature() { return [[this.ExtractSchema()]]; } } exports.Token = Token; Token._fieldNumberingStrategy = 'Latest'; class ComparableToken extends Token { compare(o1, o2) { if (o1 === o2) { return 0; } return o1 < o2 ? -1 : 1; } } exports.ComparableToken = ComparableToken;