@johnhenry/browsermesh-core 0.0.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/LICENSE +21 -0
- package/README.md +42 -0
- package/package.json +32 -0
- package/src/acl.mjs +530 -0
- package/src/capabilities.mjs +852 -0
- package/src/compat.mjs +29 -0
- package/src/group-keys.mjs +610 -0
- package/src/handshake.mjs +760 -0
- package/src/hardening.mjs +1183 -0
- package/src/identity-base.mjs +491 -0
- package/src/identity-tools.mjs +383 -0
- package/src/identity-wallet.mjs +505 -0
- package/src/identity.mjs +1349 -0
- package/src/index.mjs +15 -0
- package/src/keyring.mjs +741 -0
- package/src/peer-tools.mjs +959 -0
- package/src/peer.mjs +391 -0
- package/src/silent-catch.mjs +55 -0
- package/src/trust.mjs +425 -0
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* clawser-mesh-identity-tools.js -- Identity BrowserTools for mesh.
|
|
3
|
+
*
|
|
4
|
+
* 8 BrowserTool subclasses for managing mesh identities via the agent.
|
|
5
|
+
* Follows the pattern in clawser-mesh-tools.js.
|
|
6
|
+
*
|
|
7
|
+
* Run tests:
|
|
8
|
+
* node --import ./web/test/_setup-globals.mjs --test web/test/clawser-mesh-identity-tools.test.mjs
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { BrowserTool } from './compat.mjs';
|
|
12
|
+
|
|
13
|
+
// ── Shared state ─────────────────────────────────────────────────────
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Holds references to identity subsystem instances
|
|
17
|
+
* so tools can access them without import cycles.
|
|
18
|
+
*/
|
|
19
|
+
export class IdentityToolsContext {
|
|
20
|
+
#autoIdMgr = null;
|
|
21
|
+
#keyring = null;
|
|
22
|
+
#selector = null;
|
|
23
|
+
|
|
24
|
+
setAutoIdMgr(mgr) { this.#autoIdMgr = mgr; }
|
|
25
|
+
getAutoIdMgr() { return this.#autoIdMgr; }
|
|
26
|
+
|
|
27
|
+
setKeyring(kr) { this.#keyring = kr; }
|
|
28
|
+
getKeyring() { return this.#keyring; }
|
|
29
|
+
|
|
30
|
+
setSelector(sel) { this.#selector = sel; }
|
|
31
|
+
getSelector() { return this.#selector; }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Singleton context for identity tools. */
|
|
35
|
+
export const identityToolsContext = new IdentityToolsContext();
|
|
36
|
+
|
|
37
|
+
// ── identity_create ─────────────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
export class IdentityCreateTool extends BrowserTool {
|
|
40
|
+
get name() { return 'identity_create'; }
|
|
41
|
+
get description() {
|
|
42
|
+
return 'Create a new Ed25519 mesh identity with an optional label.';
|
|
43
|
+
}
|
|
44
|
+
get parameters() {
|
|
45
|
+
return {
|
|
46
|
+
type: 'object',
|
|
47
|
+
properties: {
|
|
48
|
+
label: { type: 'string', description: 'Human-readable name for the identity' },
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
get permission() { return 'approve'; }
|
|
53
|
+
|
|
54
|
+
async execute({ label } = {}) {
|
|
55
|
+
try {
|
|
56
|
+
const mgr = identityToolsContext.getAutoIdMgr();
|
|
57
|
+
if (!mgr) {
|
|
58
|
+
return { success: false, output: '', error: 'Identity manager not initialized.' };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const idMgr = mgr.identityManager;
|
|
62
|
+
const summary = await idMgr.create(label || 'unnamed');
|
|
63
|
+
await idMgr.save();
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
success: true,
|
|
67
|
+
output: `Identity created:\n Pod ID: ${summary.podId}\n Label: ${summary.label}\n DID: ${summary.did}`,
|
|
68
|
+
};
|
|
69
|
+
} catch (err) {
|
|
70
|
+
return { success: false, output: '', error: `Create failed: ${err.message}` };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ── identity_list ───────────────────────────────────────────────────
|
|
76
|
+
|
|
77
|
+
export class IdentityListTool extends BrowserTool {
|
|
78
|
+
get name() { return 'identity_list'; }
|
|
79
|
+
get description() {
|
|
80
|
+
return 'List all mesh identities with their pod IDs, labels, and active status.';
|
|
81
|
+
}
|
|
82
|
+
get parameters() {
|
|
83
|
+
return { type: 'object', properties: {} };
|
|
84
|
+
}
|
|
85
|
+
get permission() { return 'read'; }
|
|
86
|
+
|
|
87
|
+
async execute() {
|
|
88
|
+
try {
|
|
89
|
+
const mgr = identityToolsContext.getAutoIdMgr();
|
|
90
|
+
if (!mgr) {
|
|
91
|
+
return { success: true, output: 'No identities (identity manager not initialized).' };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const identities = mgr.listIdentities();
|
|
95
|
+
if (identities.length === 0) {
|
|
96
|
+
return { success: true, output: 'No identities.' };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const lines = identities.map(id =>
|
|
100
|
+
`${id.isActive ? '* ' : ' '}${id.podId.slice(0, 12)}... | ${id.label}`
|
|
101
|
+
);
|
|
102
|
+
return {
|
|
103
|
+
success: true,
|
|
104
|
+
output: `ACTIVE | POD ID | LABEL\n${lines.join('\n')}`,
|
|
105
|
+
};
|
|
106
|
+
} catch (err) {
|
|
107
|
+
return { success: false, output: '', error: err.message };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ── identity_switch ─────────────────────────────────────────────────
|
|
113
|
+
|
|
114
|
+
export class IdentitySwitchTool extends BrowserTool {
|
|
115
|
+
get name() { return 'identity_switch'; }
|
|
116
|
+
get description() {
|
|
117
|
+
return 'Switch the active mesh identity to a different pod ID.';
|
|
118
|
+
}
|
|
119
|
+
get parameters() {
|
|
120
|
+
return {
|
|
121
|
+
type: 'object',
|
|
122
|
+
properties: {
|
|
123
|
+
podId: { type: 'string', description: 'Pod ID of the identity to activate' },
|
|
124
|
+
},
|
|
125
|
+
required: ['podId'],
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
get permission() { return 'write'; }
|
|
129
|
+
|
|
130
|
+
async execute({ podId }) {
|
|
131
|
+
try {
|
|
132
|
+
const mgr = identityToolsContext.getAutoIdMgr();
|
|
133
|
+
if (!mgr) {
|
|
134
|
+
return { success: false, output: '', error: 'Identity manager not initialized.' };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
await mgr.switchIdentity(podId);
|
|
138
|
+
const active = mgr.getActive();
|
|
139
|
+
return {
|
|
140
|
+
success: true,
|
|
141
|
+
output: `Switched to identity: ${active.label} (${active.podId.slice(0, 12)}...)`,
|
|
142
|
+
};
|
|
143
|
+
} catch (err) {
|
|
144
|
+
return { success: false, output: '', error: `Switch failed: ${err.message}` };
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ── identity_export ─────────────────────────────────────────────────
|
|
150
|
+
|
|
151
|
+
export class IdentityExportTool extends BrowserTool {
|
|
152
|
+
get name() { return 'identity_export'; }
|
|
153
|
+
get description() {
|
|
154
|
+
return 'Export an identity as a JWK (optionally encrypted with a passphrase).';
|
|
155
|
+
}
|
|
156
|
+
get parameters() {
|
|
157
|
+
return {
|
|
158
|
+
type: 'object',
|
|
159
|
+
properties: {
|
|
160
|
+
podId: { type: 'string', description: 'Pod ID to export' },
|
|
161
|
+
passphrase: { type: 'string', description: 'Optional passphrase for encryption' },
|
|
162
|
+
},
|
|
163
|
+
required: ['podId'],
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
get permission() { return 'approve'; }
|
|
167
|
+
|
|
168
|
+
async execute({ podId, passphrase }) {
|
|
169
|
+
try {
|
|
170
|
+
const mgr = identityToolsContext.getAutoIdMgr();
|
|
171
|
+
if (!mgr) {
|
|
172
|
+
return { success: false, output: '', error: 'Identity manager not initialized.' };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const jwk = await mgr.identityManager.export(podId, passphrase);
|
|
176
|
+
const encrypted = jwk.encrypted ? ' (encrypted)' : '';
|
|
177
|
+
return {
|
|
178
|
+
success: true,
|
|
179
|
+
output: `Identity exported${encrypted}:\n${JSON.stringify(jwk, null, 2)}`,
|
|
180
|
+
};
|
|
181
|
+
} catch (err) {
|
|
182
|
+
return { success: false, output: '', error: `Export failed: ${err.message}` };
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ── identity_import ─────────────────────────────────────────────────
|
|
188
|
+
|
|
189
|
+
export class IdentityImportTool extends BrowserTool {
|
|
190
|
+
get name() { return 'identity_import'; }
|
|
191
|
+
get description() {
|
|
192
|
+
return 'Import an identity from a JWK private key.';
|
|
193
|
+
}
|
|
194
|
+
get parameters() {
|
|
195
|
+
return {
|
|
196
|
+
type: 'object',
|
|
197
|
+
properties: {
|
|
198
|
+
keyData: { type: 'object', description: 'JWK private key object' },
|
|
199
|
+
label: { type: 'string', description: 'Human-readable label' },
|
|
200
|
+
},
|
|
201
|
+
required: ['keyData'],
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
get permission() { return 'approve'; }
|
|
205
|
+
|
|
206
|
+
async execute({ keyData, label }) {
|
|
207
|
+
try {
|
|
208
|
+
const mgr = identityToolsContext.getAutoIdMgr();
|
|
209
|
+
if (!mgr) {
|
|
210
|
+
return { success: false, output: '', error: 'Identity manager not initialized.' };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const summary = await mgr.identityManager.import(keyData, label || 'imported');
|
|
214
|
+
await mgr.identityManager.save();
|
|
215
|
+
return {
|
|
216
|
+
success: true,
|
|
217
|
+
output: `Identity imported:\n Pod ID: ${summary.podId}\n Label: ${summary.label}\n DID: ${summary.did}`,
|
|
218
|
+
};
|
|
219
|
+
} catch (err) {
|
|
220
|
+
return { success: false, output: '', error: `Import failed: ${err.message}` };
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// ── identity_delete ─────────────────────────────────────────────────
|
|
226
|
+
|
|
227
|
+
export class IdentityDeleteTool extends BrowserTool {
|
|
228
|
+
get name() { return 'identity_delete'; }
|
|
229
|
+
get description() {
|
|
230
|
+
return 'Delete a mesh identity by pod ID. Cannot delete the last remaining identity.';
|
|
231
|
+
}
|
|
232
|
+
get parameters() {
|
|
233
|
+
return {
|
|
234
|
+
type: 'object',
|
|
235
|
+
properties: {
|
|
236
|
+
podId: { type: 'string', description: 'Pod ID to delete' },
|
|
237
|
+
},
|
|
238
|
+
required: ['podId'],
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
get permission() { return 'approve'; }
|
|
242
|
+
|
|
243
|
+
async execute({ podId }) {
|
|
244
|
+
try {
|
|
245
|
+
const mgr = identityToolsContext.getAutoIdMgr();
|
|
246
|
+
if (!mgr) {
|
|
247
|
+
return { success: false, output: '', error: 'Identity manager not initialized.' };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const idMgr = mgr.identityManager;
|
|
251
|
+
if (idMgr.size <= 1) {
|
|
252
|
+
return { success: false, output: '', error: 'Cannot delete the last identity.' };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const existed = idMgr.delete(podId);
|
|
256
|
+
if (!existed) {
|
|
257
|
+
return { success: false, output: '', error: `Identity ${podId} not found.` };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
await idMgr.save();
|
|
261
|
+
return { success: true, output: `Identity ${podId.slice(0, 12)}... deleted.` };
|
|
262
|
+
} catch (err) {
|
|
263
|
+
return { success: false, output: '', error: `Delete failed: ${err.message}` };
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// ── identity_link ───────────────────────────────────────────────────
|
|
269
|
+
|
|
270
|
+
export class IdentityLinkTool extends BrowserTool {
|
|
271
|
+
get name() { return 'identity_link'; }
|
|
272
|
+
get description() {
|
|
273
|
+
return 'Create a signed link between two identities (parent endorses child).';
|
|
274
|
+
}
|
|
275
|
+
get parameters() {
|
|
276
|
+
return {
|
|
277
|
+
type: 'object',
|
|
278
|
+
properties: {
|
|
279
|
+
parentPodId: { type: 'string', description: 'Parent identity pod ID' },
|
|
280
|
+
childPodId: { type: 'string', description: 'Child identity pod ID' },
|
|
281
|
+
relation: { type: 'string', description: 'Relation type: device, delegate, org, alias, recovery' },
|
|
282
|
+
},
|
|
283
|
+
required: ['parentPodId', 'childPodId', 'relation'],
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
get permission() { return 'approve'; }
|
|
287
|
+
|
|
288
|
+
async execute({ parentPodId, childPodId, relation }) {
|
|
289
|
+
try {
|
|
290
|
+
const mgr = identityToolsContext.getAutoIdMgr();
|
|
291
|
+
const kr = identityToolsContext.getKeyring();
|
|
292
|
+
if (!mgr || !kr) {
|
|
293
|
+
return { success: false, output: '', error: 'Identity manager or keyring not initialized.' };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const idMgr = mgr.identityManager;
|
|
297
|
+
const parentIdentity = idMgr.getIdentity(parentPodId);
|
|
298
|
+
const childIdentity = idMgr.getIdentity(childPodId);
|
|
299
|
+
|
|
300
|
+
if (!parentIdentity || !childIdentity) {
|
|
301
|
+
return { success: false, output: '', error: 'Both identities must exist locally to create a signed link.' };
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// Import SignedKeyLink dynamically to avoid circular dep
|
|
305
|
+
const { SignedKeyLink } = await import('./keyring.mjs');
|
|
306
|
+
const signedLink = await SignedKeyLink.create(parentIdentity, childIdentity, relation);
|
|
307
|
+
|
|
308
|
+
await kr.addVerifiedLink(
|
|
309
|
+
signedLink,
|
|
310
|
+
parentIdentity.keyPair.publicKey,
|
|
311
|
+
childIdentity.keyPair.publicKey
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
return {
|
|
315
|
+
success: true,
|
|
316
|
+
output: `Signed link created: ${parentPodId.slice(0, 12)}... -[${relation}]-> ${childPodId.slice(0, 12)}...`,
|
|
317
|
+
};
|
|
318
|
+
} catch (err) {
|
|
319
|
+
return { success: false, output: '', error: `Link failed: ${err.message}` };
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// ── identity_select_rule ────────────────────────────────────────────
|
|
325
|
+
|
|
326
|
+
export class IdentitySelectRuleTool extends BrowserTool {
|
|
327
|
+
get name() { return 'identity_select_rule'; }
|
|
328
|
+
get description() {
|
|
329
|
+
return 'Set a rule to use a specific identity when connecting to a peer.';
|
|
330
|
+
}
|
|
331
|
+
get parameters() {
|
|
332
|
+
return {
|
|
333
|
+
type: 'object',
|
|
334
|
+
properties: {
|
|
335
|
+
peerId: { type: 'string', description: 'Peer ID to set the rule for' },
|
|
336
|
+
podId: { type: 'string', description: 'Pod ID of the identity to use' },
|
|
337
|
+
},
|
|
338
|
+
required: ['peerId', 'podId'],
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
get permission() { return 'write'; }
|
|
342
|
+
|
|
343
|
+
async execute({ peerId, podId }) {
|
|
344
|
+
try {
|
|
345
|
+
const sel = identityToolsContext.getSelector();
|
|
346
|
+
if (!sel) {
|
|
347
|
+
return { success: false, output: '', error: 'Identity selector not initialized.' };
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
sel.setRule(peerId, podId);
|
|
351
|
+
return {
|
|
352
|
+
success: true,
|
|
353
|
+
output: `Rule set: use identity ${podId.slice(0, 12)}... for peer ${peerId.slice(0, 12)}...`,
|
|
354
|
+
};
|
|
355
|
+
} catch (err) {
|
|
356
|
+
return { success: false, output: '', error: `Rule failed: ${err.message}` };
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// ── Registry helper ──────────────────────────────────────────────────
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Register all identity tools with a BrowserToolRegistry.
|
|
365
|
+
* @param {import('./compat.mjs').BrowserToolRegistry} registry
|
|
366
|
+
* @param {import('./identity.mjs').AutoIdentityManager} [autoIdMgr]
|
|
367
|
+
* @param {import('./keyring.mjs').MeshKeyring} [keyring]
|
|
368
|
+
* @param {import('./identity.mjs').IdentitySelector} [selector]
|
|
369
|
+
*/
|
|
370
|
+
export function registerIdentityTools(registry, autoIdMgr, keyring, selector) {
|
|
371
|
+
if (autoIdMgr) identityToolsContext.setAutoIdMgr(autoIdMgr);
|
|
372
|
+
if (keyring) identityToolsContext.setKeyring(keyring);
|
|
373
|
+
if (selector) identityToolsContext.setSelector(selector);
|
|
374
|
+
|
|
375
|
+
registry.register(new IdentityCreateTool());
|
|
376
|
+
registry.register(new IdentityListTool());
|
|
377
|
+
registry.register(new IdentitySwitchTool());
|
|
378
|
+
registry.register(new IdentityExportTool());
|
|
379
|
+
registry.register(new IdentityImportTool());
|
|
380
|
+
registry.register(new IdentityDeleteTool());
|
|
381
|
+
registry.register(new IdentityLinkTool());
|
|
382
|
+
registry.register(new IdentitySelectRuleTool());
|
|
383
|
+
}
|