Skip to content

fix: reject compressed public keys in publicKeyToAddress - #5509

Closed
erkancamli wants to merge 1 commit into
wevm:mainfrom
erkancamli:fix/public-key-to-address-compressed
Closed

erkancamli wants to merge 1 commit into
wevm:mainfrom
erkancamli:fix/public-key-to-address-compressed

Conversation

@erkancamli

Copy link
Copy Markdown
Contributor

publicKeyToAddress drops the first byte of the key and hashes the rest. For a compressed public key (33 bytes, which secp256k1.getPublicKey returns by default and BIP-32 extended keys carry) that hashes only the x-coordinate, and it returns an address no key controls, with no error:

const compressed = toHex(secp256k1.getPublicKey(privateKey)) // 0x02... or 0x03..., 33 bytes
publicKeyToAddress(compressed) // a valid-looking address that is not privateKeyToAddress(privateKey)

ox rejects compressed keys in Address.fromPublicKey (wevm/ox#624). This does the same: anything other than an uncompressed key (65 bytes with the 0x04 prefix) throws the new InvalidPublicKeyError, exported next to publicKeyToAddress from viem/accounts and viem/utils and added to the errors glossary. Uncompressed keys produce the same addresses as before. I used a length and prefix check rather than importing ox's PublicKey, which would add about 2.4 kB gzip to privateKeyToAccount.

Tests

publicKeyToAddress.test.ts covers a compressed key and a 64-byte key without the 0x04 prefix; they fail on main (8d3f29e) and pass with the change. The export snapshots are updated. biome and tsc are clean. The new error puts import * from 'viem' (esm) just over its size limit, so that limit goes from 91.5 kB to 92 kB; the cjs bundle stays under its limit. A changeset is included.

`publicKeyToAddress` dropped the first byte of the public key and hashed
the rest, so a compressed public key (33 bytes, as `secp256k1.getPublicKey`
returns by default and BIP-32 extended keys carry) hashed only the
x-coordinate and returned an address no key controls, without an error.

The address is derived from both coordinates, so the public key must be
uncompressed (65 bytes with a `0x04` prefix); anything else now throws
`InvalidPublicKeyError`, as ox's `Address.fromPublicKey` rejects
compressed public keys.

The new error pushes `import * from 'viem' (esm)` just over its budget,
so that limit goes from 91.5 kB to 92 kB.
@vercel

vercel Bot commented Nov 13, 2026

Copy link
Copy Markdown

@erkancamli is attempting to deploy a commit to the Wevm Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Nov 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 654026c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

jxom commented Nov 15, 2026

Copy link
Copy Markdown
Member

Thanks @erkancamli! Superseded by #5515, which keeps your commit and adds a small follow-up (namespaced error name, one-sentence changeset).

@jxom jxom closed this Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants