import type { GossipSub } from "@chainsafe/libp2p-gossipsub"; import type { Stream } from "@libp2p/interface-connection"; import type { PeerId } from "@libp2p/interface-peer-id"; import type { Peer } from "@libp2p/interface-peer-store"; import type { Multiaddr } from "@multiformats/multiaddr"; import type { Libp2p } from "libp2p"; export enum Protocols { Relay = "relay", Store = "store", LightPush = "lightpush", Filter = "filter", } export interface PointToPointProtocol { libp2p: Libp2p; peers: () => Promise; } export interface Index { digest?: Uint8Array; receivedTime?: bigint; senderTime?: bigint; pubsubTopic?: string; } export type ProtocolOptions = { pubSubTopic?: string; /** * Optionally specify an PeerId for the protocol request. If not included, will use a random peer. */ peerId?: PeerId; }; export type Callback = (msg: T) => void | Promise; export interface Filter extends PointToPointProtocol { subscribe: ( decoders: Decoder[], callback: Callback, opts?: ProtocolOptions ) => Promise<() => Promise>; } export interface LightPush extends PointToPointProtocol { push: ( encoder: Encoder, message: Message, opts?: ProtocolOptions ) => Promise; } export enum PageDirection { BACKWARD = "backward", FORWARD = "forward", } export interface TimeFilter { startTime: Date; endTime: Date; } export type StoreQueryOptions = { /** * The direction in which pages are retrieved: * - { @link PageDirection.BACKWARD }: Most recent page first. * - { @link PageDirection.FORWARD }: Oldest page first. * * Note: This does not affect the ordering of messages with the page * (the oldest message is always first). * * @default { @link PageDirection.BACKWARD } */ pageDirection?: PageDirection; /** * The number of message per page. */ pageSize?: number; /** * Retrieve messages with a timestamp within the provided values. */ timeFilter?: TimeFilter; /** * Cursor as an index to start a query from. */ cursor?: Index; } & ProtocolOptions; export interface Store extends PointToPointProtocol { queryOrderedCallback: ( decoders: Decoder[], callback: (message: T) => Promise | boolean | void, options?: StoreQueryOptions ) => Promise; queryCallbackOnPromise: ( decoders: Decoder[], callback: ( message: Promise ) => Promise | boolean | void, options?: StoreQueryOptions ) => Promise; queryGenerator: ( decoders: Decoder[], options?: StoreQueryOptions ) => AsyncGenerator[]>; } export interface Relay extends GossipSub { send: (encoder: Encoder, message: Message) => Promise; addObserver: ( decoder: Decoder, callback: Callback ) => () => void; getMeshPeers: () => string[]; } export interface Waku { libp2p: Libp2p; relay?: Relay; store?: Store; filter?: Filter; lightPush?: LightPush; dial(peer: PeerId | Multiaddr, protocols?: Protocols[]): Promise; addPeerToAddressBook( peerId: PeerId | string, multiaddrs: Multiaddr[] | string[] ): void; start(): Promise; stop(): Promise; isStarted(): boolean; } export interface WakuLight extends Waku { relay: undefined; store: Store; filter: Filter; lightPush: LightPush; } export interface WakuPrivacy extends Waku { relay: Relay; store: undefined; filter: undefined; lightPush: undefined; } export interface WakuFull extends Waku { relay: Relay; store: Store; filter: Filter; lightPush: LightPush; } export interface RateLimitProof { proof: Uint8Array; merkleRoot: Uint8Array; epoch: Uint8Array; shareX: Uint8Array; shareY: Uint8Array; nullifier: Uint8Array; rlnIdentifier: Uint8Array; } /** * Interface matching the protobuf library. * Field types matches the protobuf type over the wire */ export interface ProtoMessage { payload: Uint8Array | undefined; contentTopic: string | undefined; version: number | undefined; timestamp: bigint | undefined; rateLimitProof: RateLimitProof | undefined; } /** * Interface for messages to encode and send. */ export interface Message { payload?: Uint8Array; timestamp?: Date; rateLimitProof?: RateLimitProof; } export interface Encoder { contentTopic: string; toWire: (message: Message) => Promise; toProtoObj: (message: Message) => Promise; } export interface DecodedMessage { payload: Uint8Array | undefined; contentTopic: string | undefined; timestamp: Date | undefined; rateLimitProof: RateLimitProof | undefined; } export interface Decoder { contentTopic: string; fromWireToProtoObj: (bytes: Uint8Array) => Promise; fromProtoObj: (proto: ProtoMessage) => Promise; } export interface SendResult { recipients: PeerId[]; }