Skip to content

Commit

Permalink
Require Infura project ID
Browse files Browse the repository at this point in the history
The network controller now requires an Infura project ID to be provided
upon construction. This brings the network controller more in-line with
the extension. Runtime validation has been added for the sake of making
the merge with the extension network controller easier.

Closes #1201
  • Loading branch information
Gudahtt committed Apr 26, 2023
1 parent 0b59ad9 commit 5cf5f60
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const setupControllers = () => {
allowedActions: [],
});
const network = new NetworkController({
infuraProjectId: 'infura-project-id',
messenger,
trackMetaMetricsEvent: jest.fn(),
});
Expand Down
6 changes: 5 additions & 1 deletion packages/assets-controllers/src/TokenRatesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ describe('TokenRatesController', () => {
});

it('should update all rates', async () => {
new NetworkController({ messenger, trackMetaMetricsEvent: jest.fn() });
new NetworkController({
infuraProjectId: 'infura-project-id',
messenger,
trackMetaMetricsEvent: jest.fn(),
});
const preferences = new PreferencesController();
const tokensController = new TokensController({
onPreferencesStateChange: (listener) => preferences.subscribe(listener),
Expand Down
7 changes: 5 additions & 2 deletions packages/network-controller/src/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export type NetworkControllerMessenger = RestrictedControllerMessenger<
export type NetworkControllerOptions = {
messenger: NetworkControllerMessenger;
trackMetaMetricsEvent: () => void;
infuraProjectId?: string;
infuraProjectId: string;
state?: Partial<NetworkState>;
};

Expand Down Expand Up @@ -213,7 +213,7 @@ export class NetworkController extends BaseControllerV2<
> {
#ethQuery: EthQuery;

#infuraProjectId: string | undefined;
#infuraProjectId: string;

#trackMetaMetricsEvent: (event: MetaMetricsEventPayload) => void;

Expand Down Expand Up @@ -260,6 +260,9 @@ export class NetworkController extends BaseControllerV2<
messenger,
state: { ...defaultState, ...state },
});
if (!infuraProjectId || typeof infuraProjectId !== 'string') {
throw new Error('Invalid Infura project ID');
}
this.#infuraProjectId = infuraProjectId;
this.#trackMetaMetricsEvent = trackMetaMetricsEvent;
this.messagingSystem.registerActionHandler(
Expand Down
34 changes: 30 additions & 4 deletions packages/network-controller/tests/NetworkController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,35 @@ describe('NetworkController', () => {
},
);
});

it('throws if the infura project ID is missing', async () => {
const messenger = buildMessenger();

expect(
() =>
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
new NetworkController({
messenger,
trackMetaMetricsEvent: jest.fn(),
}),
).toThrow('Invalid Infura project ID');
});

it('throws if the infura project ID is not a string', async () => {
const messenger = buildMessenger();

expect(
() =>
new NetworkController({
messenger,
trackMetaMetricsEvent: jest.fn(),
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
infuraProjectId: 10,
}),
).toThrow('Invalid Infura project ID');
});
});

describe('initializeProvider', () => {
Expand Down Expand Up @@ -235,7 +264,6 @@ describe('NetworkController', () => {
type: networkType,
}),
},
infuraProjectId: 'infura-project-id',
},
async ({ controller }) => {
const fakeInfuraProvider = buildFakeInfuraProvider();
Expand Down Expand Up @@ -273,7 +301,6 @@ describe('NetworkController', () => {
type: networkType,
}),
},
infuraProjectId: 'infura-project-id',
},
async ({ controller }) => {
const fakeInfuraProvider = buildFakeInfuraProvider();
Expand Down Expand Up @@ -351,7 +378,6 @@ describe('NetworkController', () => {
type: networkType,
}),
},
infuraProjectId: 'infura-project-id',
},
async ({ controller }) => {
const fakeInfuraProvider = buildFakeInfuraProvider();
Expand Down Expand Up @@ -3325,7 +3351,6 @@ describe('NetworkController', () => {
chainId: '0x9999999',
},
},
infuraProjectId: 'infura-project-id',
},
async ({ controller }) => {
const fakeInfuraProvider = buildFakeInfuraProvider();
Expand Down Expand Up @@ -5673,6 +5698,7 @@ async function withController<ReturnValue>(
const controller = new NetworkController({
messenger,
trackMetaMetricsEvent: jest.fn(),
infuraProjectId: 'infura-project-id',
...rest,
});
try {
Expand Down

0 comments on commit 5cf5f60

Please sign in to comment.