Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: #2306 repro #2308

Merged
merged 3 commits into from
May 25, 2024
Merged
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
84 changes: 84 additions & 0 deletions src/utils/signature/verifyTypedData.test.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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]
`)
})
Loading