@aithos/sdk 0.1.0-alpha.3 → 0.1.0-alpha.31
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 +159 -0
- package/dist/src/auth-api.d.ts +149 -0
- package/dist/src/auth-api.js +226 -0
- package/dist/src/auth.d.ts +436 -67
- package/dist/src/auth.js +1098 -69
- package/dist/src/compute.d.ts +221 -9
- package/dist/src/compute.js +293 -16
- package/dist/src/data-schema-contacts-v1.d.ts +14 -0
- package/dist/src/data-schema-contacts-v1.js +28 -0
- package/dist/src/data.d.ts +97 -0
- package/dist/src/data.js +634 -0
- package/dist/src/endpoints.d.ts +9 -0
- package/dist/src/endpoints.js +5 -0
- package/dist/src/ethos.d.ts +202 -1
- package/dist/src/ethos.js +821 -16
- package/dist/src/index.d.ts +15 -6
- package/dist/src/index.js +36 -9
- package/dist/src/internal/delegate-bundle.d.ts +18 -0
- package/dist/src/internal/delegate-bundle.js +94 -0
- package/dist/src/internal/delegate-state.d.ts +45 -0
- package/dist/src/internal/delegate-state.js +120 -0
- package/dist/src/internal/owner-signers.d.ts +78 -0
- package/dist/src/internal/owner-signers.js +179 -0
- package/dist/src/internal/protocol-client-bridge.d.ts +8 -0
- package/dist/src/internal/protocol-client-bridge.js +20 -0
- package/dist/src/internal/recovery-file.d.ts +29 -0
- package/dist/src/internal/recovery-file.js +98 -0
- package/dist/src/internal/signer.d.ts +59 -0
- package/dist/src/internal/signer.js +86 -0
- package/dist/src/key-store.d.ts +128 -0
- package/dist/src/key-store.js +244 -0
- package/dist/src/mandates.d.ts +163 -1
- package/dist/src/mandates.js +286 -8
- package/dist/src/sdk.d.ts +39 -3
- package/dist/src/sdk.js +36 -23
- package/dist/src/session-store.d.ts +58 -0
- package/dist/src/session-store.js +158 -0
- package/dist/src/wallet.d.ts +4 -6
- package/dist/src/wallet.js +18 -8
- package/dist/src/web.d.ts +279 -0
- package/dist/src/web.js +186 -0
- package/dist/test/auth-j3.test.d.ts +2 -0
- package/dist/test/auth-j3.test.js +391 -0
- package/dist/test/compute-delegate-path.test.d.ts +2 -0
- package/dist/test/compute-delegate-path.test.js +183 -0
- package/dist/test/compute.test.js +26 -11
- package/dist/test/endpoints.test.js +20 -1
- package/dist/test/ethos-first-edition.test.d.ts +2 -0
- package/dist/test/ethos-first-edition.test.js +248 -0
- package/dist/test/ethos.test.d.ts +2 -0
- package/dist/test/ethos.test.js +219 -0
- package/dist/test/key-store.test.d.ts +2 -0
- package/dist/test/key-store.test.js +161 -0
- package/dist/test/mandates-compute.test.d.ts +2 -0
- package/dist/test/mandates-compute.test.js +256 -0
- package/dist/test/mandates.test.d.ts +2 -0
- package/dist/test/mandates.test.js +93 -0
- package/dist/test/sdk.test.js +70 -30
- package/dist/test/signer.test.d.ts +2 -0
- package/dist/test/signer.test.js +117 -0
- package/dist/test/signup-bootstrap.test.d.ts +2 -0
- package/dist/test/signup-bootstrap.test.js +222 -0
- package/dist/test/wallet.test.js +20 -9
- package/dist/test/web.test.d.ts +2 -0
- package/dist/test/web.test.js +270 -0
- package/package.json +5 -4
package/dist/src/ethos.js
CHANGED
|
@@ -1,21 +1,826 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
// Copyright 2026 Mathieu Colla
|
|
3
|
-
//
|
|
3
|
+
// `sdk.ethos` namespace — high-level ethos editing.
|
|
4
4
|
//
|
|
5
|
-
//
|
|
6
|
-
// `@aithos/protocol-client` for the common operations: parsing zone
|
|
7
|
-
// markdown, composing zone documents, building signed editions of the
|
|
8
|
-
// user's ethos.
|
|
5
|
+
// Lazy + commit pattern:
|
|
9
6
|
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
|
|
13
|
-
//
|
|
14
|
-
|
|
15
|
-
//
|
|
16
|
-
|
|
17
|
-
//
|
|
18
|
-
|
|
19
|
-
//
|
|
20
|
-
|
|
7
|
+
// const me = sdk.ethos.me();
|
|
8
|
+
// me.zone("public").addSection({ title, body });
|
|
9
|
+
// me.zone("circle").deleteSection(id);
|
|
10
|
+
// await me.publish(); // 1 edition
|
|
11
|
+
//
|
|
12
|
+
// Mutations stage in memory. `sections()` / `pendingChanges()` /
|
|
13
|
+
// `publish()` are the points where the snapshot is fetched (memoized
|
|
14
|
+
// per zone) and the staged changes are applied on top.
|
|
15
|
+
//
|
|
16
|
+
// Three actor modes:
|
|
17
|
+
// - owner — sdk.ethos.me() returns this; full access to public
|
|
18
|
+
// + private zones using OwnerSigners
|
|
19
|
+
// - delegate — sdk.ethos.of(did) when a mandate matches this
|
|
20
|
+
// subject; capability bounded by mandate scopes
|
|
21
|
+
// - anonymous — sdk.ethos.of(did) when no mandate matches; public
|
|
22
|
+
// zone read-only, every write rejected
|
|
23
|
+
//
|
|
24
|
+
// The namespace stays thin: it builds an EthosClient with the right
|
|
25
|
+
// actor and forwards all real work into protocol-client's
|
|
26
|
+
// `loadEditSnapshot` / `publishZoneEdit` / `publishPublicZoneAsDelegate`
|
|
27
|
+
// / `publishPrivateZoneAsDelegate`.
|
|
28
|
+
import { addSectionToList, AithosRpcError, browserIdentityFromStored, buildSignedEnvelope, buildSignedFirstEditionFromSections, deleteSectionFromList, loadEditSnapshot, modifySectionInList, publishPrivateZoneAsDelegate, publishPublicZoneAsDelegate, publishZoneEdit, signedDidDocument, writeEndpoint, } from "@aithos/protocol-client";
|
|
29
|
+
import { delegateKeyPair } from "./internal/protocol-client-bridge.js";
|
|
30
|
+
import { AithosSDKError } from "./types.js";
|
|
31
|
+
export const ZONE_NAMES = ["public", "circle", "self"];
|
|
32
|
+
/* -------------------------------------------------------------------------- */
|
|
33
|
+
/* EthosClient — per-subject working buffer */
|
|
34
|
+
/* -------------------------------------------------------------------------- */
|
|
35
|
+
export class EthosClient {
|
|
36
|
+
subjectDid;
|
|
37
|
+
mode;
|
|
38
|
+
#actor;
|
|
39
|
+
#snapshots = new Map();
|
|
40
|
+
#mutations = [];
|
|
41
|
+
/**
|
|
42
|
+
* Set to `true` when the server reports that no edition has been
|
|
43
|
+
* published yet for this subject (JSON-RPC code -32020 with the message
|
|
44
|
+
* `not found: edition for <did>`). Toggled lazily on the first read /
|
|
45
|
+
* publish attempt and cleared again after a successful first-edition
|
|
46
|
+
* publish so subsequent operations take the regular `publishZoneEdit`
|
|
47
|
+
* path.
|
|
48
|
+
*/
|
|
49
|
+
#ethosHasNoEditionYet = false;
|
|
50
|
+
constructor(actor) {
|
|
51
|
+
this.#actor = actor;
|
|
52
|
+
this.subjectDid = actor.subjectDid;
|
|
53
|
+
this.mode = actor.kind;
|
|
54
|
+
}
|
|
55
|
+
/** Return the per-zone proxy. */
|
|
56
|
+
zone(name) {
|
|
57
|
+
if (!ZONE_NAMES.includes(name)) {
|
|
58
|
+
throw new AithosSDKError("ethos_invalid_zone", `unknown zone "${String(name)}"`);
|
|
59
|
+
}
|
|
60
|
+
return new EthosZone(this, name);
|
|
61
|
+
}
|
|
62
|
+
hasPendingChanges() {
|
|
63
|
+
return this.#mutations.length > 0;
|
|
64
|
+
}
|
|
65
|
+
pendingChanges() {
|
|
66
|
+
return this.#mutations.slice();
|
|
67
|
+
}
|
|
68
|
+
discard() {
|
|
69
|
+
this.#mutations = [];
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Build and publish a new edition with all staged mutations applied.
|
|
73
|
+
* Throws if there's nothing staged. After a successful publish, the
|
|
74
|
+
* mutation buffer is cleared and any cached snapshot is invalidated
|
|
75
|
+
* so the next read picks up the fresh edition.
|
|
76
|
+
*/
|
|
77
|
+
async publish() {
|
|
78
|
+
if (this.#actor.kind === "anonymous") {
|
|
79
|
+
throw new AithosSDKError("ethos_anonymous_cannot_publish", "anonymous reader has no signing capability");
|
|
80
|
+
}
|
|
81
|
+
if (this.#mutations.length === 0) {
|
|
82
|
+
throw new AithosSDKError("ethos_nothing_to_publish", "no staged mutations; call addSection / updateSection / deleteSection first");
|
|
83
|
+
}
|
|
84
|
+
// Compute effective sections per zone, but only for zones that
|
|
85
|
+
// actually have staged mutations. Zones without staged changes
|
|
86
|
+
// roll forward by reusing protocol-client's existing bytes.
|
|
87
|
+
const touched = new Set(this.#mutations.map((m) => m.zone));
|
|
88
|
+
// Owner-side path is the most common: owner can write any zone they
|
|
89
|
+
// have staged mutations for. Build everything we need then call
|
|
90
|
+
// publishZoneEdit once.
|
|
91
|
+
if (this.#actor.kind === "owner") {
|
|
92
|
+
// First-edition path: no prior edition exists. Detected when the
|
|
93
|
+
// tolerant snapshot wrapper returned null on a previous read OR
|
|
94
|
+
// when this is the first network touch and the server responds
|
|
95
|
+
// with "not found: edition". Skip the snapshot fetch entirely and
|
|
96
|
+
// build a height=1 manifest from the staged mutations.
|
|
97
|
+
const snap = await this.#tryEnsureSnapshotOwner();
|
|
98
|
+
if (snap === null) {
|
|
99
|
+
return this.#publishFirstEditionOwner();
|
|
100
|
+
}
|
|
101
|
+
const newPublic = touched.has("public")
|
|
102
|
+
? this.#applyMutations("public", snap.publicSections)
|
|
103
|
+
: undefined;
|
|
104
|
+
const newCircle = touched.has("circle")
|
|
105
|
+
? this.#applyMutations("circle", snap.circleSections ?? [])
|
|
106
|
+
: undefined;
|
|
107
|
+
const newSelf = touched.has("self")
|
|
108
|
+
? this.#applyMutations("self", snap.selfSections ?? [])
|
|
109
|
+
: undefined;
|
|
110
|
+
const identity = this.#actor.signers._unsafeStoredIdentity();
|
|
111
|
+
try {
|
|
112
|
+
const result = await publishZoneEdit({
|
|
113
|
+
identity,
|
|
114
|
+
snapshot: snap,
|
|
115
|
+
// protocol-client's publishZoneEdit always wants newPublic; if we
|
|
116
|
+
// didn't touch public we re-publish the existing public sections.
|
|
117
|
+
newPublicSections: newPublic ?? snap.publicSections,
|
|
118
|
+
...(newCircle ? { newCircleSections: newCircle } : {}),
|
|
119
|
+
...(newSelf ? { newSelfSections: newSelf } : {}),
|
|
120
|
+
});
|
|
121
|
+
this.#afterPublish();
|
|
122
|
+
return projectPublishResult(result.manifest, this.subjectDid, [
|
|
123
|
+
...touched,
|
|
124
|
+
]);
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
// identity holds raw seeds via reference; not our copy to zeroize
|
|
128
|
+
// (the underlying seeds live in the OwnerSigners' private fields).
|
|
129
|
+
// Drop our reference to the temporary StoredIdentity object.
|
|
130
|
+
void identity;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// Delegate path. Per-zone scope check + per-zone protocol-client
|
|
134
|
+
// function. For a multi-zone delegate publish we'd need to call
|
|
135
|
+
// multiple endpoints; we iterate zones, calling the right primitive.
|
|
136
|
+
if (this.#actor.kind === "delegate") {
|
|
137
|
+
const actor = this.#actor.actor;
|
|
138
|
+
const stored = delegateActorToStored(actor);
|
|
139
|
+
// Validate scopes upfront.
|
|
140
|
+
for (const zone of touched) {
|
|
141
|
+
const required = `ethos.write.${zone}`;
|
|
142
|
+
const scopes = actor.mandate["scopes"] ?? [];
|
|
143
|
+
if (!Array.isArray(scopes) || !scopes.includes(required)) {
|
|
144
|
+
throw new AithosSDKError("ethos_delegate_scope_missing", `delegate mandate does not grant ${required}`, { data: { mandateId: actor.mandateId, scopes } });
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const snap = await this.#ensureSnapshotDelegate();
|
|
148
|
+
// Public-zone update — use publishPublicZoneAsDelegate.
|
|
149
|
+
if (touched.has("public")) {
|
|
150
|
+
const newPublic = this.#applyMutations("public", snap.publicSections);
|
|
151
|
+
await publishPublicZoneAsDelegate({
|
|
152
|
+
delegate: stored,
|
|
153
|
+
snapshot: snap,
|
|
154
|
+
newPublicSections: newPublic,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
// Private zones — one publishPrivateZoneAsDelegate call each.
|
|
158
|
+
for (const zone of ["circle", "self"]) {
|
|
159
|
+
if (!touched.has(zone))
|
|
160
|
+
continue;
|
|
161
|
+
// CRITICAL — refuse to publish a private zone the delegate
|
|
162
|
+
// can't decrypt. publishPrivateZoneAsDelegate treats
|
|
163
|
+
// `newSections` as the COMPLETE new content of the zone (the
|
|
164
|
+
// server can't merge encrypted bytes it can't read), so if we
|
|
165
|
+
// forwarded `[]` here the new edition would silently wipe
|
|
166
|
+
// every section the owner had. This data-loss landed in alpha.9
|
|
167
|
+
// — alpha.10 turns it into a clean error so the caller knows
|
|
168
|
+
// the mandate isn't usable until the owner has bootstrapped a
|
|
169
|
+
// wrap for this delegate (a single owner-side publish on the
|
|
170
|
+
// zone after mint is enough).
|
|
171
|
+
if (snap.zoneDecryptErrors?.[zone]) {
|
|
172
|
+
throw new AithosSDKError("ethos_delegate_cannot_overwrite_unreadable", `delegate cannot publish to "${zone}": the existing edition is not readable for this delegate (${snap.zoneDecryptErrors[zone]}). Ask the owner to publish ${zone} once so this delegate's wrap is included; only then can the delegate write — otherwise the new edition would replace content this delegate cannot see.`, {
|
|
173
|
+
data: {
|
|
174
|
+
zone,
|
|
175
|
+
mandateId: actor.mandateId,
|
|
176
|
+
decryptError: snap.zoneDecryptErrors[zone],
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
const base = (zone === "circle"
|
|
181
|
+
? snap.circleSections
|
|
182
|
+
: snap.selfSections) ?? [];
|
|
183
|
+
const newSections = this.#applyMutations(zone, base);
|
|
184
|
+
await publishPrivateZoneAsDelegate({
|
|
185
|
+
delegate: stored,
|
|
186
|
+
snapshot: snap,
|
|
187
|
+
zone,
|
|
188
|
+
newSections,
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
this.#afterPublish();
|
|
192
|
+
// We don't have a single "manifest" to project from since each call
|
|
193
|
+
// returned its own. Use the last touched call's snapshot as the
|
|
194
|
+
// base for height — fetch fresh on next read anyway.
|
|
195
|
+
return {
|
|
196
|
+
editionHeight: snap.manifest.edition.height + 1,
|
|
197
|
+
manifestHash: "", // not surfaced by protocol-client today on the delegate path
|
|
198
|
+
subjectDid: this.subjectDid,
|
|
199
|
+
zonesPublished: [...touched],
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
// Unreachable — anonymous case returned earlier.
|
|
203
|
+
throw new AithosSDKError("ethos_invalid_actor", "unsupported actor for publish()");
|
|
204
|
+
}
|
|
205
|
+
/* ------------------------------------------------------------------------ */
|
|
206
|
+
/* Bootstrap — first-edition init for fresh Ethos */
|
|
207
|
+
/* ------------------------------------------------------------------------ */
|
|
208
|
+
/**
|
|
209
|
+
* Idempotently ensure the subject's Ethos has at least one published
|
|
210
|
+
* edition. Required because a **delegate** cannot bootstrap a first
|
|
211
|
+
* edition (the first edition's manifest is signed with the owner's
|
|
212
|
+
* public-sphere key, which delegates do not have). Without an initial
|
|
213
|
+
* owner-published edition, subsequent delegate writes via
|
|
214
|
+
* {@link publish} fail with `not found: edition for did:…`.
|
|
215
|
+
*
|
|
216
|
+
* Semantics:
|
|
217
|
+
* - If an edition already exists (owner OR delegate OR anonymous mode),
|
|
218
|
+
* this is a NO-OP and returns `{ alreadyInitialized: true }`.
|
|
219
|
+
* - If no edition exists AND the actor is the owner, this stages and
|
|
220
|
+
* publishes a height=1 edition containing a single sentinel section
|
|
221
|
+
* `aithos-init` in the `public` zone, then returns
|
|
222
|
+
* `{ alreadyInitialized: false, editionHeight: 1 }`. Any previously
|
|
223
|
+
* staged mutations on this client are preserved and NOT auto-flushed.
|
|
224
|
+
* - If no edition exists AND the actor is NOT the owner (delegate or
|
|
225
|
+
* anonymous), throws `ethos_bootstrap_not_owner` — only the owner
|
|
226
|
+
* can sign a first edition.
|
|
227
|
+
*
|
|
228
|
+
* Call site: typically the owner's dashboard, right after sign-in and
|
|
229
|
+
* before any delegate-mode write (e.g. before triggering a backend
|
|
230
|
+
* worker that holds a mandate). Idempotent ⇒ safe to call on every
|
|
231
|
+
* mount.
|
|
232
|
+
*
|
|
233
|
+
* Implementation note: this routes through {@link #publishFirstEditionOwner}
|
|
234
|
+
* which the SDK already uses internally when {@link publish} detects a
|
|
235
|
+
* fresh Ethos with staged owner mutations. ensureInitialized() exposes
|
|
236
|
+
* the same code path as an explicit primitive, so the caller doesn't
|
|
237
|
+
* need to stage a mutation just to trigger first-edition logic.
|
|
238
|
+
*/
|
|
239
|
+
async ensureInitialized() {
|
|
240
|
+
// Read attempt — tolerant (returns null if no edition exists). Routes
|
|
241
|
+
// through the right snapshot method based on actor kind so the
|
|
242
|
+
// detection is correct in every mode.
|
|
243
|
+
let snap;
|
|
244
|
+
if (this.#actor.kind === "anonymous") {
|
|
245
|
+
snap = await this.#tryEnsureSnapshotAnonymous();
|
|
246
|
+
}
|
|
247
|
+
else if (this.#actor.kind === "owner") {
|
|
248
|
+
snap = await this.#tryEnsureSnapshotOwner();
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
snap = await this.#tryEnsureSnapshotDelegate();
|
|
252
|
+
}
|
|
253
|
+
if (snap !== null) {
|
|
254
|
+
return { alreadyInitialized: true };
|
|
255
|
+
}
|
|
256
|
+
// Bootstrap path — owner only.
|
|
257
|
+
if (this.#actor.kind !== "owner") {
|
|
258
|
+
throw new AithosSDKError("ethos_bootstrap_not_owner", `subject ${this.subjectDid} has no published edition yet, and only the owner can sign a first edition. Have the owner call ensureInitialized() once (typically from their dashboard at sign-in time).`, { data: { subjectDid: this.subjectDid, actorKind: this.#actor.kind } });
|
|
259
|
+
}
|
|
260
|
+
// Stage the sentinel init section. We don't disturb any pre-existing
|
|
261
|
+
// staged mutations — they'll be picked up by the same first-edition
|
|
262
|
+
// publish that runs underneath.
|
|
263
|
+
const prevMutations = this.#mutations.slice();
|
|
264
|
+
this.#mutations.push({
|
|
265
|
+
kind: "add",
|
|
266
|
+
zone: "public",
|
|
267
|
+
section: {
|
|
268
|
+
id: "sec_" + randomHex(12),
|
|
269
|
+
title: "aithos-init",
|
|
270
|
+
body: "Ethos initialized.\n\n" +
|
|
271
|
+
"This section is a sentinel created by `EthosClient.ensureInitialized()` " +
|
|
272
|
+
"to materialise the subject's first edition. It is safe to delete or " +
|
|
273
|
+
"edit later — its only purpose was to satisfy the protocol's " +
|
|
274
|
+
"requirement that an edition contain at least one section.",
|
|
275
|
+
gamma_ref: "gamma_none_" + randomHex(24),
|
|
276
|
+
},
|
|
277
|
+
});
|
|
278
|
+
try {
|
|
279
|
+
const result = await this.#publishFirstEditionOwner();
|
|
280
|
+
// Re-stage the caller's prior mutations (if any) so a subsequent
|
|
281
|
+
// publish() in the same call sequence picks them up. The init
|
|
282
|
+
// section we added has been consumed by the publish above.
|
|
283
|
+
this.#mutations = prevMutations;
|
|
284
|
+
return {
|
|
285
|
+
alreadyInitialized: false,
|
|
286
|
+
editionHeight: result.editionHeight,
|
|
287
|
+
manifestHash: result.manifestHash,
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
catch (e) {
|
|
291
|
+
// On failure, restore the caller's mutations as they were before
|
|
292
|
+
// we touched the buffer so the client is left in a consistent state.
|
|
293
|
+
this.#mutations = prevMutations;
|
|
294
|
+
throw e;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
/* ------------------------------------------------------------------------ */
|
|
298
|
+
/* Internal — snapshot management */
|
|
299
|
+
/* ------------------------------------------------------------------------ */
|
|
300
|
+
async _readZone(zone) {
|
|
301
|
+
if (this.#actor.kind === "anonymous") {
|
|
302
|
+
if (zone !== "public") {
|
|
303
|
+
throw new AithosSDKError("ethos_anonymous_private_zone", `anonymous reader cannot access the "${zone}" zone`);
|
|
304
|
+
}
|
|
305
|
+
const snap = await this.#tryEnsureSnapshotAnonymous();
|
|
306
|
+
// No edition yet → no base sections; only staged mutations contribute.
|
|
307
|
+
const base = snap === null ? [] : snap.publicSections;
|
|
308
|
+
return this.#applyMutations(zone, base);
|
|
309
|
+
}
|
|
310
|
+
if (this.#actor.kind === "owner") {
|
|
311
|
+
const snap = await this.#tryEnsureSnapshotOwner();
|
|
312
|
+
if (snap === null) {
|
|
313
|
+
// No edition yet for this owner — base sections are empty in every
|
|
314
|
+
// zone. Staged mutations apply on top of an empty list.
|
|
315
|
+
return this.#applyMutations(zone, []);
|
|
316
|
+
}
|
|
317
|
+
const base = baseSectionsFromSnapshot(snap, zone);
|
|
318
|
+
// Surface decryption errors clearly (if reading a private zone we
|
|
319
|
+
// can't unwrap, the snapshot has it in zoneDecryptErrors).
|
|
320
|
+
if (zone !== "public" && snap.zoneDecryptErrors?.[zone]) {
|
|
321
|
+
throw new AithosSDKError("ethos_zone_unreadable", `cannot read ${zone}: ${snap.zoneDecryptErrors[zone]}`);
|
|
322
|
+
}
|
|
323
|
+
return this.#applyMutations(zone, base);
|
|
324
|
+
}
|
|
325
|
+
// Delegate
|
|
326
|
+
const snap = await this.#tryEnsureSnapshotDelegate();
|
|
327
|
+
if (snap === null) {
|
|
328
|
+
return this.#applyMutations(zone, []);
|
|
329
|
+
}
|
|
330
|
+
const base = baseSectionsFromSnapshot(snap, zone);
|
|
331
|
+
if (zone !== "public" && snap.zoneDecryptErrors?.[zone]) {
|
|
332
|
+
throw new AithosSDKError("ethos_zone_unreadable", `cannot read ${zone}: ${snap.zoneDecryptErrors[zone]}`);
|
|
333
|
+
}
|
|
334
|
+
return this.#applyMutations(zone, base);
|
|
335
|
+
}
|
|
336
|
+
_stageAdd(zone, input) {
|
|
337
|
+
if (this.#actor.kind === "anonymous") {
|
|
338
|
+
throw new AithosSDKError("ethos_anonymous_write", "anonymous reader cannot stage mutations");
|
|
339
|
+
}
|
|
340
|
+
const section = {
|
|
341
|
+
id: "sec_" + randomHex(12),
|
|
342
|
+
title: input.title,
|
|
343
|
+
body: input.body,
|
|
344
|
+
gamma_ref: "gamma_none_" + randomHex(24),
|
|
345
|
+
...(input.tags && input.tags.length > 0 ? { tags: input.tags } : {}),
|
|
346
|
+
};
|
|
347
|
+
this.#mutations.push({ kind: "add", zone, section });
|
|
348
|
+
}
|
|
349
|
+
_stageUpdate(zone, sectionId, patch) {
|
|
350
|
+
if (this.#actor.kind === "anonymous") {
|
|
351
|
+
throw new AithosSDKError("ethos_anonymous_write", "anonymous reader cannot stage mutations");
|
|
352
|
+
}
|
|
353
|
+
this.#mutations.push({ kind: "update", zone, sectionId, patch });
|
|
354
|
+
}
|
|
355
|
+
_stageDelete(zone, sectionId) {
|
|
356
|
+
if (this.#actor.kind === "anonymous") {
|
|
357
|
+
throw new AithosSDKError("ethos_anonymous_write", "anonymous reader cannot stage mutations");
|
|
358
|
+
}
|
|
359
|
+
this.#mutations.push({ kind: "delete", zone, sectionId });
|
|
360
|
+
}
|
|
361
|
+
/** Apply staged mutations for a zone on top of base sections. */
|
|
362
|
+
#applyMutations(zone, base) {
|
|
363
|
+
let acc = base;
|
|
364
|
+
for (const m of this.#mutations) {
|
|
365
|
+
if (m.zone !== zone)
|
|
366
|
+
continue;
|
|
367
|
+
switch (m.kind) {
|
|
368
|
+
case "add":
|
|
369
|
+
acc = addSectionToList(acc, {
|
|
370
|
+
title: m.section.title,
|
|
371
|
+
body: m.section.body,
|
|
372
|
+
...(m.section.tags ? { tags: m.section.tags } : {}),
|
|
373
|
+
});
|
|
374
|
+
break;
|
|
375
|
+
case "update":
|
|
376
|
+
acc = modifySectionInList(acc, {
|
|
377
|
+
sectionId: m.sectionId,
|
|
378
|
+
...(m.patch.title !== undefined ? { title: m.patch.title } : {}),
|
|
379
|
+
...(m.patch.body !== undefined ? { body: m.patch.body } : {}),
|
|
380
|
+
...(m.patch.tags !== undefined ? { tags: m.patch.tags } : {}),
|
|
381
|
+
});
|
|
382
|
+
break;
|
|
383
|
+
case "delete":
|
|
384
|
+
acc = deleteSectionFromList(acc, m.sectionId);
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return acc;
|
|
389
|
+
}
|
|
390
|
+
async #ensureSnapshotOwner() {
|
|
391
|
+
const cached = this.#snapshots.get("public");
|
|
392
|
+
if (cached)
|
|
393
|
+
return cached;
|
|
394
|
+
if (this.#actor.kind !== "owner") {
|
|
395
|
+
throw new AithosSDKError("ethos_invalid_actor", "expected owner actor");
|
|
396
|
+
}
|
|
397
|
+
const identity = this.#actor.signers._unsafeStoredIdentity();
|
|
398
|
+
const snap = await loadEditSnapshot(this.subjectDid, identity);
|
|
399
|
+
this.#cacheSnapshotAllZones(snap);
|
|
400
|
+
return snap;
|
|
401
|
+
}
|
|
402
|
+
async #ensureSnapshotDelegate() {
|
|
403
|
+
const cached = this.#snapshots.get("public");
|
|
404
|
+
if (cached)
|
|
405
|
+
return cached;
|
|
406
|
+
if (this.#actor.kind !== "delegate") {
|
|
407
|
+
throw new AithosSDKError("ethos_invalid_actor", "expected delegate actor");
|
|
408
|
+
}
|
|
409
|
+
const stored = delegateActorToStored(this.#actor.actor);
|
|
410
|
+
const snap = await loadEditSnapshot(this.subjectDid, undefined, stored);
|
|
411
|
+
this.#cacheSnapshotAllZones(snap);
|
|
412
|
+
return snap;
|
|
413
|
+
}
|
|
414
|
+
async #ensureSnapshotAnonymous() {
|
|
415
|
+
const cached = this.#snapshots.get("public");
|
|
416
|
+
if (cached)
|
|
417
|
+
return cached;
|
|
418
|
+
const snap = await loadEditSnapshot(this.subjectDid);
|
|
419
|
+
this.#cacheSnapshotAllZones(snap);
|
|
420
|
+
return snap;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Wrap {@link #ensureSnapshotOwner} so the "no edition published yet"
|
|
424
|
+
* server response (-32020 with message `not found: edition for <did>`)
|
|
425
|
+
* is converted into `null`. Once converted, {@link #ethosHasNoEditionYet}
|
|
426
|
+
* is set to short-circuit subsequent reads without re-hitting the network.
|
|
427
|
+
*
|
|
428
|
+
* Returns `null` to mean "subject has an identity but no editions yet —
|
|
429
|
+
* treat all zones as empty". Any other error is re-thrown.
|
|
430
|
+
*/
|
|
431
|
+
async #tryEnsureSnapshotOwner() {
|
|
432
|
+
if (this.#ethosHasNoEditionYet)
|
|
433
|
+
return null;
|
|
434
|
+
try {
|
|
435
|
+
return await this.#ensureSnapshotOwner();
|
|
436
|
+
}
|
|
437
|
+
catch (e) {
|
|
438
|
+
if (isNoEditionYetError(e)) {
|
|
439
|
+
this.#ethosHasNoEditionYet = true;
|
|
440
|
+
return null;
|
|
441
|
+
}
|
|
442
|
+
throw e;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
async #tryEnsureSnapshotDelegate() {
|
|
446
|
+
if (this.#ethosHasNoEditionYet)
|
|
447
|
+
return null;
|
|
448
|
+
try {
|
|
449
|
+
return await this.#ensureSnapshotDelegate();
|
|
450
|
+
}
|
|
451
|
+
catch (e) {
|
|
452
|
+
if (isNoEditionYetError(e)) {
|
|
453
|
+
this.#ethosHasNoEditionYet = true;
|
|
454
|
+
return null;
|
|
455
|
+
}
|
|
456
|
+
throw e;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
async #tryEnsureSnapshotAnonymous() {
|
|
460
|
+
if (this.#ethosHasNoEditionYet)
|
|
461
|
+
return null;
|
|
462
|
+
try {
|
|
463
|
+
return await this.#ensureSnapshotAnonymous();
|
|
464
|
+
}
|
|
465
|
+
catch (e) {
|
|
466
|
+
if (isNoEditionYetError(e)) {
|
|
467
|
+
this.#ethosHasNoEditionYet = true;
|
|
468
|
+
return null;
|
|
469
|
+
}
|
|
470
|
+
throw e;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
#cacheSnapshotAllZones(snap) {
|
|
474
|
+
for (const z of ZONE_NAMES)
|
|
475
|
+
this.#snapshots.set(z, snap);
|
|
476
|
+
}
|
|
477
|
+
#afterPublish() {
|
|
478
|
+
this.#mutations = [];
|
|
479
|
+
this.#snapshots.clear();
|
|
480
|
+
}
|
|
481
|
+
/* ------------------------------------------------------------------------ */
|
|
482
|
+
/* First-edition publish (owner) */
|
|
483
|
+
/* ------------------------------------------------------------------------ */
|
|
484
|
+
/**
|
|
485
|
+
* Publish height=1 for an owner whose Ethos identity exists on
|
|
486
|
+
* `api.aithos.be` (provisioned by `auth.signUp()` in alpha.6+) but who
|
|
487
|
+
* has no editions yet. Builds the manifest from the staged ADD
|
|
488
|
+
* mutations on the public zone and POSTs `aithos.publish_ethos_edition`.
|
|
489
|
+
*
|
|
490
|
+
* Limitations of the alpha.7 cut:
|
|
491
|
+
* - Public zone only. Staged mutations on circle/self are rejected
|
|
492
|
+
* with `ethos_first_edition_public_only` — those zones can be
|
|
493
|
+
* populated in subsequent editions via the regular
|
|
494
|
+
* `publishZoneEdit` path once the public zone has been seeded.
|
|
495
|
+
* - First-edition publishes don't accept update/delete mutations
|
|
496
|
+
* (there's nothing to update or delete yet) — those are rejected
|
|
497
|
+
* with `ethos_first_edition_invalid_op`.
|
|
498
|
+
*/
|
|
499
|
+
async #publishFirstEditionOwner() {
|
|
500
|
+
if (this.#actor.kind !== "owner") {
|
|
501
|
+
// Defensive — caller already checked this branch.
|
|
502
|
+
throw new AithosSDKError("ethos_invalid_actor", "expected owner actor");
|
|
503
|
+
}
|
|
504
|
+
// Validate the staged operation set. First edition = ADDs on public
|
|
505
|
+
// zone only.
|
|
506
|
+
const publicAdds = [];
|
|
507
|
+
for (const m of this.#mutations) {
|
|
508
|
+
if (m.kind !== "add") {
|
|
509
|
+
throw new AithosSDKError("ethos_first_edition_invalid_op", `first edition: cannot ${m.kind} a section before any edition exists; only addSection is supported on a fresh Ethos`, { data: { mutation: m } });
|
|
510
|
+
}
|
|
511
|
+
if (m.zone !== "public") {
|
|
512
|
+
throw new AithosSDKError("ethos_first_edition_public_only", `first edition: only the "public" zone is supported on a fresh Ethos; "${m.zone}" sections can be added after the first publish`, { data: { zone: m.zone } });
|
|
513
|
+
}
|
|
514
|
+
publicAdds.push({ section: m.section });
|
|
515
|
+
}
|
|
516
|
+
if (publicAdds.length === 0) {
|
|
517
|
+
// Should never reach here — publish() short-circuits on empty
|
|
518
|
+
// mutations. Belt-and-braces in case the contract drifts.
|
|
519
|
+
throw new AithosSDKError("ethos_first_edition_empty", "first edition: stage at least one public-zone section before publishing");
|
|
520
|
+
}
|
|
521
|
+
const identity = this.#actor.signers._unsafeStoredIdentity();
|
|
522
|
+
const browserId = browserIdentityFromStored(identity);
|
|
523
|
+
const signedDoc = signedDidDocument(browserId);
|
|
524
|
+
const built = buildSignedFirstEditionFromSections({
|
|
525
|
+
identity: browserId,
|
|
526
|
+
signedDidDoc: signedDoc,
|
|
527
|
+
publicSections: publicAdds.map((a) => a.section),
|
|
528
|
+
});
|
|
529
|
+
const url = writeEndpoint();
|
|
530
|
+
const params = {
|
|
531
|
+
manifest: built.manifest,
|
|
532
|
+
zones: {
|
|
533
|
+
public: { bytes_base64: bytesToBase64Padded(built.publicMarkdownBytes) },
|
|
534
|
+
},
|
|
535
|
+
};
|
|
536
|
+
const envelope = buildSignedEnvelope({
|
|
537
|
+
iss: browserId.did,
|
|
538
|
+
aud: url,
|
|
539
|
+
method: "aithos.publish_ethos_edition",
|
|
540
|
+
verificationMethod: `${browserId.did}#public`,
|
|
541
|
+
params,
|
|
542
|
+
signer: browserId.public,
|
|
543
|
+
});
|
|
544
|
+
const body = JSON.stringify({
|
|
545
|
+
jsonrpc: "2.0",
|
|
546
|
+
id: "publish_ethos_edition",
|
|
547
|
+
method: "aithos.publish_ethos_edition",
|
|
548
|
+
params: { ...params, _envelope: envelope },
|
|
549
|
+
});
|
|
550
|
+
let res;
|
|
551
|
+
try {
|
|
552
|
+
res = await fetch(url, {
|
|
553
|
+
method: "POST",
|
|
554
|
+
headers: { "content-type": "application/json" },
|
|
555
|
+
body,
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
catch (e) {
|
|
559
|
+
throw new AithosSDKError("ethos_publish_network", `publish_ethos_edition (first edition): network error: ${e.message ?? "unknown"}`);
|
|
560
|
+
}
|
|
561
|
+
let json;
|
|
562
|
+
try {
|
|
563
|
+
json = (await res.json());
|
|
564
|
+
}
|
|
565
|
+
catch {
|
|
566
|
+
throw new AithosSDKError("ethos_publish_invalid_response", `publish_ethos_edition (first edition): server returned non-JSON (HTTP ${res.status})`);
|
|
567
|
+
}
|
|
568
|
+
if (json.error) {
|
|
569
|
+
throw new AithosSDKError("ethos_first_edition_rejected", `publish_ethos_edition (first edition) rejected: ${json.error.message}`, {
|
|
570
|
+
status: res.status,
|
|
571
|
+
data: { rpc_code: json.error.code, ...(json.error.data ?? {}) },
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
// Success: clear the no-edition flag so subsequent reads/publishes
|
|
575
|
+
// take the regular next-edition path.
|
|
576
|
+
this.#ethosHasNoEditionYet = false;
|
|
577
|
+
this.#afterPublish();
|
|
578
|
+
return {
|
|
579
|
+
editionHeight: 1,
|
|
580
|
+
manifestHash: "", // protocol-client surfaces this on later editions; not on first
|
|
581
|
+
subjectDid: browserId.did,
|
|
582
|
+
zonesPublished: ["public"],
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
/* -------------------------------------------------------------------------- */
|
|
587
|
+
/* EthosZone — per-zone proxy */
|
|
588
|
+
/* -------------------------------------------------------------------------- */
|
|
589
|
+
export class EthosZone {
|
|
590
|
+
#parent;
|
|
591
|
+
#name;
|
|
592
|
+
constructor(parent, name) {
|
|
593
|
+
this.#parent = parent;
|
|
594
|
+
this.#name = name;
|
|
595
|
+
}
|
|
596
|
+
get name() {
|
|
597
|
+
return this.#name;
|
|
598
|
+
}
|
|
599
|
+
/** Effective sections (persisted + staged mutations applied). */
|
|
600
|
+
async sections() {
|
|
601
|
+
return this.#parent._readZone(this.#name);
|
|
602
|
+
}
|
|
603
|
+
addSection(input) {
|
|
604
|
+
this.#parent._stageAdd(this.#name, input);
|
|
605
|
+
}
|
|
606
|
+
updateSection(sectionId, patch) {
|
|
607
|
+
this.#parent._stageUpdate(this.#name, sectionId, patch);
|
|
608
|
+
}
|
|
609
|
+
deleteSection(sectionId) {
|
|
610
|
+
this.#parent._stageDelete(this.#name, sectionId);
|
|
611
|
+
}
|
|
612
|
+
/* ------------------------------------------------------------------------ */
|
|
613
|
+
/* By-title helpers */
|
|
614
|
+
/* */
|
|
615
|
+
/* The Aithos protocol does not (yet) treat section titles as a queryable */
|
|
616
|
+
/* index — only `section.id` is a normative anchor. These three methods */
|
|
617
|
+
/* are an ergonomic SDK-side affordance for the common LLM-driven case */
|
|
618
|
+
/* "act on the section called X". They resolve titles client-side by */
|
|
619
|
+
/* loading the full zone (`sections()`) and exact-match filtering on */
|
|
620
|
+
/* `Section.title`. */
|
|
621
|
+
/* */
|
|
622
|
+
/* When a future revision of `aithos.get_ethos_zone` (or a new */
|
|
623
|
+
/* `aithos.find_sections` primitive) supports server-side title */
|
|
624
|
+
/* filtering, the implementation behind these three methods can swap to */
|
|
625
|
+
/* the server call without changing the public signatures here. The */
|
|
626
|
+
/* contract — async, exact case-sensitive match, plural semantics — is */
|
|
627
|
+
/* intentionally aligned with what such an API would return. */
|
|
628
|
+
/* ------------------------------------------------------------------------ */
|
|
629
|
+
/**
|
|
630
|
+
* Return every section in this zone whose `title` is exactly `title`.
|
|
631
|
+
*
|
|
632
|
+
* Match is exact and case-sensitive. The result is always an array — it
|
|
633
|
+
* may be empty (no match), have one element (the typical case), or have
|
|
634
|
+
* more than one element when the author has happened to publish two
|
|
635
|
+
* sections with the same title. Section titles are not required by the
|
|
636
|
+
* protocol to be unique within a zone.
|
|
637
|
+
*
|
|
638
|
+
* The order of returned sections is the zone's authored order
|
|
639
|
+
* (`sections()` ordering, spec §2.5.2).
|
|
640
|
+
*
|
|
641
|
+
* @param title Section title to look up — exact, case-sensitive.
|
|
642
|
+
*/
|
|
643
|
+
async findSectionsByTitle(title) {
|
|
644
|
+
const all = await this.#parent._readZone(this.#name);
|
|
645
|
+
return all.filter((s) => s.title === title);
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Stage an update for **every** section in this zone whose `title`
|
|
649
|
+
* matches `title` exactly. Returns the list of section IDs that were
|
|
650
|
+
* staged — empty when nothing matched.
|
|
651
|
+
*
|
|
652
|
+
* Apply with `client.publish()` like any other staged mutation. The
|
|
653
|
+
* staged entries are identical to what `updateSection(id, patch)` would
|
|
654
|
+
* produce, one per matched section, so `pendingChanges()` /
|
|
655
|
+
* `discard()` behave normally.
|
|
656
|
+
*
|
|
657
|
+
* Note: this method does NOT throw when there is no match — it returns
|
|
658
|
+
* `[]`. That's intentional: callers driven by an LLM frequently want to
|
|
659
|
+
* upsert (try update, then fall back to add) and shouldn't have to
|
|
660
|
+
* catch.
|
|
661
|
+
*
|
|
662
|
+
* @param title Section title to look up — exact, case-sensitive.
|
|
663
|
+
* @param patch Same patch shape accepted by `updateSection`.
|
|
664
|
+
* @returns Array of `section.id` strings whose updates were staged.
|
|
665
|
+
*/
|
|
666
|
+
async updateSectionsByTitle(title, patch) {
|
|
667
|
+
const matches = await this.findSectionsByTitle(title);
|
|
668
|
+
const ids = [];
|
|
669
|
+
for (const s of matches) {
|
|
670
|
+
this.#parent._stageUpdate(this.#name, s.id, patch);
|
|
671
|
+
ids.push(s.id);
|
|
672
|
+
}
|
|
673
|
+
return ids;
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* Stage a delete for **every** section in this zone whose `title`
|
|
677
|
+
* matches `title` exactly. Returns the list of section IDs that were
|
|
678
|
+
* staged — empty when nothing matched.
|
|
679
|
+
*
|
|
680
|
+
* Same semantics as {@link updateSectionsByTitle}: silent on no-match,
|
|
681
|
+
* apply with `client.publish()`.
|
|
682
|
+
*
|
|
683
|
+
* @param title Section title to look up — exact, case-sensitive.
|
|
684
|
+
* @returns Array of `section.id` strings whose deletes were staged.
|
|
685
|
+
*/
|
|
686
|
+
async deleteSectionsByTitle(title) {
|
|
687
|
+
const matches = await this.findSectionsByTitle(title);
|
|
688
|
+
const ids = [];
|
|
689
|
+
for (const s of matches) {
|
|
690
|
+
this.#parent._stageDelete(this.#name, s.id);
|
|
691
|
+
ids.push(s.id);
|
|
692
|
+
}
|
|
693
|
+
return ids;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
export class EthosNamespace {
|
|
697
|
+
#deps;
|
|
698
|
+
constructor(deps) {
|
|
699
|
+
this.#deps = deps;
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* EthosClient for the currently signed-in owner. Throws if there is
|
|
703
|
+
* no owner — callers should check `auth.canSignAsOwner()` first or
|
|
704
|
+
* surface the error to the user as "please sign in".
|
|
705
|
+
*/
|
|
706
|
+
me() {
|
|
707
|
+
const signers = this.#deps.auth._getOwnerSigners();
|
|
708
|
+
if (!signers || signers.destroyed) {
|
|
709
|
+
throw new AithosSDKError("ethos_no_owner", "no owner signed in; sign in with email/password, Google, or a recovery file first");
|
|
710
|
+
}
|
|
711
|
+
return new EthosClient({
|
|
712
|
+
kind: "owner",
|
|
713
|
+
subjectDid: signers.did,
|
|
714
|
+
signers,
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* EthosClient for an arbitrary subject DID. The mode is resolved at
|
|
719
|
+
* construction time:
|
|
720
|
+
* - if `did` matches the currently signed-in owner → owner mode
|
|
721
|
+
* - else if a mandate held by `auth` covers this subject → delegate mode
|
|
722
|
+
* - else → anonymous read-only mode
|
|
723
|
+
*
|
|
724
|
+
* Async signature so future implementations may do an eager manifest
|
|
725
|
+
* fetch (e.g. to fail fast on unknown DIDs); today resolution is sync
|
|
726
|
+
* and the actual fetch happens on the first `sections()` / `publish()`
|
|
727
|
+
* call.
|
|
728
|
+
*/
|
|
729
|
+
async of(did) {
|
|
730
|
+
const owner = this.#deps.auth._getOwnerSigners();
|
|
731
|
+
if (owner && !owner.destroyed && owner.did === did) {
|
|
732
|
+
return new EthosClient({
|
|
733
|
+
kind: "owner",
|
|
734
|
+
subjectDid: did,
|
|
735
|
+
signers: owner,
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
const delegate = this.#deps.auth._findDelegateForSubject(did);
|
|
739
|
+
if (delegate && !delegate.destroyed) {
|
|
740
|
+
return new EthosClient({
|
|
741
|
+
kind: "delegate",
|
|
742
|
+
subjectDid: did,
|
|
743
|
+
actor: delegate,
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
return new EthosClient({ kind: "anonymous", subjectDid: did });
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
/* -------------------------------------------------------------------------- */
|
|
750
|
+
/* Helpers */
|
|
751
|
+
/* -------------------------------------------------------------------------- */
|
|
752
|
+
function baseSectionsFromSnapshot(snap, zone) {
|
|
753
|
+
switch (zone) {
|
|
754
|
+
case "public":
|
|
755
|
+
return snap.publicSections;
|
|
756
|
+
case "circle":
|
|
757
|
+
return snap.circleSections ?? [];
|
|
758
|
+
case "self":
|
|
759
|
+
return snap.selfSections ?? [];
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
function delegateActorToStored(a) {
|
|
763
|
+
const kp = delegateKeyPair(a);
|
|
764
|
+
let hex = "";
|
|
765
|
+
for (let i = 0; i < kp.seed.length; i++) {
|
|
766
|
+
hex += kp.seed[i].toString(16).padStart(2, "0");
|
|
767
|
+
}
|
|
768
|
+
return {
|
|
769
|
+
version: "0.1.0",
|
|
770
|
+
subjectDid: a.subjectDid,
|
|
771
|
+
mandate: a.mandate,
|
|
772
|
+
mandateId: a.mandateId,
|
|
773
|
+
granteeId: a.granteeId,
|
|
774
|
+
granteePubkeyMultibase: a.granteePubkeyMultibase,
|
|
775
|
+
delegateSeedHex: hex,
|
|
776
|
+
importedAt: new Date().toISOString(),
|
|
777
|
+
};
|
|
778
|
+
}
|
|
779
|
+
function projectPublishResult(manifest, subjectDid, zones) {
|
|
780
|
+
return {
|
|
781
|
+
editionHeight: manifest.edition.height,
|
|
782
|
+
manifestHash: manifest.bundle_id ?? "",
|
|
783
|
+
subjectDid,
|
|
784
|
+
zonesPublished: zones,
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* Detect the server-side "no edition published yet" response. Matches the
|
|
789
|
+
* exact error shape emitted by primitives-read's `notFound("edition for
|
|
790
|
+
* <did>")` helper: JSON-RPC code -32020 + message starting with `not
|
|
791
|
+
* found: edition for `. Other -32020 cases (e.g. "not found: manifest
|
|
792
|
+
* <did>@<height>" raised when the index advertises an edition the S3
|
|
793
|
+
* bucket can't serve) deliberately fall through — they're symptoms, not
|
|
794
|
+
* the "fresh subject" case we're trying to swallow.
|
|
795
|
+
*/
|
|
796
|
+
function isNoEditionYetError(e) {
|
|
797
|
+
if (!(e instanceof AithosRpcError))
|
|
798
|
+
return false;
|
|
799
|
+
if (e.code !== -32020)
|
|
800
|
+
return false;
|
|
801
|
+
return typeof e.message === "string"
|
|
802
|
+
&& e.message.startsWith("not found: edition for ");
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* Standard base64 with `=` padding — matches what protocol-client's
|
|
806
|
+
* publishZoneEdit uses for `zones.<zone>.bytes_base64`. The server is
|
|
807
|
+
* tolerant of either padded or unpadded variants per the API contract,
|
|
808
|
+
* but we mirror the existing wire to keep payloads byte-identical for
|
|
809
|
+
* easy diffing in dev tools.
|
|
810
|
+
*/
|
|
811
|
+
function bytesToBase64Padded(bytes) {
|
|
812
|
+
let bin = "";
|
|
813
|
+
for (let i = 0; i < bytes.length; i++)
|
|
814
|
+
bin += String.fromCharCode(bytes[i]);
|
|
815
|
+
return btoa(bin);
|
|
816
|
+
}
|
|
817
|
+
function randomHex(n) {
|
|
818
|
+
const bytes = new Uint8Array(Math.ceil(n / 2));
|
|
819
|
+
crypto.getRandomValues(bytes);
|
|
820
|
+
let hex = "";
|
|
821
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
822
|
+
hex += bytes[i].toString(16).padStart(2, "0");
|
|
823
|
+
}
|
|
824
|
+
return hex.slice(0, n);
|
|
825
|
+
}
|
|
21
826
|
//# sourceMappingURL=ethos.js.map
|