Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/react-components/src/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,12 @@ export default function Dashboard() {
}
};

const handleVerify = () => {
const handleVerify = async () => {
if (!signature) return;
const addressType = selectedAccount?.startsWith("0x") ? "ETH" : "SOL";
const verificationPassed =
addressType === "ETH"
? verifyEthSignatureWithAddress(
? await verifyEthSignatureWithAddress(
messageToSign,
signature.r,
signature.s,
Expand Down
22 changes: 14 additions & 8 deletions examples/react-components/src/app/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PublicKey, PublicKeyInitData } from "@solana/web3.js";
import { PublicKey } from "@solana/web3.js";
import nacl from "tweetnacl";
import { Buffer } from "buffer";

import { hashMessage, keccak256, recoverAddress, toUtf8Bytes } from "ethers";
import { recoverMessageAddress, keccak256, stringToHex } from "viem";

/**
* Verifies an Ethereum signature and returns the address it was signed with.
Expand All @@ -11,22 +11,28 @@ import { hashMessage, keccak256, recoverAddress, toUtf8Bytes } from "ethers";
* @param {string} s - The s value of the signature.
* @param {string} v - The v value of the signature.
* @param {string} address - The Ethereum address of the signer.
* @returns {boolean} - The recovered Ethereum address.
* @returns {Promise<boolean>} - The recovered Ethereum address.
*/
export function verifyEthSignatureWithAddress(
export async function verifyEthSignatureWithAddress(
message: string,
r: string,
s: string,
v: string,
address: string,
): boolean {
): Promise<boolean> {
try {
// Construct the full signature
const signature = `0x${r}${s}${v === "00" ? "1b" : "1c"}`; // 1b/1c corresponds to v for Ethereum
const hashedMessage = keccak256(toUtf8Bytes(message));
const signature: `0x${string}` = `0x${r}${s}${v === "00" ? "1b" : "1c"}`; // 1b/1c corresponds to v for Ethereum
const hashedMessage = keccak256(stringToHex(message));

// Recover the address from the signature
return address == recoverAddress(hashedMessage, signature);
return (
address ==
(await recoverMessageAddress({
message: hashedMessage,
signature,
}))
);
} catch (error) {
console.error("Ethereum signature verification failed:", error);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export default function DemoPanel() {
const verificationPassed =
selectedWalletAccount.addressFormat ===
"ADDRESS_FORMAT_ETHEREUM"
? verifyEthSignatureWithAddress(
? await verifyEthSignatureWithAddress(
messageToSign,
res.r,
res.s,
Expand Down
13 changes: 8 additions & 5 deletions examples/react-wallet-kit/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { PublicKey } from "@solana/web3.js";
import nacl from "tweetnacl";
import { Buffer } from "buffer";

import { hashMessage, recoverAddress } from "ethers";
import { hashMessage } from "ethers";
import { recoverMessageAddress } from "viem";

// Custom hook to get the current screen size
export function useScreenSize() {
Expand Down Expand Up @@ -231,23 +232,25 @@ function oklchToHex({ L, C, h }: { L: number; C: number; h: number }) {
* @param {string} address - The Ethereum address of the signer.
* @returns {boolean} - The recovered Ethereum address.
*/
export function verifyEthSignatureWithAddress(
export async function verifyEthSignatureWithAddress(
message: string,
r: string,
s: string,
v: string,
address: string,
): boolean {
): Promise<boolean> {
try {
// Construct the full signature
const signature = `0x${r}${s}${v}`;
const signature: `0x${string}` = `0x${r}${s}${v}`;

const hashedMessage = hashMessage(message);

// Recover the address from the signature
return (
address.toLowerCase() ===
recoverAddress(hashedMessage, signature).toLowerCase()
(
await recoverMessageAddress({ message: hashedMessage, signature })
).toLowerCase()
);
} catch (error) {
console.error("Ethereum signature verification failed:", error);
Expand Down
Loading