fix: make react-dapp-v2-with-ethers support non-custodial wallet (#172)

Co-authored-by: Ben Kremer <contact@bkrem.dev>
This commit is contained in:
Basheer Alsammarraie 2023-06-09 16:52:03 +03:00 committed by GitHub
parent c93ca10827
commit 54e7a96280
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 14 deletions

View File

@ -195,7 +195,7 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
], ],
chains: [`eip155:${chainId}`], chains: [`eip155:${chainId}`],
events: ["chainChanged", "accountsChanged"], events: ["chainChanged", "accountsChanged"],
rpcMap: {}, rpcMap: {chainId: `https://rpc.walletconnect.com?chainId=eip155:${chainId}&projectId=${DEFAULT_PROJECT_ID}`,},
}, },
}, },
pairingTopic: pairing?.topic, pairingTopic: pairing?.topic,

View File

@ -138,9 +138,8 @@ export async function verifySignature(
address: string, address: string,
sig: string, sig: string,
hash: string, hash: string,
rpcUrl: string, provider: providers.Web3Provider,
): Promise<boolean> { ): Promise<boolean> {
const provider = new providers.JsonRpcProvider(rpcUrl);
const bytecode = await provider.getCode(address); const bytecode = await provider.getCode(address);
if (!bytecode || bytecode === "0x" || bytecode === "0x0" || bytecode === "0x00") { if (!bytecode || bytecode === "0x" || bytecode === "0x0" || bytecode === "0x00") {
const signer = recoverAddress(sig, hash); const signer = recoverAddress(sig, hash);

View File

@ -16,7 +16,10 @@ import {
eip712, eip712,
formatTestTransaction, formatTestTransaction,
getLocalStorageTestnetFlag, getLocalStorageTestnetFlag,
hashPersonalMessage,
hashTypedDataMessage,
setLocaleStorageTestnetFlag, setLocaleStorageTestnetFlag,
verifySignature,
} from "./../helpers"; } from "./../helpers";
import Toggle from "./../components/Toggle"; import Toggle from "./../components/Toggle";
import RequestModal from "./../modals/RequestModal"; import RequestModal from "./../modals/RequestModal";
@ -153,11 +156,13 @@ const Home: NextPage = () => {
if (!web3Provider) { if (!web3Provider) {
throw new Error("web3Provider not connected"); throw new Error("web3Provider not connected");
} }
const msg = "hello world"; const msg = "hello world";
const hexMsg = encoding.utf8ToHex(msg, true); const hexMsg = encoding.utf8ToHex(msg, true);
const [address] = await web3Provider.listAccounts(); const [address] = await web3Provider.listAccounts();
const signature = await web3Provider.send("personal_sign", [hexMsg, address]); const signature = await web3Provider.send("personal_sign", [hexMsg, address]);
const valid = verifyEip155MessageSignature(msg, signature, address); const hashMsg = hashPersonalMessage(msg)
const valid = await verifySignature(address, signature, hashMsg, web3Provider);
return { return {
method: "personal_sign", method: "personal_sign",
address, address,
@ -198,16 +203,13 @@ const Home: NextPage = () => {
// send message // send message
const signature = await web3Provider.send("eth_signTypedData", params); const signature = await web3Provider.send("eth_signTypedData", params);
// Separate `EIP712Domain` type from remaining types to verify, otherwise `ethers.utils.verifyTypedData` const hashedTypedData = hashTypedDataMessage(message);
// will throw due to "unused" `EIP712Domain` type. const valid = await verifySignature(
// See: https://github.com/ethers-io/ethers.js/issues/687#issuecomment-714069471 address,
const { EIP712Domain, ...nonDomainTypes }: Record<string, TypedDataField[]> = signature,
eip712.example.types; hashedTypedData,
web3Provider
const valid = );
utils
.verifyTypedData(eip712.example.domain, nonDomainTypes, eip712.example.message, signature)
.toLowerCase() === address.toLowerCase();
return { return {
method: "eth_signTypedData", method: "eth_signTypedData",
address, address,