From 4135e169a7ed4021f27ead04bd3d80c0c57757dd Mon Sep 17 00:00:00 2001 From: jxom Date: Sat, 25 May 2024 10:18:30 +1000 Subject: [PATCH] chore: #2306 repro (#2308) * repro * remove .only * imports --- src/utils/signature/verifyTypedData.test.ts | 84 +++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/utils/signature/verifyTypedData.test.ts b/src/utils/signature/verifyTypedData.test.ts index 1f352da3fa..0d970f0464 100644 --- a/src/utils/signature/verifyTypedData.test.ts +++ b/src/utils/signature/verifyTypedData.test.ts @@ -1,9 +1,14 @@ +import type { Address } from 'abitype' import { expect, test } from 'vitest' import { accounts, typedData } from '~test/src/constants.js' +import { anvilMainnet } from '../../../test/src/anvil.js' +import { signTypedData } from '../../actions/index.js' import { verifyTypedData } from './verifyTypedData.js' +const client = anvilMainnet.getClient() + test('default', async () => { expect( await verifyTypedData({ @@ -25,3 +30,82 @@ test('default', async () => { }), ).toBeTruthy() }) + +test('https://github.com/wevm/viem/issues/2306', async () => { + const typedData = (address: Address) => + ({ + types: { + WalletData: [{ name: 'address', type: 'address' }], + }, + message: { + address, + }, + primaryType: 'WalletData', + }) as const + + const address = '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266' + const nonChecksumAddress = address.toLowerCase() as `0x${string}` + const invalidChecksumAddress = address.replace('f', 'F') as `0x${string}` + + // ✅ Successfully signs with valid checksum address. + const signature = await signTypedData(client, { + ...typedData(address), + account: address, + }) + + // ✅ Successfully signs with non-checksum address. + const signature_2 = await signTypedData(client, { + ...typedData(nonChecksumAddress), + account: nonChecksumAddress, + }) + expect(signature_2).toEqual(signature) + + // ❌ Throws when invalid checksum address provided. + await expect(() => + signTypedData(client, { + ...typedData(invalidChecksumAddress), + account: invalidChecksumAddress, + }), + ).rejects.toMatchInlineSnapshot(` + [InvalidAddressError: Address "0xF39Fd6e51aad88F6F4ce6aB8827279cffFb92266" is invalid. + + - Address must be a hex value of 20 bytes (40 hex characters). + - Address must match its checksum counterpart. + + Version: viem@1.0.2] + `) + + // ✅ Successfully verifies with valid checksum address. + expect( + await verifyTypedData({ + ...typedData(address), + signature, + address: address, + }), + ).toBeTruthy() + + // ✅ Successfully verifies with non-checksum address. + expect( + await verifyTypedData({ + ...typedData(nonChecksumAddress), + signature, + address: nonChecksumAddress, + }), + ).toBeTruthy() + + // ❌ Throws when invalid checksum address provided. + await expect(() => + verifyTypedData({ + ...typedData(invalidChecksumAddress), + signature, + address: invalidChecksumAddress, + }), + ).rejects.toMatchInlineSnapshot(` + [InvalidAddressError: Address "0xF39Fd6e51aad88F6F4ce6aB8827279cffFb92266" is invalid. + + - Address must be a hex value of 20 bytes (40 hex characters). + - Address must match its checksum counterpart. + + Version: viem@1.0.2] + `) +})