@chainsafe/ssz-type-schema
Version:
Utility methods and types for describing an ssz object
99 lines (98 loc) • 3.09 kB
TypeScript
/// <reference types="node" />
import { BitList, BitVector } from "@chainsafe/bit-utils";
import BN from "bn.js";
export declare type Uint = number | bigint | BN;
export declare type Bool = boolean;
export declare type Bits = BitList | BitVector;
export declare type Bytes = Buffer | Uint8Array;
export interface SerializableArray extends Array<SerializableValue> {
}
export interface SerializableObject extends Record<string, SerializableValue> {
}
export declare type SerializableValue = Uint | Bool | Bits | Bytes | SerializableArray | SerializableObject;
export declare type SimplePrimitiveType<T = any> = string;
export interface SimpleListType<T = any> {
elementType: AnySSZType;
maxLength: number;
}
export interface SimpleVectorType<T = any> {
elementType: AnySSZType;
length: number;
}
export interface SimpleContainerType<T = any> {
fields: [string, AnySSZType][];
}
/**
* A convenient interface for specifying types
*
* In most cases, types are specified by users in simple form.
* They will be parsed to a [[FullSSZType]] before any processing.
*/
export declare type SimpleSSZType<T = any> = SimplePrimitiveType<T> | SimpleListType<T> | SimpleVectorType<T> | SimpleContainerType<T>;
export declare enum Type {
uint = 0,
bool = 1,
bitList = 2,
bitVector = 3,
byteList = 4,
byteVector = 5,
list = 6,
vector = 7,
container = 8
}
export declare enum UintImpl {
number = "number",
bigint = "bigint",
bn = "bn"
}
export interface UintType<T = any> {
type: Type.uint;
byteLength: number;
use: UintImpl[keyof UintImpl];
}
export interface BoolType<T = any> {
type: Type.bool;
}
export interface BitListType<T = any> {
type: Type.bitList;
maxLength: number;
}
export interface BitVectorType<T = any> {
type: Type.bitVector;
length: number;
}
export declare type BitsType<T = any> = BitListType<T> | BitVectorType<T>;
export interface ByteListType<T = any> {
type: Type.byteList;
maxLength: number;
}
export interface ByteVectorType<T = any> {
type: Type.byteVector;
length: number;
}
export declare type BytesType<T = any> = ByteListType<T> | ByteVectorType<T>;
export interface ListType<T = any> {
type: Type.list;
elementType: FullSSZType;
maxLength: number;
}
export interface VectorType<T = any> {
type: Type.vector;
elementType: FullSSZType;
length: number;
}
export declare type ArrayType<T = any> = ListType<T> | VectorType<T>;
export interface ContainerType<T = any> {
type: Type.container;
fields: [string, FullSSZType][];
}
/**
* A more consistent and verbose interface for specifying types
*
* Full types are used internally.
*/
export declare type FullSSZType<T = any> = UintType<T> | BoolType<T> | BitsType<T> | BytesType<T> | ArrayType<T> | ContainerType<T>;
export declare type AnyContainerType<T = any> = ContainerType<T> | SimpleContainerType<T>;
export declare type AnySSZType<T = any> = FullSSZType<T> | SimpleSSZType<T>;
export declare const bit: BoolType<boolean>;
export declare const byte: UintType<number>;