Skip to content

Commit 030ebee

Browse files
fix: Use proper chain ID for signTypedData example (#3013)
When writing the signTypedData example I had based the code on some example code that hardcoded the chain ID. Instead we should use the current chain ID, so we can test the Snap on any chain.
1 parent 493eedc commit 030ebee

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
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": "T/7hin+Vhg0SdK8RxXvonPhkTRe/luTzGwqfWrH/8MQ=",
10+
"shasum": "1QLc9RhVgpwxBu8FjPo5kZY61mpNoNAfkiAatq99Nc4=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import {
33
type OnRpcRequestHandler,
44
} from '@metamask/snaps-sdk';
55
import type { Hex } from '@metamask/utils';
6-
import { assert, stringToBytes, bytesToHex } from '@metamask/utils';
6+
import {
7+
assert,
8+
stringToBytes,
9+
bytesToHex,
10+
hexToNumber,
11+
} from '@metamask/utils';
712

813
import type { PersonalSignParams, SignTypedDataParams } from './types';
914

@@ -107,6 +112,9 @@ async function personalSign(message: string, from: string) {
107112
* @see https://docs.metamask.io/wallet/concepts/signing-methods/#eth_signtypeddata_v4
108113
*/
109114
async function signTypedData(message: string, from: string) {
115+
const chainId = (await ethereum.request<string>({
116+
method: 'eth_chainId',
117+
})) as string;
110118
const signature = await ethereum.request<Hex>({
111119
method: 'eth_signTypedData_v4',
112120
params: [
@@ -160,7 +168,7 @@ async function signTypedData(message: string, from: string) {
160168
domain: {
161169
name: 'Ether Mail',
162170
version: '1',
163-
chainId: 1,
171+
chainId: hexToNumber(chainId),
164172
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
165173
},
166174
message: {

0 commit comments

Comments
 (0)