@nori-zk/proof-conversion
Version:
Verifying zkVM proofs inside o1js circuits, to generate Mina compatible proof
109 lines (108 loc) • 4.31 kB
TypeScript
import { type ValidatorFn } from './core.js';
import type { AccumulatorRange } from './types.js';
/**
* Recursively builds a fixed-length tuple type.
*
* @example
* Tuple<string, 3> = [string, string, string]
*/
type Tuple<T, N extends number, Acc extends T[] = []> = Acc['length'] extends N ? Acc : Tuple<T, N, [...Acc, T]>;
/**
* Helper type to create tuple unions for constrained arrays.
*
* @example
* TupleUnion<string, 1, 3> = [string] | [string, string] | [string, string, string]
*/
type TupleUnion<T, Min extends number, Max extends number> = AccumulatorRange<Min, Max, T>;
/**
* Creates a variable-length array validator.
* NOTE: The guard function checks only that it's an array, NOT the elements.
* Element validation is handled by the diagnose function.
*
* @example
* const isStringArray = isArray(isString);
* // Returns: (val: unknown) => val is string[]
*/
export declare const isArray: <T>(inner: ValidatorFn<T>) => ValidatorFn<T[]>;
/**
* Constraint metadata for fixed-length array validation.
*/
export interface FixedLengthConstraint {
/** The exact required length of the array */
length: number;
}
/**
* Creates a fixed-length array validator that returns a tuple type.
* NOTE: The guard function checks only array type and length, NOT the elements.
*
* @example
* const isStringTriple = isArrayOfLength(isString, 3);
* // Returns: (val: unknown) => val is [string, string, string]
*/
export declare const isArrayOfLength: <T, N extends number>(inner: ValidatorFn<T>, len: N) => ValidatorFn<Tuple<T, N>>;
/**
* Constraint metadata for bounded-length array validation.
* At least one of minLength or maxLength should be specified for meaningful bounds.
*/
export interface ArrayConstraints {
/** Minimum allowed array length (inclusive) */
minLength?: number;
/** Maximum allowed array length (inclusive) */
maxLength?: number;
}
/**
* Creates an array validator with length constraints and union of tuple types.
* NOTE: The guard function checks only array type and length, NOT the elements.
*
* The return type is a union of all valid tuple lengths from minLength to maxLength.
*
* @example
* const isSmallStringArray = isArrayOfBoundedLength(isString, { minLength: 1, maxLength: 3 });
* // Returns: (val: unknown) => val is [string] | [string, string] | [string, string, string]
*/
export declare const isArrayOfBoundedLength: <T, Min extends number, Max extends number>(inner: ValidatorFn<T>, options: {
minLength: Min;
maxLength?: Max;
} | {
minLength?: Min;
maxLength: Max;
}) => ValidatorFn<TupleUnion<T, Min, Max>>;
/**
* Type guard validator for variable-length arrays of numbers.
* Returns a ValidatorFn that narrows the type to `number[]` on success.
*
* @returns ValidatorFn<number[]> - Validator function with type predicate
*/
export declare const isNumberArray: ValidatorFn<number[]>;
/**
* Type guard validator for variable-length arrays of strings.
* Returns a ValidatorFn that narrows the type to `string[]` on success.
*
* @returns ValidatorFn<string[]> - Validator function with type predicate
*/
export declare const isStringArray: ValidatorFn<string[]>;
/**
* Factory function that creates a type guard validator for fixed-length string arrays.
* Returns a ValidatorFn that narrows the type to a tuple of strings with exact length.
*
* @param len - The exact required length
* @returns ValidatorFn<[string, string, ...]> - Validator function with tuple type predicate
*
* @example
* const isTriplet = isStringArrayOfLength(3);
* // Returns: (val: unknown) => val is [string, string, string]
*/
export declare const isStringArrayOfLength: <N extends number>(len: N) => ValidatorFn<Tuple<string, N, []>>;
/**
* Factory function that creates a type guard validator for fixed-length uint8 arrays.
* Returns a ValidatorFn that narrows the type to a tuple of numbers (0-255) with exact length.
*
* @param len - The exact required length
* @returns ValidatorFn<[number, number, ...]> - Validator function with tuple type predicate
*
* @example
* const isRGBTriple = isUint8Array(3);
* // Returns: (val: unknown) => val is [number, number, number]
*/
export declare const isUint8Array: <N extends number>(len: N) => ValidatorFn<Tuple<number, N, []>>;
export {};