ERC-7739 · Draft · ERC · 2024-05-28
ERC-7739: Readable Typed Signatures for Smart Accounts
A defensive rehashing scheme for ERC-1271 smart accounts that stops one owner's signature being replayed across several of their accounts, while keeping the signed content readable in EIP-712 wallets.
What problem does it solve?
Smart accounts verify signatures through ERC-1271 isValidSignature(hash, signature). The naive implementation just checks that the account's owner signed hash. If one EOA owns several smart accounts and the application's hash does not include the account address (common in practice; the spec cites Permit2), a signature made for account A is equally valid for account B. That is a replay attack.
Accounts can defend themselves by rehashing: mix their own address and chain ID into the hash before checking the signature. Done naively, though, the wallet ends up asking the user to sign an opaque 32-byte hash, which is a phishing risk. ERC-7739 defines a rehashing scheme built from nested EIP-712 structs so the wallet still shows the original, human-readable message.
How it works
isValidSignature keeps its signature but runs one of two workflows.
TypedDataSign (the app hash was EIP-712). The user signs, under the app's domain, a wrapper struct:
TypedDataSign(<Contents> contents, string name, string version,
uint256 chainId, address verifyingContract, bytes32 salt)<ContentsType>
where the name/version/chainId/verifyingContract/salt fields come from the account's ERC-5267 eip712Domain(). Because the original struct is a nested field, wallets display it normally, and because the account's address is inside, the signature only works for that account.
The signature passed on-chain is packed as:
originalSignature ‖ APP_DOMAIN_SEPARATOR ‖ contentsHash ‖ contentsDescription ‖ uint16(len)
The account first checks hash == keccak256(0x1901 ‖ APP_DOMAIN_SEPARATOR ‖ contentsHash), then rebuilds the wrapper hash and verifies the signature against it. contentsName should be sanitised (non-empty, not starting with a lowercase letter or (, no , ) or NUL) to block type-name injection tricks.
PersonalSign (the app hash was an EIP-191 personal message). The user signs PersonalSign(bytes prefixed) under the account's domain, with prefixed set to the original hash. Nothing extra is appended to the signature.
If the appended data reconstructs hash, the account must use TypedDataSign; otherwise PersonalSign. Rehashing may be skipped for off-chain calls or when hash already commits to the account address.
Detection: isValidSignature(0x7739…7739, "") should return 0x77390001.
Sample code
Building a TypedDataSign signature for a smart account (ethers v6):
import { TypedDataEncoder, solidityPacked, toUtf8Bytes } from "ethers";
const appDomain = { name: "MailApp", version: "1", chainId: 1, verifyingContract: appAddr };
const mailTypes = { Mail: [{ name: "from", type: "address" }, { name: "to", type: "address" },
{ name: "message", type: "string" }] };
const mail = { from: alice, to: bob, message: "hi" };
// Account's own domain, read from eip712Domain() (ERC-5267)
const [, name, version, chainId, verifyingContract, salt] = await account.eip712Domain();
const types = {
TypedDataSign: [
{ name: "contents", type: "Mail" }, { name: "name", type: "string" },
{ name: "version", type: "string" }, { name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" }, { name: "salt", type: "bytes32" },
],
...mailTypes,
};
const sig = await owner.signTypedData(appDomain, types,
{ contents: mail, name, version, chainId, verifyingContract, salt });
const contentsType = "Mail(address from,address to,string message)";
const wrapped = solidityPacked(
["bytes", "bytes32", "bytes32", "bytes", "uint16"],
[sig, TypedDataEncoder.hashDomain(appDomain),
TypedDataEncoder.hashStruct("Mail", mailTypes, mail),
toUtf8Bytes(contentsType), toUtf8Bytes(contentsType).length],
);
// The app then calls account.isValidSignature(TypedDataEncoder.hash(appDomain, mailTypes, mail), wrapped)
Things to know
- Status: Draft. Details such as the support magic value may still change (an earlier draft used
supportsNestedTypedDataSign()returning0xd620c85a). - An account using this scheme cannot have a signer that itself uses the same scheme; nested
TypedDataSigntypes cannot be expressed in one EIP-712 request. - The spec's reference implementation is deliberately full-featured. Do not assume a minimal version is safe; that mistake was made with ERC-1271.
- Application contracts are unchanged; only the signing request is wrapped. The spec notes this can work even without wallet or frontend updates, via injected client-side JavaScript.