Skip to content

Commit 3c95dfd

Browse files
committed
Use selected network controller for Snaps
1 parent af90e67 commit 3c95dfd

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

packages/selected-network-controller/src/SelectedNetworkController.ts

+2-15
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ const stateMetadata = {
2626

2727
const getDefaultState = () => ({ domains: {} });
2828

29-
// npm and local are currently the only valid prefixes for snap domains
30-
// TODO: eventually we maybe want to pull this in from snaps-utils to ensure it stays in sync
31-
// For now it seems like overkill to add a dependency for this one constant
32-
// https://github.com/MetaMask/snaps/blob/2beee7803bfe9e540788a3558b546b9f55dc3cb4/packages/snaps-utils/src/types.ts#L120
33-
const snapsPrefixes = ['npm:', 'local:'] as const;
34-
3529
export type Domain = string;
3630

3731
export const METAMASK_DOMAIN = 'metamask' as const;
@@ -357,10 +351,6 @@ export class SelectedNetworkController extends BaseController<
357351
);
358352
}
359353

360-
if (snapsPrefixes.some((prefix) => domain.startsWith(prefix))) {
361-
return;
362-
}
363-
364354
if (!this.#domainHasPermissions(domain)) {
365355
throw new Error(
366356
'NetworkClientId for domain cannot be called with a domain that has not yet been granted permissions',
@@ -386,11 +376,8 @@ export class SelectedNetworkController extends BaseController<
386376
* @returns The proxy and block tracker proxies.
387377
*/
388378
getProviderAndBlockTracker(domain: Domain): NetworkProxy {
389-
// If the domain is 'metamask' or a snap, return the NetworkController's globally selected network client proxy
390-
if (
391-
domain === METAMASK_DOMAIN ||
392-
snapsPrefixes.some((prefix) => domain.startsWith(prefix))
393-
) {
379+
// If the domain is 'metamask', return the NetworkController's globally selected network client proxy
380+
if (domain === METAMASK_DOMAIN) {
394381
const networkClient = this.messagingSystem.call(
395382
'NetworkController:getSelectedNetworkClient',
396383
);

packages/selected-network-controller/tests/SelectedNetworkController.test.ts

+28-15
Original file line numberDiff line numberDiff line change
@@ -525,33 +525,46 @@ describe('SelectedNetworkController', () => {
525525
});
526526

527527
describe('when the requesting domain is a snap (starts with "npm:" or "local:"', () => {
528-
it('skips setting the networkClientId for the passed in domain', () => {
528+
it('sets the networkClientId for the passed in snap ID', () => {
529529
const { controller, mockHasPermissions } = setup({
530530
state: { domains: {} },
531531
useRequestQueuePreference: true,
532532
});
533533
mockHasPermissions.mockReturnValue(true);
534-
const snapDomainOne = 'npm:@metamask/bip32-example-snap';
535-
const snapDomainTwo = 'local:@metamask/bip32-example-snap';
536-
const nonSnapDomain = 'example.com';
534+
const domain = 'npm:foo-snap';
537535
const networkClientId = 'network1';
536+
controller.setNetworkClientIdForDomain(domain, networkClientId);
537+
expect(controller.state.domains[domain]).toBe(networkClientId);
538+
});
538539

540+
it('updates the provider and block tracker proxy when they already exist for the snap ID', () => {
541+
const { controller, mockProviderProxy, mockHasPermissions } = setup({
542+
state: { domains: {} },
543+
useRequestQueuePreference: true,
544+
});
545+
mockHasPermissions.mockReturnValue(true);
546+
const initialNetworkClientId = '123';
547+
548+
// creates the proxy for the new domain
539549
controller.setNetworkClientIdForDomain(
540-
nonSnapDomain,
541-
networkClientId,
542-
);
543-
controller.setNetworkClientIdForDomain(
544-
snapDomainOne,
545-
networkClientId,
550+
'npm:foo-snap',
551+
initialNetworkClientId,
546552
);
553+
const newNetworkClientId = 'abc';
554+
555+
expect(mockProviderProxy.setTarget).toHaveBeenCalledTimes(1);
556+
557+
// calls setTarget on the proxy
547558
controller.setNetworkClientIdForDomain(
548-
snapDomainTwo,
549-
networkClientId,
559+
'npm:foo-snap',
560+
newNetworkClientId,
550561
);
551562

552-
expect(controller.state.domains).toStrictEqual({
553-
[nonSnapDomain]: networkClientId,
554-
});
563+
expect(mockProviderProxy.setTarget).toHaveBeenNthCalledWith(
564+
2,
565+
expect.objectContaining({ request: expect.any(Function) }),
566+
);
567+
expect(mockProviderProxy.setTarget).toHaveBeenCalledTimes(2);
555568
});
556569
});
557570

0 commit comments

Comments
 (0)