@celljs/ta-relay 4.20.0 → 4.22.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 (91) hide show
  1. package/esm/common/index.d.ts +2 -0
  2. package/esm/common/index.js +2 -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 +78 -0
  12. package/esm/common/relay-hub-charter.js +152 -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 +15 -15
  31. package/esm/common/relay-hubs.js +76 -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-app.d.ts +106 -0
  35. package/esm/common/relay-room-app.js +171 -0
  36. package/esm/common/relay-room-chat.d.ts +6 -1
  37. package/esm/common/relay-room-chat.js +1 -0
  38. package/esm/guest/guest-i18n.d.ts +3 -0
  39. package/esm/guest/guest-i18n.js +12 -0
  40. package/esm/guest/guest-style.d.ts +1 -1
  41. package/esm/guest/guest-style.js +11 -0
  42. package/esm/guest/guest.js +76 -6
  43. package/esm/node/relay-server-accounts.d.ts +1 -1
  44. package/esm/node/relay-server-accounts.js +1 -1
  45. package/lib/common/index.d.ts +2 -0
  46. package/lib/common/index.js +2 -0
  47. package/lib/common/relay-account-client.d.ts +4 -0
  48. package/lib/common/relay-account-client.js +13 -3
  49. package/lib/common/relay-account.d.ts +17 -2
  50. package/lib/common/relay-account.js +15 -6
  51. package/lib/common/relay-accounts.d.ts +2 -0
  52. package/lib/common/relay-accounts.js +16 -2
  53. package/lib/common/relay-group-invites.d.ts +6 -2
  54. package/lib/common/relay-group-invites.js +6 -5
  55. package/lib/common/relay-hub-charter.d.ts +79 -0
  56. package/lib/common/relay-hub-charter.js +155 -0
  57. package/lib/common/relay-hub-epochs.d.ts +44 -0
  58. package/lib/common/relay-hub-epochs.js +95 -0
  59. package/lib/common/relay-hub-util.d.ts +39 -7
  60. package/lib/common/relay-hub-util.js +46 -15
  61. package/lib/common/relay-hub.d.ts +13 -0
  62. package/lib/common/relay-hub.js +81 -18
  63. package/lib/common/relay-hubs-admins.d.ts +28 -0
  64. package/lib/common/relay-hubs-admins.js +95 -0
  65. package/lib/common/relay-hubs-connection.d.ts +19 -63
  66. package/lib/common/relay-hubs-connection.js +106 -94
  67. package/lib/common/relay-hubs-identity.d.ts +7 -1
  68. package/lib/common/relay-hubs-identity.js +28 -1
  69. package/lib/common/relay-hubs-live.d.ts +59 -0
  70. package/lib/common/relay-hubs-live.js +31 -0
  71. package/lib/common/relay-hubs-protocol.d.ts +10 -0
  72. package/lib/common/relay-hubs-util.d.ts +9 -0
  73. package/lib/common/relay-hubs-util.js +23 -1
  74. package/lib/common/relay-hubs.d.ts +15 -15
  75. package/lib/common/relay-hubs.js +82 -63
  76. package/lib/common/relay-protocol.d.ts +14 -3
  77. package/lib/common/relay-protocol.js +1 -1
  78. package/lib/common/relay-room-app.d.ts +107 -0
  79. package/lib/common/relay-room-app.js +176 -0
  80. package/lib/common/relay-room-chat.d.ts +6 -1
  81. package/lib/common/relay-room-chat.js +1 -0
  82. package/lib/guest/guest-i18n.d.ts +3 -0
  83. package/lib/guest/guest-i18n.js +12 -0
  84. package/lib/guest/guest-style.d.ts +1 -1
  85. package/lib/guest/guest-style.js +11 -0
  86. package/lib/guest/guest.js +76 -6
  87. package/lib/node/relay-server-accounts.d.ts +1 -1
  88. package/lib/node/relay-server-accounts.js +1 -1
  89. package/package.json +3 -3
  90. package/viewer/join.html +17 -6
  91. package/viewer/room.html +26 -15
@@ -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,79 @@
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
+ * `guests` (RELAY_GUESTS_RULE), those asking from a browser come in without asking; the others are the packages' that
11
+ * read them (ta-rooms: `agents`, `doubles`, `tasks`), each false to forbid.
12
+ */
13
+ export type RelayHubRules = Record<string, boolean>;
14
+ /** The rule that lets those asking from a browser in without asking (they only chat), as many an hour at most. */
15
+ export declare const RELAY_GUESTS_RULE = "guests";
16
+ export declare const RELAY_GUESTS_PER_HOUR = 30;
17
+ /** 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. */
18
+ export interface RelayHubCharterBody {
19
+ room: string;
20
+ v: number;
21
+ admins: RelayHubAdmin[];
22
+ rules: RelayHubRules;
23
+ capacity?: number;
24
+ }
25
+ /** The charter as the roster carries it: `p` its JSON, `s` the owner's signature of it. */
26
+ export interface RelayHubCharter {
27
+ p: string;
28
+ s: string;
29
+ }
30
+ /** A roster opened: who signed it (the owner, or an admin of its charter) and the charter it carries. */
31
+ export interface RelayHubRosterOpened {
32
+ signer: string;
33
+ byAdmin: boolean;
34
+ body: RelayHubRosterBody;
35
+ charter?: RelayHubCharterBody;
36
+ }
37
+ /** Admins of a room at most. */
38
+ export declare const RELAY_HUB_MAX_ADMINS = 8;
39
+ /**
40
+ * The charter of a room (docs/design/ta-link.md 8): the owner names admins and sets rules, signed, carried in the
41
+ * roster; an admin signs a roster that changes members alone, which every member checks against the charter.
42
+ */
43
+ export declare namespace RelayHubCharterUtil {
44
+ /** Rules as kept: names of letters, booleans, a few. */
45
+ function rules(input: unknown): RelayHubRules;
46
+ function seal(owner: RelayIdentity, body: RelayHubCharterBody): Promise<RelayHubCharter>;
47
+ /** Undefined unless signed by the owner, and of this room. */
48
+ function open(ownerSign: string, room: string, charter: unknown): Promise<RelayHubCharterBody | undefined>;
49
+ /**
50
+ * A roster, once it checks: signed by the owner of `owner` (whose entry it has), or by an admin its charter names,
51
+ * the entry of that admin of the identity named. Whether an admin may make such a change is the reader's to judge.
52
+ */
53
+ function openRoster(keys: RelayHubKeys, sealed: string, owner: {
54
+ id: string;
55
+ sign: string;
56
+ }, room: string): Promise<RelayHubRosterOpened | undefined>;
57
+ /**
58
+ * Whether a roster an admin signed only changes what an admin may: the charter no older than the one before, the
59
+ * owner and every admin still in, those who were members still the same identities.
60
+ */
61
+ function adminMay(next: RelayHubRosterOpened, before: {
62
+ members: RelayHubMember[];
63
+ charter?: RelayHubCharterBody;
64
+ } | undefined): boolean;
65
+ /**
66
+ * The answer of an admin to an ask: as the owner's (RelayHubUtil.sealAnswer), with the owner's public identity
67
+ * and the charter naming the admin, which the one who asked checks against the owner of its link.
68
+ */
69
+ function answerProof(head: RelayPublicIdentity, charter: RelayHubCharter): {
70
+ head: RelayPublicIdentity;
71
+ charter: RelayHubCharter;
72
+ };
73
+ /** The one who answered may: the owner of the link, or an admin its charter names (its identity checked). */
74
+ function answeredBy(signer: RelayPublicIdentity, ownerId: string, room: string, proof: {
75
+ head?: RelayPublicIdentity;
76
+ charter?: RelayHubCharter;
77
+ }): Promise<RelayPublicIdentity | undefined>;
78
+ }
79
+ //# sourceMappingURL=relay-hub-charter.d.ts.map
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelayHubCharterUtil = exports.RELAY_HUB_MAX_ADMINS = exports.RELAY_GUESTS_PER_HOUR = exports.RELAY_GUESTS_RULE = void 0;
4
+ const relay_hub_util_1 = require("./relay-hub-util");
5
+ const relay_identity_1 = require("./relay-identity");
6
+ /** The rule that lets those asking from a browser in without asking (they only chat), as many an hour at most. */
7
+ exports.RELAY_GUESTS_RULE = 'guests';
8
+ exports.RELAY_GUESTS_PER_HOUR = 30;
9
+ /** Admins of a room at most. */
10
+ exports.RELAY_HUB_MAX_ADMINS = 8;
11
+ const MAX_RULES = 16;
12
+ /**
13
+ * The charter of a room (docs/design/ta-link.md 8): the owner names admins and sets rules, signed, carried in the
14
+ * roster; an admin signs a roster that changes members alone, which every member checks against the charter.
15
+ */
16
+ var RelayHubCharterUtil;
17
+ (function (RelayHubCharterUtil) {
18
+ /** Rules as kept: names of letters, booleans, a few. */
19
+ function rules(input) {
20
+ if (!input || typeof input !== 'object') {
21
+ return {};
22
+ }
23
+ return Object.fromEntries(Object.entries(input)
24
+ .filter(([name, value]) => /^[a-z][a-zA-Z]{0,30}$/.test(name) && typeof value === 'boolean')
25
+ .slice(0, MAX_RULES));
26
+ }
27
+ RelayHubCharterUtil.rules = rules;
28
+ async function seal(owner, body) {
29
+ const p = JSON.stringify(body);
30
+ return { p, s: await relay_identity_1.RelayIdentityUtil.sign(owner, p) };
31
+ }
32
+ RelayHubCharterUtil.seal = seal;
33
+ /** Undefined unless signed by the owner, and of this room. */
34
+ async function open(ownerSign, room, charter) {
35
+ try {
36
+ const { p, s } = charter;
37
+ if (typeof p !== 'string' || typeof s !== 'string' || !(await relay_identity_1.RelayIdentityUtil.verify(ownerSign, p, s))) {
38
+ return undefined;
39
+ }
40
+ const body = JSON.parse(p);
41
+ if (body.room !== room || !(Number(body.v) > 0) || !Array.isArray(body.admins)) {
42
+ return undefined;
43
+ }
44
+ const capacity = Math.round(Number(body.capacity));
45
+ return {
46
+ room,
47
+ v: Number(body.v),
48
+ admins: body.admins
49
+ .filter(admin => admin && typeof admin.member === 'string' && typeof admin.id === 'string')
50
+ .slice(0, exports.RELAY_HUB_MAX_ADMINS)
51
+ .map(admin => ({ member: admin.member, id: admin.id })),
52
+ rules: rules(body.rules),
53
+ ...(capacity >= 2 ? { capacity } : {}),
54
+ };
55
+ }
56
+ catch (_a) {
57
+ return undefined;
58
+ }
59
+ }
60
+ RelayHubCharterUtil.open = open;
61
+ /**
62
+ * A roster, once it checks: signed by the owner of `owner` (whose entry it has), or by an admin its charter names,
63
+ * the entry of that admin of the identity named. Whether an admin may make such a change is the reader's to judge.
64
+ */
65
+ async function openRoster(keys, sealed, owner, room) {
66
+ try {
67
+ const found = relay_hub_util_1.RelayHubUtil.keyFor(keys, sealed);
68
+ if (!found) {
69
+ return undefined;
70
+ }
71
+ const envelope = JSON.parse(await relay_identity_1.RelayKeys.open(found.key, found.sealed));
72
+ const signed = JSON.parse(envelope.p);
73
+ if (!(signed === null || signed === void 0 ? void 0 : signed.b) || !Array.isArray(signed.b.members) || typeof signed.m !== 'string') {
74
+ return undefined;
75
+ }
76
+ const members = signed.b.members.filter((member) => !!member && typeof member.member === 'string');
77
+ const head = members.find(member => member.owner && member.id === owner.id);
78
+ if (!head) {
79
+ return undefined;
80
+ }
81
+ const charter = signed.b.charter ? await open(owner.sign, room, signed.b.charter) : undefined;
82
+ let key;
83
+ let byAdmin = false;
84
+ if (signed.m === head.member) {
85
+ key = owner.sign;
86
+ }
87
+ else {
88
+ const named = charter === null || charter === void 0 ? void 0 : charter.admins.find(admin => admin.member === signed.m);
89
+ const entry = named && members.find(member => member.member === named.member && member.id === named.id && !member.owner && !member.guest);
90
+ if (entry && (await relay_identity_1.RelayIdentityUtil.check(entry))) {
91
+ key = entry.sign;
92
+ byAdmin = true;
93
+ }
94
+ }
95
+ if (!key || !(await relay_identity_1.RelayIdentityUtil.verify(key, envelope.p, envelope.s))) {
96
+ return undefined;
97
+ }
98
+ return {
99
+ signer: signed.m,
100
+ byAdmin,
101
+ body: { v: Number(signed.b.v), members, ...(signed.b.charter ? { charter: signed.b.charter } : {}) },
102
+ ...(charter ? { charter } : {}),
103
+ };
104
+ }
105
+ catch (_a) {
106
+ return undefined;
107
+ }
108
+ }
109
+ RelayHubCharterUtil.openRoster = openRoster;
110
+ /**
111
+ * Whether a roster an admin signed only changes what an admin may: the charter no older than the one before, the
112
+ * owner and every admin still in, those who were members still the same identities.
113
+ */
114
+ function adminMay(next, before) {
115
+ const charter = next.charter;
116
+ if (!charter || ((before === null || before === void 0 ? void 0 : before.charter) && charter.v < before.charter.v)) {
117
+ return false;
118
+ }
119
+ const has = (member) => next.body.members.some(each => each.member === member);
120
+ if (!charter.admins.every(admin => has(admin.member))) {
121
+ return false;
122
+ }
123
+ const head = before === null || before === void 0 ? void 0 : before.members.find(member => member.owner);
124
+ if (head && !next.body.members.some(member => member.member === head.member && member.owner && member.id === head.id)) {
125
+ return false;
126
+ }
127
+ return next.body.members.every(member => {
128
+ const was = before === null || before === void 0 ? void 0 : before.members.find(each => each.member === member.member);
129
+ return !was || (was.id === member.id && !!was.owner === !!member.owner);
130
+ });
131
+ }
132
+ RelayHubCharterUtil.adminMay = adminMay;
133
+ /**
134
+ * The answer of an admin to an ask: as the owner's (RelayHubUtil.sealAnswer), with the owner's public identity
135
+ * and the charter naming the admin, which the one who asked checks against the owner of its link.
136
+ */
137
+ function answerProof(head, charter) {
138
+ return { head: relay_identity_1.RelayIdentityUtil.publicOf(head), charter };
139
+ }
140
+ RelayHubCharterUtil.answerProof = answerProof;
141
+ /** The one who answered may: the owner of the link, or an admin its charter names (its identity checked). */
142
+ async function answeredBy(signer, ownerId, room, proof) {
143
+ if (signer.id === ownerId) {
144
+ return (await relay_identity_1.RelayIdentityUtil.check(signer)) ? signer : undefined;
145
+ }
146
+ const head = proof.head;
147
+ if (!head || head.id !== ownerId || !(await relay_identity_1.RelayIdentityUtil.check(head)) || !(await relay_identity_1.RelayIdentityUtil.check(signer))) {
148
+ return undefined;
149
+ }
150
+ const charter = await open(head.sign, room, proof.charter);
151
+ return (charter === null || charter === void 0 ? void 0 : charter.admins.some(admin => admin.id === signer.id)) ? head : undefined;
152
+ }
153
+ RelayHubCharterUtil.answeredBy = answeredBy;
154
+ })(RelayHubCharterUtil || (exports.RelayHubCharterUtil = RelayHubCharterUtil = {}));
155
+ //# 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
@@ -1,3 +1,4 @@
1
+ import { type RelayHubCharter } from './relay-hub-charter';
1
2
  import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity';
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;