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
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
"changelog:validate": "yarn workspaces foreach --all --parallel --interlaced --verbose run changelog:validate",
"child-workspace-package-names-as-json": "ts-node scripts/child-workspace-package-names-as-json.ts",
"clean": "yarn workspaces foreach --all --parallel --verbose run clean",
"generate-method-action-types": "yarn workspaces foreach --all --parallel --interlaced --verbose run generate-method-action-types",
"get-release-tag": "ts-node --swc scripts/get-release-tag.ts",
"install-chrome": "./scripts/install-chrome.sh",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn lint:tsconfig && yarn constraints && yarn lint:dependencies",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn lint:tsconfig && yarn constraints && yarn lint:dependencies && yarn generate-method-action-types --check",
"lint:dependencies": "yarn workspaces foreach --all --parallel --verbose run lint:dependencies && yarn dedupe --check",
"lint:eslint": "eslint . --cache",
"lint:fix": "yarn workspaces foreach --all --parallel run lint:eslint --fix && yarn lint:misc --write && yarn lint:tsconfig && yarn constraints --fix && yarn dedupe",
Expand Down Expand Up @@ -120,7 +121,8 @@
"tsx": "^4.20.3",
"typescript": "~5.3.3",
"typescript-eslint": "^8.6.0",
"vite": "^6.4.1"
"vite": "^6.4.1",
"yargs": "^17.7.1"
},
"packageManager": "yarn@4.10.3",
"engines": {
Expand Down
92 changes: 92 additions & 0 deletions packages/snaps-controllers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,98 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- **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), [#3912](https://github.com/MetaMask/snaps/pull/3912), [#3916](https://github.com/MetaMask/snaps/pull/3916), [#3918](https://github.com/MetaMask/snaps/pull/3918))
- `SnapController` actions:
- `GetSnap` is now `SnapControllerGetSnapAction`.
- Note: The method is now called `getSnap` instead of `get`.
- Note: It now returns `null` if a Snap is not found, instead of
`undefined`.
- `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`.
- `SnapInterfaceController` actions:
- `CreateInterface` is now `SnapInterfaceControllerCreateInterfaceAction`.
- `GetInterface` is now `SnapInterfaceControllerGetInterfaceAction`.
- `UpdateInterface` is now `SnapInterfaceControllerUpdateInterfaceAction`.
- `DeleteInterface` is now `SnapInterfaceControllerDeleteInterfaceAction`.
- `UpdateInterfaceState` is now `SnapInterfaceControllerUpdateInterfaceStateAction`.
- `ResolveInterface` is now `SnapInterfaceControllerResolveInterfaceAction`.
- `ExecutionService` actions:
- `ExecuteSnap` is now `ExecutionServiceExecuteSnapAction`.
- `HandleRequest` is now `ExecutionServiceHandleRequestAction`.
- `TerminateSnap` is now `ExecutionServiceTerminateSnapAction`.
- `GetExecutionStatus` is now `ExecutionServiceGetExecutionStatusAction`.
- `SnapRegistryController` actions:
- `GetResult` is now `SnapRegistryControllerGetAction`.
- `GetMetadata` is now `SnapRegistryControllerGetMetadataAction`.
- `ResolveVersion` is now `SnapRegistryControllerResolveVersionAction`.
- `Update` is now `SnapRegistryControllerRequestUpdateAction`.
- Note: The method is now called `requestUpdate` instead of `update`.
- **BREAKING:** All event types were renamed from `OnSomething` to `ControllerOnSomethingEvent` ([#3907](https://github.com/MetaMask/snaps/pull/3907), [#3916](https://github.com/MetaMask/snaps/pull/3916))
- `SnapController` events:
- `SnapStateChange` was removed in favour of `SnapControllerStateChangeEvent`.
- `SnapBlocked` is now `SnapControllerSnapBlockedEvent`.
- `SnapInstallStarted` is now `SnapControllerSnapInstallStartedEvent`.
- `SnapInstallFailed` is now `SnapControllerSnapInstallFailedEvent`.
- `SnapInstalled` is now `SnapControllerSnapInstalledEvent`.
- `SnapUninstalled` is now `SnapControllerSnapUninstalledEvent`.
- `SnapUnblocked` is now `SnapControllerSnapUnblockedEvent`.
- `SnapUpdated` is now `SnapControllerSnapUpdatedEvent`.
- `SnapRolledback` is now `SnapControllerSnapRolledbackEvent`.
- `SnapTerminated` is now `SnapControllerSnapTerminatedEvent`.
- `SnapEnabled` is now `SnapControllerSnapEnabledEvent`.
- `SnapDisabled` is now `SnapControllerSnapDisabledEvent`.
- `ExecutionService` events:
- `ErrorMessageEvent` is now `ExecutionServiceUnhandledErrorEvent`.
- `OutboundRequest` is now `ExecutionServiceOutboundRequestEvent`.
- `OutboundResponse` is now `ExecutionServiceOutboundResponseEvent`.
- **BREAKING:**: Rename `MultichainRouter` to `MultichainRoutingService` and update action types accordingly ([#3913](https://github.com/MetaMask/snaps/pull/3913))
- This is consistent with the naming of other services.
- **BREAKING:** Rename `JsonSnapsRegistry` to `SnapRegistryController` and update action types accordingly ([#3918](https://github.com/MetaMask/snaps/pull/3918))
- This is consistent with the naming of other controllers.
- The controller name is now `SnapRegistryController` instead of `SnapsRegistry` as well.
- **BREAKING:** `MultichainRoutingService` now requires `SnapController:getRunnableSnaps` instead of `SnapController:getAllSnaps` ([#3913](https://github.com/MetaMask/snaps/pull/3913))
- **BREAKING:** `SnapInsightsController` now requires `SnapController:getRunnableSnaps` instead of `SnapController:getAllSnaps` ([#3915](https://github.com/MetaMask/snaps/pull/3915))
- **RREAKING:** Replace `ExecutionService` interface with abstract class ([#3916](https://github.com/MetaMask/snaps/pull/3916))
- The `ExecutionService` is now an abstract class and replaces the previous `AbstractExecutionService` class interface.

### Removed

- **RREAKING:** Remove `AbstractExecutionService` class in favour of `ExecutionService` ([#3916](https://github.com/MetaMask/snaps/pull/3916))
- **BREAKING:** Remove `incrementActiveReferences` and `decrementActiveReferences` actions and methods from `SnapController` ([#3907](https://github.com/MetaMask/snaps/pull/3907), [#3920](https://github.com/MetaMask/snaps/pull/3920))
- This was never used in production.
- **BREAKING:** Remove public `getTruncatedSnap` and `getTruncatedSnapExpect` methods from `SnapController` ([#3923](https://github.com/MetaMask/snaps/pull/3923))

## [18.0.4]

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"branches": 94.97,
"functions": 98.78,
"lines": 98.63,
"statements": 98.43
"statements": 98.32
}
1 change: 1 addition & 0 deletions packages/snaps-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
"changelog:update": "../../scripts/update-changelog.sh @metamask/snaps-controllers",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/snaps-controllers",
"generate-method-action-types": "tsx ../../scripts/generate-method-action-types.mts",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn changelog:validate && yarn lint:dependencies",
"lint:ci": "yarn lint",
"lint:dependencies": "depcheck",
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;
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ describe('CronjobController', () => {
deriveStateFromMetadata(
controller.state,
controller.metadata,
'anonymous',
'includeInDebugSnapshot',
),
).toMatchInlineSnapshot(`{}`);
});
Expand Down
94 changes: 32 additions & 62 deletions packages/snaps-controllers/src/cronjob/CronjobController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,72 +21,48 @@ 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 {
HandleSnapRequest,
SnapDisabled,
SnapEnabled,
SnapInstalled,
SnapUninstalled,
SnapUpdated,
} from '..';
SnapControllerHandleRequestAction,
SnapControllerSnapDisabledEvent,
SnapControllerSnapEnabledEvent,
SnapControllerSnapInstalledEvent,
SnapControllerSnapUninstalledEvent,
SnapControllerSnapUpdatedEvent,
} from '../snaps';
import { METAMASK_ORIGIN } from '../snaps/constants';
import { Timer } from '../snaps/Timer';

export type CronjobControllerGetStateAction = ControllerGetStateAction<
typeof controllerName,
CronjobControllerState
>;

export type CronjobControllerStateChangeEvent = ControllerStateChangeEvent<
typeof controllerName,
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 CronjobControllerActions =
| CronjobControllerGetStateAction
| CronjobControllerMethodActions;

export type Schedule = {
type: `${typeof controllerName}:schedule`;
handler: CronjobController['schedule'];
};
export type CronjobControllerEvents = CronjobControllerStateChangeEvent;

export type Cancel = {
type: `${typeof controllerName}:cancel`;
handler: CronjobController['cancel'];
};
type AllowedActions = GetPermissions | SnapControllerHandleRequestAction;

export type Get = {
type: `${typeof controllerName}:get`;
handler: CronjobController['get'];
};

export type CronjobControllerActions =
| CronjobControllerGetStateAction
| HandleSnapRequest
| GetPermissions
| Schedule
| Cancel
| Get
| CronjobControllerInitAction;

export type CronjobControllerEvents =
| CronjobControllerStateChangeEvent
| SnapInstalled
| SnapUninstalled
| SnapUpdated
| SnapEnabled
| SnapDisabled;
type AllowedEvents =
| SnapControllerSnapInstalledEvent
| SnapControllerSnapUninstalledEvent
| SnapControllerSnapUpdatedEvent
| SnapControllerSnapEnabledEvent
| SnapControllerSnapDisabledEvent;

export type CronjobControllerMessenger = Messenger<
typeof controllerName,
CronjobControllerActions,
CronjobControllerEvents
CronjobControllerActions | AllowedActions,
CronjobControllerEvents | AllowedEvents
>;

export const DAILY_TIMEOUT = inMilliseconds(24, Duration.Hour);
Expand Down Expand Up @@ -156,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 @@ -219,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
18 changes: 17 additions & 1 deletion packages/snaps-controllers/src/cronjob/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
export * from './CronjobController';
export type {
CronjobControllerActions,
CronjobControllerArgs,
CronjobControllerEvents,
CronjobControllerGetStateAction,
CronjobControllerMessenger,
CronjobControllerState,
CronjobControllerStateChangeEvent,
CronjobControllerStateManager,
} from './CronjobController';
export { CronjobController } from './CronjobController';
export type {
CronjobControllerInitAction,
CronjobControllerScheduleAction,
CronjobControllerCancelAction,
CronjobControllerGetAction,
} from './CronjobController-method-action-types';
Loading
Loading