import { EthChainDefinition, EthEvent, EthEventParams, EthMethod, EthMethodParams, EthRequest, EthResult, } from 'eth-provider-types'; import { JsonRpcRequest, PendingJsonRpcResponse } from 'json-rpc-engine'; import type { BitskiProviderStateStore } from './store'; import { PaymasterDefinition, WaasDefinition } from './utils/transaction'; export interface User { id: string; accounts?: string[]; } export interface EthChainDefinitionWithRpcUrl extends EthChainDefinition { rpcUrls: [firstUrl: string, ...restUrls: string[]]; } // A generic interface for caching values. Allows for more // flexibility in storing access tokens and other persistent data. export interface BitskiProviderStore { // Empty the cache clear(): void | Promise; keys?(): string[] | Promise; // Get an item from the cache getItem(key: string): unknown | undefined | Promise; // Set an item in the cache setItem(key: string, value: unknown): void | Promise; // Remove the key from the cache clearItem(key: string): void | Promise; onUpdate?(fn: () => void); destroy?(): void; } export interface InternalBitskiProviderConfig { fetch: typeof fetch; additionalHeaders: Record; paymaster?: PaymasterDefinition | PaymasterDefinition[]; prependMiddleware?: ProviderMiddleware[]; pollingInterval?: number; disableCaching?: boolean; disableValidation?: boolean; additionalSigningContext?: Record; appId?: string; /** @deprecated switch to appId */ clientId?: string; apiBaseUrl: string; signerBaseUrl: string; transactionCallbackUrl?: string; signerQueryParams?: URLSearchParams; waas?: WaasDefinition; store: BitskiProviderStore; sign: SignFn; getUser?(): Promise; getAccessToken?(): Promise; clearAccessToken?(): Promise; } type Optional = Omit & Partial; export type BitskiProviderConfig = Optional< InternalBitskiProviderConfig, 'apiBaseUrl' | 'signerBaseUrl' | 'fetch' | 'additionalHeaders' | 'sign' | 'getUser' | 'store' > & { signerMethod?: 'popup' | 'iframe' | 'redirect'; }; /** * Each provider request is made with a context object. The context object contains * information such as the current chain the request should be made on and the * configuration of the provider. This allows us to "switch" chains just by using * a different context. */ export interface RequestContext { // The current chain the request is being made on chain: EthChainDefinitionWithRpcUrl; paymaster?: PaymasterDefinition | PaymasterDefinition[]; // The configuration of the provider config: InternalBitskiProviderConfig; store: BitskiProviderStateStore; // Extra information that can be added by a user of the provider extra?: Extra; // Allows middleware to emit an event on the provider emit: (eventName: T, ...params: EthEventParams[T]) => void; // Allows middleware to make a request using the provider request( { method, params }: EthRequest, opts?: { skipCache?: boolean }, ): EthResult; // Adds a destructor that should run when the provider is destroyed addDestructor(fn: () => void): void; } export interface EthSignMethodParams extends EthMethodParams { personal_sign: [data: string, address: string]; } export type ProviderMiddleware = ( req: JsonRpcRequest & { context: RequestContext }, res: PendingJsonRpcResponse, next: () => Promise, ) => Promise; export type EthSignMethod = | EthMethod.eth_sendTransaction | EthMethod.eth_signTransaction | EthMethod.eth_sign | EthMethod.eth_signTypedData | EthMethod.eth_signTypedData_v1 | EthMethod.eth_signTypedData_v3 // For metamask compatibility | EthMethod.eth_signTypedData_v4 | 'personal_sign'; // legacy compat export type SignFn = ( method: T, params: EthSignMethodParams[T], context: RequestContext, ) => Promise;