BC2: Keys and digital signatures

Lesson 2 of 5 ยท reading 15โ€“25 minutes; practice and project time is on the module page

Signing and verifying

๐ŸŽฏYou'll be able to

Sign a message, verify a signature, and say exactly what a valid signature proves.

๐Ÿ The everyday picture

A wax seal pressed with a signet ring: anyone who knows the ring's pattern can check the seal, but only the ring's owner can make it.

๐Ÿ“˜What it really is

๐Ÿ“A worked example

Input: M01 signs "Approve payment of 0.2 to M04".

Steps:

  1. M01's wallet hashes the message and signs it.
  2. The treasurer receives the message and signature, and recovers the signer's address with ethers' verifyMessage.
  3. It matches M01's address, so the treasurer knows M01's key approved this exact text.
  4. If someone changes "0.2" to "2.0", the recovered address is a different, random one, and the check fails.

Expected output: a check that passes only for M01's key and the exact message.

๐ŸŒWhere it's used

๐Ÿ“Case scenario

A team checked that ecrecover returned "an owner" but forgot that it returns the zero address for a broken signature, and the zero address had been set as an owner by mistake. Anyone could send garbage signatures and pass. The fix: refuse the zero address as an owner and as a recovered signer.

๐Ÿ–ฑ๏ธTry it

With ethers in the kit folder, sign a message with a random wallet and verify it, then change one character and verify again. Success: the first check returns the wallet's address and the second returns a different one.

โš ๏ธCommon mistakes

๐Ÿ› ๏ธFor your project

Project 2's executeWithSignatures recovers each signer and checks they're an owner (tests 8 and 9).

โœ…Quick check

What does a valid signature prove?

Show the answer

That the holder of the private key approved those exact bytes.

What does ecrecover return for a broken signature?

Show the answer

The zero address, which must be refused.

โ† PreviousPublic and private keys, and addresses
ProLearners Assist