@minca/core 0.1.15 → 0.1.17
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.
- package/README.md +10 -10
- package/dist/elements/auth-styles.d.ts +1 -1
- package/dist/elements/auth-styles.d.ts.map +1 -1
- package/dist/elements/auth-styles.js +35 -9
- package/dist/elements/auth-styles.js.map +1 -1
- package/dist/elements/minca-auth.d.ts.map +1 -1
- package/dist/elements/minca-auth.js +169 -72
- package/dist/elements/minca-auth.js.map +1 -1
- package/dist/identity/abilities.d.ts +2 -0
- package/dist/identity/abilities.d.ts.map +1 -1
- package/dist/identity/abilities.js +2 -0
- package/dist/identity/abilities.js.map +1 -1
- package/dist/identity/account-vault.d.ts +15 -71
- package/dist/identity/account-vault.d.ts.map +1 -1
- package/dist/identity/account-vault.js +45 -104
- package/dist/identity/account-vault.js.map +1 -1
- package/dist/identity/channel.d.ts +3 -9
- package/dist/identity/channel.d.ts.map +1 -1
- package/dist/identity/channel.js +42 -42
- package/dist/identity/channel.js.map +1 -1
- package/dist/identity/contact-key.d.ts +14 -29
- package/dist/identity/contact-key.d.ts.map +1 -1
- package/dist/identity/contact-key.js +31 -51
- package/dist/identity/contact-key.js.map +1 -1
- package/dist/identity/crypto-p256.d.ts.map +1 -1
- package/dist/identity/crypto-p256.js +39 -53
- package/dist/identity/crypto-p256.js.map +1 -1
- package/dist/identity/hpke.d.ts.map +1 -1
- package/dist/identity/hpke.js +19 -19
- package/dist/identity/hpke.js.map +1 -1
- package/dist/identity/identity-manager.d.ts.map +1 -1
- package/dist/identity/identity-manager.js +1 -2
- package/dist/identity/identity-manager.js.map +1 -1
- package/dist/identity/policy.d.ts.map +1 -1
- package/dist/identity/policy.js +35 -61
- package/dist/identity/policy.js.map +1 -1
- package/dist/identity/recovery-code.d.ts +2 -8
- package/dist/identity/recovery-code.d.ts.map +1 -1
- package/dist/identity/recovery-code.js +4 -11
- package/dist/identity/recovery-code.js.map +1 -1
- package/dist/identity/root-signer.d.ts +1 -6
- package/dist/identity/root-signer.d.ts.map +1 -1
- package/dist/identity/root-signer.js +12 -15
- package/dist/identity/root-signer.js.map +1 -1
- package/dist/identity/ucan.d.ts.map +1 -1
- package/dist/identity/ucan.js +48 -39
- package/dist/identity/ucan.js.map +1 -1
- package/dist/identity/webauthn.d.ts +6 -11
- package/dist/identity/webauthn.d.ts.map +1 -1
- package/dist/identity/webauthn.js +18 -30
- package/dist/identity/webauthn.js.map +1 -1
- package/dist/inbox/inbox.d.ts.map +1 -1
- package/dist/inbox/inbox.js +40 -30
- package/dist/inbox/inbox.js.map +1 -1
- package/dist/inbox/index.d.ts +2 -0
- package/dist/inbox/index.d.ts.map +1 -1
- package/dist/inbox/index.js +1 -0
- package/dist/inbox/index.js.map +1 -1
- package/dist/inbox/names.d.ts +54 -0
- package/dist/inbox/names.d.ts.map +1 -0
- package/dist/inbox/names.js +299 -0
- package/dist/inbox/names.js.map +1 -0
- package/dist/index.d.ts +3 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -12
- package/dist/index.js.map +1 -1
- package/dist/network/mesh.d.ts +7 -20
- package/dist/network/mesh.d.ts.map +1 -1
- package/dist/network/mesh.js +267 -245
- package/dist/network/mesh.js.map +1 -1
- package/dist/network/relay-link.d.ts +31 -0
- package/dist/network/relay-link.d.ts.map +1 -0
- package/dist/network/relay-link.js +62 -0
- package/dist/network/relay-link.js.map +1 -0
- package/dist/network/relay-minca.d.ts.map +1 -1
- package/dist/network/relay-minca.js +11 -49
- package/dist/network/relay-minca.js.map +1 -1
- package/dist/network/relay-nostr.d.ts.map +1 -1
- package/dist/network/relay-nostr.js +23 -63
- package/dist/network/relay-nostr.js.map +1 -1
- package/dist/network/relay-socket.d.ts +1 -1
- package/dist/network/relay-socket.d.ts.map +1 -1
- package/dist/network/relay-socket.js +3 -1
- package/dist/network/relay-socket.js.map +1 -1
- package/dist/network/ws-transport.d.ts.map +1 -1
- package/dist/network/ws-transport.js +3 -0
- package/dist/network/ws-transport.js.map +1 -1
- package/dist/node/actions.d.ts +2 -0
- package/dist/node/actions.d.ts.map +1 -1
- package/dist/node/actions.js +89 -58
- package/dist/node/actions.js.map +1 -1
- package/dist/node/api.d.ts +7 -0
- package/dist/node/api.d.ts.map +1 -1
- package/dist/node/api.js +7 -0
- package/dist/node/api.js.map +1 -1
- package/dist/node/carrier.d.ts +5 -0
- package/dist/node/carrier.d.ts.map +1 -1
- package/dist/node/carrier.js +94 -73
- package/dist/node/carrier.js.map +1 -1
- package/dist/node/copy.d.ts +8 -7
- package/dist/node/copy.d.ts.map +1 -1
- package/dist/node/copy.js +54 -56
- package/dist/node/copy.js.map +1 -1
- package/dist/node/host.d.ts +39 -65
- package/dist/node/host.d.ts.map +1 -1
- package/dist/node/host.js +362 -379
- package/dist/node/host.js.map +1 -1
- package/dist/node/index.d.ts +2 -2
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +1 -1
- package/dist/node/index.js.map +1 -1
- package/dist/node/node-carriers.d.ts +35 -0
- package/dist/node/node-carriers.d.ts.map +1 -0
- package/dist/node/node-carriers.js +567 -0
- package/dist/node/node-carriers.js.map +1 -0
- package/dist/node/node-contacts.d.ts +11 -0
- package/dist/node/node-contacts.d.ts.map +1 -0
- package/dist/node/node-contacts.js +808 -0
- package/dist/node/node-contacts.js.map +1 -0
- package/dist/node/node-context.d.ts +76 -0
- package/dist/node/node-context.d.ts.map +1 -0
- package/dist/node/node-context.js +54 -0
- package/dist/node/node-context.js.map +1 -0
- package/dist/node/node-hosting.d.ts +20 -0
- package/dist/node/node-hosting.d.ts.map +1 -0
- package/dist/node/node-hosting.js +456 -0
- package/dist/node/node-hosting.js.map +1 -0
- package/dist/node/node-records.d.ts +18 -0
- package/dist/node/node-records.d.ts.map +1 -0
- package/dist/node/node-records.js +262 -0
- package/dist/node/node-records.js.map +1 -0
- package/dist/node/node.d.ts.map +1 -1
- package/dist/node/node.js +199 -1990
- package/dist/node/node.js.map +1 -1
- package/dist/node/remote.d.ts +5 -6
- package/dist/node/remote.d.ts.map +1 -1
- package/dist/node/remote.js +272 -260
- package/dist/node/remote.js.map +1 -1
- package/dist/node/space-catalog.d.ts +40 -0
- package/dist/node/space-catalog.d.ts.map +1 -0
- package/dist/node/space-catalog.js +158 -0
- package/dist/node/space-catalog.js.map +1 -0
- package/dist/node/space-checks.d.ts +25 -0
- package/dist/node/space-checks.d.ts.map +1 -0
- package/dist/node/space-checks.js +343 -0
- package/dist/node/space-checks.js.map +1 -0
- package/dist/node/space-history.d.ts +37 -0
- package/dist/node/space-history.d.ts.map +1 -0
- package/dist/node/space-history.js +350 -0
- package/dist/node/space-history.js.map +1 -0
- package/dist/node/space-holding.d.ts +53 -0
- package/dist/node/space-holding.d.ts.map +1 -0
- package/dist/node/space-holding.js +339 -0
- package/dist/node/space-holding.js.map +1 -0
- package/dist/node/space-index.d.ts +41 -0
- package/dist/node/space-index.d.ts.map +1 -0
- package/dist/node/space-index.js +217 -0
- package/dist/node/space-index.js.map +1 -0
- package/dist/node/space-keys.d.ts +34 -0
- package/dist/node/space-keys.d.ts.map +1 -0
- package/dist/node/space-keys.js +309 -0
- package/dist/node/space-keys.js.map +1 -0
- package/dist/node/space-lines.d.ts +33 -0
- package/dist/node/space-lines.d.ts.map +1 -0
- package/dist/node/space-lines.js +145 -0
- package/dist/node/space-lines.js.map +1 -0
- package/dist/node/space-membership.d.ts +69 -0
- package/dist/node/space-membership.d.ts.map +1 -0
- package/dist/node/space-membership.js +242 -0
- package/dist/node/space-membership.js.map +1 -0
- package/dist/node/space-mirrors.d.ts +43 -0
- package/dist/node/space-mirrors.d.ts.map +1 -0
- package/dist/node/space-mirrors.js +125 -0
- package/dist/node/space-mirrors.js.map +1 -0
- package/dist/node/space-peers.d.ts +104 -0
- package/dist/node/space-peers.d.ts.map +1 -0
- package/dist/node/space-peers.js +629 -0
- package/dist/node/space-peers.js.map +1 -0
- package/dist/node/space-profiles.d.ts +18 -0
- package/dist/node/space-profiles.d.ts.map +1 -0
- package/dist/node/space-profiles.js +105 -0
- package/dist/node/space-profiles.js.map +1 -0
- package/dist/node/space-rules.d.ts +29 -0
- package/dist/node/space-rules.d.ts.map +1 -0
- package/dist/node/space-rules.js +144 -0
- package/dist/node/space-rules.js.map +1 -0
- package/dist/node/space-runtime.d.ts +107 -140
- package/dist/node/space-runtime.d.ts.map +1 -1
- package/dist/node/space-runtime.js +331 -3985
- package/dist/node/space-runtime.js.map +1 -1
- package/dist/node/space-standing.d.ts +20 -0
- package/dist/node/space-standing.d.ts.map +1 -0
- package/dist/node/space-standing.js +317 -0
- package/dist/node/space-standing.js.map +1 -0
- package/dist/node/space-versions.d.ts +104 -0
- package/dist/node/space-versions.d.ts.map +1 -0
- package/dist/node/space-versions.js +153 -0
- package/dist/node/space-versions.js.map +1 -0
- package/dist/node/space-warrants.d.ts +25 -0
- package/dist/node/space-warrants.d.ts.map +1 -0
- package/dist/node/space-warrants.js +214 -0
- package/dist/node/space-warrants.js.map +1 -0
- package/dist/node/space-writing.d.ts +74 -0
- package/dist/node/space-writing.d.ts.map +1 -0
- package/dist/node/space-writing.js +442 -0
- package/dist/node/space-writing.js.map +1 -0
- package/dist/node/stores.d.ts +19 -50
- package/dist/node/stores.d.ts.map +1 -1
- package/dist/node/stores.js +14 -33
- package/dist/node/stores.js.map +1 -1
- package/dist/node/types.d.ts +243 -306
- package/dist/node/types.d.ts.map +1 -1
- package/dist/node/usage.d.ts +2 -24
- package/dist/node/usage.d.ts.map +1 -1
- package/dist/node/usage.js +118 -102
- package/dist/node/usage.js.map +1 -1
- package/dist/node/watch-notifications.d.ts.map +1 -1
- package/dist/node/watch-notifications.js +32 -29
- package/dist/node/watch-notifications.js.map +1 -1
- package/dist/node/worker.d.ts +5 -6
- package/dist/node/worker.d.ts.map +1 -1
- package/dist/node/worker.js +80 -72
- package/dist/node/worker.js.map +1 -1
- package/dist/node/writer-keys.d.ts +5 -6
- package/dist/node/writer-keys.d.ts.map +1 -1
- package/dist/node/writer-keys.js +66 -57
- package/dist/node/writer-keys.js.map +1 -1
- package/dist/node/writers.d.ts +12 -19
- package/dist/node/writers.d.ts.map +1 -1
- package/dist/node/writers.js +7 -15
- package/dist/node/writers.js.map +1 -1
- package/dist/privacy/direct.d.ts.map +1 -1
- package/dist/privacy/direct.js +31 -27
- package/dist/privacy/direct.js.map +1 -1
- package/dist/privacy/space-encryption.d.ts.map +1 -1
- package/dist/privacy/space-encryption.js +5 -9
- package/dist/privacy/space-encryption.js.map +1 -1
- package/dist/query/computed.d.ts.map +1 -1
- package/dist/query/computed.js +78 -77
- package/dist/query/computed.js.map +1 -1
- package/dist/query/engine.d.ts +3 -4
- package/dist/query/engine.d.ts.map +1 -1
- package/dist/query/engine.js +93 -86
- package/dist/query/engine.js.map +1 -1
- package/dist/query/filter.d.ts +8 -10
- package/dist/query/filter.d.ts.map +1 -1
- package/dist/query/filter.js +144 -161
- package/dist/query/filter.js.map +1 -1
- package/dist/records/cel-types.d.ts.map +1 -1
- package/dist/records/cel-types.js +13 -27
- package/dist/records/cel-types.js.map +1 -1
- package/dist/records/cel.d.ts.map +1 -1
- package/dist/records/cel.js +177 -242
- package/dist/records/cel.js.map +1 -1
- package/dist/records/rules.d.ts.map +1 -1
- package/dist/records/rules.js +46 -50
- package/dist/records/rules.js.map +1 -1
- package/dist/records/topics.d.ts.map +1 -1
- package/dist/records/topics.js +18 -22
- package/dist/records/topics.js.map +1 -1
- package/dist/schemas/automations.d.ts +8 -15
- package/dist/schemas/automations.d.ts.map +1 -1
- package/dist/schemas/automations.js +203 -165
- package/dist/schemas/automations.js.map +1 -1
- package/dist/schemas/blueprints.d.ts.map +1 -1
- package/dist/schemas/blueprints.js +14 -13
- package/dist/schemas/blueprints.js.map +1 -1
- package/dist/schemas/connectors.d.ts.map +1 -1
- package/dist/schemas/connectors.js +16 -11
- package/dist/schemas/connectors.js.map +1 -1
- package/dist/schemas/contacts.d.ts +57 -0
- package/dist/schemas/contacts.d.ts.map +1 -1
- package/dist/schemas/contacts.js +32 -0
- package/dist/schemas/contacts.js.map +1 -1
- package/dist/schemas/cron.d.ts.map +1 -1
- package/dist/schemas/cron.js +27 -28
- package/dist/schemas/cron.js.map +1 -1
- package/dist/schemas/defined.d.ts +6 -0
- package/dist/schemas/defined.d.ts.map +1 -0
- package/dist/schemas/defined.js +7 -0
- package/dist/schemas/defined.js.map +1 -0
- package/dist/schemas/guides.d.ts +3 -1
- package/dist/schemas/guides.d.ts.map +1 -1
- package/dist/schemas/guides.js +13 -12
- package/dist/schemas/guides.js.map +1 -1
- package/dist/schemas/index.d.ts +2 -2
- package/dist/schemas/index.d.ts.map +1 -1
- package/dist/schemas/index.js +18 -14
- package/dist/schemas/index.js.map +1 -1
- package/dist/schemas/library/social.d.ts +258 -0
- package/dist/schemas/library/social.d.ts.map +1 -1
- package/dist/schemas/library/social.js +28 -1
- package/dist/schemas/library/social.js.map +1 -1
- package/dist/session/account-hint.d.ts.map +1 -1
- package/dist/session/account-hint.js +8 -5
- package/dist/session/account-hint.js.map +1 -1
- package/dist/session/auth.d.ts +36 -2
- package/dist/session/auth.d.ts.map +1 -1
- package/dist/session/auth.js +95 -2
- package/dist/session/auth.js.map +1 -1
- package/dist/session/connect.d.ts +1 -0
- package/dist/session/connect.d.ts.map +1 -1
- package/dist/session/connect.js +3 -1
- package/dist/session/connect.js.map +1 -1
- package/dist/session/credentials.d.ts +8 -26
- package/dist/session/credentials.d.ts.map +1 -1
- package/dist/session/credentials.js +14 -32
- package/dist/session/credentials.js.map +1 -1
- package/dist/session/hosting.d.ts.map +1 -1
- package/dist/session/hosting.js +8 -2
- package/dist/session/hosting.js.map +1 -1
- package/dist/session/index.d.ts +1 -0
- package/dist/session/index.d.ts.map +1 -1
- package/dist/session/index.js +1 -0
- package/dist/session/index.js.map +1 -1
- package/dist/session/pairing.d.ts.map +1 -1
- package/dist/session/pairing.js +92 -88
- package/dist/session/pairing.js.map +1 -1
- package/dist/session/password-strength.d.ts.map +1 -1
- package/dist/session/password-strength.js +37 -37
- package/dist/session/password-strength.js.map +1 -1
- package/dist/session/places.d.ts.map +1 -1
- package/dist/session/places.js +18 -13
- package/dist/session/places.js.map +1 -1
- package/dist/session/popup.d.ts +13 -0
- package/dist/session/popup.d.ts.map +1 -0
- package/dist/session/popup.js +23 -0
- package/dist/session/popup.js.map +1 -0
- package/dist/space/constitution.d.ts.map +1 -1
- package/dist/space/constitution.js +22 -29
- package/dist/space/constitution.js.map +1 -1
- package/dist/space/grants.d.ts.map +1 -1
- package/dist/space/grants.js +37 -39
- package/dist/space/grants.js.map +1 -1
- package/dist/space/invite-link.d.ts.map +1 -1
- package/dist/space/invite-link.js +33 -33
- package/dist/space/invite-link.js.map +1 -1
- package/dist/space/own-automations.d.ts +13 -24
- package/dist/space/own-automations.d.ts.map +1 -1
- package/dist/space/own-automations.js +47 -37
- package/dist/space/own-automations.js.map +1 -1
- package/dist/space/presets.d.ts +2 -2
- package/dist/space/presets.d.ts.map +1 -1
- package/dist/space/presets.js +3 -2
- package/dist/space/presets.js.map +1 -1
- package/dist/space/roles.d.ts +56 -80
- package/dist/space/roles.d.ts.map +1 -1
- package/dist/space/roles.js +448 -441
- package/dist/space/roles.js.map +1 -1
- package/dist/space/space-access.d.ts +3 -4
- package/dist/space/space-access.d.ts.map +1 -1
- package/dist/space/space-access.js +3 -8
- package/dist/space/space-access.js.map +1 -1
- package/dist/storage/blob/directory.d.ts.map +1 -1
- package/dist/storage/blob/directory.js +39 -32
- package/dist/storage/blob/directory.js.map +1 -1
- package/dist/storage/blob/s3.d.ts.map +1 -1
- package/dist/storage/blob/s3.js +21 -19
- package/dist/storage/blob/s3.js.map +1 -1
- package/dist/storage/blob-store.d.ts +0 -9
- package/dist/storage/blob-store.d.ts.map +1 -1
- package/dist/storage/directory-access.d.ts +8 -32
- package/dist/storage/directory-access.d.ts.map +1 -1
- package/dist/storage/directory-access.js +11 -36
- package/dist/storage/directory-access.js.map +1 -1
- package/dist/storage/encrypted-adapter.d.ts +12 -32
- package/dist/storage/encrypted-adapter.d.ts.map +1 -1
- package/dist/storage/encrypted-adapter.js +31 -54
- package/dist/storage/encrypted-adapter.js.map +1 -1
- package/dist/storage/folder-adapter.d.ts +12 -26
- package/dist/storage/folder-adapter.d.ts.map +1 -1
- package/dist/storage/folder-adapter.js +37 -54
- package/dist/storage/folder-adapter.js.map +1 -1
- package/dist/storage/folder-reconcile.d.ts +12 -26
- package/dist/storage/folder-reconcile.d.ts.map +1 -1
- package/dist/storage/folder-reconcile.js +39 -48
- package/dist/storage/folder-reconcile.js.map +1 -1
- package/dist/storage/indexeddb-adapter.d.ts +2 -6
- package/dist/storage/indexeddb-adapter.d.ts.map +1 -1
- package/dist/storage/indexeddb-adapter.js +60 -114
- package/dist/storage/indexeddb-adapter.js.map +1 -1
- package/dist/storage/mirror.d.ts +0 -29
- package/dist/storage/mirror.d.ts.map +1 -1
- package/dist/storage/mirror.js +153 -110
- package/dist/storage/mirror.js.map +1 -1
- package/dist/storage/segment.d.ts.map +1 -1
- package/dist/storage/segment.js +3 -7
- package/dist/storage/segment.js.map +1 -1
- package/dist/storage/storage-provider.d.ts +6 -11
- package/dist/storage/storage-provider.d.ts.map +1 -1
- package/dist/storage/storage-provider.js +112 -116
- package/dist/storage/storage-provider.js.map +1 -1
- package/dist/sync/sync-engine.d.ts +10 -44
- package/dist/sync/sync-engine.d.ts.map +1 -1
- package/dist/sync/sync-engine.js +245 -247
- package/dist/sync/sync-engine.js.map +1 -1
- package/dist/sync/sync-messages.d.ts +1 -1
- package/dist/sync/sync-messages.d.ts.map +1 -1
- package/dist/sync/sync-messages.js +1 -1
- package/dist/sync/sync-messages.js.map +1 -1
- package/dist/utils/guards.d.ts +1 -0
- package/dist/utils/guards.d.ts.map +1 -1
- package/dist/utils/guards.js +1 -0
- package/dist/utils/guards.js.map +1 -1
- package/dist/utils/hmac.d.ts +5 -0
- package/dist/utils/hmac.d.ts.map +1 -0
- package/dist/utils/hmac.js +20 -0
- package/dist/utils/hmac.js.map +1 -0
- package/dist/utils/map.d.ts +10 -0
- package/dist/utils/map.d.ts.map +1 -0
- package/dist/utils/map.js +30 -0
- package/dist/utils/map.js.map +1 -0
- package/dist/utils/serial.d.ts +2 -0
- package/dist/utils/serial.d.ts.map +1 -1
- package/dist/utils/serial.js +9 -0
- package/dist/utils/serial.js.map +1 -1
- package/dist/validation/capability-gate.d.ts +7 -15
- package/dist/validation/capability-gate.d.ts.map +1 -1
- package/dist/validation/capability-gate.js +32 -36
- package/dist/validation/capability-gate.js.map +1 -1
- package/dist/validation/crypto-gate.d.ts.map +1 -1
- package/dist/validation/crypto-gate.js +12 -21
- package/dist/validation/crypto-gate.js.map +1 -1
- package/docs/README.md +1 -0
- package/docs/actions.md +32 -25
- package/docs/agents.md +28 -16
- package/docs/automations.md +1 -1
- package/docs/blueprints-and-guides.md +37 -5
- package/docs/building-an-app.md +7 -4
- package/docs/collections.md +3 -3
- package/docs/contacts.md +34 -1
- package/docs/contracts.md +1 -1
- package/docs/direct-messages.md +6 -2
- package/docs/inbox.md +5 -11
- package/docs/node.md +54 -22
- package/docs/records-and-queries.md +1 -1
- package/docs/sign-in.md +54 -26
- package/docs/spaces.md +9 -8
- package/docs/standard-library.md +37 -14
- package/docs/storage.md +5 -6
- package/docs/usernames.md +161 -0
- package/docs/warrants.md +1 -1
- package/package.json +1 -2
package/dist/space/roles.js
CHANGED
|
@@ -1,38 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Who may do what in a space: its roles and their grants, the accounts
|
|
3
|
-
* assigned to them, its invites, and the history of changes to them
|
|
4
|
-
*
|
|
3
|
+
* assigned to them, its invites, and the history of changes to them, replayed
|
|
4
|
+
* the same way on every peer by `strong-remove/1`.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* these arguments here, as of this point in the space's history?*
|
|
6
|
+
* It answers one question: *may this account invoke this command with these
|
|
7
|
+
* arguments here, as of this point in the space's history?*
|
|
9
8
|
*
|
|
10
|
-
* **Grants.** A role holds grants (`space/grants.ts`)
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* definitions written apart hold only the grants they share.
|
|
9
|
+
* **Grants.** A role holds grants (`space/grants.ts`). The constitution's
|
|
10
|
+
* grants are a role's first definition; a later one replaces them. A
|
|
11
|
+
* definition holds only what its author could do, and rival definitions hold
|
|
12
|
+
* only what they share.
|
|
15
13
|
*
|
|
16
|
-
* **Accounts.** An account holds power only through
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* change its roles or make it step down. Under `top: "none"` there are no
|
|
20
|
-
* founders, and anyone may be removed by whoever holds the grant.
|
|
14
|
+
* **Accounts.** An account holds power only through its roles. Under
|
|
15
|
+
* `top: { founders }` the founders hold every command, and only each founder
|
|
16
|
+
* itself can remove one, change its roles or make it step down.
|
|
21
17
|
*
|
|
22
|
-
* **The access history.** Every change
|
|
23
|
-
*
|
|
24
|
-
* `seen` names the latest changes its author knew of. That makes a small
|
|
25
|
-
* graph, and no order over it decides anything (spec 03 §4):
|
|
18
|
+
* **The access history.** Every change names in `seen` the latest changes its
|
|
19
|
+
* author knew of. No order over that graph decides anything (spec 03 §4):
|
|
26
20
|
*
|
|
27
|
-
* 1. A change is allowed when its author had the power in its own view
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* invite strikes what it reaches. Changes that strike each other all
|
|
32
|
-
* stand.
|
|
21
|
+
* 1. A change is allowed when its author had the power in its own view.
|
|
22
|
+
* 2. A removal or unassignment strikes what its account did that it hadn't
|
|
23
|
+
* seen. A revoke, a fork proof or a closed invite strikes what it reaches.
|
|
24
|
+
* Changes that strike each other all stand.
|
|
33
25
|
* 3. The state is a merge of the valid changes: on each record the latest.
|
|
34
|
-
* Rival definitions of a role hold what they share, an unassignment beats
|
|
35
|
-
* an assignment, and a closed invite an open one.
|
|
36
26
|
*/
|
|
37
27
|
import { commandCovers } from '../identity/ucan.js';
|
|
38
28
|
import { canonicalize } from '../schema/version.js';
|
|
@@ -44,15 +34,15 @@ export const ASSIGN_COLLECTION = 'sys.assign';
|
|
|
44
34
|
export const REMOVE_COLLECTION = 'sys.remove';
|
|
45
35
|
export const INVITE_COLLECTION = 'sys.invite';
|
|
46
36
|
export const REVOKE_COLLECTION = 'sys.revoke';
|
|
47
|
-
/** Collection definitions
|
|
37
|
+
/** Collection definitions: what a version must keep to */
|
|
48
38
|
export const DEFINITION_COLLECTION = 'sys.collection';
|
|
49
|
-
/** Which of a private space's keys is current; the key itself travels sealed
|
|
39
|
+
/** Which of a private space's keys is current; the key itself travels sealed (`sys.box`) */
|
|
50
40
|
export const KEY_COLLECTION = 'sys.key';
|
|
51
|
-
/** The relays the space's members meet at
|
|
41
|
+
/** The relays the space's members meet at */
|
|
52
42
|
export const RELAYS_COLLECTION = 'sys.relays';
|
|
53
|
-
/** Nodes that hold the whole space, and how many a write should reach
|
|
43
|
+
/** Nodes that hold the whole space, and how many a write should reach */
|
|
54
44
|
export const KEEPERS_COLLECTION = 'sys.keepers';
|
|
55
|
-
/** A fork proof: two versions one key signed at one place in its line (02 §4.10)
|
|
45
|
+
/** A fork proof: two versions one key signed at one place in its line (02 §4.10) */
|
|
56
46
|
export const FORK_COLLECTION = 'sys.fork';
|
|
57
47
|
/** A founder stepping down: it keeps its roles, and is a founder no more */
|
|
58
48
|
export const STEPDOWN_COLLECTION = 'sys.stepdown';
|
|
@@ -64,9 +54,9 @@ export const STRONG_REMOVE = 'strong-remove/1';
|
|
|
64
54
|
export const RESOLVERS = new Set([STRONG_REMOVE]);
|
|
65
55
|
const accessKind = (collection, prefix, clear = true) => Object.freeze({ collection, prefix, clear });
|
|
66
56
|
/**
|
|
67
|
-
* Each kind of change: its collection,
|
|
68
|
-
*
|
|
69
|
-
*
|
|
57
|
+
* Each kind of change: its collection, its record keys' prefix, and whether a
|
|
58
|
+
* private space keeps it in the clear so a peer without the key can judge it.
|
|
59
|
+
* Collection definitions stay sealed: only readers need their rules.
|
|
70
60
|
*/
|
|
71
61
|
export const ACCESS_KINDS = Object.freeze({
|
|
72
62
|
role: accessKind(ROLE_COLLECTION, 'role:'),
|
|
@@ -91,25 +81,21 @@ export function checkKeepers(keepers, copies = null) {
|
|
|
91
81
|
if (!Array.isArray(keepers) || keepers.length > MAX_KEEPERS)
|
|
92
82
|
return `A space names at most ${MAX_KEEPERS} keepers`;
|
|
93
83
|
const list = keepers;
|
|
94
|
-
const dids = new Set();
|
|
95
84
|
for (const keeper of list) {
|
|
96
|
-
if (!isObject(keeper) ||
|
|
97
|
-
typeof keeper.did !== 'string' ||
|
|
98
|
-
!keeper.did.startsWith('did:key:') ||
|
|
99
|
-
keeper.did.length > 200)
|
|
85
|
+
if (!isObject(keeper) || !isKeeperDid(keeper.did))
|
|
100
86
|
return 'A keeper is named by its did:key';
|
|
101
87
|
if (typeof keeper.name !== 'string' || keeper.name.length > 80)
|
|
102
88
|
return 'A keeper has a name of at most 80 characters';
|
|
103
|
-
dids.add(keeper.did);
|
|
104
89
|
}
|
|
90
|
+
const dids = new Set(list.map((keeper) => isObject(keeper) && keeper.did));
|
|
105
91
|
if (dids.size !== list.length)
|
|
106
92
|
return 'A keeper is named twice';
|
|
107
|
-
if (copies !== null &&
|
|
108
|
-
!(typeof copies === 'number' && Number.isSafeInteger(copies) && copies >= 1 && copies <= MAX_KEEPERS)) {
|
|
93
|
+
if (copies !== null && !isCopies(copies))
|
|
109
94
|
return `Copies is a whole number from 1 to ${MAX_KEEPERS}`;
|
|
110
|
-
}
|
|
111
95
|
return null;
|
|
112
96
|
}
|
|
97
|
+
const isKeeperDid = (did) => typeof did === 'string' && did.startsWith('did:key:') && did.length <= 200;
|
|
98
|
+
const isCopies = (copies) => typeof copies === 'number' && Number.isSafeInteger(copies) && copies >= 1 && copies <= MAX_KEEPERS;
|
|
113
99
|
/** How many relays a space may name */
|
|
114
100
|
export const MAX_RELAYS = 8;
|
|
115
101
|
/** Why a list of relays can't be a space's, or null */
|
|
@@ -117,35 +103,42 @@ export function checkRelays(relays) {
|
|
|
117
103
|
if (!Array.isArray(relays) || relays.length > MAX_RELAYS)
|
|
118
104
|
return `A space names at most ${MAX_RELAYS} relays`;
|
|
119
105
|
for (const url of relays) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
try {
|
|
124
|
-
parsed = new URL(url);
|
|
125
|
-
}
|
|
126
|
-
catch {
|
|
127
|
-
return `"${url}" is not a URL`;
|
|
128
|
-
}
|
|
129
|
-
// A kind of relay other than Minca's own goes before the scheme (`nostr+wss://`). Plain ws://
|
|
130
|
-
// only on this machine: anywhere else it would show the room to everyone on the way.
|
|
131
|
-
const local = ['localhost', '127.0.0.1', '[::1]'].includes(parsed.hostname);
|
|
132
|
-
const transport = /^(?:[a-z][a-z0-9-]*\+)?(wss?):$/.exec(parsed.protocol)?.[1];
|
|
133
|
-
if (transport !== 'wss' && !(transport === 'ws' && local))
|
|
134
|
-
return `"${url}" is not a wss:// relay`;
|
|
106
|
+
const problem = relayProblem(url);
|
|
107
|
+
if (problem)
|
|
108
|
+
return problem;
|
|
135
109
|
}
|
|
136
110
|
return new Set(relays).size === relays.length ? null : 'A relay is named twice';
|
|
137
111
|
}
|
|
138
|
-
|
|
139
|
-
|
|
112
|
+
function relayProblem(url) {
|
|
113
|
+
if (typeof url !== 'string' || url.length > 200)
|
|
114
|
+
return 'A relay is a URL of at most 200 characters';
|
|
115
|
+
let parsed;
|
|
116
|
+
try {
|
|
117
|
+
parsed = new URL(url);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return `"${url}" is not a URL`;
|
|
121
|
+
}
|
|
122
|
+
// Another kind of relay goes before the scheme (`nostr+wss://`). Plain ws:// only on this
|
|
123
|
+
// machine: anywhere else it would show the room to everyone on the way.
|
|
124
|
+
const local = ['localhost', '127.0.0.1', '[::1]'].includes(parsed.hostname);
|
|
125
|
+
const transport = /^(?:[a-z][a-z0-9-]*\+)?(wss?):$/.exec(parsed.protocol)?.[1];
|
|
126
|
+
return transport === 'wss' || (transport === 'ws' && local) ? null : `"${url}" is not a wss:// relay`;
|
|
127
|
+
}
|
|
128
|
+
function roleDefinition(event, by, role, view) {
|
|
140
129
|
return Object.freeze({
|
|
141
|
-
event
|
|
142
|
-
by
|
|
130
|
+
event,
|
|
131
|
+
by,
|
|
143
132
|
...(role.title !== undefined ? { title: role.title } : {}),
|
|
144
133
|
grants: role.grants,
|
|
145
134
|
nests: role.nests ?? [],
|
|
146
|
-
view
|
|
135
|
+
view,
|
|
147
136
|
});
|
|
148
137
|
}
|
|
138
|
+
/** A role's definition from a change, judged in `view` */
|
|
139
|
+
function definitionFrom(event, view) {
|
|
140
|
+
return event.kind === 'role' ? roleDefinition(event.id, event.root, event, view) : null;
|
|
141
|
+
}
|
|
149
142
|
function startState(genesis) {
|
|
150
143
|
// The constitution's assignments are as if made by the space itself: their id is the space's.
|
|
151
144
|
const assigned = new Map();
|
|
@@ -153,7 +146,7 @@ function startState(genesis) {
|
|
|
153
146
|
assigned.set(did, new Map(roles.map((role) => [role, genesis.id])));
|
|
154
147
|
return {
|
|
155
148
|
founders: new Set(genesis.founders),
|
|
156
|
-
roles: new Map(genesis.roles.map((role) => [role.name, [
|
|
149
|
+
roles: new Map(genesis.roles.map((role) => [role.name, [roleDefinition(genesis.id, null, role, null)]])),
|
|
157
150
|
assigned,
|
|
158
151
|
invites: new Map(),
|
|
159
152
|
definitions: new Map(),
|
|
@@ -174,7 +167,7 @@ export function rolesOf(state, did) {
|
|
|
174
167
|
export function isMember(state, did) {
|
|
175
168
|
return state.founders.has(did) || (state.assigned.get(did)?.size ?? 0) > 0;
|
|
176
169
|
}
|
|
177
|
-
/** Every member
|
|
170
|
+
/** Every member: who can read, as far as the history is concerned */
|
|
178
171
|
export function readers(state) {
|
|
179
172
|
const out = new Set(state.founders);
|
|
180
173
|
for (const [did, roles] of state.assigned)
|
|
@@ -194,14 +187,11 @@ export function headless(state) {
|
|
|
194
187
|
return ![...readers(state)].some((did) => roles.some((role) => may(state, did, ROLE_DEFINE, { role }) ||
|
|
195
188
|
may(state, did, ROLE_ASSIGN, { role, self: false, roles: [] })));
|
|
196
189
|
}
|
|
197
|
-
/** Answers already given in a state, which never changes once made */
|
|
198
|
-
const answers = new WeakMap();
|
|
199
190
|
/**
|
|
200
191
|
* Whether an account may invoke a command with these arguments in a state:
|
|
201
192
|
* a founder may invoke anything; anyone else, what a role assigned to it
|
|
202
|
-
* holds
|
|
203
|
-
*
|
|
204
|
-
* and what the roles it nests hold.
|
|
193
|
+
* holds, each definition only as far as its author could invoke it (UCAN's
|
|
194
|
+
* attenuation), with what the roles it nests hold.
|
|
205
195
|
*/
|
|
206
196
|
export function may(state, did, cmd, args, options = {}) {
|
|
207
197
|
if (state.founders.has(did))
|
|
@@ -209,17 +199,22 @@ export function may(state, did, cmd, args, options = {}) {
|
|
|
209
199
|
const roles = rolesOf(state, did);
|
|
210
200
|
if (roles.length === 0)
|
|
211
201
|
return false;
|
|
212
|
-
const key = `${did}\n${cmd}\n${canonicalize(args)}\n${options.opaque ? 1 : 0}`;
|
|
213
|
-
let cache = answers.get(state);
|
|
214
|
-
if (!cache)
|
|
215
|
-
answers.set(state, (cache = new Map()));
|
|
216
|
-
const known = cache.get(key);
|
|
217
|
-
if (known !== undefined)
|
|
218
|
-
return known;
|
|
219
202
|
const opaque = !!options.opaque;
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
|
|
203
|
+
const question = `${did}\n${cmd}\n${canonicalize(args)}\n${opaque ? 1 : 0}`;
|
|
204
|
+
return remembered(state, question, () => roles.some((role) => roleHolds(state, role, (grant) => grantFits(grant, cmd, args, opaque), (view, by) => may(view, by, cmd, args, { opaque }))));
|
|
205
|
+
}
|
|
206
|
+
/** Answers already given in a state, which never changes once made */
|
|
207
|
+
const answers = new WeakMap();
|
|
208
|
+
function remembered(state, question, answer) {
|
|
209
|
+
let known = answers.get(state);
|
|
210
|
+
if (!known)
|
|
211
|
+
answers.set(state, (known = new Map()));
|
|
212
|
+
const given = known.get(question);
|
|
213
|
+
if (given !== undefined)
|
|
214
|
+
return given;
|
|
215
|
+
const result = answer();
|
|
216
|
+
known.set(question, result);
|
|
217
|
+
return result;
|
|
223
218
|
}
|
|
224
219
|
/** A role as its rival definitions leave it: what they all hold, and the authors whose power bounds it */
|
|
225
220
|
function effectiveRole(state, role) {
|
|
@@ -243,18 +238,17 @@ function roleHolds(state, role, fits, authorHolds, visiting = new Set()) {
|
|
|
243
238
|
const inner = new Set(visiting).add(role);
|
|
244
239
|
const held = found.grants.some(fits) ||
|
|
245
240
|
found.nests.some((nested) => nested !== role && roleHolds(state, nested, fits, authorHolds, inner));
|
|
246
|
-
// Each definition holds only what its author could do itself, as of it.
|
|
247
241
|
return held && found.authors.every(({ view, by }) => authorHolds(view, by));
|
|
248
242
|
}
|
|
249
243
|
/**
|
|
250
|
-
* Whether an account holds every grant a role gives, each as wide or wider
|
|
251
|
-
*
|
|
252
|
-
*
|
|
244
|
+
* Whether an account holds every grant a role gives, each as wide or wider:
|
|
245
|
+
* what it may hand a bot of its own. Only the policies' words are compared, so
|
|
246
|
+
* it may say no where they would allow, never yes where they wouldn't.
|
|
253
247
|
*/
|
|
254
248
|
export function holdsRole(state, did, role) {
|
|
255
249
|
return grantsOfRole(state, role, new Set()).every((grant) => holdsGrant(state, did, grant));
|
|
256
250
|
}
|
|
257
|
-
/** What a role gives its members
|
|
251
|
+
/** What a role gives its members: its current definitions' shared grants, and its nested roles' */
|
|
258
252
|
function grantsOfRole(state, role, visiting) {
|
|
259
253
|
const found = effectiveRole(state, role);
|
|
260
254
|
if (!found || visiting.has(role))
|
|
@@ -262,7 +256,7 @@ function grantsOfRole(state, role, visiting) {
|
|
|
262
256
|
const inner = new Set(visiting).add(role);
|
|
263
257
|
return [...found.grants, ...found.nests.flatMap((nested) => grantsOfRole(state, nested, inner))];
|
|
264
258
|
}
|
|
265
|
-
/** Whether an account holds a grant as wide as `wanted` or wider
|
|
259
|
+
/** Whether an account holds a grant as wide as `wanted` or wider */
|
|
266
260
|
function holdsGrant(state, did, wanted) {
|
|
267
261
|
if (state.founders.has(did))
|
|
268
262
|
return true;
|
|
@@ -277,7 +271,7 @@ const targetArgs = (state, invoker, target, role) => ({
|
|
|
277
271
|
self: invoker === target,
|
|
278
272
|
roles: rolesOf(state, target),
|
|
279
273
|
});
|
|
280
|
-
/** Whether any member holds `/space/succeed`, once per state
|
|
274
|
+
/** Whether any member holds `/space/succeed`, once per state */
|
|
281
275
|
const maySucceed = new WeakMap();
|
|
282
276
|
function anyMaySucceed(state) {
|
|
283
277
|
let found = maySucceed.get(state);
|
|
@@ -298,49 +292,12 @@ export function refusal(event, state) {
|
|
|
298
292
|
return may(state, root, ROLE_DEFINE, { role: event.name })
|
|
299
293
|
? null
|
|
300
294
|
: 'Its author may not define this role';
|
|
301
|
-
case 'assign':
|
|
302
|
-
|
|
303
|
-
if (event.did.startsWith('role:'))
|
|
304
|
-
return 'A role is assigned to accounts, not to roles';
|
|
305
|
-
const holds = rolesOf(state, event.did).includes(event.role);
|
|
306
|
-
// Joining with an invite: your own record, for the invite's role, judged as the assignment
|
|
307
|
-
// its opener makes, with the joiner's own `self` and `roles`.
|
|
308
|
-
if (event.viaInvite !== undefined) {
|
|
309
|
-
const invite = state.invites.get(event.viaInvite);
|
|
310
|
-
if (!event.assigned)
|
|
311
|
-
return 'An invite only assigns';
|
|
312
|
-
if (root !== event.did)
|
|
313
|
-
return 'Only the person joining can use an invite for themselves';
|
|
314
|
-
if (!invite?.open)
|
|
315
|
-
return 'The invite is not open';
|
|
316
|
-
if (invite.role !== event.role)
|
|
317
|
-
return 'The invite is for another role';
|
|
318
|
-
if (holds)
|
|
319
|
-
return 'Already holds that role';
|
|
320
|
-
if (!state.roles.has(event.role))
|
|
321
|
-
return `There is no role "${event.role}"`;
|
|
322
|
-
return invite.by.every((opener) => may(state, opener, ROLE_ASSIGN, targetArgs(state, opener, event.did, event.role)))
|
|
323
|
-
? null
|
|
324
|
-
: 'Whoever opened the invite may not assign this role to them';
|
|
325
|
-
}
|
|
326
|
-
// Leaving a role is built in.
|
|
327
|
-
if (!event.assigned && root === event.did)
|
|
328
|
-
return holds ? null : 'It does not hold that role';
|
|
329
|
-
if (state.founders.has(event.did) && root !== event.did)
|
|
330
|
-
return "No one but a founder itself changes a founder's roles";
|
|
331
|
-
if (!state.roles.has(event.role))
|
|
332
|
-
return `There is no role "${event.role}"`;
|
|
333
|
-
const args = targetArgs(state, root, event.did, event.role);
|
|
334
|
-
if (event.assigned)
|
|
335
|
-
return may(state, root, ROLE_ASSIGN, args) ? null : 'Its author may not assign them this role';
|
|
336
|
-
if (!holds)
|
|
337
|
-
return 'They do not hold that role';
|
|
338
|
-
return may(state, root, ROLE_UNASSIGN, args) ? null : 'Its author may not take this role from them';
|
|
339
|
-
}
|
|
295
|
+
case 'assign':
|
|
296
|
+
return assignRefusal(event, state);
|
|
340
297
|
case 'remove': {
|
|
341
298
|
if (!isMember(state, event.did))
|
|
342
299
|
return 'Not a member';
|
|
343
|
-
// Leaving
|
|
300
|
+
// Leaving is built in, for a founder too.
|
|
344
301
|
if (root === event.did)
|
|
345
302
|
return null;
|
|
346
303
|
if (state.founders.has(event.did))
|
|
@@ -350,7 +307,7 @@ export function refusal(event, state) {
|
|
|
350
307
|
: 'Its author may not remove them';
|
|
351
308
|
}
|
|
352
309
|
case 'invite': {
|
|
353
|
-
// An invite
|
|
310
|
+
// An invite assigns to whoever holds its secret: someone else, holding nothing yet.
|
|
354
311
|
const assigns = (role) => state.roles.has(role) && may(state, root, ROLE_ASSIGN, { role, self: false, roles: [] });
|
|
355
312
|
const existing = state.invites.get(event.inviteKey);
|
|
356
313
|
if (existing && !assigns(existing.role))
|
|
@@ -361,49 +318,32 @@ export function refusal(event, state) {
|
|
|
361
318
|
return `There is no role "${event.role}"`;
|
|
362
319
|
return assigns(event.role) ? null : 'Its author may not invite to this role';
|
|
363
320
|
}
|
|
364
|
-
// Stepping down is built in, and only a founder's own.
|
|
365
321
|
case 'stepdown':
|
|
366
322
|
if (root !== event.did)
|
|
367
323
|
return 'Only a founder itself steps down';
|
|
368
324
|
return state.founders.has(event.did) ? null : 'Not a founder';
|
|
369
|
-
// A successor binds no one, so where no one holds the command, any member may name one.
|
|
370
325
|
case 'succeed': {
|
|
326
|
+
// A successor binds no one, so where no one holds the command, any member may name one.
|
|
371
327
|
if (!isMember(state, root))
|
|
372
328
|
return 'Only a member may name a successor';
|
|
373
329
|
if (may(state, root, SPACE_SUCCEED, {}))
|
|
374
330
|
return null;
|
|
375
331
|
return anyMaySucceed(state) ? 'Its author may not name a successor' : null;
|
|
376
332
|
}
|
|
377
|
-
//
|
|
333
|
+
// A revoke only ever takes from its own account.
|
|
378
334
|
case 'revoke':
|
|
379
335
|
return isMember(state, root) ? null : 'Only a member may revoke a key here';
|
|
380
|
-
// The proof
|
|
336
|
+
// The proof was checked before it got here; a member only has to bring it.
|
|
381
337
|
case 'fork':
|
|
382
338
|
return isMember(state, root) ? null : 'Only a member may report a fork';
|
|
383
|
-
case 'definition':
|
|
384
|
-
|
|
385
|
-
const contested = state.contested.has(event.name);
|
|
386
|
-
const defines = may(state, root, COLLECTION_DEFINE, { collection: event.name });
|
|
387
|
-
if (!existing && !contested) {
|
|
388
|
-
if (event.deleted)
|
|
389
|
-
return 'There is no such definition to remove';
|
|
390
|
-
return defines ? null : 'Its author may not define this collection';
|
|
391
|
-
}
|
|
392
|
-
if (defines)
|
|
393
|
-
return null;
|
|
394
|
-
// Rivals no one settled: no one first defined it, so only someone who may define it settles them.
|
|
395
|
-
if (contested)
|
|
396
|
-
return 'Rival definitions of it stand: only someone who may define it may settle them';
|
|
397
|
-
return isMember(state, root) && existing?.definedBy === root
|
|
398
|
-
? null
|
|
399
|
-
: 'Only whoever defined it, or someone who may define it, may change it';
|
|
400
|
-
}
|
|
339
|
+
case 'definition':
|
|
340
|
+
return definitionRefusal(event, state);
|
|
401
341
|
case 'key': {
|
|
402
342
|
if (state.keys.length === 0)
|
|
403
343
|
return 'A public space has no key to change';
|
|
404
344
|
if (!may(state, root, KEY_CHANGE, {}))
|
|
405
345
|
return "Its author may not change the space's key";
|
|
406
|
-
// Going back to a key a removed member still holds would undo the
|
|
346
|
+
// Going back to a key a removed member still holds would undo the change.
|
|
407
347
|
return state.keys.some((known) => known.keyId === event.keyId || known.readKey === event.readKey)
|
|
408
348
|
? 'That key was used before'
|
|
409
349
|
: null;
|
|
@@ -418,10 +358,68 @@ export function refusal(event, state) {
|
|
|
418
358
|
: 'Its author may not change who keeps the space';
|
|
419
359
|
}
|
|
420
360
|
}
|
|
361
|
+
function assignRefusal(event, state) {
|
|
362
|
+
const root = event.root;
|
|
363
|
+
// Roles go to accounts: putting one role inside another is part of a definition.
|
|
364
|
+
if (event.did.startsWith('role:'))
|
|
365
|
+
return 'A role is assigned to accounts, not to roles';
|
|
366
|
+
const holds = rolesOf(state, event.did).includes(event.role);
|
|
367
|
+
if (event.viaInvite !== undefined)
|
|
368
|
+
return inviteJoinRefusal(event, event.viaInvite, state, holds);
|
|
369
|
+
// Leaving a role is built in.
|
|
370
|
+
if (!event.assigned && root === event.did)
|
|
371
|
+
return holds ? null : 'It does not hold that role';
|
|
372
|
+
if (state.founders.has(event.did) && root !== event.did)
|
|
373
|
+
return "No one but a founder itself changes a founder's roles";
|
|
374
|
+
if (!state.roles.has(event.role))
|
|
375
|
+
return `There is no role "${event.role}"`;
|
|
376
|
+
const args = targetArgs(state, root, event.did, event.role);
|
|
377
|
+
if (event.assigned)
|
|
378
|
+
return may(state, root, ROLE_ASSIGN, args) ? null : 'Its author may not assign them this role';
|
|
379
|
+
if (!holds)
|
|
380
|
+
return 'They do not hold that role';
|
|
381
|
+
return may(state, root, ROLE_UNASSIGN, args) ? null : 'Its author may not take this role from them';
|
|
382
|
+
}
|
|
383
|
+
/** Joining with an invite: your own record, judged as the assignment its openers make */
|
|
384
|
+
function inviteJoinRefusal(event, inviteKey, state, holds) {
|
|
385
|
+
const invite = state.invites.get(inviteKey);
|
|
386
|
+
if (!event.assigned)
|
|
387
|
+
return 'An invite only assigns';
|
|
388
|
+
if (event.root !== event.did)
|
|
389
|
+
return 'Only the person joining can use an invite for themselves';
|
|
390
|
+
if (!invite?.open)
|
|
391
|
+
return 'The invite is not open';
|
|
392
|
+
if (invite.role !== event.role)
|
|
393
|
+
return 'The invite is for another role';
|
|
394
|
+
if (holds)
|
|
395
|
+
return 'Already holds that role';
|
|
396
|
+
if (!state.roles.has(event.role))
|
|
397
|
+
return `There is no role "${event.role}"`;
|
|
398
|
+
const openersMay = invite.by.every((opener) => may(state, opener, ROLE_ASSIGN, targetArgs(state, opener, event.did, event.role)));
|
|
399
|
+
return openersMay ? null : 'Whoever opened the invite may not assign this role to them';
|
|
400
|
+
}
|
|
401
|
+
function definitionRefusal(event, state) {
|
|
402
|
+
const root = event.root;
|
|
403
|
+
const existing = state.definitions.get(event.name);
|
|
404
|
+
const contested = state.contested.has(event.name);
|
|
405
|
+
const defines = may(state, root, COLLECTION_DEFINE, { collection: event.name });
|
|
406
|
+
if (!existing && !contested) {
|
|
407
|
+
if (event.deleted)
|
|
408
|
+
return 'There is no such definition to remove';
|
|
409
|
+
return defines ? null : 'Its author may not define this collection';
|
|
410
|
+
}
|
|
411
|
+
if (defines)
|
|
412
|
+
return null;
|
|
413
|
+
if (contested)
|
|
414
|
+
return 'Rival definitions of it stand: only someone who may define it may settle them';
|
|
415
|
+
return isMember(state, root) && existing?.definedBy === root
|
|
416
|
+
? null
|
|
417
|
+
: 'Only whoever defined it, or someone who may define it, may change it';
|
|
418
|
+
}
|
|
421
419
|
const NO_CUTOFF = new Map();
|
|
422
420
|
/** Whether a reduction reaches its key's changes whatever either saw */
|
|
423
421
|
const reachesBack = (reduction) => reduction.reach.kind === 'key' || reduction.reach.kind === 'line';
|
|
424
|
-
/** What a change takes, in its own view: null when it takes nothing
|
|
422
|
+
/** What a change takes, in its own view: null when it takes nothing */
|
|
425
423
|
function reductionOf(event, view) {
|
|
426
424
|
const of = (reach, cutoff = event.cutoff) => ({ id: event.id, reach, cutoff });
|
|
427
425
|
const account = (holds, did) => (holds ? of({ kind: 'account', root: did }) : null);
|
|
@@ -445,16 +443,12 @@ function reductionOf(event, view) {
|
|
|
445
443
|
}
|
|
446
444
|
/** Whether a reduction reaches a change or a record that its cutoff doesn't cover */
|
|
447
445
|
function reaches(reduction, subject) {
|
|
448
|
-
|
|
449
|
-
if (writer !== undefined &&
|
|
450
|
-
collection !== undefined &&
|
|
451
|
-
line !== undefined &&
|
|
452
|
-
(reduction.cutoff.get(writer)?.get(collection) ?? -1) >= line)
|
|
446
|
+
if (coveredBy(reduction.cutoff, subject))
|
|
453
447
|
return false;
|
|
448
|
+
const { writer, collection, line } = subject;
|
|
454
449
|
const reach = reduction.reach;
|
|
455
450
|
switch (reach.kind) {
|
|
456
|
-
//
|
|
457
|
-
// judged in a view its author chooses, so it can't decide what a strike leaves.
|
|
451
|
+
// Everything its account did: what an action needed is judged in a view its author chooses.
|
|
458
452
|
case 'account':
|
|
459
453
|
return subject.root === reach.root;
|
|
460
454
|
case 'key':
|
|
@@ -465,6 +459,11 @@ function reaches(reduction, subject) {
|
|
|
465
459
|
return subject.viaInvite === reach.invite;
|
|
466
460
|
}
|
|
467
461
|
}
|
|
462
|
+
function coveredBy(cutoff, { writer, collection, line }) {
|
|
463
|
+
if (writer === undefined || collection === undefined || line === undefined)
|
|
464
|
+
return false;
|
|
465
|
+
return (cutoff.get(writer)?.get(collection) ?? -1) >= line;
|
|
466
|
+
}
|
|
468
467
|
/** Adds a valid reduction to what judging looks up: by the account or key it reaches, and each line's lowest fork */
|
|
469
468
|
function indexReduction(taken, forks, reduction) {
|
|
470
469
|
const reach = reduction.reach;
|
|
@@ -478,19 +477,6 @@ function indexReduction(taken, forks, reduction) {
|
|
|
478
477
|
taken.set(who, [...(taken.get(who) ?? []), reduction]);
|
|
479
478
|
}
|
|
480
479
|
}
|
|
481
|
-
/** A role's definition from a change, judged in `view` */
|
|
482
|
-
function definitionFrom(event, view) {
|
|
483
|
-
if (event.kind !== 'role')
|
|
484
|
-
return null;
|
|
485
|
-
return Object.freeze({
|
|
486
|
-
event: event.id,
|
|
487
|
-
by: event.root,
|
|
488
|
-
...(event.title !== undefined ? { title: event.title } : {}),
|
|
489
|
-
grants: event.grants,
|
|
490
|
-
nests: event.nests,
|
|
491
|
-
view,
|
|
492
|
-
});
|
|
493
|
-
}
|
|
494
480
|
function setAssigned(state, did, role, event) {
|
|
495
481
|
const roles = new Map(state.assigned.get(did) ?? []);
|
|
496
482
|
if (event === null)
|
|
@@ -512,7 +498,7 @@ function change(event, state, view) {
|
|
|
512
498
|
setAssigned(state, event.did, event.role, event.assigned ? event.id : null);
|
|
513
499
|
return;
|
|
514
500
|
case 'remove':
|
|
515
|
-
// It ends every assignment it saw
|
|
501
|
+
// It ends every assignment it saw; a founder who leaves steps down.
|
|
516
502
|
state.assigned.delete(event.did);
|
|
517
503
|
if (event.root === event.did)
|
|
518
504
|
state.founders.delete(event.did);
|
|
@@ -521,7 +507,7 @@ function change(event, state, view) {
|
|
|
521
507
|
state.founders.delete(event.did);
|
|
522
508
|
return;
|
|
523
509
|
case 'succeed':
|
|
524
|
-
state.successors.set(event.space,
|
|
510
|
+
state.successors.set(event.space, sortedUnique([...(state.successors.get(event.space) ?? []), event.root]));
|
|
525
511
|
return;
|
|
526
512
|
case 'invite':
|
|
527
513
|
state.invites.set(event.inviteKey, {
|
|
@@ -549,7 +535,7 @@ function change(event, state, view) {
|
|
|
549
535
|
return;
|
|
550
536
|
}
|
|
551
537
|
case 'key':
|
|
552
|
-
state.keys.push(
|
|
538
|
+
state.keys.push(keyEpoch(event));
|
|
553
539
|
return;
|
|
554
540
|
case 'relays':
|
|
555
541
|
state.relays = [...event.relays];
|
|
@@ -560,17 +546,6 @@ function change(event, state, view) {
|
|
|
560
546
|
return;
|
|
561
547
|
}
|
|
562
548
|
}
|
|
563
|
-
// ─── The convergent replay (spec/03-spaces.md §4) ──────────
|
|
564
|
-
//
|
|
565
|
-
// No order decides anything. A change is allowed when its author had the
|
|
566
|
-
// power in the state its own view gives (the changes it saw that were
|
|
567
|
-
// allowed, merged). A removal or an unassignment, judged in its own view, is
|
|
568
|
-
// a reduction; it strikes every change of its account that it had not seen,
|
|
569
|
-
// past its cutoff. Reductions that strike each other all stand. The state is a
|
|
570
|
-
// merge of the valid changes: on each record, the latest wins, and of latest
|
|
571
|
-
// changes that didn't see each other, the one that gives least.
|
|
572
|
-
/** A key's line for one collection, as a map key */
|
|
573
|
-
const lineOf = (writer, collection) => `${writer}\n${collection}`;
|
|
574
549
|
/** What a change names in `seen`, once each, leaving out the space itself */
|
|
575
550
|
const parentsOf = (event, genesisId) => [
|
|
576
551
|
...new Set(event.seen.filter((parent) => parent !== genesisId)),
|
|
@@ -603,50 +578,21 @@ function seenOf(parents, saw, order, index) {
|
|
|
603
578
|
},
|
|
604
579
|
};
|
|
605
580
|
}
|
|
606
|
-
const byId = (a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
|
|
607
581
|
/** The state a set of changes gives, merged: no order, only what saw what (spec 03 §4.4) */
|
|
608
582
|
function merge(genesis, events, saw, defViews) {
|
|
609
583
|
const state = startState(genesis);
|
|
610
584
|
const byKey = new Map();
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
if (event.kind
|
|
614
|
-
|
|
615
|
-
(byKey.get(event.key) ?? byKey.set(event.key, []).get(event.key)).push(event);
|
|
616
|
-
}
|
|
617
|
-
// The latest on a key, those no other on it saw, in one pass: below another's prefix, or among its exceptions.
|
|
618
|
-
const latest = (group) => {
|
|
619
|
-
if (group.length <= 1)
|
|
620
|
-
return group;
|
|
621
|
-
let top = -1;
|
|
622
|
-
let topBy = '';
|
|
623
|
-
let second = -1;
|
|
624
|
-
const exceptions = new Set();
|
|
625
|
-
for (const event of group) {
|
|
626
|
-
const seen = saw.get(event.id);
|
|
627
|
-
if (seen.prefix > top) {
|
|
628
|
-
second = top;
|
|
629
|
-
top = seen.prefix;
|
|
630
|
-
topBy = event.id;
|
|
631
|
-
}
|
|
632
|
-
else if (seen.prefix > second)
|
|
633
|
-
second = seen.prefix;
|
|
634
|
-
for (const n of seen.extra)
|
|
635
|
-
exceptions.add(n);
|
|
636
|
-
}
|
|
637
|
-
return group.filter((event) => {
|
|
638
|
-
const place = saw.get(event.id).placeOf(event.id);
|
|
639
|
-
const below = event.id === topBy ? second : top;
|
|
640
|
-
return !(place < below || exceptions.has(place));
|
|
641
|
-
});
|
|
642
|
-
};
|
|
585
|
+
// A revoke or a fork proof changes no state.
|
|
586
|
+
for (const event of events)
|
|
587
|
+
if (event.kind !== 'revoke' && event.kind !== 'fork')
|
|
588
|
+
pushTo(byKey, event.key, event);
|
|
643
589
|
// Removals first: an assignment counts only if it saw every one of its account's.
|
|
644
590
|
const removals = new Map();
|
|
645
591
|
for (const group of byKey.values()) {
|
|
646
592
|
if (group[0]?.kind !== 'remove')
|
|
647
593
|
continue;
|
|
648
594
|
const did = group[0].did;
|
|
649
|
-
removals.set(did,
|
|
595
|
+
removals.set(did, latestOf(group, saw));
|
|
650
596
|
// A founder who leaves is one no more; no one else can remove one.
|
|
651
597
|
if (group.some((event) => event.root === did))
|
|
652
598
|
state.founders.delete(did);
|
|
@@ -656,7 +602,7 @@ function merge(genesis, events, saw, defViews) {
|
|
|
656
602
|
let currentKey = null;
|
|
657
603
|
const keyEvents = [];
|
|
658
604
|
for (const group of byKey.values()) {
|
|
659
|
-
const tops = [...
|
|
605
|
+
const tops = [...latestOf(group, saw)].sort(idOrder);
|
|
660
606
|
const win = tops[0];
|
|
661
607
|
if (!win)
|
|
662
608
|
continue;
|
|
@@ -666,19 +612,16 @@ function merge(genesis, events, saw, defViews) {
|
|
|
666
612
|
case 'stepdown':
|
|
667
613
|
state.founders.delete(win.did);
|
|
668
614
|
continue;
|
|
669
|
-
case 'succeed':
|
|
615
|
+
case 'succeed':
|
|
670
616
|
// Every one stands, whoever named it: none is first.
|
|
671
|
-
|
|
672
|
-
state.successors.set(space, [...new Set(group.map((event) => event.root))].sort());
|
|
617
|
+
state.successors.set(win.space, sortedUnique(group.map((event) => event.root)));
|
|
673
618
|
continue;
|
|
674
|
-
}
|
|
675
619
|
case 'role':
|
|
676
620
|
// Rivals all stand: the role holds what they share.
|
|
677
621
|
state.roles.set(win.name, tops.map((event) => definitionFrom(event, defViews.get(event.id))));
|
|
678
622
|
continue;
|
|
679
623
|
case 'assign': {
|
|
680
624
|
// Taking a role away beats giving it; an assignment counts only if it saw its account's removals.
|
|
681
|
-
// The changes on its key all saw the constitution, so they decide over its assignment too.
|
|
682
625
|
const gone = removals.get(win.did) ?? [];
|
|
683
626
|
const counted = tops.some((event) => event.kind === 'assign' && !event.assigned)
|
|
684
627
|
? undefined
|
|
@@ -686,25 +629,12 @@ function merge(genesis, events, saw, defViews) {
|
|
|
686
629
|
setAssigned(state, win.did, win.role, counted ? counted.id : null);
|
|
687
630
|
continue;
|
|
688
631
|
}
|
|
689
|
-
case 'invite':
|
|
690
|
-
|
|
691
|
-
// all stand: a join must be one each of them could have made.
|
|
692
|
-
const roles = new Set(tops.map((event) => (event.kind === 'invite' ? event.role : '')));
|
|
693
|
-
const closed = tops.find((event) => event.kind === 'invite' && !event.open);
|
|
694
|
-
const pick = closed ?? win;
|
|
695
|
-
if (pick.kind === 'invite')
|
|
696
|
-
state.invites.set(pick.inviteKey, {
|
|
697
|
-
role: pick.role,
|
|
698
|
-
open: !closed && roles.size === 1,
|
|
699
|
-
event: pick.id,
|
|
700
|
-
by: [...new Set(tops.map((event) => event.root))].sort(),
|
|
701
|
-
});
|
|
632
|
+
case 'invite':
|
|
633
|
+
mergeInvites(tops, win, state);
|
|
702
634
|
continue;
|
|
703
|
-
}
|
|
704
635
|
case 'key':
|
|
636
|
+
// Every key stands; an id picks which is current among keys any author could have made current alone.
|
|
705
637
|
keyEvents.push(...group);
|
|
706
|
-
// Only those granted it change the key, and every key stands: an id picks which is current
|
|
707
|
-
// among keys any of them could have made current alone.
|
|
708
638
|
currentKey = win;
|
|
709
639
|
continue;
|
|
710
640
|
case 'definition':
|
|
@@ -715,23 +645,65 @@ function merge(genesis, events, saw, defViews) {
|
|
|
715
645
|
change(win, state, state);
|
|
716
646
|
}
|
|
717
647
|
}
|
|
718
|
-
if (
|
|
719
|
-
//
|
|
720
|
-
const sorted = [...keyEvents].sort(byId);
|
|
648
|
+
if (currentKey) {
|
|
649
|
+
// The current key goes last, the rest by id: only which is current matters.
|
|
721
650
|
const current = currentKey;
|
|
722
|
-
|
|
651
|
+
const rest = [...keyEvents].sort(idOrder).filter((event) => event !== current);
|
|
652
|
+
for (const event of [...rest, current])
|
|
723
653
|
if (event.kind === 'key')
|
|
724
|
-
state.keys.push(
|
|
654
|
+
state.keys.push(keyEpoch(event));
|
|
725
655
|
}
|
|
726
656
|
return state;
|
|
727
657
|
}
|
|
658
|
+
/** The latest changes on one record: those no other on it saw */
|
|
659
|
+
function latestOf(group, saw) {
|
|
660
|
+
if (group.length <= 1)
|
|
661
|
+
return group;
|
|
662
|
+
// In one pass: an event is seen when it is below another's prefix, or among its exceptions.
|
|
663
|
+
let top = -1;
|
|
664
|
+
let topBy = '';
|
|
665
|
+
let second = -1;
|
|
666
|
+
const exceptions = new Set();
|
|
667
|
+
for (const event of group) {
|
|
668
|
+
const seen = saw.get(event.id);
|
|
669
|
+
if (seen.prefix > top) {
|
|
670
|
+
second = top;
|
|
671
|
+
top = seen.prefix;
|
|
672
|
+
topBy = event.id;
|
|
673
|
+
}
|
|
674
|
+
else if (seen.prefix > second)
|
|
675
|
+
second = seen.prefix;
|
|
676
|
+
for (const n of seen.extra)
|
|
677
|
+
exceptions.add(n);
|
|
678
|
+
}
|
|
679
|
+
return group.filter((event) => {
|
|
680
|
+
const place = saw.get(event.id).placeOf(event.id);
|
|
681
|
+
const below = event.id === topBy ? second : top;
|
|
682
|
+
return !(place < below || exceptions.has(place));
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Closed beats open, and open ones naming different roles close it. Rival
|
|
687
|
+
* openers all stand: a join must be one each of them could have made.
|
|
688
|
+
*/
|
|
689
|
+
function mergeInvites(tops, win, state) {
|
|
690
|
+
const roles = new Set(tops.map((event) => (event.kind === 'invite' ? event.role : '')));
|
|
691
|
+
const closed = tops.find((event) => event.kind === 'invite' && !event.open);
|
|
692
|
+
const pick = closed ?? win;
|
|
693
|
+
if (pick.kind !== 'invite')
|
|
694
|
+
return;
|
|
695
|
+
state.invites.set(pick.inviteKey, {
|
|
696
|
+
role: pick.role,
|
|
697
|
+
open: !closed && roles.size === 1,
|
|
698
|
+
event: pick.id,
|
|
699
|
+
by: sortedUnique(tops.map((event) => event.root)),
|
|
700
|
+
});
|
|
701
|
+
}
|
|
728
702
|
/**
|
|
729
703
|
* A state with one change applied on top: the change saw all of it, so it is
|
|
730
|
-
* the latest on its record. Who defined a collection is worked out among
|
|
731
|
-
* changes `counted`.
|
|
704
|
+
* the latest on its record. Who defined a collection is worked out among `counted`.
|
|
732
705
|
*/
|
|
733
706
|
function applied(state, event, r, counted) {
|
|
734
|
-
const genesisId = r.genesis.id;
|
|
735
707
|
const next = {
|
|
736
708
|
...state,
|
|
737
709
|
founders: new Set(state.founders),
|
|
@@ -744,15 +716,8 @@ function applied(state, event, r, counted) {
|
|
|
744
716
|
successors: new Map(state.successors),
|
|
745
717
|
};
|
|
746
718
|
change(event, next, state);
|
|
747
|
-
if (event.kind === 'key')
|
|
748
|
-
|
|
749
|
-
const earlier = next.keys.slice(0, -1);
|
|
750
|
-
const own = earlier.filter((key) => key.event === genesisId);
|
|
751
|
-
const rest = earlier
|
|
752
|
-
.filter((key) => key.event !== genesisId)
|
|
753
|
-
.sort((a, b) => (a.event < b.event ? -1 : a.event > b.event ? 1 : 0));
|
|
754
|
-
next.keys = [...own, ...rest, next.keys.at(-1)];
|
|
755
|
-
}
|
|
719
|
+
if (event.kind === 'key')
|
|
720
|
+
next.keys = inMergeOrder(next.keys, r.genesis.id);
|
|
756
721
|
if (event.kind === 'definition' && !event.deleted) {
|
|
757
722
|
const group = [...r.saw.get(event.id)]
|
|
758
723
|
.filter((other) => counted.has(other) && r.byId.get(other).key === event.key)
|
|
@@ -762,52 +727,54 @@ function applied(state, event, r, counted) {
|
|
|
762
727
|
}
|
|
763
728
|
return next;
|
|
764
729
|
}
|
|
730
|
+
/** Keys in the order a merge gives: the space's own first, the rest by id, the newest last */
|
|
731
|
+
function inMergeOrder(keys, genesisId) {
|
|
732
|
+
const earlier = keys.slice(0, -1);
|
|
733
|
+
const own = earlier.filter((key) => key.event === genesisId);
|
|
734
|
+
const rest = earlier.filter((key) => key.event !== genesisId).sort((a, b) => compare(a.event, b.event));
|
|
735
|
+
return [...own, ...rest, keys.at(-1)];
|
|
736
|
+
}
|
|
765
737
|
/**
|
|
766
738
|
* The latest changes to one collection's definition, merged (02 §6.3). One
|
|
767
|
-
* account's alone, or changes by accounts that may each change it
|
|
768
|
-
*
|
|
769
|
-
*
|
|
770
|
-
*
|
|
771
|
-
* delete beside another account's change, is contested: no id decides, since
|
|
772
|
-
* an id can be ground, and readers judge the rivals by what they say.
|
|
739
|
+
* account's alone, or changes by accounts that may each change it, are picked
|
|
740
|
+
* between by id: each could have made its own the latest alone. Anything else
|
|
741
|
+
* (rival first definitions by two accounts, or a delete beside another
|
|
742
|
+
* account's change) is contested: an id can be ground, so no id decides.
|
|
773
743
|
*/
|
|
774
744
|
function mergeDefinitions(tops, group, saw, state) {
|
|
775
|
-
const sorted = [...tops].sort(
|
|
745
|
+
const sorted = [...tops].sort(idOrder);
|
|
776
746
|
const win = sorted[0];
|
|
777
747
|
if (win.kind !== 'definition')
|
|
778
748
|
return;
|
|
779
|
-
// Who first defined it, as each latest definition sees the whole group: null where rivals stand.
|
|
780
749
|
const definitions = sorted.filter((top) => !isDelete(top));
|
|
750
|
+
// Who first defined it, as each latest definition sees it: null where rivals stand.
|
|
781
751
|
const definers = new Set(definitions.map((top) => definerOf(top, group, saw)));
|
|
782
|
-
//
|
|
783
|
-
// settling change did; rival firsts that are themselves latest are not agreement.
|
|
752
|
+
// Rival firsts are settled only once every latest change saw them all.
|
|
784
753
|
const settled = definitions.every((top) => firstsOf(top, group, saw).every((first) => first === top || saw.get(top.id)?.has(first.id)));
|
|
785
754
|
const oneAccount = new Set(sorted.map((top) => top.root)).size === 1;
|
|
786
755
|
const agreed = sorted.length === 1 ||
|
|
787
756
|
oneAccount ||
|
|
788
757
|
(definers.size === 1 && !sorted.some(isDelete) && (!definers.has(null) || settled));
|
|
789
|
-
if (agreed) {
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
else
|
|
793
|
-
state.definitions.set(win.name, { event: win.id, definedBy: definerOf(win, group, saw) });
|
|
794
|
-
return;
|
|
758
|
+
if (!agreed) {
|
|
759
|
+
state.definitions.delete(win.name);
|
|
760
|
+
state.contested.set(win.name, sorted.map((top) => top.id));
|
|
795
761
|
}
|
|
796
|
-
|
|
797
|
-
|
|
762
|
+
else if (win.deleted)
|
|
763
|
+
change(win, state, state);
|
|
764
|
+
else
|
|
765
|
+
state.definitions.set(win.name, { event: win.id, definedBy: definerOf(win, group, saw) });
|
|
798
766
|
}
|
|
799
767
|
const isDelete = (event) => event.kind === 'definition' && event.deleted;
|
|
800
768
|
/**
|
|
801
|
-
* Whoever defined a collection this time (02 §6.3): of
|
|
802
|
-
*
|
|
803
|
-
*
|
|
804
|
-
* accounts give no one: no id decides it, since an id can be ground, so only
|
|
805
|
-
* someone who may define it may change it.
|
|
769
|
+
* Whoever defined a collection this time (02 §6.3): the account of its first
|
|
770
|
+
* definitions, if they are all one account's. Rival firsts by two accounts
|
|
771
|
+
* give no one, so only someone who may define it may change it.
|
|
806
772
|
*/
|
|
807
773
|
function definerOf(win, group, saw) {
|
|
808
|
-
const
|
|
809
|
-
|
|
810
|
-
|
|
774
|
+
const roots = new Set(firstsOf(win, group, saw).map((event) => event.root));
|
|
775
|
+
if (roots.size === 0)
|
|
776
|
+
return win.root;
|
|
777
|
+
return roots.size === 1 ? [...roots][0] : null;
|
|
811
778
|
}
|
|
812
779
|
/** The first definitions of the life `win` is in: those of `group` that saw every removal it saw, and no other */
|
|
813
780
|
function firstsOf(win, group, saw) {
|
|
@@ -817,7 +784,10 @@ function firstsOf(win, group, saw) {
|
|
|
817
784
|
(event === win || removals.every((removal) => saw.get(event.id)?.has(removal.id))));
|
|
818
785
|
return life.filter((event) => !life.some((other) => other !== event && saw.get(event.id)?.has(other.id)));
|
|
819
786
|
}
|
|
820
|
-
/**
|
|
787
|
+
/**
|
|
788
|
+
* A graph's strongly connected components, each after those with an edge into
|
|
789
|
+
* it. Tarjan's, without recursion, so a long history can't overflow the stack.
|
|
790
|
+
*/
|
|
821
791
|
function componentsOf(nodes, edges) {
|
|
822
792
|
let counter = 0;
|
|
823
793
|
const indices = new Map();
|
|
@@ -825,7 +795,6 @@ function componentsOf(nodes, edges) {
|
|
|
825
795
|
const stack = [];
|
|
826
796
|
const onStack = new Set();
|
|
827
797
|
const found = [];
|
|
828
|
-
const of = new Map();
|
|
829
798
|
const work = [];
|
|
830
799
|
const visit = (id) => {
|
|
831
800
|
indices.set(id, counter);
|
|
@@ -867,26 +836,87 @@ function componentsOf(nodes, edges) {
|
|
|
867
836
|
}
|
|
868
837
|
// Tarjan's finds components sinks first.
|
|
869
838
|
const list = found.reverse();
|
|
839
|
+
const of = new Map();
|
|
870
840
|
list.forEach((members, i) => members.forEach((id) => of.set(id, i)));
|
|
871
841
|
return { of, list };
|
|
872
842
|
}
|
|
873
843
|
const STRUCK = "Its author's power was taken away by a change it had not seen, or rested on one that was";
|
|
874
844
|
/** Replays a space's access history. */
|
|
875
845
|
export function replayAccess(genesis, events) {
|
|
876
|
-
// Another resolver would judge the same changes differently
|
|
846
|
+
// Another resolver would judge the same changes differently, so this peer doesn't judge the space.
|
|
877
847
|
if (!RESOLVERS.has(genesis.resolver))
|
|
878
848
|
throw new Error(`This peer doesn't know the resolver ${genesis.resolver}, so it can't judge this space`);
|
|
879
|
-
const
|
|
849
|
+
const byId = new Map();
|
|
880
850
|
for (const event of events)
|
|
881
|
-
if (!
|
|
882
|
-
|
|
883
|
-
|
|
851
|
+
if (!byId.has(event.id))
|
|
852
|
+
byId.set(event.id, event);
|
|
853
|
+
const waiting = waitingOf(genesis.id, byId);
|
|
854
|
+
const placeable = [...byId.values()].filter((event) => !waiting.has(event.id));
|
|
855
|
+
const order = ancestorsFirst(placeable, genesis.id);
|
|
856
|
+
const placed = new Set(order);
|
|
857
|
+
for (const event of placeable)
|
|
858
|
+
if (!placed.has(event.id))
|
|
859
|
+
waiting.add(event.id);
|
|
860
|
+
const index = new Map(order.map((id, i) => [id, i]));
|
|
861
|
+
const saw = new Map();
|
|
862
|
+
for (const id of order)
|
|
863
|
+
saw.set(id, seenOf(parentsOf(byId.get(id), genesis.id), saw, order, index));
|
|
864
|
+
const judged = judgeInOrder(order, { genesis, saw, byId });
|
|
865
|
+
const { allowedIds, taking, strikes, defViews } = judged;
|
|
866
|
+
const within = validWithin(order, { genesis, byId, saw, allowedIds, strikes, defViews });
|
|
867
|
+
const valid = within.valid;
|
|
868
|
+
const struck = new Map();
|
|
869
|
+
for (const id of order)
|
|
870
|
+
if (allowedIds.has(id) && !valid.has(id))
|
|
871
|
+
struck.set(id, STRUCK);
|
|
872
|
+
const taken = new Map();
|
|
873
|
+
const forks = new Map();
|
|
874
|
+
for (const id of order) {
|
|
875
|
+
const reduction = valid.has(id) && taking.get(id);
|
|
876
|
+
if (reduction)
|
|
877
|
+
indexReduction(taken, forks, reduction);
|
|
878
|
+
}
|
|
879
|
+
const named = new Set([...genesis.founders, ...Object.keys(genesis.assigned ?? {})]);
|
|
880
|
+
const invites = new Set();
|
|
881
|
+
for (const event of byId.values()) {
|
|
882
|
+
if (event.kind === 'assign' && event.assigned)
|
|
883
|
+
named.add(event.did);
|
|
884
|
+
if (event.kind === 'invite')
|
|
885
|
+
invites.add(event.inviteKey);
|
|
886
|
+
}
|
|
887
|
+
const state = merge(genesis, order.filter((id) => valid.has(id)).map((id) => byId.get(id)), saw, within.defViews);
|
|
888
|
+
state.keyDue = keyDue(state, { genesis, order, byId, saw, valid, ...judged });
|
|
889
|
+
return historyOf({
|
|
890
|
+
genesis,
|
|
891
|
+
byId,
|
|
892
|
+
waiting,
|
|
893
|
+
order,
|
|
894
|
+
saw,
|
|
895
|
+
index,
|
|
896
|
+
refused: judged.refused,
|
|
897
|
+
valid,
|
|
898
|
+
struck,
|
|
899
|
+
taking,
|
|
900
|
+
taken,
|
|
901
|
+
forks,
|
|
902
|
+
state,
|
|
903
|
+
named,
|
|
904
|
+
invites,
|
|
905
|
+
views: new Map(),
|
|
906
|
+
allowedIds,
|
|
907
|
+
defViews,
|
|
908
|
+
validDefViews: within.defViews,
|
|
909
|
+
strikes,
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
/** Changes that wait until everything they saw is here, and anything that saw them */
|
|
913
|
+
function waitingOf(genesisId, byId) {
|
|
884
914
|
const waiting = new Set();
|
|
885
|
-
const known = (id) => id ===
|
|
915
|
+
const known = (id) => id === genesisId || byId.has(id);
|
|
886
916
|
let changed = true;
|
|
887
917
|
while (changed) {
|
|
888
918
|
changed = false;
|
|
889
|
-
for (const event of
|
|
919
|
+
for (const event of byId.values()) {
|
|
890
920
|
if (waiting.has(event.id))
|
|
891
921
|
continue;
|
|
892
922
|
if (event.seen.some((parent) => parent === event.id || !known(parent) || waiting.has(parent))) {
|
|
@@ -895,15 +925,17 @@ export function replayAccess(genesis, events) {
|
|
|
895
925
|
}
|
|
896
926
|
}
|
|
897
927
|
}
|
|
898
|
-
|
|
899
|
-
|
|
928
|
+
return waiting;
|
|
929
|
+
}
|
|
930
|
+
/** Kahn's order, ties by id. A cycle can only be forged: its changes are left out rather than hang it. */
|
|
931
|
+
function ancestorsFirst(placeable, genesisId) {
|
|
900
932
|
const children = new Map();
|
|
901
933
|
const pending = new Map();
|
|
902
934
|
for (const event of placeable) {
|
|
903
|
-
const parents = parentsOf(event,
|
|
935
|
+
const parents = parentsOf(event, genesisId);
|
|
904
936
|
pending.set(event.id, parents.length);
|
|
905
937
|
for (const parent of parents)
|
|
906
|
-
(children
|
|
938
|
+
pushTo(children, parent, event.id);
|
|
907
939
|
}
|
|
908
940
|
const ready = placeable.filter((event) => pending.get(event.id) === 0).map((event) => event.id);
|
|
909
941
|
const order = [];
|
|
@@ -918,30 +950,25 @@ export function replayAccess(genesis, events) {
|
|
|
918
950
|
ready.push(child);
|
|
919
951
|
}
|
|
920
952
|
}
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
}
|
|
930
|
-
// In order, ancestors first: each change's own view (the allowed changes it saw), whether it is
|
|
931
|
-
// allowed there, what it takes, and the strikes it is part of.
|
|
953
|
+
return order;
|
|
954
|
+
}
|
|
955
|
+
/**
|
|
956
|
+
* Each change in order, ancestors first: its own view (the allowed changes it
|
|
957
|
+
* saw), whether it is allowed there, what it takes, and the strikes it is part of.
|
|
958
|
+
*/
|
|
959
|
+
function judgeInOrder(order, r) {
|
|
960
|
+
const { saw, byId } = r;
|
|
932
961
|
const tookReading = new Set();
|
|
933
962
|
const refused = new Map();
|
|
934
963
|
const allowedIds = new Set();
|
|
935
964
|
const strikes = new Map();
|
|
936
965
|
const afterOf = new Map();
|
|
937
966
|
const defViews = new Map();
|
|
938
|
-
/** The allowed reductions, in order */
|
|
939
967
|
const taking = new Map();
|
|
940
|
-
const
|
|
941
|
-
const allowedView = viewsAlong({ genesis, saw, byId: byIdMap }, (id) => allowedIds.has(id), afterOf, defViews);
|
|
968
|
+
const allowedView = viewsAlong(r, (id) => allowedIds.has(id), afterOf, defViews);
|
|
942
969
|
for (const id of order) {
|
|
943
|
-
const event =
|
|
944
|
-
const result = step(event, allowedView(event), {
|
|
970
|
+
const event = byId.get(id);
|
|
971
|
+
const result = step(event, allowedView(event), { ...r, allowedIds, taking });
|
|
945
972
|
if (typeof result === 'string') {
|
|
946
973
|
refused.set(id, result);
|
|
947
974
|
continue;
|
|
@@ -950,10 +977,9 @@ export function replayAccess(genesis, events) {
|
|
|
950
977
|
afterOf.set(id, result.after);
|
|
951
978
|
if (result.tookReading)
|
|
952
979
|
tookReading.add(id);
|
|
953
|
-
// Strikes it is part of, with the allowed changes before it.
|
|
954
980
|
for (const by of result.reachedBy)
|
|
955
981
|
if (strikesOn(by, event, saw))
|
|
956
|
-
|
|
982
|
+
pushTo(strikes, by.id, id);
|
|
957
983
|
const reduction = result.reduction;
|
|
958
984
|
if (!reduction)
|
|
959
985
|
continue;
|
|
@@ -961,15 +987,15 @@ export function replayAccess(genesis, events) {
|
|
|
961
987
|
for (const other of order) {
|
|
962
988
|
if (other === id)
|
|
963
989
|
break;
|
|
964
|
-
if (allowedIds.has(other) && strikesOn(reduction,
|
|
965
|
-
|
|
990
|
+
if (allowedIds.has(other) && strikesOn(reduction, byId.get(other), saw))
|
|
991
|
+
pushTo(strikes, id, other);
|
|
966
992
|
}
|
|
967
993
|
}
|
|
968
|
-
// A stepping down its key's revoke or fork proof reaches only ends founder status: it
|
|
969
|
-
// nothing, and withdraws nothing
|
|
994
|
+
// A stepping down that its key's revoke or fork proof reaches only ends founder status: it
|
|
995
|
+
// strikes nothing, and withdraws nothing.
|
|
970
996
|
const back = [...taking.values()].filter(reachesBack);
|
|
971
997
|
for (const id of order) {
|
|
972
|
-
const event =
|
|
998
|
+
const event = byId.get(id);
|
|
973
999
|
if (event.kind !== 'stepdown' || !allowedIds.has(id))
|
|
974
1000
|
continue;
|
|
975
1001
|
if (back.some((reduction) => reachedBy(reduction, event, saw))) {
|
|
@@ -977,74 +1003,34 @@ export function replayAccess(genesis, events) {
|
|
|
977
1003
|
taking.delete(id);
|
|
978
1004
|
}
|
|
979
1005
|
}
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
for (const id of order) {
|
|
989
|
-
const reduction = valid.has(id) && taking.get(id);
|
|
990
|
-
if (reduction)
|
|
991
|
-
indexReduction(taken, forks, reduction);
|
|
992
|
-
}
|
|
993
|
-
const named = new Set([...genesis.founders, ...Object.keys(genesis.assigned ?? {})]);
|
|
994
|
-
const invites = new Set();
|
|
995
|
-
for (const event of byIdMap.values()) {
|
|
996
|
-
if (event.kind === 'assign' && event.assigned)
|
|
997
|
-
named.add(event.did);
|
|
998
|
-
if (event.kind === 'invite')
|
|
999
|
-
invites.add(event.inviteKey);
|
|
1000
|
-
}
|
|
1001
|
-
const state = merge(genesis, order.filter((id) => valid.has(id)).map((id) => byIdMap.get(id)), saw, within.defViews);
|
|
1002
|
-
// Due: someone lost reading in a valid change the current key had not seen. They may hold it.
|
|
1006
|
+
return { tookReading, refused, allowedIds, strikes, afterOf, defViews, taking };
|
|
1007
|
+
}
|
|
1008
|
+
/**
|
|
1009
|
+
* Whether a new key is due: someone lost reading in a valid change the current
|
|
1010
|
+
* key had not seen, or someone who held the current key reads no more, however
|
|
1011
|
+
* that came about (two changes together can take reading where neither did alone).
|
|
1012
|
+
*/
|
|
1013
|
+
function keyDue(state, r) {
|
|
1003
1014
|
const keyEvent = state.keys.at(-1)?.event;
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
// made held it, and so did whoever came to read after
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
const
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
if (after && isMember(after, event.did))
|
|
1021
|
-
held.add(event.did);
|
|
1022
|
-
}
|
|
1023
|
-
if ([...held].some((did) => !now.has(did)))
|
|
1024
|
-
state.keyDue = true;
|
|
1015
|
+
if (keyEvent === undefined)
|
|
1016
|
+
return false;
|
|
1017
|
+
const sawByKey = r.saw.get(keyEvent);
|
|
1018
|
+
const unseen = (id) => r.valid.has(id) && id !== keyEvent && !sawByKey?.has(id);
|
|
1019
|
+
if (r.order.some((id) => r.tookReading.has(id) && unseen(id)))
|
|
1020
|
+
return true;
|
|
1021
|
+
// Whoever could read as the key was made held it, and so did whoever came to read after.
|
|
1022
|
+
const atKey = keyEvent === r.genesis.id ? startState(r.genesis) : r.afterOf.get(keyEvent);
|
|
1023
|
+
const held = new Set(atKey ? readers(atKey) : []);
|
|
1024
|
+
for (const id of r.order) {
|
|
1025
|
+
const event = r.byId.get(id);
|
|
1026
|
+
if (!r.valid.has(id) || event.kind !== 'assign' || sawByKey?.has(id))
|
|
1027
|
+
continue;
|
|
1028
|
+
const after = r.afterOf.get(id);
|
|
1029
|
+
if (after && isMember(after, event.did))
|
|
1030
|
+
held.add(event.did);
|
|
1025
1031
|
}
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
byId: byIdMap,
|
|
1029
|
-
waiting,
|
|
1030
|
-
order,
|
|
1031
|
-
saw,
|
|
1032
|
-
index,
|
|
1033
|
-
refused,
|
|
1034
|
-
valid,
|
|
1035
|
-
struck,
|
|
1036
|
-
taking,
|
|
1037
|
-
taken,
|
|
1038
|
-
forks,
|
|
1039
|
-
state,
|
|
1040
|
-
named,
|
|
1041
|
-
invites,
|
|
1042
|
-
views: new Map(),
|
|
1043
|
-
allowedIds,
|
|
1044
|
-
defViews,
|
|
1045
|
-
validDefViews: within.defViews,
|
|
1046
|
-
strikes,
|
|
1047
|
-
});
|
|
1032
|
+
const now = readers(state);
|
|
1033
|
+
return [...held].some((did) => !now.has(did));
|
|
1048
1034
|
}
|
|
1049
1035
|
// Revokes and fork proofs are never struck: every allowed one applies.
|
|
1050
1036
|
const reachedBy = (reduction, event, saw) => reduction.id !== event.id &&
|
|
@@ -1053,8 +1039,7 @@ const reachedBy = (reduction, event, saw) => reduction.id !== event.id &&
|
|
|
1053
1039
|
(reachesBack(reduction) ||
|
|
1054
1040
|
(!saw.get(event.id).has(reduction.id) && !saw.get(reduction.id).has(event.id))) &&
|
|
1055
1041
|
reaches(reduction, { ...event, writer: event.author, collection: ACCESS_KINDS[event.kind].collection });
|
|
1056
|
-
// A stepping down is never struck
|
|
1057
|
-
// revoke or a fork proof reaches ends founder status and does nothing more.
|
|
1042
|
+
// A stepping down is never struck: founder status can't be given again.
|
|
1058
1043
|
const strikesOn = (reduction, event, saw) => !(reachesBack(reduction) && event.kind === 'stepdown') && reachedBy(reduction, event, saw);
|
|
1059
1044
|
/**
|
|
1060
1045
|
* One change judged in its own view, as replaying and extending both do: why it is refused, or the
|
|
@@ -1076,21 +1061,23 @@ function step(event, view, r) {
|
|
|
1076
1061
|
};
|
|
1077
1062
|
}
|
|
1078
1063
|
/**
|
|
1079
|
-
* Each change's view over the changes `counts` keeps among what it saw
|
|
1064
|
+
* Each change's view over the changes `counts` keeps among what it saw: along a chain, the one
|
|
1080
1065
|
* before's view, with it on top when kept; where branches meet, a merge. Asked in order. A role
|
|
1081
1066
|
* definition's view is kept in `defViews`, where its grants are judged.
|
|
1082
1067
|
*/
|
|
1083
1068
|
function viewsAlong({ genesis, saw, byId }, counts, afterOf, defViews) {
|
|
1084
1069
|
const viewOf = new Map();
|
|
1085
|
-
|
|
1070
|
+
const viewFor = (event) => {
|
|
1086
1071
|
const parents = parentsOf(event, genesis.id);
|
|
1087
|
-
let view;
|
|
1088
1072
|
if (parents.length === 0)
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1073
|
+
return startState(genesis);
|
|
1074
|
+
if (parents.length === 1)
|
|
1075
|
+
return counts(parents[0]) ? afterOf.get(parents[0]) : viewOf.get(parents[0]);
|
|
1076
|
+
const counted = [...saw.get(event.id)].filter(counts).map((other) => byId.get(other));
|
|
1077
|
+
return merge(genesis, counted, saw, defViews);
|
|
1078
|
+
};
|
|
1079
|
+
return (event) => {
|
|
1080
|
+
const view = viewFor(event);
|
|
1094
1081
|
viewOf.set(event.id, view);
|
|
1095
1082
|
if (event.kind === 'role')
|
|
1096
1083
|
defViews.set(event.id, view);
|
|
@@ -1100,34 +1087,11 @@ function viewsAlong({ genesis, saw, byId }, counts, afterOf, defViews) {
|
|
|
1100
1087
|
/**
|
|
1101
1088
|
* The valid changes among `cut` (in replay order, closed under what they saw), and the views their
|
|
1102
1089
|
* role definitions are judged in. First the strikes: an allowed change is struck by an unstruck one
|
|
1103
|
-
* from another component of the strikes inside the cut,
|
|
1104
|
-
*
|
|
1090
|
+
* from another component of the strikes inside the cut. Then, ancestors first, a change stays
|
|
1091
|
+
* valid only if the valid changes it saw still give its author the power.
|
|
1105
1092
|
*/
|
|
1106
1093
|
function validWithin(cut, r) {
|
|
1107
|
-
const
|
|
1108
|
-
const out = new Set(cut.filter((id) => !r.allowedIds.has(id)));
|
|
1109
|
-
const edges = new Map();
|
|
1110
|
-
for (const [from, targets] of r.strikes) {
|
|
1111
|
-
if (!inCut.has(from) || !r.allowedIds.has(from))
|
|
1112
|
-
continue;
|
|
1113
|
-
const kept = targets.filter((to) => inCut.has(to) && r.allowedIds.has(to));
|
|
1114
|
-
if (kept.length > 0)
|
|
1115
|
-
edges.set(from, kept);
|
|
1116
|
-
}
|
|
1117
|
-
if (edges.size > 0) {
|
|
1118
|
-
const nodes = [...new Set([...edges.keys(), ...[...edges.values()].flat()])].sort();
|
|
1119
|
-
const { of, list } = componentsOf(nodes, edges);
|
|
1120
|
-
const strikers = new Map();
|
|
1121
|
-
for (const [from, targets] of edges)
|
|
1122
|
-
for (const to of targets)
|
|
1123
|
-
if (of.get(from) !== of.get(to))
|
|
1124
|
-
(strikers.get(to) ?? strikers.set(to, []).get(to)).push(from);
|
|
1125
|
-
// A stepping down is never struck: founder status can't be given back.
|
|
1126
|
-
for (const members of list)
|
|
1127
|
-
for (const id of members)
|
|
1128
|
-
if (r.byId.get(id)?.kind !== 'stepdown' && (strikers.get(id) ?? []).some((from) => !out.has(from)))
|
|
1129
|
-
out.add(id);
|
|
1130
|
-
}
|
|
1094
|
+
const out = struckWithin(cut, r);
|
|
1131
1095
|
const valid = new Set(cut.filter((id) => !out.has(id)));
|
|
1132
1096
|
if (cut.every((id) => !r.allowedIds.has(id) || valid.has(id)))
|
|
1133
1097
|
return { valid, defViews: r.defViews };
|
|
@@ -1147,6 +1111,34 @@ function validWithin(cut, r) {
|
|
|
1147
1111
|
}
|
|
1148
1112
|
return { valid, defViews };
|
|
1149
1113
|
}
|
|
1114
|
+
/** The changes in `cut` that are not allowed, or that an unstruck change from another component strikes */
|
|
1115
|
+
function struckWithin(cut, r) {
|
|
1116
|
+
const inCut = new Set(cut);
|
|
1117
|
+
const out = new Set(cut.filter((id) => !r.allowedIds.has(id)));
|
|
1118
|
+
const edges = new Map();
|
|
1119
|
+
for (const [from, targets] of r.strikes) {
|
|
1120
|
+
if (!inCut.has(from) || !r.allowedIds.has(from))
|
|
1121
|
+
continue;
|
|
1122
|
+
const kept = targets.filter((to) => inCut.has(to) && r.allowedIds.has(to));
|
|
1123
|
+
if (kept.length > 0)
|
|
1124
|
+
edges.set(from, kept);
|
|
1125
|
+
}
|
|
1126
|
+
if (edges.size === 0)
|
|
1127
|
+
return out;
|
|
1128
|
+
const nodes = [...new Set([...edges.keys(), ...[...edges.values()].flat()])].sort();
|
|
1129
|
+
const { of, list } = componentsOf(nodes, edges);
|
|
1130
|
+
const strikers = new Map();
|
|
1131
|
+
for (const [from, targets] of edges)
|
|
1132
|
+
for (const to of targets)
|
|
1133
|
+
if (of.get(from) !== of.get(to))
|
|
1134
|
+
pushTo(strikers, to, from);
|
|
1135
|
+
// Visited strikers first. A stepping down is never struck: founder status can't be given back.
|
|
1136
|
+
for (const members of list)
|
|
1137
|
+
for (const id of members)
|
|
1138
|
+
if (r.byId.get(id)?.kind !== 'stepdown' && (strikers.get(id) ?? []).some((from) => !out.has(from)))
|
|
1139
|
+
out.add(id);
|
|
1140
|
+
return out;
|
|
1141
|
+
}
|
|
1150
1142
|
/**
|
|
1151
1143
|
* The replay with one more change that saw every change placed, or null for any other. Such a change
|
|
1152
1144
|
* strikes nothing and only a revoke or fork can strike it; its view is every allowed change, and it
|
|
@@ -1157,8 +1149,8 @@ function extended(r, event) {
|
|
|
1157
1149
|
// A revoke or a fork proof reaches back, whatever it saw: only replaying whole can say what it strikes.
|
|
1158
1150
|
if (event.kind === 'revoke' || event.kind === 'fork')
|
|
1159
1151
|
return null;
|
|
1160
|
-
//
|
|
1161
|
-
//
|
|
1152
|
+
// With something allowed but not valid, the allowed changes are not the state; and whether a key
|
|
1153
|
+
// change is due rests on everyone who held the key. Both are worked out whole.
|
|
1162
1154
|
if (r.struck.size > 0 || r.state.keyDue)
|
|
1163
1155
|
return null;
|
|
1164
1156
|
const parents = parentsOf(event, genesis.id);
|
|
@@ -1170,7 +1162,7 @@ function extended(r, event) {
|
|
|
1170
1162
|
if (mine.prefix !== r.order.length || mine.extra.size > 0 || r.waiting.size > 0)
|
|
1171
1163
|
return null;
|
|
1172
1164
|
const saw = new Map(r.saw).set(event.id, mine);
|
|
1173
|
-
const
|
|
1165
|
+
const byId = new Map(r.byId).set(event.id, event);
|
|
1174
1166
|
const named = new Set(r.named);
|
|
1175
1167
|
if (event.kind === 'assign' && event.assigned)
|
|
1176
1168
|
named.add(event.did);
|
|
@@ -1179,11 +1171,10 @@ function extended(r, event) {
|
|
|
1179
1171
|
invites.add(event.inviteKey);
|
|
1180
1172
|
// It saw everything: its view is the current state.
|
|
1181
1173
|
const view = r.state;
|
|
1182
|
-
const
|
|
1183
|
-
const validDefViews = event.kind === 'role' ? new Map(r.validDefViews).set(event.id, view) : r.validDefViews;
|
|
1174
|
+
const withView = (views) => event.kind === 'role' ? new Map(views).set(event.id, view) : views;
|
|
1184
1175
|
const base = {
|
|
1185
1176
|
genesis,
|
|
1186
|
-
byId
|
|
1177
|
+
byId,
|
|
1187
1178
|
waiting: r.waiting,
|
|
1188
1179
|
order,
|
|
1189
1180
|
saw,
|
|
@@ -1191,15 +1182,14 @@ function extended(r, event) {
|
|
|
1191
1182
|
named,
|
|
1192
1183
|
invites,
|
|
1193
1184
|
views: new Map(),
|
|
1194
|
-
defViews,
|
|
1185
|
+
defViews: withView(r.defViews),
|
|
1195
1186
|
};
|
|
1196
|
-
const result = step(event, view, { ...r, saw, byId
|
|
1187
|
+
const result = step(event, view, { ...r, saw, byId });
|
|
1197
1188
|
if (typeof result === 'string')
|
|
1198
1189
|
return { ...r, ...base, refused: new Map(r.refused).set(event.id, result) };
|
|
1199
|
-
//
|
|
1200
|
-
// reduction can, since it saw them all.
|
|
1190
|
+
// It saw every reduction, so only a revoke or fork of its key can strike it.
|
|
1201
1191
|
const strikers = result.reachedBy;
|
|
1202
|
-
// A stepping down its key's revoke reaches ends founder status
|
|
1192
|
+
// A stepping down its key's revoke reaches only ends founder status: only a whole replay says.
|
|
1203
1193
|
if (strikers.length > 0 && event.kind === 'stepdown')
|
|
1204
1194
|
return null;
|
|
1205
1195
|
const taking = result.reduction ? new Map(r.taking).set(event.id, result.reduction) : r.taking;
|
|
@@ -1210,9 +1200,9 @@ function extended(r, event) {
|
|
|
1210
1200
|
strikes.set(id, [...(strikes.get(id) ?? []), event.id]);
|
|
1211
1201
|
return { ...r, ...base, strikes, struck: new Map(r.struck).set(event.id, STRUCK), allowedIds, taking };
|
|
1212
1202
|
}
|
|
1213
|
-
//
|
|
1203
|
+
// Every allowed change is valid: the state is it on top of the current one.
|
|
1214
1204
|
const state = result.after;
|
|
1215
|
-
// Due once it takes reading from someone: the key saw everything else
|
|
1205
|
+
// Due once it takes reading from someone: the key saw everything else.
|
|
1216
1206
|
state.keyDue = result.tookReading && state.keys.length > 0 && event.kind !== 'key';
|
|
1217
1207
|
const taken = new Map(r.taken);
|
|
1218
1208
|
const forks = new Map(r.forks);
|
|
@@ -1221,7 +1211,7 @@ function extended(r, event) {
|
|
|
1221
1211
|
return {
|
|
1222
1212
|
...r,
|
|
1223
1213
|
...base,
|
|
1224
|
-
validDefViews,
|
|
1214
|
+
validDefViews: withView(r.validDefViews),
|
|
1225
1215
|
valid: new Set(r.valid).add(event.id),
|
|
1226
1216
|
allowedIds,
|
|
1227
1217
|
taking,
|
|
@@ -1288,8 +1278,7 @@ function historyOf(r) {
|
|
|
1288
1278
|
const sawOrIs = (head, id) => head === id || r.saw.get(head).has(id);
|
|
1289
1279
|
const chosen = [];
|
|
1290
1280
|
for (const key of keys) {
|
|
1291
|
-
const
|
|
1292
|
-
const latest = mine.at(-1);
|
|
1281
|
+
const latest = r.order.filter((id) => r.valid.has(id) && r.byId.get(id).key === key).at(-1);
|
|
1293
1282
|
if (!latest || chosen.some((head) => sawOrIs(head, latest)))
|
|
1294
1283
|
continue;
|
|
1295
1284
|
const head = recent.find((id) => sawOrIs(id, latest));
|
|
@@ -1323,7 +1312,7 @@ function historyOf(r) {
|
|
|
1323
1312
|
return { ok: false, reason: 'Its writer’s log forked at or before it', withdrawn: true };
|
|
1324
1313
|
if (!needs(at(record.seen, record.seen)))
|
|
1325
1314
|
return { ok: false, reason: 'Its author was not allowed to, as of what it had seen' };
|
|
1326
|
-
//
|
|
1315
|
+
// A removal or an unassignment it had not seen takes everything.
|
|
1327
1316
|
const cut = cutOf(record.seen);
|
|
1328
1317
|
const reduced = (reduction) => reduction.reach.kind === 'account' && !cut.has(reduction.id) && takes(reduction);
|
|
1329
1318
|
if ((r.taken.get(record.root) ?? []).some(reduced))
|
|
@@ -1360,4 +1349,22 @@ function historyOf(r) {
|
|
|
1360
1349
|
});
|
|
1361
1350
|
return history;
|
|
1362
1351
|
}
|
|
1352
|
+
// ─── Plumbing ─────────────────────────────────────────────────────────
|
|
1353
|
+
/** A key's line for one collection, as a map key */
|
|
1354
|
+
const lineOf = (writer, collection) => `${writer}\n${collection}`;
|
|
1355
|
+
const keyEpoch = (event) => ({
|
|
1356
|
+
keyId: event.keyId,
|
|
1357
|
+
readKey: event.readKey,
|
|
1358
|
+
event: event.id,
|
|
1359
|
+
});
|
|
1360
|
+
const compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
1361
|
+
const idOrder = (a, b) => compare(a.id, b.id);
|
|
1362
|
+
const sortedUnique = (values) => [...new Set(values)].sort();
|
|
1363
|
+
function pushTo(map, key, value) {
|
|
1364
|
+
const list = map.get(key);
|
|
1365
|
+
if (list)
|
|
1366
|
+
list.push(value);
|
|
1367
|
+
else
|
|
1368
|
+
map.set(key, [value]);
|
|
1369
|
+
}
|
|
1363
1370
|
//# sourceMappingURL=roles.js.map
|