@kumiai/rpc 0.1.0 → 0.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/lib/recovery.d.ts CHANGED
@@ -1,12 +1,32 @@
1
- /**
2
- * Payload codecs for the recovery rendezvous carried on the handshake lane. A
3
- * request names a `requestID` so replies correlate and redundant responders can
4
- * observe-and-suppress; a reply echoes the `requestID` and carries the GroupInfo.
5
- */
6
- export declare function encodeRecoveryRequest(requestID: string): Uint8Array;
7
- export declare function decodeRecoveryRequest(payload: Uint8Array): string;
1
+ export declare function encodeRecoveryRequest(requestID: string, request: Uint8Array): Uint8Array;
2
+ export declare function decodeRecoveryRequest(payload: Uint8Array): {
3
+ requestID: string;
4
+ request: Uint8Array;
5
+ };
8
6
  export declare function encodeRecoveryReply(requestID: string, groupInfo: Uint8Array): Uint8Array;
9
7
  export declare function decodeRecoveryReply(payload: Uint8Array): {
10
8
  requestID: string;
11
9
  groupInfo: Uint8Array;
12
10
  };
11
+ /**
12
+ * A ledger gather carries the port's signed request blob, as a recovery request does: it is
13
+ * what a responder authorizes against and the only key it will seal to. A request with no blob
14
+ * is from nobody, and every responder refuses it.
15
+ */
16
+ export declare function encodeLedgerRequest(requestID: string, request: Uint8Array): Uint8Array;
17
+ export declare function decodeLedgerRequest(payload: Uint8Array): {
18
+ requestID: string;
19
+ request: Uint8Array;
20
+ };
21
+ /**
22
+ * A responder's whole ordered ledger, SEALED to the ephemeral key inside the request it
23
+ * answers. The lane carries the bytes and reads none of them.
24
+ *
25
+ * Order is inside the seal and load-bearing: the head is a chain digest, so a permuted list of
26
+ * the same tokens folds to a different head and the requester rejects it.
27
+ */
28
+ export declare function encodeLedgerReply(requestID: string, sealed: Uint8Array): Uint8Array;
29
+ export declare function decodeLedgerReply(payload: Uint8Array): {
30
+ requestID: string;
31
+ sealed: Uint8Array;
32
+ };
package/lib/recovery.js CHANGED
@@ -1,35 +1,90 @@
1
1
  import { fromUTF, toUTF } from '@sozai/codec';
2
2
  /**
3
- * Payload codecs for the recovery rendezvous carried on the handshake lane. A
4
- * request names a `requestID` so replies correlate and redundant responders can
5
- * observe-and-suppress; a reply echoes the `requestID` and carries the GroupInfo.
6
- */ export function encodeRecoveryRequest(requestID) {
7
- return fromUTF(requestID);
8
- }
9
- export function decodeRecoveryRequest(payload) {
10
- return toUTF(payload);
11
- }
12
- export function encodeRecoveryReply(requestID, groupInfo) {
3
+ * Payload codecs for the rendezvous carried on the handshake lane.
4
+ *
5
+ * A request names a `requestID` (reply correlation + responder observe-and-suppress) and carries
6
+ * the port's signed request blob, which the peer never reads. Requester DID and the ephemeral
7
+ * key its reply must be sealed to live INSIDE that signed blob, never in a peer-added field — a
8
+ * field beside the token would offer the responder an unsigned DID to seal against.
9
+ *
10
+ * The ledger gather is the same rendezvous reversed and carries the SAME signed blob: the topic
11
+ * is public and secretless, so an unnamed request is one anyone can mint, and an unsealed reply
12
+ * puts the group's whole authority state in the clear.
13
+ */ /** Cap on decoded ID lengths — these become attacker-controlled map keys. */ const MAX_REQUEST_ID_BYTES = 128;
14
+ function encodeWithRequestID(requestID, payload, label) {
13
15
  const rid = fromUTF(requestID);
14
- if (rid.length > 0xffff) {
15
- throw new Error('recovery reply requestID is too long');
16
+ if (rid.length > MAX_REQUEST_ID_BYTES) {
17
+ throw new Error(`${label} requestID is too long`);
16
18
  }
17
- const out = new Uint8Array(2 + rid.length + groupInfo.length);
19
+ const out = new Uint8Array(2 + rid.length + payload.length);
18
20
  new DataView(out.buffer).setUint16(0, rid.length, true);
19
21
  out.set(rid, 2);
20
- out.set(groupInfo, 2 + rid.length);
22
+ out.set(payload, 2 + rid.length);
21
23
  return out;
22
24
  }
23
- export function decodeRecoveryReply(payload) {
25
+ function decodeWithRequestID(payload, label) {
24
26
  if (payload.length < 2) {
25
- throw new Error('recovery reply is too short');
27
+ throw new Error(`${label} is too short`);
26
28
  }
27
29
  const ridLen = new DataView(payload.buffer, payload.byteOffset, payload.byteLength).getUint16(0, true);
30
+ if (ridLen > MAX_REQUEST_ID_BYTES) {
31
+ throw new Error(`${label} requestID is too long`);
32
+ }
28
33
  if (payload.length < 2 + ridLen) {
29
- throw new Error('recovery reply is truncated');
34
+ throw new Error(`${label} is truncated`);
30
35
  }
31
36
  return {
32
37
  requestID: toUTF(payload.subarray(2, 2 + ridLen)),
33
- groupInfo: payload.subarray(2 + ridLen)
38
+ rest: payload.subarray(2 + ridLen)
39
+ };
40
+ }
41
+ export function encodeRecoveryRequest(requestID, request) {
42
+ return encodeWithRequestID(requestID, request, 'recovery request');
43
+ }
44
+ export function decodeRecoveryRequest(payload) {
45
+ const { requestID, rest } = decodeWithRequestID(payload, 'recovery request');
46
+ return {
47
+ requestID,
48
+ request: rest
49
+ };
50
+ }
51
+ export function encodeRecoveryReply(requestID, groupInfo) {
52
+ return encodeWithRequestID(requestID, groupInfo, 'recovery reply');
53
+ }
54
+ export function decodeRecoveryReply(payload) {
55
+ const { requestID, rest } = decodeWithRequestID(payload, 'recovery reply');
56
+ return {
57
+ requestID,
58
+ groupInfo: rest
59
+ };
60
+ }
61
+ /**
62
+ * A ledger gather carries the port's signed request blob, as a recovery request does: it is
63
+ * what a responder authorizes against and the only key it will seal to. A request with no blob
64
+ * is from nobody, and every responder refuses it.
65
+ */ export function encodeLedgerRequest(requestID, request) {
66
+ return encodeWithRequestID(requestID, request, 'ledger request');
67
+ }
68
+ export function decodeLedgerRequest(payload) {
69
+ const { requestID, rest } = decodeWithRequestID(payload, 'ledger request');
70
+ return {
71
+ requestID,
72
+ request: rest
73
+ };
74
+ }
75
+ /**
76
+ * A responder's whole ordered ledger, SEALED to the ephemeral key inside the request it
77
+ * answers. The lane carries the bytes and reads none of them.
78
+ *
79
+ * Order is inside the seal and load-bearing: the head is a chain digest, so a permuted list of
80
+ * the same tokens folds to a different head and the requester rejects it.
81
+ */ export function encodeLedgerReply(requestID, sealed) {
82
+ return encodeWithRequestID(requestID, sealed, 'ledger reply');
83
+ }
84
+ export function decodeLedgerReply(payload) {
85
+ const { requestID, rest } = decodeWithRequestID(payload, 'ledger reply');
86
+ return {
87
+ requestID,
88
+ sealed: rest
34
89
  };
35
90
  }
package/lib/topic.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  /** Reserved label for per-member unicast inbox topics. */
2
2
  export declare const INBOX_LABEL = "enkaku/inbox/v1";
3
- /** Reserved label for the non-rotating MLS-handshake/recovery topic. */
4
- export declare const HANDSHAKE_LABEL = "enkaku/handshake/v1";
3
+ /** Reserved label for the non-rotating MLS commit topic. */
4
+ export declare const COMMIT_LABEL = "enkaku/commit/v1";
5
+ /** Reserved label for the non-rotating recovery-rendezvous topic. */
6
+ export declare const RENDEZVOUS_LABEL = "enkaku/rendezvous/v1";
5
7
  /**
6
8
  * Group-scoped broadcast/multicast topic for an application protocol. Opaque to
7
9
  * the hub; derivable only by members holding the epoch secret. `scope`
@@ -15,12 +17,21 @@ export declare function protocolTopic(secret: Uint8Array, epoch: number, protoco
15
17
  */
16
18
  export declare function inboxTopic(secret: Uint8Array, epoch: number, memberDID: string): string;
17
19
  /**
18
- * The non-rotating MLS-handshake/recovery topic, derived from the
19
- * epoch-independent recovery secret (epoch fixed at `0`). Stable for the group's
20
- * whole life so every member — including one stranded on a stale epoch — can
21
- * always derive the rendezvous. Opaque to the hub.
20
+ * The non-rotating commit topic: MLS Commits only, retained as a log, read by pull. Derived
21
+ * from the epoch-independent recovery secret (epoch fixed at `0`), so it is stable for the
22
+ * group's life and a member stranded on any epoch can still derive it. Opaque to the hub.
23
+ *
24
+ * Separate from {@link rendezvousTopic}: the commit lane is a log whose head every commit
25
+ * moves, the rendezvous lane is a mailbox whose frames must never move a head.
22
26
  */
23
- export declare function handshakeTopic(recoverySecret: Uint8Array): string;
27
+ export declare function commitTopic(recoverySecret: Uint8Array): string;
28
+ /**
29
+ * The non-rotating recovery-rendezvous topic: recovery request/reply, published
30
+ * unconditionally and pushed. Derived from the same epoch-independent recovery secret, so a
31
+ * stranded peer always shares it with the live group. Mailbox semantics: a requester
32
+ * subscribes before it asks, so it cannot miss its own reply. Opaque to the hub.
33
+ */
34
+ export declare function rendezvousTopic(recoverySecret: Uint8Array): string;
24
35
  /**
25
36
  * Public, secretless pre-group rendezvous topic (invite / keypackage / Welcome).
26
37
  * `b64url(SHA-256(DISCOVERY_PREFIX ‖ SEP ‖ memberDID))` — intentionally
package/lib/topic.js CHANGED
@@ -2,7 +2,8 @@ import { deriveTopicID } from '@kumiai/broadcast';
2
2
  import { sha256 } from '@noble/hashes/sha2.js';
3
3
  import { fromUTF, toB64U } from '@sozai/codec';
4
4
  /** Reserved label for per-member unicast inbox topics. */ export const INBOX_LABEL = 'enkaku/inbox/v1';
5
- /** Reserved label for the non-rotating MLS-handshake/recovery topic. */ export const HANDSHAKE_LABEL = 'enkaku/handshake/v1';
5
+ /** Reserved label for the non-rotating MLS commit topic. */ export const COMMIT_LABEL = 'enkaku/commit/v1';
6
+ /** Reserved label for the non-rotating recovery-rendezvous topic. */ export const RENDEZVOUS_LABEL = 'enkaku/rendezvous/v1';
6
7
  const DISCOVERY_PREFIX = 'enkaku/discovery/v1';
7
8
  const SEP = '\0';
8
9
  /**
@@ -20,12 +21,22 @@ const SEP = '\0';
20
21
  return deriveTopicID(secret, epoch, INBOX_LABEL, memberDID);
21
22
  }
22
23
  /**
23
- * The non-rotating MLS-handshake/recovery topic, derived from the
24
- * epoch-independent recovery secret (epoch fixed at `0`). Stable for the group's
25
- * whole life so every member — including one stranded on a stale epoch — can
26
- * always derive the rendezvous. Opaque to the hub.
27
- */ export function handshakeTopic(recoverySecret) {
28
- return deriveTopicID(recoverySecret, 0, HANDSHAKE_LABEL);
24
+ * The non-rotating commit topic: MLS Commits only, retained as a log, read by pull. Derived
25
+ * from the epoch-independent recovery secret (epoch fixed at `0`), so it is stable for the
26
+ * group's life and a member stranded on any epoch can still derive it. Opaque to the hub.
27
+ *
28
+ * Separate from {@link rendezvousTopic}: the commit lane is a log whose head every commit
29
+ * moves, the rendezvous lane is a mailbox whose frames must never move a head.
30
+ */ export function commitTopic(recoverySecret) {
31
+ return deriveTopicID(recoverySecret, 0, COMMIT_LABEL);
32
+ }
33
+ /**
34
+ * The non-rotating recovery-rendezvous topic: recovery request/reply, published
35
+ * unconditionally and pushed. Derived from the same epoch-independent recovery secret, so a
36
+ * stranded peer always shares it with the live group. Mailbox semantics: a requester
37
+ * subscribes before it asks, so it cannot miss its own reply. Opaque to the hub.
38
+ */ export function rendezvousTopic(recoverySecret) {
39
+ return deriveTopicID(recoverySecret, 0, RENDEZVOUS_LABEL);
29
40
  }
30
41
  /**
31
42
  * Public, secretless pre-group rendezvous topic (invite / keypackage / Welcome).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kumiai/rpc",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "license": "MIT",
5
5
  "description": "High-level MLS-aware group RPC for Enkaku: bus broadcast/anycast/gather plus directed RPC over the pub/sub hub",
6
6
  "keywords": [
@@ -26,18 +26,19 @@
26
26
  ],
27
27
  "sideEffects": false,
28
28
  "dependencies": {
29
- "@enkaku/client": "^0.18.0",
30
- "@sozai/codec": "^0.1.0",
31
- "@enkaku/protocol": "^0.18.0",
32
- "@enkaku/server": "^0.18.0",
33
- "@enkaku/transport": "^0.18.0",
29
+ "@enkaku/client": "^0.19.0",
30
+ "@sozai/codec": "^0.2.0",
31
+ "@enkaku/protocol": "^0.19.0",
32
+ "@enkaku/server": "^0.19.0",
33
+ "@enkaku/transport": "^0.19.1",
34
34
  "@noble/hashes": "^2.0.0",
35
- "@kumiai/broadcast": "^0.1.0",
36
- "@kumiai/hub-tunnel": "^0.1.0",
37
- "@kumiai/hub-protocol": "^0.1.0"
35
+ "@kumiai/broadcast": "^0.3.0",
36
+ "@kumiai/hub-tunnel": "^0.3.0",
37
+ "@kumiai/hub-protocol": "^0.3.0"
38
38
  },
39
39
  "devDependencies": {
40
- "vitest": "^4.1.9"
40
+ "@noble/curves": "^2.0.1",
41
+ "vitest": "^4.1.10"
41
42
  },
42
43
  "scripts": {
43
44
  "build:clean": "del lib",
@@ -1,21 +0,0 @@
1
- import type { GroupMLS } from './crypto.js';
2
- export type MemoryGroupMLS = GroupMLS & {
3
- epoch: () => number;
4
- commits: () => number;
5
- lastSender: () => string | undefined;
6
- };
7
- export type MemoryGroupMLSOptions = {
8
- recoverySecret?: Uint8Array;
9
- epoch?: number;
10
- /** Called whenever the modelled epoch advances (e.g. to keep a GroupCrypto in step). */
11
- onAdvance?: (epoch: number) => void;
12
- };
13
- /**
14
- * In-memory {@link GroupMLS} for exercising peer orchestration WITHOUT real MLS —
15
- * the group-rpc analogue of `createMemoryBus`. It models a single epoch counter:
16
- * a non-empty Commit advances it; GroupInfo carries the epoch so a stranded peer
17
- * can jump forward. The recovery secret is fixed for the instance's life
18
- * (epoch-independent). NOT real cryptography — a test double for wiring, not a
19
- * production implementation (a real port adapts a live MLS group).
20
- */
21
- export declare function createMemoryGroupMLS(options?: MemoryGroupMLSOptions): MemoryGroupMLS;
@@ -1,55 +0,0 @@
1
- /**
2
- * In-memory {@link GroupMLS} for exercising peer orchestration WITHOUT real MLS —
3
- * the group-rpc analogue of `createMemoryBus`. It models a single epoch counter:
4
- * a non-empty Commit advances it; GroupInfo carries the epoch so a stranded peer
5
- * can jump forward. The recovery secret is fixed for the instance's life
6
- * (epoch-independent). NOT real cryptography — a test double for wiring, not a
7
- * production implementation (a real port adapts a live MLS group).
8
- */ export function createMemoryGroupMLS(options = {}) {
9
- const recoverySecret = options.recoverySecret ?? new Uint8Array(32).fill(0x33);
10
- let epoch = options.epoch ?? 0;
11
- let commits = 0;
12
- let lastSender;
13
- const advance = (to)=>{
14
- epoch = to;
15
- options.onAdvance?.(epoch);
16
- };
17
- return {
18
- epoch: ()=>epoch,
19
- commits: ()=>commits,
20
- lastSender: ()=>lastSender,
21
- async processCommit (commit, context) {
22
- lastSender = context.senderDID;
23
- if (commit.length === 0) {
24
- return {
25
- advanced: false
26
- };
27
- }
28
- commits += 1;
29
- advance(epoch + 1);
30
- return {
31
- advanced: true
32
- };
33
- },
34
- async exportGroupInfo () {
35
- return Uint8Array.from([
36
- epoch
37
- ]);
38
- },
39
- async applyRecovery (groupInfo) {
40
- const target = groupInfo[0] ?? epoch;
41
- if (target <= epoch) {
42
- return {
43
- advanced: false
44
- };
45
- }
46
- advance(target);
47
- return {
48
- advanced: true
49
- };
50
- },
51
- exportRecoverySecret () {
52
- return recoverySecret;
53
- }
54
- };
55
- }