@cero-base/core 3.1.0 → 3.3.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cero-base/core",
3
- "version": "3.1.0",
3
+ "version": "3.3.0",
4
4
  "description": "cero p2p primitives — identity, storage, network, database, blobs, rpc, pairing.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -152,20 +152,20 @@
152
152
  },
153
153
  "dependencies": {
154
154
  "@hyperswarm/secret-stream": "^6.9.2",
155
- "autobee": "2.13.4",
155
+ "autobee": "2.13.6",
156
156
  "b4a": "^1.9.0",
157
157
  "bare-crypto": "^1.15.4",
158
158
  "bare-fs": "^4.8.4",
159
- "bare-path": "^3.1.2",
159
+ "bare-path": "^3.1.3",
160
160
  "bip39-mnemonic": "^2.5.0",
161
161
  "blind-peering": "^2.14.0",
162
162
  "blind-push": "^1.2.1",
163
- "compact-encoding": "^3.6.0",
163
+ "compact-encoding": "^3.7.0",
164
164
  "corestore": "^7.16.1",
165
165
  "framed-stream": "^1.0.1",
166
166
  "hrpc": "^4.3.1",
167
167
  "hyperblobs": "^2.13.0",
168
- "hypercore": "^11.40.3",
168
+ "hypercore": "^11.40.4",
169
169
  "hypercore-blob-server": "^1.15.1",
170
170
  "hypercore-crypto": "^3.7.0",
171
171
  "hypercore-id-encoding": "^1.3.0",
@@ -177,7 +177,7 @@
177
177
  "hyperswarm": "^4.17.2",
178
178
  "keet-identity-key": "^3.2.0",
179
179
  "ms": "^2.1.3",
180
- "protomux-wakeup": "^2.9.0",
180
+ "protomux-wakeup": "^2.9.1",
181
181
  "ready-resource": "^1.2.0",
182
182
  "safety-catch": "^1.0.3",
183
183
  "sodium-universal": "^5.0.1",
@@ -188,11 +188,11 @@
188
188
  "@hyperswarm/testnet": "^3.1.4",
189
189
  "bare-abort-controller": "^1.1.2",
190
190
  "bare-events": "^2.9.2",
191
- "bare-fetch": "^3.4.1",
191
+ "bare-fetch": "^3.5.0",
192
192
  "bare-process": "^4.5.1",
193
193
  "bare-subprocess": "^6.2.2",
194
- "bare-url": "^2.5.6",
195
- "blind-peer": "^3.18.1",
194
+ "bare-url": "^2.5.7",
195
+ "blind-peer": "^3.18.2",
196
196
  "blind-push-gateway": "^1.0.0",
197
197
  "brittle": "^4.5.0",
198
198
  "protomux": "^3.13.1",
@@ -1020,10 +1020,14 @@ export function makeDispatcher({
1020
1020
  const requests = `@${ns}/requests`
1021
1021
  // an invite grants at most its minter's rank, or the join it admits would grant more
1022
1022
  const capped = (r, record) => grants(r, record.role)
1023
+ // a rotation is sealed to the keys an invite names: only a seat names any, the kind a removal spends first
1024
+ const named = ({ keys, to, secret, confirm, reuse }) =>
1025
+ !keys?.byteLength || (to && secret && !confirm && !reuse && keys.byteLength <= MAX_PASS)
1023
1026
  add('add-invite', async (op, ctx) => {
1024
1027
  await requireWrite(ctx)
1025
1028
  if (await ctx.view.get(invites, { id: op.id })) throw CeroError.REFUSED('invite')
1026
1029
  if (!capped(await getSignerRole(ctx.view, ctx.key), op)) throw CeroError.REFUSED('invite')
1030
+ if (!named(op)) throw CeroError.REFUSED('invite')
1027
1031
  await save(ctx, 'put', 'invites', op)
1028
1032
  })
1029
1033
  add('set-invite', async (op, ctx) => {
@@ -1031,10 +1035,11 @@ export function makeDispatcher({
1031
1035
  const existing = await ctx.view.get(invites, { id: op.id })
1032
1036
  if (!existing) return
1033
1037
  const next = { ...existing, ...op }
1034
- if (!can(r, REMOVE) || !capped(r, next)) throw CeroError.REFUSED('invite')
1038
+ if (!can(r, REMOVE) || !capped(r, next) || !named(next)) throw CeroError.REFUSED('invite')
1035
1039
  await save(ctx, 'set', 'invites', next)
1036
1040
  })
1037
- // a live invite is revoked; a spent one, deleted again, is forgotten
1041
+ // a live invite is revoked; a spent one, deleted again once it ran out, is forgotten. Before, a
1042
+ // second revoke, which two devices write as readily as one, keeps it: whoever holds it hears why
1038
1043
  add('del-invite', async (op, ctx) => {
1039
1044
  const r = await getSignerRole(ctx.view, ctx.key)
1040
1045
  const existing = await ctx.view.get(invites, { id: op.id })
@@ -1042,8 +1047,10 @@ export function makeDispatcher({
1042
1047
  if (!can(r, REMOVE) || !capped(r, existing)) throw CeroError.REFUSED('invite')
1043
1048
  return spend(ctx, existing, 'revoked')
1044
1049
  }
1045
- if (!(await ctx.view.get(spent, { id: op.id }))) return
1050
+ const record = await ctx.view.get(spent, { id: op.id })
1051
+ if (!record) return
1046
1052
  if (!can(r, REMOVE)) throw CeroError.REFUSED('invite')
1053
+ if (!record.expires || !(record.expires <= (ctx.ts || 0))) return
1047
1054
  await ctx.view.delete(spent, { id: op.id })
1048
1055
  })
1049
1056
  // its joins still waiting for a member are turned away with it, and the promises made under it
@@ -98,8 +98,10 @@ export function seal(devices, secret) {
98
98
  * @returns {Generator<Uint8Array, void, unknown>}
99
99
  */
100
100
  export function* opened(identity, id, wrapped) {
101
+ // one sealed before this device had a writer is addressed by its device key
102
+ const device = hid.encode(identity.device.publicKey)
101
103
  for (const w of c.decode(wraps, wrapped)) {
102
- if (w.id !== id) continue
104
+ if (w.id !== id && w.id !== device) continue
103
105
  const secret = identity.unseal(w.box)
104
106
  if (secret) yield secret
105
107
  }
@@ -134,6 +134,8 @@ export class Database extends ReadyResource {
134
134
  /** @private */
135
135
  this._landed = false
136
136
  /** @private */
137
+ this._hooked = 0 // update hooks run: a view move none follows is announced on its own
138
+ /** @private */
137
139
  this._rebuilding = null
138
140
  /** @private */
139
141
  this._writes = new Set()
@@ -305,6 +307,15 @@ export class Database extends ReadyResource {
305
307
  this.bee.on('warning', this._onerror)
306
308
  const bee = this.bee
307
309
  this.bee.on('move-to', () => this._onmove(bee))
310
+ // autobee's update hook follows the view's moves as it applies, not those of a boot, a remap or
311
+ // an interrupted run: one no hook follows changes rows all the same, and anyone waiting hears it
312
+ bee.view.watch(() => {
313
+ const hooked = this._hooked
314
+ Promise.resolve().then(() => {
315
+ if (this._hooked !== hooked || this.bee !== bee || this.closing) return
316
+ this.emit('update', new Set(['*']))
317
+ })
318
+ })
308
319
 
309
320
  // with no mirror to hold the database, every member holds it: any member serves a joiner, offline
310
321
  if (this.network && !this.network.mirrors.length) {
@@ -1228,6 +1239,7 @@ export class Database extends ReadyResource {
1228
1239
  // One that landed ranks its database all the same, changed or not: its ops have to reach the others
1229
1240
  /** @private */
1230
1241
  async _update(db) {
1242
+ this._hooked++
1231
1243
  await db.update()
1232
1244
  this._track(await this.dispatcher.version(db))
1233
1245
  const touched = this._touched
@@ -1541,7 +1553,9 @@ export class Database extends ReadyResource {
1541
1553
  key,
1542
1554
  dbKey: this.key,
1543
1555
  seed: crypto.hash([key, value]),
1544
- stamp
1556
+ stamp,
1557
+ // the time the append will carry, at least: what reads it decides as apply will
1558
+ ts: Math.max(Date.now(), this.bee.system.timestamp || 0)
1545
1559
  })
1546
1560
  wrapped.push(wrap(stamp, value))
1547
1561
  }
@@ -71,6 +71,14 @@ export class Rotation {
71
71
  return this._queued(() => this._carry(ops))
72
72
  }
73
73
 
74
+ /**
75
+ * @returns {Promise<void>} Once every rotation asked of this device so far has ended: one asked
76
+ * after is sealed to the devices as they stand by then.
77
+ */
78
+ async settled() {
79
+ await this._queue.catch(() => {})
80
+ }
81
+
74
82
  /** @private */
75
83
  _queued(fn) {
76
84
  const run = this._queue.catch(() => {}).then(fn)
@@ -353,6 +361,12 @@ export class Rotation {
353
361
  const { data: requests } = await this.db.get('requests')
354
362
  const promised = []
355
363
  for (const row of requests) if (await this._stands(row)) promised.push(row)
364
+ // a seat handed its member the keys before any device of hers asked: sealed to them until she comes
365
+ const { data: invites } = await this.db.get('invites')
366
+ for (const { keys, expires } of invites) {
367
+ if (expires > 0 && Date.now() > expires) continue
368
+ for (const key of keysOf(keys)) promised.push({ id: hid.encode(key), key })
369
+ }
356
370
  return promised
357
371
  }
358
372
 
@@ -490,7 +504,8 @@ export class Rotation {
490
504
  const { data: members } = await this.db.get('members')
491
505
  const standing = new Set(members.map((m) => m.id))
492
506
  const { data: devices } = await this.db.get('devices')
493
- return devices.some((d) => d.key && standing.has(d.memberId) && !named.has(d.id))
507
+ const unnamed = (d) => !named.has(d.id) && !named.has(hid.encode(d.key))
508
+ return devices.some((d) => d.key && standing.has(d.memberId) && unnamed(d))
494
509
  }
495
510
  }
496
511
 
@@ -46,6 +46,7 @@ const MAX_DELAY = 2 ** 31 - 1
46
46
  * @property {boolean} [confirm] Its joins wait for a member to accept them, as requests.
47
47
  * @property {Uint8Array | null} [data] The app's payload in the invite, readable before joining: `Invite.parse(invite).data`.
48
48
  * @property {string | null} [to] The id of the one member it is made for: nobody else joins with it. Single-use, never `confirm`.
49
+ * @property {Uint8Array[]} [keys] The device keys of the member it is made `to`, handed the room's keys with it: a rotation before she comes is sealed to them.
49
50
  *
50
51
  * @typedef {object} AcceptOpts
51
52
  * @property {string} [role] Role granted: the invite's by default, at most the invite's.
@@ -175,7 +176,9 @@ export class Pairing extends ReadyResource {
175
176
  expires: invite.expires,
176
177
  createdAt: Date.now(),
177
178
  secret,
178
- to
179
+ to,
180
+ // only an invite with an address of its own is spent by a removal before its rotation is sealed
181
+ keys: to && secret && opts.keys && (await this._has('keys')) ? b4a.concat(opts.keys) : null
179
182
  }
180
183
  invite.stamp = await this.db.write([['add-invite', row]])
181
184
  if (secret || invite.stamp > this.db.start) invite.version = STAMPED
@@ -188,9 +191,15 @@ export class Pairing extends ReadyResource {
188
191
  // field, or the database's version has not reached it, and the invite carries the room's address
189
192
  /** @private */
190
193
  async _secret() {
191
- const since = this.db.meta.since?.types[`@${this.db.ns}/invite`]?.secret
192
- const version = await this.db.dispatcher.version(this.db.view)
193
- return since <= version ? crypto.randomBytes(32) : null
194
+ return (await this._has('secret')) ? crypto.randomBytes(32) : null
195
+ }
196
+
197
+ // a field of an invite's row the built spec has and the database's version has reached: a write
198
+ // carrying one it has not is refused
199
+ /** @private */
200
+ async _has(field) {
201
+ const since = this.db.meta.since?.types[`@${this.db.ns}/invite`]?.[field]
202
+ return since <= (await this.db.dispatcher.version(this.db.view))
194
203
  }
195
204
 
196
205
  /**
@@ -264,6 +264,8 @@ export declare class Database extends ReadyResource {
264
264
  /** @private */
265
265
  _landed;
266
266
  /** @private */
267
+ _hooked;
268
+ /** @private */
267
269
  _rebuilding;
268
270
  /** @private */
269
271
  _writes;
@@ -44,6 +44,11 @@ export declare class Rotation {
44
44
  * @returns {Promise<number | undefined>} What the removal's write resolves.
45
45
  */
46
46
  carry(ops: Array<[string, unknown]>): Promise<number | undefined>;
47
+ /**
48
+ * @returns {Promise<void>} Once every rotation asked of this device so far has ended: one asked
49
+ * after is sealed to the devices as they stand by then.
50
+ */
51
+ settled(): Promise<void>;
47
52
  /** @private */
48
53
  private _queued;
49
54
  /**
@@ -41,6 +41,10 @@ export type InviteOpts = {
41
41
  * The id of the one member it is made for: nobody else joins with it. Single-use, never `confirm`.
42
42
  */
43
43
  to?: string | null;
44
+ /**
45
+ * The device keys of the member it is made `to`, handed the room's keys with it: a rotation before she comes is sealed to them.
46
+ */
47
+ keys?: Uint8Array[];
44
48
  };
45
49
  export type AcceptOpts = {
46
50
  /**
@@ -119,6 +123,7 @@ export type JoinResult = {
119
123
  * @property {boolean} [confirm] Its joins wait for a member to accept them, as requests.
120
124
  * @property {Uint8Array | null} [data] The app's payload in the invite, readable before joining: `Invite.parse(invite).data`.
121
125
  * @property {string | null} [to] The id of the one member it is made for: nobody else joins with it. Single-use, never `confirm`.
126
+ * @property {Uint8Array[]} [keys] The device keys of the member it is made `to`, handed the room's keys with it: a rotation before she comes is sealed to them.
122
127
  *
123
128
  * @typedef {object} AcceptOpts
124
129
  * @property {string} [role] Role granted: the invite's by default, at most the invite's.
@@ -190,6 +195,8 @@ export declare class Pairing extends ReadyResource {
190
195
  invite(opts?: InviteOpts): Promise<string>;
191
196
  /** @private */
192
197
  private _secret;
198
+ /** @private */
199
+ private _has;
193
200
  /**
194
201
  * Revoke an invite, for every member. Needs the remove permission.
195
202
  *