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

chore: call getPermissions on accountsChanged when using extension #1185

Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { TrackingEvents } from '@metamask/sdk-communication-layer';
import { MetaMaskSDK } from '../../../sdk';
import { SDKProvider } from '../../../provider/SDKProvider';
import { getBrowserExtension } from '../../../utils/get-browser-extension';
import { EXTENSION_EVENTS, RPC_METHODS } from '../../../config';
import { setupExtensionPreferences } from './setupExtensionPreferences';

jest.mock('../../../utils/get-browser-extension');
Expand Down Expand Up @@ -81,4 +83,36 @@ describe('setupExtensionPreferences', () => {
event: TrackingEvents.SDK_USE_EXTENSION,
});
});

it('should terminate instance if permissions are revoked', async () => {
const mockTerminate = jest.fn().mockResolvedValue(undefined);
const mockRequest = jest.fn().mockResolvedValue([]);

instance.getProvider = jest.fn().mockReturnValue({
request: mockRequest,
}) as unknown as () => SDKProvider;

instance.terminate = mockTerminate;
instance.extensionActive = true; // Ensure extension is active

const mockBrowserExtension = {
on: jest.fn((event: string, callback: (accounts: string[]) => void) => {
if (event === EXTENSION_EVENTS.ACCOUNTS_CHANGED) {
// Directly call the callback to simulate the event
callback([]);
}
}),
};

(getBrowserExtension as jest.Mock).mockReturnValue(mockBrowserExtension);

await setupExtensionPreferences(instance);

expect(mockRequest).toHaveBeenCalledWith({
method: RPC_METHODS.WALLET_GETPERMISSIONS,
params: [],
});

expect(mockTerminate).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { TrackingEvents } from '@metamask/sdk-communication-layer';
import { logger } from '../../../utils/logger';
import { SDKProvider } from '../../../provider/SDKProvider';
import { EXTENSION_EVENTS, STORAGE_PROVIDER_TYPE } from '../../../config';
import {
EXTENSION_EVENTS,
RPC_METHODS,
STORAGE_PROVIDER_TYPE,
} from '../../../config';
import { MetaMaskSDK } from '../../../sdk';
import { getBrowserExtension } from '../../../utils/get-browser-extension';
import { Ethereum } from '../../Ethereum';
Expand Down Expand Up @@ -64,7 +68,7 @@

metamaskBrowserExtension.on(
EXTENSION_EVENTS.ACCOUNTS_CHANGED,
(accounts) => {
async (accounts) => {
logger(
`[MetaMaskSDK: setupExtensionPreferences()] PROPAGATE accountsChanged accounts=${accounts}`,
);
Expand All @@ -80,9 +84,28 @@
}

if (isExtensionActive && (accounts as string[])?.length === 0) {
logger(
`[MetaMaskSDK: setupExtensionPreferences()] permissions were revoked on extension or extension was locked`,
);
const getPermissionsResponse = await instance
.getProvider()
?.request({
method: RPC_METHODS.WALLET_GETPERMISSIONS,
params: [],
});

const permissions = getPermissionsResponse as {
caveats: { type: string; value: string[] }[];
parentCapability: string;
}[];

if (permissions.length === 0) {
try {
await instance.terminate();
} catch (error) {
logger(

Check warning on line 103 in packages/sdk/src/services/MetaMaskSDK/InitializerManager/setupExtensionPreferences.ts

View check run for this annotation

Codecov / codecov/patch

packages/sdk/src/services/MetaMaskSDK/InitializerManager/setupExtensionPreferences.ts#L103

Added line #L103 was not covered by tests
`[MetaMaskSDK: setupExtensionPreferences()] error terminating on permissions revoked`,
error,
);
}
}
}
},
);
Expand Down
Loading