ERC-1271 · Final · ERC · 2018-07-25
ERC-1271: Standard Signature Validation Method for Contracts
Lets smart contracts 'sign' messages by exposing isValidSignature, so apps can verify signatures from multisigs and smart wallets just as they verify EOA signatures.
What problem does it solve?
An externally owned account (EOA) signs with its private key, and anyone can check the signature with ecrecover. A contract has no private key, so it cannot produce such a signature. That shut out multisigs, DAOs and smart contract wallets from anything that relies on signed messages: off-chain order books, "Sign in with Ethereum", gasless permits and so on.
ERC-1271 lets the contract itself decide whether a signature is valid on its behalf. The verifying app simply asks it.
How it works
The signing contract implements one function:
function isValidSignature(bytes32 hash, bytes memory signature)
external view returns (bytes4 magicValue);
- It returns the magic value
0x1626ba7e(the selector ofisValidSignature(bytes32,bytes)) if the signature is valid, and anything else otherwise. - It must not modify state (
view), so it can be queried off-chain and cannot be abused for side effects. - The logic is entirely up to the contract: check an owner's ECDSA signature, require M-of-N signers, use passkeys or BLS, add time limits, and so on.
The verifier's rule is simple: if the signer address has code, call isValidSignature and compare the result with the magic value; otherwise use ecrecover. The hash is usually an EIP-712 or EIP-191 message hash.
Sample code
A single-owner wallet that accepts its owner's signature:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
contract OwnerWallet {
bytes4 internal constant MAGICVALUE = 0x1626ba7e;
address public immutable owner;
constructor(address _owner) { owner = _owner; }
function isValidSignature(bytes32 hash, bytes memory signature)
external view returns (bytes4)
{
(address signer, ECDSA.RecoverError err, ) = ECDSA.tryRecover(hash, signature);
return (err == ECDSA.RecoverError.NoError && signer == owner) ? MAGICVALUE : bytes4(0xffffffff);
}
}
Checking a signature off-chain with ethers v6:
import { Contract, JsonRpcProvider } from "ethers";
const provider = new JsonRpcProvider(process.env.RPC_URL);
const wallet = new Contract(
signerAddress,
["function isValidSignature(bytes32,bytes) view returns (bytes4)"],
provider,
);
const ok = (await wallet.isValidSignature(hash, signature)) === "0x1626ba7e";
On-chain, OpenZeppelin's SignatureChecker.isValidSignatureNow handles both EOAs and ERC-1271 contracts.
Things to know
- Status: Final. It is supported by major smart wallets and by many protocols that accept signed messages.
- Do not hard-code a gas limit when calling
isValidSignature; implementations may legitimately use a lot of gas. - Check for the exact magic value, not just a successful call. A contract with a fallback function might return arbitrary data.
- A wallet that is not deployed yet has no code to call. ERC-6492 defines a wrapper for verifying signatures from counterfactual wallets, common with ERC-4337 accounts.
- If one key controls several wallets, a naive implementation may accept the same signature for all of them. Including the wallet address in the signed data (for example via the EIP-712 domain) avoids that replay.