UNPKG

@taquito/michelson-encoder

Version:

Michelson encoding and decoding utilities for Taquito.

91 lines (90 loc) 2.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KeyHashToken = exports.KeyHashValidationError = void 0; const token_1 = require("../token"); const utils_1 = require("@taquito/utils"); /** * @category Error * Error that indicates a failure happening when parsing encoding/executing Key Hash */ class KeyHashValidationError extends token_1.TokenValidationError { constructor(value, token, message) { super(value, token, message); this.value = value; this.token = token; this.name = 'KeyHashValidationError'; } } exports.KeyHashValidationError = KeyHashValidationError; class KeyHashToken extends token_1.ComparableToken { constructor(val, idx, fac) { super(val, idx, fac); this.val = val; this.idx = idx; this.fac = fac; } Execute(val) { if (val.string) { return val.string; } return (0, utils_1.encodeKeyHash)(val.bytes); } /** * @throws {@link KeyHashValidationError} */ validate(value) { if ((0, utils_1.validateKeyHash)(value) !== utils_1.ValidationResult.VALID) { throw new KeyHashValidationError(value, this, `KeyHash is not valid: ${JSON.stringify(value)}`); } } /** * @throws {@link KeyHashValidationError} */ Encode(args) { const val = args.pop(); this.validate(val); return { string: val }; } /** * @throws {@link KeyHashValidationError} */ EncodeObject(val, semantic) { this.validate(val); if (semantic && semantic[KeyHashToken.prim]) { return semantic[KeyHashToken.prim](val); } return { string: val }; } generateSchema() { return { __michelsonType: KeyHashToken.prim, schema: KeyHashToken.prim, }; } ToKey({ string, bytes }) { if (string) { return string; } return (0, utils_1.encodeKeyHash)(bytes); } ToBigMapKey(val) { return { key: { string: val }, type: { prim: KeyHashToken.prim }, }; } compare(pkh1, pkh2) { // binary type tag actually reflects the expected prefix order const h1 = (0, utils_1.b58DecodePublicKeyHash)(pkh1, 'array'); const h2 = (0, utils_1.b58DecodePublicKeyHash)(pkh2, 'array'); return (0, utils_1.compareArrays)(h1, h2); } findAndReturnTokens(tokenToFind, tokens) { if (KeyHashToken.prim === tokenToFind) { tokens.push(this); } return tokens; } } exports.KeyHashToken = KeyHashToken; KeyHashToken.prim = 'key_hash';