refactor(dapp-v2): use ethers.utils.verifyMessage

This commit is contained in:
Ben Kremer 2022-03-21 11:47:30 +01:00
parent f1e69504a0
commit 1f3af8763c

View File

@ -7,13 +7,7 @@ import bs58 from "bs58";
import { verifyMessageSignature } from "solana-wallet"; import { verifyMessageSignature } from "solana-wallet";
import { clusterApiUrl, Connection, Keypair, SystemProgram, Transaction } from "@solana/web3.js"; import { clusterApiUrl, Connection, Keypair, SystemProgram, Transaction } from "@solana/web3.js";
import { import { eip712, formatTestTransaction, getLocalStorageTestnetFlag } from "../helpers";
eip712,
formatTestTransaction,
getLocalStorageTestnetFlag,
hashPersonalMessage,
verifySignature,
} from "../helpers";
import { useWalletConnectClient } from "./ClientContext"; import { useWalletConnectClient } from "./ClientContext";
import { import {
DEFAULT_COSMOS_METHODS, DEFAULT_COSMOS_METHODS,
@ -206,7 +200,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
const params = [hexMsg, address]; const params = [hexMsg, address];
// send message // send message
const result: string = await client!.request({ const signature: string = await client!.request({
topic: session!.topic, topic: session!.topic,
chainId, chainId,
request: { request: {
@ -224,18 +218,14 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
throw new Error(`Missing chain data for chainId: ${chainId}`); throw new Error(`Missing chain data for chainId: ${chainId}`);
} }
const rpcUrl = targetChainData.rpc[0]; const valid = utils.verifyMessage(message, signature) === address;
// verify signature
const hash = hashPersonalMessage(message);
const valid = await verifySignature(address, result, hash, rpcUrl);
// format displayed result // format displayed result
return { return {
method: DEFAULT_EIP155_METHODS.PERSONAL_SIGN, method: DEFAULT_EIP155_METHODS.PERSONAL_SIGN,
address, address,
valid, valid,
result, result: signature,
}; };
}, },
), ),
@ -248,7 +238,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
const params = [address, hexMsg]; const params = [address, hexMsg];
// send message // send message
const result: string = await client!.request({ const signature: string = await client!.request({
topic: session!.topic, topic: session!.topic,
chainId, chainId,
request: { request: {
@ -266,18 +256,14 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
throw new Error(`Missing chain data for chainId: ${chainId}`); throw new Error(`Missing chain data for chainId: ${chainId}`);
} }
const rpcUrl = targetChainData.rpc[0]; const valid = utils.verifyMessage(message, signature) === address;
// verify signature
const hash = hashPersonalMessage(message);
const valid = await verifySignature(address, result, hash, rpcUrl);
// format displayed result // format displayed result
return { return {
method: DEFAULT_EIP155_METHODS.ETH_SIGN + " (standard)", method: DEFAULT_EIP155_METHODS.ETH_SIGN + " (standard)",
address, address,
valid, valid,
result, result: signature,
}; };
}), }),
testSignTypedData: _createJsonRpcRequestHandler(async (chainId: string, address: string) => { testSignTypedData: _createJsonRpcRequestHandler(async (chainId: string, address: string) => {