Skip to content

Commit

Permalink
fix: getAddress regression (#2051)
Browse files Browse the repository at this point in the history
* fix: `getAddress` regression

* Update violet-bugs-burn.md
  • Loading branch information
jxom committed Mar 31, 2024
1 parent 85c3695 commit 15be6ea
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-bugs-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed regression where `getAddress` threw an error for non-checksum addresses instead of converting to a valid checksum address.
13 changes: 3 additions & 10 deletions src/utils/address/getAddress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,13 @@ test('checksums address', () => {
expect(
getAddress('0x3599689e6292b81b2d85451025146515070129bb', 30),
).toMatchInlineSnapshot('"0x3599689E6292B81B2D85451025146515070129Bb"')
expect(
getAddress('0xa5cc3c03994db5b0d9a5eEdD10Cabab0813678ac'),
).toMatchInlineSnapshot(`"0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC"`)
})

describe('errors', () => {
test('invalid address', () => {
expect(() =>
getAddress('0xa5cc3c03994db5b0d9a5eEdD10Cabab0813678ac'),
).toThrowErrorMatchingInlineSnapshot(`
[InvalidAddressError: Address "0xa5cc3c03994db5b0d9a5eEdD10Cabab0813678ac" 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]
`)
expect(() =>
getAddress('0xa5cc3c03994db5b0d9a5eEdD10Cabab0813678az'),
).toThrowErrorMatchingInlineSnapshot(`
Expand Down
3 changes: 2 additions & 1 deletion src/utils/address/getAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type GetAddressErrorType =
| ErrorType

export function getAddress(address: string, chainId?: number): Address {
if (!isAddress(address)) throw new InvalidAddressError({ address })
if (!isAddress(address, { strict: false }))
throw new InvalidAddressError({ address })
return checksumAddress(address, chainId)
}
4 changes: 3 additions & 1 deletion src/utils/address/isAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ export type IsAddressErrorType = ErrorType

export function isAddress(
address: string,
{ strict = true }: IsAddressOptions = {},
options?: IsAddressOptions | undefined,
): address is Address {
const { strict = true } = options ?? {}

if (isAddressCache.has(address)) return isAddressCache.get(address)!

const result = (() => {
Expand Down

0 comments on commit 15be6ea

Please sign in to comment.