Skip to content

Commit

Permalink
feat: add hidden flag for preinstalled Snaps (#2463)
Browse files Browse the repository at this point in the history
Adds a `hidden` flag that preinstalled Snaps can use to signal whether
they should be displayed to the user or not.
  • Loading branch information
FrederikBolding authored Jun 5, 2024
1 parent 2919879 commit 491f8fb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
39 changes: 39 additions & 0 deletions packages/snaps-controllers/src/snaps/SnapController.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4789,6 +4789,45 @@ describe('SnapController', () => {
snapController.destroy();
});

it('supports preinstalled Snaps specifying the hidden flag', async () => {
const rootMessenger = getControllerMessenger();
jest.spyOn(rootMessenger, 'call');

// The snap should not have permission initially
rootMessenger.registerActionHandler(
'PermissionController:getPermissions',
() => ({}),
);

const preinstalledSnaps = [
{
snapId: MOCK_SNAP_ID,
manifest: getSnapManifest(),
hidden: true,
files: [
{
path: DEFAULT_SOURCE_PATH,
value: stringToBytes(DEFAULT_SNAP_BUNDLE),
},
{
path: DEFAULT_ICON_PATH,
value: stringToBytes(DEFAULT_SNAP_ICON),
},
],
},
];

const snapControllerOptions = getSnapControllerWithEESOptions({
preinstalledSnaps,
rootMessenger,
});
const [snapController] = getSnapControllerWithEES(snapControllerOptions);

expect(snapController.get(MOCK_SNAP_ID)?.hidden).toBe(true);

snapController.destroy();
});

it('authorizes permissions needed for snaps', async () => {
const manifest = getSnapManifest();
const rootMessenger = getControllerMessenger();
Expand Down
13 changes: 12 additions & 1 deletion packages/snaps-controllers/src/snaps/SnapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export interface PreinstalledSnap {
manifest: SnapManifest;
files: PreinstalledSnapFile[];
removable?: boolean;
hidden?: boolean;
}

type SnapRpcHandler = (
Expand Down Expand Up @@ -687,6 +688,7 @@ type SetSnapArgs = Omit<AddSnapArgs, 'location' | 'versionRange'> & {
isUpdate?: boolean;
removable?: boolean;
preinstalled?: boolean;
hidden?: boolean;
};

const defaultState: SnapControllerState = {
Expand Down Expand Up @@ -1083,7 +1085,13 @@ export class SnapController extends BaseController<
}

#handlePreinstalledSnaps(preinstalledSnaps: PreinstalledSnap[]) {
for (const { snapId, manifest, files, removable } of preinstalledSnaps) {
for (const {
snapId,
manifest,
files,
removable,
hidden,
} of preinstalledSnaps) {
const existingSnap = this.get(snapId);
const isAlreadyInstalled = existingSnap !== undefined;
const isUpdate =
Expand Down Expand Up @@ -1152,6 +1160,7 @@ export class SnapController extends BaseController<
origin: 'metamask',
files: filesObject,
removable,
hidden,
preinstalled: true,
});

Expand Down Expand Up @@ -2769,6 +2778,7 @@ export class SnapController extends BaseController<
isUpdate = false,
removable,
preinstalled,
hidden,
} = args;

const {
Expand Down Expand Up @@ -2824,6 +2834,7 @@ export class SnapController extends BaseController<

removable,
preinstalled,
hidden,

id: snapId,
initialConnections: manifest.result.initialConnections,
Expand Down
5 changes: 5 additions & 0 deletions packages/snaps-utils/src/snaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ export type Snap = TruncatedSnap & {
* A lack of specifying this option will be deemed as removable.
*/
removable?: boolean;

/**
* Flag to signal whether this snap should be hidden from the user or not.
*/
hidden?: boolean;
};

export type TruncatedSnapFields =
Expand Down

0 comments on commit 491f8fb

Please sign in to comment.