import { BaseController } from '@metamask/base-controller'; import type { ControllerGetStateAction } from '@metamask/base-controller'; import type { ControllerStateChangeEvent } from '@metamask/base-controller'; import type { Messenger } from '@metamask/messenger'; import type { JsonRpcError, DataWithOptionalCause } from '@metamask/rpc-errors'; import type { Json, OptionalField } from '@metamask/utils'; import type { ApprovalControllerMethodActions } from './ApprovalController-method-action-types.js'; export declare const ORIGIN_METAMASK = "metamask"; export declare const APPROVAL_TYPE_RESULT_ERROR = "result_error"; export declare const APPROVAL_TYPE_RESULT_SUCCESS = "result_success"; declare const controllerName = "ApprovalController"; type ApprovalRequestData = Record | null; type ApprovalRequestState = Record | null; type ApprovalFlow = { id: string; loadingText: string | null; }; type ResultOptions = { flowToEnd?: string; header?: (string | ResultComponent)[]; icon?: string | null; title?: string | null; }; export type ApprovalRequest = { /** * The ID of the approval request. */ id: string; /** * The origin of the approval request. */ origin: string; /** * The time that the request was received, per Date.now(). */ time: number; /** * The type of the approval request. * Unfortunately, not all values will match the `ApprovalType` enum, so we are using `string` here. * TODO: Replace `string` with `ApprovalType` when all `type` values used by the clients can be encompassed by the `ApprovalType` enum. */ type: string; /** * Additional data associated with the request. */ requestData: RequestData; /** * Additional mutable state associated with the request */ requestState: ApprovalRequestState; /** * Whether the request expects a result object to be returned instead of just the approval value. */ expectsResult: boolean; }; export type ApprovalFlowState = ApprovalFlow; export type ApprovalControllerState = { pendingApprovals: Record>>; pendingApprovalCount: number; approvalFlows: ApprovalFlowState[]; }; export type ApprovalControllerMessenger = Messenger; export type ShowApprovalRequest = () => void | Promise; export type ResultComponent = { /** * A unique identifier for this instance of the component. */ key: string; /** * The name of the component to render. */ name: string; /** * Any properties required by the component. */ properties?: Record; /** * Any child components to render inside the component. */ children?: string | ResultComponent | (string | ResultComponent)[]; }; export type ApprovalControllerOptions = { messenger: ApprovalControllerMessenger; showApprovalRequest: ShowApprovalRequest; state?: Partial; typesExcludedFromRateLimiting?: string[]; }; export type AddApprovalOptions = { id?: string; origin: string; type: string; requestData?: Record; requestState?: Record; expectsResult?: boolean; }; export type UpdateRequestStateOptions = { id: string; requestState: Record; }; export type AcceptOptions = { /** * Whether to resolve the returned promise only when the request creator indicates the success of the * post-approval logic using the result callbacks. * If false or unspecified, the promise will resolve immediately. */ waitForResult?: boolean; /** * Whether to delete the approval request after a result callback is called. * If false or unspecified, the approval request will be deleted immediately. * Ignored if `waitForResult` is false or unspecified. */ deleteAfterResult?: boolean; }; export type StartFlowOptions = OptionalField & { show?: boolean; }; export type EndFlowOptions = Pick; export type SetFlowLoadingTextOptions = ApprovalFlow; export type SuccessOptions = ResultOptions & { message?: string | ResultComponent | (string | ResultComponent)[]; }; export type ErrorOptions = ResultOptions & { error?: string | ResultComponent | (string | ResultComponent)[]; }; export type AcceptResultCallbacks = { /** * Inform the request acceptor that the post-approval logic was successful. * * @param value - An optional value generated by the post-approval logic. */ success: (value?: unknown) => void; /** * Inform the request acceptor that the post-approval logic failed. * * @param error - The reason for the failure. */ error: (error: Error) => void; }; export type AddResult = { /** * An optional value provided by the request acceptor. */ value?: unknown; /** * Callback functions that must be used to indicate to the request acceptor whether the post-approval logic was successful or not. * Will be undefined if the request acceptor did not specify that they want to wait for a result. */ resultCallbacks?: AcceptResultCallbacks; }; export type AcceptResult = { /** * An optional value provided by the request creator when indicating a successful result. */ value?: unknown; }; export type ApprovalFlowStartResult = ApprovalFlow; export type SuccessResult = Record; export type ErrorResult = Record; export type ApprovalStateChange = ControllerStateChangeEvent; export type ApprovalControllerEvents = ApprovalStateChange; export type ApprovalControllerGetStateAction = ControllerGetStateAction; export type ApprovalControllerActions = ApprovalControllerGetStateAction | ApprovalControllerMethodActions; /** * Controller for managing requests that require user approval. * * Enables limiting the number of pending requests by origin and type, counting * pending requests, and more. * * Adding a request returns a promise that resolves or rejects when the request * is approved or denied, respectively. */ export declare class ApprovalController extends BaseController { #private; /** * Construct an Approval controller. * * @param options - The controller options. * @param options.showApprovalRequest - Function for opening the UI such that * the request can be displayed to the user. * @param options.messenger - The restricted messenger for the Approval controller. * @param options.state - The initial controller state. * @param options.typesExcludedFromRateLimiting - Array of approval types which allow multiple pending approval requests from the same origin. */ constructor({ messenger, showApprovalRequest, state, typesExcludedFromRateLimiting, }: ApprovalControllerOptions); /** * Adds an approval request per the given arguments, optionally showing * the approval request to the user. * * @param opts - Options bag. * @param opts.id - The id of the approval request. A random id will be * generated if none is provided. * @param opts.origin - The origin of the approval request. * @param opts.type - The type associated with the approval request. * @param opts.requestData - Additional data associated with the request, * if any. * @param opts.requestState - Additional state associated with the request, * if any. * @param shouldShowRequest - Whether to show the approval request to the user. * @returns The approval promise. */ addRequest(opts: AddApprovalOptions, shouldShowRequest: boolean): Promise; /** * Adds an approval request per the given arguments, calls the show approval * request function, and returns the associated approval promise resolving to * an AddResult object. * * There can only be one approval per origin and type. An error is thrown if * attempting to add an invalid or duplicate request. * * @param opts - Options bag. * @param opts.id - The id of the approval request. A random id will be * generated if none is provided. * @param opts.origin - The origin of the approval request. * @param opts.type - The type associated with the approval request. * @param opts.requestData - Additional data associated with the request, * @param opts.requestState - Additional state associated with the request, * if any. * @returns The approval promise resolving to an AddResult object. */ addAndShowApprovalRequest(opts: AddApprovalOptions & { expectsResult: true; }): Promise; /** * Adds an approval request per the given arguments, calls the show approval * request function, and returns the associated approval promise resolving * to a value provided during acceptance. * * There can only be one approval per origin and type. An error is thrown if * attempting to add an invalid or duplicate request. * * @param opts - Options bag. * @param opts.id - The id of the approval request. A random id will be * generated if none is provided. * @param opts.origin - The origin of the approval request. * @param opts.type - The type associated with the approval request. * @param opts.requestData - Additional data associated with the request, * @param opts.requestState - Additional state associated with the request, * if any. * @returns The approval promise resolving to a value provided during acceptance. */ addAndShowApprovalRequest(opts: AddApprovalOptions): Promise; /** * Adds an approval request per the given arguments and returns the approval * promise resolving to an AddResult object. * * There can only be one approval per origin and type. An error is thrown if * attempting to add an invalid or duplicate request. * * @param opts - Options bag. * @param opts.id - The id of the approval request. A random id will be * generated if none is provided. * @param opts.origin - The origin of the approval request. * @param opts.type - The type associated with the approval request. * @param opts.requestData - Additional data associated with the request, * if any. * @returns The approval promise resolving to an AddResult object. */ add(opts: AddApprovalOptions & { expectsResult: true; }): Promise; /** * Adds an approval request per the given arguments and returns the approval * promise resolving to a value provided during acceptance. * * There can only be one approval per origin and type. An error is thrown if * attempting to add an invalid or duplicate request. * * @param opts - Options bag. * @param opts.id - The id of the approval request. A random id will be * generated if none is provided. * @param opts.origin - The origin of the approval request. * @param opts.type - The type associated with the approval request. * @param opts.requestData - Additional data associated with the request, * if any. * @returns The approval promise resolving to a value provided during acceptance. */ add(opts: AddApprovalOptions): Promise; /** * Gets the info for the approval request with the given id. * * @param id - The id of the approval request. * @returns The approval request data associated with the id. */ get(id: string): ApprovalRequest | undefined; /** * Gets the number of pending approvals, by origin and/or type. * * If only `origin` is specified, all approvals for that origin will be * counted, regardless of type. * If only `type` is specified, all approvals for that type will be counted, * regardless of origin. * If both `origin` and `type` are specified, 0 or 1 will be returned. * * @param opts - The approval count options. * @param opts.origin - An approval origin. * @param opts.type - The type of the approval request. * @returns The current approval request count for the given origin and/or * type. */ getApprovalCount(opts?: { origin?: string; type?: string; }): number; /** * Get the total count of all pending approval requests for all origins. * * @returns The total pending approval request count. */ getTotalApprovalCount(): number; /** * Checks if there's a pending approval request per the given parameters. * At least one parameter must be specified. An error will be thrown if the * parameters are invalid. * * If `id` is specified, all other parameters will be ignored. * If `id` is not specified, the method will check for requests that match * all of the specified parameters. * * @param opts - Options bag. * @param opts.id - The ID to check for. * @param opts.origin - The origin to check for. * @param opts.type - The type to check for. * @returns `true` if a matching approval is found, and `false` otherwise. */ hasRequest(opts?: { id?: string; origin?: string; type?: string; }): boolean; /** * Resolves the promise of the approval with the given id, and deletes the * approval. Throws an error if no such approval exists. * * @param id - The id of the approval request. * @param value - The value to resolve the approval promise with. * @param options - Options bag. * @returns A promise that either resolves once a result is provided by * the creator of the approval request, or immediately if `options.waitForResult` * is `false` or `undefined`. */ acceptRequest(id: string, value?: unknown, options?: AcceptOptions): Promise; /** * Rejects the promise of the approval with the given id, and deletes the * approval. Throws an error if no such approval exists. * * @param id - The id of the approval request. * @param error - The error to reject the approval promise with. */ rejectRequest(id: string, error: unknown): void; /** * Rejects and deletes all approval requests. * * @param rejectionError - The JsonRpcError to reject the approval * requests with. */ clearRequests(rejectionError: JsonRpcError): void; /** * Updates the request state of the approval with the given id. * * @param opts - Options bag. * @param opts.id - The id of the approval request. * @param opts.requestState - Additional data associated with the request */ updateRequestState(opts: UpdateRequestStateOptions): void; /** * Starts a new approval flow. * * @param opts - Options bag. * @param opts.id - The id of the approval flow. * @param opts.loadingText - The loading text that will be associated to the approval flow. * @param opts.show - A flag to determine whether the approval should show to the user. * @returns The object containing the approval flow id. */ startFlow(opts?: StartFlowOptions): ApprovalFlowStartResult; /** * Ends the current approval flow. * * @param opts - Options bag. * @param opts.id - The id of the approval flow that will be finished. */ endFlow({ id }: EndFlowOptions): void; /** * Sets the loading text for the approval flow. * * @param opts - Options bag. * @param opts.id - The approval flow loading text that will be displayed. * @param opts.loadingText - The loading text that will be associated to the approval flow. */ setFlowLoadingText({ id, loadingText }: SetFlowLoadingTextOptions): void; /** * Show a success page. * * @param opts - Options bag. * @param opts.message - The message text or components to display in the page. * @param opts.header - The text or components to display in the header of the page. * @param opts.flowToEnd - The ID of the approval flow to end once the success page is approved. * @param opts.title - The title to display above the message. Shown by default but can be hidden with `null`. * @param opts.icon - The icon to display in the page. Shown by default but can be hidden with `null`. * @returns Empty object to support future additions. */ showSuccess(opts?: SuccessOptions): Promise; /** * Show an error page. * * @param opts - Options bag. * @param opts.message - The message text or components to display in the page. * @param opts.header - The text or components to display in the header of the page. * @param opts.flowToEnd - The ID of the approval flow to end once the error page is approved. * @param opts.title - The title to display above the message. Shown by default but can be hidden with `null`. * @param opts.icon - The icon to display in the page. Shown by default but can be hidden with `null`. * @returns Empty object to support future additions. */ showError(opts?: ErrorOptions): Promise; } export default ApprovalController; //# sourceMappingURL=ApprovalController.d.ts.map