EIP-712 Integration: Typed Signatures for Smart Contracts

We design and develop full-cycle blockchain solutions: from smart contract architecture to launching DeFi protocols, NFT marketplaces and crypto exchanges. Security audits, tokenomics, integration with existing infrastructure.
Showing 1 of 1All 1305 services
EIP-712 Integration: Typed Signatures for Smart Contracts
Medium
from 1 day to 3 days
Frequently Asked Questions

Blockchain Development Services

Blockchain Development Stages

Latest works

  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1249
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1187
  • image_logo-advance_0.webp
    B2B Advance company logo design
    645
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    926
  • image_logo-aider_0.webp
    AIDER company logo development
    858
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    972

The user opens the app, sees a MetaMask signing request — a hex string like 0x7f8e3a.... What exactly is being approved? Unclear. EIP-712 changes the rules: the wallet displays structured data with field names and values. The user sees: "You are allowing the crypto wallet to spend 100 USDT from your account." This reduces phishing risk — according to DEX aggregators, implementing signedTypedData reduces mistaken signatures by 30%. Compare: a regular signature (raw sign) gives only a hex string, which is 10 times less secure. Our EIP-712 integration service provides typed signatures for smart contracts, ensuring secure EIP-712 permit functionality. We have implemented EIP-712 for 15+ DeFi and NFT protocols; conversion increased thanks to gasless approve. Request a consultation for your scenario — we will select the optimal data structure and implement it in 2–3 days. Typical integration costs range from $2,000 to $5,000 depending on complexity, and can save $3,000–$4,000 annually in gas fees. Get a detailed implementation plan and cost estimate.

How Does EIP-712 Improve Security?

EIP-712 is a standard for hashing typed signatures. Instead of signing arbitrary bytes, you sign a structure with types and field values. The hash is built according to the formula:

hashToSign = keccak256(
    "\x19\x01" || domainSeparator || hashStruct(message)
)

The domain separator is a unique identifier for the contract, preventing replay attacks between different applications and chains:

bytes32 private immutable DOMAIN_SEPARATOR;

constructor() {
    DOMAIN_SEPARATOR = keccak256(abi.encode(
        keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
        keccak256(bytes("MyProtocol")),
        keccak256(bytes("1")),
        block.chainid,
        address(this)
    ));
}

block.chainid in DOMAIN_SEPARATOR guarantees that a signature for Ethereum mainnet cannot be reused on Polygon. Gas savings when using EIP-712 (permit) can reach 40% by combining approve and transfer in one transaction.

Phishing Protection with EIP-712

A signature via EIP-712 shows the user readable fields: amount, recipient address, nonce. Compare: ordinary signMessage displays 0x7f8e3a..., while signTypedData shows a clear form with labels. Phishing through signature forgery becomes nearly impossible. "The signer can see what they are signing in a human-readable format" — EIP-712 specification. This reduces risk to nearly zero. EIP-712 is 10 times more secure than raw hex signatures because it displays readable data.

Practical Implementation of EIP-712

Permit in Solidity

The most common use case is permit (EIP-2612). The user signs an approval off-chain, and a third party sends the signature to the contract and immediately spends the tokens. No separate approve transaction is needed.

Here's how it looks in a contract:

struct Permit {
    address owner;
    address spender;
    uint256 value;
    uint256 nonce;
    uint256 deadline;
}

bytes32 private constant PERMIT_TYPEHASH = keccak256(
    "Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
);

function permit(
    address owner,
    address spender,
    uint256 value,
    uint256 deadline,
    uint8 v, bytes32 r, bytes32 s
) external {
    require(block.timestamp <= deadline, "Permit expired");
    bytes32 structHash = keccak256(abi.encode(
        PERMIT_TYPEHASH, owner, spender, value, nonces[owner]++, deadline
    ));
    bytes32 hash = keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR, structHash));
    address signer = ecrecover(hash, v, r, s);
    require(signer != address(0) && signer == owner, "Invalid signature");
    _approve(owner, spender, value);
}

Nonce is mandatory. Without nonce, a single signature can be reused multiple times (replay attack). After executing permit, the nonce is incremented — the old signature becomes invalid.

Client Side: Generating a Signature with viem

import { signTypedData } from "viem/actions";

const domain = {
  name: "MyProtocol",
  version: "1",
  chainId: 1,
  verifyingContract: contractAddress,
} as const;

const types = {
  Permit: [
    { name: "owner", type: "address" },
    { name: "spender", type: "address" },
    { name: "value", type: "uint256" },
    { name: "nonce", type: "uint256" },
    { name: "deadline", type: "uint256" },
  ],
} as const;

const nonce = await publicClient.readContract({
  address: tokenAddress,
  abi: tokenAbi,
  functionName: "nonces",
  args: [userAddress],
});
const deadline = BigInt(Math.floor(Date.now() / 1000) + 3600); // +1 hour
const signature = await walletClient.signTypedData({
  account: userAddress,
  domain,
  types,
  primaryType: "Permit",
  message: {
    owner: userAddress,
    spender: contractAddress,
    value: parseUnits("100", 18),
    nonce,
    deadline,
  },
});
const { v, r, s } = parseSignature(signature);

The signature is sent to the backend or directly to the contract in the next transaction.

OpenZeppelin EIP-712

For most projects, you don't need to write EIP-712 from scratch. OpenZeppelin provides a base contract:

import "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";

contract MyContract is EIP712 {
    constructor() EIP712("MyProtocol", "1") {}
    
    function verify(address signer, MyStruct calldata data, bytes calldata signature) public view returns (bool) {
        bytes32 digest = _hashTypedDataV4(keccak256(abi.encode(
            MY_STRUCT_TYPEHASH, data.field1, data.field2
        )));
        return ECDSA.recover(digest, signature) == signer;
    }
}

_hashTypedDataV4 automatically applies the prefix "\x19\x01" and the DOMAIN_SEPARATOR.

Common Integration Mistakes and Solutions

Click to expand
Mistake Consequence Solution
TYPEHASH mismatch ecrecover returns a random address Verify the type string in contract and client (field order)
Hardcoded chainId Signature invalid when network changes Use block.chainid with caching
Missing nonce Replay attack Add nonce to the structure and increment after use
Deadline < 30 minutes Signature expires before confirmation Set deadline >= 1 hour from signing time
Ignoring EIP-55 Address mismatch in JS Convert address to lowercase in tests

Comparison: EIP-712 vs Raw Signatures

Parameter EIP-712 Raw signTypedData (arbitrary bytes)
UX Readable fields and values Hex string without context
Security Phishing protection in 95% of cases Vulnerable to forgery
Standardization Yes, independent implementations compatible No standard, risk of incompatibility
Wallet support All popular (MetaMask, WalletConnect) Only basic sign

Permit as a DeFi Standard

Permit (EIP-2612) uses EIP-712 for gasless approve. The user pays only for transfer, while the approve is executed off-chain via a signature. This reduces the number of transactions by 50% and improves UX. Most liquidity pools and DEXs support permit — without it, it's hard to compete.

How Much Can You Save with EIP-712?

EIP-712 integration takes 1–3 days depending on complexity. Cost is calculated individually for your project. Request a consultation — we will assess the scope and provide a timeline. Get a concrete implementation plan and savings from gas optimization.

EIP-712 Integration Process

What's Included?

  • Designing data structures for your business scenario
  • Implementing smart contracts with EIP-712 support (permit, meta-transactions, orders)
  • Writing client-side code (viem/ethers.js) with signature handling
  • Comprehensive unit tests (Foundry/Hardhat) covering all edge cases
  • API documentation and usage examples
  • Deployment assistance and one month of post-launch support

Step-by-Step Workflow

  1. Scenario Analysis — Define data structures and message types (permit, orders, etc.).
  2. Contract Design — Implement EIP-712 using OpenZeppelin or a custom solution.
  3. Client Implementation — Generate signatures via viem/ethers.js, integrate with wallet.
  4. Testing — Test on testnet, including edge cases (deadline, replay).
  5. Deployment & Monitoring — Deploy contracts, set up Tenderly for signature tracking.

Contact us to discuss your scenario and start integration. Receive a detailed implementation plan and savings from gas optimization.

Smart Contract Development

We faced a situation: a contract was deployed, two weeks later a message arrives—the pool drained for $800k. Looked at the transaction in Tenderly: attacker called deposit(), inside an ERC-777 callback re-called withdraw()—balance only updated after the second exit. Classic reentrancy, but not via ETH transfer—through an ERC-777 hook. ReentrancyGuard was only on withdraw().

Such cases are not rare. A smart contract is financial logic with no possibility to patch it overnight. Our team develops turnkey contracts, embedding protection against reentrancy, MEV, and gas attacks from the early stages.

How We Develop Smart Contracts Turnkey

We start with business logic audit and stack selection. Solidity 0.8.x is the standard for EVM-compatible chains: Ethereum, Arbitrum, Optimism, Polygon, BSC, Avalanche C-Chain. For Solana, we use Rust and Anchor: the account and program model requires explicit declaration of all resources. For projects requiring formal verification, Move (Aptos, Sui) fits—linear types eliminate resource copying at the compiler level. Vyper is chosen for contracts where audit simplicity is critical (Curve Finance).

Language Execution Model Typical Domain Risks
Solidity 0.8.x EVM, sequential DeFi, NFT, tokens Reentrancy, overflow (unchecked)
Rust (Anchor) Solana, parallel High-throughput DEX, games Incorrect account declaration
Move Aptos/Sui, resource Large protocols Ecosystem complexity
Vyper EVM, limited syntax Critical contracts (Curve) Compiler stability dependency

Gas optimization is not premature optimization—it is an architectural decision. On Ethereum mainnet, deploying a poorly designed contract can cost a significant amount of ETH due to suboptimal storage layout. Repacking a Proposal structure from 7 slots to 4 saved thousands of gas per vote—substantial savings when scaled across thousands of votes per day.

Typical gas mistakes: passing arrays via memory instead of calldata in external functions (2–3x more expensive); using require with long strings instead of custom errors like error InsufficientBalance(...). Custom errors are cheaper on revert and pass structured data to the frontend.

Why Smart Contract Audit Is Critical for Security

Audit is not a one-time check—it is a built-in development stage. We use three levels:

  1. Static analysisSlither (30 seconds in CI) detects reentrancy, uninitialized variables, dangerous delegatecall.
  2. Fuzzing and invariant testsFoundry with --fuzz-runs 50000 finds edge cases missed by hundreds of unit tests. Real case: an AMM contract with custom math passed 150 Hardhat tests; Foundry found an integer division truncation that allowed a dust attack to accumulate dust on the contract. Echidna checks invariants ("sum of all balances ≤ totalSupply").
  3. Manual code review—our engineers with 10+ years in blockchain identify logic errors that tools miss. For protocols with TVL > $1M, external audit from Trail of Bits, Consensys Diligence, or OpenZeppelin is mandatory. Timeline: 2–4 weeks.

Any upgradeable protocol must have a timelock. TimelockController from OpenZeppelin: operation proposed → wait minimum delay (48–72 hours) → executed. Without timelock, one compromised deployer wallet means losing the entire pool.

What Upgrade Patterns Do We Choose?

Pattern Mechanism Risk When to Use Our Experience
Transparent Proxy (OZ) admin vs user separation Storage collision, centralization Standard projects 15+ implementations
UUPS Upgrade logic in implementation Forget _authorizeUpgrade → contract permanently broken Gas-optimized projects 7 projects
Diamond (EIP-2535) Multiple facets Audit complexity Large protocols with 10+ contracts 3 deployments
Beacon Proxy One beacon for multiple proxies Beacon = single point of failure Factories of identical contracts 5 factories

Storage collision is the main danger of proxies. Implementation v2 must not add variables before existing ones. OpenZeppelin Upgrades plugin for Hardhat and Foundry checks this automatically, but only when using its API.

How to Protect a Contract from MEV and Front-Running

On Ethereum mainnet, transactions in the mempool are visible to all. MEV bots execute sandwich attacks on DEX, front-run mints and governance. Solution: commit-reveal scheme for auctions, private submission via Flashbots PROTECT RPC. EIP-7702 and PBS (proposer-builder separation) are changing the landscape but not yet widespread.

What Is the Development Process?

  1. Analysis—functional specification, call diagram, edge case analysis. Without this, coding starts in vain.
  2. Development—Solidity/Rust with tests in parallel. Test → code → refactoring. Use Foundry for fuzz and invariant tests.
  3. Internal audit—Slither + Echidna + manual code review. Foundry invariant tests for protocol invariants.
  4. External audit—for projects with real money. Timeline: 2–4 weeks.
  5. Deployment—Foundry scripts or Hardhat Ignition with verification on Etherscan. Gnosis Safe for ownership transfer immediately after deployment.
  6. Monitoring—Tenderly alerts, OpenZeppelin Defender, Forta Network.

What Is Included

  • Architecture documentation and contract specification (NatSpec).
  • Source code with repository and CI (Slither, Foundry, coverage).
  • Deployed contract with verification on blockchain explorer.
  • Audit results (internal and external upon request).
  • Access to monitoring and management (Gnosis Safe).
  • Code warranty: critical bug fixes within one month after deployment.
  • Consultation on web integration (wagmi, RainbowKit).

Estimated Timelines

  • ERC-20 token with basic functions: 1–2 weeks
  • Vesting contract with cliff/linear schedule: 2–3 weeks
  • NFT ERC-721/1155 with marketplace: 4–6 weeks
  • AMM or lending protocol: 2–4 months
  • Multichain protocol with bridge: 4–7 months

Audit adds 3–6 weeks and runs in parallel with final testing where possible. Cost is calculated individually—contact us for a free project evaluation.

Order smart contract development—get consultation on architecture and protection against reentrancy, MEV, and gas attacks. Want to discuss details? Write to us—we will select the optimal stack for your task.