Skip to content

Commit

Permalink
fix(snaps-registry): Define missing getState action, stateChange
Browse files Browse the repository at this point in the history
…event (#2655)

## Explanation

Fixes the `SnapsRegistryMessenger` type and the `JsonSnapsRegistry`
class. Currently they do not fulfill their intended specifications
correctly.

- [Define the `*:getState` action using the `ControllerGetStateAction`
utility
type](https://github.com/MetaMask/core/blob/add-controller-guidelines/docs/writing-controllers.md#define-the-getstate-action-using-the-controllergetstateaction-utility-type)
- [Define the `*:stateChange` event using the
`ControllerStateChangeEvent` utility
type](https://github.com/MetaMask/core/blob/add-controller-guidelines/docs/writing-controllers.md#define-the-statechange-event-using-the-controllerstatechangeevent-utility-type)
- [Define and export a type union for internal action
types](https://github.com/MetaMask/core/blob/add-controller-guidelines/docs/writing-controllers.md#define-and-export-a-type-union-for-internal-action-types)
- [Define and export a type union for internal event
types](https://github.com/MetaMask/core/blob/add-controller-guidelines/docs/writing-controllers.md#define-and-export-a-type-union-for-internal-event-types)
- [Define and export a type for the controller's
messenger](https://github.com/MetaMask/core/blob/add-controller-guidelines/docs/writing-controllers.md#define-and-export-a-type-for-the-controllers-messenger)

This also resolves downstream errors in mobile caused by
`composable-controller` expecting all of its child controllers to have a
`stateChange` event.
- See
MetaMask/metamask-mobile@4ea1202

## References

- Fixes MetaMask/core#4579
- Blocks MetaMask/metamask-mobile#10441

## Changelog

### `@metamask/snaps-controllers`

### Added

- Define and export new types: `SnapsRegistryGetStateAction`,
`SnapsRegistryStateChangeEvent`
([#2655](#2655))

### Changed

- `SnapsRegistryActions` is widened to include the
`SnapsRegistry:getState` action
([#2655](#2655))
- `SnapsRegistryEvents` is widened to include the
`SnapsRegistry:stateChange` event
([#2655](#2655))
  • Loading branch information
MajorLift authored Aug 21, 2024
1 parent fbe2205 commit d6c869e
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions packages/snaps-controllers/src/snaps/registry/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { RestrictedControllerMessenger } from '@metamask/base-controller';
import type {
ControllerGetStateAction,
ControllerStateChangeEvent,
RestrictedControllerMessenger,
} from '@metamask/base-controller';
import { BaseController } from '@metamask/base-controller';
import type { SnapsRegistryDatabase } from '@metamask/snaps-registry';
import { verify } from '@metamask/snaps-registry';
Expand Down Expand Up @@ -65,13 +69,24 @@ export type Update = {
handler: SnapsRegistry['update'];
};

export type SnapsRegistryGetStateAction = ControllerGetStateAction<
typeof controllerName,
SnapsRegistryState
>;

export type SnapsRegistryActions =
| SnapsRegistryGetStateAction
| GetResult
| GetMetadata
| Update
| ResolveVersion;

export type SnapsRegistryEvents = never;
export type SnapsRegistryStateChangeEvent = ControllerStateChangeEvent<
typeof controllerName,
SnapsRegistryState
>;

export type SnapsRegistryEvents = SnapsRegistryStateChangeEvent;

export type SnapsRegistryMessenger = RestrictedControllerMessenger<
'SnapsRegistry',
Expand Down

0 comments on commit d6c869e

Please sign in to comment.