Skip to content

Commit 9d44945

Browse files
committed
use viem signature verification
1 parent 64a1793 commit 9d44945

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

examples/react-components/src/app/dashboard/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,12 @@ export default function Dashboard() {
451451
}
452452
};
453453

454-
const handleVerify = () => {
454+
const handleVerify = async () => {
455455
if (!signature) return;
456456
const addressType = selectedAccount?.startsWith("0x") ? "ETH" : "SOL";
457457
const verificationPassed =
458458
addressType === "ETH"
459-
? verifyEthSignatureWithAddress(
459+
? await verifyEthSignatureWithAddress(
460460
messageToSign,
461461
signature.r,
462462
signature.s,

examples/react-components/src/app/utils.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { PublicKey, PublicKeyInitData } from "@solana/web3.js";
1+
import { PublicKey } from "@solana/web3.js";
22
import nacl from "tweetnacl";
33
import { Buffer } from "buffer";
44

5-
import { hashMessage, keccak256, recoverAddress, toUtf8Bytes } from "ethers";
5+
import { recoverMessageAddress, keccak256, stringToHex } from "viem";
66

77
/**
88
* Verifies an Ethereum signature and returns the address it was signed with.
@@ -11,22 +11,28 @@ import { hashMessage, keccak256, recoverAddress, toUtf8Bytes } from "ethers";
1111
* @param {string} s - The s value of the signature.
1212
* @param {string} v - The v value of the signature.
1313
* @param {string} address - The Ethereum address of the signer.
14-
* @returns {boolean} - The recovered Ethereum address.
14+
* @returns {Promise<boolean>} - The recovered Ethereum address.
1515
*/
16-
export function verifyEthSignatureWithAddress(
16+
export async function verifyEthSignatureWithAddress(
1717
message: string,
1818
r: string,
1919
s: string,
2020
v: string,
2121
address: string,
22-
): boolean {
22+
): Promise<boolean> {
2323
try {
2424
// Construct the full signature
25-
const signature = `0x${r}${s}${v === "00" ? "1b" : "1c"}`; // 1b/1c corresponds to v for Ethereum
26-
const hashedMessage = keccak256(toUtf8Bytes(message));
25+
const signature: `0x${string}` = `0x${r}${s}${v === "00" ? "1b" : "1c"}`; // 1b/1c corresponds to v for Ethereum
26+
const hashedMessage = keccak256(stringToHex(message));
2727

2828
// Recover the address from the signature
29-
return address == recoverAddress(hashedMessage, signature);
29+
return (
30+
address ==
31+
(await recoverMessageAddress({
32+
message: hashedMessage,
33+
signature,
34+
}))
35+
);
3036
} catch (error) {
3137
console.error("Ethereum signature verification failed:", error);
3238
return false;

examples/react-wallet-kit/src/components/demo/DemoPanel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ export default function DemoPanel() {
332332
const verificationPassed =
333333
selectedWalletAccount.addressFormat ===
334334
"ADDRESS_FORMAT_ETHEREUM"
335-
? verifyEthSignatureWithAddress(
335+
? await verifyEthSignatureWithAddress(
336336
messageToSign,
337337
res.r,
338338
res.s,

examples/react-wallet-kit/src/utils.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { PublicKey } from "@solana/web3.js";
33
import nacl from "tweetnacl";
44
import { Buffer } from "buffer";
55

6-
import { hashMessage, recoverAddress } from "ethers";
6+
import { hashMessage } from "ethers";
7+
import { recoverMessageAddress } from "viem";
78

89
// Custom hook to get the current screen size
910
export function useScreenSize() {
@@ -231,23 +232,25 @@ function oklchToHex({ L, C, h }: { L: number; C: number; h: number }) {
231232
* @param {string} address - The Ethereum address of the signer.
232233
* @returns {boolean} - The recovered Ethereum address.
233234
*/
234-
export function verifyEthSignatureWithAddress(
235+
export async function verifyEthSignatureWithAddress(
235236
message: string,
236237
r: string,
237238
s: string,
238239
v: string,
239240
address: string,
240-
): boolean {
241+
): Promise<boolean> {
241242
try {
242243
// Construct the full signature
243-
const signature = `0x${r}${s}${v}`;
244+
const signature: `0x${string}` = `0x${r}${s}${v}`;
244245

245246
const hashedMessage = hashMessage(message);
246247

247248
// Recover the address from the signature
248249
return (
249250
address.toLowerCase() ===
250-
recoverAddress(hashedMessage, signature).toLowerCase()
251+
(
252+
await recoverMessageAddress({ message: hashedMessage, signature })
253+
).toLowerCase()
251254
);
252255
} catch (error) {
253256
console.error("Ethereum signature verification failed:", error);

0 commit comments

Comments
 (0)