Sealed envelope
Six exercises from the 2018 developer course, redone for today. The solutions are already on the page, sealed in a hash: they open only with the right answer.
In autumn 2018, while people at Mani al Cielo were paying with Lightning, inbitcoin's development team was writing the lessons of the bcademy developer course: Jupyter notebooks, one per topic, with code to complete.1 Twenty-one are left, from the third to the sixth week. We took the questions that still hold up and redid them for 2026, using the real numbers from those lessons where we could.
You need Python 3.9 or newer, and that's all: nothing to install, everything with the standard library. Each exercise takes about an hour.
How the envelope works
Under each exercise there is an envelope: a line of 64 characters, the SHA-256 hash of busta-chiusa/ followed by the exercise number, a slash and the answer. Type your answer and your phone computes the hash: if it matches the envelope, you've found the answer, and nobody told you. Not even this page knows it.
The idea isn't ours: a 2018 lesson already gave the solutions this way, as hashes to compare against. It's a cryptographic commitment, and it runs through the whole course: Merkle trees, Lightning invoices, RGB seals. The sixth exercise teaches you to open the envelopes without solving anything, and that is where you see when they hold.
To check from the terminal, with the answer written in lowercase and without spaces:
printf 'busta-chiusa/3/%s' "$RISPOSTA" | sha256sum
1. The hash done by hand
The course syllabus began like this: “create your own hash function”. As an example it gave this one, which multiplies a prime number for each letter, raised to a power that depends on the letter and its position:
def basic_hash(in_str):
m = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
v = 1
base = ord('a')
MAX = 2 ** 64
for i, c in enumerate(in_str):
idx = ord(c) - base + 1
assert 0 <= idx <= 25
v *= m[idx] ** (idx + i)
return v % MAX
Try basic_hash('moneta') and basic_hash('monete'): one letter apart, numbers miles apart, as it should be. Then try basic_hash('pizza').
The question: with words made only of lowercase letters, from a to y, how many different results can basic_hash give, at most? Write the whole number. A hint: look at the last bit of the results.
You need JavaScript to open the envelope.
After the envelope: what it teaches
An off-by-one error in the index lets the backtick (`, the only one that carries the 2) in and leaves the z out. Without the 2, every word is a product of odd primes, and the result is always odd: half of the possible values never come out. A real hash function accepts any sequence of bytes and uses all its bits equally. That is why Bitcoin uses SHA-256, and not a function made up at home.
2. The proof in four numbers
A block header doesn't hold the transactions: it holds a single hash, the root of a Merkle tree built on top of them. The 2018 lesson had you build it by hand, with fake two-byte transactions and a four-character hash, so that everything would fit on a sheet of paper:
from hashlib import md5 # MD5 is not secure: here it only makes short numbers
def merge(a, b):
return md5(a + b).digest()[:2]
txids = [bytes.fromhex(x) for x in
'b930 9162 5a4e 9808 f157 7dc1 754e 1ddb e758 e747 59a1 ce11 0b4d 7181 d68b 3790'.split()]
Write make_tree(txids), which returns the levels of the tree, from the leaves to the root. If the root comes out as b63d, it's the same tree as in 2018. Then write make_proof(index, tree) and check_proof(txid, root, proof): the proof that 1ddb is in the block is four numbers, not sixteen. The witness shows which ones, to tap with your finger.
The lesson then asked: what if the transactions are an odd number? Bitcoin pairs the last one with itself. Try it with a block of seven:
third_block = [bytes.fromhex(x) for x in '1ff1 df9f 491a 952d 4691 0efa 7245'.split()]
The question: what is the root of third_block, with Bitcoin's rule? Write the four hexadecimal characters.
You need JavaScript to open the envelope.
After the envelope: the duplicate
The 2018 notebook closed with a mischievous question: can you find another list of transactions with the same root? Just repeat the last one, 7245, and the root doesn't change. It's a real flaw of Bitcoin's tree, CVE-2012-2459, and Bitcoin Core's code explains it in a comment that begins with “WARNING!” and speaks straight to the person learning. The defence: a block with two identical transactions at the end of a level is discarded as “bad-txns-duplicate”, but its header is not marked invalid, because the same header also belongs to the good block.6
3. Twelve words, the last one your choice
A wallet's recovery words follow BIP-39: 128 random bits, plus 4 check bits taken from the SHA-256 of those 128, make 132 bits, that is twelve groups of 11. Each group is the number of a word in a list of 2,048.2 The 2018 lesson had you build it step by step.
Download the official English list, english.txt from the BIP repository, and check it's the right one: its SHA-256 is 2f5eed53a4727b4bf8880d8f3f199efc90e58503646d9ff8eff3a2ed3b24dbda. Write valid_mnemonic(words), which checks the check bits.
To test your code, use only the public BIP-39 test phrases, such as eleven times abandon and then about. The words of a real wallet never go into a program or onto a website: you write them on paper and that's it.
A check halfway: with hashlib.pbkdf2_hmac("sha512", ...), 2,048 rounds and the test passphrase TREZOR, the seed of that phrase starts with c55257c360c07c72.
The question: the first eleven words are abandon. How many words from the list, put in twelfth place, give a valid phrase?
You need JavaScript to open the envelope.
After the envelope: why exactly that many
The last word carries 7 random bits and the 4 check bits. The 7 bits can be chosen freely, the 4 cannot: they are decided by the SHA-256 of everything else. A twelfth word picked at random is right one time in sixteen: that is why, fifteen times out of sixteen, the wallet notices a word copied wrongly.
4. Why two derivations
The 2018 notebook on BIP-32, the one about wallets that generate a tree of keys from a single seed, asked a question and left it there: “why are there two algorithms for creating child keys?”. Normal keys can also be derived from the extended public key alone, the xpub: a watch-only wallet can generate all the addresses without being able to spend. Hardened keys can't. This exercise answers with an attack.
A colleague has shared the xpub of his test account, to set up a watch-only wallet. Then, for a test, he pasted into a chat the private key of address number 5, derived the normal way. These are test-network keys, born from a seed written on this page: anyone can rebuild them, and there is no money on them.
tpub = "tpubD9KA96Wig8KzCRAX9ESPbuaNUd9tkUahrq1j9vR86hUEZkovCnvpg6yZoyeJg7kVjVDaYkJob9BXPg7C9S8D5SsTx6sx3AT9hLrgLtrJp3H"
child_5 = 0x9dbb596bfa26edc00c6e4b90f73227cf1279ce54a8efabe400f9c69106d3aff1
N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 # the order of secp256k1
You need two things from the BIP-32 specification: the format of an extended key (78 bytes in base58 with a checksum: the chain code sits in bytes 13 to 44, the compressed public key in 45 to 77) and the formula for normal derivation. The child key is the sum, modulo N, of the parent key and a number, IL, computed with HMAC-SHA512 using the chain code as the key, and the parent public key plus the index as the message. Everything you need to compute IL is in the xpub.
You don't need to do any elliptic-curve arithmetic. Base58 takes you six lines: it's a change of base, with an alphabet that has no 0, O, I or l.
The question: what is the parent private key? Write it in hexadecimal, 64 characters.
You need JavaScript to open the envelope.
After the envelope: the 2018 answer
With the xpub and a single normal child private key you can climb back to the parent key, and from there to all the keys of the account. BIP-32 says so in its security section: an extended public key must be treated with more care than an ordinary public key.3 That is why wallets derive the upper levels of the path with hardened derivation (m/84'/0'/0'), where the apostrophe means an index from 231 upwards and HMAC computed with the private key, and leave only the last levels, those of the addresses, normal. Whoever receives an xpub also sees all the payments received on that account.
5. Relative time
BIP-68 gives a new meaning to the nSequence field of every input: a minimum wait from the confirmation of the output being spent, in blocks or in multiples of 512 seconds. BIP-112 adds the OP_CHECKSEQUENCEVERIFY instruction, which puts that wait inside the script.4 The rules: if bit 31 is set, the field doesn't count as a wait; if bit 22 is set, the wait is timed; the value sits in the low 16 bits.
The 2018 notebook gave seven numbers to decode:
nsequences = [6, 506353076, 2147483647, 4194310, 4259839, 2147483648, 2822828760]
def decode_nsequence(n_seq):
"""Returns ('blocks', n), ('time', seconds) or ('disabled', 0)."""
Write the function, and before you trust it look at the numbers in hexadecimal: some have bits set where you wouldn't expect them.
The question: how many blocks must the input with nSequence 506353076 wait?
You need JavaScript to open the envelope.
After the envelope: the spare bits
506353076 is 0x1e2e55b4: it has several bits set that BIP-68 doesn't use, and they must be ignored. They are left free on purpose: the notebook asked whether a soft fork could stretch the wait to ten years, and BIP-68 answers that those bits are there precisely to increase the maximum duration or the precision in the future. Today the relative wait works in every Lightning channel: whoever closes a channel alone waits to_self_delay blocks before spending their share, and in that time the other side can punish a close made with an old state.5
At the 52nd Bitcoin Milano Meetup, on 13 June 2018, the channel was explained exactly this way: Alice holds a transaction that is valid only from 31 December, so she knows she'll get her money back even if Bob disappears, and Bob knows he has to close before then.8
A note on the 2018 envelopes. The notebook gave the solutions of this exercise as seven RIPEMD-160 hashes, with the function to compute them. Redoing the sums today, with the instructor's solution, none of them opens: whoever sealed them used a slightly different recipe from the one written down, and we haven't found it. An envelope is worth as much as the recipe published with it: that is why the recipe is written at the top of the page here.
6. Open the envelope
This envelope holds four hexadecimal characters, from 0000 to ffff, and no exercise to solve.
The question: what are they? You have the recipe, you have Python, and there are only 65,536 possibilities.
You need JavaScript to open the envelope.
With the same loop you open envelopes 2 and 5 without doing the exercise, and 3 and 1 with a few reasoned attempts. Only 4 holds, because it hides a 256-bit number. A hash only hides what is hard to guess: if the possible answers are few, the envelope is made of glass.
Lightning uses the same envelope, in the version that holds. Every invoice contains a payment_hash, the SHA-256 of 32 random bytes known only to the person being paid: the preimage. The payee collects only by revealing the preimage, and the payer ends up holding it as a receipt.7 Nobody can open the envelope beforehand, because nobody makes 2256 attempts. At the same Milan meetup the contract was described as a black box from which bitcoin comes out when someone shows the preimage of a hash; three weeks later, in Venice, Amadori explained it with Alice who has to pay Eric: Eric creates the secret and gives Alice only the hash.9
In 2019 inbitcoin told where the idea leads, in its contribution to a research project with the University of Calabria: single-use seals, “a digital version of the numbered strap for parcels”, and RGB, born in 2015-2016 from the research sessions of Blockchainlab in Milan, in which inbitcoin took part.10 There the envelope sits inside any ordinary bitcoin transaction, hidden in a public key: it isn't read, it's proved afterwards, to whoever needs it.
What has changed since 2018
The rest of the course was about tools that have changed in the meantime. If you pick the notebooks up again, bear in mind:
- BIP-70, the payment protocol that the course had you use with a test server, was removed from Bitcoin Core in version 0.20, in 2020.11 Today the on-chain payment request is the old BIP-21,
bitcoin:address?amount=…, and for Lightning the BOLT 11 invoice. - The blockchain.info APIs of the syllabus exercises are not needed: your node answers the same questions, and an open source explorer like mempool.space can be installed at home.
- Taproot (BIP-340, 341 and 342) has been active since block 709,632, November 2021: Schnorr signatures, which you can try in the signature.12
- The Lightning specifications no longer live in
lightning-rfc: the repository is called lightning/bolts. - Lighter, the case study of the sixth week, has been stopped since 2020. Its story is in the chronicle.
One thing from 2018 holds as it was: the exercise on Lightning units asked you to convert bitcoin, satoshis and millisatoshis by truncating, never rounding. We add a detail for anyone writing a wallet in the browser: 21 million bitcoin in millisatoshis is 2.1 × 10¹⁸, beyond the limit of exact integers in JavaScript (Number.MAX_SAFE_INTEGER, about 9 × 10¹⁵). For millisatoshis you need BigInt or strings.
In class: heads or tails in an envelope
Fifteen minutes, for a computer science class or for anyone with a phone. You need a real coin, and this seal:
You need JavaScript for the seal.
- Ask for a prediction. Everyone chooses heads or tails, writes it into the seal and copies onto the board, next to their name, the first eight characters (the red ones).
- Toss the coin. Whoever guessed right reveals their choice; the class writes it into the seal again and checks that the same eight characters come out. Nobody can change their mind after the toss.
- Point out the hole. Before the toss, writing “heads” and “tails” into the seal was enough to learn everyone's prediction: there were only two possible answers.
- Repeat with a secret word. Everyone adds words that only the writer knows: “heads cat blue 17”. Now the board says nothing until the reveal.
That secret addition is Lightning's preimage, and the 32 random bytes of an invoice do the same job. For a whole hour with students there is In class; for the reference solutions in Python, write to us.
-
bcademy course, Jupyter notebooks of weeks 3-6 (student and instructor versions) and course syllabus, September-November 2018; inbitcoin archive. The 2018 questions and code quoted here come from there: the
basic_hashfunction (syllabus), the sixteen txids and the block of seven (lesson 5b), the BIP-32 question (lesson 4c), the sevennSequencevalues (lesson 5h), the Lightning units (lesson 6c). ↩ -
BIP-39, read on 20 November 2026. The test phrase and the seed with the passphrase
TREZORare the first of the official test vectors, in the reference repository python-mnemonic. ↩ -
BIP-32, section “Security”, read on 20 November 2026. The extended key in this exercise comes from the SHA-256 seed of “bcademy, Rovereto, novembre 2018”, path
m/0', and the leaked key ism/0'/5. ↩ -
BIP-68 and BIP-112, read on 20 November 2026. The free bits are in the “Deployment” section of BIP-68. ↩
-
Bitcoin Core, src/consensus/merkle.cpp, the comment at the top of the file, and
bad-txns-duplicatein src/validation.cpp, read on 20 November 2026. ↩ -
BOLT 11, the
pfield: “Preimage of this provides proof of payment”. Read on 20 November 2026. ↩ -
52nd Bitcoin Milano Meetup, “Layer 2: Lightning Network”, 13 June 2018, min. 8; the contract with the hash at min. 11. ↩
-
Amadori at the Bitcoin Venezia Meetup, “Lightning Network è arrivato”, 2 July 2018, min. 26. ↩
-
Marco Amadori and Martino Salvetti, The path to Blockchain Enabled Smart Contracts, inbitcoin's contribution to the IOT&B research project with Blockchainlab and the University of Calabria, 3 December 2019, updated 24 February 2020, CC BY-SA 4.0 licence; inbitcoin archive. The quotation is on p. 6; the “small historical parenthesis” on the birth of RGB is on pp. 5-6. ↩
-
Bitcoin Core 0.20.0 release notes: “BIP70 support has been fully removed”. ↩
-
Bitcoin Core 0.21.1 release notes: Taproot active from block 709632. ↩