ERC-7739 Smart account readable typed signing (Readable Typed Signatures for Smart Accounts)

Draft · Signatures & auth · created 2024-05-28

A defensive rehashing standard that prevents the same signature from being replayed on a different account when a single EOA owns multiple smart accounts. It uses a nested EIP-712 structure so the signed content remains visible on the wallet screen.

Why it is needed

Smart accounts verify signatures using ERC-1271's isValidSignature function. However, if the hash being verified does not include the smart account's address, a replay attack becomes possible: one externally owned account (EOA) that owns multiple smart accounts could reuse the same signature on a different account. Widely used applications such as Permit2 actually hit this case. For this reason, many smart account implementations perform defensive rehashing, combining at minimum the hash, their own address, and the chain ID and rehashing them. However, if this is implemented naively with EIP-712, the content being signed becomes opaque, so users can no longer see what they are signing in the wallet. This standard defines a rehashing method that prevents replay while keeping the signed content visible in any wallet that supports EIP-712.

What to implement

In depth

TypedDataSign final hash

This is the workflow used when the original hash was computed using EIP-712. The final hash is defined as keccak256(\x19\x01 ‖ APP_DOMAIN_SEPARATOR ‖ hashStruct(TypedDataSign)). The TypedDataSign structure contains contents (the hashStruct of the original struct) and the name, version, chainId, verifyingContract, and salt from eip712Domain(). The typedDataSignTypehash is constructed on the fly on-chain using contentsType. This design forces contents to be a user-defined type so that the content is always visible in the signing request. The domain fields are flattened rather than using the EIP712Domain type in order to avoid collisions with the verifying contract's own domain type. The fields bitmap and extensions array from ERC-5267 are omitted since they provide no security benefit for on-chain rehashing.

Extracting contentsName and recommended validation rules

contentsName is the substring of contentsType up to (but not including) the first "(". For example, if contentsType is "Mail(address from,address to,string message)", then contentsName is "Mail". For safety, it is RECOMMENDED to treat the signature as invalid if contentsName is an empty string, if it starts with any of abcdefghijklmnopqrstuvwxyz(, or if it contains the bytes ", )\x00". Rejecting names that start with a lowercase letter is a choice made to keep the standard language-neutral and future-proof, since the same type may be named differently in other languages (e.g. uint256 might be called u256).

TypedDataSign signature format

The signature passed to isValidSignature is transformed into originalSignature ‖ APP_DOMAIN_SEPARATOR ‖ contents ‖ contentsDescription ‖ uint16(contentsDescription.length). contents is the bytes32 struct hash of the original struct. contentsDescription has an implicit mode, which uses only contentsType, and an explicit mode, which uses contentsType ‖ contentsName. Explicit mode is needed when the contents struct has nested types, since EIP-712's alphabetical ordering can place contentsName somewhere other than the front of contentsType. The appended APP_DOMAIN_SEPARATOR and contents are used to verify that the received hash is correct by checking hash == keccak256(\x19\x01 ‖ APP_DOMAIN_SEPARATOR ‖ contents). If they do not match, the signature is invalid under this workflow.

PersonalSign workflow

This is the workflow used when the original hash was computed using EIP-191. The final hash is keccak256(\x19\x01 ‖ ACCOUNT_DOMAIN_SEPARATOR ‖ hashStruct(PersonalSign)), and the type hash is keccak256("PersonalSign(bytes prefixed)"). ACCOUNT_DOMAIN_SEPARATOR is the smart account's own domain separator, which can be computed via eip712Domain(). The hash is computed on the application side from "\x19Ethereum Signed Message:\n", the message length, and the original message, and then passed in. The smart account does not need to know how that computation was done. This workflow requires no additional data to be appended to the signature.

Determining the workflow

Since the function signature of isValidSignature stays the same, the implementation MUST determine for itself which workflow to use for verification. If reconstructing the hash from the APP_DOMAIN_SEPARATOR and contents embedded in the signature matches the hash that was passed in, the TypedDataSign workflow MUST be used; if it does not match, the PersonalSign workflow MUST be used.

Support detection and magic number

It is RECOMMENDED that a smart account signal support for this standard by returning bytes4(0x77390001) in response to a call to isValidSignature(0x7739773977397739773977397739773977397739773977397739773977397739, ""). This magic number MAY be incremented as the standard is updated. isValidSignature was reused instead of defining a new function in order to make implementation easier for modular smart accounts. An earlier draft used a supportsNestedTypedDataSign() function that returned bytes4(0xd620c85a).

Conditions under which rehashing can be skipped

The defensive rehashing workflow MAY be skipped when isValidSignature is called off-chain, or when the passed-in hash already includes the smart account's address. Since many developers may not update their apps to support the nested EIP-712 workflow, it is RECOMMENDED that smart account implementations skip rehashing when it is safe to do so, in order to accommodate existing apps. This design minimizes adoption friction so that users can still make use of the standard by injecting client-side JavaScript even when wallets or app frontends have not been updated.

Reference implementation and proposal status

A production-ready, optimized reference implementation is provided. The reference implementation is intentionally not minimal, in order to avoid repeating the mistake made with ERC-1271, where a minimal reference implementation was wrongly assumed to also be safe for production use. This proposal is still in Draft status and its contents may change.

Things to watch out for

Original ERC · ← ERCs