@qvac/core 0.6.7 → 0.7.1
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/CHANGELOG.md +211 -1
- package/README.md +2 -0
- package/dist-lib/lib/api/app-config.d.ts +3 -1
- package/dist-lib/lib/api/app-config.js +15 -4
- package/dist-lib/lib/api/chat.d.ts +1 -1
- package/dist-lib/lib/api/chat.js +9 -0
- package/dist-lib/lib/api/index.d.ts +2 -1
- package/dist-lib/lib/api/index.js +2 -1
- package/dist-lib/lib/api/network.d.ts +8 -1
- package/dist-lib/lib/api/network.js +6 -0
- package/dist-lib/lib/assistant/fake-models.d.ts +22 -0
- package/dist-lib/lib/assistant/fake-models.js +112 -0
- package/dist-lib/lib/assistant/harness.d.ts +12 -0
- package/dist-lib/lib/assistant/harness.js +26 -0
- package/dist-lib/lib/assistant/history.d.ts +5 -1
- package/dist-lib/lib/assistant/history.js +14 -2
- package/dist-lib/lib/assistant-tree.d.ts +4 -0
- package/dist-lib/lib/assistant-tree.js +45 -7
- package/dist-lib/lib/assistant.d.ts +15 -2
- package/dist-lib/lib/assistant.js +181 -42
- package/dist-lib/lib/constants.d.ts +6 -0
- package/dist-lib/lib/constants.js +6 -0
- package/dist-lib/lib/core/agent.d.ts +2 -0
- package/dist-lib/lib/core/agent.js +16 -5
- package/dist-lib/lib/core/blind-peers.d.ts +4 -0
- package/dist-lib/lib/core/blind-peers.js +1 -0
- package/dist-lib/lib/core/chat-activity.d.ts +1 -1
- package/dist-lib/lib/core/chat-activity.js +3 -0
- package/dist-lib/lib/core/chat-copy.d.ts +4 -0
- package/dist-lib/lib/core/chat-copy.js +1 -1
- package/dist-lib/lib/core/chat-fork.d.ts +21 -0
- package/dist-lib/lib/core/chat-fork.js +245 -0
- package/dist-lib/lib/core/chat-move.js +1 -4
- package/dist-lib/lib/core/chat.d.ts +1 -0
- package/dist-lib/lib/core/chat.js +1 -1
- package/dist-lib/lib/core/collections.d.ts +1 -0
- package/dist-lib/lib/core/collections.js +1 -0
- package/dist-lib/lib/core/contract/compute.d.ts +1 -1
- package/dist-lib/lib/core/contract/compute.js +2 -1
- package/dist-lib/lib/core/contract/index.js +6 -1
- package/dist-lib/lib/core/contract/invites.d.ts +1 -11
- package/dist-lib/lib/core/contract/invites.js +3 -3
- package/dist-lib/lib/core/contract/mesh-seeds.d.ts +2 -0
- package/dist-lib/lib/core/contract/mesh-seeds.js +26 -0
- package/dist-lib/lib/core/contract/open-join.d.ts +2 -0
- package/dist-lib/lib/core/contract/open-join.js +57 -0
- package/dist-lib/lib/core/departures.d.ts +8 -1
- package/dist-lib/lib/core/departures.js +59 -2
- package/dist-lib/lib/core/fs-drives.d.ts +8 -1
- package/dist-lib/lib/core/fs-drives.js +47 -15
- package/dist-lib/lib/core/harness-loan.d.ts +4 -20
- package/dist-lib/lib/core/harness-loan.js +7 -184
- package/dist-lib/lib/core/id.d.ts +1 -0
- package/dist-lib/lib/core/id.js +3 -0
- package/dist-lib/lib/core/identity.d.ts +1 -1
- package/dist-lib/lib/core/identity.js +6 -4
- package/dist-lib/lib/core/invite.d.ts +2 -0
- package/dist-lib/lib/core/invite.js +6 -2
- package/dist-lib/lib/core/membership.d.ts +1 -12
- package/dist-lib/lib/core/membership.js +1 -94
- package/dist-lib/lib/core/mesh.d.ts +11 -15
- package/dist-lib/lib/core/mesh.js +52 -88
- package/dist-lib/lib/core/migrations/agent-tools-scope.d.ts +2 -0
- package/dist-lib/lib/core/migrations/agent-tools-scope.js +24 -0
- package/dist-lib/lib/core/migrations/index.js +3 -1
- package/dist-lib/lib/core/peer-duplex.d.ts +32 -0
- package/dist-lib/lib/core/peer-duplex.js +207 -0
- package/dist-lib/lib/core/sync/index.js +2 -16
- package/dist-lib/lib/core.d.ts +47 -50
- package/dist-lib/lib/core.js +524 -878
- package/dist-lib/lib/diagnostics.d.ts +7 -0
- package/dist-lib/lib/diagnostics.js +22 -0
- package/dist-lib/lib/doctor/checks.d.ts +15 -0
- package/dist-lib/lib/doctor/checks.js +272 -0
- package/dist-lib/lib/doctor/redact.d.ts +4 -0
- package/dist-lib/lib/doctor/redact.js +31 -0
- package/dist-lib/lib/doctor/render.d.ts +8 -0
- package/dist-lib/lib/doctor/render.js +83 -0
- package/dist-lib/lib/doctor/tree.d.ts +5 -0
- package/dist-lib/lib/doctor/tree.js +167 -0
- package/dist-lib/lib/downloads-allowed-pusher.d.ts +10 -0
- package/dist-lib/lib/downloads-allowed-pusher.js +37 -0
- package/dist-lib/lib/guards.d.ts +1 -0
- package/dist-lib/lib/guards.js +4 -0
- package/dist-lib/lib/inference-calls.js +1 -5
- package/dist-lib/lib/kv-cache-reclaim.d.ts +2 -1
- package/dist-lib/lib/kv-cache-reclaim.js +24 -0
- package/dist-lib/lib/mesh-scoped-engine.js +4 -0
- package/dist-lib/lib/model-download-status.d.ts +1 -1
- package/dist-lib/lib/model-download.d.ts +9 -10
- package/dist-lib/lib/model-download.js +49 -16
- package/dist-lib/lib/model-memory-cap.d.ts +29 -0
- package/dist-lib/lib/model-memory-cap.js +69 -0
- package/dist-lib/lib/models-watch.d.ts +1 -1
- package/dist-lib/lib/models-watch.js +4 -4
- package/dist-lib/lib/oauth/flow.js +1 -1
- package/dist-lib/lib/oauth/loopback.js +16 -2
- package/dist-lib/lib/oauth/redirect-types.d.ts +1 -0
- package/dist-lib/lib/run-replay.d.ts +26 -0
- package/dist-lib/lib/run-replay.js +107 -0
- package/dist-lib/schema/config.d.ts +1 -1
- package/dist-lib/schema/config.js +1 -1
- package/dist-lib/schema/content.js +9 -1
- package/dist-lib/schema/engine.js +75 -6
- package/dist-lib/schema/field-order.js +1 -1
- package/dist-lib/schema/local.js +3 -2
- package/dist-lib/schema/qvac.js +28 -2
- package/dist-lib/spec/bind.js +17 -0
- package/dist-lib/spec/calls.js +4 -0
- package/dist-lib/spec/capabilities.d.ts +8 -0
- package/dist-lib/spec/config/hyperconf/index.js +1 -1
- package/dist-lib/spec/hrpc/hrpc.json +52 -0
- package/dist-lib/spec/hrpc/index.js +53 -4
- package/dist-lib/spec/hrpc/messages.js +1498 -1278
- package/dist-lib/spec/hyperschema/index.js +1498 -1278
- package/dist-lib/spec/hyperschema/schema.json +182 -0
- package/dist-lib/spec/hyperschema/types.d.ts +49 -3
- package/dist-lib/spec/local/hyperdb/messages.js +22 -10
- package/dist-lib/spec/local/hyperschema/index.js +16 -7
- package/dist-lib/spec/local/hyperschema/schema.json +10 -1
- package/dist-lib/spec/local/hyperschema/types.d.ts +3 -2
- 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 +530 -246
- 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 +223 -10
- package/dist-lib/spec/mesh/hyperschema/index.js +223 -10
- package/dist-lib/spec/mesh/hyperschema/schema.json +128 -1
- package/dist-lib/spec/mesh/hyperschema/types.d.ts +34 -2
- package/package.json +14 -19
- package/tui/README.md +7 -1
- package/tui/app.mjs +366 -78
- package/tui/boot.mjs +1 -0
- package/tui/index.mjs +1 -0
- package/tui/lib/fork-command.mjs +43 -0
- package/tui/lib/render.mjs +25 -1
- package/worker/assistant-sidecar.mjs +5 -2
- package/dist-lib/lib/core/device-invites.d.ts +0 -11
- package/dist-lib/lib/core/device-invites.js +0 -63
- package/dist-lib/lib/core/grant-expiry.d.ts +0 -11
- package/dist-lib/lib/core/grant-expiry.js +0 -66
- package/dist-lib/lib/core/mesh-seed-handoff.d.ts +0 -27
- package/dist-lib/lib/core/mesh-seed-handoff.js +0 -53
- package/dist-lib/lib/oauth/loopback.stub.d.ts +0 -3
- package/dist-lib/lib/oauth/loopback.stub.js +0 -6
|
@@ -10,9 +10,16 @@ export interface DriveHost {
|
|
|
10
10
|
acquire(key: string): Promise<AcquiredDrive>;
|
|
11
11
|
close(): Promise<void>;
|
|
12
12
|
}
|
|
13
|
+
export interface PeeredDriveHost extends DriveHost {
|
|
14
|
+
startBlindPeering(): Promise<void>;
|
|
15
|
+
stopBlindPeering(): Promise<void>;
|
|
16
|
+
suspend(): Promise<void>;
|
|
17
|
+
resume(): Promise<void>;
|
|
18
|
+
}
|
|
13
19
|
export interface DriveHostOptions {
|
|
14
20
|
readonly swarm: Hyperswarm;
|
|
15
21
|
readonly blindPeerKeys: ReadonlyArray<Buffer>;
|
|
16
22
|
readonly blindPeeringOpts?: BlindPeeringOpts;
|
|
23
|
+
readonly blindPeering?: boolean;
|
|
17
24
|
}
|
|
18
|
-
export declare function createFsDriveHost(store: Corestore, opts: DriveHostOptions):
|
|
25
|
+
export declare function createFsDriveHost(store: Corestore, opts: DriveHostOptions): PeeredDriveHost;
|
|
@@ -25,11 +25,23 @@ async function openDrive(store, swarm, key) {
|
|
|
25
25
|
}
|
|
26
26
|
export function createFsDriveHost(store, opts) {
|
|
27
27
|
const namespaced = store.namespace(NAMESPACE);
|
|
28
|
-
const peering = new BlindPeering(opts.swarm.dht, namespaced, {
|
|
29
|
-
pick: 2,
|
|
30
|
-
...opts.blindPeeringOpts
|
|
31
|
-
});
|
|
32
28
|
const drives = new Map();
|
|
29
|
+
let suspended = false;
|
|
30
|
+
let peering = opts.blindPeering === false ? null : startPeering();
|
|
31
|
+
function startPeering() {
|
|
32
|
+
return new BlindPeering(opts.swarm.dht, namespaced, {
|
|
33
|
+
pick: 2,
|
|
34
|
+
suspended,
|
|
35
|
+
...opts.blindPeeringOpts
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
async function peer(held) {
|
|
39
|
+
const blobs = await held.drive.getBlobs();
|
|
40
|
+
peering?.addCoreBackground(held.drive.core, { keys: opts.blindPeerKeys });
|
|
41
|
+
if (blobs) {
|
|
42
|
+
peering?.addCoreBackground(blobs.core, { keys: opts.blindPeerKeys });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
33
45
|
async function acquire(key) {
|
|
34
46
|
const cached = drives.get(key);
|
|
35
47
|
if (cached) {
|
|
@@ -37,27 +49,47 @@ export function createFsDriveHost(store, opts) {
|
|
|
37
49
|
}
|
|
38
50
|
const opening = withDeadline((async () => {
|
|
39
51
|
const held = await openDrive(namespaced, opts.swarm, key);
|
|
40
|
-
|
|
41
|
-
peering.addCoreBackground(held.drive.core, { keys: opts.blindPeerKeys });
|
|
42
|
-
if (blobs) {
|
|
43
|
-
peering.addCoreBackground(blobs.core, { keys: opts.blindPeerKeys });
|
|
44
|
-
}
|
|
52
|
+
await peer(held);
|
|
45
53
|
return held;
|
|
46
54
|
})(), DRIVE_OPEN_TIMEOUT_MS);
|
|
47
55
|
drives.set(key, opening);
|
|
48
56
|
opening.catch(() => drives.delete(key));
|
|
49
57
|
return opening;
|
|
50
58
|
}
|
|
51
|
-
async function
|
|
59
|
+
async function opened() {
|
|
52
60
|
const settled = await Promise.allSettled(drives.values());
|
|
53
|
-
|
|
54
|
-
await Promise.all(settled
|
|
61
|
+
return settled
|
|
55
62
|
.filter((result) => result.status === 'fulfilled')
|
|
56
|
-
.map(
|
|
63
|
+
.map(({ value }) => value);
|
|
64
|
+
}
|
|
65
|
+
async function startBlindPeering() {
|
|
66
|
+
if (peering) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
peering = startPeering();
|
|
70
|
+
await Promise.all((await opened()).map(peer));
|
|
71
|
+
}
|
|
72
|
+
async function stopBlindPeering() {
|
|
73
|
+
const closing = peering;
|
|
74
|
+
peering = null;
|
|
75
|
+
await closing?.close();
|
|
76
|
+
}
|
|
77
|
+
async function suspend() {
|
|
78
|
+
suspended = true;
|
|
79
|
+
await peering?.suspend();
|
|
80
|
+
}
|
|
81
|
+
async function resume() {
|
|
82
|
+
suspended = false;
|
|
83
|
+
await peering?.resume();
|
|
84
|
+
}
|
|
85
|
+
async function close() {
|
|
86
|
+
const held = await opened();
|
|
87
|
+
drives.clear();
|
|
88
|
+
await Promise.all(held.map(async ({ drive, topic }) => {
|
|
57
89
|
await topic.destroy();
|
|
58
90
|
await drive.close();
|
|
59
91
|
}));
|
|
60
|
-
await peering
|
|
92
|
+
await peering?.close();
|
|
61
93
|
}
|
|
62
|
-
return { acquire, close };
|
|
94
|
+
return { acquire, startBlindPeering, stopBlindPeering, suspend, resume, close };
|
|
63
95
|
}
|
|
@@ -1,30 +1,14 @@
|
|
|
1
|
-
import { Duplex } from 'streamx';
|
|
1
|
+
import type { Duplex } from 'streamx';
|
|
2
|
+
import { PeerDuplexChannel, type PeerStream } from './peer-duplex.ts';
|
|
2
3
|
export declare const LOAN_UNREACHABLE = "the device lending its model is not connected";
|
|
3
4
|
export declare const LOAN_REFUSED = "the device would not lend its model";
|
|
4
5
|
export declare const LOAN_REVOKED = "the device stopped lending its model";
|
|
5
6
|
export declare function loanFailureNotice(error: unknown): string | null;
|
|
6
|
-
export
|
|
7
|
-
readonly remotePublicKey: Uint8Array;
|
|
8
|
-
readonly noiseStream: {
|
|
9
|
-
userData: unknown;
|
|
10
|
-
};
|
|
11
|
-
}
|
|
7
|
+
export type LoanStream = PeerStream;
|
|
12
8
|
export interface HarnessLoanOptions {
|
|
13
9
|
readonly onborrow: (fromDeviceId: Uint8Array) => Promise<Duplex | null>;
|
|
14
10
|
}
|
|
15
|
-
export declare class HarnessLoan {
|
|
16
|
-
private readonly _opts;
|
|
17
|
-
private readonly _peers;
|
|
18
|
-
private _nextId;
|
|
19
|
-
private readonly _pairing;
|
|
11
|
+
export declare class HarnessLoan extends PeerDuplexChannel {
|
|
20
12
|
constructor(opts: HarnessLoanOptions);
|
|
21
|
-
addStream(stream: LoanStream): void;
|
|
22
13
|
borrow(deviceId: string): Duplex;
|
|
23
|
-
revalidate(allowed: (deviceId: Uint8Array) => Promise<boolean>): Promise<void>;
|
|
24
|
-
destroy(): void;
|
|
25
|
-
private _attach;
|
|
26
|
-
private _serve;
|
|
27
|
-
private _deliver;
|
|
28
|
-
private _end;
|
|
29
|
-
private _drop;
|
|
30
14
|
}
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { raw, uint } from 'compact-encoding';
|
|
3
|
-
import { Duplex } from 'streamx';
|
|
4
|
-
import { createProtocolPairing } from './protocol-pairing.js';
|
|
1
|
+
import { PeerDuplexChannel } from './peer-duplex.js';
|
|
5
2
|
const PROTOCOL = 'qvac/harness-loan';
|
|
6
|
-
function noop() { }
|
|
7
3
|
export const LOAN_UNREACHABLE = 'the device lending its model is not connected';
|
|
8
4
|
export const LOAN_REFUSED = 'the device would not lend its model';
|
|
9
5
|
export const LOAN_REVOKED = 'the device stopped lending its model';
|
|
@@ -22,189 +18,16 @@ function messageOf(error) {
|
|
|
22
18
|
return error.message;
|
|
23
19
|
return typeof error === 'string' ? error : '';
|
|
24
20
|
}
|
|
25
|
-
|
|
26
|
-
const frame = {
|
|
27
|
-
preencode(state, f) {
|
|
28
|
-
uint.preencode(state, f.id);
|
|
29
|
-
uint.preencode(state, f.mine ? 1 : 0);
|
|
30
|
-
bytes.preencode(state, f.data);
|
|
31
|
-
},
|
|
32
|
-
encode(state, f) {
|
|
33
|
-
uint.encode(state, f.id);
|
|
34
|
-
uint.encode(state, f.mine ? 1 : 0);
|
|
35
|
-
bytes.encode(state, f.data);
|
|
36
|
-
},
|
|
37
|
-
decode(state) {
|
|
38
|
-
const id = uint.decode(state);
|
|
39
|
-
const mine = uint.decode(state) === 1;
|
|
40
|
-
return { id, mine, data: bytes.decode(state) };
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
const ends = {
|
|
44
|
-
preencode(state, e) {
|
|
45
|
-
uint.preencode(state, e.id);
|
|
46
|
-
uint.preencode(state, e.mine ? 1 : 0);
|
|
47
|
-
},
|
|
48
|
-
encode(state, e) {
|
|
49
|
-
uint.encode(state, e.id);
|
|
50
|
-
uint.encode(state, e.mine ? 1 : 0);
|
|
51
|
-
},
|
|
52
|
-
decode(state) {
|
|
53
|
-
return { id: uint.decode(state), mine: uint.decode(state) === 1 };
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
export class HarnessLoan {
|
|
57
|
-
_opts;
|
|
58
|
-
_peers;
|
|
59
|
-
_nextId;
|
|
60
|
-
_pairing;
|
|
21
|
+
export class HarnessLoan extends PeerDuplexChannel {
|
|
61
22
|
constructor(opts) {
|
|
62
|
-
|
|
63
|
-
this._peers = new Map();
|
|
64
|
-
this._nextId = 1;
|
|
65
|
-
this._pairing = createProtocolPairing(PROTOCOL);
|
|
66
|
-
}
|
|
67
|
-
addStream(stream) {
|
|
68
|
-
const deviceId = b4a.toHex(stream.remotePublicKey);
|
|
69
|
-
if (this._peers.has(deviceId)) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
const channel = this._pairing
|
|
73
|
-
.mux(stream, () => this.addStream(stream))
|
|
74
|
-
.createChannel({
|
|
23
|
+
super({
|
|
75
24
|
protocol: PROTOCOL,
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
const open = channel.addMessage({
|
|
82
|
-
encoding: uint,
|
|
83
|
-
onmessage: (id) => void this._serve(deviceId, id)
|
|
25
|
+
onaccept: opts.onborrow,
|
|
26
|
+
unreachable: LOAN_UNREACHABLE,
|
|
27
|
+
revoked: LOAN_REVOKED
|
|
84
28
|
});
|
|
85
|
-
const data = channel.addMessage({
|
|
86
|
-
encoding: frame,
|
|
87
|
-
onmessage: (f) => this._deliver(deviceId, f)
|
|
88
|
-
});
|
|
89
|
-
const close = channel.addMessage({
|
|
90
|
-
encoding: ends,
|
|
91
|
-
onmessage: (e) => this._end(deviceId, e)
|
|
92
|
-
});
|
|
93
|
-
this._peers.set(deviceId, {
|
|
94
|
-
channel,
|
|
95
|
-
remoteKey: stream.remotePublicKey,
|
|
96
|
-
open,
|
|
97
|
-
data,
|
|
98
|
-
close,
|
|
99
|
-
borrowed: new Map(),
|
|
100
|
-
served: new Map()
|
|
101
|
-
});
|
|
102
|
-
channel.open();
|
|
103
29
|
}
|
|
104
30
|
borrow(deviceId) {
|
|
105
|
-
|
|
106
|
-
if (!peer) {
|
|
107
|
-
throw new Error(LOAN_UNREACHABLE);
|
|
108
|
-
}
|
|
109
|
-
const id = this._nextId;
|
|
110
|
-
this._nextId += 1;
|
|
111
|
-
const stream = this._attach(peer, id, { mine: true });
|
|
112
|
-
peer.open.send(id);
|
|
113
|
-
return stream;
|
|
114
|
-
}
|
|
115
|
-
async revalidate(allowed) {
|
|
116
|
-
for (const peer of [...this._peers.values()]) {
|
|
117
|
-
if (peer.served.size === 0)
|
|
118
|
-
continue;
|
|
119
|
-
if (await allowed(peer.remoteKey).catch(() => false))
|
|
120
|
-
continue;
|
|
121
|
-
for (const [id, stream] of [...peer.served]) {
|
|
122
|
-
peer.served.delete(id);
|
|
123
|
-
peer.close.send({ id, mine: false });
|
|
124
|
-
stream.destroy(new Error(LOAN_REVOKED));
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
destroy() {
|
|
129
|
-
this._pairing.destroy();
|
|
130
|
-
for (const [deviceId, peer] of [...this._peers]) {
|
|
131
|
-
try {
|
|
132
|
-
peer.channel.close();
|
|
133
|
-
}
|
|
134
|
-
catch { }
|
|
135
|
-
this._drop(deviceId);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
_attach(peer, id, opts) {
|
|
139
|
-
const { mine } = opts;
|
|
140
|
-
const held = mine ? peer.borrowed : peer.served;
|
|
141
|
-
const stream = new Duplex({
|
|
142
|
-
write: (chunk, cb) => {
|
|
143
|
-
peer.data.send({ id, mine, data: chunk });
|
|
144
|
-
cb(null);
|
|
145
|
-
},
|
|
146
|
-
final: (cb) => {
|
|
147
|
-
peer.close.send({ id, mine });
|
|
148
|
-
cb(null);
|
|
149
|
-
},
|
|
150
|
-
destroy: (cb) => {
|
|
151
|
-
if (held.delete(id)) {
|
|
152
|
-
peer.close.send({ id, mine });
|
|
153
|
-
}
|
|
154
|
-
cb(null);
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
held.set(id, stream);
|
|
158
|
-
return stream;
|
|
159
|
-
}
|
|
160
|
-
async _serve(deviceId, id) {
|
|
161
|
-
const peer = this._peers.get(deviceId);
|
|
162
|
-
if (!peer || peer.served.has(id)) {
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
const stream = this._attach(peer, id, { mine: false });
|
|
166
|
-
const served = await this._opts.onborrow(peer.remoteKey).catch(noop);
|
|
167
|
-
if (!served) {
|
|
168
|
-
stream.destroy();
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
stream.on('error', noop);
|
|
172
|
-
served.on('error', noop);
|
|
173
|
-
stream.pipe(served).pipe(stream);
|
|
174
|
-
}
|
|
175
|
-
_deliver(deviceId, f) {
|
|
176
|
-
const peer = this._peers.get(deviceId);
|
|
177
|
-
if (!peer) {
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
const held = f.mine ? peer.served : peer.borrowed;
|
|
181
|
-
held.get(f.id)?.push(f.data);
|
|
182
|
-
}
|
|
183
|
-
_end(deviceId, e) {
|
|
184
|
-
const peer = this._peers.get(deviceId);
|
|
185
|
-
if (!peer) {
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
const held = e.mine ? peer.served : peer.borrowed;
|
|
189
|
-
const stream = held.get(e.id);
|
|
190
|
-
if (!stream) {
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
held.delete(e.id);
|
|
194
|
-
stream.push(null);
|
|
195
|
-
stream.destroy();
|
|
196
|
-
}
|
|
197
|
-
_drop(deviceId) {
|
|
198
|
-
const peer = this._peers.get(deviceId);
|
|
199
|
-
if (!peer) {
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
this._peers.delete(deviceId);
|
|
203
|
-
for (const held of [peer.borrowed, peer.served]) {
|
|
204
|
-
for (const stream of held.values()) {
|
|
205
|
-
stream.destroy(new Error(LOAN_UNREACHABLE));
|
|
206
|
-
}
|
|
207
|
-
held.clear();
|
|
208
|
-
}
|
|
31
|
+
return this.dial(deviceId);
|
|
209
32
|
}
|
|
210
33
|
}
|
package/dist-lib/lib/core/id.js
CHANGED
|
@@ -18,9 +18,9 @@ export declare function generateMnemonic(): Promise<string>;
|
|
|
18
18
|
export declare function openIdentity(mnemonic: string): Promise<IdentityKey>;
|
|
19
19
|
export declare function mintIdentity(mnemonic: string, devicePublicKey: Buffer): Promise<MintedIdentity>;
|
|
20
20
|
export declare function attestDeviceProof(devicePublicKey: Buffer, parent: KeyPair, parentProof: Buffer): Promise<Buffer>;
|
|
21
|
+
export declare function delegationFits(proof: Buffer, delegate: KeyPair): Promise<boolean>;
|
|
21
22
|
export declare function verifyDeviceProof(proof: Buffer, expected: ExpectedDeviceProof): Promise<boolean>;
|
|
22
23
|
export declare function claimAttestation(meshPublicKey: Buffer, deviceId: Buffer, writerKey: Buffer): Buffer;
|
|
23
|
-
export declare function deviceInviteToken(nonce: Buffer, deviceId: Buffer): Buffer;
|
|
24
24
|
export declare function attestMemberClaim(attestation: Buffer, deviceKeyPair: KeyPair, deviceProof: Buffer): Promise<Buffer>;
|
|
25
25
|
export declare function verifyMemberClaim(proof: Buffer, expected: ExpectedMemberClaim): Promise<boolean>;
|
|
26
26
|
export declare function verifyRecoveryClaim(proof: Buffer, expected: ExpectedMemberClaim): Promise<boolean>;
|
|
@@ -3,7 +3,6 @@ import crypto from 'hypercore-crypto';
|
|
|
3
3
|
import { ProofEncoding } from 'keet-identity-key/lib/encoding.js';
|
|
4
4
|
export const MAX_PROOF_BYTES = 1024;
|
|
5
5
|
const [CLAIM_NAMESPACE] = crypto.namespace('@qvac/core/member-claim', 1);
|
|
6
|
-
const [INVITE_NAMESPACE] = crypto.namespace('@qvac/core/device-invite', 1);
|
|
7
6
|
let loaded = null;
|
|
8
7
|
async function identityKey() {
|
|
9
8
|
if (loaded === null) {
|
|
@@ -26,6 +25,12 @@ export async function attestDeviceProof(devicePublicKey, parent, parentProof) {
|
|
|
26
25
|
throw new Error('attestDeviceProof needs the parent proof');
|
|
27
26
|
return (await identityKey()).attestDevice(devicePublicKey, parent, parentProof);
|
|
28
27
|
}
|
|
28
|
+
export async function delegationFits(proof, delegate) {
|
|
29
|
+
const device = crypto.keyPair();
|
|
30
|
+
const extended = await attestDeviceProof(device.publicKey, delegate, proof);
|
|
31
|
+
const claim = await attestMemberClaim(crypto.randomBytes(32), device, extended);
|
|
32
|
+
return claim.byteLength <= MAX_PROOF_BYTES;
|
|
33
|
+
}
|
|
29
34
|
export async function verifyDeviceProof(proof, expected) {
|
|
30
35
|
if (proof.byteLength > MAX_PROOF_BYTES)
|
|
31
36
|
return false;
|
|
@@ -53,9 +58,6 @@ export async function verifyDeviceProof(proof, expected) {
|
|
|
53
58
|
export function claimAttestation(meshPublicKey, deviceId, writerKey) {
|
|
54
59
|
return crypto.hash([CLAIM_NAMESPACE, meshPublicKey, deviceId, writerKey]);
|
|
55
60
|
}
|
|
56
|
-
export function deviceInviteToken(nonce, deviceId) {
|
|
57
|
-
return crypto.hash([INVITE_NAMESPACE, nonce, deviceId]);
|
|
58
|
-
}
|
|
59
61
|
export async function attestMemberClaim(attestation, deviceKeyPair, deviceProof) {
|
|
60
62
|
return (await identityKey()).attestData(attestation, deviceKeyPair, deviceProof);
|
|
61
63
|
}
|
|
@@ -16,6 +16,8 @@ export interface MeshInvite {
|
|
|
16
16
|
readonly compute?: boolean;
|
|
17
17
|
readonly recovery?: RecoveryProof;
|
|
18
18
|
readonly inviterName?: string;
|
|
19
|
+
readonly deviceSeed?: Buffer;
|
|
20
|
+
readonly deviceProof?: Buffer;
|
|
19
21
|
}
|
|
20
22
|
export declare function encodeMeshInvite(invite: MeshInvite): string;
|
|
21
23
|
export declare function decodeMeshInvite(raw: string): MeshInvite | null;
|
|
@@ -13,7 +13,9 @@ export function encodeMeshInvite(invite) {
|
|
|
13
13
|
memberId: invite.memberId ?? null,
|
|
14
14
|
inviteNonce: invite.inviteNonce ?? null,
|
|
15
15
|
compute: invite.compute ?? false,
|
|
16
|
-
inviterName: invite.inviterName ?? null
|
|
16
|
+
inviterName: invite.inviterName ?? null,
|
|
17
|
+
deviceSeed: invite.deviceSeed ?? null,
|
|
18
|
+
deviceProof: invite.deviceProof ?? null
|
|
17
19
|
};
|
|
18
20
|
return INVITE_PREFIX + z32.encode(encodeStruct(INVITE_STRUCT, payload));
|
|
19
21
|
}
|
|
@@ -42,6 +44,8 @@ export function decodeMeshInvite(raw) {
|
|
|
42
44
|
memberId: payload.memberId ?? undefined,
|
|
43
45
|
inviteNonce: payload.inviteNonce ?? undefined,
|
|
44
46
|
compute: payload.compute ?? false,
|
|
45
|
-
inviterName: payload.inviterName ?? undefined
|
|
47
|
+
inviterName: payload.inviterName ?? undefined,
|
|
48
|
+
deviceSeed: payload.deviceSeed ?? undefined,
|
|
49
|
+
deviceProof: payload.deviceProof ?? undefined
|
|
46
50
|
};
|
|
47
51
|
}
|
|
@@ -2,9 +2,7 @@ import type { KeyPair } from 'hyperswarm';
|
|
|
2
2
|
import type { Logger } from '../../index.ts';
|
|
3
3
|
import type { LocalIdentity } from '../../spec/local/hyperschema/types.d.ts';
|
|
4
4
|
import type { QvacDevice } from '../../spec/mesh/hyperschema/types.d.ts';
|
|
5
|
-
import type {
|
|
6
|
-
import { type Mesh } from './mesh.ts';
|
|
7
|
-
import type { MeshAdmissionRequest } from './mesh-seed-handoff.ts';
|
|
5
|
+
import type { Mesh } from './mesh.ts';
|
|
8
6
|
export interface MembershipView {
|
|
9
7
|
readonly members: number;
|
|
10
8
|
readonly own: QvacDevice | null;
|
|
@@ -15,14 +13,6 @@ export interface MembershipContext {
|
|
|
15
13
|
readonly log: Logger | null;
|
|
16
14
|
}
|
|
17
15
|
export declare function membershipView(mesh: Mesh, deviceId: Buffer): Promise<MembershipView>;
|
|
18
|
-
export declare function attestUnattestedDevices(mesh: Mesh, { deviceKeyPair, log }: MembershipContext, invites: DeviceInvites): Promise<void>;
|
|
19
|
-
interface AdmitInvitedDeviceOptions {
|
|
20
|
-
readonly invites: DeviceInvites;
|
|
21
|
-
readonly deviceId: Buffer;
|
|
22
|
-
readonly request: MeshAdmissionRequest;
|
|
23
|
-
}
|
|
24
|
-
export declare function admitInvitedDevice(mesh: Mesh, { deviceKeyPair, log }: MembershipContext, { invites, deviceId, request }: AdmitInvitedDeviceOptions): Promise<boolean>;
|
|
25
|
-
export declare function sealAccount(mesh: Mesh, { deviceKeyPair, log }: MembershipContext): Promise<void>;
|
|
26
16
|
export interface KnownMember {
|
|
27
17
|
readonly id: Buffer;
|
|
28
18
|
readonly name: string;
|
|
@@ -37,4 +27,3 @@ export interface RenameFoundedMemberOptions {
|
|
|
37
27
|
readonly adopt: (name: string, namedAt: number) => Promise<unknown>;
|
|
38
28
|
}
|
|
39
29
|
export declare function renameFoundedMember(mesh: Mesh, { deviceKeyPair, log }: MembershipContext, { identity, adopt }: RenameFoundedMemberOptions): Promise<void>;
|
|
40
|
-
export {};
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import b4a from 'b4a';
|
|
2
|
-
import { CAPABILITY_PROFILES } from '../../schema/qvac.js';
|
|
3
|
-
import { verifyMemberWriterClaim, verifyWriterClaim } from './device-attestation.js';
|
|
4
2
|
import { attestDeviceProof, attestMemberClaim, claimAttestation } from './identity.js';
|
|
5
|
-
import { claimableDevice } from './mesh.js';
|
|
6
3
|
import { meshKind } from './contract/mesh-kind.js';
|
|
7
|
-
import { DEVICES, MEMBERS
|
|
4
|
+
import { DEVICES, MEMBERS } from './collections.js';
|
|
8
5
|
export async function membershipView(mesh, deviceId) {
|
|
9
6
|
const snap = mesh.view.snapshot();
|
|
10
7
|
try {
|
|
@@ -28,93 +25,6 @@ export async function membershipView(mesh, deviceId) {
|
|
|
28
25
|
await snap.close();
|
|
29
26
|
}
|
|
30
27
|
}
|
|
31
|
-
export async function attestUnattestedDevices(mesh, { deviceKeyPair, log }, invites) {
|
|
32
|
-
const { own, unattested } = await membershipView(mesh, deviceKeyPair.publicKey);
|
|
33
|
-
if (!own?.memberId || !own.memberProof || unattested.length === 0) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
const ours = own.memberId;
|
|
37
|
-
const adoptable = [];
|
|
38
|
-
for (const device of unattested) {
|
|
39
|
-
if (!device.claimedMemberId || !b4a.equals(device.claimedMemberId, ours)) {
|
|
40
|
-
continue;
|
|
41
|
-
}
|
|
42
|
-
if (await invites.redeem(device.inviteToken, device.id)) {
|
|
43
|
-
adoptable.push(device);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
if (adoptable.length === 0) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
const now = Date.now();
|
|
50
|
-
const batch = mesh.batch();
|
|
51
|
-
for (const device of adoptable) {
|
|
52
|
-
batch.dispatch('attest-member-device', {
|
|
53
|
-
deviceId: device.id,
|
|
54
|
-
memberId: ours,
|
|
55
|
-
proof: await attestDeviceProof(device.id, deviceKeyPair, own.memberProof),
|
|
56
|
-
attestedAt: now
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
await batch.flush();
|
|
60
|
-
log?.info(['p2p', 'mesh'], 'every device that asked for our member is attested', {
|
|
61
|
-
attested: adoptable.length
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
export async function admitInvitedDevice(mesh, { deviceKeyPair, log }, { invites, deviceId, request }) {
|
|
65
|
-
const own = await mesh.view.get(DEVICES, { id: deviceKeyPair.publicKey });
|
|
66
|
-
if (!own?.memberId || !own.memberProof || own.revoked) {
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
const { writerKey, name, recommendedModelName } = request;
|
|
70
|
-
if (!writerKey) {
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
const joinedAt = request.joinedAt ?? 0;
|
|
74
|
-
const claim = { meshPublicKey: mesh.publicKey, deviceId, writerKey, joinedAt };
|
|
75
|
-
if (!verifyWriterClaim(request.deviceSignature, claim) ||
|
|
76
|
-
!verifyMemberWriterClaim(request.memberSignature, { ...claim, memberId: own.memberId })) {
|
|
77
|
-
log?.warn(['p2p', 'mesh'], 'device invite refused: the asker did not sign its claim');
|
|
78
|
-
return false;
|
|
79
|
-
}
|
|
80
|
-
const { capabilityProfile } = request;
|
|
81
|
-
if (!name || !recommendedModelName || !isCapabilityProfile(capabilityProfile)) {
|
|
82
|
-
log?.warn(['p2p', 'mesh'], 'device invite refused: the asker sent no usable profile');
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
if (!(await invites.redeem(request.inviteToken, deviceId))) {
|
|
86
|
-
log?.warn(['p2p', 'mesh'], 'device invite refused: not one of ours for this device');
|
|
87
|
-
return false;
|
|
88
|
-
}
|
|
89
|
-
await mesh.dispatch('vouched-join', {
|
|
90
|
-
signature: mesh.sign(writerKey),
|
|
91
|
-
deviceId,
|
|
92
|
-
device: claimableDevice({
|
|
93
|
-
id: deviceId,
|
|
94
|
-
joinedAt,
|
|
95
|
-
name,
|
|
96
|
-
capabilityProfile,
|
|
97
|
-
recommendedModelName
|
|
98
|
-
}),
|
|
99
|
-
deviceSignature: request.deviceSignature,
|
|
100
|
-
writerKey,
|
|
101
|
-
memberProof: await attestDeviceProof(deviceId, deviceKeyPair, own.memberProof),
|
|
102
|
-
memberSignature: request.memberSignature
|
|
103
|
-
});
|
|
104
|
-
log?.info(['p2p', 'mesh'], 'the device that redeemed our invite is admitted');
|
|
105
|
-
return true;
|
|
106
|
-
}
|
|
107
|
-
export async function sealAccount(mesh, { deviceKeyPair, log }) {
|
|
108
|
-
const own = await mesh.view.get(DEVICES, { id: deviceKeyPair.publicKey });
|
|
109
|
-
if (!own?.memberId) {
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
if (await mesh.view.get(MESH_KINDS, { id: 'mesh' })) {
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
await mesh.dispatch('mint-mesh-kind', { id: 'mesh', kind: 'account' });
|
|
116
|
-
log?.info(['p2p', 'mesh'], 'the account is sealed against a bare join');
|
|
117
|
-
}
|
|
118
28
|
export async function claimOwnMembership(mesh, { deviceKeyPair, log }, known) {
|
|
119
29
|
const deviceId = deviceKeyPair.publicKey;
|
|
120
30
|
const own = await mesh.view.get(DEVICES, { id: deviceId });
|
|
@@ -163,6 +73,3 @@ export async function renameFoundedMember(mesh, { deviceKeyPair, log }, { identi
|
|
|
163
73
|
});
|
|
164
74
|
log?.info('membership', 'the member this device founded carries its user name');
|
|
165
75
|
}
|
|
166
|
-
function isCapabilityProfile(value) {
|
|
167
|
-
return !!value && CAPABILITY_PROFILES.includes(value);
|
|
168
|
-
}
|
|
@@ -9,7 +9,7 @@ import { Files } from './files/index.ts';
|
|
|
9
9
|
import type { Logger } from '../../index.ts';
|
|
10
10
|
import { Router } from '../../spec/mesh/hyperdispatch/index.js';
|
|
11
11
|
import type { QvacCapabilityProfile, QvacDevice, QvacMember, QvacMeshName } from '../../spec/mesh/hyperschema/types.d.ts';
|
|
12
|
-
import type { BlindPeeringOpts } from './blind-peers.ts';
|
|
12
|
+
import type { BlindPeeringOpts, StartBlindPeeringOpts } from './blind-peers.ts';
|
|
13
13
|
import type { AttachmentUrl } from './attachment-link.ts';
|
|
14
14
|
import type { MeshFileIndex } from './files/local-index.ts';
|
|
15
15
|
import { type DeviceAttestation } from './device-attestation.ts';
|
|
@@ -41,6 +41,7 @@ export interface MeshOptions {
|
|
|
41
41
|
attachmentUrl?: AttachmentUrl | null;
|
|
42
42
|
fileIndex?: MeshFileIndex | null;
|
|
43
43
|
}
|
|
44
|
+
export declare const MESH_JOINING = "This mesh is still joining: it accepts writes once it is joined";
|
|
44
45
|
export declare function meshPublicKeyFromSeed(seed: Buffer): Buffer;
|
|
45
46
|
export declare function isOurTrustedWriter(snap: HyperDB, writerKey: Buffer, ourMemberId: Buffer | null): Promise<boolean>;
|
|
46
47
|
export type BirthRow = {
|
|
@@ -56,17 +57,9 @@ export interface OurMembership {
|
|
|
56
57
|
readonly proof: Buffer;
|
|
57
58
|
readonly attestedAt: number;
|
|
58
59
|
}
|
|
59
|
-
export interface BirthClaim {
|
|
60
|
-
readonly memberId?: Buffer;
|
|
61
|
-
readonly inviteNonce?: Buffer | null;
|
|
62
|
-
}
|
|
63
60
|
interface BirthOptions {
|
|
64
61
|
readonly member?: BirthMember;
|
|
65
|
-
readonly claim?: BirthClaim;
|
|
66
62
|
readonly sharing?: boolean;
|
|
67
|
-
readonly grantSecret?: Buffer;
|
|
68
|
-
readonly recovery?: RecoveryProof;
|
|
69
|
-
readonly vouched?: boolean;
|
|
70
63
|
}
|
|
71
64
|
export interface RecoveryProof {
|
|
72
65
|
readonly memberId: Buffer;
|
|
@@ -116,8 +109,8 @@ export declare class Mesh extends ReadyResource {
|
|
|
116
109
|
private _name;
|
|
117
110
|
private _membership;
|
|
118
111
|
private readonly _deviceId;
|
|
119
|
-
private readonly _grantExpiry;
|
|
120
112
|
private _admitted;
|
|
113
|
+
private _birth;
|
|
121
114
|
private _autobee;
|
|
122
115
|
private _blobs;
|
|
123
116
|
private _syncActivity;
|
|
@@ -142,14 +135,12 @@ export declare class Mesh extends ReadyResource {
|
|
|
142
135
|
get name(): QvacMeshName | null;
|
|
143
136
|
refreshName(): Promise<void>;
|
|
144
137
|
setName(name: string): Promise<void>;
|
|
145
|
-
refreshGrantExpiry(): Promise<void>;
|
|
146
|
-
bornKind(): Promise<string | null | undefined>;
|
|
147
|
-
waitForGenesis(timeout: number): Promise<string | null | undefined>;
|
|
148
138
|
sign(message: Buffer): Buffer;
|
|
149
139
|
attestDevice(deviceId: Buffer): DeviceAttestation;
|
|
150
140
|
constructor(store: Corestore, opts?: MeshOptions);
|
|
151
141
|
_open(): Promise<void>;
|
|
152
|
-
startBlindPeering(dht: unknown): void;
|
|
142
|
+
startBlindPeering(dht: unknown, { suspended }?: StartBlindPeeringOpts): void;
|
|
143
|
+
stopBlindPeering(): Promise<void>;
|
|
153
144
|
openBlobs(coreKey: Buffer): Promise<Hyperblobs>;
|
|
154
145
|
interruptBoot(): void;
|
|
155
146
|
_close(): Promise<void>;
|
|
@@ -160,9 +151,12 @@ export declare class Mesh extends ReadyResource {
|
|
|
160
151
|
get minted(): boolean;
|
|
161
152
|
get writable(): boolean;
|
|
162
153
|
get localLength(): number;
|
|
154
|
+
get joining(): boolean;
|
|
163
155
|
dispatch(name: string, payload: unknown, opts?: AppendOptions): Promise<void>;
|
|
164
156
|
batch(): MeshBatch;
|
|
165
|
-
appendBirthOp(identity: Buffer | KeyPair, device?: DeviceMetadata,
|
|
157
|
+
appendBirthOp(identity: Buffer | KeyPair, device?: DeviceMetadata, opts?: BirthOptions | (() => Promise<BirthOptions>)): Promise<void>;
|
|
158
|
+
private _settleBirth;
|
|
159
|
+
private _appendBirthOp;
|
|
166
160
|
get interrupted(): unknown;
|
|
167
161
|
throwIfInterrupted(): void;
|
|
168
162
|
waitForWritable(timeout?: number): Promise<void>;
|
|
@@ -190,10 +184,12 @@ export interface MeshDispatcher {
|
|
|
190
184
|
}
|
|
191
185
|
export interface MeshBatchOptions {
|
|
192
186
|
readonly compute?: boolean;
|
|
187
|
+
readonly joined?: boolean;
|
|
193
188
|
}
|
|
194
189
|
export declare class MeshBatch {
|
|
195
190
|
private readonly _autobee;
|
|
196
191
|
private readonly _compute;
|
|
192
|
+
private readonly _joined;
|
|
197
193
|
private _ops;
|
|
198
194
|
constructor(autobee: Autobee<MeshView>, opts?: MeshBatchOptions);
|
|
199
195
|
get size(): number;
|