Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle initial connections for preinstalled Snaps #2591

Merged
merged 2 commits into from
Jul 19, 2024
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
2 changes: 1 addition & 1 deletion packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"branches": 92.46,
"branches": 92.5,
"functions": 96.91,
"lines": 98.01,
"statements": 97.7
Expand Down
75 changes: 75 additions & 0 deletions packages/snaps-controllers/src/snaps/SnapController.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1848,7 +1848,7 @@
});

// This isn't stable in CI unfortunately
it.skip('throws if the Snap is terminated while executing', async () => {

Check warning on line 1851 in packages/snaps-controllers/src/snaps/SnapController.test.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (@metamask/snaps-controllers)

Disabled test
const { manifest, sourceCode, svgIcon } =
await getMockSnapFilesWithUpdatedChecksum({
sourceCode: `
Expand Down Expand Up @@ -4695,6 +4695,81 @@
snapController.destroy();
});

it('supports preinstalled snaps with initial connections', async () => {
const rootMessenger = getControllerMessenger();
jest.spyOn(rootMessenger, 'call');

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

const initialConnections = {
'npm:filsnap': {},
'https://snaps.metamask.io': {},
};

const preinstalledSnaps = [
{
snapId: MOCK_SNAP_ID,
manifest: getSnapManifest({
initialConnections,
}),
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);

const approvedPermissions = {
[WALLET_SNAP_PERMISSION_KEY]: {
caveats: [
{
type: SnapCaveatType.SnapIds,
value: {
[MOCK_SNAP_ID]: {},
},
},
],
},
};

// After install the snap should have permissions
rootMessenger.registerActionHandler(
'PermissionController:getPermissions',
() => MOCK_SNAP_PERMISSIONS,
);

expect(rootMessenger.call).toHaveBeenCalledWith(
'PermissionController:grantPermissions',
{ approvedPermissions, subject: { origin: 'npm:filsnap' } },
);

expect(rootMessenger.call).toHaveBeenCalledWith(
'PermissionController:grantPermissions',
{
approvedPermissions,
subject: { origin: 'https://snaps.metamask.io' },
},
);

snapController.destroy();
});

it('supports preinstalled snaps when Snap installation is disabled', async () => {
const rootMessenger = getControllerMessenger();
jest.spyOn(rootMessenger, 'call');
Expand Down
8 changes: 8 additions & 0 deletions packages/snaps-controllers/src/snaps/SnapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,14 @@ export class SnapController extends BaseController<

this.#updatePermissions({ snapId, newPermissions, unusedPermissions });

if (manifest.initialConnections) {
this.#handleInitialConnections(
snapId,
existingSnap?.initialConnections ?? null,
manifest.initialConnections,
);
}

// Set status
this.update((state) => {
state.snaps[snapId].status = SnapStatus.Stopped;
Expand Down
Loading