Digital signatures and signature recovery
Suggest an editA node receives a transaction from an open network and has to decide whether it came from someone authorised to spend these funds. A digital signature lets any node check this using only the sender's public key.
The network cannot ask who you are
You hold a private key, and you want to tell the network "move some value from my account to this other account." The network has never met you, and it cannot ask for your password because there is no password and no one to receive it.
What you can send is a transaction plus a signature on that transaction. Anyone with your public key can then check whether the signature is valid for that exact transaction. If it is, the network accepts that the transaction came from whoever holds the private key that pairs with that public key. If it isn't, the transaction is rejected.
For this to work, the signature scheme must guarantee three things:
- Only the holder of the private key can produce a valid signature for a given message.
- Anyone with the public key can verify that a given signature was produced for that message by that private key.
- The signature is tied to the exact message. Change one bit of the transaction and the same signature no longer verifies, so an old signature cannot be reused on a different transaction.
Suppose you sign "send 1 coin from my account to Bob" and send it to the network. Any node checks the signature against your public key and sees that it is valid. Now suppose someone changes the amount to 10 on the way. The signature was produced for the message that says 1, so the check on the changed message fails and every node rejects it. To make the changed transfer valid the attacker needs a new signature, and only your private key can produce one.
Signing needs the private key, verifying does not
The same key pair that powered the encryption examples works in the opposite direction: instead of encrypting to the public key, you sign with the private key, and anyone verifies against the public key. A signature scheme has two operations.
Sign takes the private key and a message and produces a signature, which is a short value, typically 64 or 65 bytes.
Verify takes the public key, the message, and the signature, and returns either "valid" or "invalid."
Verifying does not require the private key. The wallet that sent the transaction holds the private key, and every node that validates the transaction has only the public key, the message, and the signature.
The most common signature scheme on the chains you'll meet is ECDSA, the Elliptic Curve Digital Signature Algorithm, used with the same secp256k1 curve that produces the key pairs. Bitcoin and Ethereum both use it. Ed25519 has its own integrated signing scheme (technically called EdDSA) and is used by Solana and some newer chains.
ECDSA has one weakness that a wallet has to handle. Each signature needs a fresh random value called the nonce. If a wallet signs two different messages with the same nonce, an attacker who sees both signatures can compute the private key, and real wallets have been drained this way. Modern wallets follow a standard called RFC 6979, which makes the nonce a deterministic function of the private key and the message instead of relying on the device's random number generator.
Ethereum computes the sender's public key from the signature
The verify operation takes the public key as an input, so a node has to get the sender's public key from somewhere. Bitcoin and Solana put it in the transaction itself (a later lesson, The UTXO model, shows where it sits in a Bitcoin transaction). Ethereum leaves it out, which makes every transaction smaller, and computes it instead.
This works because ECDSA has a property called public-key recovery: given the message and the signature, anyone can compute the public key that signed it. The signature carries one extra byte for this, called the recovery id, which is why it is sometimes 65 bytes and not 64.
An Ethereum node recovers the public key from each transaction as it arrives and hashes it to get the sender address. If the chain's state shows that this address has the funds being moved, the transaction is accepted. If not, it is rejected. Smart contracts can run the same recovery through a built-in function called ecrecover, which is how a contract checks that a user signed a message.