@taquito/michelson-encoder
Version:
converts michelson data and types into convenient JS/TS objects
36 lines (35 loc) • 1.41 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.InvalidTokenError = void 0;
exports.createToken = createToken;
const tokens_1 = require("./tokens");
const pair_1 = require("./pair");
const core_1 = require("@taquito/core");
/**
* @category Error
* @description Error that indicates a script having an invalid type or it being unsupported by the Michelson Encoder. Note some protocol changes might affect this, we encourage users to open an issue so we can look into implementing support for said types.
*/
class InvalidTokenError extends core_1.TaquitoError {
constructor(message, data) {
super(message);
this.message = message;
this.data = data;
this.name = 'Invalid token error';
}
}
exports.InvalidTokenError = InvalidTokenError;
/**
*
* @description Create a token from a value
* @throws {@link InvalidTokenError} If the value passed is not supported by the Michelson Encoder
*/
function createToken(val, idx, parentTokenType) {
if (Array.isArray(val)) {
return new pair_1.PairToken(val, idx, createToken, parentTokenType);
}
const t = tokens_1.tokens.find((x) => x.prim === val.prim);
if (!t) {
throw new InvalidTokenError(`Malformed data: ${JSON.stringify(val)}. Expected a value with a valid prim property`, val);
}
return new t(val, idx, createToken, parentTokenType);
}
;