UNPKG

@web3auth/ws-embed

Version:

Embed script

67 lines (66 loc) 3.06 kB
import { JRPCEngine, JRPCRequest, ProviderEvents, RequestArguments, SafeEventEmitter, SafeEventEmitterProvider, SendCallBack } from "@web3auth/auth"; import type { Duplex } from "readable-stream"; import { BaseProviderState, Maybe, ProviderOptions, UnValidatedJsonRpcRequest } from "./interfaces"; /** * @param connectionStream - A Node.js duplex stream * @param opts - An options bag */ declare abstract class BaseProvider<U extends BaseProviderState, E extends ProviderEvents = ProviderEvents> extends SafeEventEmitter<E> implements SafeEventEmitterProvider<E> { rpcEngine: JRPCEngine; jsonRpcConnectionEvents: SafeEventEmitter; /** * Indicating that this provider is a Web3Auth provider. */ readonly isWeb3Auth: true; protected state: U; constructor(connectionStream: Duplex, { maxEventListeners, jsonRpcStreamName }: ProviderOptions); /** * Submits an RPC request for the given method, with the given params. * Resolves with the result of the method call, or rejects on error. * * @param args - The RPC request arguments. * @returns A Promise that resolves with the result of the RPC method, * or rejects if an error is encountered. */ request<T, P>(args: RequestArguments<T>): Promise<Maybe<P>>; send<T, V>(req: JRPCRequest<T>, callback: SendCallBack<V>): void; sendAsync<T, V>(req: JRPCRequest<T>): Promise<V>; /** * Called when connection is lost to critical streams. * * emits InpageProvider#disconnect */ protected handleStreamDisconnect(streamName: string, error: Error): void; /** * Constructor helper. * Populates initial state by calling 'wallet_getProviderState' and emits * necessary events. */ abstract initializeState(...args: unknown[]): Promise<void>; /** * Internal RPC method. Forwards requests to background via the RPC engine. * Also remap ids inbound and outbound */ protected abstract rpcRequest(payload: UnValidatedJsonRpcRequest | UnValidatedJsonRpcRequest[], callback: (...args: unknown[]) => void, isInternal?: boolean): void; /** * When the provider becomes connected, updates internal state and emits * required events. Idempotent. * * @param chainId - The ID of the newly connected chain. * emits InPageProvider#connect */ protected abstract handleConnect(...args: unknown[]): void; /** * When the provider becomes disconnected, updates internal state and emits * required events. Idempotent with respect to the isRecoverable parameter. * * Error codes per the CloseEvent status codes as required by EIP-1193: * https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes * * @param isRecoverable - Whether the disconnection is recoverable. * @param errorMessage - A custom error message. * emits InpageProvider#disconnect */ protected abstract handleDisconnect(isRecoverable: boolean, errorMessage?: string): void; } export default BaseProvider;