@cero-base/core 2.5.0 → 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 +1 -1
- package/src/database/dispatch.js +159 -30
- package/src/database/encryption.js +9 -8
- package/src/database/index.js +53 -17
- 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 +4 -11
- package/src/pairing/index.js +18 -10
- package/src/push/index.js +4 -3
- package/types/database/encryption.d.ts +8 -6
- package/types/database/index.d.ts +22 -5
- 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 +3 -2
package/package.json
CHANGED
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,7 +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
|
|
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.
|
|
51
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.
|
|
52
52
|
* @property {(err: Error) => void} [onerror] Called when a node is skipped or refused, or the bee errors.
|
|
53
53
|
*
|
|
@@ -68,6 +68,7 @@ import { keyPair } from '../mailbox/inbox.js'
|
|
|
68
68
|
* @property {string | null} id The row id for a `del`.
|
|
69
69
|
* @property {string | null} memberId The writer's member id, null for a core no member owns yet (a join, a claim, genesis).
|
|
70
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.
|
|
71
72
|
* @property {(ref: string | { name: string }, query?: string | Record<string, unknown>) => Promise<{ data: unknown }>} get
|
|
72
73
|
* @property {(ref: string | { name: string }, row: Record<string, unknown>) => Promise<void>} put
|
|
73
74
|
* @property {(ref: string | { name: string }, row: Record<string, unknown>) => Promise<void>} set
|
|
@@ -115,9 +116,11 @@ export class Database extends ReadyResource {
|
|
|
115
116
|
this.key = opts.key || null
|
|
116
117
|
this.pinned = opts.pinned === true
|
|
117
118
|
this.push = opts.push === true
|
|
119
|
+
/** @private */
|
|
120
|
+
this._pushes = pushes(this.refs)
|
|
118
121
|
this.keyPair = opts.keyPair || Identity.randomKeyPair()
|
|
119
|
-
if (b4a.equals(this.keyPair.publicKey, this.identity.publicKey)) {
|
|
120
|
-
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')
|
|
121
124
|
}
|
|
122
125
|
|
|
123
126
|
/** @private */
|
|
@@ -136,10 +139,12 @@ export class Database extends ReadyResource {
|
|
|
136
139
|
/** @private */
|
|
137
140
|
this._pushed = -1
|
|
138
141
|
/** @private */
|
|
142
|
+
this._pushRef = null
|
|
143
|
+
/** @private */
|
|
139
144
|
this._onResume = () => {
|
|
140
145
|
if (!this._pushAgain) return
|
|
141
146
|
this._pushAgain = false
|
|
142
|
-
this._push()
|
|
147
|
+
this._push(this._pushRef)
|
|
143
148
|
}
|
|
144
149
|
/** @private */
|
|
145
150
|
this._txChain = null
|
|
@@ -435,7 +440,8 @@ export class Database extends ReadyResource {
|
|
|
435
440
|
if (this.closing || this.closed) throw CeroError.CLOSED('Database')
|
|
436
441
|
throw err
|
|
437
442
|
}
|
|
438
|
-
|
|
443
|
+
const ref = this.push ? this._ref(ops) : null
|
|
444
|
+
if (ref) this._push(ref)
|
|
439
445
|
}
|
|
440
446
|
|
|
441
447
|
/** @returns {boolean} Whether a push notification is on its way to the mirrors. */
|
|
@@ -443,10 +449,10 @@ export class Database extends ReadyResource {
|
|
|
443
449
|
return this._pushing !== null
|
|
444
450
|
}
|
|
445
451
|
|
|
446
|
-
// one in flight, then once more at the latest block
|
|
447
|
-
// one due while the network sleeps goes at resume
|
|
452
|
+
// one in flight, then once more at the latest block; one due while the network sleeps goes at resume
|
|
448
453
|
/** @private */
|
|
449
|
-
_push() {
|
|
454
|
+
_push(ref) {
|
|
455
|
+
this._pushRef = ref
|
|
450
456
|
if (!this.network?.mirrors.length || this.closing) return
|
|
451
457
|
if (this._pushing || this.network.suspended) {
|
|
452
458
|
this._pushAgain = true
|
|
@@ -456,10 +462,20 @@ export class Database extends ReadyResource {
|
|
|
456
462
|
this._pushing = null
|
|
457
463
|
if (!this._pushAgain) return
|
|
458
464
|
this._pushAgain = false
|
|
459
|
-
this._push()
|
|
465
|
+
this._push(this._pushRef)
|
|
460
466
|
})
|
|
461
467
|
}
|
|
462
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
|
+
|
|
463
479
|
// a fresh writer core reaches the mirror a few seconds after its first block: one more try
|
|
464
480
|
/** @private */
|
|
465
481
|
async _send() {
|
|
@@ -476,7 +492,8 @@ export class Database extends ReadyResource {
|
|
|
476
492
|
await this.network.peering().sendNotification(core, {
|
|
477
493
|
roomKey: this.key,
|
|
478
494
|
index,
|
|
479
|
-
target: this.key
|
|
495
|
+
target: this.key,
|
|
496
|
+
extra: b4a.from(this._pushRef)
|
|
480
497
|
})
|
|
481
498
|
this._pushed = index
|
|
482
499
|
return
|
|
@@ -639,13 +656,25 @@ export class Database extends ReadyResource {
|
|
|
639
656
|
}
|
|
640
657
|
|
|
641
658
|
/**
|
|
642
|
-
*
|
|
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.
|
|
643
662
|
*
|
|
644
|
-
* @param {Uint8Array}
|
|
663
|
+
* @param {Array<{ epoch: number, stamp: number, entropy: Uint8Array }>} epochs
|
|
645
664
|
* @returns {Promise<void>}
|
|
646
665
|
*/
|
|
647
|
-
async
|
|
648
|
-
|
|
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()
|
|
649
678
|
}
|
|
650
679
|
|
|
651
680
|
/**
|
|
@@ -1042,11 +1071,11 @@ export class Database extends ReadyResource {
|
|
|
1042
1071
|
})
|
|
1043
1072
|
}
|
|
1044
1073
|
|
|
1045
|
-
// 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
|
|
1046
1075
|
/** @private */
|
|
1047
1076
|
_admission(writer, ts = Date.now()) {
|
|
1048
|
-
const
|
|
1049
|
-
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 }
|
|
1050
1079
|
}
|
|
1051
1080
|
|
|
1052
1081
|
/** @private */
|
|
@@ -1069,6 +1098,13 @@ function bounded(promise, ms = 2000) {
|
|
|
1069
1098
|
const NO_HOOKS = []
|
|
1070
1099
|
const PUSH_RETRY = 5000
|
|
1071
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
|
+
}
|
|
1107
|
+
|
|
1072
1108
|
function addHook(map, op, fn) {
|
|
1073
1109
|
if (typeof fn !== 'function') throw CeroError.INVALID('hook fn must be a function')
|
|
1074
1110
|
let arr = map.get(op)
|
package/src/database/rotation.js
CHANGED
|
@@ -10,7 +10,7 @@ import { Identity } from '../identity/index.js'
|
|
|
10
10
|
import { CeroError } from '../lib/errors.js'
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* Epoch key rotation for a database: mint a secret, seal it to every
|
|
13
|
+
* Epoch key rotation for a database: mint a secret, seal it to every device, announce it
|
|
14
14
|
* through the log, and on every peer learn the secrets addressed to it.
|
|
15
15
|
*/
|
|
16
16
|
export class Rotation {
|
|
@@ -60,6 +60,7 @@ export class Rotation {
|
|
|
60
60
|
keyring.add(row.stamp, entropy, row.epoch)
|
|
61
61
|
try {
|
|
62
62
|
await this._save()
|
|
63
|
+
this.db.emit('epoch')
|
|
63
64
|
} catch (err) {
|
|
64
65
|
// an epoch we cannot reload after a restart must not be used for writes
|
|
65
66
|
keyring.remove(row.stamp)
|
|
@@ -107,7 +108,7 @@ export class Rotation {
|
|
|
107
108
|
const stamp = this._mint()
|
|
108
109
|
this.current = { entropy, stamp, epoch: 0 }
|
|
109
110
|
try {
|
|
110
|
-
const wrapped = seal(await this.
|
|
111
|
+
const wrapped = seal(await this._devices(), entropy)
|
|
111
112
|
const epoch = await this._announce(stamp, entropy, wrapped)
|
|
112
113
|
if (!epoch) {
|
|
113
114
|
// a stamp collision (~2^-32, or an adversarial pre-claim) is not a refusal
|
|
@@ -120,19 +121,20 @@ export class Rotation {
|
|
|
120
121
|
}
|
|
121
122
|
db.keyring.add(stamp, entropy, epoch)
|
|
122
123
|
await this._save()
|
|
124
|
+
db.emit('epoch')
|
|
123
125
|
return { epoch }
|
|
124
126
|
} finally {
|
|
125
127
|
this.current = null
|
|
126
128
|
}
|
|
127
129
|
}
|
|
128
130
|
|
|
131
|
+
// every seated device, readers' too: a member keeps reading on each of its devices
|
|
129
132
|
/** @private */
|
|
130
|
-
async
|
|
131
|
-
const { data:
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
return members
|
|
133
|
+
async _devices() {
|
|
134
|
+
const { data: devices } = await this.db.get('devices')
|
|
135
|
+
const keyed = devices.filter((d) => d.key)
|
|
136
|
+
if (keyed.length === 0) throw CeroError.INVALID('cannot rotate a database with no devices')
|
|
137
|
+
return keyed
|
|
136
138
|
}
|
|
137
139
|
|
|
138
140
|
// resolves the sequence apply assigned, 0 if it never applied
|
|
@@ -179,7 +181,8 @@ export class Rotation {
|
|
|
179
181
|
// every envelope addressed to us is tried, a bad one must not lock us out
|
|
180
182
|
/** @private */
|
|
181
183
|
_unseal(row) {
|
|
182
|
-
|
|
184
|
+
const { identity, writerKey } = this.db
|
|
185
|
+
for (const entropy of opened(identity, hid.encode(writerKey), row.wrapped)) {
|
|
183
186
|
if (entropy.byteLength !== 32) continue
|
|
184
187
|
// a secret failing the row's commitment means the rotator sealed different ones
|
|
185
188
|
if (row.commit && b4a.equals(crypto.hash(entropy), row.commit)) return entropy
|
|
@@ -214,8 +217,7 @@ export class Rotation {
|
|
|
214
217
|
}
|
|
215
218
|
const top = rows.reduce((a, b) => (b.epoch > a.epoch ? b : a))
|
|
216
219
|
const recipients = new Set(c.decode(wraps, top.wrapped).map((w) => w.id))
|
|
217
|
-
const
|
|
218
|
-
const ids = new Set(members.map((m) => m.id))
|
|
220
|
+
const ids = new Set((await this._devices()).map((d) => d.id))
|
|
219
221
|
const sealedOut = !db.keyring.entropy(top.stamp)
|
|
220
222
|
if (!sealedOut && recipients.size === ids.size && [...recipients].every((id) => ids.has(id))) {
|
|
221
223
|
this._healedFor = null
|