import { BytearrayWrapper } from './bytearray-wrapper'; /** * A function that encodes the input to field element bytes */ export type EncodeFunc = (value: unknown) => Uint8Array; /** * A function that encodes the input to a positive integer */ export type ToPositiveIntFunc = (value: unknown) => number; /** * A class extending `BytearrayWrapper` containing instruments for dealing with message encoding/decoding. */ export declare abstract class MessageEncoder extends BytearrayWrapper { /** The field element size is 32 bytes so the maximum byte size of encoded message must be 32. */ static readonly maxEncodedLength = 32; static readonly textEncoder: TextEncoder; static readonly textDecoder: TextDecoder; /** * This is an irreversible encoding as a hash function is used to convert a message of * arbitrary length to a fixed length encoding. * @param message */ static encodeMessageForSigning(message: Uint8Array): Uint8Array; /** * This is an irreversible encoding as a hash function is used to convert a message of * arbitrary length to a fixed length encoding. * @param message */ static encodeMessageForSigningConstantTime(message: Uint8Array): Uint8Array; /** * Encodes a positive safe integer, i.e. of 53 bits * @param num */ static encodePositiveNumberForSigning(num: number): Uint8Array; /** * Encode the given string to bytes and create a field element by considering the bytes in little-endian format. * Use this way of encoding only if the input string's UTF-8 representation is <= 32 bytes else this will throw an error. * Also adds trailing 0s to the bytes to make the size 32 bytes so use this function carefully. The only place this is * currently useful is verifiable encryption as in some cases the prover might not be willing/available at the time of * decryption and thus the decryptor must be able to decrypt it independently. This is different from selective disclosure * where the verifier can check that the revealed message is same as the encoded one before even verifying the proof. * @param message - utf-8 string of at most 32 bytes * @param compress - whether to compress the text before encoding to bytes. Compression might not always help as things * like public keys, DIDs, UUIDs, etc. are designed to be random and thus won't be compressed */ static reversibleEncodeStringForSigning(message: string, compress?: boolean): Uint8Array; /** * Decode the given representation. This should **only** be used when the encoding was done * using `this.reversibleEncodeStringMessageForSigning`. Also, this function trims any characters from the first * occurrence of a null characters (UTF-16 code unit 0) so if the encoded (using `this.reversibleEncodeStringMessageForSigning`) * string also had a null then the decoded string will be different from it. * @param message * @param decompress - whether to decompress the bytes before converting to a string */ static reversibleDecodeStringForSigning(message: Uint8Array, decompress?: boolean): string; } /** * Encodes the input to a field element for signing. * Used when working with messages that are specified as JS objects. This encoder object will contain * the mapping from message name (key in JS object) to an encoding function. * * TODO: Support identity encoder for values that are already field elements. */ export declare class Encoder { encoders?: Map; defaultEncoder?: EncodeFunc; constructor(encoders?: Map, defaultEncoder?: EncodeFunc); /** * Encode a message with given name and value. Will throw an error if no appropriate encoder found. * @param name * @param value * @param strict - If set to false and no appropriate encoder is found but the value is a bytearray, it will encode it using the built-in mechanism * @param encodingFunc */ private _encodeMessage; /** * Encode messages given as JS object. It flattens the object into a sorted list and encodes each value as per the known * encoding functions. * Returns 2 arrays, 1st with message names and 2nd with encoded values. * @param encodingFunc * @param messages * @param strict - If set to false and no appropriate encoder is found but the value is a bytearray, it will encode it using the built-in mechanism */ private _encodeMessageObject; /** * Encode messages given as JS object. It flattens the object into a sorted list and encodes each value as per the known * encoding functions. * Returns an object with names as keys and encoded messages as values. * @param encodingFunc * @param messages * @param strict - If set to false and no appropriate encoder is found but the value is a bytearray, it will encode it using the built-in mechanism */ private _encodeMessageObjectAsObject; /** * Encode messages given as JS object. It flattens the object into a sorted list and encodes each value as per the known * encoding functions. * Returns a Map with names as keys and encoded messages as values. * @param encodingFunc * @param messages * @param strict - If set to false and no appropriate encoder is found but the value is a bytearray, it will encode it using the built-in mechanism */ private _encodeMessageObjectAsMap; private _encodeDefault; /** * Encode a message with given name and value. Will throw an error if no appropriate encoder found. * @param name * @param value * @param strict - If set to false and no appropriate encoder is found but the value is a bytearray, it will encode it using the built-in mechanism */ encodeMessage(name: string, value: unknown, strict?: boolean): Uint8Array; /** * Encode a message with given name and value. Will throw an error if no appropriate encoder found. * @param name * @param value * @param strict - If set to false and no appropriate encoder is found but the value is a bytearray, it will encode it using the built-in mechanism */ encodeMessageConstantTime(name: string, value: unknown, strict?: boolean): Uint8Array; /** * Encode messages given as JS object. It flattens the object into a sorted list and encodes each value as per the known * encoding functions. * Returns 2 arrays, 1st with message names and 2nd with encoded values. * @param messages * @param strict - If set to false and no appropriate encoder is found but the value is a bytearray, it will encode it using the built-in mechanism */ encodeMessageObject(messages: object, strict?: boolean): [string[], Uint8Array[]]; /** * Encode messages given as JS object. It flattens the object into a sorted list and encodes each value as per the known * encoding functions. * Returns 2 arrays, 1st with message names and 2nd with encoded values. * @param messages * @param strict - If set to false and no appropriate encoder is found but the value is a bytearray, it will encode it using the built-in mechanism */ encodeMessageObjectConstantTime(messages: object, strict?: boolean): [string[], Uint8Array[]]; /** * Encode messages given as JS object. It flattens the object into a sorted list and encodes each value as per the known * encoding functions. * Returns an object with names as keys and encoded messages as values. * @param messages * @param strict - If set to false and no appropriate encoder is found but the value is a bytearray, it will encode it using the built-in mechanism */ encodeMessageObjectAsObject(messages: object, strict?: boolean): { [name: string]: Uint8Array; }; /** * Encode messages given as JS object. It flattens the object into a sorted list and encodes each value as per the known * encoding functions. * Returns an object with names as keys and encoded messages as values. * @param messages * @param strict - If set to false and no appropriate encoder is found but the value is a bytearray, it will encode it using the built-in mechanism */ encodeMessageObjectAsObjectConstantTime(messages: object, strict?: boolean): { [name: string]: Uint8Array; }; /** * Encode messages given as JS object. It flattens the object into a sorted list and encodes each value as per the known * encoding functions. * Returns a Map with names as keys and encoded messages as values. * @param messages * @param strict - If set to false and no appropriate encoder is found but the value is a bytearray, it will encode it using the built-in mechanism */ encodeMessageObjectAsMap(messages: object, strict?: boolean): Map; /** * Encode messages given as JS object. It flattens the object into a sorted list and encodes each value as per the known * encoding functions. * Returns a Map with names as keys and encoded messages as values. * @param messages * @param strict - If set to false and no appropriate encoder is found but the value is a bytearray, it will encode it using the built-in mechanism */ encodeMessageObjectAsMapConstantTime(messages: object, strict?: boolean): Map; encodeDefault(value: unknown, strict?: boolean): Uint8Array; encodeDefaultConstantTime(value: unknown, strict?: boolean): Uint8Array; /** * Returns an encoding function to be used on a message that is a positive integer. */ static positiveIntegerEncoder(): EncodeFunc; /** * Returns an encoding function to be used on a message that is a boolean, encoded as positive int (0 or 1) */ static booleanEncoder(): EncodeFunc; /** * Returns an encoding function to be used on a message that is a date */ static dateEncoder(minimum: number): EncodeFunc; /** * Returns a function that can convert any input integer to a positive integer when its minimum * negative value is known. Does that by adding an offset of abs(minimum) to the input * @param minimum */ static integerToPositiveInt(minimum: number): ToPositiveIntFunc; /** * Returns an encoding function to be used on a message that can be a positive or negative integer. * @param minimum - The minimum negative value that the message can take */ static integerEncoder(minimum: number): EncodeFunc; /** * Returns a function that can convert any positive number to a positive integer when its maximum decimal * places are known. Does that by multiplying it by 10^max_decimal_places, eg. 23.452 -> 23452 * @param maxDecimalPlaces */ static positiveDecimalNumberToPositiveInt(maxDecimalPlaces: number): ToPositiveIntFunc; /** * Returns an encoding function to be used on a message that can be a positive decimal number, eg. 2.7 * @param maxDecimalPlaces - The maximum decimal places */ static positiveDecimalNumberEncoder(maxDecimalPlaces: number): EncodeFunc; /** * Returns a reversible encoding function to be used on a string message. The output can of the `EncodeFunc` can be * reversed. * @param compress */ static reversibleEncoderString(compress?: boolean): EncodeFunc; /** * Returns a function that can convert any number to a positive integer when its minimum negative value and maximum * decimal places are known. Does that by adding an offset of abs(minimum) and then multiplying it by 10^max_decimal_places * @param minimum * @param maxDecimalPlaces */ static decimalNumberToPositiveInt(minimum: number, maxDecimalPlaces: number): ToPositiveIntFunc; /** * Returns an encoding function to be used on a message that can be a positive, negative or decimal number, eg. -2.35 * @param minimum - The minimum negative value that the message can take * @param maxDecimalPlaces - The maximum decimal places */ static decimalNumberEncoder(minimum: number, maxDecimalPlaces: number): EncodeFunc; /** * Returns an encoding function to convert utf-8 string message. It might fail if the encoding target cannot be made a string */ static defaultEncodeFunc(): EncodeFunc; /** * Returns an encoding function to convert utf-8 string message. It might fail if the encoding target cannot be made a string */ static defaultEncodeFuncConstantTime(): EncodeFunc; private static ensureNumber; private static ensureCorrectDecimalNumberPlaces; } //# sourceMappingURL=encoder.d.ts.map