@kumiai/rpc 0.10.2 → 0.10.3

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.
@@ -0,0 +1,63 @@
1
+ import type { GroupMLS, OpenedRecoveryVerdict, PendingRecovery, RecoveryRefusalReason } from './crypto.js';
2
+ export type RecoveryCommitOutcome = {
3
+ commitDigest: string;
4
+ } & ({
5
+ kind: 'applied';
6
+ epoch: number;
7
+ key: Uint8Array;
8
+ } | {
9
+ kind: 'superseded';
10
+ } | {
11
+ kind: 'refused';
12
+ reason: RecoveryRefusalReason;
13
+ });
14
+ export declare function confirmationTag(key: Uint8Array, requestID: string): string;
15
+ export type RecoveryCacheEntry<TValue> = {
16
+ value: TValue;
17
+ expiresAt: number;
18
+ };
19
+ /** Expiry releases exporter keys even when the peer receives no further traffic. */
20
+ export declare function createRecoveryCache<TValue>(release?: (value: TValue) => void): {
21
+ delete: (key: string) => void;
22
+ get(key: string): RecoveryCacheEntry<TValue> | undefined;
23
+ set(key: string, value: TValue, expiresAt: number): void;
24
+ clear(): void;
25
+ };
26
+ export type ConfirmationOutcome = {
27
+ kind: 'confirmed';
28
+ position: string;
29
+ epoch: number;
30
+ } | {
31
+ kind: 'superseded';
32
+ } | {
33
+ kind: 'refused';
34
+ refusal: RecoveryRefusalReason;
35
+ responder: string;
36
+ } | {
37
+ kind: 'unconfirmed';
38
+ advisory: Array<OpenedRecoveryVerdict>;
39
+ } | {
40
+ kind: 'disposed';
41
+ } | {
42
+ kind: 'error';
43
+ error: unknown;
44
+ };
45
+ /** Routes verdicts for one requestID to its waiting attempt. */
46
+ export type ConfirmationWaiter = {
47
+ receive: (sealed: Uint8Array) => void;
48
+ dispose: () => void;
49
+ };
50
+ export type WaitForRecoveryConfirmationParams = {
51
+ port: GroupMLS;
52
+ pending: PendingRecovery;
53
+ groupID: string;
54
+ requestID: string;
55
+ position: string;
56
+ commitDigest: string;
57
+ key: Uint8Array;
58
+ deadline: number;
59
+ timeoutMs: number;
60
+ send: () => Promise<unknown>;
61
+ waiters: Map<string, ConfirmationWaiter>;
62
+ };
63
+ export declare function waitForRecoveryConfirmation(params: WaitForRecoveryConfirmationParams): Promise<ConfirmationOutcome>;
@@ -0,0 +1,132 @@
1
+ import { encodeMultibase } from '@kokuin/token';
2
+ import { hmac } from '@noble/hashes/hmac.js';
3
+ import { sha256 } from '@noble/hashes/sha2.js';
4
+ export function confirmationTag(key, requestID) {
5
+ return encodeMultibase(hmac(sha256, key, new TextEncoder().encode(requestID)));
6
+ }
7
+ /** Expiry releases exporter keys even when the peer receives no further traffic. */ export function createRecoveryCache(release = ()=>{}) {
8
+ const entries = new Map();
9
+ const remove = (key)=>{
10
+ const held = entries.get(key);
11
+ if (held == null) return;
12
+ clearTimeout(held.timer);
13
+ release(held.value);
14
+ entries.delete(key);
15
+ };
16
+ return {
17
+ delete: remove,
18
+ get (key) {
19
+ const held = entries.get(key);
20
+ if (held != null && held.expiresAt <= Date.now()) {
21
+ remove(key);
22
+ return;
23
+ }
24
+ return held;
25
+ },
26
+ set (key, value, expiresAt) {
27
+ remove(key);
28
+ if (expiresAt <= Date.now()) {
29
+ release(value);
30
+ return;
31
+ }
32
+ entries.set(key, {
33
+ value,
34
+ expiresAt,
35
+ timer: setTimeout(()=>remove(key), expiresAt - Date.now())
36
+ });
37
+ while(entries.size > 1024){
38
+ const oldest = entries.keys().next().value;
39
+ if (oldest != null) remove(oldest);
40
+ }
41
+ },
42
+ clear () {
43
+ for (const key of entries.keys())remove(key);
44
+ }
45
+ };
46
+ }
47
+ export function waitForRecoveryConfirmation(params) {
48
+ const { port, pending, groupID, requestID, position, commitDigest, key, deadline, timeoutMs, send, waiters } = params;
49
+ return new Promise((resolve)=>{
50
+ let finished = false;
51
+ let settle;
52
+ let best;
53
+ const advisory = new Map();
54
+ let opening = Promise.resolve();
55
+ const finish = (outcome)=>{
56
+ if (finished) return;
57
+ finished = true;
58
+ clearTimeout(end);
59
+ clearInterval(repeat);
60
+ if (settle != null) clearTimeout(settle);
61
+ waiters.delete(requestID);
62
+ key.fill(0);
63
+ resolve(outcome);
64
+ };
65
+ const ask = ()=>{
66
+ if (finished) return;
67
+ void send().catch((error)=>finish({
68
+ kind: 'error',
69
+ error
70
+ }));
71
+ };
72
+ const finishWithBest = ()=>{
73
+ finish(best ?? {
74
+ kind: 'unconfirmed',
75
+ advisory: [
76
+ ...advisory.values()
77
+ ]
78
+ });
79
+ };
80
+ const end = setTimeout(finishWithBest, Math.max(0, deadline - Date.now()));
81
+ const repeat = setInterval(ask, timeoutMs);
82
+ waiters.set(requestID, {
83
+ dispose: ()=>finish({
84
+ kind: 'disposed'
85
+ }),
86
+ receive: (sealed)=>{
87
+ opening = opening.then(async ()=>{
88
+ if (finished) return;
89
+ const opened = await port.openRecoveryVerdict(sealed, requestID);
90
+ if (finished || opened == null) return;
91
+ const verdict = opened.verdict;
92
+ const boundToAttempt = verdict.groupID === groupID && verdict.requestID === requestID && verdict.position === position && verdict.commitDigest === commitDigest;
93
+ if (!boundToAttempt) return;
94
+ if (verdict.verdict === 'confirmed' && (verdict.epoch !== pending.epoch || verdict.tag !== confirmationTag(key, requestID))) return;
95
+ if (pending.judgeVerdict(opened) === 'advisory') {
96
+ advisory.set(JSON.stringify(opened), opened);
97
+ if (advisory.size > 1024) {
98
+ const oldest = advisory.keys().next().value;
99
+ if (oldest != null) advisory.delete(oldest);
100
+ }
101
+ return;
102
+ }
103
+ if (verdict.verdict === 'confirmed') {
104
+ finish({
105
+ kind: 'confirmed',
106
+ position,
107
+ epoch: pending.epoch
108
+ });
109
+ return;
110
+ }
111
+ if (verdict.verdict === 'superseded') best = {
112
+ kind: 'superseded'
113
+ };
114
+ else if (best == null) best = {
115
+ kind: 'refused',
116
+ refusal: verdict.reason,
117
+ responder: opened.signer
118
+ };
119
+ if (settle == null) {
120
+ clearInterval(repeat);
121
+ settle = setTimeout(finishWithBest, Math.min(timeoutMs, Math.max(0, deadline - Date.now())));
122
+ ask();
123
+ }
124
+ }).catch((error)=>finish({
125
+ kind: 'error',
126
+ error
127
+ }));
128
+ }
129
+ });
130
+ ask();
131
+ });
132
+ }
package/lib/recovery.d.ts CHANGED
@@ -30,3 +30,17 @@ export declare function decodeLedgerReply(payload: Uint8Array): {
30
30
  requestID: string;
31
31
  sealed: Uint8Array;
32
32
  };
33
+ export type RecoveryConfirmRequest = {
34
+ requestID: string;
35
+ request: Uint8Array;
36
+ position: string;
37
+ commitDigest: string;
38
+ };
39
+ export declare function encodeRecoveryConfirmRequest(value: RecoveryConfirmRequest): Uint8Array;
40
+ export declare function decodeRecoveryConfirmRequest(payload: Uint8Array): RecoveryConfirmRequest;
41
+ export declare function encodeRecoveryVerdict(requestID: string, sealed: Uint8Array): Uint8Array;
42
+ export type RecoveryVerdictFrame = {
43
+ requestID: string;
44
+ sealed: Uint8Array;
45
+ };
46
+ export declare function decodeRecoveryVerdict(payload: Uint8Array): RecoveryVerdictFrame;
package/lib/recovery.js CHANGED
@@ -11,9 +11,11 @@ import { fromUTF, toUTF } from '@sozai/codec';
11
11
  * is public and secretless, so an unnamed request is one anyone can mint, and an unsealed reply
12
12
  * puts the group's whole authority state in the clear.
13
13
  */ /** Cap on decoded ID lengths — these become attacker-controlled map keys. */ const MAX_REQUEST_ID_BYTES = 128;
14
- function encodeWithRequestID(requestID, payload, label) {
14
+ /** Confirmation position and digest prefixes are bounded only by the u16 length field. */ const MAX_CONFIRMATION_FIELD_BYTES = 0xffff;
15
+ function encodeWithRequestID(params) {
16
+ const { requestID, payload, label, maxBytes = MAX_REQUEST_ID_BYTES } = params;
15
17
  const rid = fromUTF(requestID);
16
- if (rid.length > MAX_REQUEST_ID_BYTES) {
18
+ if (rid.length > maxBytes) {
17
19
  throw new Error(`${label} requestID is too long`);
18
20
  }
19
21
  const out = new Uint8Array(2 + rid.length + payload.length);
@@ -22,12 +24,12 @@ function encodeWithRequestID(requestID, payload, label) {
22
24
  out.set(payload, 2 + rid.length);
23
25
  return out;
24
26
  }
25
- function decodeWithRequestID(payload, label) {
27
+ function decodeWithRequestID(payload, label, maxBytes = MAX_REQUEST_ID_BYTES) {
26
28
  if (payload.length < 2) {
27
29
  throw new Error(`${label} is too short`);
28
30
  }
29
31
  const ridLen = new DataView(payload.buffer, payload.byteOffset, payload.byteLength).getUint16(0, true);
30
- if (ridLen > MAX_REQUEST_ID_BYTES) {
32
+ if (ridLen > maxBytes) {
31
33
  throw new Error(`${label} requestID is too long`);
32
34
  }
33
35
  if (payload.length < 2 + ridLen) {
@@ -39,7 +41,11 @@ function decodeWithRequestID(payload, label) {
39
41
  };
40
42
  }
41
43
  export function encodeRecoveryRequest(requestID, request) {
42
- return encodeWithRequestID(requestID, request, 'recovery request');
44
+ return encodeWithRequestID({
45
+ requestID,
46
+ payload: request,
47
+ label: 'recovery request'
48
+ });
43
49
  }
44
50
  export function decodeRecoveryRequest(payload) {
45
51
  const { requestID, rest } = decodeWithRequestID(payload, 'recovery request');
@@ -49,7 +55,11 @@ export function decodeRecoveryRequest(payload) {
49
55
  };
50
56
  }
51
57
  export function encodeRecoveryReply(requestID, groupInfo) {
52
- return encodeWithRequestID(requestID, groupInfo, 'recovery reply');
58
+ return encodeWithRequestID({
59
+ requestID,
60
+ payload: groupInfo,
61
+ label: 'recovery reply'
62
+ });
53
63
  }
54
64
  export function decodeRecoveryReply(payload) {
55
65
  const { requestID, rest } = decodeWithRequestID(payload, 'recovery reply');
@@ -63,7 +73,11 @@ export function decodeRecoveryReply(payload) {
63
73
  * what a responder authorizes against and the only key it will seal to. A request with no blob
64
74
  * is from nobody, and every responder refuses it.
65
75
  */ export function encodeLedgerRequest(requestID, request) {
66
- return encodeWithRequestID(requestID, request, 'ledger request');
76
+ return encodeWithRequestID({
77
+ requestID,
78
+ payload: request,
79
+ label: 'ledger request'
80
+ });
67
81
  }
68
82
  export function decodeLedgerRequest(payload) {
69
83
  const { requestID, rest } = decodeWithRequestID(payload, 'ledger request');
@@ -79,7 +93,11 @@ export function decodeLedgerRequest(payload) {
79
93
  * Order is inside the seal and load-bearing: the head is a chain digest, so a permuted list of
80
94
  * the same tokens folds to a different head and the requester rejects it.
81
95
  */ export function encodeLedgerReply(requestID, sealed) {
82
- return encodeWithRequestID(requestID, sealed, 'ledger reply');
96
+ return encodeWithRequestID({
97
+ requestID,
98
+ payload: sealed,
99
+ label: 'ledger reply'
100
+ });
83
101
  }
84
102
  export function decodeLedgerReply(payload) {
85
103
  const { requestID, rest } = decodeWithRequestID(payload, 'ledger reply');
@@ -88,3 +106,49 @@ export function decodeLedgerReply(payload) {
88
106
  sealed: rest
89
107
  };
90
108
  }
109
+ export function encodeRecoveryConfirmRequest(value) {
110
+ const label = 'recovery confirmation';
111
+ const digestPrefixed = encodeWithRequestID({
112
+ requestID: value.commitDigest,
113
+ payload: value.request,
114
+ label,
115
+ maxBytes: MAX_CONFIRMATION_FIELD_BYTES
116
+ });
117
+ const positionPrefixed = encodeWithRequestID({
118
+ requestID: value.position,
119
+ payload: digestPrefixed,
120
+ label,
121
+ maxBytes: MAX_CONFIRMATION_FIELD_BYTES
122
+ });
123
+ return encodeWithRequestID({
124
+ requestID: value.requestID,
125
+ payload: positionPrefixed,
126
+ label
127
+ });
128
+ }
129
+ export function decodeRecoveryConfirmRequest(payload) {
130
+ const outer = decodeWithRequestID(payload, 'recovery confirmation');
131
+ const position = decodeWithRequestID(outer.rest, 'recovery confirmation', MAX_CONFIRMATION_FIELD_BYTES);
132
+ const digest = decodeWithRequestID(position.rest, 'recovery confirmation', MAX_CONFIRMATION_FIELD_BYTES);
133
+ if (digest.rest.length === 0) throw new Error('recovery confirmation has no signed request');
134
+ return {
135
+ requestID: outer.requestID,
136
+ position: position.requestID,
137
+ commitDigest: digest.requestID,
138
+ request: digest.rest
139
+ };
140
+ }
141
+ export function encodeRecoveryVerdict(requestID, sealed) {
142
+ return encodeWithRequestID({
143
+ requestID,
144
+ payload: sealed,
145
+ label: 'recovery verdict'
146
+ });
147
+ }
148
+ export function decodeRecoveryVerdict(payload) {
149
+ const { requestID, rest } = decodeWithRequestID(payload, 'recovery verdict');
150
+ return {
151
+ requestID,
152
+ sealed: rest
153
+ };
154
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kumiai/rpc",
3
- "version": "0.10.2",
3
+ "version": "0.10.3",
4
4
  "description": "High-level MLS-aware group RPC for Enkaku: bus broadcast/anycast/gather plus directed RPC over the pub/sub hub",
5
5
  "keywords": [
6
6
  "rpc",
@@ -27,25 +27,25 @@
27
27
  ],
28
28
  "dependencies": {
29
29
  "@enkaku/client": "^0.21.3",
30
- "@enkaku/protocol": "^0.21.3",
30
+ "@enkaku/protocol": "^0.21.4",
31
31
  "@enkaku/server": "^0.21.3",
32
32
  "@enkaku/transport": "^0.21.3",
33
33
  "@kokuin/token": "^0.5.0",
34
34
  "@kumiai/broadcast": "^0.10.0",
35
- "@kumiai/hub-protocol": "^0.10.0",
36
- "@kumiai/hub-tunnel": "^0.10.0",
35
+ "@kumiai/hub-protocol": "^0.10.1",
36
+ "@kumiai/hub-tunnel": "^0.10.2",
37
37
  "@noble/hashes": "^2.4.0",
38
- "@sozai/codec": "^0.4.0",
39
- "@sozai/event": "^0.1.3",
40
- "@sozai/log": "^0.3.1",
38
+ "@sozai/codec": "^0.4.2",
39
+ "@sozai/event": "^0.1.5",
40
+ "@sozai/log": "^0.4.0",
41
41
  "@sozai/runtime": "^0.1.0",
42
- "@sozai/schema": "^0.1.2"
42
+ "@sozai/schema": "^0.1.6"
43
43
  },
44
44
  "devDependencies": {
45
- "@kumiai/hub-conformance": "^0.10.0",
46
- "@kumiai/rpc-conformance": "^0.10.0",
45
+ "@kumiai/hub-conformance": "^0.10.1",
46
+ "@kumiai/rpc-conformance": "^0.10.1",
47
47
  "@noble/curves": "^2.4.0",
48
- "vitest": "^5.0.2"
48
+ "vitest": "^5.0.3"
49
49
  },
50
50
  "publishConfig": {
51
51
  "access": "public"