Skip to content

Commit 074793b

Browse files
committed
gui: Verify recovery before import
1 parent 78f4a3b commit 074793b

4 files changed

Lines changed: 106 additions & 2 deletions

File tree

‎docs/security/invariants.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ and evidence.
1111
3. Shared creation uses a separate OS-CSPRNG call for each random initial share,
1212
gated by confirmation. Input cannot replace entropy or the original secret.
1313
4. Wallet setup uses the original ceremony result or a validated recovered seed.
14+
Graphical recovery derives public wallet identity first and requires the
15+
operator to confirm it against the separately stored wallet record before
16+
any recovered key material may mutate a Bitcoin Core wallet.
1417
5. Correction shares one mass bound and deadline across target lengths. The
1518
public API fails closed on incomplete required work; CLI searches may return
1619
one primary-best-so-far eligible candidate at the deadline. Incomplete

‎docs/security/model.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ The operator must:
4646
- protect recovery cards and store shared cards in different trusted places;
4747
- confirm every newly recorded secret or share;
4848
- keep wallet records separate from shares and compare recovered fingerprints,
49-
addresses, account, policy, and history with those records;
49+
addresses, account, policy, and history with those records; the graphical
50+
restore requires the fingerprint comparison before it imports recovered keys;
5051
- compare every correction suggestion with the original codex32 string and stop
5152
when recovered information and wallet records disagree; and
5253
- never put recovery text in command arguments or transfer a master seed,
@@ -268,6 +269,14 @@ only while more than one answers. On screen a wallet is chosen by the position o
268269
its row, never by the text of its label, and Core's text is rendered without
269270
Pango markup, so a wallet name cannot hide or impersonate another.
270271

272+
Restore derives the recovered seed's master fingerprint through Bitcoin Core
273+
before destination selection and displays it with the backup identifier. The
274+
operator must explicitly confirm that fingerprint against the separately stored
275+
wallet record before the program lists, creates, unlocks, or imports into a
276+
destination wallet. A mismatch can therefore stop recovery without mutating a
277+
Bitcoin Core wallet. The fingerprint remains only a short diagnostic identifier;
278+
its authentication-strength limitation above still applies.
279+
271280
The program draws no entropy, opens no socket, starts no process of its own, and
272281
writes no file: no settings, no recent list, no log, and no clipboard write of
273282
recovery text. Entered recovery text is cleared when its screen is left, subject

‎src/codex32_gui/pages.py‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,61 @@ def _record(
933933
)
934934

935935

936+
def _restore_identity_page(
937+
view: Adw.NavigationView,
938+
core: BitcoinCore,
939+
secret: MasterSeed,
940+
fingerprint: str,
941+
) -> Adw.NavigationPage:
942+
"""Require the wallet record to match before any recovered key is imported."""
943+
944+
def continue_restore() -> None:
945+
_wallets(view, core, secret, 0, restoring=True)
946+
947+
content = _column(
948+
_title(
949+
"Check the wallet before restoring it",
950+
"Compare this master fingerprint with the wallet record you stored separately from the cards.",
951+
),
952+
_rows(
953+
"Recovered wallet identity",
954+
(
955+
("Backup identifier", secret.header.identifier.upper()),
956+
("Master fingerprint", fingerprint),
957+
),
958+
),
959+
_note(
960+
"If the fingerprint does not match your wallet record, stop. Bitcoin Core has not been changed.",
961+
"warning",
962+
),
963+
)
964+
return _page(
965+
"Verify wallet",
966+
content,
967+
actions=_actions(
968+
_button("Stop", lambda: view.replace([home(view)])),
969+
_button("Matches my record", continue_restore, style="suggested-action"),
970+
),
971+
can_pop=False,
972+
)
973+
974+
975+
def _verify_restore(view: Adw.NavigationView, core: BitcoinCore, secret: MasterSeed) -> None:
976+
"""Derive public identity without mutating Core, then require operator confirmation."""
977+
page = _working(view, "Restore my wallet", "Checking the recovered wallet identity…")
978+
work.run(
979+
view,
980+
page,
981+
lambda: wallet_setup.fingerprint(core, secret),
982+
_then(
983+
view,
984+
page,
985+
lambda fingerprint: _restore_identity_page(view, core, secret, fingerprint),
986+
CARDS_SAFE,
987+
),
988+
)
989+
990+
936991
def _import(
937992
view: Adw.NavigationView,
938993
core: BitcoinCore,
@@ -1410,7 +1465,7 @@ def _restore(view: Adw.NavigationView, core: BitcoinCore, secret: Secret) -> Non
14101465
if not isinstance(secret, MasterSeed):
14111466
_failure(view, "Only a Bitcoin master-seed backup can restore a wallet.")
14121467
return
1413-
_wallets(view, core, secret, 0, restoring=True)
1468+
_verify_restore(view, core, secret)
14141469

14151470

14161471
def _start_restore(view: Adw.NavigationView) -> None:

‎tests/test_gui_boundaries.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,31 @@ def _imports(tree: ast.AST) -> set[str]:
5656
return found
5757

5858

59+
def _named_function(tree: ast.AST, name: str) -> ast.FunctionDef:
60+
return next(node for node in ast.walk(tree) if isinstance(node, ast.FunctionDef) and node.name == name)
61+
62+
63+
def _direct_calls(function: ast.FunctionDef) -> set[str]:
64+
"""Collect calls in one function body without entering callbacks it defines."""
65+
found: set[str] = set()
66+
67+
class Calls(ast.NodeVisitor):
68+
def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
69+
if node is function:
70+
self.generic_visit(node)
71+
72+
def visit_Lambda(self, _node: ast.Lambda) -> None:
73+
return
74+
75+
def visit_Call(self, node: ast.Call) -> None:
76+
if isinstance(node.func, ast.Name):
77+
found.add(node.func.id)
78+
self.generic_visit(node)
79+
80+
Calls().visit(function)
81+
return found
82+
83+
5984
@pytest.mark.parametrize("path", _modules(), ids=lambda path: path.name)
6085
def test_the_gui_draws_no_entropy_opens_no_socket_and_touches_no_file(path: Path) -> None:
6186
imported = _imports(ast.parse(path.read_text()))
@@ -114,3 +139,15 @@ def test_the_gui_keeps_its_own_size_budget() -> None:
114139
for path in _modules()
115140
}
116141
assert sum(counts.values()) < BUDGET, counts
142+
143+
144+
def test_restore_verifies_identity_before_wallet_mutation() -> None:
145+
tree = ast.parse((_package() / "pages.py").read_text())
146+
restore = _named_function(tree, "_restore")
147+
verify = _named_function(tree, "_verify_restore")
148+
identity_page = _named_function(tree, "_restore_identity_page")
149+
150+
assert "_verify_restore" in _direct_calls(restore)
151+
assert "_wallets" not in _direct_calls(restore)
152+
assert "_wallets" not in _direct_calls(verify)
153+
assert "_wallets" not in _direct_calls(identity_page)

0 commit comments

Comments
 (0)