@serfab/cadre-core 1.13.0 → 1.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cadre-invite-protocol.d.ts +348 -0
- package/dist/cadre-invite-protocol.js +605 -0
- package/dist/cadre-invite-protocol.js.map +1 -0
- package/dist/cadre-node.d.ts +523 -180
- package/dist/cadre-node.js +921 -293
- package/dist/cadre-node.js.map +1 -1
- package/dist/claim-proof.d.ts +45 -0
- package/dist/claim-proof.js +132 -0
- package/dist/claim-proof.js.map +1 -0
- package/dist/control-authorization.d.ts +80 -19
- package/dist/control-authorization.js +55 -13
- package/dist/control-authorization.js.map +1 -1
- package/dist/control-cohort.js +1 -1
- package/dist/control-cohort.js.map +1 -1
- package/dist/control-database.d.ts +514 -97
- package/dist/control-database.js +1033 -195
- package/dist/control-database.js.map +1 -1
- package/dist/control-protocol-guard.d.ts +142 -0
- package/dist/control-protocol-guard.js +293 -0
- package/dist/control-protocol-guard.js.map +1 -0
- package/dist/control-schema.d.ts +1 -1
- package/dist/control-schema.js +801 -1067
- package/dist/control-schema.js.map +1 -1
- package/dist/control-stream.d.ts +4 -0
- package/dist/control-stream.js +22 -7
- package/dist/control-stream.js.map +1 -1
- package/dist/delegate-admission.d.ts +1 -1
- package/dist/delegate-admission.js +1 -1
- package/dist/device-token.d.ts +11 -9
- package/dist/device-token.js +14 -12
- package/dist/device-token.js.map +1 -1
- package/dist/ed25519-key.d.ts +15 -0
- package/dist/ed25519-key.js +28 -1
- package/dist/ed25519-key.js.map +1 -1
- package/dist/index.d.ts +14 -9
- package/dist/index.js +27 -9
- package/dist/index.js.map +1 -1
- package/dist/link-budget.d.ts +16 -29
- package/dist/link-budget.js +16 -31
- package/dist/link-budget.js.map +1 -1
- package/dist/membership-connection-gater.d.ts +190 -151
- package/dist/membership-connection-gater.js +235 -173
- package/dist/membership-connection-gater.js.map +1 -1
- package/dist/node-claim-payload.d.ts +19 -0
- package/dist/node-claim-payload.js +120 -0
- package/dist/node-claim-payload.js.map +1 -0
- package/dist/owner-anchor-sync.d.ts +22 -0
- package/dist/owner-anchor-sync.js +143 -0
- package/dist/owner-anchor-sync.js.map +1 -0
- package/dist/peer-authorization.d.ts +113 -18
- package/dist/peer-authorization.js +225 -29
- package/dist/peer-authorization.js.map +1 -1
- package/dist/peer-dial.d.ts +19 -0
- package/dist/peer-dial.js +22 -1
- package/dist/peer-dial.js.map +1 -1
- package/dist/peer-join-backfill.d.ts +26 -2
- package/dist/peer-join-backfill.js +54 -12
- package/dist/peer-join-backfill.js.map +1 -1
- package/dist/peer-record.d.ts +13 -9
- package/dist/peer-record.js +16 -12
- package/dist/peer-record.js.map +1 -1
- package/dist/pending-join-runner.d.ts +66 -58
- package/dist/pending-join-runner.js +136 -145
- package/dist/pending-join-runner.js.map +1 -1
- package/dist/push-fanout.d.ts +8 -2
- package/dist/push-fanout.js +8 -2
- package/dist/push-fanout.js.map +1 -1
- package/dist/seed-bootstrap.d.ts +147 -122
- package/dist/seed-bootstrap.js +284 -269
- package/dist/seed-bootstrap.js.map +1 -1
- package/dist/seed-trust-policy.d.ts +98 -37
- package/dist/seed-trust-policy.js +188 -2
- package/dist/seed-trust-policy.js.map +1 -1
- package/dist/strand-addr-protocol.d.ts +24 -26
- package/dist/strand-addr-protocol.js +19 -26
- package/dist/strand-addr-protocol.js.map +1 -1
- package/dist/strand-formation-protocol.js +1 -1
- package/dist/strand-formation-protocol.js.map +1 -1
- package/dist/strand-revocation-enforcer.d.ts +11 -11
- package/dist/strand-revocation-enforcer.js +11 -11
- package/dist/strand-wake-protocol.d.ts +13 -16
- package/dist/strand-wake-protocol.js +13 -20
- package/dist/strand-wake-protocol.js.map +1 -1
- package/dist/trusted-owner-store.d.ts +35 -5
- package/dist/trusted-owner-store.js +50 -8
- package/dist/trusted-owner-store.js.map +1 -1
- package/dist/types.d.ts +302 -87
- package/dist/types.js.map +1 -1
- package/package.json +8 -7
- package/src/cadre-invite-protocol.ts +864 -0
- package/src/cadre-node.ts +9381 -8686
- package/src/claim-proof.ts +147 -0
- package/src/control-authorization.ts +203 -117
- package/src/control-cohort.ts +1 -1
- package/src/control-database.ts +1216 -218
- package/src/control-protocol-guard.ts +348 -0
- package/src/control-schema.ts +812 -1078
- package/src/control-stream.ts +26 -7
- package/src/delegate-admission.ts +1 -1
- package/src/device-token.ts +14 -10
- package/src/ed25519-key.ts +28 -1
- package/src/index.ts +84 -10
- package/src/link-budget.ts +16 -32
- package/src/membership-connection-gater.ts +637 -570
- package/src/node-claim-payload.ts +125 -0
- package/src/owner-anchor-sync.ts +171 -0
- package/src/peer-authorization.ts +258 -29
- package/src/peer-dial.ts +22 -1
- package/src/peer-join-backfill.ts +56 -11
- package/src/peer-record.ts +16 -11
- package/src/pending-join-runner.ts +729 -735
- package/src/push-fanout.ts +8 -2
- package/src/seed-bootstrap.ts +344 -321
- package/src/seed-trust-policy.ts +353 -140
- package/src/strand-addr-protocol.ts +26 -35
- package/src/strand-formation-protocol.ts +1 -1
- package/src/strand-revocation-enforcer.ts +11 -11
- package/src/strand-wake-protocol.ts +13 -23
- package/src/trusted-owner-store.ts +242 -171
- package/src/types.ts +2471 -2242
|
@@ -83,23 +83,23 @@
|
|
|
83
83
|
*
|
|
84
84
|
* ## What transfers from the control gater (`membership-connection-gater.ts`)
|
|
85
85
|
*
|
|
86
|
-
* Transfers: the two-layer shape (fail-closed per-stream gate as PRIMARY,
|
|
87
|
-
*
|
|
86
|
+
* Transfers: the two-layer shape (fail-closed per-stream gate as PRIMARY, a
|
|
87
|
+
* connection-level layer on top), the synchronous in-memory
|
|
88
88
|
* snapshot judged by the stream gate (a live DB read inside a gate deadlocks —
|
|
89
89
|
* the same argument as the control node's `authorizeInboundControlStream`), the
|
|
90
90
|
* fail-open-on-error/timeout posture for the connection hooks
|
|
91
91
|
* (`decideWithinDeadline`, reused as-is), and the "compose over the caller's
|
|
92
92
|
* gater, every base hook preserved" mechanics.
|
|
93
93
|
*
|
|
94
|
-
* Does NOT transfer — the relay-reservation seam
|
|
95
|
-
* unauthorized budget). That
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* verdict, no
|
|
94
|
+
* Does NOT transfer — provisional admission and the relay-reservation seam
|
|
95
|
+
* (`'admit-provisionally'`, its deadline, the unauthorized budget). That
|
|
96
|
+
* machinery exists because the control gate judges on ABSENCE of placement,
|
|
97
|
+
* where a member whose row is in flight, or a device whose invitation row has
|
|
98
|
+
* not arrived, is indistinguishable from an outsider, and a wrong reservation
|
|
99
|
+
* deny is unrecoverable. This gate denies on POSITIVE revocation evidence, so a
|
|
100
|
+
* deny is never that kind of wrong answer; the one stale-wrong case (a missed
|
|
101
|
+
* re-add) heals via replication as above. Every hook here is therefore a plain
|
|
102
|
+
* "deny iff revoked" — no budget, no provisional verdict, no deadline.
|
|
103
103
|
*
|
|
104
104
|
* Does NOT transfer — the bring-up quiet period. A strand node's DB bring-up
|
|
105
105
|
* NEEDS its cross-party cohort connections; this enforcer simply starts with an
|
|
@@ -16,13 +16,12 @@
|
|
|
16
16
|
*
|
|
17
17
|
* **Authorization (v1):** a wake is low-risk — it only causes the receiver to
|
|
18
18
|
* spend resources coming online for a strand it already participates in — so the
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* a voucher that verifies against an owner key in the receiver's
|
|
23
|
-
* trusted-owner anchor), so a peer that merely published rows into the
|
|
24
|
-
* control DB
|
|
25
|
-
* predicate it is given.
|
|
19
|
+
* request carries no signature. Membership is checked before this handler runs, by
|
|
20
|
+
* the control node's protocol guard (`control-protocol-guard.ts`, the `members`
|
|
21
|
+
* class: `CadreNode.isAuthorizedMember`, under which the sender's `CadrePeer` row
|
|
22
|
+
* must carry a voucher that verifies against an owner key in the receiver's
|
|
23
|
+
* node-local trusted-owner anchor), so a peer that merely published rows into the
|
|
24
|
+
* replicated control DB gets its stream reset and never reaches this module.
|
|
26
25
|
*/
|
|
27
26
|
|
|
28
27
|
import debug from 'debug';
|
|
@@ -55,8 +54,8 @@ const MAX_WAKE_SIZE = 64 * 1024;
|
|
|
55
54
|
* It holds only link work because the receiver acks as soon as it has DECIDED, before the
|
|
56
55
|
* wake itself runs ({@link StrandWakeService.processWakeRequest}).
|
|
57
56
|
*
|
|
58
|
-
* NOTE: the receiver's membership check (two live control reads
|
|
59
|
-
* is not counted. In steady state those reads touch only held blocks and do not consult the
|
|
57
|
+
* NOTE: the receiver's membership check (two live control reads, in its protocol guard) runs
|
|
58
|
+
* inside this deadline and is not counted. In steady state those reads touch only held blocks and do not consult the
|
|
60
59
|
* cohort. If a wake or address request is seen timing out while the receiver's membership read
|
|
61
60
|
* is consulting, count one membership decision in this deadline or answer the check from the
|
|
62
61
|
* materialized authorized-peer snapshot. See docs/cadre-consistency.md → "Deadlines Over
|
|
@@ -122,8 +121,6 @@ async function readFrame<T>(stream: ControlStream, timeoutMs: number): Promise<T
|
|
|
122
121
|
* (`CadreNode`), injected so the service is testable without a full node.
|
|
123
122
|
*/
|
|
124
123
|
export interface StrandWakeServiceOptions {
|
|
125
|
-
/** Membership gate: is the remote peer a `CadrePeer` member of this cadre? */
|
|
126
|
-
isMember(remotePeerId: string): Promise<boolean>;
|
|
127
124
|
/** Look up a local strand instance by id (undefined if not participated in). */
|
|
128
125
|
getStrand(strandId: string): StrandInstance | undefined;
|
|
129
126
|
/**
|
|
@@ -157,9 +154,10 @@ export interface StrandWakeServiceOptions {
|
|
|
157
154
|
|
|
158
155
|
/**
|
|
159
156
|
* Receiver side of the push-wake protocol. Registers a `WAKE_PROTOCOL` handler
|
|
160
|
-
* on the control node and, for each inbound {@link WakeRequest}
|
|
161
|
-
*
|
|
162
|
-
* strand if it is hibernating/idle
|
|
157
|
+
* on the control node and, for each inbound {@link WakeRequest} (from a peer the
|
|
158
|
+
* protocol guard has already admitted as an authorized member), replies with a
|
|
159
|
+
* {@link WakeAck} and starts resuming the named strand if it is hibernating/idle
|
|
160
|
+
* and we participate in it.
|
|
163
161
|
*/
|
|
164
162
|
export class StrandWakeService {
|
|
165
163
|
private readonly options: StrandWakeServiceOptions;
|
|
@@ -249,8 +247,7 @@ export class StrandWakeService {
|
|
|
249
247
|
* Decide the wake for a decoded request and start it. Exposed (not private) so
|
|
250
248
|
* the decision matrix can be unit-tested directly.
|
|
251
249
|
*
|
|
252
|
-
* -
|
|
253
|
-
* - Unknown / not-participated strand → rejected.
|
|
250
|
+
* - Unknown / not-participated strand → rejected (`accepted: false`).
|
|
254
251
|
* - Hibernating or idle strand → `accepted` with that status, and a wake started.
|
|
255
252
|
* - Already-live strand → no-op, `accepted` with current status.
|
|
256
253
|
*
|
|
@@ -260,13 +257,6 @@ export class StrandWakeService {
|
|
|
260
257
|
* sender's attempt deadline, coupling that deadline to this node's own budgets.
|
|
261
258
|
*/
|
|
262
259
|
async processWakeRequest(request: WakeRequest, remotePeerId: string): Promise<WakeAck> {
|
|
263
|
-
// The injected membership predicate is the whole v1 authorization (CadreNode
|
|
264
|
-
// supplies the voucher-anchored one); only a peer it admits may ask us to wake.
|
|
265
|
-
if (!(await this.options.isMember(remotePeerId))) {
|
|
266
|
-
log('Rejecting wake from non-member %s', remotePeerId);
|
|
267
|
-
return { accepted: false, reason: 'Sender is not a cadre member' };
|
|
268
|
-
}
|
|
269
|
-
|
|
270
260
|
const instance = this.options.getStrand(request.strandId);
|
|
271
261
|
if (!instance) {
|
|
272
262
|
log('Rejecting wake for unknown/unparticipated strand %s', request.strandId);
|
|
@@ -1,171 +1,242 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Node-local, NON-replicated trusted-owner anchor.
|
|
3
|
-
*
|
|
4
|
-
* The replicated `CadreControl.OwnerKey` table cannot anchor trust: any
|
|
5
|
-
* connecting node can genesis-insert its own key into its local table and that
|
|
6
|
-
* row replicates into every peer's copy, so "is this key in `OwnerKey`?" can be
|
|
7
|
-
* made true by a stranger. This store is the anchor that CAN be trusted — a
|
|
8
|
-
* per-party, on-device record of owner keys established OUT OF BAND: founding
|
|
9
|
-
* the party (genesis self-trust), the pinned keys carried by the
|
|
10
|
-
* that enrolled this node,
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
*
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
return
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Node-local, NON-replicated trusted-owner anchor.
|
|
3
|
+
*
|
|
4
|
+
* The replicated `CadreControl.OwnerKey` table cannot anchor trust: any
|
|
5
|
+
* connecting node can genesis-insert its own key into its local table and that
|
|
6
|
+
* row replicates into every peer's copy, so "is this key in `OwnerKey`?" can be
|
|
7
|
+
* made true by a stranger. This store is the anchor that CAN be trusted — a
|
|
8
|
+
* per-party, on-device record of owner keys established OUT OF BAND: founding
|
|
9
|
+
* the party (genesis self-trust), the pinned keys carried by the cadre invitation
|
|
10
|
+
* that enrolled this node, an explicit operator pin, or the claim of a brand-new
|
|
11
|
+
* node by whoever holds its one-time claim secret. It is never written from
|
|
12
|
+
* replicated control state; that is the whole point.
|
|
13
|
+
*
|
|
14
|
+
* Three implementations:
|
|
15
|
+
* - {@link MemoryTrustedOwnerStore} (this module, cross-platform) — ephemeral;
|
|
16
|
+
* the default when no store is injected via `CadreNodeConfig.trustedOwners`.
|
|
17
|
+
* - {@link PersistentTrustedOwnerStore} (this module, cross-platform) — durable
|
|
18
|
+
* over any `DurableSlot` the embedding app supplies (IndexedDB, SecureStore,
|
|
19
|
+
* SQLite, …). The load/persist policy lives in `node-local-snapshot.ts`; read
|
|
20
|
+
* {@link NodeLocalSnapshot.open} for what an absent, corrupt, foreign-party
|
|
21
|
+
* or unreadable slot does.
|
|
22
|
+
* - `FileTrustedOwnerStore` — the above over a Node file in the node's state
|
|
23
|
+
* directory; Node-only, behind the subpath
|
|
24
|
+
* `@serfab/cadre-core/trusted-owner-store-file` (same isolation pattern as
|
|
25
|
+
* `key-store-file`) so `node:fs` never lands in the RN/browser entry graph.
|
|
26
|
+
*
|
|
27
|
+
* The anchor follows the replicated `OwnerKey` table along VERIFIABLE chains only
|
|
28
|
+
* (`CadreNode.syncOwnerAnchor`, `owner-anchor-sync.ts`): a key whose stored proof verifies
|
|
29
|
+
* against a key already anchored enters under source `chain`, and a key named by a
|
|
30
|
+
* tombstone whose stored signer is anchored leaves. The out-of-band entries are the base
|
|
31
|
+
* that derivation starts from; a replicated row can never seat itself here.
|
|
32
|
+
*
|
|
33
|
+
* Two callers remove keys: the claim-secret trust policy rolling back a claim whose
|
|
34
|
+
* persist failed (`claimSecretTrustPolicy` in `seed-trust-policy.ts`), and the anchor
|
|
35
|
+
* sync applying a verifiable `OwnerKey` tombstone or pruning a `chain` entry whose row
|
|
36
|
+
* is gone. Nothing else removes a key.
|
|
37
|
+
*/
|
|
38
|
+
import debug from 'debug';
|
|
39
|
+
import { NodeLocalSnapshot, type DurableSlot, type NodeLocalSnapshotSpec } from './node-local-snapshot.js';
|
|
40
|
+
|
|
41
|
+
const log = debug('sereus:cadre:trusted-owner-store');
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* How an owner key entered the anchor. Every source but `chain` is out-of-band provenance
|
|
45
|
+
* and forms the base the anchor sync derives from; `claim` is the durable "this node has
|
|
46
|
+
* been claimed" marker: the first seed whose sender proved it held the node's claim secret
|
|
47
|
+
* anchored its signer under this source. `chain` marks a key derived from the replicated
|
|
48
|
+
* `OwnerKey` table through a proof that verified against a key already anchored
|
|
49
|
+
* (`CadreNode.syncOwnerAnchor`); the sync keeps such an entry while its row is live and
|
|
50
|
+
* prunes it once the row is gone, which a base entry never is.
|
|
51
|
+
*/
|
|
52
|
+
export type TrustSource = 'genesis' | 'invite' | 'operator' | 'claim' | 'chain';
|
|
53
|
+
|
|
54
|
+
export interface TrustedOwnerStore {
|
|
55
|
+
/** Party this anchor is scoped to. */
|
|
56
|
+
readonly partyId: string;
|
|
57
|
+
|
|
58
|
+
/** Is this ed25519 (base64url) key one of my party's out-of-band-trusted owners? */
|
|
59
|
+
has(ownerKey: string): boolean;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* All anchored owner keys (e.g. for seed-trust `knownOwnerKeys`).
|
|
63
|
+
*
|
|
64
|
+
* NOTE: every backend copies into a fresh Set per call (so the result is a
|
|
65
|
+
* snapshot decoupled from later `trust()` calls). Anchors hold a handful of
|
|
66
|
+
* keys and callers are per-seed, so the copy is free today; if a hot path
|
|
67
|
+
* ever calls this per message, prefer `has()` or cache the snapshot.
|
|
68
|
+
*/
|
|
69
|
+
all(): ReadonlySet<string>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Every anchored key with its provenance, as a snapshot (same copy contract as
|
|
73
|
+
* {@link all}). The anchor sync splits the base (every source but `chain`) from the
|
|
74
|
+
* derived entries with this.
|
|
75
|
+
*/
|
|
76
|
+
sources(): ReadonlyMap<string, TrustSource>;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Add a key. Idempotent with one asymmetry in provenance: re-trusting a known key
|
|
80
|
+
* keeps its source, EXCEPT that an out-of-band source over an existing `chain` entry
|
|
81
|
+
* rewrites the provenance (out of band wins: the key is then base, and a later sync
|
|
82
|
+
* no longer prunes it when its row goes), while `trust(key, 'chain')` over any
|
|
83
|
+
* existing entry is a no-op. Implementations MUST reflect the key in {@link has} /
|
|
84
|
+
* {@link all} synchronously; the returned promise tracks durability only
|
|
85
|
+
* (a persistent backend's write), so a synchronous caller may safely consult
|
|
86
|
+
* the store right after invoking this.
|
|
87
|
+
*/
|
|
88
|
+
trust(ownerKey: string, source: TrustSource): Promise<void>;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Drop a key, with {@link trust}'s contract: gone from {@link has} / {@link all}
|
|
92
|
+
* synchronously, the returned promise tracks durability. Removing an absent key
|
|
93
|
+
* is a no-op. See the module doc for who may call this.
|
|
94
|
+
*/
|
|
95
|
+
remove(ownerKey: string): Promise<void>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Ephemeral in-memory anchor for nodes without durable storage (tests, browser
|
|
100
|
+
* demos, not-yet-persisted mobile). Same contract, no disk: trust established
|
|
101
|
+
* here must be re-supplied (invite / operator pin) on the next process.
|
|
102
|
+
*/
|
|
103
|
+
export class MemoryTrustedOwnerStore implements TrustedOwnerStore {
|
|
104
|
+
private readonly keys = new Map<string, TrustSource>();
|
|
105
|
+
|
|
106
|
+
constructor(readonly partyId: string) {}
|
|
107
|
+
|
|
108
|
+
has(ownerKey: string): boolean {
|
|
109
|
+
return this.keys.has(ownerKey);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
all(): ReadonlySet<string> {
|
|
113
|
+
return new Set(this.keys.keys());
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
sources(): ReadonlyMap<string, TrustSource> {
|
|
117
|
+
return new Map(this.keys);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async trust(ownerKey: string, source: TrustSource): Promise<void> {
|
|
121
|
+
if (!provenanceChanges(this.keys.get(ownerKey), source)) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
this.keys.set(ownerKey, source);
|
|
125
|
+
log('trusted owner key anchored (party=%s, source=%s)', this.partyId, source);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async remove(ownerKey: string): Promise<void> {
|
|
129
|
+
if (this.keys.delete(ownerKey)) {
|
|
130
|
+
log('trusted owner key removed (party=%s)', this.partyId);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Does `trust(key, source)` write, given the key's current provenance (`undefined` when
|
|
137
|
+
* absent)? The one rule both backends share (see {@link TrustedOwnerStore.trust}): an absent
|
|
138
|
+
* key always lands; a `chain` entry is promoted by any out-of-band source; everything else
|
|
139
|
+
* keeps its original provenance.
|
|
140
|
+
*/
|
|
141
|
+
function provenanceChanges(current: TrustSource | undefined, source: TrustSource): boolean {
|
|
142
|
+
return current === undefined || (current === 'chain' && source !== 'chain');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** One anchored key as persisted: provenance + wall-clock trust time (ms). */
|
|
146
|
+
interface TrustedOwnerEntry {
|
|
147
|
+
source: TrustSource;
|
|
148
|
+
trustedAt: number;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Every TrustSource must be listed: under the discard-all policy below, one entry with a
|
|
152
|
+
// source this set does not know discards the WHOLE anchor on reload, so a node whose only
|
|
153
|
+
// owner was anchored by a claim would silently come back unclaimed.
|
|
154
|
+
const KNOWN_SOURCES: ReadonlySet<string> = new Set<TrustSource>(['genesis', 'invite', 'operator', 'claim', 'chain']);
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* What the anchor persists: `owners` maps ownerKey (base64url) -> provenance.
|
|
158
|
+
*
|
|
159
|
+
* One unusable entry discards the WHOLE record: trusting a subset of the keys a
|
|
160
|
+
* record claims is a silent, security-relevant downgrade, so a record that
|
|
161
|
+
* cannot be read in full is treated as no anchor at all (see
|
|
162
|
+
* `UnusableEntryPolicy` in `node-local-snapshot.ts`).
|
|
163
|
+
*/
|
|
164
|
+
const TRUSTED_OWNER_SNAPSHOT: NodeLocalSnapshotSpec<TrustedOwnerEntry> = {
|
|
165
|
+
label: 'trusted-owner anchor',
|
|
166
|
+
payloadKey: 'owners',
|
|
167
|
+
unusableEntry: 'discard-all',
|
|
168
|
+
acceptEntry: (_ownerKey, entry) => {
|
|
169
|
+
if (typeof entry !== 'object' || entry === null) return undefined;
|
|
170
|
+
const { source, trustedAt } = entry as { source?: unknown; trustedAt?: unknown };
|
|
171
|
+
if (typeof source !== 'string' || !KNOWN_SOURCES.has(source)) return undefined;
|
|
172
|
+
if (typeof trustedAt !== 'number') return undefined;
|
|
173
|
+
return { source: source as TrustSource, trustedAt };
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Durable {@link TrustedOwnerStore} over an app-supplied {@link DurableSlot} —
|
|
179
|
+
* the cross-platform half of every persistent backend (the Node
|
|
180
|
+
* `FileTrustedOwnerStore` is this class over a file slot).
|
|
181
|
+
*
|
|
182
|
+
* Construct via {@link open}. Load and persist policy — what an absent,
|
|
183
|
+
* corrupt, foreign-party or unreadable slot does, and what a failed persist
|
|
184
|
+
* does — is documented once on `NodeLocalSnapshot`; this class only supplies
|
|
185
|
+
* the payload shape and the anchor's whole-record-on-bad-entry policy.
|
|
186
|
+
*/
|
|
187
|
+
export class PersistentTrustedOwnerStore implements TrustedOwnerStore {
|
|
188
|
+
private constructor(private readonly snapshot: NodeLocalSnapshot<TrustedOwnerEntry>) {}
|
|
189
|
+
|
|
190
|
+
/** Load (or cold-start) the party's anchor from `slot`. */
|
|
191
|
+
static async open(slot: DurableSlot, partyId: string): Promise<PersistentTrustedOwnerStore> {
|
|
192
|
+
return new PersistentTrustedOwnerStore(
|
|
193
|
+
await NodeLocalSnapshot.open(slot, partyId, TRUSTED_OWNER_SNAPSHOT)
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
get partyId(): string {
|
|
198
|
+
return this.snapshot.partyId;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
has(ownerKey: string): boolean {
|
|
202
|
+
return this.snapshot.has(ownerKey);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
all(): ReadonlySet<string> {
|
|
206
|
+
return this.snapshot.keySnapshot();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
sources(): ReadonlyMap<string, TrustSource> {
|
|
210
|
+
return new Map(Array.from(this.snapshot.entrySnapshot(), ([key, entry]) => [key, entry.source]));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Anchor a key: visible via {@link has} / {@link all} synchronously, then the
|
|
215
|
+
* full snapshot is persisted (see `NodeLocalSnapshot.put`).
|
|
216
|
+
*/
|
|
217
|
+
trust(ownerKey: string, source: TrustSource): Promise<void> {
|
|
218
|
+
if (!provenanceChanges(this.snapshot.get(ownerKey)?.source, source)) {
|
|
219
|
+
// Idempotent: re-enrollment / restart re-seeding keeps the original
|
|
220
|
+
// provenance and skips the write entirely.
|
|
221
|
+
// NOTE: this resolves immediately rather than joining the write chain, so
|
|
222
|
+
// re-trusting a key whose first persist is still in flight does not await
|
|
223
|
+
// that persist. Harmless for the seeding callers (they re-trust only after
|
|
224
|
+
// the original settled, or do not await at all); chain it if a caller ever
|
|
225
|
+
// needs "durable by the time trust() resolves" for a repeat key.
|
|
226
|
+
return Promise.resolve();
|
|
227
|
+
}
|
|
228
|
+
log('trusted owner key anchored (party=%s, source=%s); persisting', this.partyId, source);
|
|
229
|
+
return this.snapshot.put(ownerKey, { source, trustedAt: Date.now() });
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Drop a key: gone from {@link has} / {@link all} synchronously, then the full
|
|
234
|
+
* snapshot is persisted (see `NodeLocalSnapshot.remove`).
|
|
235
|
+
*/
|
|
236
|
+
remove(ownerKey: string): Promise<void> {
|
|
237
|
+
if (this.snapshot.has(ownerKey)) {
|
|
238
|
+
log('trusted owner key removed (party=%s); persisting', this.partyId);
|
|
239
|
+
}
|
|
240
|
+
return this.snapshot.remove(ownerKey);
|
|
241
|
+
}
|
|
242
|
+
}
|