EIPs Explained

ERC-5202 · Final · ERC · 2022-06-23

ERC-5202: Blueprint contract format

A tiny bytecode header (0xFE71…) that marks a contract as a 'blueprint', meaning stored initcode for factories to copy, so it cannot be executed directly and tools can recognise it.

What problem does it solve?

Factory contracts that deploy many copies of a contract would bloat past the EIP-170 24 KB code limit if they embedded the child's initcode. A common trick is to store the initcode on-chain as its own contract (a blueprint), then EXTCODECOPY it into memory and call CREATE/CREATE2.

But a blueprint is just code to the EVM, which creates two problems:

  • Safety: anyone can call the blueprint and run the initcode as ordinary runtime code, triggering storage writes, external calls or even SELFDESTRUCT, which could break every factory depending on it.
  • Tooling: indexers and explorers cannot reliably tell a blueprint from a normal contract.

How it works

A blueprint's deployed bytecode starts with a preamble:

0xFE 0x71 <version:6 bits | length-encoding:2 bits> [length bytes] [data] <initcode>
  • 0xFE is INVALID, so a direct call halts immediately and the initcode never runs as runtime code.
  • 0x71 is a magic byte (the last byte of keccak256("blueprint")) that distinguishes blueprints from other code starting with 0xFE.
  • The third byte holds a 6-bit version (currently 0) and 2 length-encoding bits n (0, 1 or 2). If n > 0, the next n bytes give, big-endian, the length of an optional data section that indexers can skip. 0b11 is reserved for a future multi-byte version.
  • Everything after that is the initcode, which MUST be at least one byte. Its length is not stored; use EXTCODESIZE.

Examples from the spec: 0xFE710000 is a blueprint whose initcode is STOP, and 0xFE71|01|07|FFFFFFFFFFFFFF|00 adds a 7-byte data section.

The spec notes it may be obsoleted if EOF (EIP-3540) ever adds a native initcode section.

Sample code

Parsing a blueprint off-chain (TypeScript, ethers v6):

import { JsonRpcProvider, getBytes } from "ethers";

async function readBlueprint(provider: JsonRpcProvider, addr: string) {
  const code = getBytes(await provider.getCode(addr));
  if (code[0] !== 0xfe || code[1] !== 0x71) throw new Error("not a blueprint");
  const version = code[2] >> 2;
  const nLen = code[2] & 0b11;
  if (nLen === 3) throw new Error("reserved length encoding");
  let dataLen = 0;
  for (let i = 0; i < nLen; i++) dataLen = (dataLen << 8) | code[3 + i];
  const start = 3 + nLen;
  const data = nLen ? code.slice(start, start + dataLen) : null;
  const initcode = code.slice(start + dataLen);
  if (initcode.length === 0) throw new Error("empty initcode");
  return { version, data, initcode };
}

Things to know

  • Status: Final. Vyper's create_from_blueprint builtin is built around blueprint contracts of this kind.
  • Factories must skip the preamble when copying: EXTCODECOPY from offset 3 + n + dataLen, not 0.
  • A blueprint is not a proxy. Each deployment from it is an independent contract, unlike ERC-1167 minimal proxies, which delegate to shared logic.
  • Pre-existing contracts could start with 0xFE71 by chance; the spec judged this low risk and says that, as of July 2022, none on mainnet did.

#factory #bytecode #deployment #vyper #create2

Details

Authors
Charles Cooper (@charles-cooper), Edward Amor (@skellet0r)
Status
Final
Type
Standards Track / ERC
Created
2022-06-23
Requires

Machine-readable: Markdown · JSON