From 77520b5dfa8d6c9920ba460e0253b9cdf5e7eff4 Mon Sep 17 00:00:00 2001 From: Boidushya Date: Wed, 28 Jun 2023 14:22:51 +0530 Subject: [PATCH] feat(dapp-v2): adds eth_signTypedData_v4 (#205) * Test: eth_signTypedData_v4 * fix: merged with main for eth_signTypedData_v4 --- dapps/react-dapp-v2/package.json | 4 +- dapps/react-dapp-v2/src/constants/default.ts | 1 + .../src/contexts/JsonRpcContext.tsx | 45 +++++++++++++++++++ dapps/react-dapp-v2/src/pages/index.tsx | 7 +++ dapps/react-dapp-v2/yarn.lock | 2 +- 5 files changed, 56 insertions(+), 3 deletions(-) diff --git a/dapps/react-dapp-v2/package.json b/dapps/react-dapp-v2/package.json index 14af61c..da48d8c 100644 --- a/dapps/react-dapp-v2/package.json +++ b/dapps/react-dapp-v2/package.json @@ -11,9 +11,9 @@ }, "dependencies": { "@celo/wallet-base": "^3.0.1", + "@ethereumjs/tx": "^3.5.0", "@multiversx/sdk-core": "11.5.0", "@multiversx/sdk-wallet": "4.0.0", - "@ethereumjs/tx": "^3.5.0", "@polkadot/util-crypto": "^10.1.2", "@solana/web3.js": "^1.36.0", "@walletconnect/encoding": "^1.0.1", @@ -52,7 +52,7 @@ "@types/styled-components": "^5.1.25", "eslint": "8.21.0", "eslint-config-next": "12.2.4", - "prettier": "^2.7.1", + "prettier": "^2.8.8", "typescript": "^4.7.4" } } diff --git a/dapps/react-dapp-v2/src/constants/default.ts b/dapps/react-dapp-v2/src/constants/default.ts index 3556c8d..09b4ab3 100644 --- a/dapps/react-dapp-v2/src/constants/default.ts +++ b/dapps/react-dapp-v2/src/constants/default.ts @@ -58,6 +58,7 @@ export enum DEFAULT_EIP155_OPTIONAL_METHODS { ETH_SIGN_TRANSACTION = "eth_signTransaction", ETH_SIGN = "eth_sign", ETH_SIGN_TYPED_DATA = "eth_signTypedData", + ETH_SIGN_TYPED_DATA_V4 = "eth_signTypedData_v4", } export enum DEFAULT_EIP_155_EVENTS { diff --git a/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx b/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx index bb6fe12..43753c7 100644 --- a/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx +++ b/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx @@ -73,6 +73,7 @@ interface IContext { testEthSign: TRpcRequestCallback; testSignPersonalMessage: TRpcRequestCallback; testSignTypedData: TRpcRequestCallback; + testSignTypedDatav4: TRpcRequestCallback; }; cosmosRpc: { testSignDirect: TRpcRequestCallback; @@ -420,6 +421,50 @@ export function JsonRpcContextProvider({ rpc.baseURL ); + return { + method: DEFAULT_EIP155_OPTIONAL_METHODS.ETH_SIGN_TYPED_DATA, + address, + valid, + result: signature, + }; + } + ), + testSignTypedDatav4: _createJsonRpcRequestHandler( + async (chainId: string, address: string) => { + const message = JSON.stringify(eip712.example); + console.log("eth_signTypedData_v4"); + + // eth_signTypedData_v4 params + const params = [address, message]; + + // send message + const signature = await client!.request({ + topic: session!.topic, + chainId, + request: { + method: DEFAULT_EIP155_OPTIONAL_METHODS.ETH_SIGN_TYPED_DATA_V4, + params, + }, + }); + + // split chainId + const [namespace, reference] = chainId.split(":"); + const rpc = rpcProvidersByChainId[Number(reference)]; + + if (typeof rpc === "undefined") { + throw new Error( + `Missing rpcProvider definition for chainId: ${chainId}` + ); + } + + const hashedTypedData = hashTypedDataMessage(message); + const valid = await verifySignature( + address, + signature, + hashedTypedData, + rpc.baseURL + ); + return { method: DEFAULT_EIP155_OPTIONAL_METHODS.ETH_SIGN_TYPED_DATA, address, diff --git a/dapps/react-dapp-v2/src/pages/index.tsx b/dapps/react-dapp-v2/src/pages/index.tsx index 04276a5..761675e 100644 --- a/dapps/react-dapp-v2/src/pages/index.tsx +++ b/dapps/react-dapp-v2/src/pages/index.tsx @@ -160,6 +160,13 @@ const Home: NextPage = () => { await ethereumRpc.testSignTypedData(chainId, address); }, }, + [DEFAULT_EIP155_OPTIONAL_METHODS.ETH_SIGN_TYPED_DATA_V4]: { + method: DEFAULT_EIP155_OPTIONAL_METHODS.ETH_SIGN_TYPED_DATA_V4, + callback: async (chainId: string, address: string) => { + openRequestModal(); + await ethereumRpc.testSignTypedDatav4(chainId, address); + }, + }, }; let availableActions: AccountAction[] = []; diff --git a/dapps/react-dapp-v2/yarn.lock b/dapps/react-dapp-v2/yarn.lock index 6f5001c..dd6f33d 100644 --- a/dapps/react-dapp-v2/yarn.lock +++ b/dapps/react-dapp-v2/yarn.lock @@ -11359,7 +11359,7 @@ prepend-http@^1.0.0: resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-1.0.4.tgz#d4f4562b0ce3696e41ac52d0e002e57a635dc6dc" integrity sha512-PhmXi5XmoyKw1Un4E+opM2KcsJInDvKyuOumcjjw3waw86ZNjHwVUOOWLc4bCzLdcKNaWBH9e99sbWzDQsVaYg== -prettier@^2.7.1: +prettier@^2.8.8: version "2.8.8" resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da" integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==