-
-
Notifications
You must be signed in to change notification settings - Fork 740
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* repro * remove .only * imports
- Loading branch information
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
|
@@ -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: [email protected]] | ||
`) | ||
|
||
// ✅ 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: [email protected]] | ||
`) | ||
}) |