import type { Coder as BaseCoder } from '@scure/base'; /** * TODO: * - Holes, simplify pointers. Hole is some sized element which is skipped at encoding, * but later other elements can write to it by path * - Composite / tuple keys for dict * - Web UI for easier debugging. We can wrap every coder to something that would write * start & end positions to; and we can colorize specific bytes used by specific coder */ export declare const EMPTY: Uint8Array; export declare const NULL: Uint8Array; export declare function equalBytes(a: Uint8Array, b: Uint8Array): boolean; export declare function isBytes(a: unknown): a is Bytes; /** * Copies several Uint8Arrays into one. */ export declare function concatBytes(...arrays: Uint8Array[]): Uint8Array; export type Bytes = Uint8Array; export type Option = T | undefined; export interface Coder { encode(from: F): T; decode(to: T): F; } export interface BytesCoder extends Coder { size?: number; encode: (data: T) => Bytes; decode: (data: Bytes) => T; } export interface BytesCoderStream { size?: number; encodeStream: (w: Writer, value: T) => void; decodeStream: (r: Reader) => T; } export type CoderType = BytesCoderStream & BytesCoder; export type Sized = CoderType & { size: number; }; export type UnwrapCoder = T extends CoderType ? U : T; export type Length = CoderType | CoderType | number | Bytes | string | null; type ArrLike = Array | ReadonlyArray; export type TypedArray = Uint8Array | Int8Array | Uint8ClampedArray | Uint16Array | Int16Array | Uint32Array | Int32Array; export type Writable = T extends {} ? T extends TypedArray ? T : { -readonly [P in keyof T]: Writable; } : T; export type Values = T[keyof T]; export type NonUndefinedKey = T[K] extends undefined ? never : K; export type NullableKey = T[K] extends NonNullable ? never : K; export type OptKey = NullableKey & NonUndefinedKey; export type ReqKey = T[K] extends NonNullable ? K : never; export type OptKeys = Pick; }[keyof T]>; export type ReqKeys = Pick; }[keyof T]>; export type StructInput> = { [P in keyof ReqKeys]: T[P]; } & { [P in keyof OptKeys]?: T[P]; }; export type StructRecord> = { [P in keyof T]: CoderType; }; export type StructOut = Record; export type PadFn = (i: number) => number; export type ReaderOpts = { allowUnreadBytes?: boolean; allowMultipleReads?: boolean; }; export declare class Reader { readonly data: Bytes; readonly opts: ReaderOpts; path: StructOut[]; fieldPath: string[]; private parent; parentOffset: number; pos: number; bitBuf: number; bitPos: number; private bs; constructor(data: Bytes, opts?: ReaderOpts, path?: StructOut[], fieldPath?: string[], parent?: Reader | undefined, parentOffset?: number); enablePtr(): void; private markBytesBS; private markBytes; err(msg: string): Error; absBytes(n: number): Uint8Array; offsetReader(n: number): Reader; bytes(n: number, peek?: boolean): Uint8Array; byte(peek?: boolean): number; get leftBytes(): number; isEnd(): boolean; length(len: Length): number; bits(bits: number): number; find(needle: Bytes, pos?: number): number | undefined; finish(): void; fieldPathPush(s: string): void; fieldPathPop(): void; } export declare class Writer { path: StructOut[]; fieldPath: string[]; private buffers; pos: number; ptrs: { pos: number; ptr: CoderType; buffer: Bytes; }[]; bitBuf: number; bitPos: number; constructor(path?: StructOut[], fieldPath?: string[]); err(msg: string): Error; bytes(b: Bytes): void; byte(b: number): void; get buffer(): Bytes; length(len: Length, value: number): void; bits(value: number, bits: number): void; fieldPathPush(s: string): void; fieldPathPop(): void; } export declare function checkBounds(p: Writer | Reader, value: bigint, bits: bigint, signed: boolean): void; export declare function wrap(inner: BytesCoderStream): BytesCoderStream & BytesCoder; export declare function isCoder(elm: any): elm is CoderType; declare function dict(): BaseCoder<[string, T][], Record>; type Enum = { [k: string]: number | string; } & { [k: number]: string; }; type EnumKeys = keyof T; declare function tsEnum(e: T): BaseCoder>; declare function decimal(precision: number): { encode: (from: bigint) => string; decode: (to: string) => bigint; }; type BaseInput = F extends BaseCoder ? T : never; type BaseOutput = F extends BaseCoder ? T : never; /** * Allows to split big conditional coders into a small one; also sort of parser combinator: * * `encode = [Ae, Be]; decode = [Ad, Bd]` * -> * `match([{encode: Ae, decode: Ad}, {encode: Be; decode: Bd}])` * * 1. It is easier to reason: encode/decode of specific part are closer to each other * 2. Allows composable coders and ability to add conditions on runtime * @param lst * @returns */ declare function match[], I = { [K in keyof L]: NonNullable>; }[number], O = { [K in keyof L]: NonNullable>; }[number]>(lst: L): BaseCoder; export declare const coders: { dict: typeof dict; number: BaseCoder; tsEnum: typeof tsEnum; decimal: typeof decimal; match: typeof match; reverse: (coder: Coder) => Coder; }; export declare const bits: (len: number) => CoderType; export declare const bigint: (size: number, le?: boolean, signed?: boolean, sized?: boolean) => CoderType; export declare const U256LE: CoderType; export declare const U256BE: CoderType; export declare const I256LE: CoderType; export declare const I256BE: CoderType; export declare const U128LE: CoderType; export declare const U128BE: CoderType; export declare const I128LE: CoderType; export declare const I128BE: CoderType; export declare const U64LE: CoderType; export declare const U64BE: CoderType; export declare const I64LE: CoderType; export declare const I64BE: CoderType; export declare const int: (size: number, le?: boolean, signed?: boolean, sized?: boolean) => CoderType; export declare const U32LE: CoderType; export declare const U32BE: CoderType; export declare const I32LE: CoderType; export declare const I32BE: CoderType; export declare const U16LE: CoderType; export declare const U16BE: CoderType; export declare const I16LE: CoderType; export declare const I16BE: CoderType; export declare const U8: CoderType; export declare const I8: CoderType; export declare const bool: CoderType; export declare const bytes: (len: Length, le?: boolean) => CoderType; export declare const string: (len: Length, le?: boolean) => CoderType; export declare const cstring: CoderType; export declare const hex: (len: Length, le?: boolean, withZero?: boolean) => CoderType; export declare function apply(inner: CoderType, b: BaseCoder): CoderType; export declare function validate(inner: CoderType, fn: (elm: T) => T): CoderType; export declare function lazy(fn: () => CoderType): CoderType; type baseFmt = 'utf8' | 'hex' | 'base16' | 'base32' | 'base64' | 'base64url' | 'base58' | 'base58xmr'; export declare const bytesFormatted: (len: Length, fmt: baseFmt, le?: boolean) => BytesCoderStream & BytesCoder; export declare const flag: (flagValue: Bytes, xor?: boolean) => CoderType; export declare function flagged(path: string | BytesCoderStream, inner: BytesCoderStream, def?: T): CoderType>; export declare function optional(flag: BytesCoderStream, inner: BytesCoderStream, def?: T): CoderType>; export declare function magic(inner: CoderType, constant: T, check?: boolean): CoderType; export declare const magicBytes: (constant: Bytes | string) => CoderType; export declare function constant(c: T): CoderType; export declare function struct>(fields: StructRecord): CoderType>; export declare function tuple>, O = Writable<{ [K in keyof T]: UnwrapCoder; }>>(fields: T): CoderType; type PrefixLength = string | number | CoderType | CoderType; export declare function prefix(len: PrefixLength, inner: CoderType): CoderType; export declare function array(len: Length, inner: CoderType): CoderType; export declare function map(inner: CoderType, variants: Record): CoderType; export declare function tag; }; }>, TagValue extends string | number, Variants extends Record>>(tag: CoderType, variants: Variants): CoderType; export declare function mappedTag; }; }>, TagValue extends string | number, Variants extends Record]>>(tagCoder: CoderType, variants: Variants): CoderType; export declare function bitset(names: Names, pad?: boolean): CoderType>; export declare const ZeroPad: PadFn; export declare function padLeft(blockSize: number, inner: CoderType, padFn: Option): CoderType; export declare function padRight(blockSize: number, inner: CoderType, padFn: Option): CoderType; export declare function pointer(ptr: CoderType, inner: CoderType, sized?: boolean): CoderType; export declare function base64armor(name: string, lineLen: number, inner: Coder, checksum?: (data: Bytes) => Bytes): Coder; export declare const nothing: CoderType; export declare function debug(inner: CoderType): CoderType; export declare const _TEST: { _bitset: { BITS: number; FULL_MASK: number; len: (len: number) => number; create: (len: number) => Uint32Array; clean: (bs: Uint32Array) => Uint32Array; debug: (bs: Uint32Array) => string[]; checkLen: (bs: Uint32Array, len: number) => void; chunkLen: (bsLen: number, pos: number, len: number) => void; set: (bs: Uint32Array, chunk: number, value: number, allowRewrite?: boolean) => boolean; pos: (pos: number, i: number) => { chunk: number; mask: number; }; indices: (bs: Uint32Array, len: number, invert?: boolean) => number[]; range: (arr: number[]) => { pos: number; length: number; }[]; rangeDebug: (bs: Uint32Array, len: number, invert?: boolean) => string; setRange: (bs: Uint32Array, bsLen: number, pos: number, len: number, allowRewrite?: boolean) => boolean; }; }; export {}; //# sourceMappingURL=index.d.ts.map