@qvac/core 0.1.4 → 0.2.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.
- package/.claude/skills/asana/SKILL.md +86 -0
- package/.claude/skills/compaction/SKILL.md +127 -0
- package/.claude/skills/device-mesh-debug/SKILL.md +328 -0
- package/.claude/skills/gh-stack/SKILL.md +181 -0
- package/.claude/skills/gh-stack/references/commands.md +177 -0
- package/.claude/skills/gh-stack/references/stack-design.md +97 -0
- package/.claude/skills/gh-stack/references/troubleshooting.md +159 -0
- package/.claude/skills/js-house-style/SKILL.md +670 -0
- package/.claude/skills/pr-create/SKILL.md +180 -0
- package/.claude/skills/pr-review/SKILL.md +216 -0
- package/CLAUDE.md +45 -0
- package/README.md +10 -5
- package/dist-lib/lib/api/agents.js +3 -3
- package/dist-lib/lib/api/chat-groups.d.ts +1 -1
- package/dist-lib/lib/api/chat-groups.js +5 -3
- package/dist-lib/lib/api/chat.js +2 -2
- package/dist-lib/lib/api/current-mesh.d.ts +3 -2
- package/dist-lib/lib/api/current-mesh.js +4 -3
- package/dist-lib/lib/api/devices.js +1 -0
- package/dist-lib/lib/api/index.js +2 -0
- package/dist-lib/lib/api/members.d.ts +3 -0
- package/dist-lib/lib/api/members.js +23 -0
- package/dist-lib/lib/api/mesh.d.ts +14 -3
- package/dist-lib/lib/api/mesh.js +20 -3
- package/dist-lib/lib/api/watchable.d.ts +1 -1
- package/dist-lib/lib/api/watchable.js +1 -1
- package/dist-lib/lib/assistant.js +2 -1
- package/dist-lib/lib/core/agent.d.ts +1 -0
- package/dist-lib/lib/core/agent.js +29 -1
- package/dist-lib/lib/core/chat-group.d.ts +4 -0
- package/dist-lib/lib/core/chat-group.js +43 -1
- package/dist-lib/lib/core/chat.d.ts +10 -0
- package/dist-lib/lib/core/chat.js +75 -9
- package/dist-lib/lib/core/contract/index.js +2 -0
- package/dist-lib/lib/core/contract/joined-meshes.d.ts +2 -0
- package/dist-lib/lib/core/contract/joined-meshes.js +41 -0
- package/dist-lib/lib/core/contract/members.js +68 -2
- package/dist-lib/lib/core/contract/writers.js +19 -3
- package/dist-lib/lib/core/departures.d.ts +3 -1
- package/dist-lib/lib/core/departures.js +6 -4
- package/dist-lib/lib/core/device-attestation.d.ts +8 -0
- package/dist-lib/lib/core/device-attestation.js +25 -0
- package/dist-lib/lib/core/device-invites.d.ts +9 -0
- package/dist-lib/lib/core/device-invites.js +40 -0
- package/dist-lib/lib/core/files/index.d.ts +2 -1
- package/dist-lib/lib/core/files/index.js +1 -1
- package/dist-lib/lib/core/files/store/index.d.ts +1 -0
- package/dist-lib/lib/core/files/store/server.d.ts +5 -4
- package/dist-lib/lib/core/files/store/server.js +10 -6
- package/dist-lib/lib/core/helpers/index.d.ts +1 -1
- package/dist-lib/lib/core/hosted-agents.d.ts +3 -1
- package/dist-lib/lib/core/hosted-agents.js +9 -3
- package/dist-lib/lib/core/identity.d.ts +10 -0
- package/dist-lib/lib/core/identity.js +45 -0
- package/dist-lib/lib/core/invite.d.ts +3 -0
- package/dist-lib/lib/core/invite.js +8 -2
- package/dist-lib/lib/core/local/index.d.ts +22 -7
- package/dist-lib/lib/core/local/index.js +202 -66
- package/dist-lib/lib/core/membership.d.ts +8 -2
- package/dist-lib/lib/core/membership.js +33 -9
- package/dist-lib/lib/core/mesh-seed-handoff.d.ts +17 -0
- package/dist-lib/lib/core/mesh-seed-handoff.js +34 -0
- package/dist-lib/lib/core/mesh.d.ts +13 -1
- package/dist-lib/lib/core/mesh.js +61 -8
- package/dist-lib/lib/core/migrations/index.d.ts +3 -1
- package/dist-lib/lib/core/migrations/index.js +2 -2
- package/dist-lib/lib/core/migrations/membership.js +27 -15
- package/dist-lib/lib/core/rank.d.ts +14 -0
- package/dist-lib/lib/core/rank.js +23 -0
- package/dist-lib/lib/core/sync/index.js +6 -0
- package/dist-lib/lib/core.d.ts +91 -13
- package/dist-lib/lib/core.js +1051 -207
- package/dist-lib/schema/build.js +3 -2
- package/dist-lib/schema/engine.js +153 -9
- package/dist-lib/schema/field-order.js +4 -1
- package/dist-lib/schema/local.js +32 -0
- package/dist-lib/schema/qvac.js +45 -6
- package/dist-lib/spec/bind.js +35 -0
- package/dist-lib/spec/calls.js +37 -27
- package/dist-lib/spec/capabilities.d.ts +30 -10
- package/dist-lib/spec/hrpc/hrpc.json +135 -5
- package/dist-lib/spec/hrpc/index.js +130 -9
- package/dist-lib/spec/hrpc/messages.js +408 -14
- package/dist-lib/spec/hyperschema/index.js +408 -14
- package/dist-lib/spec/hyperschema/schema.json +309 -1
- package/dist-lib/spec/hyperschema/types.d.ts +90 -1
- package/dist-lib/spec/local/hyperdb/db.json +46 -1
- package/dist-lib/spec/local/hyperdb/index.js +209 -2
- package/dist-lib/spec/local/hyperdb/messages.js +557 -90
- package/dist-lib/spec/local/hyperschema/index.js +240 -2
- package/dist-lib/spec/local/hyperschema/schema.json +120 -1
- package/dist-lib/spec/local/hyperschema/types.d.ts +33 -1
- package/dist-lib/spec/mesh/hyperdb/db.json +16 -1
- package/dist-lib/spec/mesh/hyperdb/index.js +71 -2
- package/dist-lib/spec/mesh/hyperdb/messages.js +571 -192
- package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +13 -1
- package/dist-lib/spec/mesh/hyperdispatch/index.js +35 -1
- package/dist-lib/spec/mesh/hyperdispatch/messages.js +283 -20
- package/dist-lib/spec/mesh/hyperschema/index.js +283 -20
- package/dist-lib/spec/mesh/hyperschema/schema.json +179 -1
- package/dist-lib/spec/mesh/hyperschema/types.d.ts +45 -1
- package/package.json +6 -6
- package/tui/app.mjs +416 -50
- package/tui/boot.mjs +1 -1
- package/tui/lib/sidebar.mjs +3 -2
|
@@ -5,15 +5,18 @@ import ScopeLock from 'scope-lock';
|
|
|
5
5
|
import { generateMnemonic, mintIdentity } from "../identity.js";
|
|
6
6
|
import LocalDatabaseSpec from '../../../spec/local/hyperdb/index.js';
|
|
7
7
|
const CONFIG = '@local/config';
|
|
8
|
-
const
|
|
8
|
+
const ACTIVE_MESH = '@local/active-mesh';
|
|
9
|
+
const MESH_MEMBERSHIP = '@local/mesh-membership';
|
|
9
10
|
const MESH_HISTORY = '@local/mesh-history';
|
|
10
11
|
const MESH_DEPARTURE = '@local/mesh-departure';
|
|
12
|
+
const MESH_SESSION = '@local/mesh-session';
|
|
11
13
|
const DEVICE = '@local/device';
|
|
12
14
|
const APP_CONFIG = '@local/app-config';
|
|
13
15
|
const MODEL_DOWNLOAD = '@local/model-download';
|
|
14
16
|
const CUSTOM_MODEL = '@local/custom-model';
|
|
15
17
|
const IDENTITY = '@local/identity';
|
|
16
18
|
const IDENTITY_ID = 'identity';
|
|
19
|
+
const MEMBER_PROOF = '@local/member-proof';
|
|
17
20
|
const MEMBERSHIP = '@local/membership';
|
|
18
21
|
const MEMBERSHIP_ID = 'membership';
|
|
19
22
|
const CHAT_NOTIFY = '@local/chat-notify';
|
|
@@ -22,6 +25,13 @@ const APP_CONFIG_ID = 'default';
|
|
|
22
25
|
function freshWriterSeed() {
|
|
23
26
|
return `local-${crypto.randomBytes(8).toString('hex')}`;
|
|
24
27
|
}
|
|
28
|
+
function sessionOf(row) {
|
|
29
|
+
return {
|
|
30
|
+
seed: row.seed,
|
|
31
|
+
baseKey: row.key ?? undefined,
|
|
32
|
+
writerSeed: row.writerSeed ?? undefined
|
|
33
|
+
};
|
|
34
|
+
}
|
|
25
35
|
export function openLocalStore(storage) {
|
|
26
36
|
const db = HyperDB.rocks(storage.rocks.columnFamily('assistant/local'), LocalDatabaseSpec);
|
|
27
37
|
return new LocalStore(db);
|
|
@@ -60,6 +70,18 @@ class LocalStore {
|
|
|
60
70
|
await tx.flush();
|
|
61
71
|
});
|
|
62
72
|
}
|
|
73
|
+
rekey(fromKey, toKey) {
|
|
74
|
+
return this._locked(async () => {
|
|
75
|
+
const row = await this._db.get(CONFIG, { key: fromKey });
|
|
76
|
+
if (!row)
|
|
77
|
+
return null;
|
|
78
|
+
const tx = this._db.transaction();
|
|
79
|
+
await tx.insert(CONFIG, { key: toKey, value: row.value });
|
|
80
|
+
await tx.delete(CONFIG, { key: fromKey });
|
|
81
|
+
await tx.flush();
|
|
82
|
+
return row.value;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
63
85
|
deleteAll(keys) {
|
|
64
86
|
return this._locked(async () => {
|
|
65
87
|
const tx = this._db.transaction();
|
|
@@ -248,16 +270,16 @@ class LocalStore {
|
|
|
248
270
|
readIdentity() {
|
|
249
271
|
return this._db.get(IDENTITY, { id: IDENTITY_ID });
|
|
250
272
|
}
|
|
251
|
-
|
|
252
|
-
return this._db.get(
|
|
273
|
+
readMemberProof(meshKey) {
|
|
274
|
+
return this._db.get(MEMBER_PROOF, { meshKey });
|
|
253
275
|
}
|
|
254
|
-
|
|
276
|
+
recordMemberProof(link) {
|
|
255
277
|
return this._locked(async () => {
|
|
256
|
-
const existing = await this._db.get(
|
|
278
|
+
const existing = await this._db.get(MEMBER_PROOF, { meshKey: link.meshKey });
|
|
257
279
|
if (existing && existing.attestedAt >= link.attestedAt)
|
|
258
280
|
return;
|
|
259
281
|
const tx = this._db.transaction();
|
|
260
|
-
await tx.insert(
|
|
282
|
+
await tx.insert(MEMBER_PROOF, link);
|
|
261
283
|
await tx.flush();
|
|
262
284
|
});
|
|
263
285
|
}
|
|
@@ -280,88 +302,129 @@ class LocalStore {
|
|
|
280
302
|
return { identity, mnemonic };
|
|
281
303
|
});
|
|
282
304
|
}
|
|
283
|
-
resolveSession(deviceId, opts = {}) {
|
|
305
|
+
async resolveSession(deviceId, opts = {}) {
|
|
306
|
+
const current = await this._activeMembership(deviceId);
|
|
307
|
+
const sameSeed = Boolean(opts.seed && current?.seed.equals(opts.seed));
|
|
308
|
+
if (opts.seed && !sameSeed) {
|
|
309
|
+
return {
|
|
310
|
+
seed: opts.seed,
|
|
311
|
+
baseKey: opts.baseKey,
|
|
312
|
+
writerSeed: opts.writerSeed ?? freshWriterSeed()
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
if (current && !opts.new)
|
|
316
|
+
return sessionOf(current);
|
|
317
|
+
return {
|
|
318
|
+
seed: crypto.randomBytes(32),
|
|
319
|
+
baseKey: undefined,
|
|
320
|
+
writerSeed: undefined
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
createCandidateSession(seed, baseKey) {
|
|
324
|
+
return {
|
|
325
|
+
seed,
|
|
326
|
+
baseKey,
|
|
327
|
+
writerSeed: freshWriterSeed()
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
async _activeMembership(deviceId) {
|
|
331
|
+
const pointer = await this._db.get(ACTIVE_MESH, { deviceId });
|
|
332
|
+
if (!pointer)
|
|
333
|
+
return this._migrateLegacySession(deviceId);
|
|
334
|
+
return ((await this._membership(pointer.activeMeshKey)) ??
|
|
335
|
+
(await this._membership(pointer.homeMeshKey)));
|
|
336
|
+
}
|
|
337
|
+
async _membership(key) {
|
|
338
|
+
if (!key)
|
|
339
|
+
return null;
|
|
340
|
+
const row = await this._db.get(MESH_MEMBERSHIP, { key });
|
|
341
|
+
return row && !row.leftAt ? row : null;
|
|
342
|
+
}
|
|
343
|
+
_migrateLegacySession(deviceId) {
|
|
284
344
|
return this._locked(async () => {
|
|
345
|
+
const legacy = await this._legacyMembership(deviceId);
|
|
346
|
+
if (!legacy)
|
|
347
|
+
return null;
|
|
348
|
+
const key = legacy.key;
|
|
349
|
+
if (!key)
|
|
350
|
+
return legacy;
|
|
285
351
|
const tx = this._db.transaction();
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
if (opts.seed && !sameSeed) {
|
|
289
|
-
await tx.insert(MESH_SESSION, {
|
|
290
|
-
deviceId,
|
|
291
|
-
meshSeed: opts.seed,
|
|
292
|
-
meshKey: opts.baseKey ?? null,
|
|
293
|
-
meshCreator: opts.creator ?? false
|
|
294
|
-
});
|
|
295
|
-
await tx.flush();
|
|
296
|
-
return {
|
|
297
|
-
seed: opts.seed,
|
|
298
|
-
baseKey: opts.baseKey,
|
|
299
|
-
writerSeed: opts.writerSeed ?? freshWriterSeed(),
|
|
300
|
-
creator: opts.creator ?? false
|
|
301
|
-
};
|
|
302
|
-
}
|
|
303
|
-
if (session?.meshSeed && !opts.new) {
|
|
304
|
-
const baseKey = !session.meshCreator && session.meshKey ? session.meshKey : undefined;
|
|
305
|
-
const history = await tx.findOne(MESH_HISTORY);
|
|
306
|
-
await tx.close();
|
|
307
|
-
return {
|
|
308
|
-
seed: session.meshSeed,
|
|
309
|
-
baseKey,
|
|
310
|
-
writerSeed: history?.writerSeed ?? undefined,
|
|
311
|
-
creator: session.meshCreator ?? false
|
|
312
|
-
};
|
|
313
|
-
}
|
|
314
|
-
const seed = crypto.randomBytes(32);
|
|
315
|
-
await tx.insert(MESH_SESSION, {
|
|
352
|
+
await tx.insert(MESH_MEMBERSHIP, { ...legacy, key });
|
|
353
|
+
await tx.insert(ACTIVE_MESH, {
|
|
316
354
|
deviceId,
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
meshCreator: true
|
|
355
|
+
activeMeshKey: key,
|
|
356
|
+
homeMeshKey: key
|
|
320
357
|
});
|
|
358
|
+
const shipped = await tx.get(MEMBERSHIP, { id: MEMBERSHIP_ID });
|
|
359
|
+
if (shipped) {
|
|
360
|
+
const { memberId, proof, attestedAt } = shipped;
|
|
361
|
+
await tx.insert(MEMBER_PROOF, {
|
|
362
|
+
meshKey: key,
|
|
363
|
+
memberId,
|
|
364
|
+
proof,
|
|
365
|
+
attestedAt
|
|
366
|
+
});
|
|
367
|
+
}
|
|
321
368
|
await tx.flush();
|
|
322
|
-
return
|
|
369
|
+
return legacy;
|
|
323
370
|
});
|
|
324
371
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
372
|
+
async _legacyMembership(deviceId) {
|
|
373
|
+
const history = await this._db.findOne(MESH_HISTORY);
|
|
374
|
+
if (history) {
|
|
375
|
+
return { key: history.key, seed: history.seed, writerSeed: history.writerSeed };
|
|
376
|
+
}
|
|
377
|
+
const session = await this._db.get(MESH_SESSION, { deviceId });
|
|
378
|
+
if (!session?.meshSeed)
|
|
379
|
+
return null;
|
|
380
|
+
return { key: session.meshKey ?? null, seed: session.meshSeed, writerSeed: null };
|
|
332
381
|
}
|
|
333
|
-
recordMesh(deviceId, key,
|
|
382
|
+
recordMesh(deviceId, key, session, { account = false } = {}) {
|
|
334
383
|
return this._locked(async () => {
|
|
384
|
+
const existing = await this._db.get(MESH_MEMBERSHIP, { key });
|
|
385
|
+
const minted = account && !existing && session.baseKey === undefined;
|
|
386
|
+
const pointer = minted ? await this._db.get(ACTIVE_MESH, { deviceId }) : null;
|
|
335
387
|
const tx = this._db.transaction();
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
388
|
+
await tx.insert(MESH_MEMBERSHIP, {
|
|
389
|
+
key,
|
|
390
|
+
seed: session.seed,
|
|
391
|
+
writerSeed: session.writerSeed
|
|
392
|
+
});
|
|
393
|
+
if (minted) {
|
|
394
|
+
await tx.insert(ACTIVE_MESH, {
|
|
395
|
+
deviceId,
|
|
396
|
+
activeMeshKey: pointer?.activeMeshKey,
|
|
397
|
+
homeMeshKey: key
|
|
398
|
+
});
|
|
339
399
|
}
|
|
340
|
-
await tx.
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
400
|
+
await tx.flush();
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
setActiveMesh(deviceId, key) {
|
|
404
|
+
return this._locked(async () => {
|
|
405
|
+
const pointer = await this._db.get(ACTIVE_MESH, { deviceId });
|
|
406
|
+
const tx = this._db.transaction();
|
|
407
|
+
await tx.insert(ACTIVE_MESH, {
|
|
344
408
|
deviceId,
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
meshCreator: creator
|
|
409
|
+
activeMeshKey: key,
|
|
410
|
+
homeMeshKey: pointer?.homeMeshKey
|
|
348
411
|
});
|
|
349
412
|
await tx.flush();
|
|
350
413
|
});
|
|
351
414
|
}
|
|
352
|
-
recordDeparture(key) {
|
|
415
|
+
recordDeparture(key, opts) {
|
|
353
416
|
return this._locked(async () => {
|
|
354
417
|
const tx = this._db.transaction();
|
|
355
|
-
const
|
|
356
|
-
if (!
|
|
418
|
+
const row = await tx.get(MESH_MEMBERSHIP, { key });
|
|
419
|
+
if (!row) {
|
|
357
420
|
await tx.close();
|
|
358
421
|
return;
|
|
359
422
|
}
|
|
360
423
|
await tx.insert(MESH_DEPARTURE, {
|
|
361
|
-
key:
|
|
362
|
-
seed:
|
|
363
|
-
creator:
|
|
364
|
-
writerSeed:
|
|
424
|
+
key: row.key,
|
|
425
|
+
seed: row.seed,
|
|
426
|
+
creator: opts.creator,
|
|
427
|
+
writerSeed: row.writerSeed
|
|
365
428
|
});
|
|
366
429
|
await tx.flush();
|
|
367
430
|
});
|
|
@@ -379,6 +442,79 @@ class LocalStore {
|
|
|
379
442
|
await tx.flush();
|
|
380
443
|
});
|
|
381
444
|
}
|
|
445
|
+
archiveMesh(key) {
|
|
446
|
+
return this._locked(async () => {
|
|
447
|
+
const existing = await this._db.get(MESH_MEMBERSHIP, { key });
|
|
448
|
+
const aimed = await this._pointersAt(key);
|
|
449
|
+
const tx = this._db.transaction();
|
|
450
|
+
if (existing) {
|
|
451
|
+
await tx.insert(MESH_MEMBERSHIP, { ...existing, leftAt: Date.now() });
|
|
452
|
+
}
|
|
453
|
+
for (const pointer of aimed)
|
|
454
|
+
await tx.insert(ACTIVE_MESH, pointer);
|
|
455
|
+
await tx.flush();
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
adoptHomeMesh(deviceId, key) {
|
|
459
|
+
return this._locked(async () => {
|
|
460
|
+
const departed = [];
|
|
461
|
+
for await (const row of this._db.find(MESH_MEMBERSHIP)) {
|
|
462
|
+
if (!row.key.equals(key) && !row.leftAt)
|
|
463
|
+
departed.push(row);
|
|
464
|
+
}
|
|
465
|
+
const pointer = await this._db.get(ACTIVE_MESH, { deviceId });
|
|
466
|
+
const leftAt = Date.now();
|
|
467
|
+
const tx = this._db.transaction();
|
|
468
|
+
for (const row of departed) {
|
|
469
|
+
await tx.insert(MESH_MEMBERSHIP, { ...row, leftAt });
|
|
470
|
+
}
|
|
471
|
+
await tx.insert(ACTIVE_MESH, {
|
|
472
|
+
deviceId,
|
|
473
|
+
activeMeshKey: pointer?.activeMeshKey,
|
|
474
|
+
homeMeshKey: key
|
|
475
|
+
});
|
|
476
|
+
await tx.flush();
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
async _pointersAt(key) {
|
|
480
|
+
const cleared = [];
|
|
481
|
+
for await (const pointer of this._db.find(ACTIVE_MESH)) {
|
|
482
|
+
const active = pointer.activeMeshKey?.equals(key);
|
|
483
|
+
const home = pointer.homeMeshKey?.equals(key);
|
|
484
|
+
if (!active && !home)
|
|
485
|
+
continue;
|
|
486
|
+
cleared.push({
|
|
487
|
+
deviceId: pointer.deviceId,
|
|
488
|
+
activeMeshKey: active ? null : pointer.activeMeshKey,
|
|
489
|
+
homeMeshKey: home ? null : pointer.homeMeshKey
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
return cleared;
|
|
493
|
+
}
|
|
494
|
+
async listMeshes() {
|
|
495
|
+
const rows = [];
|
|
496
|
+
for await (const row of this._db.find(MESH_MEMBERSHIP)) {
|
|
497
|
+
if (!row.leftAt)
|
|
498
|
+
rows.push(row);
|
|
499
|
+
}
|
|
500
|
+
return rows;
|
|
501
|
+
}
|
|
502
|
+
async listArchivedMeshes() {
|
|
503
|
+
const rows = [];
|
|
504
|
+
for await (const row of this._db.find(MESH_MEMBERSHIP)) {
|
|
505
|
+
if (row.leftAt)
|
|
506
|
+
rows.push(row);
|
|
507
|
+
}
|
|
508
|
+
return rows;
|
|
509
|
+
}
|
|
510
|
+
async homeMesh(deviceId) {
|
|
511
|
+
const pointer = await this._db.get(ACTIVE_MESH, { deviceId });
|
|
512
|
+
return this._membership(pointer?.homeMeshKey);
|
|
513
|
+
}
|
|
514
|
+
async readMeshSession(key) {
|
|
515
|
+
const row = await this._membership(key);
|
|
516
|
+
return row ? sessionOf(row) : null;
|
|
517
|
+
}
|
|
382
518
|
watch(listener) {
|
|
383
519
|
this._db.watch(listener);
|
|
384
520
|
}
|
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import type { KeyPair } from 'hyperswarm';
|
|
2
2
|
import type { Logger } from '../../index.ts';
|
|
3
3
|
import type { QvacDevice } from '../../spec/mesh/hyperschema/types.d.ts';
|
|
4
|
+
import type { DeviceInvites } from './device-invites.ts';
|
|
4
5
|
import type { Mesh } from './mesh.ts';
|
|
5
6
|
export interface MembershipView {
|
|
6
7
|
readonly members: number;
|
|
7
8
|
readonly own: QvacDevice | null;
|
|
8
|
-
readonly unattested: ReadonlyArray<
|
|
9
|
+
readonly unattested: ReadonlyArray<QvacDevice>;
|
|
9
10
|
}
|
|
10
11
|
export interface MembershipContext {
|
|
11
12
|
readonly deviceKeyPair: KeyPair;
|
|
12
13
|
readonly log: Logger | null;
|
|
13
14
|
}
|
|
14
15
|
export declare function membershipView(mesh: Mesh, deviceId: Buffer): Promise<MembershipView>;
|
|
15
|
-
export declare function attestUnattestedDevices(mesh: Mesh, { deviceKeyPair, log }: MembershipContext): Promise<void>;
|
|
16
|
+
export declare function attestUnattestedDevices(mesh: Mesh, { deviceKeyPair, log }: MembershipContext, invites: DeviceInvites): Promise<void>;
|
|
17
|
+
export interface KnownMember {
|
|
18
|
+
readonly id: Buffer;
|
|
19
|
+
readonly proof: Buffer;
|
|
20
|
+
}
|
|
21
|
+
export declare function claimOwnMembership(mesh: Mesh, { deviceKeyPair, log }: MembershipContext, known: KnownMember): Promise<void>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { attestDeviceProof } from "./identity.js";
|
|
1
|
+
import { attestDeviceProof, attestMemberClaim, claimAttestation } from "./identity.js";
|
|
2
2
|
export async function membershipView(mesh, deviceId) {
|
|
3
3
|
const snap = mesh.view.snapshot();
|
|
4
4
|
try {
|
|
@@ -11,7 +11,7 @@ export async function membershipView(mesh, deviceId) {
|
|
|
11
11
|
if (device.id.equals(deviceId))
|
|
12
12
|
own = device;
|
|
13
13
|
else if (!device.revoked && !device.memberId)
|
|
14
|
-
unattested.push(device
|
|
14
|
+
unattested.push(device);
|
|
15
15
|
}
|
|
16
16
|
return { members, own, unattested };
|
|
17
17
|
}
|
|
@@ -19,22 +19,46 @@ export async function membershipView(mesh, deviceId) {
|
|
|
19
19
|
await snap.close();
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
|
-
export async function attestUnattestedDevices(mesh, { deviceKeyPair, log }) {
|
|
22
|
+
export async function attestUnattestedDevices(mesh, { deviceKeyPair, log }, invites) {
|
|
23
23
|
const { own, unattested } = await membershipView(mesh, deviceKeyPair.publicKey);
|
|
24
24
|
if (!own?.memberId || !own.memberProof || unattested.length === 0)
|
|
25
25
|
return;
|
|
26
|
+
const ours = own.memberId;
|
|
27
|
+
const adoptable = [];
|
|
28
|
+
for (const device of unattested) {
|
|
29
|
+
if (!device.claimedMemberId?.equals(ours))
|
|
30
|
+
continue;
|
|
31
|
+
if (await invites.redeem(device.inviteToken, device.id))
|
|
32
|
+
adoptable.push(device);
|
|
33
|
+
}
|
|
34
|
+
if (adoptable.length === 0)
|
|
35
|
+
return;
|
|
26
36
|
const now = Date.now();
|
|
27
37
|
const batch = mesh.batch();
|
|
28
|
-
for (const
|
|
38
|
+
for (const device of adoptable) {
|
|
29
39
|
batch.dispatch('attest-member-device', {
|
|
30
|
-
deviceId: id,
|
|
31
|
-
memberId:
|
|
32
|
-
proof: await attestDeviceProof(id, deviceKeyPair, own.memberProof),
|
|
40
|
+
deviceId: device.id,
|
|
41
|
+
memberId: ours,
|
|
42
|
+
proof: await attestDeviceProof(device.id, deviceKeyPair, own.memberProof),
|
|
33
43
|
attestedAt: now
|
|
34
44
|
});
|
|
35
45
|
}
|
|
36
46
|
await batch.flush();
|
|
37
|
-
log?.info(['p2p', 'mesh'], 'every device
|
|
38
|
-
attested:
|
|
47
|
+
log?.info(['p2p', 'mesh'], 'every device that asked for our member is attested', {
|
|
48
|
+
attested: adoptable.length
|
|
39
49
|
});
|
|
40
50
|
}
|
|
51
|
+
export async function claimOwnMembership(mesh, { deviceKeyPair, log }, known) {
|
|
52
|
+
const deviceId = deviceKeyPair.publicKey;
|
|
53
|
+
const own = await mesh.view.get('@qvac/devices', { id: deviceId });
|
|
54
|
+
if (!own || own.memberId)
|
|
55
|
+
return;
|
|
56
|
+
const member = await mesh.view.get('@qvac/members', { id: known.id });
|
|
57
|
+
if (!member)
|
|
58
|
+
return;
|
|
59
|
+
const attestation = claimAttestation(mesh.publicKey, deviceId, mesh.localWriterKey);
|
|
60
|
+
const proof = await attestMemberClaim(attestation, deviceKeyPair, known.proof);
|
|
61
|
+
await mesh.dispatch('join-member', { member, proof });
|
|
62
|
+
await mesh.refreshOurMember();
|
|
63
|
+
log?.info('membership', 'claimed our own member in a mesh a sibling device joined');
|
|
64
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type HandoffStream } from './handoff-channel.ts';
|
|
2
|
+
import type { AssistantMeshSeedRequest } from '../../spec/hyperschema/types.d.ts';
|
|
3
|
+
export { type HandoffStream };
|
|
4
|
+
export declare const SEED_REFUSED = "the device holding this room would not share its seed";
|
|
5
|
+
export type MeshSeedRequest = AssistantMeshSeedRequest;
|
|
6
|
+
export type MeshSeedRequestHandler = (request: MeshSeedRequest, fromDeviceId: Uint8Array) => Promise<Buffer | null>;
|
|
7
|
+
export interface MeshSeedHandoffOptions {
|
|
8
|
+
readonly onrequest: MeshSeedRequestHandler;
|
|
9
|
+
readonly ackTimeoutMs?: number;
|
|
10
|
+
}
|
|
11
|
+
export declare class MeshSeedHandoff {
|
|
12
|
+
private readonly _channel;
|
|
13
|
+
constructor(opts: MeshSeedHandoffOptions);
|
|
14
|
+
addStream(stream: HandoffStream): void;
|
|
15
|
+
request(holderDeviceId: Uint8Array, meshKey: Buffer): Promise<Buffer | null>;
|
|
16
|
+
destroy(): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { getStruct } from '../../spec/hyperschema/index.js';
|
|
2
|
+
import { HandoffChannel } from "./handoff-channel.js";
|
|
3
|
+
const PROTOCOL = 'qvac/mesh-seed-handoff';
|
|
4
|
+
export {};
|
|
5
|
+
export const SEED_REFUSED = 'the device holding this room would not share its seed';
|
|
6
|
+
const handoff = getStruct('@assistant/mesh-seed-handoff');
|
|
7
|
+
const handoffAck = getStruct('@assistant/mesh-seed-handoff-ack');
|
|
8
|
+
export class MeshSeedHandoff {
|
|
9
|
+
_channel;
|
|
10
|
+
constructor(opts) {
|
|
11
|
+
this._channel = new HandoffChannel({
|
|
12
|
+
protocol: PROTOCOL,
|
|
13
|
+
requestEncoding: handoff,
|
|
14
|
+
ackEncoding: handoffAck,
|
|
15
|
+
onrequest: opts.onrequest,
|
|
16
|
+
buildAck: (id, error, result) => ({
|
|
17
|
+
id,
|
|
18
|
+
error: error || (result ? undefined : SEED_REFUSED),
|
|
19
|
+
seed: result ?? undefined
|
|
20
|
+
}),
|
|
21
|
+
readAck: (ack) => ({ error: ack.error ?? '', result: ack.seed ?? null }),
|
|
22
|
+
ackTimeoutMs: opts.ackTimeoutMs
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
addStream(stream) {
|
|
26
|
+
this._channel.addStream(stream);
|
|
27
|
+
}
|
|
28
|
+
request(holderDeviceId, meshKey) {
|
|
29
|
+
return this._channel.send(holderDeviceId, { meshKey });
|
|
30
|
+
}
|
|
31
|
+
destroy() {
|
|
32
|
+
this._channel.destroy();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -2,6 +2,7 @@ import Autobee, { type AutobeeChanges, type AutobeeHost, type AutobeeNode, type
|
|
|
2
2
|
import Corestore from 'corestore';
|
|
3
3
|
import HyperDB from 'hyperdb';
|
|
4
4
|
import Hyperblobs from 'hyperblobs';
|
|
5
|
+
import type { KeyPair } from 'hypercore-crypto';
|
|
5
6
|
import type Wakeup from 'protomux-wakeup';
|
|
6
7
|
import ReadyResource from 'ready-resource';
|
|
7
8
|
import { Files } from './files/index.ts';
|
|
@@ -46,6 +47,11 @@ export interface BirthMember {
|
|
|
46
47
|
readonly member: QvacMember;
|
|
47
48
|
readonly proof: Buffer;
|
|
48
49
|
}
|
|
50
|
+
export interface BirthClaim {
|
|
51
|
+
readonly deviceKeyPair?: KeyPair;
|
|
52
|
+
readonly memberId?: Buffer;
|
|
53
|
+
readonly inviteNonce?: Buffer | null;
|
|
54
|
+
}
|
|
49
55
|
export interface MembershipStats {
|
|
50
56
|
readonly members: number;
|
|
51
57
|
readonly attested: number;
|
|
@@ -92,6 +98,7 @@ export declare class Mesh extends ReadyResource {
|
|
|
92
98
|
get devicePublicKey(): Buffer;
|
|
93
99
|
get key(): Buffer;
|
|
94
100
|
get encryptionKey(): Buffer;
|
|
101
|
+
get seed(): Buffer;
|
|
95
102
|
get blindPeerKeys(): ReadonlyArray<Buffer>;
|
|
96
103
|
get orphanedBlobSweepStatus(): OrphanedBlobSweepStatus;
|
|
97
104
|
blindPeerStats(): Array<{
|
|
@@ -101,6 +108,7 @@ export declare class Mesh extends ReadyResource {
|
|
|
101
108
|
uploaded: number;
|
|
102
109
|
}>;
|
|
103
110
|
get ourMemberId(): Buffer | null;
|
|
111
|
+
get ourDeviceId(): Buffer | null;
|
|
104
112
|
get membership(): MembershipStats;
|
|
105
113
|
refreshMembership(): Promise<void>;
|
|
106
114
|
refreshOurMember(): Promise<void>;
|
|
@@ -115,13 +123,16 @@ export declare class Mesh extends ReadyResource {
|
|
|
115
123
|
private _sweepOrphanedBlobs;
|
|
116
124
|
get view(): HyperDB;
|
|
117
125
|
get localWriterKey(): Buffer<ArrayBufferLike>;
|
|
126
|
+
get minted(): boolean;
|
|
118
127
|
get writable(): boolean;
|
|
119
128
|
get localLength(): number;
|
|
120
129
|
dispatch(name: string, payload: unknown, opts?: {
|
|
121
130
|
optimistic?: boolean;
|
|
122
131
|
}): Promise<void>;
|
|
123
132
|
batch(): MeshBatch;
|
|
124
|
-
appendBirthOp(deviceId: Buffer, device?: DeviceMetadata, member?: BirthMember
|
|
133
|
+
appendBirthOp(deviceId: Buffer, device?: DeviceMetadata, member?: BirthMember, claim?: BirthClaim, { room }?: {
|
|
134
|
+
room?: boolean;
|
|
135
|
+
}): Promise<void>;
|
|
125
136
|
get interrupted(): unknown;
|
|
126
137
|
throwIfInterrupted(): void;
|
|
127
138
|
waitForWritable(): Promise<void>;
|
|
@@ -133,6 +144,7 @@ export declare class Mesh extends ReadyResource {
|
|
|
133
144
|
_autobeeIsTrusted(key: Buffer, reference: MeshView): Promise<boolean>;
|
|
134
145
|
_autobeeMostRecentTrusted(target: MeshView, reference: MeshView | null): Promise<AutobeeOplogRef | null>;
|
|
135
146
|
_autobeeOpen(bee: unknown): MeshView;
|
|
147
|
+
_autobeeWarmup(view: MeshView): Promise<void>;
|
|
136
148
|
_autobeeApply(nodes: ReadonlyArray<AutobeeNode>, view: MeshView, host: AutobeeHost): Promise<void>;
|
|
137
149
|
private _halt;
|
|
138
150
|
_autobeeUpdate(view: MeshView, changes: AutobeeChanges): Promise<void>;
|