Skip to content

Commit cf2e4a0

Browse files
committed
Add chain ID selector to test-snaps
1 parent 4c34406 commit cf2e4a0

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

packages/test-snaps/src/features/snaps/ethereum-provider/EthereumProvider.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { logError } from '@metamask/snaps-utils';
2+
import { numberToHex } from '@metamask/utils';
23
import type { FunctionComponent } from 'react';
4+
import { useState } from 'react';
35
import { Button, ButtonGroup } from 'react-bootstrap';
46

57
import { useInvokeMutation } from '../../../api';
68
import { Result, Snap } from '../../../components';
79
import { getSnapId } from '../../../utils';
8-
import { SignMessage, SignTypedData } from './components';
10+
import { SignMessage, SignTypedData, SwitchChain } from './components';
911
import {
1012
ETHEREUM_PROVIDER_SNAP_ID,
1113
ETHEREUM_PROVIDER_SNAP_PORT,
@@ -14,11 +16,15 @@ import {
1416

1517
export const EthereumProvider: FunctionComponent = () => {
1618
const [invokeSnap, { isLoading, data, error }] = useInvokeMutation();
19+
const [chainId, setChainId] = useState<number>(1);
1720

1821
const handleSubmit = (method: string) => {
1922
invokeSnap({
2023
snapId: getSnapId(ETHEREUM_PROVIDER_SNAP_ID, ETHEREUM_PROVIDER_SNAP_PORT),
2124
method,
25+
params: {
26+
chainId: numberToHex(chainId),
27+
},
2228
}).catch(logError);
2329
};
2430

@@ -33,6 +39,7 @@ export const EthereumProvider: FunctionComponent = () => {
3339
version={ETHEREUM_PROVIDER_VERSION}
3440
testId="ethereum-provider"
3541
>
42+
<SwitchChain onChange={setChainId} />
3643
<ButtonGroup>
3744
<Button
3845
variant="secondary"
@@ -59,8 +66,8 @@ export const EthereumProvider: FunctionComponent = () => {
5966
{JSON.stringify(error, null, 2)}
6067
</span>
6168
</Result>
62-
<SignMessage />
63-
<SignTypedData />
69+
<SignMessage chainId={chainId} />
70+
<SignTypedData chainId={chainId} />
6471
</Snap>
6572
);
6673
};

packages/test-snaps/src/features/snaps/ethereum-provider/components/SignMessage.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { logError } from '@metamask/snaps-utils';
2+
import { numberToHex } from '@metamask/utils';
23
import type { ChangeEvent, FormEvent, FunctionComponent } from 'react';
34
import { useState } from 'react';
45
import { Button, Form } from 'react-bootstrap';
@@ -11,7 +12,13 @@ import {
1112
ETHEREUM_PROVIDER_SNAP_PORT,
1213
} from '../constants';
1314

14-
export const SignMessage: FunctionComponent = () => {
15+
export type SignMessageProps = {
16+
chainId: number;
17+
};
18+
19+
export const SignMessage: FunctionComponent<SignMessageProps> = ({
20+
chainId,
21+
}) => {
1522
const [message, setMessage] = useState('');
1623
const [invokeSnap, { isLoading, data, error }] = useInvokeMutation();
1724

@@ -27,6 +34,7 @@ export const SignMessage: FunctionComponent = () => {
2734
method: 'personalSign',
2835
params: {
2936
message,
37+
chainId: numberToHex(chainId),
3038
},
3139
}).catch(logError);
3240
};

packages/test-snaps/src/features/snaps/ethereum-provider/components/SignTypedData.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { logError } from '@metamask/snaps-utils';
2+
import { numberToHex } from '@metamask/utils';
23
import type { ChangeEvent, FormEvent, FunctionComponent } from 'react';
34
import { useState } from 'react';
45
import { Button, Form } from 'react-bootstrap';
@@ -11,7 +12,13 @@ import {
1112
ETHEREUM_PROVIDER_SNAP_PORT,
1213
} from '../constants';
1314

14-
export const SignTypedData: FunctionComponent = () => {
15+
export type SignTypedDataProps = {
16+
chainId: number;
17+
};
18+
19+
export const SignTypedData: FunctionComponent<SignTypedDataProps> = ({
20+
chainId,
21+
}) => {
1522
const [message, setMessage] = useState('');
1623
const [invokeSnap, { isLoading, data, error }] = useInvokeMutation();
1724

@@ -27,6 +34,7 @@ export const SignTypedData: FunctionComponent = () => {
2734
method: 'signTypedData',
2835
params: {
2936
message,
37+
chainId: numberToHex(chainId),
3038
},
3139
}).catch(logError);
3240
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { ChangeEvent, FunctionComponent } from 'react';
2+
3+
export type SwitchChainProps = {
4+
onChange: (chainId: number) => void;
5+
};
6+
7+
export const SwitchChain: FunctionComponent<SwitchChainProps> = ({
8+
onChange,
9+
}) => {
10+
const handleChange = (event: ChangeEvent<HTMLSelectElement>) => {
11+
onChange(Number(event.target.value));
12+
};
13+
14+
return (
15+
<>
16+
<h3 className="h5">Select chain</h3>
17+
<select
18+
id="select-chain"
19+
className="form-select mb-3"
20+
onChange={handleChange}
21+
>
22+
<option value="1">Ethereum</option>
23+
<option value="59144">Linea</option>
24+
<option value="11155111">Sepolia</option>
25+
</select>
26+
</>
27+
);
28+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './SignMessage';
22
export * from './SignTypedData';
3+
export * from './SwitchChain';

0 commit comments

Comments
 (0)