Solidity theorytheory 0/50 · 0%
Cryptography · hard

35. Hashing and signatures

keccak256, ecrecover and EIP-712.

`keccak256(abi.encode(...))` gives a collision-resistant digest; prefer `encode` over `encodePacked` when two dynamic values could be ambiguous. `ecrecover` returns the signer of a hash, letting users authorise actions off chain.

bytes32 digest = keccak256(abi.encode(user, amount, nonce));
require(ecrecover(digest, v, r, s) == signer, "bad sig");

EIP-712 gives typed, human-readable signing and binds signatures to a domain (chain id and contract) so they cannot be replayed elsewhere. Always include a nonce and a deadline.

Check your understanding

  1. 1. Why avoid `encodePacked` with two dynamic types?

  2. 2. What does EIP-712 domain separation prevent?

  3. 3. What stops a signature being reused?