Skip to content

Commit

Permalink
Merge branch 'main' into chore/improve-react-connected-hook-behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherferreira9 authored Jan 9, 2025
2 parents ae94b02 + 4151089 commit 97516ee
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
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 @@ export async function setupExtensionPreferences(instance: MetaMaskSDK) {

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

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(
`[MetaMaskSDK: setupExtensionPreferences()] error terminating on permissions revoked`,
error,
);
}
}
}
},
);
Expand Down

0 comments on commit 97516ee

Please sign in to comment.