@taquito/michelson-encoder
Version:
Michelson encoding and decoding utilities for Taquito.
117 lines (116 loc) • 3.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddressToken = exports.AddressValidationError = 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 an Address
*/
class AddressValidationError extends token_1.TokenValidationError {
constructor(value, token, message) {
super(value, token, message);
this.value = value;
this.token = token;
this.name = 'AddressValidationError';
}
}
exports.AddressValidationError = AddressValidationError;
class AddressToken extends token_1.ComparableToken {
constructor(val, idx, fac) {
super(val, idx, fac);
this.val = val;
this.idx = idx;
this.fac = fac;
}
ToBigMapKey(val) {
const decoded = (0, utils_1.b58DecodeAddress)(val);
return {
key: { bytes: decoded },
type: { prim: 'bytes' },
};
}
/**
* @throws {@link AddressValidationError}
*/
validate(value) {
if (typeof value !== 'string') {
throw new AddressValidationError(value, this, 'Type error');
}
if ((0, utils_1.validateAddress)(value) !== utils_1.ValidationResult.VALID) {
throw new AddressValidationError(value, this, `Address is not valid: ${JSON.stringify(value)}`);
}
}
/**
* @throws {@link AddressValidationError}
*/
Encode(args) {
const val = args.pop();
this.validate(val);
return { string: val };
}
/**
* @throws {@link AddressValidationError}
*/
EncodeObject(val, semantic) {
this.validate(val);
if (semantic && semantic[AddressToken.prim]) {
return semantic[AddressToken.prim](val);
}
return { string: val };
}
/**
* @throws {@link AddressValidationError}
*/
Execute(val) {
if (val.string) {
return val.string;
}
if (!val.bytes) {
throw new AddressValidationError(val, this, `cannot be missing both string and bytes: ${JSON.stringify(val)}`);
}
return (0, utils_1.encodeAddress)(val.bytes);
}
generateSchema() {
return {
__michelsonType: AddressToken.prim,
schema: AddressToken.prim,
};
}
/**
* @throws {@link AddressValidationError}
*/
ToKey({ bytes, string }) {
if (string) {
return string;
}
if (!bytes) {
throw new AddressValidationError({ bytes, string }, this, `cannot be missing both string and bytes ${JSON.stringify({ string, bytes })}`);
}
return (0, utils_1.encodeAddress)(bytes);
}
compare(address1, address2) {
const [addr1, endpoint1] = (0, utils_1.splitAddress)(address1);
const [addr2, endpoint2] = (0, utils_1.splitAddress)(address2);
const ep1 = endpoint1 || '';
const ep2 = endpoint2 || '';
// binary type tag actually reflects the expected prefix order
const bytes1 = (0, utils_1.b58DecodeAddress)(addr1, 'array');
const bytes2 = (0, utils_1.b58DecodeAddress)(addr2, 'array');
const res = (0, utils_1.compareArrays)(bytes1, bytes2);
if (res === 0) {
return ep1 < ep2 ? -1 : ep1 > ep2 ? 1 : 0;
}
else {
return res;
}
}
findAndReturnTokens(tokenToFind, tokens) {
if (AddressToken.prim === tokenToFind) {
tokens.push(this);
}
return tokens;
}
}
exports.AddressToken = AddressToken;
AddressToken.prim = 'address';