@taquito/michelson-encoder
Version:
converts michelson data and types into convenient JS/TS objects
82 lines (81 loc) • 2.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChestKeyToken = exports.ChestKeyValidationError = void 0;
const token_1 = require("./token");
/**
* @category Error
* @description Error that indicates a failure happening when parsing encoding/executing a Chest Key
*/
class ChestKeyValidationError extends token_1.TokenValidationError {
constructor(value, token, message) {
super(value, token, message);
this.value = value;
this.token = token;
this.name = 'ChestKeyValidationError';
}
}
exports.ChestKeyValidationError = ChestKeyValidationError;
class ChestKeyToken extends token_1.Token {
constructor(val, idx, fac) {
super(val, idx, fac);
this.val = val;
this.idx = idx;
this.fac = fac;
}
/**
* @throws {@link ChestKeyValidationError}
*/
validate(val) {
if (/^[0-9a-fA-F]*$/.test(val) && val.length % 2 === 0) {
return;
}
throw new ChestKeyValidationError(val, this, `Invalid bytes: ${JSON.stringify(val)}`);
}
convertUint8ArrayToHexString(val) {
return val.constructor === Uint8Array ? Buffer.from(val).toString('hex') : val;
}
/**
* @throws {@link ChestKeyValidationError}
*/
Encode(args) {
let val = args.pop();
val = this.convertUint8ArrayToHexString(val);
this.validate(val);
return { bytes: val };
}
/**
* @throws {@link ChestKeyValidationError}
*/
EncodeObject(val, semantic) {
val = this.convertUint8ArrayToHexString(val);
this.validate(val);
if (semantic && semantic[ChestKeyToken.prim]) {
return semantic[ChestKeyToken.prim](val);
}
return { bytes: val };
}
Execute(val) {
return val.bytes;
}
/**
* @deprecated ExtractSchema has been deprecated in favor of generateSchema
*
*/
ExtractSchema() {
return ChestKeyToken.prim;
}
generateSchema() {
return {
__michelsonType: ChestKeyToken.prim,
schema: ChestKeyToken.prim,
};
}
findAndReturnTokens(tokenToFind, tokens) {
if (ChestKeyToken.prim === tokenToFind) {
tokens.push(this);
}
return tokens;
}
}
exports.ChestKeyToken = ChestKeyToken;
ChestKeyToken.prim = 'chest_key';