Skip to content

Commit

Permalink
chore: #2306 repro (#2308)
Browse files Browse the repository at this point in the history
* repro

* remove .only

* imports
  • Loading branch information
jxom committed May 25, 2024
1 parent 288aacf commit 4135e16
Showing 1 changed file with 84 additions and 0 deletions.
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]
`)
})

0 comments on commit 4135e16

Please sign in to comment.