@taquito/michelson-encoder
Version:
Michelson encoding and decoding utilities for Taquito.
98 lines (97 loc) • 2.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeyToken = exports.KeyValidationError = void 0;
const token_1 = require("./token");
const utils_1 = require("@taquito/utils");
const signer_1 = require("@taquito/signer");
/**
* @category Error
* Error that indicates a failure happening when parsing encoding/executing a Key
*/
class KeyValidationError extends token_1.TokenValidationError {
constructor(value, token, message) {
super(value, token, message);
this.value = value;
this.token = token;
this.name = 'KeyValidationError';
}
}
exports.KeyValidationError = KeyValidationError;
class KeyToken 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.encodeKey)(val.bytes);
}
/**
* @throws {@link KeyValidationError}
*/
validate(value) {
if ((0, utils_1.validatePublicKey)(value) !== utils_1.ValidationResult.VALID) {
throw new KeyValidationError(value, this, 'Key is not valid');
}
}
/**
* @throws {@link KeyValidationError}
*/
Encode(args) {
const val = args.pop();
this.validate(val);
return { string: val };
}
/**
* @throws {@link KeyValidationError}
*/
EncodeObject(val, semantic) {
this.validate(val);
if (semantic && semantic[KeyToken.prim]) {
return semantic[KeyToken.prim](val);
}
return { string: val };
}
generateSchema() {
return {
__michelsonType: KeyToken.prim,
schema: KeyToken.prim,
};
}
ToKey(val) {
return this.Execute(val);
}
ToBigMapKey(val) {
return {
key: { string: val },
type: { prim: KeyToken.prim },
};
}
compare(key1, key2) {
const publicKey1 = (0, signer_1.publicKeyFromString)(key1);
const publicKey2 = (0, signer_1.publicKeyFromString)(key2);
const bytes1 = publicKey1.toProtocol();
const bytes2 = publicKey2.toProtocol();
if (bytes1[0] === bytes2[0]) {
return publicKey1.compare(publicKey2);
}
else if (bytes1[0] > bytes2[0]) {
return 1;
}
else {
return -1;
}
}
findAndReturnTokens(tokenToFind, tokens) {
if (KeyToken.prim === tokenToFind) {
tokens.push(this);
}
return tokens;
}
}
exports.KeyToken = KeyToken;
KeyToken.prim = 'key';