@johnhenry/browsermesh-core 0.2.3 → 0.2.4

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/group-keys.mjs +59 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnhenry/browsermesh-core",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Identity, crypto, peer management, and trust primitives for BrowserMesh",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
@@ -386,18 +386,73 @@ export class GroupKeyManager {
386
386
  * Used when adding a member (existing key is fine, but new epoch
387
387
  * ensures clean key lifecycle) or periodically for key freshness.
388
388
  *
389
+ * By default this self-generates a fresh random key for the new epoch
390
+ * (unchanged, original behavior). Pass `{ epoch, key }` to instead
391
+ * *adopt* a specific, externally-agreed key rather than generating one —
392
+ * see the "Adopt-path calling convention" note below.
393
+ *
394
+ * ## Adopt-path calling convention
395
+ *
396
+ * This exists for callers that run a distributed agreement protocol
397
+ * (e.g. PBFT/consensus) over *when* to rotate and *who* the members are,
398
+ * without letting the actual key bytes anywhere near that agreement
399
+ * payload — broadcasting raw key material to every participant of a
400
+ * consensus round (rather than just the intended group members) would be
401
+ * a security regression relative to the per-recipient envelope
402
+ * mechanism this package already provides (`wrapKeyForMember` /
403
+ * `unwrapKeyForMember`, used by `broadcastDistribute()` /
404
+ * `wireTransport()`'s `GROUP_KEY_DISTRIBUTE` handler).
405
+ *
406
+ * The intended shape of that split:
407
+ * 1. Consensus agrees ONLY on `{ epoch, members }` — never key bytes.
408
+ * 2. Exactly one designated party (the proposer/leader) generates the
409
+ * real key and distributes it to each member individually via the
410
+ * existing secure envelope mechanism (`wrapKeyForMember` on the
411
+ * sender, `unwrapKeyForMember` — or the `GROUP_KEY_DISTRIBUTE`
412
+ * handler wired by `wireTransport()`, which calls
413
+ * `unwrapKeyForMember` internally — on each recipient).
414
+ * 3. Once a caller has BOTH the consensus-agreed epoch number AND the
415
+ * real `CryptoKey` obtained locally from that envelope decrypt (not
416
+ * exported/wire bytes — `unwrapKeyForMember` already hands back a
417
+ * live `CryptoKey`, the same shape `acceptEpoch()` accepts), it
418
+ * calls `rotate(members, { epoch, key })` to install that exact key
419
+ * as this manager's active state for that epoch, instead of calling
420
+ * plain `rotate(members)` (which would self-generate a different,
421
+ * non-agreed key).
422
+ *
423
+ * `key` is deliberately a `CryptoKey`, not exported/serialized bytes:
424
+ * this path is for in-process callers on the same JS runtime (e.g. a
425
+ * PBFT `applyBlock()` dispatch handing off to this manager) that already
426
+ * hold a real key locally — it is not a wire format and must never be
427
+ * put on the wire itself.
428
+ *
389
429
  * @param {string[]} [newMembers] - Updated member list; defaults to current members
430
+ * @param {object} [adopt] - Omit entirely for today's self-generating behavior.
431
+ * @param {number} [adopt.epoch] - Externally-agreed epoch number to adopt. Must be
432
+ * provided together with `adopt.key`, never alone.
433
+ * @param {CryptoKey} [adopt.key] - Externally-agreed key to adopt in-process (e.g. from
434
+ * `unwrapKeyForMember`/an envelope decrypt), rather than generating a new one. Must be
435
+ * provided together with `adopt.epoch`, never alone.
390
436
  * @returns {Promise<GroupState>}
391
437
  */
392
- async rotate(newMembers) {
438
+ async rotate(newMembers, { epoch, key } = {}) {
439
+ const hasEpoch = epoch !== undefined
440
+ const hasKey = key !== undefined
441
+ if (hasEpoch !== hasKey) {
442
+ throw new Error('rotate: epoch and key must be provided together (both or neither)')
443
+ }
444
+ if (hasKey && !(typeof CryptoKey !== 'undefined' && key instanceof CryptoKey)) {
445
+ throw new Error('rotate: key must be a CryptoKey (e.g. from unwrapKeyForMember/an envelope decrypt), not exported bytes')
446
+ }
447
+
393
448
  const current = this.getCurrentState()
394
449
  const members = newMembers || (current ? current.members : [this.#localPodId])
395
450
 
396
- const nextEpoch = this.#currentEpoch + 1
397
- const key = await this.#generateKey()
451
+ const nextEpoch = hasEpoch ? epoch : this.#currentEpoch + 1
452
+ const nextKey = hasKey ? key : await this.#generateKey()
398
453
  const state = new GroupState({
399
454
  epoch: nextEpoch,
400
- key,
455
+ key: nextKey,
401
456
  members,
402
457
  })
403
458