diff options
Diffstat (limited to 'packages/meshbay-node/src/meshbay_node/bundle_store.py')
| -rw-r--r-- | packages/meshbay-node/src/meshbay_node/bundle_store.py | 63 |
1 files changed, 52 insertions, 11 deletions
diff --git a/packages/meshbay-node/src/meshbay_node/bundle_store.py b/packages/meshbay-node/src/meshbay_node/bundle_store.py index 4cf3236..7f03caa 100644 --- a/packages/meshbay-node/src/meshbay_node/bundle_store.py +++ b/packages/meshbay-node/src/meshbay_node/bundle_store.py @@ -3,7 +3,10 @@ Bundle store — SQLite-backed storage for GEK bundles and keypair bundles. GEK bundles: ECIES-wrapped GEK targeted at a specific user's X25519 key. Keypair bundles: AES-GCM encrypted (Ed25519 + X25519) private keys, encrypted -with the user's password-derived bundle_key. Opaque to the node. +with the user's password-derived bundle_key. Opaque to the node. An optional +second copy (bundle_enc_recovery) is wrapped under the account's recovery key +instead, so a forgotten passphrase does not strand the identity — see +docs/auth-confirm.md §4.3. Both are stored and served over the P2P DataChannel during MNP handshake. """ @@ -29,9 +32,10 @@ CREATE TABLE IF NOT EXISTS gek_bundles ( _SCHEMA_KEYPAIR = """\ CREATE TABLE IF NOT EXISTS keypair_bundles ( - user_id TEXT PRIMARY KEY, - bundle_enc TEXT NOT NULL, - stored_at TEXT NOT NULL DEFAULT (datetime('now')) + user_id TEXT PRIMARY KEY, + bundle_enc TEXT NOT NULL, + bundle_enc_recovery TEXT, + stored_at TEXT NOT NULL DEFAULT (datetime('now')) ); """ @@ -46,8 +50,22 @@ class BundleStore: self._db = await aiosqlite.connect(str(self._db_path)) await self._db.execute(_SCHEMA_GEK) await self._db.execute(_SCHEMA_KEYPAIR) + await self._migrate_keypair_recovery() await self._db.commit() + async def _migrate_keypair_recovery(self) -> None: + """ + Add bundle_enc_recovery to a keypair_bundles table created before it + existed. SQLite has no ADD COLUMN IF NOT EXISTS, so check the columns + first — this table is node-only and has no Alembic history. + """ + assert self._db + async with self._db.execute("PRAGMA table_info(keypair_bundles)") as cur: + cols = {row[1] for row in await cur.fetchall()} + if "bundle_enc_recovery" not in cols: + await self._db.execute( + "ALTER TABLE keypair_bundles ADD COLUMN bundle_enc_recovery TEXT") + async def store( self, group_id: str, @@ -81,23 +99,46 @@ class BundleStore: "wrapped_b64": row[2], } - async def store_keypair(self, user_id: str, bundle_enc: str) -> None: + async def store_keypair( + self, + user_id: str, + bundle_enc: str, + bundle_enc_recovery: str | None = None, + ) -> None: + """ + Store the passphrase-wrapped keypair bundle, and optionally a second + copy wrapped under the account's recovery key. + + A call that omits bundle_enc_recovery — a plain re-backup, or a + passphrase-change re-wrap (docs/auth-confirm.md §3.2) — must not erase a + recovery copy already stored, so the upsert keeps the existing value + when the new one is None. + """ assert self._db await self._db.execute( - "INSERT OR REPLACE INTO keypair_bundles " - "(user_id, bundle_enc, stored_at) VALUES (?, ?, datetime('now'))", - (user_id, bundle_enc), + "INSERT INTO keypair_bundles " + "(user_id, bundle_enc, bundle_enc_recovery, stored_at) " + "VALUES (?, ?, ?, datetime('now')) " + "ON CONFLICT(user_id) DO UPDATE SET " + " bundle_enc = excluded.bundle_enc, " + " bundle_enc_recovery = COALESCE(excluded.bundle_enc_recovery, " + " keypair_bundles.bundle_enc_recovery), " + " stored_at = excluded.stored_at", + (user_id, bundle_enc, bundle_enc_recovery), ) await self._db.commit() - async def fetch_keypair(self, user_id: str) -> str | None: + async def fetch_keypair(self, user_id: str) -> dict | None: assert self._db async with self._db.execute( - "SELECT bundle_enc FROM keypair_bundles WHERE user_id = ?", + "SELECT bundle_enc, bundle_enc_recovery FROM keypair_bundles " + "WHERE user_id = ?", (user_id,), ) as cursor: row = await cursor.fetchone() - return row[0] if row else None + if not row: + return None + return {"bundle_enc": row[0], "bundle_enc_recovery": row[1]} async def delete_keypair(self, user_id: str) -> bool: """ |