import type { PermissionSpecificationBuilder, PermissionType, RestrictedMethodOptions } from '@metamask/permission-controller'; import type { PermittedHandlerExport } from '@metamask/types'; import type { JsonRpcParams } from '@metamask/utils'; import type { methodHandlers } from './permitted'; import type { restrictedMethodPermissionBuilders } from './restricted'; /** * Get the method implementation from a {@link PermittedHandlerExport}. * * @template Handler - A permitted handler export. */ declare type PermittedMethodImplementation = Handler extends PermittedHandlerExport ? (args: Args) => Promise : never; /** * Get a JSON-RPC method type from a {@link PermittedHandlerExport} and a method * name. * * @template MethodName - The name of the method. * @template Handler - A permitted handler export. */ declare type PermittedMethod = PermittedMethodImplementation extends (args: infer Args) => infer Return ? (args: { method: MethodName; params?: Args; }) => Return : never; /** * Get a restricted method implementation from a * {@link PermissionSpecificationBuilder}. * * @template Builder - A permission specification builder. */ declare type RestrictedMethodImplementation = Builder extends { specificationBuilder: PermissionSpecificationBuilder; } ? Specification['methodImplementation'] : never; /** * Get a JSON-RPC method type from a {@link PermissionSpecificationBuilder}. * * @template Builder - A permission specification builder. */ declare type RestrictedMethod = RestrictedMethodImplementation extends (args: infer Args) => infer Return ? Args extends RestrictedMethodOptions ? (args: { method: Builder['targetName']; params?: Params; }) => Return : never : never; /** * A type containing all permitted JSON-RPC methods as functions. */ declare type PermittedMethodFunction = { [MethodName in keyof typeof methodHandlers]: PermittedMethod; }; /** * A type containing all restricted JSON-RPC methods as functions. */ declare type RestrictedMethodFunction = { [Builder in keyof typeof restrictedMethodPermissionBuilders]: RestrictedMethod<(typeof restrictedMethodPermissionBuilders)[Builder]>; }; /** * A type containing all supported JSON-RPC methods as functions. */ declare type MethodFunction = RestrictedMethodFunction & PermittedMethodFunction; /** * Fallback method name. `wallet_*` is supported by Snaps, but these functions * are not implemented in `@metamask/rpc-methods`, so we don't have a type for * them. */ declare type WalletMethodName = `wallet_${string}`; /** * Get a typed function if the method is defined in {@link MethodFunction}, or * a generic function if the method name extends {@link WalletMethodName}. * Otherwise, this returns `never`. */ declare type MethodFunctionFallback = MethodName extends keyof MethodFunction ? MethodFunction[MethodName] : MethodName extends WalletMethodName ? (args: { method: MethodName; params?: JsonRpcParams; }) => Promise : never; /** * Get the JSON-RPC object from a method name. * * @template MethodName - The name of the method. In most cases this is inferred * from the args. */ export declare type ObjectFromMethodName = { method: MethodName; params?: Parameters>[0] extends { params?: infer Params; } ? Params : never; }; export declare type MethodReturnType = ReturnType>; /** * A function that takes a JSON-RPC request and returns a JSON-RPC response. * * @template MethodName - The name of the method. In most cases this is inferred * from the args. */ export declare type RequestFunction = (args: ObjectFromMethodName) => MethodReturnType; /** * The global `snap` object. This is injected into the global scope of a snap. */ export declare type SnapsGlobalObject = { request: RequestFunction; }; export {};