@cero-base/core 2.2.0 → 2.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.
Files changed (55) hide show
  1. package/README.md +32 -212
  2. package/package.json +19 -10
  3. package/src/blobs/index.js +9 -0
  4. package/src/blobs/server.js +2 -0
  5. package/src/database/changes.js +1 -1
  6. package/src/database/dispatch.js +341 -209
  7. package/src/database/encryption.js +83 -33
  8. package/src/database/index.js +184 -83
  9. package/src/database/rotation.js +43 -30
  10. package/src/identity/index.js +20 -78
  11. package/src/index.js +3 -10
  12. package/src/lib/constants.js +2 -0
  13. package/src/lib/errors.js +3 -11
  14. package/src/lib/spec/index.js +136 -132
  15. package/src/lib/spec/schema.json +84 -99
  16. package/src/lib/utils.js +57 -3
  17. package/src/mailbox/inbox.js +103 -0
  18. package/src/mailbox/index.js +146 -0
  19. package/src/mailbox/post.js +69 -0
  20. package/src/network/discovery.js +26 -1
  21. package/src/network/index.js +105 -64
  22. package/src/network/presence.js +6 -2
  23. package/src/pairing/index.js +330 -254
  24. package/src/pairing/invite.js +83 -144
  25. package/src/pairing/request.js +54 -56
  26. package/src/rpc/index.js +18 -93
  27. package/src/rpc/peer.js +4 -1
  28. package/src/storage/index.js +78 -39
  29. package/types/blobs/index.d.ts +22 -9
  30. package/types/blobs/server.d.ts +4 -2
  31. package/types/database/changes.d.ts +2 -2
  32. package/types/database/dispatch.d.ts +19 -13
  33. package/types/database/encryption.d.ts +67 -29
  34. package/types/database/index.d.ts +144 -119
  35. package/types/database/rotation.d.ts +32 -13
  36. package/types/identity/index.d.ts +6 -47
  37. package/types/index.d.ts +3 -16
  38. package/types/lib/constants.d.ts +1 -0
  39. package/types/lib/errors.d.ts +3 -9
  40. package/types/lib/spec/index.d.ts +52 -16
  41. package/types/lib/utils.d.ts +40 -8
  42. package/types/mailbox/inbox.d.ts +52 -0
  43. package/types/mailbox/index.d.ts +111 -0
  44. package/types/mailbox/post.d.ts +37 -0
  45. package/types/network/discovery.d.ts +21 -5
  46. package/types/network/index.d.ts +64 -48
  47. package/types/network/presence.d.ts +17 -4
  48. package/types/pairing/index.d.ts +143 -134
  49. package/types/pairing/invite.d.ts +70 -114
  50. package/types/pairing/request.d.ts +58 -98
  51. package/types/rpc/index.d.ts +26 -44
  52. package/types/rpc/peer.d.ts +10 -7
  53. package/types/storage/index.d.ts +64 -54
  54. package/src/pairing/candidate.js +0 -123
  55. package/types/pairing/candidate.d.ts +0 -36
@@ -1,14 +1,17 @@
1
1
  import Autobee from 'autobee'
2
- import autobeeEncryption from 'autobee-encryption'
2
+ import autobeeEncryption from 'autobee/lib/encryption.js'
3
3
  import crypto from 'hypercore-crypto'
4
4
  import c from 'compact-encoding'
5
5
  import b4a from 'b4a'
6
6
 
7
+ import hid from 'hypercore-id-encoding'
8
+
7
9
  import { CeroError } from '../lib/errors.js'
10
+ import { Identity } from '../identity/index.js'
8
11
 
9
- const { AutobeeEncryption, WriterEncryption } = autobeeEncryption
12
+ const { AutobeeEncryption } = autobeeEncryption
10
13
 
11
- // the same derivation constant autobee-encryption uses
14
+ // the same derivation constant autobee's encryption uses
12
15
  const NS_HASH_KEY = crypto.namespace('autobase', 4)[2]
13
16
 
14
17
  const NS_BLOBS = crypto.namespace('cero/blobs', 1)[0]
@@ -23,30 +26,32 @@ export function blobEpochKey(entropy) {
23
26
  return crypto.hash([NS_BLOBS, entropy])
24
27
  }
25
28
 
26
- // autobee news WriterEncryption directly, so epoch awareness is patched onto the
27
- // autobee-encryption prototype. Epoch 0 keeps the upstream derivation, epoch n derives
28
- // from the keyring; without a keyring behaviour is identical to upstream
29
+ // autobee reads and writes under key 0 only. Until it takes key ids from its instance (keyId,
30
+ // getEntropy), the same two changes are applied to the base class every provider comes from
29
31
  const baseGetKeys = AutobeeEncryption.prototype.getKeys
32
+ const baseEncrypt = AutobeeEncryption.prototype.encrypt
30
33
 
31
34
  AutobeeEncryption.prototype.getKeys = async function (id, ctx) {
32
35
  if (!id) return baseGetKeys.call(this, id, ctx)
33
-
34
- const keyring = this.auto?.keyring
35
- const entropy = keyring && keyring.entropy(id)
36
- if (!entropy) {
37
- // a writer autobee freezes over this throw and only wakeup() re-adds it
38
- if (ctx?.key && this.auto?._epochStalled) {
39
- this.auto._epochStalled.add(b4a.toString(ctx.key, 'hex'))
40
- this.auto._scheduleEpochRetry()
41
- }
42
- throw CeroError.UNKNOWN_EPOCH(id)
43
- }
44
-
45
- const block = this.blockKey(entropy, ctx)
36
+ const block = this.blockKey(await this.auto.getEntropy(id, ctx), ctx)
46
37
  return { id, block, hash: crypto.hash([NS_HASH_KEY, block]) }
47
38
  }
48
39
 
49
- /** Prime a keyring from a local core's persisted epoch stash. */
40
+ AutobeeEncryption.prototype.encrypt = function (index, block, fork, ctx) {
41
+ const keyId = this.auto.keyId || 0
42
+ if (!keyId) return baseEncrypt.call(this, index, block, fork, ctx)
43
+ const current = Object.create(this)
44
+ current.getKeys = (id, c) => this.getKeys(keyId, c)
45
+ return baseEncrypt.call(current, index, block, fork, ctx)
46
+ }
47
+
48
+ /**
49
+ * Prime a keyring from a local core's persisted epoch stash.
50
+ *
51
+ * @param {Keyring} keyring
52
+ * @param {import('hypercore')} local
53
+ * @returns {Promise<void>}
54
+ */
50
55
  export async function loadEpochs(keyring, local) {
51
56
  const saved = await local.getUserData('cero/epochs').catch(() => null)
52
57
  if (!saved) return
@@ -57,15 +62,32 @@ export async function loadEpochs(keyring, local) {
57
62
  }
58
63
  }
59
64
 
60
- AutobeeEncryption.prototype.update = async function (ctx) {
61
- const current = this.auto?.keyring ? this.auto.keyring.current : 0
62
- if (!this.keys || this.keys.id !== current) this.keys = await this.get(current, ctx)
63
- }
64
-
65
65
  /**
66
66
  * Wire codec for a rotation announcement's envelope list — one sealed box
67
67
  * per remaining member, addressed by member id.
68
+ *
69
+ * @param {Array<{ id: string }>} members
70
+ * @param {Uint8Array} secret
71
+ * @returns {Array<{ id: string, box: Uint8Array }>}
72
+ */
73
+ export function seal(members, secret) {
74
+ return members.map((m) => ({ id: m.id, box: Identity.seal(hid.decode(m.id), secret) }))
75
+ }
76
+
77
+ /**
78
+ * @param {import('../identity/index.js').Identity} identity
79
+ * @param {Uint8Array} wrapped
80
+ * @returns {Generator<Uint8Array, void, unknown>}
68
81
  */
82
+ export function* opened(identity, wrapped) {
83
+ for (const w of c.decode(wraps, wrapped)) {
84
+ if (w.id !== identity.id) continue
85
+ const secret = identity.unseal(w.box)
86
+ if (secret) yield secret
87
+ }
88
+ }
89
+
90
+ /** @type {import('compact-encoding').Encoder<Array<{ id: string, box: Uint8Array }>>} */
69
91
  export const wraps = c.array({
70
92
  preencode(state, w) {
71
93
  c.string.preencode(state, w.id)
@@ -82,6 +104,8 @@ export const wraps = c.array({
82
104
 
83
105
  /**
84
106
  * Wire codec for locally persisted / pairing-delivered epoch secrets.
107
+ *
108
+ * @type {import('compact-encoding').Encoder<Array<{ epoch: number, stamp: number, entropy: Uint8Array }>>}
85
109
  */
86
110
  export const epochEntries = c.array({
87
111
  preencode(state, e) {
@@ -177,27 +201,50 @@ export class Keyring {
177
201
  }
178
202
  }
179
203
 
180
- /**
181
- * The epoch-aware provider — the class itself is upstream WriterEncryption; the epoch
182
- * behavior lives on the (patched) base prototype above.
183
- */
184
- export class EpochEncryption extends WriterEncryption {}
185
-
186
204
  /**
187
205
  * Autobee with a rotation keyring. Every provider autobee constructs (view/system factory,
188
- * foreign cores, ActiveWriters) picks the epochs up through the patched base class and
189
- * this `keyring` property.
206
+ * foreign cores, ActiveWriters) picks the epochs up through the patched base class, which asks
207
+ * this instance for `keyId` and `getEntropy`.
190
208
  */
191
209
  export class EpochAutobee extends Autobee {
210
+ /**
211
+ * @param {import('corestore')} store
212
+ * @param {Uint8Array | null} key
213
+ * @param {{ keyring?: Keyring } & Record<string, unknown>} [handlers] Autobee's options, plus the keyring.
214
+ */
192
215
  constructor(store, key, handlers = {}) {
193
216
  super(store, key, handlers)
217
+ /** @type {Keyring | null} */
194
218
  this.keyring = handlers.keyring || null
219
+ /** @private */
195
220
  this._epochStalled = new Set()
221
+ /** @private */
196
222
  this._epochRetry = null
223
+ /** @private */
197
224
  this._epochRetryDelay = 1000
225
+ /** @private */
198
226
  this._epochRetrySeen = 0
199
227
  }
200
228
 
229
+ /** @returns {number} the key id new blocks are written with */
230
+ get keyId() {
231
+ return this.keyring ? this.keyring.current : 0
232
+ }
233
+
234
+ // a block from an epoch not learned yet: the writer freezes over the throw, and the retry
235
+ // wakes it once the announcement lands
236
+ /** @param {number} id @param {{ key?: Uint8Array }} [ctx] @returns {Promise<Uint8Array>} */
237
+ async getEntropy(id, ctx) {
238
+ const entropy = this.keyring && this.keyring.entropy(id)
239
+ if (entropy) return entropy
240
+ if (ctx?.key) {
241
+ this._epochStalled.add(b4a.toHex(ctx.key))
242
+ this._scheduleEpochRetry()
243
+ }
244
+ throw CeroError.UNKNOWN_EPOCH(id)
245
+ }
246
+
247
+ /** @private */
201
248
  async _close() {
202
249
  if (this._epochRetry) clearTimeout(this._epochRetry)
203
250
  this._epochRetry = null
@@ -206,6 +253,7 @@ export class EpochAutobee extends Autobee {
206
253
 
207
254
  // an UNKNOWN_EPOCH from the drain parks the pass; the retry wakes the stalled cores once the
208
255
  // announcement lands. Forward the arguments: the local drain passes { local: true }
256
+ /** @private */
209
257
  async _bumpPendingWriters(...args) {
210
258
  try {
211
259
  return await super._bumpPendingWriters(...args)
@@ -217,6 +265,7 @@ export class EpochAutobee extends Autobee {
217
265
  }
218
266
 
219
267
  // hints read the system bee outside the guarded drain; park and retry like everything else
268
+ /** @private */
220
269
  async _applyWakeupHints() {
221
270
  try {
222
271
  return await super._applyWakeupHints()
@@ -228,6 +277,7 @@ export class EpochAutobee extends Autobee {
228
277
  }
229
278
 
230
279
  // a stalled core produces no wake-up of its own, only wakeup() re-adds it; exponential backoff
280
+ /** @private */
231
281
  _scheduleEpochRetry() {
232
282
  if (this._epochRetry || this.closing) return
233
283
  const version = this.keyring ? this.keyring.version : 0