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
4 changes: 4 additions & 0 deletions packages/snaps-controllers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add two new controller state metadata properties: `includeInStateLogs` and `usedInUi` ([#3632](https://github.com/MetaMask/snaps/pull/3632))

## [14.2.2]

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions packages/snaps-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"@types/concat-stream": "^2.0.0",
"@types/gunzip-maybe": "^1.4.0",
"@types/jest": "^27.5.1",
"@types/lodash": "^4",
"@types/luxon": "^3",
"@types/node": "18.14.2",
"@types/readable-stream": "^4.0.15",
Expand All @@ -133,6 +134,7 @@
"jest": "^29.0.2",
"jest-fetch-mock": "^3.0.3",
"jest-silent-reporter": "^0.6.0",
"lodash": "^4.17.21",
"prettier": "^3.3.3",
"rimraf": "^4.1.2",
"ts-node": "^10.9.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { deriveStateFromMetadata } from '@metamask/base-controller';
import { SnapEndowments } from '@metamask/snaps-rpc-methods';
import type { TruncatedSnap } from '@metamask/snaps-utils';
import { HandlerType } from '@metamask/snaps-utils';
Expand Down Expand Up @@ -1126,4 +1127,66 @@ describe('CronjobController', () => {
cronjobController.destroy();
});
});

describe('metadata', () => {
it('includes expected state in debug snapshots', () => {
const controller = new CronjobController({
messenger: getRestrictedCronjobControllerMessenger(),
stateManager: getMockStateManager(),
});

expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'anonymous',
),
).toMatchInlineSnapshot(`{}`);
});

it('includes expected state in state logs', () => {
const controller = new CronjobController({
messenger: getRestrictedCronjobControllerMessenger(),
stateManager: getMockStateManager(),
});

expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'includeInStateLogs',
),
).toMatchInlineSnapshot(`{}`);
});

it('persists expected state', () => {
const controller = new CronjobController({
messenger: getRestrictedCronjobControllerMessenger(),
stateManager: getMockStateManager(),
});

expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'persist',
),
).toMatchInlineSnapshot(`{}`);
});

it('exposes expected state to UI', () => {
const controller = new CronjobController({
messenger: getRestrictedCronjobControllerMessenger(),
stateManager: getMockStateManager(),
});

expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'usedInUi',
),
).toMatchInlineSnapshot(`{}`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ export class CronjobController extends BaseController<
super({
messenger,
metadata: {
events: { persist: false, anonymous: false },
events: {
includeInStateLogs: false,
persist: false,
anonymous: false,
usedInUi: false,
},
},
name: controllerName,
state: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { deriveStateFromMetadata } from '@metamask/base-controller';
import { InternalError } from '@metamask/snaps-sdk';
import { HandlerType } from '@metamask/snaps-utils';
import {
Expand Down Expand Up @@ -649,4 +650,70 @@ describe('SnapInsightsController', () => {
},
);
});

describe('metadata', () => {
it('includes expected state in debug snapshots', () => {
const controller = new SnapInsightsController({
messenger: getRestrictedSnapInsightsControllerMessenger(),
});

expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'anonymous',
),
).toMatchInlineSnapshot(`{}`);
});

it('includes expected state in state logs', () => {
const controller = new SnapInsightsController({
messenger: getRestrictedSnapInsightsControllerMessenger(),
});

expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'includeInStateLogs',
),
).toMatchInlineSnapshot(`
{
"insights": {},
}
`);
});

it('persists expected state', () => {
const controller = new SnapInsightsController({
messenger: getRestrictedSnapInsightsControllerMessenger(),
});

expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'persist',
),
).toMatchInlineSnapshot(`{}`);
});

it('exposes expected state to UI', () => {
const controller = new SnapInsightsController({
messenger: getRestrictedSnapInsightsControllerMessenger(),
});

expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'usedInUi',
),
).toMatchInlineSnapshot(`
{
"insights": {},
}
`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ export class SnapInsightsController extends BaseController<
super({
messenger,
metadata: {
insights: { persist: false, anonymous: false },
insights: {
includeInStateLogs: true,
persist: false,
anonymous: false,
usedInUi: true,
},
},
name: controllerName,
state: { insights: {}, ...state },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPersistentState } from '@metamask/base-controller';
import { deriveStateFromMetadata } from '@metamask/base-controller';
import type { SnapId } from '@metamask/snaps-sdk';
import {
form,
Expand Down Expand Up @@ -82,40 +82,6 @@ describe('SnapInterfaceController', () => {
});
});

describe('constructor', () => {
it('persists notification interfaces', () => {
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 was moved below to the metadata describe block, as it seemed like a better fit there.

const rootMessenger = getRootSnapInterfaceControllerMessenger();
const controllerMessenger =
getRestrictedSnapInterfaceControllerMessenger(rootMessenger);

const controller = new SnapInterfaceController({
messenger: controllerMessenger,
state: {
interfaces: {
// @ts-expect-error missing properties
'1': {
contentType: ContentType.Notification,
},
// @ts-expect-error missing properties
'2': {
contentType: ContentType.Dialog,
},
},
},
});

expect(
getPersistentState(controller.state, controller.metadata),
).toStrictEqual({
interfaces: {
'1': {
contentType: ContentType.Notification,
},
},
});
});
});

describe('createInterface', () => {
it('can create a new interface', async () => {
const rootMessenger = getRootSnapInterfaceControllerMessenger();
Expand Down Expand Up @@ -1745,4 +1711,112 @@ describe('SnapInterfaceController', () => {
).rejects.toThrow(`Approval request with id '${id}' not found.`);
});
});

describe('metadata', () => {
it('includes expected state in debug snapshots', () => {
const controller = new SnapInterfaceController({
messenger: getRestrictedSnapInterfaceControllerMessenger(),
});

expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'anonymous',
),
).toMatchInlineSnapshot(`{}`);
});

it('includes expected state in state logs', () => {
const controller = new SnapInterfaceController({
messenger: getRestrictedSnapInterfaceControllerMessenger(),
});

expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'includeInStateLogs',
),
).toMatchInlineSnapshot(`
{
"interfaces": {},
}
`);
});

describe('persist', () => {
it('persists expected state', () => {
const controller = new SnapInterfaceController({
messenger: getRestrictedSnapInterfaceControllerMessenger(),
});

expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'persist',
),
).toMatchInlineSnapshot(`
{
"interfaces": {},
}
`);
});

it('persists notification interfaces', () => {
const rootMessenger = getRootSnapInterfaceControllerMessenger();
const controllerMessenger =
getRestrictedSnapInterfaceControllerMessenger(rootMessenger);

const controller = new SnapInterfaceController({
messenger: controllerMessenger,
state: {
interfaces: {
// @ts-expect-error missing properties
'1': {
contentType: ContentType.Notification,
},
// @ts-expect-error missing properties
'2': {
contentType: ContentType.Dialog,
},
},
},
});

expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'persist',
),
).toStrictEqual({
interfaces: {
'1': {
contentType: ContentType.Notification,
},
},
});
});
});

it('exposes expected state to UI', () => {
const controller = new SnapInterfaceController({
messenger: getRestrictedSnapInterfaceControllerMessenger(),
});

expect(
deriveStateFromMetadata(
controller.state,
controller.metadata,
'usedInUi',
),
).toMatchInlineSnapshot(`
{
"interfaces": {},
}
`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export class SnapInterfaceController extends BaseController<
messenger,
metadata: {
interfaces: {
includeInStateLogs: true,
persist: (interfaces: Record<string, StoredInterface>) => {
return Object.entries(interfaces).reduce<
Record<string, StoredInterface>
Expand All @@ -216,6 +217,7 @@ export class SnapInterfaceController extends BaseController<
}, {});
},
anonymous: false,
usedInUi: true,
},
},
name: controllerName,
Expand Down
Loading
Loading