@celljs/ta-relay 4.20.0 → 4.21.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 (76) hide show
  1. package/esm/common/index.d.ts +1 -0
  2. package/esm/common/index.js +1 -0
  3. package/esm/common/relay-account-client.d.ts +4 -0
  4. package/esm/common/relay-account-client.js +13 -3
  5. package/esm/common/relay-account.d.ts +17 -2
  6. package/esm/common/relay-account.js +15 -6
  7. package/esm/common/relay-accounts.d.ts +2 -0
  8. package/esm/common/relay-accounts.js +16 -2
  9. package/esm/common/relay-group-invites.d.ts +6 -2
  10. package/esm/common/relay-group-invites.js +6 -5
  11. package/esm/common/relay-hub-charter.d.ts +74 -0
  12. package/esm/common/relay-hub-charter.js +149 -0
  13. package/esm/common/relay-hub-epochs.d.ts +43 -0
  14. package/esm/common/relay-hub-epochs.js +92 -0
  15. package/esm/common/relay-hub-util.d.ts +39 -7
  16. package/esm/common/relay-hub-util.js +46 -15
  17. package/esm/common/relay-hub.d.ts +13 -0
  18. package/esm/common/relay-hub.js +81 -18
  19. package/esm/common/relay-hubs-admins.d.ts +27 -0
  20. package/esm/common/relay-hubs-admins.js +91 -0
  21. package/esm/common/relay-hubs-connection.d.ts +19 -63
  22. package/esm/common/relay-hubs-connection.js +100 -87
  23. package/esm/common/relay-hubs-identity.d.ts +7 -1
  24. package/esm/common/relay-hubs-identity.js +28 -1
  25. package/esm/common/relay-hubs-live.d.ts +58 -0
  26. package/esm/common/relay-hubs-live.js +27 -0
  27. package/esm/common/relay-hubs-protocol.d.ts +10 -0
  28. package/esm/common/relay-hubs-util.d.ts +9 -0
  29. package/esm/common/relay-hubs-util.js +23 -1
  30. package/esm/common/relay-hubs.d.ts +12 -15
  31. package/esm/common/relay-hubs.js +60 -57
  32. package/esm/common/relay-protocol.d.ts +14 -3
  33. package/esm/common/relay-protocol.js +1 -1
  34. package/esm/common/relay-room-chat.d.ts +6 -1
  35. package/esm/common/relay-room-chat.js +1 -0
  36. package/esm/node/relay-server-accounts.d.ts +1 -1
  37. package/esm/node/relay-server-accounts.js +1 -1
  38. package/lib/common/index.d.ts +1 -0
  39. package/lib/common/index.js +1 -0
  40. package/lib/common/relay-account-client.d.ts +4 -0
  41. package/lib/common/relay-account-client.js +13 -3
  42. package/lib/common/relay-account.d.ts +17 -2
  43. package/lib/common/relay-account.js +15 -6
  44. package/lib/common/relay-accounts.d.ts +2 -0
  45. package/lib/common/relay-accounts.js +16 -2
  46. package/lib/common/relay-group-invites.d.ts +6 -2
  47. package/lib/common/relay-group-invites.js +6 -5
  48. package/lib/common/relay-hub-charter.d.ts +75 -0
  49. package/lib/common/relay-hub-charter.js +152 -0
  50. package/lib/common/relay-hub-epochs.d.ts +44 -0
  51. package/lib/common/relay-hub-epochs.js +95 -0
  52. package/lib/common/relay-hub-util.d.ts +39 -7
  53. package/lib/common/relay-hub-util.js +46 -15
  54. package/lib/common/relay-hub.d.ts +13 -0
  55. package/lib/common/relay-hub.js +81 -18
  56. package/lib/common/relay-hubs-admins.d.ts +28 -0
  57. package/lib/common/relay-hubs-admins.js +95 -0
  58. package/lib/common/relay-hubs-connection.d.ts +19 -63
  59. package/lib/common/relay-hubs-connection.js +106 -94
  60. package/lib/common/relay-hubs-identity.d.ts +7 -1
  61. package/lib/common/relay-hubs-identity.js +28 -1
  62. package/lib/common/relay-hubs-live.d.ts +59 -0
  63. package/lib/common/relay-hubs-live.js +31 -0
  64. package/lib/common/relay-hubs-protocol.d.ts +10 -0
  65. package/lib/common/relay-hubs-util.d.ts +9 -0
  66. package/lib/common/relay-hubs-util.js +23 -1
  67. package/lib/common/relay-hubs.d.ts +12 -15
  68. package/lib/common/relay-hubs.js +66 -63
  69. package/lib/common/relay-protocol.d.ts +14 -3
  70. package/lib/common/relay-protocol.js +1 -1
  71. package/lib/common/relay-room-chat.d.ts +6 -1
  72. package/lib/common/relay-room-chat.js +1 -0
  73. package/lib/node/relay-server-accounts.d.ts +1 -1
  74. package/lib/node/relay-server-accounts.js +1 -1
  75. package/package.json +3 -3
  76. package/viewer/room.html +15 -15
@@ -0,0 +1,92 @@
1
+ import { RelayHubUtil } from './relay-hub-util.js';
2
+ import { RelayIdentityUtil, RelayKeys } from './relay-identity.js';
3
+ /** The epochs a Ta keeps the keys of, the latest: what was sealed in one before them is no longer read. */
4
+ export const RELAY_HUB_KEEP_EPOCHS = 8;
5
+ /**
6
+ * A new key for a room once a member is taken out or leaves (docs/design/ta-link.md 8.7). The one who writes the roster
7
+ * makes it and seals the keys of the latest epochs for each member left, alone (ECDH of the two, salted by the room and
8
+ * the epoch): `roster.k` beside the roster sealed with the new key. The one gone holds the keys of before, never this.
9
+ */
10
+ export var RelayHubEpochs;
11
+ (function (RelayHubEpochs) {
12
+ /** The epochs kept: the latest RELAY_HUB_KEEP_EPOCHS. */
13
+ function kept(epochs) {
14
+ const numbers = Object.keys(epochs)
15
+ .map(Number)
16
+ .filter(n => Number.isInteger(n) && n > 0)
17
+ .sort((a, b) => b - a)
18
+ .slice(0, RELAY_HUB_KEEP_EPOCHS);
19
+ return Object.fromEntries(numbers.map(n => [String(n), epochs[String(n)]]));
20
+ }
21
+ RelayHubEpochs.kept = kept;
22
+ /** The epoch a text of RelayHubUtil.sealIn is of. */
23
+ function epochOf(sealed) {
24
+ const dot = String(sealed).indexOf('.');
25
+ const epoch = dot > 0 ? Number(String(sealed).slice(0, dot)) : 0;
26
+ return Number.isInteger(epoch) && epoch >= 0 ? epoch : -1;
27
+ }
28
+ RelayHubEpochs.epochOf = epochOf;
29
+ function latest(epochs) {
30
+ return Math.max(0, ...Object.keys(epochs !== null && epochs !== void 0 ? epochs : {})
31
+ .map(Number)
32
+ .filter(Number.isInteger));
33
+ }
34
+ RelayHubEpochs.latest = latest;
35
+ /** The epochs with one more, its key new. */
36
+ function next(epochs) {
37
+ return kept({ ...epochs, [String(latest(epochs) + 1)]: RelayHubUtil.randomKey() });
38
+ }
39
+ RelayHubEpochs.next = next;
40
+ async function keyOf(identity, otherBox, room, epoch) {
41
+ return RelayKeys.aesKey(await RelayIdentityUtil.agree(identity, otherBox, `${room}:${epoch}`, 'ta-room/1 epoch'));
42
+ }
43
+ /**
44
+ * What each of `members` reads the keys by, sealed by `identity` as `sealer` (its member id):
45
+ * `<sealer>.<its box>.<sealed>` by member id, the one who opens it checking the sealer against the roster. `cache` keeps those made for this epoch, so that writing the roster again does not agree keys again.
46
+ */
47
+ async function seal(identity, sealer, room, epochs, members, cache) {
48
+ const epoch = latest(epochs);
49
+ const text = JSON.stringify(kept(epochs));
50
+ const out = {};
51
+ for (const member of members) {
52
+ const id = `${epoch}:${sealer}:${member.member}`;
53
+ let sealed = cache.get(id);
54
+ if (!sealed) {
55
+ sealed = `${sealer}.${identity.box}.${await RelayKeys.seal(await keyOf(identity, member.box, room, epoch), text)}`;
56
+ cache.set(id, sealed);
57
+ }
58
+ out[member.member] = sealed;
59
+ }
60
+ return out;
61
+ }
62
+ RelayHubEpochs.seal = seal;
63
+ /**
64
+ * The keys sealed for this Ta (`self`) in `k`, with who sealed them (its member id and box), to be checked against
65
+ * the roster they come with (`sealedBy`); undefined when none is for it, or it does not open.
66
+ */
67
+ async function open(identity, self, room, epoch, k) {
68
+ const parts = typeof (k === null || k === void 0 ? void 0 : k[self]) === 'string' ? k[self].split('.') : [];
69
+ if (parts.length !== 3 || epoch < 1) {
70
+ return undefined;
71
+ }
72
+ const [member, box, sealed] = parts;
73
+ try {
74
+ const epochs = JSON.parse(await RelayKeys.open(await keyOf(identity, box, room, epoch), sealed));
75
+ const valid = Object.fromEntries(Object.entries(epochs).filter(([n, key]) => Number.isInteger(Number(n)) && Number(n) > 0 && typeof key === 'string' && /^[A-Za-z0-9_-]{43}$/.test(key)));
76
+ return valid[String(epoch)] ? { epochs: valid, sealer: { member, box } } : undefined;
77
+ }
78
+ catch (_a) {
79
+ return undefined;
80
+ }
81
+ }
82
+ RelayHubEpochs.open = open;
83
+ /**
84
+ * Whether the one who sealed the keys of a new epoch may: it signed the roster they came with (the owner, or an admin
85
+ * its charter names), with the box the roster gives it. Anyone else, the relay among them, gives no key.
86
+ */
87
+ function sealedBy(sealer, roster) {
88
+ return roster.signer === sealer.member && roster.body.members.some(member => member.member === sealer.member && member.box === sealer.box);
89
+ }
90
+ RelayHubEpochs.sealedBy = sealedBy;
91
+ })(RelayHubEpochs || (RelayHubEpochs = {}));
92
+ //# sourceMappingURL=relay-hub-epochs.js.map
@@ -1,3 +1,4 @@
1
+ import { type RelayHubCharter } from './relay-hub-charter.js';
1
2
  import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity.js';
2
3
  /** What a link inviting into a room names (docs/design/ta-link.md 3.3); all of it in the fragment, which a browser does not send. */
3
4
  export interface RelayHubLink {
@@ -5,7 +6,7 @@ export interface RelayHubLink {
5
6
  room: string;
6
7
  /** base64url, 32 bytes: what an ask proves and is sealed with. */
7
8
  invite: string;
8
- /** The identity id of the owner: the answer must be signed by it. */
9
+ /** The identity id of the owner: the answer must be signed by it, or by an admin it named (RelayHubCharterUtil). */
9
10
  owner: string;
10
11
  title: string;
11
12
  /** The owner's name, as they gave it. */
@@ -28,6 +29,8 @@ export interface RelayHubMember extends RelayPublicIdentity {
28
29
  export interface RelayHubRosterBody {
29
30
  v: number;
30
31
  members: RelayHubMember[];
32
+ /** Who the admins are and the rules, signed by the owner (docs/design/ta-link.md 8); none in a room of before. */
33
+ charter?: RelayHubCharter;
31
34
  }
32
35
  /** What one asking to join says of itself, sealed with the key of the invitation and signed. */
33
36
  export interface RelayHubAskBody extends RelayPublicIdentity {
@@ -39,6 +42,8 @@ export interface RelayHubAskBody extends RelayPublicIdentity {
39
42
  /** What the owner gives one it admits, sealed for it alone: the key of the room, and what the room is. */
40
43
  export interface RelayHubAnswerBody {
41
44
  key: string;
45
+ /** The keys of the epochs after the first, by number, those kept (docs/design/ta-link.md 8.7). */
46
+ epochs?: Record<string, string>;
42
47
  kind: string;
43
48
  title: string;
44
49
  view?: string;
@@ -51,10 +56,16 @@ export interface RelayHubSigned<T> {
51
56
  at: number;
52
57
  b: T;
53
58
  }
54
- /** The keys of a room, made from its key `R` once. */
59
+ /**
60
+ * The keys of a room, made from its key `R` once: the names of its state always from `R`; what is sealed, from the key
61
+ * of the epoch it is in (docs/design/ta-link.md 8.7), the latest (`epoch`) for what is written now.
62
+ */
55
63
  export interface RelayHubKeys {
56
64
  seal: CryptoKey;
57
65
  names: Uint8Array;
66
+ epoch: number;
67
+ /** The seal key of each epoch this Ta knows, by its number; 0 is that of `R`. */
68
+ ring: Map<number, CryptoKey>;
58
69
  }
59
70
  /** Links, keys, sealing and signatures of rooms (docs/design/ta-link.md 3.2, 3.3). */
60
71
  export declare namespace RelayHubUtil {
@@ -67,7 +78,24 @@ export declare namespace RelayHubUtil {
67
78
  function parse(text: string): RelayHubLink;
68
79
  function randomId(): string;
69
80
  function randomKey(): string;
70
- function keys(roomKey: string): Promise<RelayHubKeys>;
81
+ /** The keys of a room of key `roomKey` (epoch 0), and of the epochs after it this Ta has (RelayRoomRecord.epochs). */
82
+ function keys(roomKey: string, epochs?: Record<string, string>): Promise<RelayHubKeys>;
83
+ /**
84
+ * Sealed with the key of the latest epoch: as {@link seal} in epoch 0, which a Ta of before reads; `<epoch>.<sealed>`
85
+ * after (base64url has no dot).
86
+ */
87
+ function sealIn<T>(keys: RelayHubKeys, identity: RelayIdentity, member: string, body: T, at?: number): Promise<string>;
88
+ /** The key a text of {@link sealIn} was sealed with, and the text without its epoch; none for an epoch this Ta lacks. */
89
+ function keyFor(keys: RelayHubKeys, sealed: string): {
90
+ key: CryptoKey;
91
+ sealed: string;
92
+ epoch: number;
93
+ } | undefined;
94
+ /** What {@link sealIn} sealed, checked as {@link open}. */
95
+ function openIn<T>(keys: RelayHubKeys, sealed: string, signOf: (member: string) => string | undefined, options?: {
96
+ now?: number;
97
+ skewMs?: number;
98
+ }): Promise<RelayHubSigned<T> | undefined>;
71
99
  /** What the relay knows a key of the state by: HMAC of its name, 22 characters. */
72
100
  function keyId(keys: RelayHubKeys, name: string): Promise<string>;
73
101
  /** What proves an ask comes from one with the invitation; the relay keeps its {@link RelayKeys.hash}. */
@@ -88,11 +116,15 @@ export declare namespace RelayHubUtil {
88
116
  /** Undefined for an ask that does not open with this invitation, or is not signed by the keys it gives. */
89
117
  function openAsk(invite: string, sealed: string): Promise<RelayHubAskBody | undefined>;
90
118
  /**
91
- * The owner's answer to an ask: what the room is and its key, for the one who asked alone (ECDH of the two), signed by
92
- * the owner; all of it sealed with the invitation the ask came with, so that the relay does not see who the owner is.
119
+ * The answer to an ask: what the room is and its key, for the one who asked alone (ECDH of the two), signed by the
120
+ * owner, or by an admin with `proof` that the owner named it; all of it sealed with the invitation the ask came with,
121
+ * so that the relay does not see who answered.
93
122
  */
94
- function sealAnswer(owner: RelayIdentity, room: string, ask: RelayHubAskBody, body: RelayHubAnswerBody, invite: string): Promise<string>;
95
- /** Undefined unless it is signed by the owner the link names, and opens for this identity. */
123
+ function sealAnswer(owner: RelayIdentity, room: string, ask: RelayHubAskBody, body: RelayHubAnswerBody, invite: string, proof?: {
124
+ head: RelayPublicIdentity;
125
+ charter: RelayHubCharter;
126
+ }): Promise<string>;
127
+ /** Undefined unless it is signed by the owner the link names (or an admin it named), and opens for this identity; `owner` is the owner. */
96
128
  function openAnswer(identity: RelayIdentity, room: string, member: string, ownerId: string, answer: string, invite: string): Promise<{
97
129
  owner: RelayPublicIdentity;
98
130
  body: RelayHubAnswerBody;
@@ -1,4 +1,5 @@
1
1
  import { RelayBytes } from './relay-bytes.js';
2
+ import { RelayHubCharterUtil } from './relay-hub-charter.js';
2
3
  import { RelayIdentityUtil, RelayKeys } from './relay-identity.js';
3
4
  import { RELAY_ID, RELAY_ROOM_PATH } from './relay-protocol.js';
4
5
  /** Links, keys, sealing and signatures of rooms (docs/design/ta-link.md 3.2, 3.3). */
@@ -62,15 +63,45 @@ export var RelayHubUtil;
62
63
  return RelayBytes.toBase64(RelayBytes.random(32));
63
64
  }
64
65
  RelayHubUtil.randomKey = randomKey;
65
- async function keys(roomKey) {
66
+ /** The keys of a room of key `roomKey` (epoch 0), and of the epochs after it this Ta has (RelayRoomRecord.epochs). */
67
+ async function keys(roomKey, epochs = {}) {
66
68
  const key = RelayBytes.fromBase64(roomKey);
67
69
  const none = new Uint8Array(0);
68
- return {
69
- seal: await RelayKeys.aesKey(await RelayKeys.hkdf(key, none, 'ta-room/1 seal')),
70
- names: await RelayKeys.hkdf(key, none, 'ta-room/1 key'),
71
- };
70
+ const sealOf = async (value) => RelayKeys.aesKey(await RelayKeys.hkdf(value, none, 'ta-room/1 seal'));
71
+ const ring = new Map([[0, await sealOf(key)]]);
72
+ for (const [epoch, value] of Object.entries(epochs)) {
73
+ const n = Number(epoch);
74
+ if (Number.isInteger(n) && n > 0) {
75
+ ring.set(n, await sealOf(RelayBytes.fromBase64(value)));
76
+ }
77
+ }
78
+ const epoch = Math.max(...ring.keys());
79
+ return { seal: ring.get(epoch), names: await RelayKeys.hkdf(key, none, 'ta-room/1 key'), epoch, ring };
72
80
  }
73
81
  RelayHubUtil.keys = keys;
82
+ /**
83
+ * Sealed with the key of the latest epoch: as {@link seal} in epoch 0, which a Ta of before reads; `<epoch>.<sealed>`
84
+ * after (base64url has no dot).
85
+ */
86
+ async function sealIn(keys, identity, member, body, at = Date.now()) {
87
+ const sealed = await seal(keys.seal, identity, member, body, at);
88
+ return keys.epoch ? `${keys.epoch}.${sealed}` : sealed;
89
+ }
90
+ RelayHubUtil.sealIn = sealIn;
91
+ /** The key a text of {@link sealIn} was sealed with, and the text without its epoch; none for an epoch this Ta lacks. */
92
+ function keyFor(keys, sealed) {
93
+ const dot = sealed.indexOf('.');
94
+ const epoch = dot > 0 ? Number(sealed.slice(0, dot)) : 0;
95
+ const key = Number.isInteger(epoch) ? keys.ring.get(epoch) : undefined;
96
+ return key && { key, sealed: dot > 0 ? sealed.slice(dot + 1) : sealed, epoch };
97
+ }
98
+ RelayHubUtil.keyFor = keyFor;
99
+ /** What {@link sealIn} sealed, checked as {@link open}. */
100
+ async function openIn(keys, sealed, signOf, options = {}) {
101
+ const found = keyFor(keys, String(sealed));
102
+ return found ? open(found.key, found.sealed, signOf, options) : undefined;
103
+ }
104
+ RelayHubUtil.openIn = openIn;
74
105
  /** What the relay knows a key of the state by: HMAC of its name, 22 characters. */
75
106
  async function keyId(keys, name) {
76
107
  return (await RelayKeys.hmac(keys.names, name)).slice(0, 22);
@@ -147,30 +178,30 @@ export var RelayHubUtil;
147
178
  }
148
179
  RelayHubUtil.openAsk = openAsk;
149
180
  /**
150
- * The owner's answer to an ask: what the room is and its key, for the one who asked alone (ECDH of the two), signed by
151
- * the owner; all of it sealed with the invitation the ask came with, so that the relay does not see who the owner is.
181
+ * The answer to an ask: what the room is and its key, for the one who asked alone (ECDH of the two), signed by the
182
+ * owner, or by an admin with `proof` that the owner named it; all of it sealed with the invitation the ask came with,
183
+ * so that the relay does not see who answered.
152
184
  */
153
- async function sealAnswer(owner, room, ask, body, invite) {
185
+ async function sealAnswer(owner, room, ask, body, invite, proof) {
154
186
  const key = await RelayKeys.aesKey(await RelayIdentityUtil.agree(owner, ask.box, room, 'ta-room/1 answer'));
155
187
  const d = await RelayKeys.seal(key, JSON.stringify(body));
156
188
  const signed = `${room}\n${ask.member}\n${d}`;
157
- const answer = JSON.stringify({ owner: RelayIdentityUtil.publicOf(owner), d, s: await RelayIdentityUtil.sign(owner, signed) });
189
+ const answer = JSON.stringify({ owner: RelayIdentityUtil.publicOf(owner), d, s: await RelayIdentityUtil.sign(owner, signed), ...(proof !== null && proof !== void 0 ? proof : {}) });
158
190
  return RelayKeys.seal(await inviteKey(invite), answer);
159
191
  }
160
192
  RelayHubUtil.sealAnswer = sealAnswer;
161
- /** Undefined unless it is signed by the owner the link names, and opens for this identity. */
193
+ /** Undefined unless it is signed by the owner the link names (or an admin it named), and opens for this identity; `owner` is the owner. */
162
194
  async function openAnswer(identity, room, member, ownerId, answer, invite) {
163
195
  try {
164
- const { owner, d, s } = JSON.parse(await RelayKeys.open(await inviteKey(invite), answer));
165
- if (owner.id !== ownerId ||
166
- !(await RelayIdentityUtil.check(owner)) ||
167
- !(await RelayIdentityUtil.verify(owner.sign, `${room}\n${member}\n${d}`, s))) {
196
+ const { owner, d, s, head, charter } = JSON.parse(await RelayKeys.open(await inviteKey(invite), answer));
197
+ const named = await RelayHubCharterUtil.answeredBy(owner, ownerId, room, { head, charter });
198
+ if (!named || !(await RelayIdentityUtil.verify(owner.sign, `${room}\n${member}\n${d}`, s))) {
168
199
  return undefined;
169
200
  }
170
201
  const key = await RelayKeys.aesKey(await RelayIdentityUtil.agree(identity, owner.box, room, 'ta-room/1 answer'));
171
202
  const body = JSON.parse(await RelayKeys.open(key, d));
172
203
  RelayBytes.fromBase64(body.key);
173
- return { owner: { ...RelayIdentityUtil.publicOf(owner), name: String(owner.name).slice(0, 100) }, body };
204
+ return { owner: { ...RelayIdentityUtil.publicOf(named), name: String(named.name).slice(0, 100) }, body };
174
205
  }
175
206
  catch (_a) {
176
207
  return undefined;
@@ -47,6 +47,13 @@ interface HubMeta {
47
47
  hash: string;
48
48
  expiresAt: number;
49
49
  };
50
+ /** The members the owner made admins (docs/design/ta-link.md 8): they let in, take out and invite, as the owner. */
51
+ admins?: string[];
52
+ /** The invitation of each admin, by its member id: one of each, beside the owner's. */
53
+ invites?: Record<string, {
54
+ hash: string;
55
+ expiresAt: number;
56
+ }>;
50
57
  keys: number;
51
58
  size: number;
52
59
  }
@@ -103,6 +110,12 @@ export declare class RelayHub {
103
110
  t: 'admit';
104
111
  }>): Promise<void>;
105
112
  protected setRoster(socket: RelayHubSocket, roster: RelayHubRoster): Promise<void>;
113
+ /** The keys of a roster sealed for each member (RelayHubRoster.k): undefined for none, null for what is not that. */
114
+ protected static epochKeys(k: unknown): Record<string, string> | undefined | null;
115
+ /** Whether a member lets in, takes out and invites: the owner, or an admin it made. */
116
+ protected static manages(meta: HubMeta, member: string): boolean;
117
+ /** One gone is no admin, its invitation with it. */
118
+ protected unmake(meta: HubMeta, member: string): Promise<void>;
106
119
  protected closeRoom(): Promise<void>;
107
120
  /** The asks still waiting for the owner, those past their time dropped. */
108
121
  protected asks(): Promise<HubAsks>;
@@ -1,3 +1,4 @@
1
+ import { RELAY_HUB_MAX_ADMINS } from './relay-hub-charter.js';
1
2
  import { RelayKeys } from './relay-identity.js';
2
3
  import { RELAY_HUB_ASK_DAYS, RELAY_HUB_IDLE_DAYS, RELAY_HUB_MAX_ASKS, RELAY_HUB_MAX_KEYS, RELAY_HUB_MAX_MEMBERS, RELAY_HUB_MAX_STATE, RELAY_HUB_MAX_VALUE, RELAY_HUB_RATE, RELAY_ID, RELAY_INVITE_MAX_DAYS, RELAY_PING, RELAY_PONG, } from './relay-protocol.js';
3
4
  /** Close codes of a hub. */
@@ -126,8 +127,7 @@ export class RelayHub {
126
127
  const wasOnline = this.online(member, socket);
127
128
  socket.who = { member };
128
129
  await this.touch(meta);
129
- const owner = member === meta.owner;
130
- const asks = owner ? await this.pendingAsks() : undefined;
130
+ const asks = RelayHub.manages(meta, member) ? await this.pendingAsks() : undefined;
131
131
  const roster = await this.storage.get('roster');
132
132
  const online = [...new Set(this.members().map(other => other.who.member))];
133
133
  this.send(socket, { t: 'welcome', owner: meta.owner, online, ...(roster ? { roster } : {}), ...(asks ? { asks } : {}) });
@@ -151,7 +151,7 @@ export class RelayHub {
151
151
  }
152
152
  }
153
153
  async ask(socket, request) {
154
- var _a;
154
+ var _a, _b, _c;
155
155
  const meta = await this.storage.get('meta');
156
156
  if (!meta) {
157
157
  this.refuse(socket, 'gone', 'No such room.', RelayHubCloseCode.GONE);
@@ -162,12 +162,13 @@ export class RelayHub {
162
162
  return;
163
163
  }
164
164
  const now = this.now();
165
- if (!meta.invite || meta.invite.expiresAt <= now || meta.invite.hash !== (await RelayKeys.hash(String(request.proof)))) {
165
+ const proof = await RelayKeys.hash(String(request.proof));
166
+ if (![meta.invite, ...Object.values((_a = meta.invites) !== null && _a !== void 0 ? _a : {})].some(invite => invite && invite.expiresAt > now && invite.hash === proof)) {
166
167
  this.refuse(socket, 'invite', 'This invitation no longer works.', RelayHubCloseCode.REFUSED);
167
168
  return;
168
169
  }
169
170
  const asks = await this.asks();
170
- const members = (_a = (await this.storage.get('members'))) !== null && _a !== void 0 ? _a : {};
171
+ const members = (_b = (await this.storage.get('members'))) !== null && _b !== void 0 ? _b : {};
171
172
  if (asks[request.ask] || members[request.ask]) {
172
173
  this.refuse(socket, 'exists', 'Asked already.', RelayHubCloseCode.REFUSED);
173
174
  return;
@@ -180,8 +181,10 @@ export class RelayHub {
180
181
  await this.storage.put('asks', asks);
181
182
  socket.who = { ask: request.ask };
182
183
  this.send(socket, { t: 'waiting' });
183
- for (const owner of this.members(meta.owner)) {
184
- this.send(owner, { t: 'asked', ask: { ask: request.ask, d: request.d, at: now } });
184
+ for (const manager of [meta.owner, ...((_c = meta.admins) !== null && _c !== void 0 ? _c : [])]) {
185
+ for (const socket of this.members(manager)) {
186
+ this.send(socket, { t: 'asked', ask: { ask: request.ask, d: request.d, at: now } });
187
+ }
185
188
  }
186
189
  }
187
190
  async wait(socket, ask, token) {
@@ -216,9 +219,10 @@ export class RelayHub {
216
219
  socket.close(RelayHubCloseCode.ANSWERED, 'Answered.');
217
220
  }
218
221
  async fromMember(socket, member, request) {
219
- var _a;
222
+ var _a, _b;
223
+ var _c, _d;
220
224
  const meta = await this.storage.get('meta');
221
- const members = (_a = (await this.storage.get('members'))) !== null && _a !== void 0 ? _a : {};
225
+ const members = (_c = (await this.storage.get('members'))) !== null && _c !== void 0 ? _c : {};
222
226
  if (!meta || !members[member]) {
223
227
  this.refuse(socket, 'gone', 'Not in this room any more.', RelayHubCloseCode.REMOVED);
224
228
  return;
@@ -235,25 +239,44 @@ export class RelayHub {
235
239
  }
236
240
  delete members[member];
237
241
  await this.storage.put('members', members);
242
+ await this.unmake(meta, member);
238
243
  for (const other of this.members(member)) {
239
244
  other.close(RelayHubCloseCode.REMOVED, 'Left the room.');
240
245
  }
241
246
  this.broadcast({ t: 'left', member });
242
247
  return;
243
248
  }
244
- if (!owner) {
249
+ if (!RelayHub.manages(meta, member) || ((request.t === 'admins' || request.t === 'close') && !owner)) {
245
250
  this.send(socket, { t: 'error', code: 'owner', message: 'Only the owner does that.' });
246
251
  return;
247
252
  }
248
253
  switch (request.t) {
249
- case 'invite':
254
+ case 'admins': {
255
+ const admins = Array.isArray(request.admins) ? request.admins : [];
256
+ meta.admins = [...new Set(admins.map(String))].filter(id => id !== meta.owner && !!members[id]).slice(0, RELAY_HUB_MAX_ADMINS);
257
+ for (const id of Object.keys((_d = meta.invites) !== null && _d !== void 0 ? _d : {})) {
258
+ if (!meta.admins.includes(id)) {
259
+ (_a = meta.invites) === null || _a === void 0 ? true : delete _a[id];
260
+ }
261
+ }
262
+ await this.storage.put('meta', meta);
263
+ return;
264
+ }
265
+ case 'invite': {
250
266
  if (!/^[A-Za-z0-9_-]{43}$/.test(String(request.hash)) || !(request.expiresAt > this.now())) {
251
267
  this.send(socket, { t: 'error', code: 'invalid', message: 'An invitation needs a hash and a time.' });
252
268
  return;
253
269
  }
254
- meta.invite = { hash: request.hash, expiresAt: Math.min(request.expiresAt, this.now() + RELAY_INVITE_MAX_DAYS * DAY_MS) };
270
+ const invite = { hash: request.hash, expiresAt: Math.min(request.expiresAt, this.now() + RELAY_INVITE_MAX_DAYS * DAY_MS) };
271
+ if (owner) {
272
+ meta.invite = invite;
273
+ }
274
+ else {
275
+ meta.invites = { ...meta.invites, [member]: invite };
276
+ }
255
277
  await this.storage.put('meta', meta);
256
278
  return;
279
+ }
257
280
  case 'admit':
258
281
  return this.admit(socket, members, request);
259
282
  case 'refuse': {
@@ -274,8 +297,13 @@ export class RelayHub {
274
297
  if (request.member === member || !members[request.member]) {
275
298
  return;
276
299
  }
300
+ if (request.member === meta.owner || (!owner && ((_b = meta.admins) === null || _b === void 0 ? void 0 : _b.includes(request.member)))) {
301
+ this.send(socket, { t: 'error', code: 'owner', message: 'Only the owner does that.' });
302
+ return;
303
+ }
277
304
  delete members[request.member];
278
305
  await this.storage.put('members', members);
306
+ await this.unmake(meta, request.member);
279
307
  for (const other of this.members(request.member)) {
280
308
  this.refuse(other, 'auth', 'Removed by the owner.', RelayHubCloseCode.REMOVED);
281
309
  }
@@ -298,7 +326,7 @@ export class RelayHub {
298
326
  }
299
327
  }
300
328
  async set(socket, member, meta, request) {
301
- var _a, _b;
329
+ var _a, _b, _c;
302
330
  const id = Number(request.id);
303
331
  const done = (ok, v, error) => this.send(socket, { t: 'done', id, ok, v, ...(error ? { error } : {}) });
304
332
  if (typeof request.k !== 'string' || !/^[A-Za-z0-9_-]{1,64}$/.test(request.k) || typeof request.d !== 'string') {
@@ -312,17 +340,23 @@ export class RelayHub {
312
340
  const key = STATE + request.k;
313
341
  const before = await this.storage.get(key);
314
342
  const version = (_a = before === null || before === void 0 ? void 0 : before.v) !== null && _a !== void 0 ? _a : 0;
343
+ // What a member keeps as its own (its lines, its posts) no other member replaces or clears: the owner alone may.
344
+ if ((before === null || before === void 0 ? void 0 : before.own) && before.by !== member && member !== meta.owner && ((_b = (await this.storage.get('members'))) !== null && _b !== void 0 ? _b : {})[before.by]) {
345
+ done(false, version, 'owned');
346
+ return;
347
+ }
315
348
  if (request.v !== undefined && request.v !== version) {
316
349
  done(false, version);
317
350
  return;
318
351
  }
319
352
  const keys = meta.keys + (before ? 0 : 1);
320
- const size = meta.size - ((_b = before === null || before === void 0 ? void 0 : before.d.length) !== null && _b !== void 0 ? _b : 0) + request.d.length;
353
+ const size = meta.size - ((_c = before === null || before === void 0 ? void 0 : before.d.length) !== null && _c !== void 0 ? _c : 0) + request.d.length;
321
354
  if (keys > RELAY_HUB_MAX_KEYS || size > RELAY_HUB_MAX_STATE) {
322
355
  done(false, version, 'limit');
323
356
  return;
324
357
  }
325
- const entry = { k: request.k, v: version + 1, d: request.d, by: member };
358
+ const own = request.own === true || ((before === null || before === void 0 ? void 0 : before.own) === true && before.by === member);
359
+ const entry = { k: request.k, v: version + 1, d: request.d, by: member, ...(own ? { own: true } : {}) };
326
360
  await this.storage.put(key, entry);
327
361
  meta.keys = keys;
328
362
  meta.size = size;
@@ -354,12 +388,41 @@ export class RelayHub {
354
388
  async setRoster(socket, roster) {
355
389
  var _a;
356
390
  const before = await this.storage.get('roster');
357
- if (!roster || typeof roster.d !== 'string' || roster.d.length > RELAY_HUB_MAX_VALUE || !(roster.v > ((_a = before === null || before === void 0 ? void 0 : before.v) !== null && _a !== void 0 ? _a : 0))) {
391
+ const k = RelayHub.epochKeys(roster === null || roster === void 0 ? void 0 : roster.k);
392
+ if (!roster || typeof roster.d !== 'string' || roster.d.length > RELAY_HUB_MAX_VALUE || k === null || !(roster.v > ((_a = before === null || before === void 0 ? void 0 : before.v) !== null && _a !== void 0 ? _a : 0))) {
358
393
  this.send(socket, { t: 'error', code: 'invalid', message: 'A roster needs a newer version.' });
359
394
  return;
360
395
  }
361
- await this.storage.put('roster', { v: roster.v, d: roster.d });
362
- this.broadcast({ t: 'roster', roster: { v: roster.v, d: roster.d } });
396
+ const kept = { v: roster.v, d: roster.d, ...(k ? { k } : {}) };
397
+ await this.storage.put('roster', kept);
398
+ this.broadcast({ t: 'roster', roster: kept });
399
+ }
400
+ /** The keys of a roster sealed for each member (RelayHubRoster.k): undefined for none, null for what is not that. */
401
+ static epochKeys(k) {
402
+ if (k === undefined) {
403
+ return undefined;
404
+ }
405
+ if (!k || typeof k !== 'object' || Array.isArray(k)) {
406
+ return null;
407
+ }
408
+ const entries = Object.entries(k);
409
+ const ok = entries.length <= RELAY_HUB_MAX_MEMBERS &&
410
+ entries.every(([member, value]) => RelayHub.id(member) && typeof value === 'string' && value.length < 4096);
411
+ return ok ? Object.fromEntries(entries) : null;
412
+ }
413
+ /** Whether a member lets in, takes out and invites: the owner, or an admin it made. */
414
+ static manages(meta, member) {
415
+ var _a;
416
+ return member === meta.owner || !!((_a = meta.admins) === null || _a === void 0 ? void 0 : _a.includes(member));
417
+ }
418
+ /** One gone is no admin, its invitation with it. */
419
+ async unmake(meta, member) {
420
+ var _a, _b, _c, _d;
421
+ if (((_a = meta.admins) === null || _a === void 0 ? void 0 : _a.includes(member)) || ((_b = meta.invites) === null || _b === void 0 ? void 0 : _b[member])) {
422
+ meta.admins = (_c = meta.admins) === null || _c === void 0 ? void 0 : _c.filter(id => id !== member);
423
+ (_d = meta.invites) === null || _d === void 0 ? true : delete _d[member];
424
+ await this.storage.put('meta', meta);
425
+ }
363
426
  }
364
427
  async closeRoom() {
365
428
  await this.storage.deleteAll();
@@ -0,0 +1,27 @@
1
+ import { type RelayHubRules } from './relay-hub-charter.js';
2
+ import { RelayHubsIdentity } from './relay-hubs-identity.js';
3
+ import type { Live } from './relay-hubs-live.js';
4
+ import type { RelayRoomInfo } from './relay-hubs-protocol.js';
5
+ /**
6
+ * The part of {@link RelayHubs} that says who may do what in a room (docs/design/ta-link.md 8): the owner's choices,
7
+ * signed in the charter of the roster (its admins, its rules, how many it lets in), and the checks of the calls.
8
+ */
9
+ export declare abstract class RelayHubsAdmins extends RelayHubsIdentity {
10
+ /**
11
+ * The owner's choices for a room (docs/design/ta-link.md 8): how many members it lets in at most
12
+ * (RELAY_ROOM_MAX_MEMBERS by default, 2 to RELAY_ROOM_CAPACITY_MAX; those in already stay), its admins (member ids,
13
+ * Ta of the room, not guests) and its rules, for all; the last two signed in the charter of the roster.
14
+ */
15
+ configure(roomId: string, options: {
16
+ members?: number;
17
+ admins?: string[];
18
+ rules?: RelayHubRules;
19
+ }): Promise<RelayRoomInfo>;
20
+ /** The members it lets in at most: the owner's own choice, else the one its charter says. */
21
+ protected capacityOf(live: Live): number;
22
+ protected managed(roomId: string): Live;
23
+ protected owned(roomId: string): Live;
24
+ protected member(roomId: string): Live;
25
+ protected isFriends(live: Live): boolean;
26
+ protected abstract info(live: Live): RelayRoomInfo;
27
+ }
@@ -0,0 +1,91 @@
1
+ import { RELAY_FRIENDS_KIND } from './relay-friends.js';
2
+ import { RELAY_HUB_MAX_ADMINS, RelayHubCharterUtil } from './relay-hub-charter.js';
3
+ import { RelayHubsIdentity } from './relay-hubs-identity.js';
4
+ import { RELAY_FRIENDS_MAX, RELAY_ROOM_CAPACITY_MAX, RELAY_ROOM_MAX_MEMBERS } from './relay-protocol.js';
5
+ /**
6
+ * The part of {@link RelayHubs} that says who may do what in a room (docs/design/ta-link.md 8): the owner's choices,
7
+ * signed in the charter of the roster (its admins, its rules, how many it lets in), and the checks of the calls.
8
+ */
9
+ export class RelayHubsAdmins extends RelayHubsIdentity {
10
+ /**
11
+ * The owner's choices for a room (docs/design/ta-link.md 8): how many members it lets in at most
12
+ * (RELAY_ROOM_MAX_MEMBERS by default, 2 to RELAY_ROOM_CAPACITY_MAX; those in already stay), its admins (member ids,
13
+ * Ta of the room, not guests) and its rules, for all; the last two signed in the charter of the roster.
14
+ */
15
+ async configure(roomId, options) {
16
+ var _a;
17
+ var _b, _c;
18
+ const live = this.owned(roomId);
19
+ let capacity = live.record.capacity;
20
+ if (options.members !== undefined) {
21
+ const members = Math.round(Number(options.members));
22
+ if (!(members >= 2 && members <= RELAY_ROOM_CAPACITY_MAX)) {
23
+ throw new Error(`A room takes 2 to ${RELAY_ROOM_CAPACITY_MAX} members.`);
24
+ }
25
+ capacity = members === RELAY_ROOM_MAX_MEMBERS ? undefined : members;
26
+ live.record.capacity = capacity;
27
+ if (capacity === undefined) {
28
+ delete live.record.capacity;
29
+ }
30
+ this.save();
31
+ }
32
+ if (options.admins === undefined && options.rules === undefined && (options.members === undefined || !live.charter)) {
33
+ return this.info(live);
34
+ }
35
+ const before = (_b = live.charter) !== null && _b !== void 0 ? _b : { room: live.record.id, v: 0, admins: [], rules: {} };
36
+ const roster = (_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _c !== void 0 ? _c : [];
37
+ const admins = options.admins === undefined
38
+ ? before.admins
39
+ : [...new Set(options.admins.map(String))]
40
+ .map(id => roster.find(member => member.member === id && !member.owner && !member.guest))
41
+ .filter(member => !!member)
42
+ .slice(0, RELAY_HUB_MAX_ADMINS)
43
+ .map(member => ({ member: member.member, id: member.id }));
44
+ const charter = {
45
+ room: live.record.id,
46
+ v: before.v,
47
+ admins,
48
+ rules: options.rules === undefined ? before.rules : RelayHubCharterUtil.rules(options.rules),
49
+ ...(capacity ? { capacity } : {}),
50
+ };
51
+ await this.ready(live);
52
+ await this.writeRoster(live, same => same, charter);
53
+ this.request(live, { t: 'admins', admins: admins.map(admin => admin.member) });
54
+ this.emit({ type: 'rooms' });
55
+ return this.info(live);
56
+ }
57
+ /** The members it lets in at most: the owner's own choice, else the one its charter says. */
58
+ capacityOf(live) {
59
+ var _a;
60
+ var _b;
61
+ if (this.isFriends(live)) {
62
+ return RELAY_FRIENDS_MAX + 1;
63
+ }
64
+ return (_b = (live.record.own ? live.record.capacity : (_a = live.charter) === null || _a === void 0 ? void 0 : _a.capacity)) !== null && _b !== void 0 ? _b : RELAY_ROOM_MAX_MEMBERS;
65
+ }
66
+ managed(roomId) {
67
+ const live = this.member(roomId);
68
+ if (!this.manages(live)) {
69
+ throw new Error('Only the owner or an admin of the room does that.');
70
+ }
71
+ return live;
72
+ }
73
+ owned(roomId) {
74
+ const live = this.member(roomId);
75
+ if (!live.record.own) {
76
+ throw new Error('Only the owner of the room does that.');
77
+ }
78
+ return live;
79
+ }
80
+ member(roomId) {
81
+ const live = this.live.get(roomId);
82
+ if ((live === null || live === void 0 ? void 0 : live.record.status) !== 'member') {
83
+ throw new Error('Not in that room.');
84
+ }
85
+ return live;
86
+ }
87
+ isFriends(live) {
88
+ return live.record.own && live.record.kind === RELAY_FRIENDS_KIND;
89
+ }
90
+ }
91
+ //# sourceMappingURL=relay-hubs-admins.js.map