Rebuilding Faraday for auditability and agents
Faraday is an offline Bitcoin signer. Its first version grew fast: about 560,000 lines of Rust in seven weeks, wholly written by AI coding agents. A person started each session, told it what to do and checked what came back. Our review of the Faraday repo found that while the cryptography, key handling and file formats held up well, the app on top of them did not.
So we asked a plain question: should we fix that app where it stands, or start again on a blank, greenfield branch with an architecture chosen up front? We wanted the well-known patterns that keep large codebases easy to change:
- Clean architecture, with data and domain kept separate. The rules of the problem (keys, signatures, file formats) sit in a core that knows nothing of screens, storage or hardware. Everything else depends on the core, and the core depends on nothing. The parts that must be right can then be tested on their own, and the outer parts can change without touching them.
- Unidirectional data flow. Data moves in one direction only: an event comes in, the state changes, and the screen is drawn from the new state. Nothing reaches back. Every change has one place it could have come from, which makes bugs easier to trace and behaviour easier to test.
- The Elm Architecture (TEA). Each feature has a model (its state), the messages it accepts, and one
updatefunction that takes a message and returns the new state. Drawing is a separate function of the state.updateis a pure function, so a test can give it a message and check the result without a screen. - Atomic design for the UI. Screens are built in levels: tokens (colours, sizes), atoms (a button), molecules (a row), organisms (a sheet), templates (a page layout), then pages. A level uses only the levels below it, so a page is put together from parts that were built and tested on their own.
The study of the code added four patterns that fit Faraday:
- Ports and adapters. All contact with the outside world goes through one contract. The device, the desktop demo and the test harness are adapters to it.
- Effects as data.
updatenever does I/O itself; it returns a list of effects (save this, start that job) and a runtime carries them out. This is how TEA handles a real device without giving up pure functions. - Typed state machines for flows. A multi-step flow is an enum of its steps, so a step that should not be reachable cannot be written.
- Composition roots. Each app picks the features it holds in one small place at its edge.
These patterns are preferred for the same reason. Each one gives every rule a single place to live, and a dependency that points the wrong way becomes something a tool can catch. That matters for people, and even more for agents. An agent works well when its task has a clear edge, and it does damage when it can reach anything.
It matters most for auditability. An auditor, or a user who wants to check our work, should be able to find where a key can live, what touches it, and what happens to it on a lock, without reading the whole app. In this design those answers each sit in one place, and a lint holds them there. Code that is easy to audit is also code that can be trusted to stay right as it grows, and for a signing device that is the work that matters.
What the audit found
Before choosing what to do next, we measured the code:
- One crate of 247,000 lines held the whole app. Every feature kept its state in one struct with about 70 fields. Any feature could reach into any other feature's state.
- Drawing a screen also changed the app's state. It wrote touch targets and measurements into it. A change to how a screen looked could change how the app behaved.
- There were 196,000 lines of tests, and few of them said which bug they guarded against. The full check was slow, so it ran rarely.
None of this was a security fault: the audit found no secret kept after its screen or a lock. But each feature cost more to add than the one before it. It was also the wrong shape for agents. An agent has to read far more than its task needs, and nothing stops it from reaching across the code.
A clean start, built to be filled in by agents
The answer was to start again. next is a new branch, started empty. The pure crates were copied over unchanged, with their tests. Everything else is being rebuilt on the patterns above, each one held by code:
- A lint for the layers. It rejects any dependency that points the wrong way.
- The kit's levels are crate boundaries. A page cannot reach past the templates and organisms it is allowed to use.
- A model that cannot be copied. Messages carry a handle to a key, never the key, and a lock throws the whole model away.
- Tests that name what they guard. Each test opens with the rule it protects, the change that would break it, and the layer that owns the rule. A lint checks those three lines.
How the work runs
The specification came first. Before any code was written, it set out every layer, crate, module, type and function signature, and the whole of it compiles as a skeleton where every function body is a todo!(). The number of stubs left is the measure of progress.
The agents work from it one small, well-defined task at a time, with the standard elements of any software project: tracked tasks, written instructions, review, and automated checks. The layer rules, the test headers and the handling of secrets are all lints, so work that drifts from the design fails a check instead of waiting for someone to notice. The full check runs on GitHub every time work lands.
Where it stands
We are working hard on fishing the migration. Just prior to this necessary work, Faraday had undergone a pretty big UX overhaul aimed at simplifying flows and making sure the app inter-operated with itself end to end. Designing Faraday really is designing an entire operating system, just one aimed at being for one specific purpose: offline signing on off the shelf hardware. I look forward to sharing about the UX updates!
The plan and the specification are public on the next branch.