@taquito/michelson-encoder
Version:
Michelson encoding and decoding utilities for Taquito.
88 lines (87 loc) • 2.69 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Bls12381frToken = exports.Bls12381frValidationError = void 0;
const utils_1 = require("@taquito/utils");
const token_1 = require("./token");
/**
* @category Error
* Error that indicates a failure happening when parsing encoding/executing a BLS12-381 scalar field Fr
*/
class Bls12381frValidationError extends token_1.TokenValidationError {
constructor(value, token, message) {
super(value, token, message);
this.value = value;
this.token = token;
this.name = 'Bls12381frValidationError';
}
}
exports.Bls12381frValidationError = Bls12381frValidationError;
class Bls12381frToken extends token_1.Token {
constructor(val, idx, fac) {
super(val, idx, fac);
this.val = val;
this.idx = idx;
this.fac = fac;
}
/**
* @throws {@link Bls12381frValidationError}
*/
validate(val) {
if (/^[0-9a-fA-F]*$/.test(val) && val.length % 2 === 0) {
return;
}
throw new Bls12381frValidationError(val, this, `Invalid bytes: ${JSON.stringify(val)}`);
}
convertUint8ArrayToHexString(val) {
return val.constructor === Uint8Array ? (0, utils_1.buf2hex)(val) : val;
}
/**
* @throws {@link Bls12381frValidationError}
*/
Encode(args) {
let val = args.pop();
if (typeof val === 'number') {
return { int: val.toString() };
}
else {
val = this.convertUint8ArrayToHexString(val);
this.validate(val);
return { bytes: val };
}
}
/**
* @throws {@link Bls12381frValidationError}
*/
EncodeObject(val, semantic) {
if (semantic && semantic[Bls12381frToken.prim]) {
return semantic[Bls12381frToken.prim](val);
}
if (typeof val === 'number') {
return { int: val.toString() };
}
else {
val = this.convertUint8ArrayToHexString(val);
this.validate(val);
return { bytes: val };
}
}
Execute(val) {
return val.bytes;
}
generateSchema() {
return {
__michelsonType: Bls12381frToken.prim,
schema: Bls12381frToken.prim,
};
}
findAndReturnTokens(tokenToFind, tokens) {
if (Bls12381frToken.prim === tokenToFind) {
tokens.push(this);
}
return tokens;
}
}
exports.Bls12381frToken = Bls12381frToken;
// An element of the BLS12-381 scalar field Fr
// see https://tezos.gitlab.io/michelson-reference/#type-bls12_381_fr
Bls12381frToken.prim = 'bls12_381_fr';