@celljs/ta-relay 4.19.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 (82) 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 +21 -63
  22. package/esm/common/relay-hubs-connection.js +103 -87
  23. package/esm/common/relay-hubs-identity.d.ts +29 -1
  24. package/esm/common/relay-hubs-identity.js +109 -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-identity.d.ts +7 -0
  33. package/esm/common/relay-identity.js +8 -0
  34. package/esm/common/relay-protocol.d.ts +21 -3
  35. package/esm/common/relay-protocol.js +8 -1
  36. package/esm/common/relay-room-chat.d.ts +6 -1
  37. package/esm/common/relay-room-chat.js +1 -0
  38. package/esm/node/relay-server-accounts.d.ts +1 -1
  39. package/esm/node/relay-server-accounts.js +1 -1
  40. package/lib/common/index.d.ts +1 -0
  41. package/lib/common/index.js +1 -0
  42. package/lib/common/relay-account-client.d.ts +4 -0
  43. package/lib/common/relay-account-client.js +13 -3
  44. package/lib/common/relay-account.d.ts +17 -2
  45. package/lib/common/relay-account.js +15 -6
  46. package/lib/common/relay-accounts.d.ts +2 -0
  47. package/lib/common/relay-accounts.js +16 -2
  48. package/lib/common/relay-group-invites.d.ts +6 -2
  49. package/lib/common/relay-group-invites.js +6 -5
  50. package/lib/common/relay-hub-charter.d.ts +75 -0
  51. package/lib/common/relay-hub-charter.js +152 -0
  52. package/lib/common/relay-hub-epochs.d.ts +44 -0
  53. package/lib/common/relay-hub-epochs.js +95 -0
  54. package/lib/common/relay-hub-util.d.ts +39 -7
  55. package/lib/common/relay-hub-util.js +46 -15
  56. package/lib/common/relay-hub.d.ts +13 -0
  57. package/lib/common/relay-hub.js +81 -18
  58. package/lib/common/relay-hubs-admins.d.ts +28 -0
  59. package/lib/common/relay-hubs-admins.js +95 -0
  60. package/lib/common/relay-hubs-connection.d.ts +21 -63
  61. package/lib/common/relay-hubs-connection.js +109 -94
  62. package/lib/common/relay-hubs-identity.d.ts +29 -1
  63. package/lib/common/relay-hubs-identity.js +109 -1
  64. package/lib/common/relay-hubs-live.d.ts +59 -0
  65. package/lib/common/relay-hubs-live.js +31 -0
  66. package/lib/common/relay-hubs-protocol.d.ts +10 -0
  67. package/lib/common/relay-hubs-util.d.ts +9 -0
  68. package/lib/common/relay-hubs-util.js +23 -1
  69. package/lib/common/relay-hubs.d.ts +12 -15
  70. package/lib/common/relay-hubs.js +66 -63
  71. package/lib/common/relay-identity.d.ts +7 -0
  72. package/lib/common/relay-identity.js +8 -0
  73. package/lib/common/relay-protocol.d.ts +21 -3
  74. package/lib/common/relay-protocol.js +9 -2
  75. package/lib/common/relay-room-chat.d.ts +6 -1
  76. package/lib/common/relay-room-chat.js +1 -0
  77. package/lib/node/relay-server-accounts.d.ts +1 -1
  78. package/lib/node/relay-server-accounts.js +1 -1
  79. package/package.json +3 -3
  80. package/viewer/join.html +6 -6
  81. package/viewer/page.html +12 -12
  82. package/viewer/room.html +15 -15
@@ -133,6 +133,10 @@ export declare class AccountAgent {
133
133
  approve(id: string): Promise<AccountView>;
134
134
  /** Removes a device (one waiting that is not the user's, or one lost): every device drops it. */
135
135
  remove(id: string): Promise<AccountView>;
136
+ /** A token of the account for a device the user brings in by hand (a computer whose code they scanned). */
137
+ invite(): Promise<{
138
+ token: string;
139
+ }>;
136
140
  /** Leaves the account: removed from it, its token dropped. */
137
141
  signOut(): Promise<void>;
138
142
  }
@@ -129,7 +129,7 @@ class AccountAgent {
129
129
  /** A new device of `account`: its own keys, trusting itself alone. */
130
130
  static async create(service, token, account, kind, name) {
131
131
  const self = await relay_identity_1.RelayIdentityUtil.create(name);
132
- return { service: service.replace(/\/+$/, ''), token, account, kind, self, circle: relay_account_1.AccountCircle.alone(self), pairings: {} };
132
+ return { service: service.replace(/\/+$/, ''), token, account, kind, self, circle: relay_account_1.AccountCircle.alone(self, kind), pairings: {} };
133
133
  }
134
134
  get state() {
135
135
  return this.kept;
@@ -213,12 +213,14 @@ class AccountAgent {
213
213
  if (this.hooks.pair && this.hooks.paired) {
214
214
  const paired = new Set(this.hooks.paired());
215
215
  for (const id of paired) {
216
- if (!ids.has(id)) {
216
+ const device = trusted.find(one => one.id === id);
217
+ if (!ids.has(id) || (device && relay_account_1.AccountCircle.cloud(this.kept.circle, device))) {
217
218
  await ((_b = (_a = this.hooks).unpair) === null || _b === void 0 ? void 0 : _b.call(_a, id));
218
219
  }
219
220
  }
220
221
  for (const device of trusted) {
221
- if (!paired.has(device.id)) {
222
+ // A Ta in the cloud gets no way into a computer: devices open it, it does not reach them.
223
+ if (!paired.has(device.id) && !relay_account_1.AccountCircle.cloud(this.kept.circle, device)) {
222
224
  const link = await this.hooks.pair(device);
223
225
  await this.calls.send(await relay_account_1.AccountCircle.seal(this.kept.self, device, 'pairing', { link }));
224
226
  }
@@ -312,6 +314,10 @@ class AccountAgent {
312
314
  /** Lets a waiting device in: signs it, and sends it whom this device trusts. */
313
315
  async approve(id) {
314
316
  return this.guard(async () => {
317
+ if (this.kept.kind === 'cloud') {
318
+ // Not 403: that would sign this device out (guard).
319
+ throw new AccountCallError(409, 'A Ta in the cloud lets no device in.');
320
+ }
315
321
  const roster = await this.calls.roster(this.kept.self.id);
316
322
  const device = roster.devices.find(one => one.id === id);
317
323
  if (!device || !relay_account_1.AccountCircle.waiting(device)) {
@@ -330,6 +336,10 @@ class AccountAgent {
330
336
  return this.sync();
331
337
  });
332
338
  }
339
+ /** A token of the account for a device the user brings in by hand (a computer whose code they scanned). */
340
+ invite() {
341
+ return this.guard(() => this.calls.invite());
342
+ }
333
343
  /** Leaves the account: removed from it, its token dropped. */
334
344
  async signOut() {
335
345
  const { self, account } = this.kept;
@@ -5,7 +5,11 @@ export declare const RELAY_ACCOUNT_PATH = "/v1/account/";
5
5
  export declare const RELAY_ACCOUNT_MAX_DEVICES = 20;
6
6
  /** A device seen within this is online: computers say they are there every minute. */
7
7
  export declare const RELAY_ACCOUNT_ONLINE_MS: number;
8
- export type AccountDeviceKind = 'computer' | 'phone' | 'browser';
8
+ /**
9
+ * `cloud`: a Ta in the cloud, in a sandbox of the gateway (docs/design/ta-me.md 2). The devices of the account open it,
10
+ * but it lets no device in and gets no way into a computer: whoever runs the gateway runs it.
11
+ */
12
+ export type AccountDeviceKind = 'computer' | 'phone' | 'browser' | 'cloud';
9
13
  export interface AccountName {
10
14
  /** `github:<id>`, `iam:<user>`: the provider and who the user is there. */
11
15
  id: string;
@@ -73,6 +77,13 @@ export interface AccountCalls {
73
77
  send(mail: Omit<AccountMail, 'at'>): Promise<void>;
74
78
  mail(to: string): Promise<AccountMail[]>;
75
79
  signOut(): Promise<void>;
80
+ /**
81
+ * A token of the account for another device the user brings in by hand (a computer whose code they scanned): it
82
+ * joins with it and waits to be let in, as any device signing in.
83
+ */
84
+ invite(): Promise<{
85
+ token: string;
86
+ }>;
76
87
  }
77
88
  export type AccountCallName = keyof AccountCalls;
78
89
  export declare const ACCOUNT_CALLS: readonly AccountCallName[];
@@ -96,9 +107,11 @@ export type AccountSignInPoll = {
96
107
  };
97
108
  /** What a device keeps of the devices it trusts (docs/design/ta-account.md 5). */
98
109
  export interface AccountCircleState {
110
+ /** `kind` as it was let in with (signed): a Ta in the cloud lets no one in. None from a device of before. */
99
111
  trusted: Record<string, {
100
112
  sign: string;
101
113
  box: string;
114
+ kind?: AccountDeviceKind;
102
115
  }>;
103
116
  /** Removed once: never trusted again, whatever the service says later. */
104
117
  removed: string[];
@@ -115,7 +128,9 @@ export declare namespace AccountCircle {
115
128
  /** Waiting for a device of the account to let it in. */
116
129
  function waiting(device: AccountDevice): boolean;
117
130
  /** A device trusting itself alone: the first one, or one not let in yet. */
118
- function alone(self: Pick<AccountDevice, 'id' | 'sign' | 'box'>): AccountCircleState;
131
+ function alone(self: Pick<AccountDevice, 'id' | 'sign' | 'box'>, kind?: AccountDeviceKind): AccountCircleState;
132
+ /** A Ta in the cloud, as trusted: its kind as it was let in with, else as the service says. */
133
+ function cloud(state: AccountCircleState, device: Pick<AccountDevice, 'id' | 'kind'>): boolean;
119
134
  /**
120
135
  * Who to trust now: those trusted before and not removed, and those a trusted device let in with a good signature,
121
136
  * until no more. A device the service adds without a signature of one of them is never trusted.
@@ -9,7 +9,7 @@ exports.RELAY_ACCOUNT_PATH = '/v1/account/';
9
9
  exports.RELAY_ACCOUNT_MAX_DEVICES = 20;
10
10
  /** A device seen within this is online: computers say they are there every minute. */
11
11
  exports.RELAY_ACCOUNT_ONLINE_MS = 3 * 60000;
12
- exports.ACCOUNT_CALLS = ['roster', 'join', 'endorse', 'remove', 'send', 'mail', 'signOut'];
12
+ exports.ACCOUNT_CALLS = ['roster', 'join', 'endorse', 'remove', 'send', 'mail', 'signOut', 'invite'];
13
13
  /** The trust of the devices of an account, kept by each device: the service holds keys, never says who to trust. */
14
14
  var AccountCircle;
15
15
  (function (AccountCircle) {
@@ -40,10 +40,17 @@ var AccountCircle;
40
40
  }
41
41
  AccountCircle.waiting = waiting;
42
42
  /** A device trusting itself alone: the first one, or one not let in yet. */
43
- function alone(self) {
44
- return { trusted: { [self.id]: { sign: self.sign, box: self.box } }, removed: [] };
43
+ function alone(self, kind) {
44
+ return { trusted: { [self.id]: { sign: self.sign, box: self.box, ...(kind ? { kind } : {}) } }, removed: [] };
45
45
  }
46
46
  AccountCircle.alone = alone;
47
+ /** A Ta in the cloud, as trusted: its kind as it was let in with, else as the service says. */
48
+ function cloud(state, device) {
49
+ var _a;
50
+ var _b;
51
+ return ((_b = (_a = state.trusted[device.id]) === null || _a === void 0 ? void 0 : _a.kind) !== null && _b !== void 0 ? _b : device.kind) === 'cloud';
52
+ }
53
+ AccountCircle.cloud = cloud;
47
54
  /**
48
55
  * Who to trust now: those trusted before and not removed, and those a trusted device let in with a good signature,
49
56
  * until no more. A device the service adds without a signature of one of them is never trusted.
@@ -64,11 +71,13 @@ var AccountCircle;
64
71
  for (let added = true; added;) {
65
72
  added = false;
66
73
  for (const device of roster.devices) {
67
- if (trusted[device.id] || removed.has(device.id) || !live(device) || !device.endorsement || !trusted[device.endorsement.by]) {
74
+ const by = device.endorsement ? trusted[device.endorsement.by] : undefined;
75
+ // A Ta in the cloud lets no one in: the gateway it runs on could otherwise add devices to the account.
76
+ if (trusted[device.id] || removed.has(device.id) || !live(device) || !device.endorsement || !by || by.kind === 'cloud') {
68
77
  continue;
69
78
  }
70
- if (await relay_identity_1.RelayIdentityUtil.verify(trusted[device.endorsement.by].sign, text(roster.account.id, device), device.endorsement.signature)) {
71
- trusted[device.id] = { sign: device.sign, box: device.box };
79
+ if (await relay_identity_1.RelayIdentityUtil.verify(by.sign, text(roster.account.id, device), device.endorsement.signature)) {
80
+ trusted[device.id] = { sign: device.sign, box: device.box, kind: device.kind };
72
81
  added = true;
73
82
  }
74
83
  }
@@ -73,6 +73,8 @@ export interface RelayAccountsOptions {
73
73
  * a provider says who signed in.
74
74
  */
75
75
  authenticate?(token: string): Promise<AccountName | undefined>;
76
+ /** A gateway: a new token of its own sign-in for the account, for a device the user brings in (AccountCalls.invite). */
77
+ issue?(account: AccountName): Promise<string>;
76
78
  maxDevices?: number;
77
79
  }
78
80
  /**
@@ -17,7 +17,7 @@ const MAIL_MAX = 16 * 1024;
17
17
  const TOKENS_MAX = 50;
18
18
  const REMOVED_MAX = 100;
19
19
  const SEEN_EVERY_MS = 30000;
20
- const KINDS = new Set(['computer', 'phone', 'browser']);
20
+ const KINDS = new Set(['computer', 'phone', 'browser', 'cloud']);
21
21
  const MAIL_KINDS = new Set(['circle', 'pairing', 'package']);
22
22
  const TOPIC = /^[@\w./:-]{1,120}$/;
23
23
  /**
@@ -356,13 +356,27 @@ class RelayAccounts {
356
356
  if (!account) {
357
357
  throw new RelayAccountError(401, 'Sign in again.');
358
358
  }
359
+ if (request.op === 'invite') {
360
+ if (!this.options.issue) {
361
+ throw new RelayAccountError(404, 'This service brings in no device by hand.');
362
+ }
363
+ return { token: await this.options.issue(account) };
364
+ }
359
365
  return this.options.book(account.id).handle({ ...request, account });
360
366
  }
361
367
  const account = RelayAccounts.accountOf(token);
362
368
  if (!account) {
363
369
  throw new RelayAccountError(401, 'Sign in first.');
364
370
  }
365
- return this.options.book(account).handle({ ...request, hash: await relay_identity_1.RelayKeys.hash(token) });
371
+ const hash = await relay_identity_1.RelayKeys.hash(token);
372
+ if (request.op === 'invite') {
373
+ // The caller's token checked by the book, then a token of the account of its own, which signs out alone.
374
+ const { account: name } = (await this.options.book(account).handle({ op: 'roster', args: [], hash }));
375
+ const invited = RelayAccounts.token(account);
376
+ await this.options.book(account).handle({ op: 'issue', account: name, hash: await relay_identity_1.RelayKeys.hash(invited) });
377
+ return { token: invited };
378
+ }
379
+ return this.options.book(account).handle({ ...request, hash });
366
380
  }
367
381
  }
368
382
  exports.RelayAccounts = RelayAccounts;
@@ -7,6 +7,8 @@ export interface RelayGroupInvite {
7
7
  /** The id of the room. */
8
8
  room: string;
9
9
  title: string;
10
+ /** The kind of the room, when not a group chat of the page (a chat of two, ta-rooms' `ta-direct`). */
11
+ kind?: string;
10
12
  by: {
11
13
  id: string;
12
14
  name: string;
@@ -20,6 +22,8 @@ export interface RelayGroupRooms {
20
22
  identity(): Promise<RelayIdentity>;
21
23
  list(): RelayRoomInfo[];
22
24
  record(room: string): RelayRoomRecord | undefined;
25
+ /** This Ta lets in and invites: the owner, or an admin. */
26
+ manages(room: string): boolean;
23
27
  save(): void;
24
28
  friends(): (RelayFriend & RelayPublicIdentity)[];
25
29
  friendRoom(): Promise<RelayRoomInfo>;
@@ -50,9 +54,9 @@ export declare class RelayGroupInvites {
50
54
  protected readonly rooms: RelayGroupRooms;
51
55
  protected readonly store: RelayHubsStore;
52
56
  constructor(rooms: RelayGroupRooms, store: RelayHubsStore);
53
- /** Invites friends into a group chat of the user's own: how many were. */
57
+ /** Invites friends into a group chat the user owns or is an admin of: how many were. */
54
58
  invite(roomId: string, friendIds: readonly string[]): Promise<number>;
55
- /** Whether an ask to a room of the user's is of a friend it invited: let in at once. */
59
+ /** Whether an ask (by an invitation of this Ta's) is of a friend it invited: let in at once. */
56
60
  admits(record: RelayRoomRecord, ask: RelayPublicIdentity & {
57
61
  guest?: boolean;
58
62
  }): boolean;
@@ -20,11 +20,11 @@ class RelayGroupInvites {
20
20
  this.rooms = rooms;
21
21
  this.store = store;
22
22
  }
23
- /** Invites friends into a group chat of the user's own: how many were. */
23
+ /** Invites friends into a group chat the user owns or is an admin of: how many were. */
24
24
  async invite(roomId, friendIds) {
25
25
  var _a;
26
26
  const record = this.rooms.record(roomId);
27
- if (!(record === null || record === void 0 ? void 0 : record.own) || record.status !== 'member' || record.unlisted || record.kind === relay_friends_1.RELAY_FRIENDS_KIND) {
27
+ if (!record || !this.rooms.manages(roomId) || record.status !== 'member' || record.unlisted || record.kind === relay_friends_1.RELAY_FRIENDS_KIND) {
28
28
  throw new Error('Only a group chat of your own takes friends.');
29
29
  }
30
30
  const ids = new Set(friendIds);
@@ -44,17 +44,17 @@ class RelayGroupInvites {
44
44
  await this.prune(mine.id, now);
45
45
  for (const friend of friends) {
46
46
  const key = await relay_identity_1.RelayKeys.aesKey(await relay_identity_1.RelayIdentityUtil.agree(identity, friend.box, friend.id, INFO));
47
- const opened = { link, title: record.title, at: now, expiresAt };
47
+ const opened = { link, title: record.title, kind: record.kind, at: now, expiresAt };
48
48
  const sealed = { d: await relay_identity_1.RelayKeys.seal(key, JSON.stringify(opened)), expiresAt };
49
49
  await this.rooms.setState(mine.id, await RelayGroupInvites.keyOf(roomId, friend.id), sealed);
50
50
  }
51
51
  return friends.length;
52
52
  }
53
- /** Whether an ask to a room of the user's is of a friend it invited: let in at once. */
53
+ /** Whether an ask (by an invitation of this Ta's) is of a friend it invited: let in at once. */
54
54
  admits(record, ask) {
55
55
  var _a;
56
56
  var _b;
57
- return record.own && !ask.guest && ((_b = (_a = record.invited) === null || _a === void 0 ? void 0 : _a[ask.id]) !== null && _b !== void 0 ? _b : 0) > Date.now();
57
+ return !ask.guest && ((_b = (_a = record.invited) === null || _a === void 0 ? void 0 : _a[ask.id]) !== null && _b !== void 0 ? _b : 0) > Date.now();
58
58
  }
59
59
  /** One invited is in: its invitation goes. */
60
60
  async admitted(record, id) {
@@ -153,6 +153,7 @@ class RelayGroupInvites {
153
153
  found.push({
154
154
  room: link.room,
155
155
  title: String(opened.title || link.title).slice(0, 80),
156
+ ...(typeof opened.kind === 'string' && opened.kind ? { kind: opened.kind.slice(0, 64) } : {}),
156
157
  by: { id: owner.id, name: owner.name, ...(owner.look ? { look: owner.look } : {}) },
157
158
  at: Number(opened.at) || now,
158
159
  expiresAt: opened.expiresAt,
@@ -0,0 +1,75 @@
1
+ import { type RelayHubKeys, type RelayHubMember, type RelayHubRosterBody } from './relay-hub-util';
2
+ import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity';
3
+ /** An admin of a room as its owner names it: its member id in the room and the identity it is. */
4
+ export interface RelayHubAdmin {
5
+ member: string;
6
+ id: string;
7
+ }
8
+ /**
9
+ * The rules of a room, its owner's for all (docs/design/ta-link.md 8): `approve`, friends invited ask as anyone else;
10
+ * the others are the packages' that read them (ta-rooms: `agents`, `doubles`, `tasks`), each false to forbid.
11
+ */
12
+ export type RelayHubRules = Record<string, boolean>;
13
+ /** What the owner of a room signs for all: who its admins are, its rules, how many it lets in. `v` grows with each change. */
14
+ export interface RelayHubCharterBody {
15
+ room: string;
16
+ v: number;
17
+ admins: RelayHubAdmin[];
18
+ rules: RelayHubRules;
19
+ capacity?: number;
20
+ }
21
+ /** The charter as the roster carries it: `p` its JSON, `s` the owner's signature of it. */
22
+ export interface RelayHubCharter {
23
+ p: string;
24
+ s: string;
25
+ }
26
+ /** A roster opened: who signed it (the owner, or an admin of its charter) and the charter it carries. */
27
+ export interface RelayHubRosterOpened {
28
+ signer: string;
29
+ byAdmin: boolean;
30
+ body: RelayHubRosterBody;
31
+ charter?: RelayHubCharterBody;
32
+ }
33
+ /** Admins of a room at most. */
34
+ export declare const RELAY_HUB_MAX_ADMINS = 8;
35
+ /**
36
+ * The charter of a room (docs/design/ta-link.md 8): the owner names admins and sets rules, signed, carried in the
37
+ * roster; an admin signs a roster that changes members alone, which every member checks against the charter.
38
+ */
39
+ export declare namespace RelayHubCharterUtil {
40
+ /** Rules as kept: names of letters, booleans, a few. */
41
+ function rules(input: unknown): RelayHubRules;
42
+ function seal(owner: RelayIdentity, body: RelayHubCharterBody): Promise<RelayHubCharter>;
43
+ /** Undefined unless signed by the owner, and of this room. */
44
+ function open(ownerSign: string, room: string, charter: unknown): Promise<RelayHubCharterBody | undefined>;
45
+ /**
46
+ * A roster, once it checks: signed by the owner of `owner` (whose entry it has), or by an admin its charter names,
47
+ * the entry of that admin of the identity named. Whether an admin may make such a change is the reader's to judge.
48
+ */
49
+ function openRoster(keys: RelayHubKeys, sealed: string, owner: {
50
+ id: string;
51
+ sign: string;
52
+ }, room: string): Promise<RelayHubRosterOpened | undefined>;
53
+ /**
54
+ * Whether a roster an admin signed only changes what an admin may: the charter no older than the one before, the
55
+ * owner and every admin still in, those who were members still the same identities.
56
+ */
57
+ function adminMay(next: RelayHubRosterOpened, before: {
58
+ members: RelayHubMember[];
59
+ charter?: RelayHubCharterBody;
60
+ } | undefined): boolean;
61
+ /**
62
+ * The answer of an admin to an ask: as the owner's (RelayHubUtil.sealAnswer), with the owner's public identity
63
+ * and the charter naming the admin, which the one who asked checks against the owner of its link.
64
+ */
65
+ function answerProof(head: RelayPublicIdentity, charter: RelayHubCharter): {
66
+ head: RelayPublicIdentity;
67
+ charter: RelayHubCharter;
68
+ };
69
+ /** The one who answered may: the owner of the link, or an admin its charter names (its identity checked). */
70
+ function answeredBy(signer: RelayPublicIdentity, ownerId: string, room: string, proof: {
71
+ head?: RelayPublicIdentity;
72
+ charter?: RelayHubCharter;
73
+ }): Promise<RelayPublicIdentity | undefined>;
74
+ }
75
+ //# sourceMappingURL=relay-hub-charter.d.ts.map
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelayHubCharterUtil = exports.RELAY_HUB_MAX_ADMINS = void 0;
4
+ const relay_hub_util_1 = require("./relay-hub-util");
5
+ const relay_identity_1 = require("./relay-identity");
6
+ /** Admins of a room at most. */
7
+ exports.RELAY_HUB_MAX_ADMINS = 8;
8
+ const MAX_RULES = 16;
9
+ /**
10
+ * The charter of a room (docs/design/ta-link.md 8): the owner names admins and sets rules, signed, carried in the
11
+ * roster; an admin signs a roster that changes members alone, which every member checks against the charter.
12
+ */
13
+ var RelayHubCharterUtil;
14
+ (function (RelayHubCharterUtil) {
15
+ /** Rules as kept: names of letters, booleans, a few. */
16
+ function rules(input) {
17
+ if (!input || typeof input !== 'object') {
18
+ return {};
19
+ }
20
+ return Object.fromEntries(Object.entries(input)
21
+ .filter(([name, value]) => /^[a-z][a-zA-Z]{0,30}$/.test(name) && typeof value === 'boolean')
22
+ .slice(0, MAX_RULES));
23
+ }
24
+ RelayHubCharterUtil.rules = rules;
25
+ async function seal(owner, body) {
26
+ const p = JSON.stringify(body);
27
+ return { p, s: await relay_identity_1.RelayIdentityUtil.sign(owner, p) };
28
+ }
29
+ RelayHubCharterUtil.seal = seal;
30
+ /** Undefined unless signed by the owner, and of this room. */
31
+ async function open(ownerSign, room, charter) {
32
+ try {
33
+ const { p, s } = charter;
34
+ if (typeof p !== 'string' || typeof s !== 'string' || !(await relay_identity_1.RelayIdentityUtil.verify(ownerSign, p, s))) {
35
+ return undefined;
36
+ }
37
+ const body = JSON.parse(p);
38
+ if (body.room !== room || !(Number(body.v) > 0) || !Array.isArray(body.admins)) {
39
+ return undefined;
40
+ }
41
+ const capacity = Math.round(Number(body.capacity));
42
+ return {
43
+ room,
44
+ v: Number(body.v),
45
+ admins: body.admins
46
+ .filter(admin => admin && typeof admin.member === 'string' && typeof admin.id === 'string')
47
+ .slice(0, exports.RELAY_HUB_MAX_ADMINS)
48
+ .map(admin => ({ member: admin.member, id: admin.id })),
49
+ rules: rules(body.rules),
50
+ ...(capacity >= 2 ? { capacity } : {}),
51
+ };
52
+ }
53
+ catch (_a) {
54
+ return undefined;
55
+ }
56
+ }
57
+ RelayHubCharterUtil.open = open;
58
+ /**
59
+ * A roster, once it checks: signed by the owner of `owner` (whose entry it has), or by an admin its charter names,
60
+ * the entry of that admin of the identity named. Whether an admin may make such a change is the reader's to judge.
61
+ */
62
+ async function openRoster(keys, sealed, owner, room) {
63
+ try {
64
+ const found = relay_hub_util_1.RelayHubUtil.keyFor(keys, sealed);
65
+ if (!found) {
66
+ return undefined;
67
+ }
68
+ const envelope = JSON.parse(await relay_identity_1.RelayKeys.open(found.key, found.sealed));
69
+ const signed = JSON.parse(envelope.p);
70
+ if (!(signed === null || signed === void 0 ? void 0 : signed.b) || !Array.isArray(signed.b.members) || typeof signed.m !== 'string') {
71
+ return undefined;
72
+ }
73
+ const members = signed.b.members.filter((member) => !!member && typeof member.member === 'string');
74
+ const head = members.find(member => member.owner && member.id === owner.id);
75
+ if (!head) {
76
+ return undefined;
77
+ }
78
+ const charter = signed.b.charter ? await open(owner.sign, room, signed.b.charter) : undefined;
79
+ let key;
80
+ let byAdmin = false;
81
+ if (signed.m === head.member) {
82
+ key = owner.sign;
83
+ }
84
+ else {
85
+ const named = charter === null || charter === void 0 ? void 0 : charter.admins.find(admin => admin.member === signed.m);
86
+ const entry = named && members.find(member => member.member === named.member && member.id === named.id && !member.owner && !member.guest);
87
+ if (entry && (await relay_identity_1.RelayIdentityUtil.check(entry))) {
88
+ key = entry.sign;
89
+ byAdmin = true;
90
+ }
91
+ }
92
+ if (!key || !(await relay_identity_1.RelayIdentityUtil.verify(key, envelope.p, envelope.s))) {
93
+ return undefined;
94
+ }
95
+ return {
96
+ signer: signed.m,
97
+ byAdmin,
98
+ body: { v: Number(signed.b.v), members, ...(signed.b.charter ? { charter: signed.b.charter } : {}) },
99
+ ...(charter ? { charter } : {}),
100
+ };
101
+ }
102
+ catch (_a) {
103
+ return undefined;
104
+ }
105
+ }
106
+ RelayHubCharterUtil.openRoster = openRoster;
107
+ /**
108
+ * Whether a roster an admin signed only changes what an admin may: the charter no older than the one before, the
109
+ * owner and every admin still in, those who were members still the same identities.
110
+ */
111
+ function adminMay(next, before) {
112
+ const charter = next.charter;
113
+ if (!charter || ((before === null || before === void 0 ? void 0 : before.charter) && charter.v < before.charter.v)) {
114
+ return false;
115
+ }
116
+ const has = (member) => next.body.members.some(each => each.member === member);
117
+ if (!charter.admins.every(admin => has(admin.member))) {
118
+ return false;
119
+ }
120
+ const head = before === null || before === void 0 ? void 0 : before.members.find(member => member.owner);
121
+ if (head && !next.body.members.some(member => member.member === head.member && member.owner && member.id === head.id)) {
122
+ return false;
123
+ }
124
+ return next.body.members.every(member => {
125
+ const was = before === null || before === void 0 ? void 0 : before.members.find(each => each.member === member.member);
126
+ return !was || (was.id === member.id && !!was.owner === !!member.owner);
127
+ });
128
+ }
129
+ RelayHubCharterUtil.adminMay = adminMay;
130
+ /**
131
+ * The answer of an admin to an ask: as the owner's (RelayHubUtil.sealAnswer), with the owner's public identity
132
+ * and the charter naming the admin, which the one who asked checks against the owner of its link.
133
+ */
134
+ function answerProof(head, charter) {
135
+ return { head: relay_identity_1.RelayIdentityUtil.publicOf(head), charter };
136
+ }
137
+ RelayHubCharterUtil.answerProof = answerProof;
138
+ /** The one who answered may: the owner of the link, or an admin its charter names (its identity checked). */
139
+ async function answeredBy(signer, ownerId, room, proof) {
140
+ if (signer.id === ownerId) {
141
+ return (await relay_identity_1.RelayIdentityUtil.check(signer)) ? signer : undefined;
142
+ }
143
+ const head = proof.head;
144
+ if (!head || head.id !== ownerId || !(await relay_identity_1.RelayIdentityUtil.check(head)) || !(await relay_identity_1.RelayIdentityUtil.check(signer))) {
145
+ return undefined;
146
+ }
147
+ const charter = await open(head.sign, room, proof.charter);
148
+ return (charter === null || charter === void 0 ? void 0 : charter.admins.some(admin => admin.id === signer.id)) ? head : undefined;
149
+ }
150
+ RelayHubCharterUtil.answeredBy = answeredBy;
151
+ })(RelayHubCharterUtil || (exports.RelayHubCharterUtil = RelayHubCharterUtil = {}));
152
+ //# sourceMappingURL=relay-hub-charter.js.map
@@ -0,0 +1,44 @@
1
+ import type { RelayHubRosterOpened } from './relay-hub-charter';
2
+ import { type RelayHubMember } from './relay-hub-util';
3
+ import { type RelayIdentity } from './relay-identity';
4
+ /** The epochs a Ta keeps the keys of, the latest: what was sealed in one before them is no longer read. */
5
+ export declare const RELAY_HUB_KEEP_EPOCHS = 8;
6
+ /**
7
+ * 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
8
+ * makes it and seals the keys of the latest epochs for each member left, alone (ECDH of the two, salted by the room and
9
+ * the epoch): `roster.k` beside the roster sealed with the new key. The one gone holds the keys of before, never this.
10
+ */
11
+ export declare namespace RelayHubEpochs {
12
+ /** The epochs kept: the latest RELAY_HUB_KEEP_EPOCHS. */
13
+ function kept(epochs: Record<string, string>): Record<string, string>;
14
+ /** The epoch a text of RelayHubUtil.sealIn is of. */
15
+ function epochOf(sealed: string): number;
16
+ function latest(epochs: Record<string, string> | undefined): number;
17
+ /** The epochs with one more, its key new. */
18
+ function next(epochs: Record<string, string> | undefined): Record<string, string>;
19
+ /**
20
+ * What each of `members` reads the keys by, sealed by `identity` as `sealer` (its member id):
21
+ * `<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.
22
+ */
23
+ function seal(identity: RelayIdentity, sealer: string, room: string, epochs: Record<string, string>, members: readonly Pick<RelayHubMember, 'member' | 'box'>[], cache: Map<string, string>): Promise<Record<string, string>>;
24
+ /**
25
+ * The keys sealed for this Ta (`self`) in `k`, with who sealed them (its member id and box), to be checked against
26
+ * the roster they come with (`sealedBy`); undefined when none is for it, or it does not open.
27
+ */
28
+ function open(identity: RelayIdentity, self: string, room: string, epoch: number, k: Record<string, string> | undefined): Promise<{
29
+ epochs: Record<string, string>;
30
+ sealer: {
31
+ member: string;
32
+ box: string;
33
+ };
34
+ } | undefined>;
35
+ /**
36
+ * Whether the one who sealed the keys of a new epoch may: it signed the roster they came with (the owner, or an admin
37
+ * its charter names), with the box the roster gives it. Anyone else, the relay among them, gives no key.
38
+ */
39
+ function sealedBy(sealer: {
40
+ member: string;
41
+ box: string;
42
+ }, roster: RelayHubRosterOpened): boolean;
43
+ }
44
+ //# sourceMappingURL=relay-hub-epochs.d.ts.map
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelayHubEpochs = exports.RELAY_HUB_KEEP_EPOCHS = void 0;
4
+ const relay_hub_util_1 = require("./relay-hub-util");
5
+ const relay_identity_1 = require("./relay-identity");
6
+ /** The epochs a Ta keeps the keys of, the latest: what was sealed in one before them is no longer read. */
7
+ exports.RELAY_HUB_KEEP_EPOCHS = 8;
8
+ /**
9
+ * 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
10
+ * makes it and seals the keys of the latest epochs for each member left, alone (ECDH of the two, salted by the room and
11
+ * the epoch): `roster.k` beside the roster sealed with the new key. The one gone holds the keys of before, never this.
12
+ */
13
+ var RelayHubEpochs;
14
+ (function (RelayHubEpochs) {
15
+ /** The epochs kept: the latest RELAY_HUB_KEEP_EPOCHS. */
16
+ function kept(epochs) {
17
+ const numbers = Object.keys(epochs)
18
+ .map(Number)
19
+ .filter(n => Number.isInteger(n) && n > 0)
20
+ .sort((a, b) => b - a)
21
+ .slice(0, exports.RELAY_HUB_KEEP_EPOCHS);
22
+ return Object.fromEntries(numbers.map(n => [String(n), epochs[String(n)]]));
23
+ }
24
+ RelayHubEpochs.kept = kept;
25
+ /** The epoch a text of RelayHubUtil.sealIn is of. */
26
+ function epochOf(sealed) {
27
+ const dot = String(sealed).indexOf('.');
28
+ const epoch = dot > 0 ? Number(String(sealed).slice(0, dot)) : 0;
29
+ return Number.isInteger(epoch) && epoch >= 0 ? epoch : -1;
30
+ }
31
+ RelayHubEpochs.epochOf = epochOf;
32
+ function latest(epochs) {
33
+ return Math.max(0, ...Object.keys(epochs !== null && epochs !== void 0 ? epochs : {})
34
+ .map(Number)
35
+ .filter(Number.isInteger));
36
+ }
37
+ RelayHubEpochs.latest = latest;
38
+ /** The epochs with one more, its key new. */
39
+ function next(epochs) {
40
+ return kept({ ...epochs, [String(latest(epochs) + 1)]: relay_hub_util_1.RelayHubUtil.randomKey() });
41
+ }
42
+ RelayHubEpochs.next = next;
43
+ async function keyOf(identity, otherBox, room, epoch) {
44
+ return relay_identity_1.RelayKeys.aesKey(await relay_identity_1.RelayIdentityUtil.agree(identity, otherBox, `${room}:${epoch}`, 'ta-room/1 epoch'));
45
+ }
46
+ /**
47
+ * What each of `members` reads the keys by, sealed by `identity` as `sealer` (its member id):
48
+ * `<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.
49
+ */
50
+ async function seal(identity, sealer, room, epochs, members, cache) {
51
+ const epoch = latest(epochs);
52
+ const text = JSON.stringify(kept(epochs));
53
+ const out = {};
54
+ for (const member of members) {
55
+ const id = `${epoch}:${sealer}:${member.member}`;
56
+ let sealed = cache.get(id);
57
+ if (!sealed) {
58
+ sealed = `${sealer}.${identity.box}.${await relay_identity_1.RelayKeys.seal(await keyOf(identity, member.box, room, epoch), text)}`;
59
+ cache.set(id, sealed);
60
+ }
61
+ out[member.member] = sealed;
62
+ }
63
+ return out;
64
+ }
65
+ RelayHubEpochs.seal = seal;
66
+ /**
67
+ * The keys sealed for this Ta (`self`) in `k`, with who sealed them (its member id and box), to be checked against
68
+ * the roster they come with (`sealedBy`); undefined when none is for it, or it does not open.
69
+ */
70
+ async function open(identity, self, room, epoch, k) {
71
+ const parts = typeof (k === null || k === void 0 ? void 0 : k[self]) === 'string' ? k[self].split('.') : [];
72
+ if (parts.length !== 3 || epoch < 1) {
73
+ return undefined;
74
+ }
75
+ const [member, box, sealed] = parts;
76
+ try {
77
+ const epochs = JSON.parse(await relay_identity_1.RelayKeys.open(await keyOf(identity, box, room, epoch), sealed));
78
+ 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)));
79
+ return valid[String(epoch)] ? { epochs: valid, sealer: { member, box } } : undefined;
80
+ }
81
+ catch (_a) {
82
+ return undefined;
83
+ }
84
+ }
85
+ RelayHubEpochs.open = open;
86
+ /**
87
+ * Whether the one who sealed the keys of a new epoch may: it signed the roster they came with (the owner, or an admin
88
+ * its charter names), with the box the roster gives it. Anyone else, the relay among them, gives no key.
89
+ */
90
+ function sealedBy(sealer, roster) {
91
+ return roster.signer === sealer.member && roster.body.members.some(member => member.member === sealer.member && member.box === sealer.box);
92
+ }
93
+ RelayHubEpochs.sealedBy = sealedBy;
94
+ })(RelayHubEpochs || (exports.RelayHubEpochs = RelayHubEpochs = {}));
95
+ //# sourceMappingURL=relay-hub-epochs.js.map