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.

This article examines ERC6492, a standard proposing signature validation for contract accounts that have not yet been deployed.

The following material is translated, summarized, and supplemented with explanations.

I have also covered a range of other ERCs.

Overview

ERC1271 defines a way to verify signatures using on-chain contracts. It lets a contract determine whether it actually authorized a signature through the isValidSignature function.

However, if the contract does not yet exist on-chain (that is, it has not been deployed), isValidSignature is unavailable.

A new proposal addresses this limitation. It defines a way to verify signatures from undeployed contracts, also known as counterfactual contracts. The method extends ERC1271.

The proposal enables signature verification for contracts that will be deployed in the future.

For more on ERC1271, see the following article.

[ERC1271] How Contracts Verify Signatures

Motivation

Account abstraction has attracted attention in the blockchain ecosystem. It makes contract wallets easier to use. Using a contract wallet ordinarily requires deploying its contract on-chain first. With account abstraction, deployment can be deferred until the user's first transaction. Users can therefore start using a contract wallet immediately without a separate deployment step.

This approach has a limitation. Many decentralized applications (dApps) require signatures for login or other interactions. Those signatures cannot be used until the contract wallet has actually been deployed, typically with its first transaction.

Furthermore, ERC1271 does not support message signatures from an undeployed contract (counterfactual contract). This is a significant limitation for contract wallets.

For more on account abstraction, see the following article.

[ERC4337] Implementing Account Abstraction Without Protocol Changes

Specification

ERC1271 defines a way for on-chain contracts to verify signatures.

The following is quoted from ERC721.

isValidSignature can call arbitrary methods to validate a given signature, which could be context dependent (e.g. time based or state based), EOA dependent (e.g. signers authorization level within smart wallet), signature scheme Dependent (e.g. ECDSA, multisig, BLS), etc.

This function should be implemented by contracts which desire to sign messages (e.g. smart contract wallets, DAOs, multisignature wallets, etc.) Applications wanting to support contract signatures should call this method if the signer is a contract.

isValidSignature may call arbitrary methods to validate a signature. These may depend on context (such as time or state), the externally owned account (EOA), such as the signer's authorization level within a smart wallet, or the signature scheme (such as ECDSA, multisignature, or BLS).

Contracts that want to sign messages should implement this function (for example, smart contract wallets, DAOs, and multisignature wallets). Applications supporting contract signatures should call it when the signer is a contract.

The new proposal retains isValidSignature and adds a wrapper signature format that can be used before deployment. This extends support to signatures from undeployed contracts.

A verifier detects the wrapper format by checking whether the signature ends with a specific sequence of magicBytes (0x6492649264926492649264926492649264926492649264926492649264926492). When the verifier detects this format, it must deploy the contract before calling isValidSignature.

Using CREATE2 contracts with this standard is recommended. The predictable deployment address of a CREATE2 contract makes signature verification more efficient.

CREATE2

CREATE2 is a mechanism for deploying smart contracts on Ethereum. Unlike ordinary contract deployment, CREATE2 allows the contract address to be calculated and determined in advance. The key difference is that CREATE2 allows the contract address to be determined before deployment.

In Ethereum's ordinary deployment mechanism (CREATE), the contract address is derived from the deployment transaction data and the deploying account address. By contrast, CREATE2 enables advance address calculation using a specific salt (a distinct value) and the contract bytecode. The address can therefore be known before the contract is deployed.

This property is particularly useful for counterfactual (undeployed) contracts and applications requiring more predictable contract behavior. For example, users can send funds to a contract that does not yet exist, and CREATE2 allows the contract to receive those funds once deployed.

CREATE2 provides predictable contract addresses on Ethereum and supports a range of advanced applications and specific use cases.

Signer

This section covers contracts that produce signatures, particularly those implementing ERC1271. These are primarily contract wallets, but also include other contracts that implement ERC1271 and have not yet been deployed (counterfactual contracts).

Deployed Contracts

If the contract is already deployed on-chain, generate an ERC1271 signature as usual.

Undeployed Contracts

If the contract has not yet been deployed, wrap the signature in a special format. Combine create2Factory (the address of the contract factory), factoryCalldata (the calldata for the factory), and originalERC1271Signature (the original ERC1271 signature), then append magicBytes.

Deployed but Unprepared Contracts

If the contract is deployed but not yet ready for ERC1271 verification, wrap the signature differently. Combine prepareTo (the preparation target address), prepareData (the preparation data), and originalERC1271Signature, then append magicBytes. prepareTo and prepareData specify the transaction needed to prepare the contract for ERC1271 verification.

This approach uses factoryCalldata rather than create2Factory, salt, and bytecode separately. This allows verification with different factory interfaces. ERC1271 verification assumes that the signer's address is already known, so the verifier does not need to calculate it.

Verifier

Signature verification follows these steps.

  1. Check magicByte
    • Check whether the signature ends with the designated magicByte.
    • If present, make an eth_call to a multicall contract and pass factoryCalldata to the factory.
    • This deploys the contract if it has not already been deployed.
    • Then perform ordinary signature verification using contract.isValidSignature.
  2. Check for contract code
    • Check whether contract code exists at the target address.
    • If code exists, verify the signature using isValidSignature as specified by ERC1271.
  3. Retry failed verification
    • If ERC1271 verification fails and the factory deployment call was skipped because the wallet already had code, execute the factoryCalldata transaction and retry isValidSignature.
  4. Verify with ecrecover
    • Finally, if the address has no contract code, use ecrecover to verify the signature.

These steps allow accurate verification of contract signatures, including those from undeployed and existing contracts.

Additional Details

The proposal specifies a particular way to wrap contract signatures. The format is contract-agnostic, so it can be applied to any contract and readily verified.

The wrapper signature contains a specific sequence of bytes called magicBytes. The magicBytes suffix ends in 0x92, distinguishing it from a valid ecrecover signature in r, s, v format. Since 0x92 is not a valid v value, the wrapper can be distinguished from an ordinary ecrecover signature. The length of magicBytes (bytes32) also helps avoid collisions with ordinary ERC1271 signatures.

ecrecover is an Ethereum function for verifying digital signatures. It can recover the address of the account that signed a message. ecrecover requires signature data consisting of three components: r, s, and v.

  • r and s
    • These are the main components of the signature and are generated during signing.
    • r and s are unique values generated for the message using a particular private key.
  • v
    • This component serves as a recovery identifier and relates to the blockchain context in which the signature was created (for example, the network ID or chain ID).
    • v normally takes the value 27 or 28, but other values may be used following the introduction of chain IDs.

During signing, the signer uses their private key to sign a message, generating r, s, and v. Passing these values and the original message (or its hash) to ecrecover recovers the signing account's public address.

The main benefit of ecrecover is that it can establish that a particular account signed a message without revealing its private key. This enables secure verification of transactions and contracts on-chain.

To ensure the verification process works correctly, follow these ordering rules.

  1. Check magicBytes before ordinary ERC1271 verification so a counterfactual contract signature remains valid after deployment.
  2. Check magicBytes before ecrecover to avoid attempting ecrecover on a recognizable counterfactual contract signature.
  3. Do not check ecrecover before ERC1271, since a contract may use a signature format that could also be a valid ecrecover signature.

When the corresponding wallet already has code, the reason a signature was encoded with a deploy prefix is unclear. The signature may have been created before the contract was deployed, or the deployed contract may not yet be ready to verify signatures. Verification must therefore account for both possibilities.

A signature encoded with a deploy prefix may have been created before the contract was deployed on-chain. Alternatively, the contract may already be deployed but not yet ready to verify signatures. This signature format can therefore be used when the contract's deployment state is unclear.

Consider both scenarios during signature verification. When a signature has a deploy prefix, the following two checks are important.

  1. Verification when the contract is undeployed
    • Determine whether the signature was generated before deployment.
    • Because the contract does not yet exist, deploying it may be necessary to verify the signature.
  2. Verification when the contract is deployed but unprepared
    • Alternatively, the contract may already be deployed, but its signature verification functionality may not yet be operational.
    • Additional steps may be required to prepare the contract for signature verification.

The verification process must account for these scenarios and adapt to the contract's deployment state to verify signatures accurately.

Backwards Compatibility

ERC6492 extends signature verification while retaining compatibility with ERC1271. It makes it possible to verify a wide range of signatures, including externally owned account (EOA) signatures and typed data signatures such as EIP712.

For more on EIP712, see the following article.

[EIP712] Hashing and Signing Typed Structured Data

ERC6492 offers the following benefits.

  • Rapid signature type identification
    • This ERC includes a special byte sequence called MaficByte.
    • It allows a verifier to identify a contract signature immediately without querying the blockchain.
  • Address recovery
    • create2Factory and factoryCalldata make it possible to identify the contract address from the signature alone.
    • This works similarly to ecrecover.

ERC6492 handles a wider range of signature types than ordinary ERC1271, offering a more efficient and straightforward way to process contract signatures.

Reference Implementation

The following universal signature verification contract can be used both on-chain and off-chain. It supports multiple signature types: this standard, ERC1271, and signatures verified by ecrecover. It also supports EIP712 typed data signatures by verifying the final digest (_hash).

javascript
// As per ERC-1271
interface IERC1271Wallet {
  function isValidSignature(bytes32 hash, bytes calldata signature) external view returns (bytes4 magicValue);
}

error ERC1271Revert(bytes error);
error ERC6492DeployFailed(bytes error);

contract UniversalSigValidator {
  bytes32 private constant ERC6492_DETECTION_SUFFIX = 0x6492649264926492649264926492649264926492649264926492649264926492;
  bytes4 private constant ERC1271_SUCCESS = 0x1626ba7e;

  function isValidSigImpl(
    address _signer,
    bytes32 _hash,
    bytes calldata _signature,
    bool allowSideEffects,
    bool tryPrepare
  ) public returns (bool) {
    uint contractCodeLen = address(_signer).code.length;
    bytes memory sigToValidate;
    // The order here is strictly defined in https://eips.ethereum.org/EIPS/eip-6492
    // - ERC-6492 suffix check and verification first, while being permissive in case the contract is already deployed; if the contract is deployed we will check the sig against the deployed version, this allows 6492 signatures to still be validated while taking into account potential key rotation
    // - ERC-1271 verification if there's contract code
    // - finally, ecrecover
    bool isCounterfactual = bytes32(_signature[_signature.length-32:_signature.length]) == ERC6492_DETECTION_SUFFIX;
    if (isCounterfactual) {
      address create2Factory;
      bytes memory factoryCalldata;
      (create2Factory, factoryCalldata, sigToValidate) = abi.decode(_signature[0:_signature.length-32], (address, bytes, bytes));

      if (contractCodeLen == 0 || tryPrepare) {
        (bool success, bytes memory err) = create2Factory.call(factoryCalldata);
        if (!success) revert ERC6492DeployFailed(err);
      }
    } else {
      sigToValidate = _signature;
    }

    // Try ERC-1271 verification
    if (isCounterfactual || contractCodeLen > 0) {
      try IERC1271Wallet(_signer).isValidSignature(_hash, sigToValidate) returns (bytes4 magicValue) {
        bool isValid = magicValue == ERC1271_SUCCESS;

        // retry, but this time assume the prefix is a prepare call
        if (!isValid && !tryPrepare && contractCodeLen > 0) {
          return isValidSigImpl(_signer, _hash, _signature, allowSideEffects, true);
        }

        if (contractCodeLen == 0 && isCounterfactual && !allowSideEffects) {
          // if the call had side effects we need to return the
          // result using a `revert` (to undo the state changes)
          assembly {
           mstore(0, isValid)
           revert(31, 1)
          }
        }

        return isValid;
      } catch (bytes memory err) {
        // retry, but this time assume the prefix is a prepare call
        if (!tryPrepare && contractCodeLen > 0) {
          return isValidSigImpl(_signer, _hash, _signature, allowSideEffects, true);
        }

        revert ERC1271Revert(err);
      }
    }

    // ecrecover verification
    require(_signature.length == 65, 'SignatureValidator#recoverSigner: invalid signature length');
    bytes32 r = bytes32(_signature[0:32]);
    bytes32 s = bytes32(_signature[32:64]);
    uint8 v = uint8(_signature[64]);
    if (v != 27 && v != 28) {
      revert('SignatureValidator: invalid signature v value');
    }
    return ecrecover(_hash, v, r, s) == _signer;
  }

  function isValidSigWithSideEffects(address _signer, bytes32 _hash, bytes calldata _signature)
    external returns (bool)
  {
    return this.isValidSigImpl(_signer, _hash, _signature, true, false);
  }

  function isValidSig(address _signer, bytes32 _hash, bytes calldata _signature)
    external returns (bool)
  {
    try this.isValidSigImpl(_signer, _hash, _signature, false, false) returns (bool isValid) { return isValid; }
    catch (bytes memory error) {
      // in order to avoid side effects from the contract getting deployed, the entire call will revert with a single byte result
      uint len = error.length;
      if (len == 1) return error[0] == 0x01;
      // all other errors are simply forwarded, but in custom formats so that nothing else can revert with a single byte in the call
      else assembly { revert(error, len) }
    }
  }
}

// this is a helper so we can perform validation in a single eth_call without pre-deploying a singleton
contract ValidateSigOffchain {
  constructor (address _signer, bytes32 _hash, bytes memory _signature) {
    UniversalSigValidator validator = new UniversalSigValidator();
    bool isValidSig = validator.isValidSigWithSideEffects(_signer, _hash, _signature);
    assembly {
      mstore(0, isValidSig)
      return(31, 1)
    }
  }
}

ERC6492_DETECTION_SUFFIX

javascript
bytes32 private constant ERC6492_DETECTION_SUFFIX = 0x6492649264926492649264926492649264926492649264926492649264926492;

Overview

A specific byte sequence used to detect ERC6492 signatures.

Details

This constant is the byte sequence used to detect whether a signature conforms to ERC6492. A signature ending in this sequence is identified as an ERC6492 signature.

Parameters

  • bytes32
    • A fixed-length sequence of 32 bytes.
    • Represents the specific value used for signature detection.

ERC1271_SUCCESS

javascript
bytes4 private constant ERC1271_SUCCESS = 0x1626ba7e;

Overview

A constant indicating successful verification of an ERC1271 signature.

Details

An ERC1271 contract returns this constant from isValidSignature to indicate that the signature is valid. When this value is returned, verification is considered successful.

Parameters

  • bytes4
    • A fixed-length sequence of 4 bytes.
    • Indicates that an ERC1271 signature was successfully verified.

isValidSigImpl

javascript
function isValidSigImpl(
    address _signer,
    bytes32 _hash,
    bytes calldata _signature,
    bool allowSideEffects,
    bool tryPrepare
) public returns (bool) {
    // 関数の実装コード
}

Overview

Determines whether a given signature is valid.

Details

This function detects whether a signature conforms to ERC6492, then verifies it using ERC1271 or ecrecover. If the signature conforms to ERC6492 and the relevant contract is undeployed, the function attempts deployment. It then checks the ERC1271 signature or whether ecrecover can recover the signer.

Arguments

  • _signer
    • The signer's address.
  • _hash
    • The hash of the signature to verify.
  • _signature
    • The signature data to verify.
  • allowSideEffects
    • A bool indicating whether side effects (state changes) are permitted.
  • tryPrepare
    • A bool indicating whether to attempt the deployment preparation call.

Return Value

  • bool
    • A bool indicating whether the signature is valid.

isValidSigWithSideEffects

javascript
function isValidSigWithSideEffects(address _signer, bytes32 _hash, bytes calldata _signature)
    external returns (bool)
{
    return this.isValidSigImpl(_signer, _hash, _signature, true, false);
}

Overview

Verifies a signature while permitting side effects (state changes).

Details

This function calls isValidSigImpl to verify the signature, but allows side effects. This may improve gas efficiency in certain cases.

Arguments

  • _signer
    • The signer's address.
  • _hash
    • The hash of the signature to verify.
  • _signature
    • The signature data to verify.

Return Value

  • bool
    • A bool indicating whether the signature is valid.

isValidSig

javascript
function isValidSig(address _signer, bytes32 _hash, bytes calldata _signature)
    external returns (bool)
{
    try this.isValidSigImpl(_signer, _hash, _signature, false, false) returns (bool isValid) { return isValid; }
    catch (bytes memory error) {
        // エラー処理
    }
}

Overview

Verifies a signature without permitting side effects.

Details

This function uses isValidSigImpl to verify the signature without allowing side effects. This makes it safe against reentrancy attacks, although it may be less gas-efficient than isValidSigWithSideEffects.

Arguments

  • _signer
    • The signer's address.
  • _hash
    • The hash of the signature to verify.
  • _signature
    • The signature data to verify.

Return Value

  • bool
    • A bool indicating whether the signature is valid.

On-chain Verification

The contract provides two verification methods.

  • UniversalSigValidator.isValidSig(_signer, _hash, _signature)
    • This function returns a bool indicating whether the signature is valid.
    • This method is safe against reentrancy attacks.
  • UniversalSigValidator.isValidSigWithSideEffects(_signer, _hash, _signature)
    • This function offers equivalent functionality and can be more gas-efficient in certain cases, but it is not safe against reentrancy attacks.

These functions may return errors if the underlying call fails.

For more on reentrancy attacks, see the following article.

https://chaldene.net/solidity-reentrancy-attack

Off-chain Verification

The ValidateSigOffchain helper enables universal signature verification in a single eth_call, without a previously deployed contract. For example, the following code uses the ethers library to determine whether a signature is valid.

javascript
const isValidSignature = '0x01' === await provider.call({
  data: ethers.utils.concat([
    validateSigOffchainBytecode,
    (new ethers.utils.AbiCoder()).encode(['address', 'bytes32', 'bytes'], [signer, hash, signature])
  ])
})

Libraries such as Ambire's signature-validator also support this universal signature verification.

Security Considerations

Reentrancy

Deploying a contract requires a CALL, which introduces a risk of reentrancy attacks. The verification method mitigates this problem by always using revert when side effects occur and retrieving the result from the revert data. The isValidSigWithSideEffects method is also available when reentrancy is not a concern.

Frequent Off-chain Use

This ERC is expected to be used more frequently for off-chain verification. In many cases, on-chain signature verification assumes that the wallet is already deployed.

Deployment Permissions

It is important that the contract is configured with the correct permissions at deployment. Because CREATE2 changes the deployment address if the bytecode or constructor calldata within the signature is modified, these modifications cannot be used to improperly expand permissions.

Dynamic Changes to Authentication

Contract accounts can change their authentication methods. Under this ERC, even counterfactual contract signatures are checked against the current contract version if the contract has already been deployed.

Replay Protection Across Networks

Replay protection matters for ordinary signatures, but this ERC requires particular attention to signature validity across networks. A signature can be verified if it was valid at deployment, or if the wallet can be deployed with the same factory address and bytecode.

Citation

Ivo Georgiev (@Ivshti), Agustin Aguilar (@Agusx1211), "ERC-6492: Signature Validation for Predeploy Contracts," Ethereum Improvement Proposals, no. 6492, February 2023. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-6492.

Conclusion

This article covered ERC6492, which proposes signature verification for contract accounts that have not yet been deployed. I hope you found it useful.

If you have questions, feel free to contact me by direct message on Twitter.

Twitter @cardene777

I also share information on other platforms, so feel free to explore them.

https://chaldene.net/