Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 35 additions & 30 deletions packages/snaps-controllers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
69 changes: 19 additions & 50 deletions packages/snaps-controllers/src/cronjob/CronjobController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
);
}

Expand Down
14 changes: 13 additions & 1 deletion packages/snaps-controllers/src/cronjob/index.ts
Original file line number Diff line number Diff line change
@@ -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';
4 changes: 3 additions & 1 deletion packages/snaps-controllers/src/snaps/SnapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This didn't need to be exported.

'init',
'updateRegistry',
'enableSnap',
Expand Down
Loading