Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

ERC-1271 defines a standard method for validating signatures when the account authorizing an operation is a smart contract.

This article translates, summarizes, and expands on the following specification.

More ERC explainers:

Overview

Externally owned accounts (EOAs) can sign messages using their private keys. A smart contract account does not ordinarily possess a corresponding private key with which it can produce an ECDSA signature for its own address.

ERC-1271 defines a function called isValidSignature(hash, signature) through which a contract can determine whether supplied authorization data is valid under its own rules.

The function allows an application to ask a contract whether a particular signature or authorization is valid for a message hash. It does not require the contract itself to produce a cryptographic signature. The authorization data may instead originate from one or more authorized signers.

Motivation

Many applications use signed messages to authorize operations such as asset transfers and trades.

For example, a decentralized exchange with an off-chain order book can collect signed buy and sell orders from users. Its settlement contract then validates those signatures before executing the trades.

An EOA can authorize an order using its private key. A smart contract account, however, may use a different authorization model, such as a multisignature policy or governance-controlled approval.

Applications therefore need a standardized way to ask the account itself whether a particular authorization is acceptable.

ERC-1271 provides this mechanism, enabling smart contract wallets, DAOs, and other contract-based accounts to participate in signature-based workflows.

Specification

javascript
pragma solidity ^0.5.0;

contract ERC1271 {

  // bytes4(keccak256("isValidSignature(bytes32,bytes)")
  bytes4 constant internal MAGICVALUE = 0x1626ba7e;

  /**
   * @dev Should return whether the signature provided is valid for the provided hash
   * @param _hash      Hash of the data to be signed
   * @param _signature Signature byte array associated with _hash
   *
   * MUST return the bytes4 magic value 0x1626ba7e when function passes.
   * MUST NOT modify state (using STATICCALL for solc < 0.5, view modifier for solc > 0.5)
   * MUST allow external calls
   */ 
  function isValidSignature(
    bytes32 _hash, 
    bytes memory _signature)
    public
    view 
    returns (bytes4 magicValue);
}

The isValidSignature function determines whether the supplied signature is valid for the supplied hash.

The validation logic may depend on several factors:

  • The current time or contract state.
  • The permissions of the account's authorized signers.
  • The applicable signature scheme, such as ECDSA, multisignature validation, or BLS.

A valid signature must produce the bytes4 magic value 0x1626ba7e. An arbitrary nonzero return value or Boolean true is not sufficient.

The function must not modify state and must allow external calls. This permits validation policies that consult other contracts while remaining compatible with static execution.

Smart contract wallets, DAOs, and multisignature accounts can implement this interface. Applications supporting contract signatures can then invoke it when the authorizing account is a contract.

ERC-1271 delegates the validity decision to the authorizing contract. The application must supply the correct message hash and check the expected return value, while the contract must enforce its intended authorization policy.

Rationale

The name isValidSignature reflects the function's purpose: determining whether a signature or other authorization data is accepted for a particular message.

A smart wallet might recognize a signature from its owner, require several authorized signers, or enforce a more complex policy. The interface does not prescribe one internal validation mechanism.

The function accepts two arguments to distinguish the message hash from the authorization bytes.

Using a bytes32 hash rather than an unhashed message allows the interface to accommodate different message-hashing and signing conventions, including EIP-712 typed structured data.

For EIP-712, see the following article.

EIP-712: Hashing and signing typed structured data

Validation must not modify state. The original rationale includes preventing unwanted side effects such as gas-token issuance and enabling clients to query signature validity off-chain.

The specification uses a particular bytes4 magic value rather than a Boolean. This gives callers an explicit indication that the contract recognizes the requested validation operation and considers the authorization valid.

Backward compatibility

ERC-1271 does not change conventional EOA signature verification.

Applications can continue to recover an ECDSA signer for ordinary EOAs while using ERC-1271 when validating an authorization under a contract account's policy.

Reference implementation

javascript
/**
* @notice Verifies that the signer is the owner of the signing contract.
*/
function isValidSignature(
    bytes32 _hash,
    bytes calldata _signature
) external override view returns (bytes4) {
    // Validate signatures
    if (recoverSigner(_hash, _signature) == owner) {
        return 0x1626ba7e;
    } else {
        return 0xffffffff;
    }
}

/**
* @notice Recover the signer of hash, assuming it's an EOA account
* @dev Only for EthSign signatures
* @param _hash       Hash of message that was signed
* @param _signature  Signature encoded as (bytes32 r, bytes32 s, uint8 v)
*/
function recoverSigner(
    bytes32 _hash,
    bytes memory _signature
) internal pure returns (address signer) {
    require(_signature.length == 65, "SignatureValidator#recoverSigner: invalid signature length");

    // Variables are not scoped in Solidity.
    uint8 v = uint8(_signature[64]);
    bytes32 r = _signature.readBytes32(0);
    bytes32 s = _signature.readBytes32(32);
    
    // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
    // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
    // the valid range for s in (281): 0 < s < secp256k1n ÷ 2 + 1, and for v in (282): v ∈ {27, 28}. Most
    // signatures from current libraries generate a unique signature with an s-value in the lower half order.
    //
    // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
    // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
    // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
    // these malleable signatures as well.
    //
    // Source OpenZeppelin
    // https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/cryptography/ECDSA.sol
    
    if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
        revert("SignatureValidator#recoverSigner: invalid signature 's' value");
    }
    
    if (v != 27 && v != 28) {
        revert("SignatureValidator#recoverSigner: invalid signature 'v' value");
    }
    
    // Recover ECDSA signer
    signer = ecrecover(_hash, v, r, s);
    
    // Prevent signer from being 0x0
    require(
        signer != address(0x0),
        "SignatureValidator#recoverSigner: INVALID_SIGNER"
    );
    
    return signer;
}

isValidSignature

javascript
function isValidSignature(
    bytes32 _hash,
    bytes calldata _signature
) external override view returns (bytes4) {
    // Validate signatures
    if (recoverSigner(_hash, _signature) == owner) {
        return 0x1626ba7e;
    } else {
        return 0xffffffff;
    }
}

Overview

Checks whether the recovered signer matches the signing contract's owner.

Details

The example recovers an ECDSA signer from the supplied hash and signature. If the recovered address equals the configured owner, the function returns 0x1626ba7e. Otherwise, it returns 0xffffffff.

Malformed signatures may also cause the recovery function to revert.

Parameters

  • _hash
    • The message hash to validate.
  • _signature
    • The signature bytes.

Return value

  • bytes4
    • The validation result. The required success value is 0x1626ba7e.

recoverSigner

javascript
function recoverSigner(
    bytes32 _hash,
    bytes memory _signature
) internal pure returns (address signer) {
    require(_signature.length == 65, "SignatureValidator#recoverSigner: invalid signature length");

    // Variables are not scoped in Solidity.
    uint8 v = uint8(_signature[64]);
    bytes32 r = _signature.readBytes32(0);
    bytes32 s = _signature.readBytes32(32);
    
    // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
    // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
    // the valid range for s in (281): 0 < s < secp256k1n ÷ 2 + 1, and for v in (282): v ∈ {27, 28}. Most
    // signatures from current libraries generate a unique signature with an s-value in the lower half order.
    //
    // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
    // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
    // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
    // these malleable signatures as well.
    //
    // Source OpenZeppelin
    // https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/cryptography/ECDSA.sol
    
    if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
        revert("SignatureValidator#recoverSigner: invalid signature 's' value");
    }
    
    if (v != 27 && v != 28) {
        revert("SignatureValidator#recoverSigner: invalid signature 'v' value");
    }
    
    // Recover ECDSA signer
    signer = ecrecover(_hash, v, r, s);
    
    // Prevent signer from being 0x0
    require(
        signer != address(0x0),
        "SignatureValidator#recoverSigner: INVALID_SIGNER"
    );
    
    return signer;
}

Overview

Recovers the address of an ECDSA signer from a message hash and signature.

Details

The displayed implementation accepts a 65-byte signature encoded as r, s, and v.

It rejects signatures whose s value is outside the permitted lower range or whose v value is neither 27 nor 28. These checks restrict signature malleability. It also rejects recovery to the zero address.

The function expects the hash supplied to ecrecover to be the actual signed digest. Any applicable Ethereum signed-message prefix or EIP-712 domain separation must be handled consistently by the surrounding signing workflow.

Parameters

  • _hash
    • The signed message digest.
  • _signature
    • The signature bytes in the displayed r, s, v format.

Return value

  • address
    • The recovered signer.

The following example calls isValidSignature on an external signing contract.

javascript
function callERC1271isValidSignature(
    address _addr,
    bytes32 _hash,
    bytes calldata _signature
) external view {
    bytes4 result = IERC1271Wallet(_addr).isValidSignature(_hash, _signature);
    require(result == 0x1626ba7e, "INVALID_SIGNATURE");
}

The caller accepts the authorization only when the returned value matches the ERC-1271 magic value.

These historical excerpts are preserved unchanged from the original article. The recovery example depends on helper methods and contract state that are not fully included in the excerpts. They have not been independently compiled or audited as part of this translation.

Security considerations

ERC-1271 does not specify a fixed gas limit for signature validation. Contract wallets may implement validation policies of different computational complexity.

Callers should not assume that an arbitrarily small hard-coded gas allowance will be sufficient for every legitimate implementation.

At the same time, applications must consider expensive validation, reverted calls, malformed return data, and other resource-related risks when interacting with untrusted contracts.

Each signing contract is responsible for correctly enforcing its own authorization policy. A faulty implementation could accept unauthorized operations or reject legitimate ones.

Signature validity can also depend on contract state and may change over time. For example, a signature accepted before a signer is removed may no longer be accepted afterward.

Finally, ERC-1271 does not provide replay protection by itself. The application consuming the authorization must implement the appropriate message-domain separation, nonce handling, expiration rules, and other operation-specific requirements.

Citation

Francisco Giordano (@frangio), Matt Condon (@shrugs), Philippe Castonguay (@PhABC), Amir Bandeali (@abandeali1), Jorge Izquierdo (@izqui), Bertrand Masius (@catageek), "ERC-1271: Standard Signature Validation Method for Contracts," Ethereum Improvement Proposals, no. 1271, July 2018. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-1271.

Conclusion

ERC-1271 standardizes how applications ask smart contract accounts to validate signatures. It allows contract wallets to participate in signed-message workflows while retaining their own authorization policies.

Questions are welcome via Twitter @cardene777.

Other publications:

https://chaldene.net/