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:
parent
c93ca10827
commit
54e7a96280
@ -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,
|
||||||
|
@ -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);
|
||||||
|
@ -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,
|
||||||
|
Loading…
Reference in New Issue
Block a user