diff --git a/packages/snaps-controllers/CHANGELOG.md b/packages/snaps-controllers/CHANGELOG.md index 79c95c4809..ec8c8b8d95 100644 --- a/packages/snaps-controllers/CHANGELOG.md +++ b/packages/snaps-controllers/CHANGELOG.md @@ -9,36 +9,41 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- **BREAKING:** All `SnapController` action types were renamed from `DoSomething` to `SnapControllerDoSomethingAction` ([#3907](https://github.com/MetaMask/snaps/pull/3907)) - - `GetSnap` is now `SnapControllerGetSnapAction`. - - Note: The method is now called `getSnap` instead of `get`. - - `HandleSnapRequest` is now `SnapControllerHandleRequestAction`. - - `GetSnapState` is now `SnapControllerGetSnapStateAction`. - - `HasSnap` is now `SnapControllerHasSnapAction`. - - Note: The method is now called `hasSnap` instead of `has`. - - `UpdateSnapState` is now `SnapControllerUpdateSnapStateAction`. - - `ClearSnapState` is now `SnapControllerClearSnapStateAction`. - - `UpdateRegistry` is now `SnapControllerUpdateRegistryAction`. - - `EnableSnap` is now `SnapControllerEnableSnapAction`. - - Note: The method is now called `enableSnap` instead of `enable`. - - `DisableSnap` is now `SnapControllerDisableSnapAction`. - - Note: The method is now called `disableSnap` instead of `disable`. - - `RemoveSnap` is now `SnapControllerRemoveSnapAction`. - - Note: The method is now called `removeSnap` instead of `remove`. - - `GetPermittedSnaps` is now `SnapControllerGetPermittedSnapsAction`. - - Note: The method is now called `getPermittedSnaps` instead of `getPermitted`. - - `GetAllSnaps` is now `SnapControllerGetAllSnapsAction`. - - Note: The method is now called `getAllSnaps` instead of `getAll`. - - `GetRunnableSnaps` is now `SnapControllerGetRunnableSnapsAction`. - - `StopAllSnaps` is now `SnapControllerStopAllSnapsAction`. - - `InstallSnaps` is now `SnapControllerInstallSnapsAction`. - - Note: The method is now called `installSnaps` instead of `install`. - - `DisconnectOrigin` is now `SnapControllerDisconnectOriginAction`. - - Note: The method is now called `disconnectOrigin` instead of `removeSnapFromSubject`. - - `RevokeDynamicPermissions` is now `SnapControllerRevokeDynamicSnapPermissionsAction`. - - `GetSnapFile` is now `SnapControllerGetSnapFileAction`. - - `IsMinimumPlatformVersion` is now `SnapControllerIsMinimumPlatformVersionAction`. - - `SetClientActive` is now `SnapControllerSetClientActiveAction`. +- **BREAKING:** All action types were renamed from `DoSomething` to `ControllerNameDoSomethingAction` ([#3907](https://github.com/MetaMask/snaps/pull/3907), [#3911](https://github.com/MetaMask/snaps/pull/3911)) + - `SnapController` actions: + - `GetSnap` is now `SnapControllerGetSnapAction`. + - Note: The method is now called `getSnap` instead of `get`. + - `HandleSnapRequest` is now `SnapControllerHandleRequestAction`. + - `GetSnapState` is now `SnapControllerGetSnapStateAction`. + - `HasSnap` is now `SnapControllerHasSnapAction`. + - Note: The method is now called `hasSnap` instead of `has`. + - `UpdateSnapState` is now `SnapControllerUpdateSnapStateAction`. + - `ClearSnapState` is now `SnapControllerClearSnapStateAction`. + - `UpdateRegistry` is now `SnapControllerUpdateRegistryAction`. + - `EnableSnap` is now `SnapControllerEnableSnapAction`. + - Note: The method is now called `enableSnap` instead of `enable`. + - `DisableSnap` is now `SnapControllerDisableSnapAction`. + - Note: The method is now called `disableSnap` instead of `disable`. + - `RemoveSnap` is now `SnapControllerRemoveSnapAction`. + - Note: The method is now called `removeSnap` instead of `remove`. + - `GetPermittedSnaps` is now `SnapControllerGetPermittedSnapsAction`. + - Note: The method is now called `getPermittedSnaps` instead of `getPermitted`. + - `GetAllSnaps` is now `SnapControllerGetAllSnapsAction`. + - Note: The method is now called `getAllSnaps` instead of `getAll`. + - `GetRunnableSnaps` is now `SnapControllerGetRunnableSnapsAction`. + - `StopAllSnaps` is now `SnapControllerStopAllSnapsAction`. + - `InstallSnaps` is now `SnapControllerInstallSnapsAction`. + - Note: The method is now called `installSnaps` instead of `install`. + - `DisconnectOrigin` is now `SnapControllerDisconnectOriginAction`. + - Note: The method is now called `disconnectOrigin` instead of `removeSnapFromSubject`. + - `RevokeDynamicPermissions` is now `SnapControllerRevokeDynamicSnapPermissionsAction`. + - `GetSnapFile` is now `SnapControllerGetSnapFileAction`. + - `IsMinimumPlatformVersion` is now `SnapControllerIsMinimumPlatformVersionAction`. + - `SetClientActive` is now `SnapControllerSetClientActiveAction`. + - `CronjobController` actions: + - `Schedule` is now `CronjobControllerScheduleAction`. + - `Cancel` is now `CronjobControllerCancelAction`. + - `Get` is now `CronjobControllerGetAction`. - **BREAKING:** All `SnapController` event types were renamed from `OnSomething` to `SnapControllerOnSomethingEvent` ([#3907](https://github.com/MetaMask/snaps/pull/3907)) - `SnapStateChange` was removed in favour of `SnapControllerStateChangeEvent`. - `SnapBlocked` is now `SnapControllerSnapBlockedEvent`. diff --git a/packages/snaps-controllers/src/cronjob/CronjobController-method-action-types.ts b/packages/snaps-controllers/src/cronjob/CronjobController-method-action-types.ts new file mode 100644 index 0000000000..162de24e67 --- /dev/null +++ b/packages/snaps-controllers/src/cronjob/CronjobController-method-action-types.ts @@ -0,0 +1,60 @@ +/** + * This file is auto generated by `scripts/generate-method-action-types.ts`. + * Do not edit manually. + */ + +import type { CronjobController } from './CronjobController'; + +/** + * Initialize the CronjobController. + * + * This starts the daily timer, clears out expired events + * and reschedules any remaining events. + */ +export type CronjobControllerInitAction = { + type: `CronjobController:init`; + handler: CronjobController['init']; +}; + +/** + * Schedule a non-recurring background event. + * + * @param event - The event to schedule. + * @returns The ID of the scheduled event. + */ +export type CronjobControllerScheduleAction = { + type: `CronjobController:schedule`; + handler: CronjobController['schedule']; +}; + +/** + * Cancel an event. + * + * @param origin - The origin making the cancel call. + * @param id - The id of the event to cancel. + * @throws If the event does not exist. + */ +export type CronjobControllerCancelAction = { + type: `CronjobController:cancel`; + handler: CronjobController['cancel']; +}; + +/** + * Get a list of a Snap's background events. + * + * @param snapId - The id of the Snap to fetch background events for. + * @returns An array of background events. + */ +export type CronjobControllerGetAction = { + type: `CronjobController:get`; + handler: CronjobController['get']; +}; + +/** + * Union of all CronjobController action types. + */ +export type CronjobControllerMethodActions = + | CronjobControllerInitAction + | CronjobControllerScheduleAction + | CronjobControllerCancelAction + | CronjobControllerGetAction; diff --git a/packages/snaps-controllers/src/cronjob/CronjobController.ts b/packages/snaps-controllers/src/cronjob/CronjobController.ts index c5914ca608..c59f4e2f7b 100644 --- a/packages/snaps-controllers/src/cronjob/CronjobController.ts +++ b/packages/snaps-controllers/src/cronjob/CronjobController.ts @@ -21,6 +21,7 @@ import { castDraft } from 'immer'; import { DateTime } from 'luxon'; import { nanoid } from 'nanoid'; +import type { CronjobControllerMethodActions } from './CronjobController-method-action-types'; import { getCronjobSpecificationSchedule, getExecutionDate } from './utils'; import type { SnapControllerHandleRequestAction, @@ -43,41 +44,15 @@ export type CronjobControllerStateChangeEvent = ControllerStateChangeEvent< CronjobControllerState >; -/** - * Initialise the CronjobController. This should be called after all controllers - * are created. - */ -export type CronjobControllerInitAction = { - type: `${typeof controllerName}:init`; - handler: CronjobController['init']; -}; - -export type Schedule = { - type: `${typeof controllerName}:schedule`; - handler: CronjobController['schedule']; -}; +export type CronjobControllerActions = + | CronjobControllerGetStateAction + | CronjobControllerMethodActions; -export type Cancel = { - type: `${typeof controllerName}:cancel`; - handler: CronjobController['cancel']; -}; +export type CronjobControllerEvents = CronjobControllerStateChangeEvent; -export type Get = { - type: `${typeof controllerName}:get`; - handler: CronjobController['get']; -}; +type AllowedActions = GetPermissions | SnapControllerHandleRequestAction; -export type CronjobControllerActions = - | CronjobControllerGetStateAction - | SnapControllerHandleRequestAction - | GetPermissions - | Schedule - | Cancel - | Get - | CronjobControllerInitAction; - -export type CronjobControllerEvents = - | CronjobControllerStateChangeEvent +type AllowedEvents = | SnapControllerSnapInstalledEvent | SnapControllerSnapUninstalledEvent | SnapControllerSnapUpdatedEvent @@ -86,8 +61,8 @@ export type CronjobControllerEvents = export type CronjobControllerMessenger = Messenger< typeof controllerName, - CronjobControllerActions, - CronjobControllerEvents + CronjobControllerActions | AllowedActions, + CronjobControllerEvents | AllowedEvents >; export const DAILY_TIMEOUT = inMilliseconds(24, Duration.Hour); @@ -157,6 +132,13 @@ export type CronjobControllerState = { const controllerName = 'CronjobController'; +const MESSENGER_EXPOSED_METHODS = [ + 'init', + 'schedule', + 'cancel', + 'get', +] as const; + /** * The cronjob controller is responsible for managing cronjobs and background * events for Snaps. It allows Snaps to schedule events that will be executed @@ -220,22 +202,9 @@ export class CronjobController extends BaseController< this.#handleSnapUpdatedEvent, ); - this.messenger.registerActionHandler(`${controllerName}:init`, (...args) => - this.init(...args), - ); - - this.messenger.registerActionHandler( - `${controllerName}:schedule`, - (...args) => this.schedule(...args), - ); - - this.messenger.registerActionHandler( - `${controllerName}:cancel`, - (...args) => this.cancel(...args), - ); - - this.messenger.registerActionHandler(`${controllerName}:get`, (...args) => - this.get(...args), + this.messenger.registerMethodActionHandlers( + this, + MESSENGER_EXPOSED_METHODS, ); } diff --git a/packages/snaps-controllers/src/cronjob/index.ts b/packages/snaps-controllers/src/cronjob/index.ts index ed316ed002..ad8d078a66 100644 --- a/packages/snaps-controllers/src/cronjob/index.ts +++ b/packages/snaps-controllers/src/cronjob/index.ts @@ -1 +1,13 @@ -export * from './CronjobController'; +export type { + CronjobControllerGetStateAction, + CronjobControllerState, + CronjobControllerStateChangeEvent, + CronjobControllerStateManager, +} from './CronjobController'; +export { CronjobController } from './CronjobController'; +export type { + CronjobControllerInitAction, + CronjobControllerScheduleAction, + CronjobControllerCancelAction, + CronjobControllerGetAction, +} from './CronjobController-method-action-types'; diff --git a/packages/snaps-controllers/src/snaps/SnapController.ts b/packages/snaps-controllers/src/snaps/SnapController.ts index af4aa5e5da..351df0cd2a 100644 --- a/packages/snaps-controllers/src/snaps/SnapController.ts +++ b/packages/snaps-controllers/src/snaps/SnapController.ts @@ -206,7 +206,9 @@ import { export const controllerName = 'SnapController'; -export const MESSENGER_EXPOSED_METHODS = [ +// This is used by the `generate-method-action-types` script. +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const MESSENGER_EXPOSED_METHODS = [ 'init', 'updateRegistry', 'enableSnap',