
Crypto signatures on the web: SIWE, EIP-712 and server-side verification
In a web3 application, a signature often replaces the password: you prove you control an address by signing a message. Easy to use, but easy to get wrong. A signature verified without a nonce, a domain or a chainId can be replayed elsewhere. Here is what you need to understand to do it properly.
The cryptographic building blocks
- secp256k1 / ECDSA: the elliptic curve and signature algorithm used by Ethereum (like Bitcoin). A 32-byte private key yields a public key.
- Address: the last 20 bytes of the keccak256 hash of the public key. The public key itself is never published in clear before the first signature.
- Signature: 65 bytes,
r,sandv. Thevparameter makes it possible to recover the public key from the message and signature (ecrecover), hence the signer's address, without knowing it in advance. - Malleability: for a given valid signature there is a "high"
svariant. Modern libraries only accept the low-sform; never use a raw signature as a unique identifier.
Signing a message: EIP-191
A wallet never signs a raw message: personal_sign prefixes the content with \x19Ethereum Signed Message:\n followed by its length. That prefix prevents a seemingly harmless message from actually being a valid transaction signed without the user knowing.
Sign-In with Ethereum (EIP-4361)
SIWE standardizes the sign-in message so it is readable by the user and verifiable by the server. It contains, among other things, the domain, the address, the URI, the chainId, a nonce and issued-at and expiration times.
The nonce is generated by the server, stored in the session and single-use: it is what prevents an intercepted signature from being replayed. The domain prevents a phishing site from reusing a signature it obtained for itself. Server side with viem:
// app/api/auth/verify/route.ts
import { createPublicClient, http } from "viem";
import { mainnet } from "viem/chains";
import { parseSiweMessage } from "viem/siwe";
import { getSession } from "@/lib/session";
const client = createPublicClient({ chain: mainnet, transport: http(process.env.RPC_URL) });
export async function POST(req: Request) {
const { message, signature } = await req.json();
const session = await getSession();
if (!session.nonce) return new Response("Missing nonce", { status: 400 });
// Verifies the signature (EOA or smart account), domain, nonce and expiration
const valid = await client.verifySiweMessage({
message,
signature,
domain: "example.com",
nonce: session.nonce,
});
session.nonce = undefined; // single use, even on failure
if (!valid) {
await session.save();
return new Response("Invalid signature", { status: 401 });
}
const { address } = parseSiweMessage(message);
session.address = address;
await session.save();
return Response.json({ address });
}EIP-712: signing typed data
To authorize an action (a sell order, a vote, a token permit), you do not sign a string but a typed structure. The wallet displays the fields one by one, and the signed hash is keccak256(0x1901 ‖ domainSeparator ‖ hashStruct(message)).
- The domain separator holds the application name, its version, the chainId and the address of the verifying contract. A signature valid on a testnet or for another contract is therefore not valid here: that is the protection against cross-chain and cross-contract replay.
- A nonce and a deadline in the message: so a signed order can only be executed once, and not six months later.
- Server side, viem's
verifyTypedDatarecomputes the hash with the same types and checks the signer.
What about smart accounts?
A smart account has no single private key: ecrecover cannot verify it. ERC-1271 defines an isValidSignature(hash, signature) function the contract implements with its own logic (multisig, passkey…). For an account that is not deployed yet, ERC-6492 wraps the signature with the deployment data.
Practical consequence: a purely offline check with ecrecover rejects smart wallet users. Verify through a client connected to the chain (which is what verifySiweMessage does above) and never compare the recovered address yourself.
Classic mistakes
- Client-generated or reusable nonce: the signature becomes replayable.
- Domain not checked: a phishing site can get users to sign a message meant for yours.
- Case-sensitive address comparison: addresses carry a checksum casing (EIP-55); compare in lowercase or with
isAddressEqual. - Sessions that never expire: a signature proves control at one point in time; the session should stay short and revocable.
- Unreadable messages: asking users to sign an opaque hash trains them to sign anything. Prefer SIWE and EIP-712.
Conclusion
Ethereum signatures rest on solid primitives: ECDSA, keccak256, public key recovery. The flaws almost always come from the application: missing nonce, ignored domain, forgotten chainId, smart accounts not supported. With SIWE for authentication, EIP-712 for authorizations and ERC-1271-aware server verification, you get robust passwordless authentication.