Skip to content

Commit da0d581

Browse files
authored
feat: Unblock wallet_switchEthereumChain (#2634)
This removes `wallet_switchEthereumChain` from the `BLOCKED_RPC_METHODS` list. Closes MetaMask/MetaMask-planning#2938. Closes #2654. Blocked by: - MetaMask/core#4602
1 parent 2dcaecd commit da0d581

File tree

16 files changed

+571
-14
lines changed

16 files changed

+571
-14
lines changed

packages/examples/packages/ethereum-provider/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "EBjcd8iJ6rPwGXcEmAhWGJWHh/m0RV8X+eid/HIUjEY=",
10+
"shasum": "cKFOaKJv2NJ7UOWRzhpP1fIq40SPyjhd1sG42AceRBY=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/ethereum-provider/src/index.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,23 @@ import {
1010
hexToNumber,
1111
} from '@metamask/utils';
1212

13-
import type { PersonalSignParams, SignTypedDataParams } from './types';
13+
import type {
14+
BaseParams,
15+
PersonalSignParams,
16+
SignTypedDataParams,
17+
} from './types';
18+
19+
/**
20+
* Set the active Ethereum chain for the Snap.
21+
*
22+
* @param chainId - The chain ID to switch to.
23+
*/
24+
async function switchChain(chainId: Hex) {
25+
await ethereum.request({
26+
method: 'wallet_switchEthereumChain',
27+
params: [{ chainId }],
28+
});
29+
}
1430

1531
/**
1632
* Get the current gas price using the `ethereum` global. This is essentially
@@ -207,6 +223,9 @@ async function signTypedData(message: string, from: string) {
207223
* @see https://docs.metamask.io/snaps/reference/rpc-api/#wallet_invokesnap
208224
*/
209225
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
226+
const { chainId = '0x1' } = (request.params as BaseParams) ?? {};
227+
await switchChain(chainId);
228+
210229
switch (request.method) {
211230
case 'getGasPrice':
212231
return await getGasPrice();

packages/examples/packages/ethereum-provider/src/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
export type PersonalSignParams = {
1+
import type { Hex } from '@metamask/utils';
2+
3+
export type BaseParams = {
4+
chainId?: Hex;
5+
};
6+
7+
export type PersonalSignParams = BaseParams & {
28
message: string;
39
};
410

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"branches": 93.64,
2+
"branches": 93.67,
33
"functions": 98.16,
4-
"lines": 98.5,
4+
"lines": 98.51,
55
"statements": 98.34
66
}

0 commit comments

Comments
 (0)