@cero-base/core 2.11.0 → 3.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.
Files changed (39) hide show
  1. package/package.json +8 -8
  2. package/src/blobs/codec.js +25 -0
  3. package/src/blobs/index.js +5 -2
  4. package/src/blobs/server.js +2 -2
  5. package/src/database/dispatch.js +805 -208
  6. package/src/database/encryption.js +25 -21
  7. package/src/database/index.js +747 -124
  8. package/src/database/rotation.js +333 -38
  9. package/src/lib/constants.js +10 -0
  10. package/src/lib/errors.js +8 -0
  11. package/src/lib/schema.js +12 -2
  12. package/src/lib/spec/index.js +10 -5
  13. package/src/lib/spec/schema.json +13 -1
  14. package/src/lib/utils.js +24 -3
  15. package/src/mailbox/inbox.js +368 -41
  16. package/src/mailbox/index.js +75 -20
  17. package/src/mailbox/post.js +13 -8
  18. package/src/network/index.js +12 -5
  19. package/src/pairing/index.js +347 -57
  20. package/src/pairing/invite.js +36 -12
  21. package/src/pairing/response.js +78 -0
  22. package/src/push/index.js +20 -2
  23. package/types/blobs/codec.d.ts +13 -0
  24. package/types/blobs/index.d.ts +2 -0
  25. package/types/database/dispatch.d.ts +52 -7
  26. package/types/database/encryption.d.ts +13 -5
  27. package/types/database/index.d.ts +199 -19
  28. package/types/database/rotation.d.ts +85 -2
  29. package/types/lib/constants.d.ts +4 -0
  30. package/types/lib/errors.d.ts +6 -0
  31. package/types/lib/schema.d.ts +11 -0
  32. package/types/lib/utils.d.ts +2 -0
  33. package/types/mailbox/inbox.d.ts +78 -10
  34. package/types/mailbox/index.d.ts +42 -16
  35. package/types/mailbox/post.d.ts +7 -5
  36. package/types/pairing/index.d.ts +53 -8
  37. package/types/pairing/invite.d.ts +27 -8
  38. package/types/pairing/response.d.ts +50 -0
  39. package/types/push/index.d.ts +12 -2
@@ -45,6 +45,23 @@ AutobeeEncryption.prototype.encrypt = function (index, block, fork, ctx) {
45
45
  return baseEncrypt.call(current, index, block, fork, ctx)
46
46
  }
47
47
 
48
+ // a block in plain (padding type 0) was written without the key: apply lets it carry a join only
49
+ const baseDecrypt = AutobeeEncryption.prototype.decrypt
50
+
51
+ AutobeeEncryption.prototype.decrypt = function (index, block, ctx) {
52
+ if (!this.isCompat(ctx, index) && block[0] === 0) this.auto.plain?.add(plainId(ctx.key, index))
53
+ return baseDecrypt.call(this, index, block, ctx)
54
+ }
55
+
56
+ /**
57
+ * @param {Uint8Array} key The writer core's key.
58
+ * @param {number} index
59
+ * @returns {string}
60
+ */
61
+ export function plainId(key, index) {
62
+ return `${b4a.toHex(key)}:${index}`
63
+ }
64
+
48
65
  /**
49
66
  * Prime a keyring from a local core's persisted epoch stash.
50
67
  *
@@ -137,14 +154,14 @@ export class Keyring {
137
154
  this.entropies = new Map()
138
155
  /** @type {Map<number, number>} stamp → apply-order sequence */
139
156
  this.seqs = new Map()
140
- /** stamp used for new blocks — the adopted epoch with the highest seq */
157
+ /** stamp used for new blocks, picked from the epoch rows (rotation.js) */
141
158
  this.current = 0
142
- /** highest adopted apply-order sequence (0 = base era) */
159
+ /** sequence of the current epoch (0 = base era) */
143
160
  this.seq = 0
144
- /** bumped on every add/remove — cheap change detection */
145
- this.version = 0
146
161
  /** @type {(() => void) | null} called when an epoch is learned or dropped */
147
162
  this.onchange = null
163
+ /** @type {((stamp: number) => Uint8Array | null) | null} asked for a key a block needs and it lacks */
164
+ this.onmiss = null
148
165
  }
149
166
 
150
167
  /** @returns {Array<{ epoch: number, stamp: number, entropy: Uint8Array }>} ascending by seq */
@@ -157,7 +174,7 @@ export class Keyring {
157
174
  /**
158
175
  * @param {number} stamp
159
176
  * @param {Uint8Array} entropy
160
- * @param {number} [seq] Apply-order sequence; drives `current` selection.
177
+ * @param {number} [seq] Apply-order sequence.
161
178
  */
162
179
  add(stamp, entropy, seq = 0) {
163
180
  if (!Number.isInteger(stamp) || stamp <= 0 || stamp > 0xffffffff) {
@@ -168,12 +185,6 @@ export class Keyring {
168
185
  }
169
186
  this.entropies.set(stamp, entropy)
170
187
  if (seq) this.seqs.set(stamp, seq)
171
- const effective = this.seqs.get(stamp) || 0
172
- if (this.current === 0 || (effective > 0 && effective >= this.seq)) {
173
- this.seq = effective
174
- this.current = stamp
175
- }
176
- this.version++
177
188
  this.onchange?.()
178
189
  }
179
190
 
@@ -193,15 +204,6 @@ export class Keyring {
193
204
  remove(stamp) {
194
205
  this.entropies.delete(stamp)
195
206
  this.seqs.delete(stamp)
196
- this.current = 0
197
- this.seq = 0
198
- for (const [s, q] of this.seqs) {
199
- if (q >= this.seq) {
200
- this.seq = q
201
- this.current = s
202
- }
203
- }
204
- this.version++
205
207
  this.onchange?.()
206
208
  }
207
209
  }
@@ -221,6 +223,8 @@ export class EpochAutobee extends Autobee {
221
223
  super(store, key, handlers)
222
224
  /** @type {Keyring | null} */
223
225
  this.keyring = handlers.keyring || null
226
+ /** @type {Set<string>} the writer blocks read in plain, by `plainId` */
227
+ this.plain = new Set()
224
228
  // a block from an unknown epoch parks what read it until the keyring learns something
225
229
  if (this.keyring) this.keyring.onchange = () => this._scheduleEpochWake()
226
230
  /** @private */
@@ -240,7 +244,7 @@ export class EpochAutobee extends Autobee {
240
244
  // learning an epoch wakes it
241
245
  /** @param {number} id @param {{ key?: Uint8Array }} [ctx] @returns {Promise<Uint8Array>} */
242
246
  async getEntropy(id, ctx) {
243
- const entropy = this.keyring && this.keyring.entropy(id)
247
+ const entropy = this.keyring && (this.keyring.entropy(id) || this.keyring.onmiss?.(id))
244
248
  if (entropy) return entropy
245
249
  if (ctx?.key) this._epochStalled.add(b4a.toHex(ctx.key))
246
250
  throw CeroError.UNKNOWN_EPOCH(id)