EIPs Explained

ERC-191 · Final · ERC · 2016-01-20

ERC-191: Signed Data Standard

Defines the 0x19-prefixed format for signed data, so an off-chain signature can never be mistaken for a transaction or replayed against the wrong contract; it underlies personal_sign and EIP-712.

What problem does it solve?

Multisig wallets and other contracts often accept presigned messages: some bytes plus a signature (v, r, s) that the contract checks with ecrecover. Early on, nobody defined what those bytes should contain, and this caused two problems:

  • A signature could double as a transaction. If the signed bytes happened to be a valid RLP-encoded Ethereum transaction, the message signature could be broadcast as a real transaction.
  • A signature could be replayed on another contract. If users A and B co-own multisig X and also multisig Y, a signature they gave X could be submitted to Y.

EIP-191 fixes both with a fixed prefix and a version byte.

How it works

Every signed payload has this shape:

0x19 <1-byte version> <version-specific data> <data to sign>

0x19 was picked because it cannot start a valid RLP-encoded transaction. In RLP, a single byte below 0x80 encodes itself, so the payload would read as a one-byte item followed by trailing data. No transaction is shaped like that.

The version byte says how to read the rest:

Version Defined in Meaning
0x00 EIP-191 Data with an intended validator. The next 20 bytes are the address of the contract that should accept it
0x01 EIP-712 Typed structured data
0x45 (E) EIP-191 personal_sign messages

Version 0x45 explains the familiar string "\x19Ethereum Signed Message:\n" + len(message). The E of "Ethereum" is the version byte, and thereum Signed Message:\n<len> is the version-specific data. Wallets used this string before EIP-191 was written, and the standard adopted it.

Sample code

Verifying a version 0x00 signature bound to this contract (Solidity):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Validator {
    address public owner;
    mapping(uint256 => bool) public used;

    constructor(address _owner) { owner = _owner; }

    function execute(uint256 nonce, bytes calldata payload, uint8 v, bytes32 r, bytes32 s) external {
        bytes32 hash = keccak256(
            abi.encodePacked(bytes1(0x19), bytes1(0x00), address(this), nonce, payload)
        );
        require(ecrecover(hash, v, r, s) == owner, "bad sig");
        require(!used[nonce], "replayed");
        used[nonce] = true;
        // ... act on payload
    }
}

Producing that signature, plus an ordinary 0x45 message (ethers v6):

import { Wallet, solidityPackedKeccak256, hashMessage, verifyMessage } from "ethers";

const wallet = new Wallet(process.env.PRIVATE_KEY!);

// Version 0x00: sign the raw digest, with no extra prefix
const digest = solidityPackedKeccak256(
  ["bytes1", "bytes1", "address", "uint256", "bytes"],
  ["0x19", "0x00", validatorAddress, 1n, "0x1234"],
);
const sig00 = wallet.signingKey.sign(digest); // { r, s, v }

// Version 0x45: signMessage adds "\x19Ethereum Signed Message:\n<len>"
const sig45 = await wallet.signMessage("hello");
console.log(hashMessage("hello"));            // the digest actually signed
console.log(verifyMessage("hello", sig45));   // recovers wallet.address

Things to know

  • EIP-191 does not prevent replay within the same contract. Add a nonce (as above) or a deadline. It also does not bind a signature to a chain. Include block.chainid if that matters, or use EIP-712, whose domain separator covers both.
  • Never sign a raw 32-byte hash that a dapp gives you without a prefix. Without the prefix, it might be a transaction hash or a digest for some other contract.
  • Smart-contract wallets validate these signatures through ERC-1271, not ecrecover.
  • ERC-4361 (Sign-In with Ethereum) uses personal_sign (0x45) messages.

#signatures #signing #security #wallet #standard

Details

Authors
Martin Holst Swende (@holiman), Nick Johnson <[email protected]>
Status
Final
Type
Standards Track / ERC
Created
2016-01-20
Required by

Machine-readable: Markdown · JSON