Skip to content

feat(crypto): add publicKeyForPeer to bind keys to peer ids - #3667

Merged
tabcat merged 9 commits into
libp2p:mainfrom
tabcat:feat/public-key-for-peer
Oct 17, 2026
Merged

tabcat merged 9 commits into
libp2p:mainfrom
tabcat:feat/public-key-for-peer

Conversation

@tabcat

@tabcat tabcat commented Oct 17, 2026

Copy link
Copy Markdown
Member

Description

Adds publicKeyForPeer(peer, key?, options?) to @libp2p/crypto/keys. It returns a peer's public key that is safe to verify the peer's signatures with:

  • A supplied key (bytes or a PublicKey) is returned only if its multihash equals the peer id's. Otherwise it throws InvalidPublicKeyError, even when the peer id inlines a key.
  • With no supplied key, it returns the key inlined in the peer id, or throws InvalidPublicKeyError if there isn't one (RSA and URL peer ids).
  • Malformed key bytes also throw InvalidPublicKeyError.

floodsub, gossipsub and libp2p each hand-wrote this check, and their behaviour had drifted apart. They now share the helper:

  • floodsub verifySignature: a message whose key field does not derive to its Ed25519 or secp256k1 author is now rejected. Previously the field was ignored and the inlined key was used. This matches gossipsub and go-libp2p-pubsub.
  • gossipsub validateToRawMessage: no behaviour change. Mismatched, missing and malformed keys still map to ValidateError.InvalidPeerId.
  • libp2p getPublicKey: keys fetched through content routing are checked with the helper. A mismatch still throws InvalidPublicKeyError, but the message text changed.

Notes & open questions

publicKeyForPeer is async even though nothing inside it awaits today. That leaves room for key parsing to become async without changing callers.

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

@tabcat
tabcat marked this pull request as ready for review October 17, 2026 01:15
@tabcat
tabcat requested a review from a team as a code owner October 17, 2026 01:15
@tabcat
tabcat merged commit f0e64b9 into libp2p:main Oct 17, 2026
34 checks passed
@tabcat tabcat mentioned this pull request Oct 17, 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.

1 participant