@taquito/michelson-encoder
Version:
converts michelson data and types into convenient JS/TS objects
86 lines (85 loc) • 2.42 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChainIDToken = exports.ChainIDValidationError = void 0;
const token_1 = require("./token");
const utils_1 = require("@taquito/utils");
/**
* @category Error
* @description Error that indicates a failure happening when parsing encoding/executing a ChainID
*/
class ChainIDValidationError extends token_1.TokenValidationError {
constructor(value, token, message) {
super(value, token, message);
this.value = value;
this.token = token;
this.name = 'ChainIDValidationError';
}
}
exports.ChainIDValidationError = ChainIDValidationError;
class ChainIDToken extends token_1.ComparableToken {
constructor(val, idx, fac) {
super(val, idx, fac);
this.val = val;
this.idx = idx;
this.fac = fac;
}
/**
* @throws {@link ChainIDValidationError}
*/
validate(value) {
if ((0, utils_1.validateChain)(value) !== utils_1.ValidationResult.VALID) {
throw new ChainIDValidationError(value, this, `Value ${JSON.stringify(value)} is not a valid ChainID`);
}
}
Execute(val) {
return val[Object.keys(val)[0]];
}
/**
* @deprecated ExtractSchema has been deprecated in favor of generateSchema
*
*/
ExtractSchema() {
return ChainIDToken.prim;
}
generateSchema() {
return {
__michelsonType: ChainIDToken.prim,
schema: ChainIDToken.prim,
};
}
/**
* @throws {@link ChainIDValidationError}
*/
Encode(args) {
const val = args.pop();
this.validate(val);
return { string: val };
}
/**
* @throws {@link ChainIDValidationError}
*/
EncodeObject(val, semantic) {
this.validate(val);
if (semantic && semantic[ChainIDToken.prim]) {
return semantic[ChainIDToken.prim](val);
}
return { string: val };
}
ToKey({ string }) {
return string;
}
ToBigMapKey(val) {
return {
key: { string: val },
type: { prim: ChainIDToken.prim },
};
}
findAndReturnTokens(tokenToFind, tokens) {
if (ChainIDToken.prim === tokenToFind) {
tokens.push(this);
}
return tokens;
}
}
exports.ChainIDToken = ChainIDToken;
ChainIDToken.prim = 'chain_id';