@minca/core 0.1.9 → 0.1.10

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 (64) hide show
  1. package/README.md +1 -1
  2. package/dist/identity/abilities.d.ts +3 -3
  3. package/dist/identity/abilities.d.ts.map +1 -1
  4. package/dist/identity/abilities.js +9 -11
  5. package/dist/identity/abilities.js.map +1 -1
  6. package/dist/identity/root-signer.d.ts +0 -5
  7. package/dist/identity/root-signer.d.ts.map +1 -1
  8. package/dist/identity/root-signer.js +0 -1
  9. package/dist/identity/root-signer.js.map +1 -1
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js.map +1 -1
  13. package/dist/node/node.d.ts.map +1 -1
  14. package/dist/node/node.js +230 -253
  15. package/dist/node/node.js.map +1 -1
  16. package/dist/node/remote.d.ts +6 -5
  17. package/dist/node/remote.d.ts.map +1 -1
  18. package/dist/node/remote.js +141 -194
  19. package/dist/node/remote.js.map +1 -1
  20. package/dist/node/space-runtime.d.ts.map +1 -1
  21. package/dist/node/space-runtime.js +69 -82
  22. package/dist/node/space-runtime.js.map +1 -1
  23. package/dist/node/types.d.ts +12 -4
  24. package/dist/node/types.d.ts.map +1 -1
  25. package/dist/node/worker.d.ts +1 -1
  26. package/dist/node/worker.d.ts.map +1 -1
  27. package/dist/node/worker.js +10 -6
  28. package/dist/node/worker.js.map +1 -1
  29. package/dist/session/auth.d.ts +20 -0
  30. package/dist/session/auth.d.ts.map +1 -1
  31. package/dist/session/auth.js +80 -63
  32. package/dist/session/auth.js.map +1 -1
  33. package/dist/session/connect.d.ts +50 -8
  34. package/dist/session/connect.d.ts.map +1 -1
  35. package/dist/session/connect.js +91 -27
  36. package/dist/session/connect.js.map +1 -1
  37. package/dist/session/connection.d.ts +39 -6
  38. package/dist/session/connection.d.ts.map +1 -1
  39. package/dist/session/connection.js +107 -38
  40. package/dist/session/connection.js.map +1 -1
  41. package/dist/session/hosting.d.ts +15 -0
  42. package/dist/session/hosting.d.ts.map +1 -1
  43. package/dist/session/hosting.js +55 -4
  44. package/dist/session/hosting.js.map +1 -1
  45. package/dist/session/index.d.ts +5 -5
  46. package/dist/session/index.d.ts.map +1 -1
  47. package/dist/session/index.js +2 -2
  48. package/dist/session/index.js.map +1 -1
  49. package/dist/space/account-registry.d.ts +14 -4
  50. package/dist/space/account-registry.d.ts.map +1 -1
  51. package/dist/space/account-registry.js +12 -3
  52. package/dist/space/account-registry.js.map +1 -1
  53. package/dist/space/roles.d.ts +59 -140
  54. package/dist/space/roles.d.ts.map +1 -1
  55. package/dist/space/roles.js +328 -427
  56. package/dist/space/roles.js.map +1 -1
  57. package/docs/actions.md +3 -3
  58. package/docs/agents.md +2 -2
  59. package/docs/blueprints-and-guides.md +20 -1
  60. package/docs/bots.md +1 -1
  61. package/docs/node.md +99 -53
  62. package/docs/sign-in.md +16 -9
  63. package/docs/storage.md +1 -1
  64. package/package.json +1 -1
@@ -24,7 +24,7 @@ import { contactKeyPair, isContactPublicKey } from '../identity/contact-key.js';
24
24
  import { openValueFrom, sealValueTo } from '../identity/hpke.js';
25
25
  import { openValue, sealValue } from '../identity/envelope.js';
26
26
  import { verifyNote } from '../identity/ucan.js';
27
- import { ACCESS_COMMAND, accessRequirement, accountSpaceRequirement, DEFINE_COMMAND, noteGrants, writeRequirement, writesRecords, } from '../identity/abilities.js';
27
+ import { ACCESS_COMMAND, accessRequirement, actsForAccount, DEFINE_COMMAND, noteGrants, writeRequirement, writesRecords, } from '../identity/abilities.js';
28
28
  import { bodyProblem, canonicalize, createVersion, getVersionId, isStub, signedPart, stubOf, } from '../schema/version.js';
29
29
  import { createStorageProvider } from '../storage/storage-provider.js';
30
30
  import { chainProblem, MAX_SEEN, nextVersion } from '../records/version.js';
@@ -44,7 +44,7 @@ import { createWebSocketTransport } from '../network/ws-transport.js';
44
44
  import { createClientAuth, createMeshAuth, MAX_EARLIER_READ_KEYS, } from '../network/peer-auth.js';
45
45
  import { BOX_COLLECTION, MEMBER_KEY_COLLECTION, SPACE_KEY_RECORD, SPACE_RELAYS_RECORD, SPACE_KEEPERS_RECORD, memberKeyRecordKey, BOX_LABEL, boxBound, boxKey, deriveInviteKey, deriveReadKey, generateInviteSecret, inviteKey as inviteRecordKey, assignKey, removalKey, revokeKey, roleKey, signInvite, verifyInvite, forkKey, stepDownKey, succeedKey, WARRANT_COLLECTION, } from '../space/space-access.js';
46
46
  import { accessGenesisOf, isSpaceId } from '../space/constitution.js';
47
- import { ACCESS_COLLECTIONS, ASSIGN_COLLECTION, DEFINITION_COLLECTION, INVITE_COLLECTION, KEY_COLLECTION, MAX_RELAYS, RELAYS_COLLECTION, checkRelays, KEEPERS_COLLECTION, FORK_COLLECTION, checkKeepers, REMOVE_COLLECTION, REVOKE_COLLECTION, ROLE_COLLECTION, STEPDOWN_COLLECTION, SUCCEED_COLLECTION, headless, isMember, holdsRole, may, readers, replayAccess, refusal, rolesOf, } from '../space/roles.js';
47
+ import { ACCESS_COLLECTIONS, ACCESS_KINDS, ASSIGN_COLLECTION, DEFINITION_COLLECTION, INVITE_COLLECTION, KEY_COLLECTION, MAX_RELAYS, RELAYS_COLLECTION, checkRelays, KEEPERS_COLLECTION, FORK_COLLECTION, checkKeepers, REMOVE_COLLECTION, REVOKE_COLLECTION, ROLE_COLLECTION, STEPDOWN_COLLECTION, SUCCEED_COLLECTION, headless, isMember, holdsRole, may, readers, replayAccess, refusal, rolesOf, } from '../space/roles.js';
48
48
  import { checkFoundingRole, definitionBody, KEY_CHANGE, readDefinition, RECORD_DELETE, RECORD_WRITE, SPACE_RELAYS, } from '../space/grants.js';
49
49
  import { FOUNDER } from '../space/space-manager.js';
50
50
  import { createSyncEngine } from '../sync/sync-engine.js';
@@ -78,35 +78,12 @@ const MANAGED = new Set([
78
78
  * read the records has no use for their rules.
79
79
  */
80
80
  const IN_THE_CLEAR = new Set([
81
- ROLE_COLLECTION,
82
- ASSIGN_COLLECTION,
83
- REMOVE_COLLECTION,
84
- INVITE_COLLECTION,
85
- REVOKE_COLLECTION,
86
- KEY_COLLECTION,
81
+ ...Object.values(ACCESS_KINDS)
82
+ .filter(({ clear }) => clear)
83
+ .map(({ collection }) => collection),
87
84
  BOX_COLLECTION,
88
85
  MEMBER_KEY_COLLECTION,
89
- RELAYS_COLLECTION,
90
- KEEPERS_COLLECTION,
91
- FORK_COLLECTION,
92
- STEPDOWN_COLLECTION,
93
- SUCCEED_COLLECTION,
94
86
  ]);
95
- /** Which collection each access record key belongs to */
96
- const ACCESS_KEYS = [
97
- ['role:', ROLE_COLLECTION],
98
- ['assign:', ASSIGN_COLLECTION],
99
- ['remove:', REMOVE_COLLECTION],
100
- ['invite:', INVITE_COLLECTION],
101
- ['revoke:', REVOKE_COLLECTION],
102
- ['collection:', DEFINITION_COLLECTION],
103
- ['key:', KEY_COLLECTION],
104
- ['relays:', RELAYS_COLLECTION],
105
- ['keepers:', KEEPERS_COLLECTION],
106
- ['fork:', FORK_COLLECTION],
107
- ['stepdown:', STEPDOWN_COLLECTION],
108
- ['succeed:', SUCCEED_COLLECTION],
109
- ];
110
87
  /** How many keys a cutoff may name */
111
88
  const MAX_CUTOFF = 1024;
112
89
  /** A change that takes nothing away covers nothing */
@@ -182,6 +159,24 @@ const grantsBeyondRecords = (proof, spaceId, cmd) => noteGrants(proof, { space:
182
159
  export const CANNOT_DEFINE = "This key's note doesn't let it add or change collections. Propose them instead (collections_propose), and someone who may adds them.";
183
160
  const CANNOT_CHANGE_ACCESS = "This key's note doesn't let it change who may do what in a space. Ask the person.";
184
161
  const NOT_ACCOUNT = "This key's note doesn't let it change the account itself. Ask the person.";
162
+ /**
163
+ * Whether a version is about the account that signs it: its own place in the
164
+ * space (taking or giving up a role, leaving, stepping down), what others
165
+ * seal to it or see of it, or another of its keys. Only a key acting as the
166
+ * account itself writes one (spec 01 §8), so an app given a space never
167
+ * changes the account's standing there.
168
+ */
169
+ function aboutItself(collection, body, root, author) {
170
+ if (collection === STEPDOWN_COLLECTION || collection === MEMBER_KEY_COLLECTION)
171
+ return true;
172
+ if (collection === PROFILE_COLLECTION)
173
+ return true;
174
+ if (!isRecord(body))
175
+ return false;
176
+ if (collection === ASSIGN_COLLECTION || collection === REMOVE_COLLECTION)
177
+ return body.did === root;
178
+ return collection === REVOKE_COLLECTION && body.key !== author;
179
+ }
185
180
  const NO_ACCOUNT = Object.freeze({ account: null, recordsOnly: false });
186
181
  /** How long a peer checked here has to become a peer before the check is forgotten: past any handshake's timeout */
187
182
  const STALE_ADMISSION_MS = 60_000;
@@ -517,6 +512,9 @@ export async function openSpaceRuntime(deps) {
517
512
  const verdict = await judge(version);
518
513
  if (!verdict.verified || !verdict.root)
519
514
  return null;
515
+ if (!actsForAccount(version.proof) &&
516
+ aboutItself(version.collection, version.body, verdict.root, version.author))
517
+ return null;
520
518
  const seen = version.seen ?? [];
521
519
  const base = {
522
520
  id: version.id,
@@ -613,11 +611,12 @@ export async function openSpaceRuntime(deps) {
613
611
  }
614
612
  case INVITE_COLLECTION: {
615
613
  const inviteDid = body.key;
616
- if (typeof inviteDid !== 'string' || typeof body.role !== 'string' || typeof body.open !== 'boolean')
614
+ // An invite carries no cutoff: closing it takes nothing from anyone but who joins through it unseen.
615
+ if (body.cutoff !== undefined || typeof inviteDid !== 'string' || typeof body.role !== 'string')
617
616
  return null;
618
- if (version.key !== (await inviteRecordKey(inviteDid)))
617
+ if (typeof body.open !== 'boolean' || version.key !== (await inviteRecordKey(inviteDid)))
619
618
  return null;
620
- return { ...base, cutoff, kind: 'invite', inviteKey: inviteDid, role: body.role, open: body.open };
619
+ return { ...base, kind: 'invite', inviteKey: inviteDid, role: body.role, open: body.open };
621
620
  }
622
621
  case KEY_COLLECTION: {
623
622
  if (version.key !== SPACE_KEY_RECORD || typeof body.keyId !== 'string' || body.keyId.length > 64)
@@ -708,7 +707,7 @@ export async function openSpaceRuntime(deps) {
708
707
  let unsettled = false;
709
708
  // By key, not by the current version's collection: whatever sits on top
710
709
  // may be anyone's, and every version of the history counts.
711
- for (const [prefix, collection] of ACCESS_KEYS) {
710
+ for (const { prefix, collection } of Object.values(ACCESS_KINDS)) {
712
711
  for (const versions of (await storage.histories(prefix)).values()) {
713
712
  for (const held of versions) {
714
713
  if (held.collection !== collection)
@@ -742,7 +741,7 @@ export async function openSpaceRuntime(deps) {
742
741
  return loadAccess();
743
742
  let pending = [];
744
743
  for (const version of placed) {
745
- if (!ACCESS_KEYS.some(([prefix, collection]) => version.key.startsWith(prefix) && version.collection === collection))
744
+ if (!Object.values(ACCESS_KINDS).some(({ prefix, collection }) => version.key.startsWith(prefix) && version.collection === collection))
746
745
  continue;
747
746
  const event = await toEvent(version);
748
747
  if (event)
@@ -950,8 +949,9 @@ export async function openSpaceRuntime(deps) {
950
949
  const verdict = await judge(version);
951
950
  if (!verdict.verified || !verdict.root)
952
951
  return { ok: false, reason: verdict.reason ?? 'Its signature does not check out' };
953
- if (deps.accountOnly && !noteGrants(version.proof, accountSpaceRequirement(space.id, version.collection)))
954
- return { ok: false, reason: "Its note doesn't let it write in the account's own spaces" };
952
+ const asAccount = deps.accountOnly || aboutItself(version.collection, version.body, verdict.root, version.author);
953
+ if (asAccount && !actsForAccount(version.proof))
954
+ return { ok: false, reason: "Its note doesn't let it act as the account itself" };
955
955
  const { history } = await access();
956
956
  if (ACCESS_COLLECTIONS.has(version.collection)) {
957
957
  const event = await toEvent(version);
@@ -1753,6 +1753,9 @@ export async function openSpaceRuntime(deps) {
1753
1753
  */
1754
1754
  let memberKeysCache = null;
1755
1755
  const memberKeyMap = () => (memberKeysCache ??= loadMemberKeys());
1756
+ /** Every key box, read once until records change */
1757
+ let boxCache = null;
1758
+ const boxIndex = () => (boxCache ??= storage.histories('box:'));
1756
1759
  async function loadMemberKeys() {
1757
1760
  const found = new Map();
1758
1761
  // By key: the current version may be anyone's.
@@ -1847,6 +1850,7 @@ export async function openSpaceRuntime(deps) {
1847
1850
  catalogCache = null;
1848
1851
  profilesCache = null;
1849
1852
  memberKeysCache = null;
1853
+ boxCache = null;
1850
1854
  accessCache = placed && accessCache ? extendAccess(accessCache, placed) : null;
1851
1855
  standings.clear();
1852
1856
  checkVerdicts.clear();
@@ -1908,7 +1912,7 @@ export async function openSpaceRuntime(deps) {
1908
1912
  return true;
1909
1913
  };
1910
1914
  if (memberPair && epochs.some((epoch) => !keyring.has(epoch.keyId))) {
1911
- for (const versions of (await storage.histories('box:')).values()) {
1915
+ for (const versions of (await boxIndex()).values()) {
1912
1916
  for (const version of versions) {
1913
1917
  if (version.collection !== BOX_COLLECTION || version.deleted || opened.has(version.id))
1914
1918
  continue;
@@ -2027,7 +2031,7 @@ export async function openSpaceRuntime(deps) {
2027
2031
  if (!epoch || !key)
2028
2032
  return;
2029
2033
  const boxed = new Set();
2030
- for (const versions of (await storage.histories('box:')).values()) {
2034
+ for (const versions of (await boxIndex()).values()) {
2031
2035
  for (const version of versions) {
2032
2036
  const body = isRecord(version.body) ? version.body : null;
2033
2037
  if (version.collection !== BOX_COLLECTION ||
@@ -2075,7 +2079,10 @@ export async function openSpaceRuntime(deps) {
2075
2079
  return;
2076
2080
  }
2077
2081
  const mine = await memberKeyMap();
2078
- if (memberPair && mine.get(deps.rootDid) !== memberPair.publicKey && !(await cannotWrite())) {
2082
+ if (memberPair &&
2083
+ mine.get(deps.rootDid) !== memberPair.publicKey &&
2084
+ actsForAccount(session.proof()) &&
2085
+ !(await cannotWrite())) {
2079
2086
  await upsert(MEMBER_KEY_COLLECTION, await memberKeyRecordKey(deps.rootDid), {
2080
2087
  key: memberPair.publicKey,
2081
2088
  });
@@ -2815,7 +2822,8 @@ export async function openSpaceRuntime(deps) {
2815
2822
  /** Signs and stores one version. A delete carries no body. */
2816
2823
  async function write(collection, body, version, deleted = false, links = [], options = {}) {
2817
2824
  const writer = options.as ?? session;
2818
- if (deps.accountOnly && !noteGrants(writer.proof(), accountSpaceRequirement(space.id, collection)))
2825
+ const asAccount = deps.accountOnly || aboutItself(collection, body, deps.rootDid, writer.did);
2826
+ if (asAccount && !actsForAccount(writer.proof()))
2819
2827
  throw new Error(NOT_ACCOUNT);
2820
2828
  // Every peer would ignore it (see buildEvent); say why here instead.
2821
2829
  if (ACCESS_COLLECTIONS.has(collection) &&
@@ -2880,8 +2888,6 @@ export async function openSpaceRuntime(deps) {
2880
2888
  collection === PROFILE_COLLECTION ||
2881
2889
  collection === BOX_COLLECTION ||
2882
2890
  collection === MEMBER_KEY_COLLECTION ||
2883
- // What a removed carrier was is read back after the delete (`nameKeepers`).
2884
- collection === CARRIER_COLLECTION ||
2885
2891
  definition?.history === 'all' ||
2886
2892
  (version.seq === 0 && !!(definition?.rules?.onePer || definition?.rules?.fixed)) ||
2887
2893
  // Every version under a contract: it reads the one before, and others cite it.
@@ -3466,64 +3472,47 @@ export async function openSpaceRuntime(deps) {
3466
3472
  * all it holds.
3467
3473
  */
3468
3474
  async function cutoffFor(did, only) {
3469
- return packCutoff([(await linesOf(new Set([did]), only)).get(did) ?? []]);
3470
- }
3471
- /**
3472
- * At most what a change may carry, shared among the accounts it reaches: one
3473
- * line from each in turn, so no one's writing can use up anyone else's
3474
- * share. What is left out is judged with the change.
3475
- */
3476
- function packCutoff(accounts) {
3477
3475
  const cutoff = {};
3478
- let named = 0;
3479
- for (let round = 0; named < MAX_CUTOFF && accounts.some((lines) => round < lines.length); round++)
3480
- for (const lines of accounts) {
3481
- const end = lines[round];
3482
- if (!end || named >= MAX_CUTOFF)
3483
- continue;
3484
- (cutoff[end.author] ??= {})[end.collection] = end.log.n;
3485
- named++;
3486
- }
3476
+ // At most what a change may carry: what is left out is judged with the change.
3477
+ for (const end of (await linesOf(did, only)).slice(0, MAX_CUTOFF))
3478
+ (cutoff[end.author] ??= {})[end.collection] = end.log.n;
3487
3479
  return cutoff;
3488
3480
  }
3489
3481
  /**
3490
- * The ends of each account's unbroken lines this node holds: defined
3491
- * collections first, then the rest, each in name order. One pass over what
3492
- * it holds, however many accounts.
3482
+ * The ends of an account's unbroken lines this node holds: defined
3483
+ * collections first, then the rest, each in name order.
3493
3484
  */
3494
- async function linesOf(dids, only) {
3485
+ async function linesOf(did, only) {
3495
3486
  // What it holds of theirs, whatever way it came: the lines' places are kept only for what arrived by sync.
3496
3487
  const placed = new Map();
3497
- const add = (did, version) => {
3488
+ const add = (version) => {
3498
3489
  if (!version.log)
3499
3490
  return;
3500
3491
  const line = `${version.author} ${version.collection}`;
3501
- const entry = placed.get(line) ?? { did, held: new Map() };
3502
- if (!entry.held.has(version.log.n))
3503
- entry.held.set(version.log.n, version);
3504
- placed.set(line, entry);
3492
+ const held = placed.get(line) ?? new Map();
3493
+ if (!held.has(version.log.n))
3494
+ held.set(version.log.n, version);
3495
+ placed.set(line, held);
3505
3496
  };
3506
3497
  for (const version of await everyCurrent()) {
3507
3498
  if (!version.log || (only && version.author !== only) || version.space !== space.id)
3508
3499
  continue;
3509
- const root = (await judge(version)).root;
3510
- if (root && dids.has(root))
3511
- add(root, version);
3500
+ if ((await judge(version)).root === did)
3501
+ add(version);
3512
3502
  }
3513
3503
  for (const name of await adapter.list(KEY_ROOT_PREFIX)) {
3514
3504
  const key = name.slice(KEY_ROOT_PREFIX.length);
3515
- const did = utf8Decode((await adapter.get(name)) ?? new Uint8Array());
3516
- if ((only && key !== only) || !dids.has(did))
3505
+ if ((only && key !== only) || utf8Decode((await adapter.get(name)) ?? new Uint8Array()) !== did)
3517
3506
  continue;
3518
3507
  for (const place of await adapter.list(`${LOG_PREFIX}${key}/`)) {
3519
3508
  const id = await adapter.get(place);
3520
3509
  const version = id ? await storage.getVersion(utf8Decode(id)) : null;
3521
3510
  if (version?.author === key)
3522
- add(did, version);
3511
+ add(version);
3523
3512
  }
3524
3513
  }
3525
- const byAccount = new Map();
3526
- for (const { did, held } of placed.values()) {
3514
+ const ends = [];
3515
+ for (const held of placed.values()) {
3527
3516
  let end = null;
3528
3517
  for (let n = 0;; n++) {
3529
3518
  const version = held.get(n);
@@ -3532,14 +3521,12 @@ export async function openSpaceRuntime(deps) {
3532
3521
  end = version;
3533
3522
  }
3534
3523
  if (end)
3535
- byAccount.set(did, [...(byAccount.get(did) ?? []), end]);
3524
+ ends.push(end);
3536
3525
  }
3537
3526
  // Defined collections first: writing into many undefined ones costs only whoever did.
3538
3527
  const defined = new Set((await catalog()).keys());
3539
- for (const ends of byAccount.values())
3540
- ends.sort((a, b) => Number(defined.has(b.collection)) - Number(defined.has(a.collection)) ||
3541
- (a.collection < b.collection ? -1 : a.collection > b.collection ? 1 : a.author < b.author ? -1 : 1));
3542
- return byAccount;
3528
+ return ends.sort((a, b) => Number(defined.has(b.collection)) - Number(defined.has(a.collection)) ||
3529
+ (a.collection < b.collection ? -1 : a.collection > b.collection ? 1 : a.author < b.author ? -1 : 1));
3543
3530
  }
3544
3531
  /** Whether this account holds a grant, now, to do something to a record */
3545
3532
  async function mayNow(collection, action, first) {
@@ -3632,8 +3619,8 @@ export async function openSpaceRuntime(deps) {
3632
3619
  },
3633
3620
  /** For the node itself: says who this account is here, when that changed and the space takes its writes */
3634
3621
  async publishProfile(profile) {
3635
- // Someone following a space without a role in it cannot write there, and says nothing.
3636
- if (await cannotWrite())
3622
+ // Someone following a space without a role in it cannot write there, and says nothing; nor does an app.
3623
+ if (!actsForAccount(session.proof()) || (await cannotWrite()))
3637
3624
  return;
3638
3625
  const name = profile.name.trim().slice(0, 64);
3639
3626
  const mine = (await profileMap()).get(deps.rootDid);