Chainhack
I had the great chance to attend this year’s Chainhack, a hackathon that is regularly organized by Chainsmiths. In its third incarnation this year, Shift got involved with three developers — Stephanie, Kaspar and me.
The event began on a Friday evening with an idea-pitching round. Some nice projects were suggested: a Lightning Network pay-per-call API middleware, a simpler interface for Bitcoin’s CHECKLOCKTIMEVERIFY / CHECKSEQUENCEVERIFY op codes in combination with multisig, a dead man’s switch for the Ledger Nano, or a Lightning hardware wallet.
As developers of a hardware wallet, it’s no surprise that we were attracted to the Lightning hardware idea, as were 10 other hackers. We formed a team of 13 and quickly divided the work into three parts: a web front end for c-lightning with hardware detection, c-lightning bindings for communicating with the hardware, and the firmware that would run on the hardware.
Stepan Snigirev, the brains behind the idea, brought some hardware to the event for us to play with. We used an Adafruit Feather M0 development board with a screen as the device. Together with the Arduino IDE, it is easy and fun to program. Of course, the Feather M0’s design is far from being secure enough for a consumer hardware wallet, but it sufficed as a proof-of-concept for how a Lightning hardware wallet could be realized. The firmware would store the cryptographic secrets, display transaction information to the user, ask for user confirmation, and sign transactions.
C-lightning has an interesting architecture, with its functionality divided into individual processes that communicate with each other. The handling and signing of transactions, for example, is separated into a sub-process daemon called hsmd. It writes 32 Bytes of secret entropy to a file called hsm_secret (nobody will ever find it there) and from this secret derives all subsequent keys. A schematic of the derivation scheme can be found here.
One of our goals for this proof-of-concept was to eliminate the need for this hsm_secret file to be saved on-disk. Instead, storage of the secret and the creation of all signatures would be on the hardware device itself. When it comes to security, segregation is vital. Private keys must be private and that means ring fenced away from your computer which is vulnerable to malware.
Currently, some secret information and keys still leak out of the hsmd process (partly through the BIP32 data structure). This should be fixed soon, according to the c-lightning maintainers, which would make c-lightning perfectly suited for direct usage with a hardware wallet. We tapped into the hsmd/hsm.c file and extended its functionality to communicate only with the hardware device when storing the secrets and signing transactions.
On Saturday, we continued working hard day and night to complete this task. On Sunday, our team leaders Stepan and Henrik put in the final effort by knitting the frontend (written in React), backend (c-lightning and a small Python flask server), and hardware device together. We had a working demo, complete with hardware integration and a nice web UI!
We also got the chance to checkout the other projects over the past two-and-a-half days. I was very impressed by Microbolts, a pay-per-byte video streamer over Lightning. It ran smoothly and the UI was quite intuitive.
Finally, the winners of the hackathon were announced:
3rd place: the dead man’s switch with a Ledger Nano
2nd place: the Lightning video streamer
1st Prize: C-lightning hardware wallet!
We are thankful for the organisers, sponsors and judges for the event. Even though the hackathon is over, we are motivated to continue working on this project. Our plan has three parts:
- Make sure the secret handling of c-lightning becomes secure
- Integrate support for the Lightning Network into the BitBox
- Tinker further on the hobbyists’ hardware implementation
Key management is hard and it should be a priority that proper infrastructure for it is built early. If you would be interested in tracking the progress, check out our team’s code for the Lightning hardware code.