homecubbie-server 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/LICENSE +75 -0
  2. package/README.md +241 -0
  3. package/dist/crypto.js +135 -0
  4. package/dist/home-network.js +103 -0
  5. package/dist/household.js +912 -0
  6. package/dist/http-server.js +604 -0
  7. package/dist/https-listener.js +92 -0
  8. package/dist/main.js +11 -0
  9. package/dist/quiet-warnings.js +34 -0
  10. package/dist/start.js +104 -0
  11. package/dist/store.js +400 -0
  12. package/dist/web-app.js +138 -0
  13. package/package.json +30 -0
  14. package/web/(now)/index.html +474 -0
  15. package/web/(now)/month.html +474 -0
  16. package/web/+not-found.html +474 -0
  17. package/web/_expo/.routes.json +3 -0
  18. package/web/_expo/static/css/global-31a95f46f0d26533c432758949249e15.css +1 -0
  19. package/web/_expo/static/js/web/entry-0f771d01e242dad2ff9cf31c003ad963.js +2839 -0
  20. package/web/_expo/static/js/web/worker-d922322f88a7f1ead9d277776b9d9ebb.js +22 -0
  21. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/arrow_down.017bc6ba3fc25503e5eb5e53826d48a8.png +0 -0
  22. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/error.d1ea1496f9057eb392d5bbf3732a61b7.png +0 -0
  23. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/file.19eeb73b9593a38f8e9f418337fc7d10.png +0 -0
  24. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/forward.d8b800c443b8972542883e0b9de2bdc6.png +0 -0
  25. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/pkg.ab19f4cbc543357183a20571f68380a3.png +0 -0
  26. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/react-navigation/elements/back-icon-mask.0a328cd9c1afd0afe8e3b1ec5165b1b4.png +0 -0
  27. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/react-navigation/elements/back-icon.35ba0eaec5a4f5ed12ca16fabeae451d.png +0 -0
  28. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/react-navigation/elements/clear-icon.c94f6478e7ae0cdd9f15de1fcb9e5e55.png +0 -0
  29. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/react-navigation/elements/clear-icon.c94f6478e7ae0cdd9f15de1fcb9e5e55@2x.png +0 -0
  30. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/react-navigation/elements/clear-icon.c94f6478e7ae0cdd9f15de1fcb9e5e55@3x.png +0 -0
  31. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/react-navigation/elements/clear-icon.c94f6478e7ae0cdd9f15de1fcb9e5e55@4x.png +0 -0
  32. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/react-navigation/elements/close-icon.808e1b1b9b53114ec2838071a7e6daa7.png +0 -0
  33. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/react-navigation/elements/close-icon.808e1b1b9b53114ec2838071a7e6daa7@2x.png +0 -0
  34. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/react-navigation/elements/close-icon.808e1b1b9b53114ec2838071a7e6daa7@3x.png +0 -0
  35. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/react-navigation/elements/close-icon.808e1b1b9b53114ec2838071a7e6daa7@4x.png +0 -0
  36. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/react-navigation/elements/search-icon.286d67d3f74808a60a78d3ebf1a5fb57.png +0 -0
  37. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/sitemap.412dd9275b6b48ad28f5e3d81bb1f626.png +0 -0
  38. package/web/assets/__HomeCubbie/node_modules/expo-router/assets/unmatched.20e71bdf79e3a97bf55fd9e164041578.png +0 -0
  39. package/web/assets/__HomeCubbie/node_modules/expo-sqlite/web/wa-sqlite/wa-sqlite.7ca566fbbc2ec2a172c5aefa63a20f4b.wasm +0 -0
  40. package/web/assets/assets/fonts/BricolageGrotesque_600SemiBold.e5b5fc505484ff3ca24da73cba67c660.ttf +0 -0
  41. package/web/assets/assets/images/icon.51a3d296cef4a538e85170017fc2f169.png +0 -0
  42. package/web/costs/[year].html +474 -0
  43. package/web/costs/index.html +474 -0
  44. package/web/favicon.ico +0 -0
  45. package/web/first-run.html +474 -0
  46. package/web/form/account/bill/edit/[id].html +474 -0
  47. package/web/form/account/bill/new.html +474 -0
  48. package/web/form/account/edit/[id].html +474 -0
  49. package/web/form/account/new.html +474 -0
  50. package/web/form/activity/new.html +474 -0
  51. package/web/form/event/edit/[id].html +474 -0
  52. package/web/form/event/new.html +474 -0
  53. package/web/form/note/edit/[id].html +474 -0
  54. package/web/form/note/new.html +474 -0
  55. package/web/form/person/edit/[id].html +474 -0
  56. package/web/form/person/new.html +474 -0
  57. package/web/form/place/edit/[id].html +474 -0
  58. package/web/form/place/new.html +474 -0
  59. package/web/form/policy/correct/[id].html +474 -0
  60. package/web/form/policy/new.html +474 -0
  61. package/web/form/policy/renew/[id].html +474 -0
  62. package/web/form/policy/term/[id].html +474 -0
  63. package/web/form/schedule/edit/[id].html +474 -0
  64. package/web/form/schedule/new.html +474 -0
  65. package/web/form/task/edit/[id].html +474 -0
  66. package/web/form/task/new.html +474 -0
  67. package/web/form/thing/edit/[id].html +474 -0
  68. package/web/form/thing/new.html +474 -0
  69. package/web/home/about/[category].html +474 -0
  70. package/web/home/about/index.html +474 -0
  71. package/web/home/arrange.html +474 -0
  72. package/web/home/index.html +474 -0
  73. package/web/home/place/[id].html +474 -0
  74. package/web/home/thing/[id].html +474 -0
  75. package/web/homecubbie-web.json +5 -0
  76. package/web/index.html +474 -0
  77. package/web/month.html +474 -0
  78. package/web/records/account/[id].html +474 -0
  79. package/web/records/accounts.html +474 -0
  80. package/web/records/document/[id].html +474 -0
  81. package/web/records/documents.html +474 -0
  82. package/web/records/events.html +474 -0
  83. package/web/records/history.html +474 -0
  84. package/web/records/index.html +474 -0
  85. package/web/records/people.html +474 -0
  86. package/web/records/person/[id].html +474 -0
  87. package/web/records/policies.html +474 -0
  88. package/web/records/policy/[id].html +474 -0
  89. package/web/records/supplies.html +474 -0
  90. package/web/records/year/[year].html +474 -0
  91. package/web/scan/[id].html +474 -0
  92. package/web/scan/index.html +474 -0
  93. package/web/search.html +474 -0
  94. package/web/settings/app-lock.html +474 -0
  95. package/web/settings/backup.html +474 -0
  96. package/web/settings/categories.html +474 -0
  97. package/web/settings/index.html +474 -0
  98. package/web/settings/open-source.html +474 -0
  99. package/web/settings/privacy.html +474 -0
  100. package/web/settings/property.html +474 -0
  101. package/web/settings/recently-deleted.html +474 -0
  102. package/web/settings/reminders.html +474 -0
  103. package/web/settings/restore.html +474 -0
  104. package/web/settings/sharing-guide.html +474 -0
  105. package/web/settings/sharing.html +474 -0
  106. package/web/settings/start-over.html +474 -0
  107. package/web/settings/storage.html +474 -0
  108. package/web/settings/tags.html +474 -0
  109. package/web/settings/who-uses-this-phone.html +474 -0
  110. package/web/tasks/[id].html +474 -0
  111. package/web/tasks/index.html +474 -0
@@ -0,0 +1,92 @@
1
+ /**
2
+ * HTTPS for a browser on a computer that isn't the server's (board #376,
3
+ * #427, decision 363), the way Actual Budget's server does it: the household
4
+ * hands the server a certificate and key for a name it owns, and the server
5
+ * also listens on port 5008 over HTTPS. The phones keep port 5007.
6
+ *
7
+ * `node:https`'s `createServer`, and nothing else from it: the server still
8
+ * starts no connection of its own, and renewing a certificate is the
9
+ * household's job or its proxy's, never this server's
10
+ * (`__tests__/no-outgoing-calls.test.ts`).
11
+ *
12
+ * - **It reads the files again when they change,** so a renewed certificate
13
+ * is served from the next connection on, with no restart. Polled rather
14
+ * than watched: a renewal that swaps a symbolic link, as certbot's does,
15
+ * changes no file an event watcher is looking at.
16
+ * - **It says the certificate's last day,** at start and at each change, so a
17
+ * household notices before browsers start warning.
18
+ * - **No HSTS.** It would hold for the whole name on every port, and break
19
+ * other things on that computer.
20
+ */
21
+ import { X509Certificate } from "node:crypto";
22
+ import { readFileSync, unwatchFile, watchFile } from "node:fs";
23
+ import { createServer } from "node:https";
24
+ import { answerHomeOnly } from "./home-network.js";
25
+ import { householdRequestListener } from "./http-server.js";
26
+ /** The port a browser on another computer uses, beside the phones' 5007. */
27
+ export const HTTPS_PORT = 5008;
28
+ /** How often the certificate and key are looked at for a change. */
29
+ export const RELOAD_POLL_MS = 1000;
30
+ /**
31
+ * The certificate and key the household gave, from the environment: both, or
32
+ * neither and no HTTPS listener at all, or "half", which `start.ts` refuses to
33
+ * start with rather than guess.
34
+ */
35
+ export function certificateFilesFrom(env) {
36
+ const certFile = env.HOMECUBBIE_HTTPS_CERT ?? "";
37
+ const keyFile = env.HOMECUBBIE_HTTPS_KEY ?? "";
38
+ if (certFile === "" && keyFile === "")
39
+ return null;
40
+ if (certFile === "" || keyFile === "")
41
+ return "half";
42
+ return { certFile, keyFile };
43
+ }
44
+ /**
45
+ * Says when the certificate runs out, and whether it already has. Null when
46
+ * the file isn't a certificate, which is said instead.
47
+ */
48
+ export function certificateLastDay(cert) {
49
+ try {
50
+ return new Date(new X509Certificate(cert).validTo);
51
+ }
52
+ catch {
53
+ return null;
54
+ }
55
+ }
56
+ function describeLastDay(cert, now) {
57
+ const lastDay = certificateLastDay(cert);
58
+ if (lastDay === null)
59
+ return "the certificate file isn't a certificate this server can read";
60
+ const day = lastDay.toISOString().slice(0, 10);
61
+ return lastDay.getTime() < now ? `the certificate ran out on ${day}. Browsers will warn: renew it` : `the certificate runs out on ${day}`;
62
+ }
63
+ export function createHouseholdHttpsServer(household, files, options) {
64
+ const read = () => ({ cert: readFileSync(files.certFile), key: readFileSync(files.keyFile) });
65
+ const first = read();
66
+ options.log.info(describeLastDay(first.cert, household.clock.now()));
67
+ const server = createServer(first, householdRequestListener(household, options));
68
+ answerHomeOnly(server, options);
69
+ const reload = (current, previous) => {
70
+ if (current.mtimeMs === previous.mtimeMs)
71
+ return;
72
+ try {
73
+ const next = read();
74
+ server.setSecureContext(next);
75
+ options.log.info(`read the certificate again: ${describeLastDay(next.cert, household.clock.now())}`);
76
+ }
77
+ catch (error) {
78
+ // Half-written, or a key that doesn't match the certificate yet: keep
79
+ // serving the one in use, and take the pair at the next change.
80
+ options.log.info(`couldn't use the changed certificate yet, so the one before stays: ${String(error)}`);
81
+ }
82
+ };
83
+ watchFile(files.certFile, { interval: RELOAD_POLL_MS }, reload);
84
+ watchFile(files.keyFile, { interval: RELOAD_POLL_MS }, reload);
85
+ return {
86
+ server,
87
+ stopWatching: () => {
88
+ unwatchFile(files.certFile, reload);
89
+ unwatchFile(files.keyFile, reload);
90
+ },
91
+ };
92
+ }
package/dist/main.js ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * `homecubbie-server`: starts the household server (`start.ts`).
4
+ *
5
+ * Node's warning about `node:sqlite` is dropped first, and only then is the
6
+ * server loaded, because loading it loads SQLite (#560, `quiet-warnings.ts`).
7
+ * Every other warning still prints.
8
+ */
9
+ import { withoutSqliteWarning } from "./quiet-warnings.js";
10
+ process.emitWarning = withoutSqliteWarning(process.emitWarning);
11
+ await import("./start.js");
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Drops Node's one warning about `node:sqlite` and passes every other warning
3
+ * on (board #560).
4
+ *
5
+ * Node prints "ExperimentalWarning: SQLite is an experimental feature and
6
+ * might change at any time" when `node:sqlite` loads. It's the first thing a
7
+ * household would read from `npx homecubbie-server`, and it says nothing about
8
+ * the server. The Dockerfile and the scripts pass
9
+ * `--disable-warning=ExperimentalWarning`, but the npm package's `bin` starts
10
+ * from a `#!/usr/bin/env node` line, which can't carry a flag on every
11
+ * platform. So `main.ts` puts this in front of `process.emitWarning`, which
12
+ * Node's own experimental warnings go through, before it loads anything that
13
+ * loads SQLite.
14
+ */
15
+ /** True for the SQLite warning and nothing else. */
16
+ export function isSqliteExperimentalWarning(warning, typeOrOptions) {
17
+ const text = typeof warning === "string" ? warning : warning.message;
18
+ const type = typeof typeOrOptions === "string"
19
+ ? typeOrOptions
20
+ : typeof typeOrOptions === "object" && typeOrOptions !== null && "type" in typeOrOptions
21
+ ? String(typeOrOptions.type)
22
+ : warning instanceof Error
23
+ ? warning.name
24
+ : "Warning";
25
+ return type === "ExperimentalWarning" && /\bSQLite\b/.test(text);
26
+ }
27
+ /** `emit`, without the SQLite warning. */
28
+ export function withoutSqliteWarning(emit) {
29
+ return function emitWarning(warning, ...rest) {
30
+ if (isSqliteExperimentalWarning(warning, rest[0]))
31
+ return;
32
+ emit.call(process, warning, ...rest);
33
+ };
34
+ }
package/dist/start.js ADDED
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Starts the household server. `main.ts` loads this once Node's SQLite
3
+ * warning is dropped (#560), because importing it loads `node:sqlite`.
4
+ *
5
+ * HOMECUBBIE_DATA the folder that holds everything (default ./homecubbie-data)
6
+ * HOMECUBBIE_PORT the port (default 5007)
7
+ * HOMECUBBIE_HOSTS comma-separated names it may be reached by, besides IP addresses
8
+ * HOMECUBBIE_ALLOW_ANY_ADDRESS
9
+ * 1 to answer any address. Otherwise it answers only this computer
10
+ * and the home network's address ranges (`home-network.ts`).
11
+ * HOMECUBBIE_HTTPS_CERT, HOMECUBBIE_HTTPS_KEY
12
+ * a certificate and key for a name in HOMECUBBIE_HOSTS: given both,
13
+ * it also listens over HTTPS on port 5008, for a browser on another
14
+ * computer (#427). The phones keep the plain port.
15
+ *
16
+ * It listens on the home network and makes no outgoing connection of any kind:
17
+ * no update check, no telemetry, no time server. `__tests__/no-outgoing-calls.test.ts`
18
+ * keeps it that way.
19
+ *
20
+ * It serves the web app from the `web` folder beside `src` and `dist`, when the
21
+ * package was made with one (`npm run export:server-web` in the app, #424).
22
+ */
23
+ import { networkInterfaces } from "node:os";
24
+ import { join, resolve } from "node:path";
25
+ import { Household } from "./household.js";
26
+ import { createHouseholdServer } from "./http-server.js";
27
+ import { certificateFilesFrom, createHouseholdHttpsServer, HTTPS_PORT } from "./https-listener.js";
28
+ import { loadWebApp } from "./web-app.js";
29
+ const dataDir = resolve(process.env.HOMECUBBIE_DATA ?? "homecubbie-data");
30
+ const port = Number(process.env.HOMECUBBIE_PORT ?? "5007");
31
+ const hostNames = (process.env.HOMECUBBIE_HOSTS ?? "")
32
+ .split(",")
33
+ .map((name) => name.trim())
34
+ .filter((name) => name.length > 0);
35
+ const log = { info: (line) => console.log(`homecubbie-server: ${line}`) };
36
+ const certificate = certificateFilesFrom(process.env);
37
+ if (certificate === "half") {
38
+ console.error("homecubbie-server: HTTPS needs both HOMECUBBIE_HTTPS_CERT and HOMECUBBIE_HTTPS_KEY, or neither.");
39
+ process.exit(1);
40
+ }
41
+ const household = new Household(dataDir, { log });
42
+ const web = loadWebApp(join(import.meta.dirname, "..", "web"));
43
+ const options = {
44
+ hostNames,
45
+ webApp: "app" in web ? web.app : null,
46
+ anyAddress: process.env.HOMECUBBIE_ALLOW_ANY_ADDRESS === "1",
47
+ log,
48
+ };
49
+ const server = createHouseholdServer(household, options);
50
+ const secure = certificate === null ? null : createHouseholdHttpsServer(household, certificate, { ...options, log });
51
+ secure?.server.listen(HTTPS_PORT, () => {
52
+ for (const name of hostNames)
53
+ log.info(`listening over HTTPS at https://${name}:${HTTPS_PORT}`);
54
+ if (hostNames.length === 0)
55
+ log.info(`listening over HTTPS on port ${HTTPS_PORT}. Put the certificate's name in HOMECUBBIE_HOSTS.`);
56
+ });
57
+ server.listen(port, () => {
58
+ log.info(`keeping the household's data in ${dataDir}`);
59
+ if (options.anyAddress)
60
+ log.info("answering any address (HOMECUBBIE_ALLOW_ANY_ADDRESS=1).");
61
+ for (const address of homeAddresses())
62
+ log.info(`listening at http://${address}:${port}`);
63
+ if ("app" in web) {
64
+ log.info(`the web app, for database version ${web.app.schemaVersion}, is at http://localhost:${port} on this computer.`);
65
+ log.info("On another computer it needs HTTPS: see the server's README.");
66
+ }
67
+ else {
68
+ log.info(`${web.missing}, so this server answers phones only.`);
69
+ }
70
+ if (household.isSetUp()) {
71
+ log.info("set up. Phones join with an invite from a phone that has already joined.");
72
+ }
73
+ else {
74
+ log.info("not set up yet. In HomeCubbie, go to Settings › Sharing › Share this house, and type:");
75
+ household.printSetupCode();
76
+ log.info("Nobody from HomeCubbie will ever ask you for this code.");
77
+ }
78
+ });
79
+ function shutdown() {
80
+ secure?.stopWatching();
81
+ secure?.server.close();
82
+ server.close(() => {
83
+ household.close();
84
+ process.exit(0);
85
+ });
86
+ }
87
+ process.on("SIGINT", shutdown);
88
+ process.on("SIGTERM", shutdown);
89
+ /**
90
+ * The private IPv4 addresses this computer has. In a container on the host's
91
+ * network (#553) they're the computer's own; behind Docker's bridge they would
92
+ * be the container's, which is why #294's guide also names the computer's
93
+ * address itself.
94
+ */
95
+ function homeAddresses() {
96
+ const found = [];
97
+ for (const entries of Object.values(networkInterfaces())) {
98
+ for (const entry of entries ?? []) {
99
+ if (entry.family === "IPv4" && !entry.internal)
100
+ found.push(entry.address);
101
+ }
102
+ }
103
+ return found.length > 0 ? found : ["localhost"];
104
+ }
package/dist/store.js ADDED
@@ -0,0 +1,400 @@
1
+ /**
2
+ * Everything the server keeps, in one SQLite file beside a folder of file
3
+ * pieces and full copies.
4
+ *
5
+ * None of it can be read here. Changes, full copies, files and phone names
6
+ * arrive encrypted under the household's data key, which never reaches the
7
+ * server. What is stored in clear is only what the server needs to do its job:
8
+ * arrival order, which phone sent what, the house's schema version, the key
9
+ * epoch, and each phone's public key.
10
+ */
11
+ import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync, existsSync, readdirSync } from "node:fs";
12
+ import { join } from "node:path";
13
+ import { DatabaseSync } from "node:sqlite";
14
+ const SCHEMA = `
15
+ CREATE TABLE IF NOT EXISTS meta (
16
+ key TEXT PRIMARY KEY NOT NULL,
17
+ value TEXT NOT NULL
18
+ );
19
+
20
+ CREATE TABLE IF NOT EXISTS phone (
21
+ id TEXT PRIMARY KEY NOT NULL,
22
+ public_key BLOB NOT NULL UNIQUE,
23
+ name_blob BLOB NOT NULL,
24
+ kind TEXT NOT NULL CHECK (kind IN ('phone', 'paper')),
25
+ joined_at TEXT NOT NULL,
26
+ last_seen_at TEXT,
27
+ removed_at TEXT,
28
+ must_reload INTEGER NOT NULL DEFAULT 0 CHECK (must_reload IN (0, 1)),
29
+ -- Board #442: its signing key, and how it came in (JSON: the door, the
30
+ -- vouch for it, and the arrival's two signatures). What the phones
31
+ -- signed, kept as it came; this server checks none of it.
32
+ signing_key BLOB,
33
+ door TEXT
34
+ );
35
+
36
+ CREATE TABLE IF NOT EXISTS change (
37
+ seq INTEGER PRIMARY KEY AUTOINCREMENT,
38
+ phone_id TEXT NOT NULL REFERENCES phone(id),
39
+ batch INTEGER NOT NULL,
40
+ epoch INTEGER NOT NULL,
41
+ schema_version INTEGER NOT NULL,
42
+ blob BLOB NOT NULL,
43
+ blob_hash TEXT NOT NULL,
44
+ received_at TEXT NOT NULL,
45
+ UNIQUE (phone_id, batch)
46
+ );
47
+
48
+ CREATE TABLE IF NOT EXISTS copy (
49
+ id TEXT PRIMARY KEY NOT NULL,
50
+ epoch INTEGER NOT NULL,
51
+ schema_version INTEGER NOT NULL,
52
+ through_seq INTEGER NOT NULL,
53
+ byte_size INTEGER NOT NULL,
54
+ phone_id TEXT NOT NULL REFERENCES phone(id),
55
+ active INTEGER NOT NULL CHECK (active IN (0, 1)),
56
+ created_at TEXT NOT NULL,
57
+ -- Board #452 (decision 384): the oldest version whose changes the house
58
+ -- takes while this copy is in use. Null from a phone before it: then only
59
+ -- the copy's own version.
60
+ schema_floor INTEGER
61
+ );
62
+
63
+ CREATE TABLE IF NOT EXISTS invite (
64
+ id TEXT PRIMARY KEY NOT NULL,
65
+ verifier BLOB NOT NULL,
66
+ packet BLOB NOT NULL,
67
+ created_by TEXT NOT NULL REFERENCES phone(id),
68
+ expires_at INTEGER NOT NULL,
69
+ failures INTEGER NOT NULL DEFAULT 0,
70
+ door TEXT
71
+ );
72
+
73
+ -- Board #419 (decision 362): the household's recovery code, an invite with
74
+ -- no time limit. At most one. Never a phone, never wrapped for, and deleted
75
+ -- by any key change.
76
+ CREATE TABLE IF NOT EXISTS recovery (
77
+ id TEXT PRIMARY KEY NOT NULL,
78
+ verifier BLOB NOT NULL,
79
+ packet BLOB NOT NULL,
80
+ epoch INTEGER NOT NULL,
81
+ created_by TEXT NOT NULL REFERENCES phone(id),
82
+ created_at TEXT NOT NULL,
83
+ failures INTEGER NOT NULL DEFAULT 0,
84
+ shut_until INTEGER NOT NULL DEFAULT 0,
85
+ door TEXT
86
+ );
87
+
88
+ -- Board #442: each key change's statement as its signer wrote it, the
89
+ -- signature, which phone sent it, and whether it was a change, a reset or
90
+ -- a move, for /api/hello.
91
+ CREATE TABLE IF NOT EXISTS rekey (
92
+ epoch INTEGER PRIMARY KEY NOT NULL,
93
+ proof BLOB NOT NULL,
94
+ statement TEXT,
95
+ signature BLOB,
96
+ signed_by TEXT,
97
+ kind TEXT
98
+ );
99
+
100
+ CREATE TABLE IF NOT EXISTS wrap (
101
+ epoch INTEGER NOT NULL,
102
+ phone_id TEXT NOT NULL REFERENCES phone(id),
103
+ ephemeral_key BLOB NOT NULL,
104
+ blob BLOB NOT NULL,
105
+ PRIMARY KEY (epoch, phone_id)
106
+ );
107
+
108
+ CREATE TABLE IF NOT EXISTS file (
109
+ id TEXT PRIMARY KEY NOT NULL,
110
+ pieces INTEGER,
111
+ created_at TEXT NOT NULL
112
+ );
113
+
114
+ CREATE TABLE IF NOT EXISTS seen_nonce (
115
+ nonce TEXT PRIMARY KEY NOT NULL,
116
+ seen_at INTEGER NOT NULL
117
+ );
118
+ `;
119
+ export class Store {
120
+ db;
121
+ dataDir;
122
+ constructor(dataDir) {
123
+ this.dataDir = dataDir;
124
+ mkdirSync(join(dataDir, "files"), { recursive: true });
125
+ mkdirSync(join(dataDir, "copies"), { recursive: true });
126
+ this.db = new DatabaseSync(join(dataDir, "household.db"));
127
+ this.db.exec("PRAGMA journal_mode = WAL; PRAGMA foreign_keys = ON; PRAGMA secure_delete = ON;");
128
+ this.db.exec(SCHEMA);
129
+ // A folder made before a column existed gets it here. CREATE TABLE IF
130
+ // NOT EXISTS leaves an existing table as it was.
131
+ this.addColumn("phone", "must_reload", "INTEGER NOT NULL DEFAULT 0 CHECK (must_reload IN (0, 1))");
132
+ // Board #442. A folder from before it keeps its phones without a signing
133
+ // key, so no member can trace them: such a household shares again from a
134
+ // new folder (`docs/open/sharing-key-scheme.md`, "The protocol").
135
+ this.addColumn("phone", "signing_key", "BLOB");
136
+ this.addColumn("phone", "door", "TEXT");
137
+ this.addColumn("invite", "door", "TEXT");
138
+ this.addColumn("recovery", "door", "TEXT");
139
+ for (const column of ["statement TEXT", "signature BLOB", "signed_by TEXT", "kind TEXT"]) {
140
+ const [name, type] = column.split(" ");
141
+ this.addColumn("rekey", name, type);
142
+ }
143
+ // Board #452: a folder from before it keeps its copy, which takes only its own version.
144
+ this.addColumn("copy", "schema_floor", "INTEGER");
145
+ }
146
+ addColumn(table, column, definition) {
147
+ const columns = this.db.prepare(`PRAGMA table_info(${table})`).all();
148
+ if (!columns.some((existing) => existing.name === column))
149
+ this.db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${definition}`);
150
+ }
151
+ close() {
152
+ this.db.close();
153
+ }
154
+ /** Runs `work` in one transaction: all of it lands, or none of it does. */
155
+ transaction(work) {
156
+ this.db.exec("BEGIN IMMEDIATE");
157
+ try {
158
+ const result = work();
159
+ this.db.exec("COMMIT");
160
+ return result;
161
+ }
162
+ catch (error) {
163
+ this.db.exec("ROLLBACK");
164
+ throw error;
165
+ }
166
+ }
167
+ // ── meta ──────────────────────────────────────────────────────────────
168
+ getMeta(key) {
169
+ const row = this.db.prepare("SELECT value FROM meta WHERE key = ?").get(key);
170
+ return row?.value ?? null;
171
+ }
172
+ setMeta(key, value) {
173
+ this.db
174
+ .prepare("INSERT INTO meta (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value")
175
+ .run(key, value);
176
+ }
177
+ deleteMeta(key) {
178
+ this.db.prepare("DELETE FROM meta WHERE key = ?").run(key);
179
+ }
180
+ getNumber(key, fallback) {
181
+ const value = this.getMeta(key);
182
+ return value === null ? fallback : Number(value);
183
+ }
184
+ // ── phones ────────────────────────────────────────────────────────────
185
+ findPhone(id) {
186
+ return this.db.prepare("SELECT * FROM phone WHERE id = ?").get(id);
187
+ }
188
+ listPhones() {
189
+ return this.db.prepare("SELECT * FROM phone ORDER BY joined_at, id").all();
190
+ }
191
+ insertPhone(row) {
192
+ this.db
193
+ .prepare("INSERT INTO phone (id, public_key, name_blob, kind, joined_at, signing_key, door) VALUES (?, ?, ?, ?, ?, ?, ?)")
194
+ .run(row.id, row.publicKey, row.nameBlob, row.kind, row.now, row.signingKey, row.door);
195
+ }
196
+ renamePhone(id, nameBlob) {
197
+ this.db.prepare("UPDATE phone SET name_blob = ? WHERE id = ?").run(nameBlob, id);
198
+ }
199
+ markPhoneSeen(id, now) {
200
+ this.db.prepare("UPDATE phone SET last_seen_at = ? WHERE id = ?").run(now, id);
201
+ }
202
+ removePhone(id, now) {
203
+ this.db.prepare("UPDATE phone SET removed_at = ? WHERE id = ? AND removed_at IS NULL").run(now, id);
204
+ }
205
+ // ── changes ───────────────────────────────────────────────────────────
206
+ findChange(phoneId, batch) {
207
+ return this.db.prepare("SELECT * FROM change WHERE phone_id = ? AND batch = ?").get(phoneId, batch);
208
+ }
209
+ insertChange(row) {
210
+ const result = this.db
211
+ .prepare(`INSERT INTO change (phone_id, batch, epoch, schema_version, blob, blob_hash, received_at)
212
+ VALUES (?, ?, ?, ?, ?, ?, ?)`)
213
+ .run(row.phoneId, row.batch, row.epoch, row.schemaVersion, row.blob, row.blobHash, row.now);
214
+ return Number(result.lastInsertRowid);
215
+ }
216
+ changesAfter(seq, limit) {
217
+ return this.db
218
+ .prepare("SELECT * FROM change WHERE seq > ? ORDER BY seq LIMIT ?")
219
+ .all(seq, limit);
220
+ }
221
+ lastSeq() {
222
+ const row = this.db.prepare("SELECT seq FROM sqlite_sequence WHERE name = 'change'").get();
223
+ return row?.seq ?? 0;
224
+ }
225
+ /** The last change sealed under a key before `epoch`'s, or 0: a copy at `epoch` runs at least this far (#435). */
226
+ lastSeqSealedBefore(epoch) {
227
+ const row = this.db.prepare("SELECT MAX(seq) AS seq FROM change WHERE epoch < ?").get(epoch);
228
+ return row.seq ?? 0;
229
+ }
230
+ lastBatchOf(phoneId) {
231
+ const row = this.db.prepare("SELECT MAX(batch) AS batch FROM change WHERE phone_id = ?").get(phoneId);
232
+ return row.batch ?? 0;
233
+ }
234
+ dropChangesThrough(seq) {
235
+ this.db.prepare("DELETE FROM change WHERE seq <= ?").run(seq);
236
+ }
237
+ /** Every phone still in the household but `exceptId` starts again from the copy at its next sync. */
238
+ markMustReload(exceptId) {
239
+ this.db.prepare("UPDATE phone SET must_reload = 1 WHERE id != ? AND removed_at IS NULL").run(exceptId);
240
+ }
241
+ clearMustReload(id) {
242
+ this.db.prepare("UPDATE phone SET must_reload = 0 WHERE id = ?").run(id);
243
+ }
244
+ // ── full copies ───────────────────────────────────────────────────────
245
+ copyPath(id) {
246
+ return join(this.dataDir, "copies", id);
247
+ }
248
+ /** Written to a temporary name and renamed, so a crash never leaves half a copy under the real one. */
249
+ writeCopyBytes(id, bytes) {
250
+ const temporary = `${this.copyPath(id)}.partial`;
251
+ writeFileSync(temporary, bytes);
252
+ renameSync(temporary, this.copyPath(id));
253
+ }
254
+ readCopyBytes(id) {
255
+ return readFileSync(this.copyPath(id));
256
+ }
257
+ insertCopy(row) {
258
+ this.db
259
+ .prepare(`INSERT INTO copy (id, epoch, schema_version, through_seq, byte_size, phone_id, active, created_at, schema_floor)
260
+ VALUES (?, ?, ?, ?, ?, ?, 0, ?, ?)`)
261
+ .run(row.id, row.epoch, row.schema_version, row.through_seq, row.byte_size, row.phone_id, row.now, row.schema_floor);
262
+ }
263
+ findCopy(id) {
264
+ return this.db.prepare("SELECT * FROM copy WHERE id = ?").get(id);
265
+ }
266
+ activeCopy() {
267
+ return this.db.prepare("SELECT * FROM copy WHERE active = 1").get();
268
+ }
269
+ setCopyThrough(id, seq) {
270
+ this.db.prepare("UPDATE copy SET through_seq = ? WHERE id = ?").run(seq, id);
271
+ }
272
+ /** Makes `id` the one active copy and deletes every other copy, row and bytes. */
273
+ activateCopy(id) {
274
+ const others = this.db.prepare("SELECT id FROM copy WHERE id != ?").all(id);
275
+ this.db.prepare("DELETE FROM copy WHERE id != ?").run(id);
276
+ this.db.prepare("UPDATE copy SET active = 1 WHERE id = ?").run(id);
277
+ for (const other of others)
278
+ rmSync(this.copyPath(other.id), { force: true });
279
+ }
280
+ // ── invites ───────────────────────────────────────────────────────────
281
+ insertInvite(row) {
282
+ this.db
283
+ .prepare("INSERT INTO invite (id, verifier, packet, created_by, expires_at, door) VALUES (?, ?, ?, ?, ?, ?)")
284
+ .run(row.id, row.verifier, row.packet, row.createdBy, row.expiresAt, row.door);
285
+ }
286
+ findInvite(id) {
287
+ return this.db.prepare("SELECT * FROM invite WHERE id = ?").get(id);
288
+ }
289
+ countInviteFailure(id) {
290
+ this.db.prepare("UPDATE invite SET failures = failures + 1 WHERE id = ?").run(id);
291
+ return this.findInvite(id)?.failures ?? 0;
292
+ }
293
+ deleteInvite(id) {
294
+ this.db.prepare("DELETE FROM invite WHERE id = ?").run(id);
295
+ }
296
+ deleteExpiredInvites(nowMs) {
297
+ this.db.prepare("DELETE FROM invite WHERE expires_at <= ?").run(nowMs);
298
+ }
299
+ deleteInvitesBy(phoneId) {
300
+ this.db.prepare("DELETE FROM invite WHERE created_by = ?").run(phoneId);
301
+ }
302
+ // ── the recovery code ─────────────────────────────────────────────────
303
+ insertRecovery(row) {
304
+ this.db
305
+ .prepare("INSERT INTO recovery (id, verifier, packet, epoch, created_by, created_at, door) VALUES (?, ?, ?, ?, ?, ?, ?)")
306
+ .run(row.id, row.verifier, row.packet, row.epoch, row.createdBy, row.now, row.door);
307
+ }
308
+ /** The household's recovery code, if it has one. There is never more than one. */
309
+ findRecovery() {
310
+ return this.db.prepare("SELECT * FROM recovery LIMIT 1").get();
311
+ }
312
+ findRecoveryById(id) {
313
+ return this.db.prepare("SELECT * FROM recovery WHERE id = ?").get(id);
314
+ }
315
+ countRecoveryFailure(id) {
316
+ this.db.prepare("UPDATE recovery SET failures = failures + 1 WHERE id = ?").run(id);
317
+ return this.findRecoveryById(id)?.failures ?? 0;
318
+ }
319
+ shutRecovery(id, untilMs) {
320
+ this.db.prepare("UPDATE recovery SET failures = 0, shut_until = ? WHERE id = ?").run(untilMs, id);
321
+ }
322
+ /** Whether there was one to delete. */
323
+ deleteRecovery() {
324
+ return Number(this.db.prepare("DELETE FROM recovery").run().changes) > 0;
325
+ }
326
+ // ── wrapped keys ──────────────────────────────────────────────────────
327
+ insertWrap(row) {
328
+ this.db
329
+ .prepare(`INSERT INTO wrap (epoch, phone_id, ephemeral_key, blob) VALUES (?, ?, ?, ?)
330
+ ON CONFLICT(epoch, phone_id) DO UPDATE SET ephemeral_key = excluded.ephemeral_key, blob = excluded.blob`)
331
+ .run(row.epoch, row.phoneId, row.ephemeralKey, row.blob);
332
+ }
333
+ findWrap(epoch, phoneId) {
334
+ return this.db.prepare("SELECT * FROM wrap WHERE epoch = ? AND phone_id = ?").get(epoch, phoneId);
335
+ }
336
+ /**
337
+ * A key change's proof, made with the key before it, which this server never
338
+ * has (#420), and its statement as its signer signed it (#442).
339
+ */
340
+ insertRekey(row) {
341
+ this.db
342
+ .prepare("INSERT INTO rekey (epoch, proof, statement, signature, signed_by, kind) VALUES (?, ?, ?, ?, ?, ?)")
343
+ .run(row.epoch, row.proof, row.statement, row.signature, row.signedBy, row.kind);
344
+ }
345
+ findRekey(epoch) {
346
+ return this.db.prepare("SELECT * FROM rekey WHERE epoch = ?").get(epoch);
347
+ }
348
+ /** Every key change this server holds, oldest first. */
349
+ listRekeys() {
350
+ return this.db.prepare("SELECT * FROM rekey ORDER BY epoch").all();
351
+ }
352
+ /** The first key change this server holds: the move that brought the household here, if one did. */
353
+ firstRekey() {
354
+ return this.db.prepare("SELECT * FROM rekey ORDER BY epoch LIMIT 1").get();
355
+ }
356
+ // ── files ─────────────────────────────────────────────────────────────
357
+ fileDir(id) {
358
+ return join(this.dataDir, "files", id);
359
+ }
360
+ ensureFile(id, now) {
361
+ this.db.prepare("INSERT INTO file (id, created_at) VALUES (?, ?) ON CONFLICT(id) DO NOTHING").run(id, now);
362
+ mkdirSync(this.fileDir(id), { recursive: true });
363
+ }
364
+ findFile(id) {
365
+ return this.db.prepare("SELECT id, pieces FROM file WHERE id = ?").get(id);
366
+ }
367
+ writePiece(id, piece, bytes) {
368
+ const path = join(this.fileDir(id), String(piece));
369
+ writeFileSync(`${path}.partial`, bytes);
370
+ renameSync(`${path}.partial`, path);
371
+ }
372
+ readPiece(id, piece) {
373
+ const path = join(this.fileDir(id), String(piece));
374
+ return existsSync(path) ? readFileSync(path) : null;
375
+ }
376
+ storedPieces(id) {
377
+ if (!existsSync(this.fileDir(id)))
378
+ return [];
379
+ return readdirSync(this.fileDir(id))
380
+ .filter((name) => /^\d+$/.test(name))
381
+ .map(Number)
382
+ .sort((a, b) => a - b);
383
+ }
384
+ completeFile(id, pieces) {
385
+ this.db.prepare("UPDATE file SET pieces = ? WHERE id = ?").run(pieces, id);
386
+ }
387
+ deleteFile(id) {
388
+ this.db.prepare("DELETE FROM file WHERE id = ?").run(id);
389
+ rmSync(this.fileDir(id), { recursive: true, force: true });
390
+ }
391
+ // ── replay protection ─────────────────────────────────────────────────
392
+ /** False if the nonce was already used. Old nonces are pruned as it goes. */
393
+ rememberNonce(nonce, nowMs, keepMs) {
394
+ this.db.prepare("DELETE FROM seen_nonce WHERE seen_at < ?").run(nowMs - keepMs);
395
+ const result = this.db
396
+ .prepare("INSERT INTO seen_nonce (nonce, seen_at) VALUES (?, ?) ON CONFLICT(nonce) DO NOTHING")
397
+ .run(nonce, nowMs);
398
+ return result.changes === 1;
399
+ }
400
+ }