@cero-base/core 2.4.1 → 2.6.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/package.json +22 -15
- package/src/database/dispatch.js +159 -30
- package/src/database/encryption.js +9 -8
- package/src/database/index.js +119 -10
- package/src/database/rotation.js +13 -11
- package/src/identity/index.js +131 -56
- package/src/lib/schema.js +15 -7
- package/src/lib/spec/index.js +15 -12
- package/src/lib/spec/schema.json +10 -10
- package/src/lib/utils.js +5 -4
- package/src/network/index.js +5 -11
- package/src/pairing/index.js +18 -10
- package/src/push/index.js +37 -0
- package/types/database/encryption.d.ts +8 -6
- package/types/database/index.d.ts +40 -3
- package/types/database/rotation.d.ts +2 -2
- package/types/identity/index.d.ts +101 -41
- package/types/lib/schema.d.ts +15 -4
- package/types/lib/spec/index.d.ts +4 -4
- package/types/network/index.d.ts +1 -7
- package/types/pairing/index.d.ts +13 -2
- package/types/push/index.d.ts +23 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cero-base/core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"description": "cero p2p primitives — identity, storage, network, database, blobs, rpc, pairing.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -73,6 +73,10 @@
|
|
|
73
73
|
"types": "./types/pairing/index.d.ts",
|
|
74
74
|
"default": "./src/pairing/index.js"
|
|
75
75
|
},
|
|
76
|
+
"./push": {
|
|
77
|
+
"types": "./types/push/index.d.ts",
|
|
78
|
+
"default": "./src/push/index.js"
|
|
79
|
+
},
|
|
76
80
|
"./invite": {
|
|
77
81
|
"types": "./types/pairing/invite.d.ts",
|
|
78
82
|
"default": "./src/pairing/invite.js"
|
|
@@ -151,22 +155,23 @@
|
|
|
151
155
|
"autobee": "2.12.1",
|
|
152
156
|
"b4a": "^1.9.0",
|
|
153
157
|
"bare-crypto": "^1.15.4",
|
|
154
|
-
"bare-fs": "^4.8.
|
|
158
|
+
"bare-fs": "^4.8.4",
|
|
155
159
|
"bare-path": "^3.1.2",
|
|
156
160
|
"bip39-mnemonic": "^2.5.0",
|
|
157
|
-
"blind-peering": "^2.
|
|
161
|
+
"blind-peering": "^2.12.0",
|
|
162
|
+
"blind-push": "^1.0.2",
|
|
158
163
|
"compact-encoding": "^3.5.2",
|
|
159
|
-
"corestore": "^7.
|
|
164
|
+
"corestore": "^7.16.1",
|
|
160
165
|
"framed-stream": "^1.0.1",
|
|
161
166
|
"hrpc": "^4.3.1",
|
|
162
|
-
"hyperblobs": "^2.
|
|
163
|
-
"hypercore": "^11.
|
|
164
|
-
"hypercore-blob-server": "^1.15.
|
|
167
|
+
"hyperblobs": "^2.13.0",
|
|
168
|
+
"hypercore": "^11.39.3",
|
|
169
|
+
"hypercore-blob-server": "^1.15.1",
|
|
165
170
|
"hypercore-crypto": "^3.7.0",
|
|
166
171
|
"hypercore-id-encoding": "^1.3.0",
|
|
167
172
|
"hypercore-storage": "^3.3.1",
|
|
168
|
-
"hyperdb": "^6.
|
|
169
|
-
"hyperdht": "^6.34.
|
|
173
|
+
"hyperdb": "^6.11.0",
|
|
174
|
+
"hyperdht": "^6.34.1",
|
|
170
175
|
"hyperdispatch": "^1.6.0",
|
|
171
176
|
"hyperschema": "^1.26.2",
|
|
172
177
|
"hyperswarm": "^4.17.2",
|
|
@@ -183,13 +188,15 @@
|
|
|
183
188
|
"@hyperswarm/testnet": "^3.1.4",
|
|
184
189
|
"bare-abort-controller": "^1.1.2",
|
|
185
190
|
"bare-events": "^2.9.2",
|
|
186
|
-
"bare-fetch": "^3.4.
|
|
191
|
+
"bare-fetch": "^3.4.1",
|
|
187
192
|
"bare-process": "^4.5.1",
|
|
188
|
-
"bare-subprocess": "^6.2.
|
|
189
|
-
"bare-url": "^2.5.
|
|
190
|
-
"blind-peer": "^3.
|
|
191
|
-
"
|
|
192
|
-
"
|
|
193
|
+
"bare-subprocess": "^6.2.2",
|
|
194
|
+
"bare-url": "^2.5.5",
|
|
195
|
+
"blind-peer": "^3.16.0",
|
|
196
|
+
"blind-push-gateway": "^1.0.0",
|
|
197
|
+
"brittle": "^4.5.0",
|
|
198
|
+
"protomux": "^3.13.0",
|
|
199
|
+
"protomux-rpc-router": "^1.3.0",
|
|
193
200
|
"typescript": "^7.0.2",
|
|
194
201
|
"which-runtime": "^1.4.0"
|
|
195
202
|
},
|
package/src/database/dispatch.js
CHANGED
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
} from '../lib/constants.js'
|
|
22
22
|
import {
|
|
23
23
|
can,
|
|
24
|
+
isRank,
|
|
24
25
|
grants,
|
|
25
26
|
outranks,
|
|
26
27
|
admission,
|
|
@@ -38,7 +39,6 @@ const Join = getEncoding('@cero/join')
|
|
|
38
39
|
|
|
39
40
|
// wire fields are variable-length: reject wrong sizes before sodium and hid throw on them
|
|
40
41
|
const isKey = (b) => b?.byteLength === 32
|
|
41
|
-
const isSig = (b) => b?.byteLength === 64
|
|
42
42
|
|
|
43
43
|
// member and device have their own set handlers, every other collection upserts via add-
|
|
44
44
|
export const BESPOKE = new Set(['member', 'device'])
|
|
@@ -210,9 +210,11 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
210
210
|
return hooked(fns, meta, ctx, (r) => insert(ctx.view, name, r))
|
|
211
211
|
}
|
|
212
212
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
213
|
+
// a seat taken again, as a linked device's own claim does, keeps the chain its link wrote
|
|
214
|
+
async function seat(ctx, writer, memberId, ts, key, proof = null) {
|
|
215
|
+
const id = hid.encode(writer)
|
|
216
|
+
proof ??= (await getDevice(ctx.view, id))?.proof ?? null
|
|
217
|
+
return save(ctx, 'put', 'devices', { id, memberId, createdAt: ts, updatedAt: ts, key, proof })
|
|
216
218
|
}
|
|
217
219
|
|
|
218
220
|
// deleting a row that is not there changes nothing, and fires nothing
|
|
@@ -223,6 +225,51 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
223
225
|
await hooked(existing ? fns : null, meta, ctx, () => ctx.view.delete(col(name), { id }))
|
|
224
226
|
}
|
|
225
227
|
|
|
228
|
+
// Who may remove a member, or change one's rank: by default her rank, as `ranked` says. An app's
|
|
229
|
+
// rule on members may grant what rank alone refuses, by answering 'grant' (never a bare true,
|
|
230
|
+
// which every rule answers for "no objection"), and refuse what rank allows, by answering false.
|
|
231
|
+
// Decided before anything moves, so a refusal leaves the member as she was. Resolves to what
|
|
232
|
+
// `landed` needs to fire the after hooks once the change lands
|
|
233
|
+
async function decide(ctx, op, { row = null, existing, ranked }, refusal) {
|
|
234
|
+
const fns = pick(op, ctx)
|
|
235
|
+
const ts = row?.updatedAt || existing?.updatedAt || 0
|
|
236
|
+
const hctx = {
|
|
237
|
+
op,
|
|
238
|
+
name: 'members',
|
|
239
|
+
row,
|
|
240
|
+
existing,
|
|
241
|
+
id: existing.id,
|
|
242
|
+
ranked,
|
|
243
|
+
...(await author(ctx)),
|
|
244
|
+
...operators(ctx, ts)
|
|
245
|
+
}
|
|
246
|
+
// a grant widens rank among the room's writers only: never for a reader, nor for someone who
|
|
247
|
+
// is no member at all
|
|
248
|
+
const canGrant = Boolean(hctx.memberId) && can(hctx.role, WRITE)
|
|
249
|
+
let granted = false
|
|
250
|
+
for (const fn of fns?.before ?? []) {
|
|
251
|
+
let verdict
|
|
252
|
+
try {
|
|
253
|
+
verdict = await fn(hctx)
|
|
254
|
+
} catch (err) {
|
|
255
|
+
throw CeroError.REFUSED('hook', err.message)
|
|
256
|
+
}
|
|
257
|
+
if (verdict === false) throw CeroError.REFUSED('hook')
|
|
258
|
+
if (verdict === 'grant' && canGrant) granted = true
|
|
259
|
+
}
|
|
260
|
+
if (!ranked && !granted) throw refusal()
|
|
261
|
+
return { fns, hctx }
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// a change decide judged: its before hooks already ran, its after hooks fire once it landed
|
|
265
|
+
function landed(ctx, { fns, hctx }) {
|
|
266
|
+
if (!fns) return
|
|
267
|
+
ctx.later.push(async () => {
|
|
268
|
+
Object.assign(hctx, await author(ctx))
|
|
269
|
+
await fire(fns.after, hctx, false)
|
|
270
|
+
})
|
|
271
|
+
}
|
|
272
|
+
|
|
226
273
|
// a rule that errors must not diverge peers: a throw refuses exactly like a false verdict
|
|
227
274
|
async function fire(fns, hctx, vote) {
|
|
228
275
|
for (const fn of fns) {
|
|
@@ -290,9 +337,14 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
290
337
|
for (const run of later) await run()
|
|
291
338
|
})
|
|
292
339
|
|
|
340
|
+
// a device seats only itself: the row's device key is the one its own proof ends at, and a
|
|
341
|
+
// rotation seals to it
|
|
293
342
|
add('add-writer', async (op, ctx) => {
|
|
294
|
-
if (!isKey(op.master) || !isKey(op.writer) || !
|
|
295
|
-
if (!
|
|
343
|
+
if (!isKey(op.master) || !isKey(op.writer) || !ctx.key) return
|
|
344
|
+
if (!b4a.equals(op.writer, ctx.key)) return
|
|
345
|
+
const signed = Identity.verify(op.proof, admission(ctx.dbKey, op.writer, ctx.key), op.master)
|
|
346
|
+
if (!signed) return
|
|
347
|
+
if (await revoked(ctx.view, op.proof)) throw CeroError.REFUSED('removed')
|
|
296
348
|
const { genesis } = ctx.host
|
|
297
349
|
const inviter = await getRole(ctx.view, op.master)
|
|
298
350
|
if (!genesis && !can(inviter, INVITE)) throw CeroError.REFUSED('invite')
|
|
@@ -303,7 +355,7 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
303
355
|
if (op.memberId && op.memberId !== memberId) throw CeroError.REFUSED('member')
|
|
304
356
|
if (await boundElsewhere(ctx.view, op.writer, memberId)) throw CeroError.REFUSED('member')
|
|
305
357
|
await ctx.host.addWriter(op.writer, { isIndexer: true })
|
|
306
|
-
await seat(ctx, op.writer, memberId, op.ts || 0)
|
|
358
|
+
await seat(ctx, op.writer, memberId, op.ts || 0, signed.device)
|
|
307
359
|
})
|
|
308
360
|
|
|
309
361
|
add('del-writer', async (op, ctx) => {
|
|
@@ -315,9 +367,26 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
315
367
|
}
|
|
316
368
|
}
|
|
317
369
|
await ctx.host.removeWriter(op.writer)
|
|
370
|
+
await revoke(ctx, target)
|
|
318
371
|
await drop(ctx, 'devices', hid.encode(op.writer))
|
|
319
372
|
})
|
|
320
373
|
|
|
374
|
+
// a removed device's key, and any device it attests after, stays out: its chain is static, so
|
|
375
|
+
// the removal is kept here, beside the members'
|
|
376
|
+
async function revoke(ctx, device) {
|
|
377
|
+
if (!device?.key) return
|
|
378
|
+
const minted = (await ctx.view.get(countersCol, { name: 'invites' }))?.value ?? 0
|
|
379
|
+
await ctx.view.insert(removals, { id: hid.encode(device.key), index: minted })
|
|
380
|
+
}
|
|
381
|
+
// the devices of the chain, not the identity it starts with: a member's removal is ordered by
|
|
382
|
+
// its invites, a device's is for good
|
|
383
|
+
async function revoked(view, proof) {
|
|
384
|
+
for (const key of Identity.chain(proof).slice(1)) {
|
|
385
|
+
if (await view.get(removals, { id: hid.encode(key) })) return true
|
|
386
|
+
}
|
|
387
|
+
return false
|
|
388
|
+
}
|
|
389
|
+
|
|
321
390
|
// the genesis batch names the creator; everyone after comes in through a signed join
|
|
322
391
|
add('add-member', async (op, ctx) => {
|
|
323
392
|
if (!isKey(op.key)) return
|
|
@@ -341,14 +410,28 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
341
410
|
next.createdAt = existing.createdAt
|
|
342
411
|
next.updatedAt = op.updatedAt || existing.updatedAt
|
|
343
412
|
|
|
413
|
+
let row
|
|
344
414
|
if (op.role && op.role !== existing.role) {
|
|
345
|
-
|
|
346
|
-
if (!(
|
|
347
|
-
|
|
415
|
+
// a rank cero has no name for is no rank, granted or not
|
|
416
|
+
if (!isRank(op.role)) throw CeroError.INVALID(`'${op.role}' is not a role`)
|
|
417
|
+
// the last owner keeps her rank, whatever a rule grants, as she is never removed
|
|
418
|
+
if (existing.role === OWNER && (await orphans(ctx.view, op.id))) {
|
|
419
|
+
throw CeroError.INVALID(
|
|
420
|
+
'the last owner cannot be demoted: make another member an owner first'
|
|
421
|
+
)
|
|
348
422
|
}
|
|
423
|
+
const r = await getSignerRole(ctx.view, ctx.key)
|
|
349
424
|
next.role = op.role
|
|
425
|
+
const ranked = can(r, ASSIGN) && grants(r, op.role) && outranks(r, existing.role)
|
|
426
|
+
const decided = await decide(ctx, 'set', { row: next, existing, ranked }, () =>
|
|
427
|
+
CeroError.REFUSED('assign')
|
|
428
|
+
)
|
|
429
|
+
row = decided.hctx.row
|
|
430
|
+
await insert(ctx.view, 'members', row)
|
|
431
|
+
landed(ctx, decided)
|
|
432
|
+
} else {
|
|
433
|
+
row = await save(ctx, 'set', 'members', next)
|
|
350
434
|
}
|
|
351
|
-
const row = await save(ctx, 'set', 'members', next)
|
|
352
435
|
// WRITE decides the writer seats: a demotion takes them, a promotion gives them back.
|
|
353
436
|
// Device rows stay: an unresolvable writer would read as not yet enrolled and pass the gate
|
|
354
437
|
const writes = can(row.role, WRITE)
|
|
@@ -363,9 +446,17 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
363
446
|
add('del-member', async (op, ctx) => {
|
|
364
447
|
const existing = await getMember(ctx.view, op.id)
|
|
365
448
|
if (!existing) return
|
|
449
|
+
let decided = null
|
|
366
450
|
if (op.id !== (await getSignerMember(ctx.view, ctx.key))) {
|
|
451
|
+
// the last owner stays, whatever a rule grants: without her nobody outranks the rest
|
|
452
|
+
if (existing.role === OWNER && (await orphans(ctx.view, op.id))) {
|
|
453
|
+
throw CeroError.INVALID(
|
|
454
|
+
'the last owner cannot be removed: make another member an owner first'
|
|
455
|
+
)
|
|
456
|
+
}
|
|
367
457
|
const r = await getSignerRole(ctx.view, ctx.key)
|
|
368
|
-
|
|
458
|
+
const ranked = can(r, REMOVE) && outranks(r, existing.role)
|
|
459
|
+
decided = await decide(ctx, 'del', { existing, ranked }, () => CeroError.REFUSED('remove'))
|
|
369
460
|
} else if (existing.role === OWNER && (await orphans(ctx.view, op.id))) {
|
|
370
461
|
throw CeroError.INVALID('the last owner cannot leave: make another member an owner first')
|
|
371
462
|
}
|
|
@@ -377,7 +468,12 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
377
468
|
await drop(ctx, 'devices', device.id)
|
|
378
469
|
await drop(ctx, 'requests', device.id)
|
|
379
470
|
}
|
|
380
|
-
|
|
471
|
+
if (decided) {
|
|
472
|
+
await ctx.view.delete(col('members'), { id: op.id })
|
|
473
|
+
landed(ctx, decided)
|
|
474
|
+
} else {
|
|
475
|
+
await drop(ctx, 'members', op.id)
|
|
476
|
+
}
|
|
381
477
|
// an invite they could hold, or saw, does not bring them back
|
|
382
478
|
const minted = (await ctx.view.get(countersCol, { name: 'invites' }))?.value ?? 0
|
|
383
479
|
await ctx.view.insert(removals, { id: op.id, index: minted })
|
|
@@ -404,6 +500,7 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
404
500
|
commit: op.commit
|
|
405
501
|
}
|
|
406
502
|
await ctx.view.insert(`@${ns}/${EPOCHS}`, row)
|
|
503
|
+
touch(EPOCHS)
|
|
407
504
|
// never in a dry run: the announcement itself is appended under the old epoch
|
|
408
505
|
if (!ctx.dryRun) {
|
|
409
506
|
try {
|
|
@@ -423,6 +520,7 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
423
520
|
}
|
|
424
521
|
}
|
|
425
522
|
await ctx.host.removeWriter(hid.decode(existing.id))
|
|
523
|
+
await revoke(ctx, existing)
|
|
426
524
|
await drop(ctx, 'devices', op.id)
|
|
427
525
|
})
|
|
428
526
|
|
|
@@ -437,9 +535,11 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
437
535
|
const selfOnly = (op, ctx) => {
|
|
438
536
|
if (!ctx.key || op.id !== hid.encode(ctx.key)) throw CeroError.REFUSED('device')
|
|
439
537
|
}
|
|
538
|
+
// the device key stays what the admission proved, never what the row says
|
|
440
539
|
add('add-device', async (op, ctx) => {
|
|
441
540
|
selfOnly(op, ctx)
|
|
442
|
-
|
|
541
|
+
const existing = await getDevice(ctx.view, op.id)
|
|
542
|
+
await save(ctx, 'put', 'devices', { ...op, memberId: await bind(ctx), key: existing?.key })
|
|
443
543
|
})
|
|
444
544
|
add('set-device', async (op, ctx) => {
|
|
445
545
|
selfOnly(op, ctx)
|
|
@@ -449,6 +549,7 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
449
549
|
await save(ctx, 'set', 'devices', {
|
|
450
550
|
...op,
|
|
451
551
|
memberId,
|
|
552
|
+
key: existing?.key,
|
|
452
553
|
createdAt: existing?.createdAt || op.createdAt || ts,
|
|
453
554
|
updatedAt: ts
|
|
454
555
|
})
|
|
@@ -508,7 +609,7 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
508
609
|
|
|
509
610
|
// the member, when new, and its device, seated when the member writes; its request stays until
|
|
510
611
|
// a member delivered the keys. The invite is spent unless it is reusable
|
|
511
|
-
async function admit(ctx, { invite, identity, writer, reply, role, ts }) {
|
|
612
|
+
async function admit(ctx, { invite, identity, key, writer, reply, role, ts }) {
|
|
512
613
|
const memberId = hid.encode(identity)
|
|
513
614
|
if (await boundElsewhere(ctx.view, writer, memberId)) throw CeroError.REFUSED('member')
|
|
514
615
|
const member =
|
|
@@ -521,19 +622,35 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
521
622
|
updatedAt: ts
|
|
522
623
|
}))
|
|
523
624
|
if (can(member.role, WRITE)) await ctx.host.addWriter(writer, { isIndexer: true })
|
|
524
|
-
await seat(ctx, writer, memberId, ts)
|
|
625
|
+
await seat(ctx, writer, memberId, ts, key)
|
|
525
626
|
await save(ctx, 'put', 'requests', {
|
|
526
|
-
...request(invite, identity, writer, reply, ts),
|
|
627
|
+
...request(invite, identity, key, writer, reply, ts),
|
|
527
628
|
role,
|
|
528
629
|
admitted: true
|
|
529
630
|
})
|
|
530
631
|
if (!invite.reuse) await spend(ctx, invite, 'spent')
|
|
531
632
|
}
|
|
532
633
|
|
|
533
|
-
|
|
634
|
+
// a device of a member, linked by one of its devices: no member row, a seat under theirs with
|
|
635
|
+
// the chain the linking device wrote
|
|
636
|
+
async function admitDevice(ctx, { invite, memberId, key, proof, writer, reply, ts }) {
|
|
637
|
+
if (await boundElsewhere(ctx.view, writer, memberId)) throw CeroError.REFUSED('member')
|
|
638
|
+
const member = await getMember(ctx.view, memberId)
|
|
639
|
+
if (can(member.role, WRITE)) await ctx.host.addWriter(writer, { isIndexer: true })
|
|
640
|
+
await seat(ctx, writer, memberId, ts, key, proof)
|
|
641
|
+
await save(ctx, 'put', 'requests', {
|
|
642
|
+
...request(invite, hid.decode(memberId), key, writer, reply, ts),
|
|
643
|
+
role: member.role,
|
|
644
|
+
admitted: true
|
|
645
|
+
})
|
|
646
|
+
if (!invite.reuse) await spend(ctx, invite, 'spent')
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function request(invite, identity, key, writer, reply, ts) {
|
|
534
650
|
const expires = invite.expires || 0
|
|
535
651
|
const id = hid.encode(writer)
|
|
536
|
-
|
|
652
|
+
const { role } = invite
|
|
653
|
+
return { id, identity, key, invite: invite.id, role, reply, expires, createdAt: ts }
|
|
537
654
|
}
|
|
538
655
|
|
|
539
656
|
// null when it does not open: anyone may append a join
|
|
@@ -548,13 +665,15 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
548
665
|
}
|
|
549
666
|
}
|
|
550
667
|
|
|
551
|
-
// the joiner's own core asks in: the invite proves it may, its
|
|
668
|
+
// the joiner's own core asks in: the invite proves it may, its device signs where the keys go
|
|
669
|
+
// and the proof's chain says whose device it is
|
|
552
670
|
add('join', async (op, ctx) => {
|
|
553
671
|
const join = open(op.box)
|
|
554
672
|
if (!join || !ctx.key) return
|
|
555
|
-
if (!Invite.proven(join.invite, ctx.key, join.
|
|
556
|
-
const signed = joining(ctx.dbKey, join.invite, ctx.key, join.reply)
|
|
557
|
-
if (!
|
|
673
|
+
if (!Invite.proven(join.invite, ctx.key, join.signature)) return
|
|
674
|
+
const signed = Identity.verify(join.proof, joining(ctx.dbKey, join.invite, ctx.key, join.reply))
|
|
675
|
+
if (!signed) return
|
|
676
|
+
join.identity = signed.identity
|
|
558
677
|
const id = hid.encode(ctx.key)
|
|
559
678
|
const invite = await ctx.view.get(invites, { id: b4a.toHex(join.invite) })
|
|
560
679
|
if (!invite) {
|
|
@@ -567,24 +686,26 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
567
686
|
const memberId = hid.encode(join.identity)
|
|
568
687
|
const known = await getMember(ctx.view, memberId)
|
|
569
688
|
const removal = known ? null : await ctx.view.get(removals, { id: memberId })
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
}
|
|
689
|
+
const out =
|
|
690
|
+
(removal && (invite.index ?? 0) <= removal.index) || (await revoked(ctx.view, join.proof))
|
|
691
|
+
if (out) return refuse(ctx, { id, reply: join.reply, expires: invite.expires || 0 }, 'removed')
|
|
573
692
|
await ctx.view.delete(refusals, { id })
|
|
574
693
|
if (invite.confirm && !known) {
|
|
575
|
-
const waiting = request(invite, join.identity, ctx.key, join.reply, ts)
|
|
694
|
+
const waiting = request(invite, join.identity, signed.device, ctx.key, join.reply, ts)
|
|
576
695
|
return save(ctx, 'put', 'requests', waiting)
|
|
577
696
|
}
|
|
578
697
|
await admit(ctx, {
|
|
579
698
|
invite,
|
|
580
699
|
identity: join.identity,
|
|
700
|
+
key: signed.device,
|
|
581
701
|
writer: ctx.key,
|
|
582
702
|
reply: join.reply,
|
|
583
703
|
role: invite.role,
|
|
584
704
|
ts
|
|
585
705
|
})
|
|
586
706
|
})
|
|
587
|
-
// a waiting join, admitted at a role within the invite's and the accepter's
|
|
707
|
+
// a waiting join, admitted at a role within the invite's and the accepter's; with a proof, as a
|
|
708
|
+
// device of the accepter's own identity, which its device attested
|
|
588
709
|
add('accept', async (op, ctx) => {
|
|
589
710
|
const waiting = await ctx.view.get(requests, { id: op.id })
|
|
590
711
|
if (!waiting || waiting.admitted) return
|
|
@@ -594,8 +715,16 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
|
|
|
594
715
|
if (!invite || !can(r, INVITE) || !grants(r, role) || !grants(invite.role, role)) {
|
|
595
716
|
throw CeroError.REFUSED('invite')
|
|
596
717
|
}
|
|
597
|
-
const { identity, reply, createdAt: ts = 0 } = waiting
|
|
598
|
-
|
|
718
|
+
const { identity, key, reply, createdAt: ts = 0 } = waiting
|
|
719
|
+
const writer = hid.decode(op.id)
|
|
720
|
+
if (op.proof) {
|
|
721
|
+
const memberId = await getSignerMember(ctx.view, ctx.key)
|
|
722
|
+
const linked = Identity.verify(op.proof, null, hid.decode(memberId))
|
|
723
|
+
if (!linked || !b4a.equals(linked.device, key)) throw CeroError.REFUSED('device')
|
|
724
|
+
if (await revoked(ctx.view, op.proof)) throw CeroError.REFUSED('removed')
|
|
725
|
+
return admitDevice(ctx, { invite, memberId, key, proof: op.proof, writer, reply, ts })
|
|
726
|
+
}
|
|
727
|
+
await admit(ctx, { invite, identity, key, writer, reply, role, ts })
|
|
599
728
|
})
|
|
600
729
|
// answered through the log like every refusal; a deny that lost the race to an accept is nothing
|
|
601
730
|
add('deny', async (op, ctx) => {
|
|
@@ -63,25 +63,26 @@ export async function loadEpochs(keyring, local) {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
/**
|
|
66
|
-
*
|
|
67
|
-
*
|
|
66
|
+
* A rotation announcement's envelope list — one box per device row, sealed to its device key and
|
|
67
|
+
* addressed by its writer id.
|
|
68
68
|
*
|
|
69
|
-
* @param {Array<{ id: string }>}
|
|
69
|
+
* @param {Array<{ id: string, key: Uint8Array }>} devices
|
|
70
70
|
* @param {Uint8Array} secret
|
|
71
71
|
* @returns {Array<{ id: string, box: Uint8Array }>}
|
|
72
72
|
*/
|
|
73
|
-
export function seal(
|
|
74
|
-
return
|
|
73
|
+
export function seal(devices, secret) {
|
|
74
|
+
return devices.map((d) => ({ id: d.id, box: Identity.seal(d.key, secret) }))
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
/**
|
|
78
|
-
* @param {import('../identity/index.js').Identity} identity
|
|
78
|
+
* @param {import('../identity/index.js').Identity} identity Whose device key opens the box.
|
|
79
|
+
* @param {string} id This device's writer id in the database.
|
|
79
80
|
* @param {Uint8Array} wrapped
|
|
80
81
|
* @returns {Generator<Uint8Array, void, unknown>}
|
|
81
82
|
*/
|
|
82
|
-
export function* opened(identity, wrapped) {
|
|
83
|
+
export function* opened(identity, id, wrapped) {
|
|
83
84
|
for (const w of c.decode(wraps, wrapped)) {
|
|
84
|
-
if (w.id !==
|
|
85
|
+
if (w.id !== id) continue
|
|
85
86
|
const secret = identity.unseal(w.box)
|
|
86
87
|
if (secret) yield secret
|
|
87
88
|
}
|
package/src/database/index.js
CHANGED
|
@@ -47,6 +47,7 @@ import { keyPair } from '../mailbox/inbox.js'
|
|
|
47
47
|
* @property {Array<{ epoch: number, entropy: Uint8Array }> | null} [epochs] Rotation epochs to prime the keyring with (delivered at join).
|
|
48
48
|
* @property {Uint8Array | null} [key] Existing autobee key to reopen.
|
|
49
49
|
* @property {boolean} [pinned] Always search and announce, outside the network's presence budget. The root.
|
|
50
|
+
* @property {boolean} [push] Push a notification through the mirrors after a local write to a ref whose meta declares `push`, naming the ref, so members learn of it while their app is dead.
|
|
50
51
|
* @property {import('../identity/index.js').KeyPair} [keyPair] This device's writer keypair; a fresh random one by default. Never the identity's, never the database key.
|
|
51
52
|
* @property {(err: Error) => void} [onerror] Called when a node is skipped or refused, or the bee errors.
|
|
52
53
|
*
|
|
@@ -67,6 +68,7 @@ import { keyPair } from '../mailbox/inbox.js'
|
|
|
67
68
|
* @property {string | null} id The row id for a `del`.
|
|
68
69
|
* @property {string | null} memberId The writer's member id, null for a core no member owns yet (a join, a claim, genesis).
|
|
69
70
|
* @property {string | null} role The writer's role, null likewise.
|
|
71
|
+
* @property {boolean} [ranked] On removing a member or changing her rank: whether the writer's rank alone allows it. A rule answers `'grant'` to allow what rank refuses, `false` to refuse.
|
|
70
72
|
* @property {(ref: string | { name: string }, query?: string | Record<string, unknown>) => Promise<{ data: unknown }>} get
|
|
71
73
|
* @property {(ref: string | { name: string }, row: Record<string, unknown>) => Promise<void>} put
|
|
72
74
|
* @property {(ref: string | { name: string }, row: Record<string, unknown>) => Promise<void>} set
|
|
@@ -113,9 +115,12 @@ export class Database extends ReadyResource {
|
|
|
113
115
|
this.rotation = new Rotation(this)
|
|
114
116
|
this.key = opts.key || null
|
|
115
117
|
this.pinned = opts.pinned === true
|
|
118
|
+
this.push = opts.push === true
|
|
119
|
+
/** @private */
|
|
120
|
+
this._pushes = pushes(this.refs)
|
|
116
121
|
this.keyPair = opts.keyPair || Identity.randomKeyPair()
|
|
117
|
-
if (b4a.equals(this.keyPair.publicKey, this.identity.publicKey)) {
|
|
118
|
-
throw CeroError.INVALID('the
|
|
122
|
+
if (b4a.equals(this.keyPair.publicKey, this.identity.device.publicKey)) {
|
|
123
|
+
throw CeroError.INVALID('the device keypair is never a writer — mint one')
|
|
119
124
|
}
|
|
120
125
|
|
|
121
126
|
/** @private */
|
|
@@ -128,6 +133,20 @@ export class Database extends ReadyResource {
|
|
|
128
133
|
/** @private */
|
|
129
134
|
this._presence = null
|
|
130
135
|
/** @private */
|
|
136
|
+
this._pushing = null
|
|
137
|
+
/** @private */
|
|
138
|
+
this._pushAgain = false
|
|
139
|
+
/** @private */
|
|
140
|
+
this._pushed = -1
|
|
141
|
+
/** @private */
|
|
142
|
+
this._pushRef = null
|
|
143
|
+
/** @private */
|
|
144
|
+
this._onResume = () => {
|
|
145
|
+
if (!this._pushAgain) return
|
|
146
|
+
this._pushAgain = false
|
|
147
|
+
this._push(this._pushRef)
|
|
148
|
+
}
|
|
149
|
+
/** @private */
|
|
131
150
|
this._txChain = null
|
|
132
151
|
/** @private */
|
|
133
152
|
this._held = new Map()
|
|
@@ -228,6 +247,7 @@ export class Database extends ReadyResource {
|
|
|
228
247
|
/** @private */
|
|
229
248
|
async _close() {
|
|
230
249
|
this.rotation.close()
|
|
250
|
+
this.network?.off('resume', this._onResume)
|
|
231
251
|
if (this._presence) {
|
|
232
252
|
this._presence.remove()
|
|
233
253
|
this._presence = null
|
|
@@ -420,6 +440,69 @@ export class Database extends ReadyResource {
|
|
|
420
440
|
if (this.closing || this.closed) throw CeroError.CLOSED('Database')
|
|
421
441
|
throw err
|
|
422
442
|
}
|
|
443
|
+
const ref = this.push ? this._ref(ops) : null
|
|
444
|
+
if (ref) this._push(ref)
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/** @returns {boolean} Whether a push notification is on its way to the mirrors. */
|
|
448
|
+
get pushing() {
|
|
449
|
+
return this._pushing !== null
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// one in flight, then once more at the latest block; one due while the network sleeps goes at resume
|
|
453
|
+
/** @private */
|
|
454
|
+
_push(ref) {
|
|
455
|
+
this._pushRef = ref
|
|
456
|
+
if (!this.network?.mirrors.length || this.closing) return
|
|
457
|
+
if (this._pushing || this.network.suspended) {
|
|
458
|
+
this._pushAgain = true
|
|
459
|
+
return
|
|
460
|
+
}
|
|
461
|
+
this._pushing = this._send().finally(() => {
|
|
462
|
+
this._pushing = null
|
|
463
|
+
if (!this._pushAgain) return
|
|
464
|
+
this._pushAgain = false
|
|
465
|
+
this._push(this._pushRef)
|
|
466
|
+
})
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// the first op on a ref that declares push names the notification; a burst keeps the latest
|
|
470
|
+
/** @private */
|
|
471
|
+
_ref(ops) {
|
|
472
|
+
for (const [op] of ops) {
|
|
473
|
+
const name = this._pushes.get(op.replace(/^(add|set|del)-/, ''))
|
|
474
|
+
if (name) return name
|
|
475
|
+
}
|
|
476
|
+
return null
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// a fresh writer core reaches the mirror a few seconds after its first block: one more try
|
|
480
|
+
/** @private */
|
|
481
|
+
async _send() {
|
|
482
|
+
for (let tries = 0; ; tries++) {
|
|
483
|
+
if (this.closing || !this.bee) return
|
|
484
|
+
if (this.network.suspended) {
|
|
485
|
+
this._pushAgain = true
|
|
486
|
+
return
|
|
487
|
+
}
|
|
488
|
+
const core = this.bee.local
|
|
489
|
+
const index = core.length - 1
|
|
490
|
+
if (index === this._pushed) return
|
|
491
|
+
try {
|
|
492
|
+
await this.network.peering().sendNotification(core, {
|
|
493
|
+
roomKey: this.key,
|
|
494
|
+
index,
|
|
495
|
+
target: this.key,
|
|
496
|
+
extra: b4a.from(this._pushRef)
|
|
497
|
+
})
|
|
498
|
+
this._pushed = index
|
|
499
|
+
return
|
|
500
|
+
} catch (err) {
|
|
501
|
+
safetyCatch(err)
|
|
502
|
+
if (tries) return
|
|
503
|
+
await new Promise((resolve) => setTimeout(resolve, PUSH_RETRY).unref())
|
|
504
|
+
}
|
|
505
|
+
}
|
|
423
506
|
}
|
|
424
507
|
|
|
425
508
|
/**
|
|
@@ -573,13 +656,25 @@ export class Database extends ReadyResource {
|
|
|
573
656
|
}
|
|
574
657
|
|
|
575
658
|
/**
|
|
576
|
-
*
|
|
659
|
+
* Learn epochs handed over after the open, as a root carries them for a handle. One is kept
|
|
660
|
+
* once its announcement applied here and the secret meets the commitment it carries: hand them
|
|
661
|
+
* over again when an announcement applies.
|
|
577
662
|
*
|
|
578
|
-
* @param {Uint8Array}
|
|
663
|
+
* @param {Array<{ epoch: number, stamp: number, entropy: Uint8Array }>} epochs
|
|
579
664
|
* @returns {Promise<void>}
|
|
580
665
|
*/
|
|
581
|
-
async
|
|
582
|
-
|
|
666
|
+
async learn(epochs) {
|
|
667
|
+
this.guard()
|
|
668
|
+
const rows = await this.rotation._epochs()
|
|
669
|
+
let added = false
|
|
670
|
+
for (const e of epochs) {
|
|
671
|
+
if (this.keyring.entropy(e.stamp)) continue
|
|
672
|
+
const row = rows.find((r) => r.stamp === e.stamp)
|
|
673
|
+
if (!row?.commit || !b4a.equals(crypto.hash(e.entropy), row.commit)) continue
|
|
674
|
+
this.keyring.add(e.stamp, e.entropy, e.epoch)
|
|
675
|
+
added = true
|
|
676
|
+
}
|
|
677
|
+
if (added) await this.rotation._save()
|
|
583
678
|
}
|
|
584
679
|
|
|
585
680
|
/**
|
|
@@ -737,6 +832,7 @@ export class Database extends ReadyResource {
|
|
|
737
832
|
/** @private */
|
|
738
833
|
_joinSwarm(bee, discoveryKey) {
|
|
739
834
|
this.network.attach(bee)
|
|
835
|
+
if (this.push && !this._presence) this.network.on('resume', this._onResume)
|
|
740
836
|
this._presence ??= this.network.presence.add(discoveryKey, { pinned: this.pinned })
|
|
741
837
|
}
|
|
742
838
|
|
|
@@ -758,7 +854,12 @@ export class Database extends ReadyResource {
|
|
|
758
854
|
const id = b4a.toHex(key)
|
|
759
855
|
const held = this._held.get(id)
|
|
760
856
|
if (this.closing || (held && (held.end === -1 || end <= held.end))) return
|
|
761
|
-
|
|
857
|
+
// another device's writer is held in the bee's wakeup group, as autobee opens its writers: once autobee lets go of
|
|
858
|
+
// it, a block landing on it still wakes the bee. A view stays out, or autobee would take it for a writer, and so
|
|
859
|
+
// does this device's own writer, as autobee keeps it
|
|
860
|
+
const other = end !== -1 && !b4a.equals(key, this.bee.local.key)
|
|
861
|
+
const group = other ? this.bee.wakeupCapability?.discoveryKey || null : null
|
|
862
|
+
const core = held ? held.core : this.store.get({ key, active: false, group })
|
|
762
863
|
held?.range.destroy()
|
|
763
864
|
this._held.set(id, { core, end, range: core.download({ start: 0, end }) })
|
|
764
865
|
}
|
|
@@ -970,11 +1071,11 @@ export class Database extends ReadyResource {
|
|
|
970
1071
|
})
|
|
971
1072
|
}
|
|
972
1073
|
|
|
973
|
-
// the add-writer row for `writer`, appended by this device and
|
|
1074
|
+
// the add-writer row for `writer`, appended by this device and proven by its device key
|
|
974
1075
|
/** @private */
|
|
975
1076
|
_admission(writer, ts = Date.now()) {
|
|
976
|
-
const
|
|
977
|
-
return { master: this.identity.publicKey, writer,
|
|
1077
|
+
const proof = this.identity.sign(admission(this.key, writer, this.writerKey))
|
|
1078
|
+
return { master: this.identity.publicKey, writer, proof, ts }
|
|
978
1079
|
}
|
|
979
1080
|
|
|
980
1081
|
/** @private */
|
|
@@ -995,6 +1096,14 @@ function bounded(promise, ms = 2000) {
|
|
|
995
1096
|
}
|
|
996
1097
|
|
|
997
1098
|
const NO_HOOKS = []
|
|
1099
|
+
const PUSH_RETRY = 5000
|
|
1100
|
+
|
|
1101
|
+
// the refs whose writes push, by the verb their ops carry
|
|
1102
|
+
function pushes(refs) {
|
|
1103
|
+
const map = new Map()
|
|
1104
|
+
for (const [name, ref] of Object.entries(refs)) if (ref.push) map.set(ref.verb || name, name)
|
|
1105
|
+
return map
|
|
1106
|
+
}
|
|
998
1107
|
|
|
999
1108
|
function addHook(map, op, fn) {
|
|
1000
1109
|
if (typeof fn !== 'function') throw CeroError.INVALID('hook fn must be a function')
|