raiden-ts
Version:
Raiden Light Client Typescript/Javascript SDK
183 lines (182 loc) • 7.84 kB
TypeScript
import type { BigNumberish } from '@ethersproject/bignumber';
import { BigNumber } from '@ethersproject/bignumber';
import * as t from 'io-ts';
/**
* Decode/validate like codec.decode, but throw or return right instead of Either
*
* @param codec - io-ts codec to be used for decoding/validation
* @param data - data to decode/validate
* @param customError - Message or error to throw if the decoding fails
* @param log - Logger to log error to
* @returns Decoded value of codec type
*/
export declare function decode<C extends t.Mixed>(codec: C, data: C['_I'], customError?: string | Error, log?: (...args: any[]) => void): C['_A'];
/**
* Test for value's non-nulliness
* Like lodash's negate(isNil), but also works as type guard (e.g. useful for filters)
*
* @param value - to be tested
* @returns true if value is not null nor undefined
*/
export declare function isntNil<T>(value: T): value is NonNullable<T>;
/**
* Codec of ethers.utils.BigNumber objects
*
* Input can be anything BigNumber.from-able: number, string or jsonStringified BigNumber object
* Output is string, so we can JSON-serialize with 'number's types bigger than JS VM limits
* of ±2^53, as Raiden python client stdlib json encode longs as string.
*/
export interface BigNumberC extends t.Type<BigNumber, string> {
}
export declare const BigNumberC: BigNumberC;
export interface SizedB<S extends number> {
readonly size: S;
}
export interface HexStringB<S extends number> extends SizedB<S> {
readonly HexString: unique symbol;
}
export declare type HexString<S extends number = number> = string & t.Brand<HexStringB<S>>;
export interface HexStringC<S extends number> extends t.RefinementType<typeof t.string, HexString<S>, string> {
}
/**
* Helper function to create codecs to validate an arbitrary or variable-sized hex bytestring
* A branded codec to indicate validated hex-strings
*
* @param size - Required number of bytes. Pass undefined or zero to have a variable-sized type
* @returns branded codec for hex-encoded bytestrings
*/
export declare const HexString: <S extends number = number>(size?: S) => HexStringC<S>;
export interface IntB<S extends number> extends SizedB<S> {
readonly Int: unique symbol;
}
export declare type Int<S extends number = number> = BigNumber & t.Brand<IntB<S>>;
export interface IntC<S extends number> extends t.RefinementType<BigNumberC, Int<S>, t.OutputOf<BigNumberC>> {
}
/**
* Helper function to create codecs to validate an arbitrary or variable-sized BigNumbers
* A branded codec/type to indicate size-validated BigNumbers
*
* @param size - Required number of bytes. Pass undefined to have a variable-sized type
* @returns branded codec for hex-encoded bytestrings
*/
export declare const Int: <S extends number = number>(size?: S) => IntC<S>;
export interface UIntB<S extends number> extends SizedB<S> {
readonly UInt: unique symbol;
}
export declare type UInt<S extends number = number> = BigNumber & t.Brand<UIntB<S>>;
export interface UIntC<S extends number = number> extends t.RefinementType<BigNumberC, UInt<S>, t.OutputOf<BigNumberC>> {
}
/**
* Helper function to create codecs to validate an arbitrary or variable-sized BigNumbers
* A branded codec/type to indicate size-validated BigNumbers
*
* @param size - Required number of bytes. Pass undefined to have a variable-sized type
* @returns branded codec for hex-encoded bytestrings
*/
export declare const UInt: <S extends number = number>(size?: S) => UIntC<S>;
export declare const Signature: HexStringC<65>;
export declare type Signature = HexString<65>;
export declare const Hash: HexStringC<32>;
export declare type Hash = HexString<32>;
export declare const Secret: HexStringC<32>;
export declare type Secret = HexString<32>;
export declare const PrivateKey: HexStringC<32>;
export declare type PrivateKey = HexString<32>;
export declare const PublicKey: HexStringC<65>;
export declare type PublicKey = HexString<65>;
export interface AddressB {
readonly Address: unique symbol;
}
export declare type Address = HexString<20> & t.Brand<AddressB>;
export interface AddressC extends t.RefinementType<HexStringC<20>, Address, string> {
}
export declare const Address: AddressC;
/**
* Helper type to extend a given type T to contain a timestamp ts member
*/
export declare type Timed<T> = T & {
readonly ts: number;
};
export interface TimedC<T extends t.Mixed> extends t.IntersectionC<[T, t.ReadonlyC<t.TypeC<{
ts: t.NumberC;
}>>]> {
}
/**
* Helper function to create codecs to validate derived types containing a timestamp ts
*
* @param codec - Codec to compose with a ts timestamp property
* @returns Codec validating such subtype
*/
export declare const Timed: <T extends t.Mixed>(codec: T) => TimedC<T>;
/**
* Given a value of type T, returns a Timed<T> with current time as 'ts' member
*
* @param v - Value to return with time
* @param ts - Timestamp to use, defaults to now
* @returns copy of v added of a ts numeric timestamp
*/
export declare function timed<T>(v: T, ts?: number): Timed<T>;
/**
* Remove ts timestamp field (from timed) from object passed as parameter (immutably)
*
* @param v - Timed object
* @returns return a copy of v without ts property
*/
export declare function untime<T extends {
readonly ts: number;
}>(v: T): Omit<T, 'ts'>;
export declare type Signed<M> = M & {
readonly signature: Signature;
};
export interface SignedC<C extends t.Mixed> extends t.IntersectionC<[C, t.ReadonlyC<t.TypeC<{
signature: typeof Signature;
}>>]> {
}
export declare const Signed: <C extends t.Mixed>(codec: C) => SignedC<C>;
/**
* Memoized factory to create codecs validating an arbitrary class C
*
* @param name - Class to create a codec for
* @returns Codec validating class C
*/
export declare const instanceOf: <C>(name: string) => t.Type<C>;
/**
* Infer type of last element of a tuple or array
* Currently supports tuples of up to 9 elements before falling back to array's inference
*/
export declare type Last<T extends readonly unknown[]> = T extends readonly [...unknown[], infer L] ? L : T[number] | undefined;
/**
* Like lodash's last, but properly infer return type when argument is a tuple
*
* @param arr - Tuple or array to get last element from
* @returns Last element from arr
*/
export declare function last<T extends readonly unknown[]>(arr: T): Last<T>;
/**
* Math.max for BigNumbers
*
* @param args - Parameters to compare, must have at least one element
* @returns Maxium of parameters as per BigNumber's lt comparison
*/
export declare function bnMax<T extends BigNumber>(...args: [T, ...T[]]): T;
/**
* Type helper to recursively map decodable properties to their simpler encoded types;
* This allows e.g. types decodable as BigNumbers to be passed in [recursive] properties where
* BigNumbers are expected at runtime, as long as the object is decoded/validated before use.
*/
export declare type Decodable<T> = T extends BigNumber ? BigNumberish : T extends string ? string : T extends boolean ? boolean : T extends number ? number : T extends null | symbol ? T : unknown extends T ? unknown : {
[K in keyof T]: Decodable<T[K]>;
};
/**
* Converts a union to the respective intersection
* Example: type UnionToIntersection<{ a: string } | { b: number }> = { a: string } & { b: number }
*/
export declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
/**
* Creates a refinement of t.string which validates a template literal string
*
* @param regex - Regex which matches the generic parameter L
* @param name - codec name
* @returns refinement type of string to tempalte literal
*/
export declare function templateLiteral<L extends string>(regex: RegExp | string, name?: string): t.RefinementType<t.StringC, L, string, unknown>;