ERC-7627 · Final · ERC · 2024-02-19
ERC-7627: Secure Messaging Protocol
A minimal on-chain interface for end-to-end encrypted messaging: users publish encryption public keys under their address, and senders emit messages encrypted to those keys as events.
What problem does it solve?
Chat and notification dApps on fast L2s want a simple way to send private messages between Ethereum addresses. Without a shared interface, each app invents its own key registry and message format, and messages from one app cannot be read in another.
ERC-7627 standardizes the two pieces needed for end-to-end encryption on-chain: a public-key directory keyed by address, and a message event carrying ciphertext. Any client that speaks the interface can look up a recipient's key, encrypt locally, and send; any client can watch the events and decrypt messages addressed to its user.
How it works
The contract implements IERC7627:
updatePublicKey(keyIndex, PublicKey)— the caller registers or replaces one of its keys. A user may hold several keys, each under abytes32 keyIndex. Only the address itself can update its keys.getUserPublicKey(user, keyIndex)— look up a recipient's key.sendMessage(to, keyIndex, sessionId, encryptedMessage)— emits aMessageSentevent; the reference implementation stores nothing.
PublicKey holds the raw key bytes, a valid_before timestamp, and an algorithm enum: ECDSA, ED25519 or X25519.
Events:
MessageSent(from, to, keyIndex, sessionId, encryptedMessage)—from,toandkeyIndexare indexed so clients can filter their inbox.PublicKeyUpdated(user, keyIndex, newPublicKey).
The sessionId is chosen by the sender and groups messages into a conversation. Encryption and decryption happen entirely off-chain; the contract never sees plaintext.
Sample code
Sending and receiving with ethers v6 (encryptFor / decrypt stand for your chosen X25519/ECIES library):
import { Contract, id, hexlify } from "ethers";
const abi = [
"function getUserPublicKey(address,bytes32) view returns (tuple(bytes public_key, uint64 valid_before, uint8 algorithm))",
"function sendMessage(address,bytes32,bytes32,bytes)",
"event MessageSent(address indexed from, address indexed to, bytes32 indexed keyIndex, bytes32 sessionId, bytes encryptedMessage)",
];
const messenger = new Contract(messengerAddress, abi, signer);
const keyIndex = id("default");
const pk = await messenger.getUserPublicKey(bob, keyIndex);
if (pk.valid_before !== 0n && pk.valid_before < BigInt(Math.floor(Date.now() / 1000))) throw new Error("key expired");
const ciphertext = await encryptFor(pk.public_key, "hello bob"); // off-chain
await messenger.sendMessage(bob, keyIndex, id("chat-42"), hexlify(ciphertext));
// Bob's inbox
messenger.on(messenger.filters.MessageSent(null, bob), (from, to, k, session, data) => {
console.log(from, session, decrypt(myPrivateKey, data));
});
Things to know
- Status is Final.
- Everything on-chain is public: the ciphertext is private, but sender, recipient, timing, size and session ID are visible to everyone forever.
- If a private key ever leaks, all past messages encrypted to it can be decrypted from chain history; there is no forward secrecy in the interface itself.
- The standard does not define how
valid_beforeis enforced or which encryption scheme to use with each algorithm; clients must agree on that. - Each message costs gas proportional to its size, so this suits short messages on low-fee chains.