@nano133/nanoterra 0.10.1 → 0.12.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 (54) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/README.md +180 -10
  3. package/ai/SPEC.md +37 -2
  4. package/dist/admin/guard.d.ts +9 -1
  5. package/dist/admin/guard.js +12 -4
  6. package/dist/admin/panel/card.d.ts +5 -0
  7. package/dist/admin/panel/card.js +3 -0
  8. package/dist/admin/panel/icons.d.ts +9 -0
  9. package/dist/admin/panel/icons.js +1 -0
  10. package/dist/admin/panel/index.d.ts +1 -1
  11. package/dist/admin/panel/mount.d.ts +7 -0
  12. package/dist/admin/panel/mount.js +4 -1
  13. package/dist/admin-signin/adapters/d1.d.ts +12 -0
  14. package/dist/admin-signin/adapters/d1.js +161 -0
  15. package/dist/admin-signin/adapters/firestore.d.ts +52 -0
  16. package/dist/admin-signin/adapters/firestore.js +248 -0
  17. package/dist/admin-signin/browser.d.ts +4 -0
  18. package/dist/admin-signin/browser.js +36 -0
  19. package/dist/admin-signin/conformance.d.ts +18 -0
  20. package/dist/admin-signin/conformance.js +275 -0
  21. package/dist/admin-signin/handler.d.ts +69 -0
  22. package/dist/admin-signin/handler.js +488 -0
  23. package/dist/admin-signin/index.d.ts +8 -0
  24. package/dist/admin-signin/index.js +16 -0
  25. package/dist/admin-signin/memory.d.ts +17 -0
  26. package/dist/admin-signin/memory.js +147 -0
  27. package/dist/admin-signin/panel/index.d.ts +6 -0
  28. package/dist/admin-signin/panel/index.js +7 -0
  29. package/dist/admin-signin/panel/passkeys.d.ts +43 -0
  30. package/dist/admin-signin/panel/passkeys.js +202 -0
  31. package/dist/admin-signin/panel/view.d.ts +52 -0
  32. package/dist/admin-signin/panel/view.js +229 -0
  33. package/dist/admin-signin/types.d.ts +155 -0
  34. package/dist/admin-signin/types.js +59 -0
  35. package/package.json +9 -2
  36. package/src/admin/guard.ts +19 -5
  37. package/src/admin/panel/card.ts +5 -0
  38. package/src/admin/panel/icons.ts +1 -0
  39. package/src/admin/panel/index.ts +1 -1
  40. package/src/admin/panel/mount.ts +11 -1
  41. package/src/admin/panel/style.css +44 -0
  42. package/src/admin-signin/adapters/d1.sql +62 -0
  43. package/src/admin-signin/adapters/d1.ts +170 -0
  44. package/src/admin-signin/adapters/firestore.ts +274 -0
  45. package/src/admin-signin/browser.ts +38 -0
  46. package/src/admin-signin/conformance.ts +308 -0
  47. package/src/admin-signin/handler.ts +545 -0
  48. package/src/admin-signin/index.ts +34 -0
  49. package/src/admin-signin/memory.ts +141 -0
  50. package/src/admin-signin/panel/index.ts +11 -0
  51. package/src/admin-signin/panel/passkeys.ts +245 -0
  52. package/src/admin-signin/panel/view.ts +279 -0
  53. package/src/admin-signin/types.ts +146 -0
  54. package/src/player-signin/panel/style.css +8 -1
@@ -0,0 +1,275 @@
1
+ // The conformance suite of the admin sign-in's store: a game that writes its own store (another database) runs it, and so proves that its store keeps
2
+ // every rule the package's adapters keep. The package runs it on the memory store, D1 (on SQLite) and Firestore (a fake).
3
+ //
4
+ // import { adminSigninStoreConformance } from "@nano133/nanoterra/admin-signin/conformance";
5
+ // for (const c of adminSigninStoreConformance(async () => ({ store: myStore(), dump: () => allMyRowsAsText() }))) it(c.name, c.run);
6
+ //
7
+ // Each case gets a NEW, empty store from `make`. The rules: a request is finished once and delivered once, and a delivery writes its session in the same
8
+ // step; the two wallet apps stay apart; a session lasts until its time; "Sign out everywhere" ends every session and every request not delivered; the
9
+ // sign-in list keeps three things for 30 days; and (since 0.12.0) the owner's passkeys: a link is written once, at most 10 for an owner even when many come
10
+ // at once, a removal ends the passkey's sessions, and a used signature counts once. (The whole chain, with the player's sign-in beside it, is `adminSigninConformance` in
11
+ // "@nano133/nanoterra/admin/conformance".)
12
+ import { ADMIN_LOGIN_KEEP_MS, ADMIN_PASSKEY_LIMIT } from "./types.js";
13
+ function check(ok, message) {
14
+ if (!ok)
15
+ throw new Error(message);
16
+ }
17
+ /** An address the suite can find in any text (a store does not check addresses: the handler does). */
18
+ const who = (n) => `nano_3${n}${"x".repeat(59 - n.length)}`;
19
+ const OWNER = who("own");
20
+ const hex = (n, len = 64) => n.toString(16).padStart(len, "0");
21
+ let serial = 0;
22
+ /** A request as a wallet's start makes it: a fresh id each time. */
23
+ function request(at, extra = {}) {
24
+ serial += 1;
25
+ const id = hex(Date.now() * 1000 + serial, 32).slice(-32);
26
+ return { key: id, r: { id, starter: hex(serial + 7), status: "waiting", expiresAt: at + 2 * 60_000, nonce: hex(serial + 99), issuedAt: at, ...extra } };
27
+ }
28
+ const session = (at, n, via = "wallet") => ({ idHash: hex(1_000_000 + n + serial++), address: OWNER, createdAt: at, expiresAt: at + 86_400_000, via });
29
+ export function adminSigninStoreConformance(make, options = {}) {
30
+ const cases = [];
31
+ const add = (name, run) => cases.push({ name, run: async () => run(await make(), Date.now()) });
32
+ /** A passkey case: it runs only on a store with the passkey methods (required with `passkeys: true`). */
33
+ const addPasskey = (name, run) => {
34
+ if (options.passkeys === false)
35
+ return;
36
+ add(`passkeys: ${name}`, async (h, now) => {
37
+ const has = ["createSession", "linkPasskey", "unlinkPasskey", "passkeys", "linked", "useOnce"].every((m) => typeof h.store[m] === "function");
38
+ if (!has) {
39
+ check(options.passkeys !== true, "the store has no passkey methods (createSession, linkPasskey, unlinkPasskey, passkeys, linked, useOnce)");
40
+ return;
41
+ }
42
+ await run(h.store, now, h);
43
+ });
44
+ };
45
+ add("a request reads back as it was made, by its key and by its browser id, and only for its own wallet app", async ({ store }, now) => {
46
+ const { key, r } = request(now, { label: "ADMIN of game.test sign-in ABCD-EFGH from US" });
47
+ await store.requests("nautilus").create(key, r);
48
+ const back = await store.requests("nautilus").get(key);
49
+ check(back !== null && back.id === r.id && back.starter === r.starter && back.status === "waiting" && back.expiresAt === r.expiresAt && back.label === r.label && back.nonce === r.nonce && back.issuedAt === r.issuedAt && back.account === undefined, `read back: ${JSON.stringify(back)}`);
50
+ const found = await store.requests("nautilus").find(r.id);
51
+ check(found?.key === key && found.request.id === r.id, `find: ${JSON.stringify(found)}`);
52
+ check((await store.requests("berry").get(key)) === null, "Berry Wallet does not see a Nautilus request");
53
+ check((await store.requests("berry").find(r.id)) === null, "Berry Wallet does not find a Nautilus request");
54
+ check((await store.requests("nautilus").get(hex(12345, 32))) === null, "an unknown key is null");
55
+ });
56
+ add("NanChat's request (when the game turns NanChat on) is kept under the hash of its nonce and found by the browser's id, for NanChat only", async ({ store }, now) => {
57
+ const { r } = request(now);
58
+ const key = hex(424242);
59
+ await store.requests("nanchat").create(key, { ...r, id: "AbCdEfGhIjKlMnOpQrStUvWx" });
60
+ check((await store.requests("nanchat").find("AbCdEfGhIjKlMnOpQrStUvWx"))?.key === key, "found by the browser's id");
61
+ check((await store.requests("berry").find("AbCdEfGhIjKlMnOpQrStUvWx")) === null, "Berry Wallet does not find a NanChat request");
62
+ });
63
+ add("a second request with the same key is refused", async ({ store }, now) => {
64
+ const { key, r } = request(now);
65
+ await store.requests("berry").create(key, r);
66
+ let threw = false;
67
+ try {
68
+ await store.requests("berry").create(key, { ...r, starter: hex(1) });
69
+ }
70
+ catch {
71
+ threw = true;
72
+ }
73
+ check(threw, "the second create throws");
74
+ check((await store.requests("berry").get(key))?.starter === r.starter, "the first request is unchanged");
75
+ });
76
+ add("finish signs a waiting request in once, with its account; a second finish, one on an unknown key, or one by the other wallet app is false", async ({ store }, now) => {
77
+ const { key, r } = request(now);
78
+ await store.requests("berry").create(key, r);
79
+ check(await store.requests("berry").finish(key, OWNER), "the first finish");
80
+ check(!(await store.requests("berry").finish(key, who("bob"))), "a second finish is false");
81
+ const back = await store.requests("berry").get(key);
82
+ check(back?.status === "signedin" && back.account === OWNER, `the account stays the first: ${JSON.stringify(back)}`);
83
+ check(!(await store.requests("berry").finish(hex(777, 32), OWNER)), "an unknown key");
84
+ check(!(await store.requests("nautilus").finish(key, OWNER)), "the other wallet app cannot finish it");
85
+ });
86
+ add("two finishes at the same moment: exactly one is true", async ({ store }, now) => {
87
+ const { key, r } = request(now);
88
+ await store.requests("berry").create(key, r);
89
+ const both = await Promise.all([store.requests("berry").finish(key, OWNER), store.requests("berry").finish(key, who("bob"))]);
90
+ check(both.filter(Boolean).length === 1, `results: ${JSON.stringify(both)}`);
91
+ });
92
+ add("deliver: false while waiting; then once, with its session; a second delivery writes no session", async ({ store }, now) => {
93
+ const { key, r } = request(now);
94
+ await store.requests("berry").create(key, r);
95
+ const early = session(now, 1);
96
+ check(!(await store.deliver("berry", key, early)), "a waiting request is not delivered");
97
+ check((await store.session(early.idHash, now)) === null, "and no session was written");
98
+ await store.requests("berry").finish(key, OWNER);
99
+ const s1 = session(now, 2);
100
+ check(await store.deliver("berry", key, s1), "the first delivery");
101
+ const read = await store.session(s1.idHash, now);
102
+ check(read?.address === OWNER && read.via === "wallet" && read.expiresAt === s1.expiresAt && read.createdAt === s1.createdAt && read.idHash === s1.idHash, `the session: ${JSON.stringify(read)}`);
103
+ const s2 = session(now, 3);
104
+ check(!(await store.deliver("berry", key, s2)), "a second delivery is false");
105
+ check((await store.session(s2.idHash, now)) === null, "and wrote no session");
106
+ check(!(await store.deliver("nautilus", key, session(now, 4))), "the other wallet app cannot deliver it");
107
+ check(!(await store.deliver("berry", hex(888, 32), session(now, 5))), "an unknown key");
108
+ });
109
+ add("two deliveries at the same moment: one is true, and one session is written", async ({ store, dump }, now) => {
110
+ const { key, r } = request(now);
111
+ await store.requests("nautilus").create(key, r);
112
+ await store.requests("nautilus").finish(key, OWNER);
113
+ const a = session(now, 10);
114
+ const b = session(now, 11);
115
+ const both = await Promise.all([store.deliver("nautilus", key, a), store.deliver("nautilus", key, b)]);
116
+ check(both.filter(Boolean).length === 1, `results: ${JSON.stringify(both)}`);
117
+ const live = [await store.session(a.idHash, now), await store.session(b.idHash, now)].filter(Boolean);
118
+ check(live.length === 1, `sessions written: ${live.length} (${(await dump()).slice(0, 300)})`);
119
+ });
120
+ add("a session reads while it lasts and not from its end, with the word it was written with; ending it removes it", async ({ store }, now) => {
121
+ for (const [i, via] of ["wallet", "ticket"].entries()) {
122
+ const { key, r } = request(now);
123
+ await store.requests("berry").create(key, r);
124
+ await store.requests("berry").finish(key, OWNER);
125
+ const s = session(now, 20 + i, via);
126
+ await store.deliver("berry", key, s);
127
+ const read = await store.session(s.idHash, now);
128
+ check(read?.address === OWNER && read.via === via, `read: ${JSON.stringify(read)} (a store returns the word it holds; the sign-in reads anything but "wallet" and "nanoterra" as "unknown")`);
129
+ check((await store.session(s.idHash, s.expiresAt)) === null, "not at its end");
130
+ check((await store.session(s.idHash, s.expiresAt + 1)) === null, "not after it");
131
+ await store.endSession(s.idHash);
132
+ check((await store.session(s.idHash, now)) === null, "ended");
133
+ await store.endSession(s.idHash);
134
+ }
135
+ check((await store.session(hex(999), now)) === null, "an unknown hash");
136
+ });
137
+ add("Sign out everywhere ends every admin session and says how many; a signed request that was not delivered, and a waiting one, make no login after it", async ({ store }, now) => {
138
+ const made = [];
139
+ for (let i = 0; i < 3; i++) {
140
+ const { key, r } = request(now);
141
+ await store.requests("berry").create(key, r);
142
+ await store.requests("berry").finish(key, OWNER);
143
+ const s = session(now, 30 + i);
144
+ await store.deliver("berry", key, s);
145
+ made.push(s);
146
+ }
147
+ const signed = request(now);
148
+ await store.requests("nautilus").create(signed.key, signed.r);
149
+ await store.requests("nautilus").finish(signed.key, OWNER);
150
+ const waiting = request(now);
151
+ await store.requests("berry").create(waiting.key, waiting.r);
152
+ check((await store.signOutEverywhere()) === 3, "three sessions went");
153
+ for (const s of made)
154
+ check((await store.session(s.idHash, now)) === null, "a session is left");
155
+ check(!(await store.deliver("nautilus", signed.key, session(now, 40))), "a signed request that was not delivered makes a login after Sign out everywhere");
156
+ const w = await store.requests("berry").get(waiting.key);
157
+ check(w === null || w.expiresAt <= now, `a waiting request must have ended (its time over), so a wallet's answer is refused: ${JSON.stringify(w)}`);
158
+ // Even if a wallet's answer were taken, the request is not delivered again.
159
+ await store.requests("berry").finish(waiting.key, OWNER);
160
+ check(!(await store.deliver("berry", waiting.key, session(now, 41))), "a request that was waiting at Sign out everywhere makes a login afterwards");
161
+ check((await store.signOutEverywhere()) === 0, "a second call ends nothing and says 0");
162
+ });
163
+ add("the sign-in list: newest first, at most the limit, only the time, the country and the browser; a row older than 30 days goes when the next is written", async ({ store, dump }, now) => {
164
+ check((await store.recentLogins(10)).length === 0, "a new store lists no sign-ins");
165
+ await store.recordLogin({ at: now - ADMIN_LOGIN_KEEP_MS - 60_000, country: "FR", browser: "Old" });
166
+ await store.recordLogin({ at: now - ADMIN_LOGIN_KEEP_MS + 86_400_000, country: "FR", browser: "Kept" });
167
+ for (let i = 0; i < 12; i++)
168
+ await store.recordLogin({ at: now - 1000 * (12 - i), country: i % 3 === 0 ? null : "US", browser: `Chrome on macOS ${i}` });
169
+ const got = await store.recentLogins(10);
170
+ check(got.length === 10, `expected 10 sign-ins, got ${got.length}`);
171
+ check(got.every((l, i, all) => i === 0 || all[i - 1].at >= l.at), "the sign-ins are not newest first");
172
+ check(got[0].at === now - 1000 && got[0].browser === "Chrome on macOS 11" && got[0].country === "US", `the first must be the newest: ${JSON.stringify(got[0])}`);
173
+ check(got.some((l) => l.country === null), "a sign-in with no country keeps null");
174
+ check(got.every((l) => Object.keys(l).sort().join() === "at,browser,country"), `a sign-in must carry only at, browser and country: ${JSON.stringify(got[0])}`);
175
+ check((await store.recentLogins(3)).length === 3, "the limit must hold");
176
+ check((await store.recentLogins(0)).length === 0, "a limit of 0 lists none");
177
+ const all = await store.recentLogins(50);
178
+ check(all.length === 13 && all.some((l) => l.browser === "Kept") && !all.some((l) => l.browser === "Old"), `the row of 31 days went, the row of 29 days stayed: ${all.map((l) => l.browser).join(", ")}`);
179
+ check(!(await dump()).includes("Old"), "the old row is gone from the store");
180
+ });
181
+ add("clean deletes requests 2 hours after they ended, ended sessions and sign-in rows past 30 days, and keeps the rest", async ({ store }, now) => {
182
+ const sessions = [];
183
+ for (const [i, at] of [now - 2 * 86_400_000, now].entries()) {
184
+ const { key, r } = request(at);
185
+ await store.requests("berry").create(key, r);
186
+ await store.requests("berry").finish(key, OWNER);
187
+ const s = session(at, 50 + i);
188
+ await store.deliver("berry", key, s);
189
+ sessions.push(s);
190
+ }
191
+ const old = request(now - 3 * 3_600_000);
192
+ const fresh = request(now);
193
+ await store.requests("berry").create(old.key, old.r);
194
+ await store.requests("berry").create(fresh.key, fresh.r);
195
+ await store.recordLogin({ at: now - ADMIN_LOGIN_KEEP_MS + 60_000, country: null, browser: "Edge on Windows" });
196
+ await store.clean(now + 120_000);
197
+ check((await store.session(sessions[1].idHash, now)) !== null, "the live session stays");
198
+ check((await store.session(sessions[0].idHash, now - 2 * 86_400_000)) === null, "the ended session went");
199
+ check((await store.requests("berry").get(old.key)) === null, "the old request went");
200
+ check((await store.requests("berry").get(fresh.key)) !== null, "the fresh request stays");
201
+ check((await store.recentLogins(10)).length === 0, "the sign-in row past 30 days went");
202
+ });
203
+ addPasskey("a link is written once and read back exactly, oldest first, for its owner only", async (store, now) => {
204
+ const a = who("pka");
205
+ const b = who("pkb");
206
+ check((await store.passkeys(OWNER)).length === 0, "a new store lists no passkey");
207
+ check(!(await store.linked(OWNER, a)), "nothing is linked yet");
208
+ check((await store.linkPasskey(OWNER, a, now - 1000)) === "linked", "the first link");
209
+ check((await store.linkPasskey(OWNER, b, now)) === "linked", "a second passkey");
210
+ check((await store.linkPasskey(OWNER, a, now + 5)) === "exists", "the same passkey again is 'exists'");
211
+ const list = await store.passkeys(OWNER);
212
+ check(list.length === 2 && list[0].address === a && list[0].createdAt === now - 1000 && list[1].address === b, `the list: ${JSON.stringify(list)}`);
213
+ check(list.every((p) => Object.keys(p).sort().join() === "address,createdAt"), `a passkey carries only address and createdAt: ${JSON.stringify(list[0])}`);
214
+ check(await store.linked(OWNER, a), "linked");
215
+ check(!(await store.linked(who("oth"), a)), "another owner has no link to it");
216
+ check(!(await store.linked(OWNER.toUpperCase(), a)) && !(await store.linked(OWNER, a.toUpperCase())) && !(await store.linked(` ${OWNER}`, a)), "only the exact texts are linked");
217
+ check((await store.passkeys(who("oth"))).length === 0, "another owner lists none");
218
+ });
219
+ addPasskey(`at most ${ADMIN_PASSKEY_LIMIT} for an owner, even when many links come at once; another owner has its own ${ADMIN_PASSKEY_LIMIT}`, async (store, now) => {
220
+ const many = Array.from({ length: ADMIN_PASSKEY_LIMIT + 4 }, (_, i) => who(`m${i}`));
221
+ const got = await Promise.all(many.map((a, i) => store.linkPasskey(OWNER, a, now + i)));
222
+ check(got.filter((r) => r === "linked").length === ADMIN_PASSKEY_LIMIT && got.filter((r) => r === "full").length === 4, `results: ${JSON.stringify(got)}`);
223
+ check((await store.passkeys(OWNER)).length === ADMIN_PASSKEY_LIMIT, "the list holds the limit");
224
+ check((await store.linkPasskey(OWNER, who("late"), now)) === "full", "full");
225
+ const linked = (await store.passkeys(OWNER))[0].address;
226
+ check((await store.linkPasskey(OWNER, linked, now)) === "exists", "a passkey that is there is 'exists', not 'full'");
227
+ check((await store.linkPasskey(who("oth"), who("late"), now)) === "linked", "another owner is not full");
228
+ check(await store.unlinkPasskey(OWNER, linked), "a removal");
229
+ check((await store.linkPasskey(OWNER, who("late"), now)) === "linked", "a removal makes room for one");
230
+ });
231
+ addPasskey("a removal ends the link and every admin session of that passkey, and nothing else", async (store, now) => {
232
+ const a = who("rma");
233
+ const b = who("rmb");
234
+ await store.linkPasskey(OWNER, a, now);
235
+ await store.linkPasskey(OWNER, b, now);
236
+ const sa = { ...session(now, 60, "passkey"), address: a };
237
+ const sb = { ...session(now, 61, "passkey"), address: b };
238
+ const owner = session(now, 62, "wallet");
239
+ for (const s of [sa, sb, owner])
240
+ await store.createSession(s);
241
+ const read = await store.session(sa.idHash, now);
242
+ check(read?.address === a && read.via === "passkey" && read.createdAt === now && read.expiresAt === sa.expiresAt, `a passkey session reads back: ${JSON.stringify(read)}`);
243
+ check(await store.unlinkPasskey(OWNER, a), "the removal says it removed one");
244
+ check(!(await store.unlinkPasskey(OWNER, a)), "a second removal says false");
245
+ check(!(await store.linked(OWNER, a)) && (await store.linked(OWNER, b)), "only that link went");
246
+ check((await store.session(sa.idHash, now)) === null, "the passkey's session ended with its link");
247
+ check((await store.session(sb.idHash, now)) !== null && (await store.session(owner.idHash, now)) !== null, "the other passkey's session and the wallet's session stay");
248
+ let threw = false;
249
+ try {
250
+ await store.createSession(sb);
251
+ }
252
+ catch {
253
+ threw = true;
254
+ }
255
+ check(threw, "a second session with the same hash is refused");
256
+ check((await store.signOutEverywhere()) === 2, "Sign out everywhere ends the passkey sessions too");
257
+ check((await store.session(sb.idHash, now)) === null, "and the passkey's session is gone");
258
+ check(await store.linked(OWNER, b), "Sign out everywhere keeps the links (a removal ends one)");
259
+ });
260
+ addPasskey("a used signature counts once, even twice at the same moment; after its time, or after clean, it may be used again", async (store) => {
261
+ const k = hex(31337);
262
+ check(await store.useOnce(k, 60_000), "the first use");
263
+ check(!(await store.useOnce(k, 60_000)), "the second use is refused");
264
+ const k2 = hex(31338);
265
+ const both = await Promise.all([store.useOnce(k2, 60_000), store.useOnce(k2, 60_000)]);
266
+ check(both.filter(Boolean).length === 1, `two at once: ${JSON.stringify(both)}`);
267
+ const k3 = hex(31339);
268
+ check(await store.useOnce(k3, 1), "a short one");
269
+ await new Promise((r) => setTimeout(r, 15));
270
+ check(await store.useOnce(k3, 60_000), "past its time it may be used again");
271
+ await store.clean(Date.now() + 120_000);
272
+ check(await store.useOnce(k, 60_000), "clean dropped the used row past its time");
273
+ });
274
+ return cases;
275
+ }
@@ -0,0 +1,69 @@
1
+ import type { AdminLogin } from "../admin/store.js";
2
+ import { type AdminSigninSession, type AdminSigninStore } from "./types.js";
3
+ /** The address that belongs to nobody (its key is all zeros): Nautilus names it while the game has no address of its own. */
4
+ export declare const EXAMPLE_ADDRESS = "nano_1111111111111111111111111111111111111111111111111111hifc8npp";
5
+ export type AdminSigninOptions = {
6
+ store: AdminSigninStore;
7
+ /**
8
+ * The game's own address (the starter's RECEIVE_ADDRESS: "nano_...", lowercase, its checksum checked), or null when none is set. Only this address makes an
9
+ * admin login: a signature by any other wallet is answered "not_owner" and the request stays unused. Nautilus also NAMES it in its request and in the
10
+ * text it signs (no key, no payment); with none, the example address.
11
+ */
12
+ owner: string | null;
13
+ /** Where the routes are. Default "/api/admin-signin" (ADMIN_SIGNIN_BASE): keep it, so every page and helper finds the admin's sign-in at the same place. */
14
+ base?: string;
15
+ /** The admin page that Berry Wallet signs in to. Default "/admin". A path with no query: Berry refuses a query. */
16
+ page?: string;
17
+ /** The game's host exactly as people see it ("mygame.com"). Every signed text names it. Default: the host of each request. When it is set, a call to any other host is refused (403). */
18
+ domain?: string;
19
+ /**
20
+ * Offer NanChat for the admin. Default false: a NanChat start is refused ("NanChat cannot open the admin yet. Use Berry Wallet or Nautilus."). NanChat's
21
+ * text must start "Login to <host>" and cannot name the ADMIN, which is why it is off: the owner who is asked to sign cannot tell an admin sign-in from a
22
+ * game sign-in. Turn it on only when the owner accepts that; give the panel the same `nanchat: true`.
23
+ */
24
+ nanchat?: boolean;
25
+ /**
26
+ * The owner's linked passkeys (since 0.12.0). Default false: no passkey route, and no passkey session opens /admin. With true, a live WALLET admin
27
+ * session of the owner may link up to 10 passkeys (each proves its key by signing), and a linked passkey's signature makes an admin login while its link to
28
+ * the current owner exists. The store needs the passkey methods (the package's stores have them; D1 needs the two tables at the end of d1.sql). Give the
29
+ * panel the same `passkeys: true` and the game's frozen passkey `scheme`.
30
+ */
31
+ passkeys?: boolean;
32
+ /** The cookies' names. Default { session: "admin", starter: "starter-admin" } (the starter's). On https they get the __Host- prefix. */
33
+ cookies?: {
34
+ session?: string;
35
+ starter?: string;
36
+ };
37
+ /** The shared limiter of the starts: false means "too many". Default: a fixed window in this process's memory. */
38
+ limit?: (key: string, limit: number, windowMs: number, now: number) => boolean | Promise<boolean>;
39
+ /** The visitor's key for the limit of the starts: a keyed hash of the IP address, never the address. Default: the CF-Connecting-IP header as it is. */
40
+ visitor?: (request: Request) => Promise<string>;
41
+ /** The person check (Turnstile) of a start: true when `token` passes. No hook: no check. */
42
+ personCheck?: (token: unknown, request: Request) => Promise<boolean>;
43
+ /** The time now in ms. Default Date.now. */
44
+ now?: () => number;
45
+ /** Told about a fault (a store). The visitor gets a plain 500 and never sees it. */
46
+ onError?: (e: unknown) => void;
47
+ };
48
+ export type AdminSignin = {
49
+ base: string;
50
+ /** Answers a route of the admin's sign-in, or null when the request is not one. */
51
+ handle(request: Request): Promise<Response | null>;
52
+ /** The ADMIN login of this request (the admin cookie only), or null: give it to `createAdminHandler` as its `session`. Never a player's login. */
53
+ session(request: Request, now?: number): Promise<AdminSigninSession | null>;
54
+ /** Ends this request's admin login only and returns the Set-Cookie value that clears the cookie (the game's own login, another person's perhaps, stays). */
55
+ signOut(request: Request): Promise<string>;
56
+ /** "Sign out everywhere": every admin login and every admin request not yet delivered. Give it to the admin store (`signOutEverywhere`) when the store has none. */
57
+ signOutEverywhere(): Promise<number>;
58
+ /** The last admin sign-ins, newest first. Give it to the admin store (`recentLogins`) when the store has none. */
59
+ recentLogins(limit: number): Promise<AdminLogin[]>;
60
+ /** The clean-up (a cron): old admin requests, ended admin sessions, sign-in rows past 30 days. */
61
+ clean(now?: number): Promise<void>;
62
+ };
63
+ /**
64
+ * What the database keeps of an admin session or of an admin request's starter: the hash of the value with a prefix. So the SERVER (not only the cookie's
65
+ * name) keeps the two scopes apart: a token made for the game (the player sign-in keeps its plain hash) does not open /admin even if it is put in the admin
66
+ * cookie, and a starter value copied from the game's cookie does not finish an admin request.
67
+ */
68
+ export declare const adminKey: (value: string) => Promise<string>;
69
+ export declare function createAdminSignin(options: AdminSigninOptions): AdminSignin;