@aithos/sdk 0.1.0-alpha.1 → 0.1.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +45 -0
  2. package/dist/src/auth-api.d.ts +41 -0
  3. package/dist/src/auth-api.js +82 -0
  4. package/dist/src/auth.d.ts +166 -0
  5. package/dist/src/auth.js +730 -0
  6. package/dist/src/compute.d.ts +27 -6
  7. package/dist/src/compute.js +67 -10
  8. package/dist/src/ethos.d.ts +117 -1
  9. package/dist/src/ethos.js +646 -16
  10. package/dist/src/index.d.ts +11 -4
  11. package/dist/src/index.js +31 -5
  12. package/dist/src/internal/delegate-bundle.d.ts +18 -0
  13. package/dist/src/internal/delegate-bundle.js +94 -0
  14. package/dist/src/internal/delegate-state.d.ts +45 -0
  15. package/dist/src/internal/delegate-state.js +120 -0
  16. package/dist/src/internal/owner-signers.d.ts +78 -0
  17. package/dist/src/internal/owner-signers.js +179 -0
  18. package/dist/src/internal/protocol-client-bridge.d.ts +8 -0
  19. package/dist/src/internal/protocol-client-bridge.js +20 -0
  20. package/dist/src/internal/recovery-file.d.ts +29 -0
  21. package/dist/src/internal/recovery-file.js +98 -0
  22. package/dist/src/internal/signer.d.ts +59 -0
  23. package/dist/src/internal/signer.js +86 -0
  24. package/dist/src/key-store.d.ts +128 -0
  25. package/dist/src/key-store.js +244 -0
  26. package/dist/src/mandates.d.ts +163 -1
  27. package/dist/src/mandates.js +286 -8
  28. package/dist/src/sdk.d.ts +36 -3
  29. package/dist/src/sdk.js +28 -22
  30. package/dist/src/session-store.d.ts +58 -0
  31. package/dist/src/session-store.js +158 -0
  32. package/dist/src/wallet.d.ts +42 -2
  33. package/dist/src/wallet.js +89 -14
  34. package/dist/test/auth-j3.test.d.ts +2 -0
  35. package/dist/test/auth-j3.test.js +391 -0
  36. package/dist/test/auth.test.d.ts +2 -0
  37. package/dist/test/auth.test.js +175 -0
  38. package/dist/test/compute-delegate-path.test.d.ts +2 -0
  39. package/dist/test/compute-delegate-path.test.js +183 -0
  40. package/dist/test/compute.test.js +22 -11
  41. package/dist/test/ethos-first-edition.test.d.ts +2 -0
  42. package/dist/test/ethos-first-edition.test.js +248 -0
  43. package/dist/test/ethos.test.d.ts +2 -0
  44. package/dist/test/ethos.test.js +219 -0
  45. package/dist/test/key-store.test.d.ts +2 -0
  46. package/dist/test/key-store.test.js +161 -0
  47. package/dist/test/mandates-compute.test.d.ts +2 -0
  48. package/dist/test/mandates-compute.test.js +256 -0
  49. package/dist/test/mandates.test.d.ts +2 -0
  50. package/dist/test/mandates.test.js +93 -0
  51. package/dist/test/sdk.test.js +70 -30
  52. package/dist/test/signer.test.d.ts +2 -0
  53. package/dist/test/signer.test.js +117 -0
  54. package/dist/test/wallet.test.js +20 -9
  55. package/package.json +4 -3
@@ -0,0 +1,730 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Copyright 2026 Mathieu Colla
3
+ // Aithos auth — sign-up, sign-in (email+password / Google SSO /
4
+ // recovery file), mandate import, sign-out.
5
+ //
6
+ // One stateful object per app. Holds the active {@link AithosSession}
7
+ // (JWT-backed when present), the loaded owner signers in memory (when
8
+ // signed in as an owner), and a registry of active delegate
9
+ // sessions. Persists across reloads via the configured
10
+ // {@link AithosSessionStore} (JWT) and {@link AithosKeyStore} (signing
11
+ // material).
12
+ //
13
+ // Strict mode (per design discussion): the two stores must agree about
14
+ // who's signed in. If sessionStore has a JWT for one DID and keyStore
15
+ // has an owner with a DIFFERENT DID, both are wiped. If sessionStore
16
+ // has a JWT but keyStore has no owner at all, the JWT is wiped (a
17
+ // JWT alone is useless without local signing capability for everything
18
+ // past compute/wallet).
19
+ //
20
+ // JWT-less sessions (recovery / mandate sign-ins) are valid: the
21
+ // keyStore is the source of truth for "is the user signed in", the
22
+ // JWT is auxiliary for compute/wallet.
23
+ import { buildBlobPlaintext, buildSignedEnvelope, createBrowserIdentity, decryptBlob, DEFAULT_KDF, deriveAuthAndEncKeys, encryptBlob, parseBlob, randomNonce, randomSalt, serializeBlob, signedDidDocument, zeroize, } from "@aithos/protocol-client";
24
+ import { loginChallenge, loginVerify, registerAccount, } from "./auth-api.js";
25
+ import { defaultSessionStore, } from "./session-store.js";
26
+ import { defaultKeyStore, } from "./key-store.js";
27
+ import { parseDelegateBundle, readDelegateBundleText, } from "./internal/delegate-bundle.js";
28
+ import { DelegateActor, DelegateRegistry, } from "./internal/delegate-state.js";
29
+ import { OwnerSigners } from "./internal/owner-signers.js";
30
+ import { parseRecoveryFile, readRecoveryFileText, serializeRecoveryFile, } from "./internal/recovery-file.js";
31
+ import { AithosSDKError } from "./types.js";
32
+ /** Default URL of the Aithos auth backend. */
33
+ export const DEFAULT_AUTH_BASE_URL = "https://auth.aithos.be";
34
+ /** Default URL of the Aithos primitives API (publish_identity, publish_ethos_edition, etc.). */
35
+ export const DEFAULT_API_BASE_URL = "https://api.aithos.be";
36
+ /* -------------------------------------------------------------------------- */
37
+ /* AithosAuth */
38
+ /* -------------------------------------------------------------------------- */
39
+ export class AithosAuth {
40
+ authBaseUrl;
41
+ apiBaseUrl;
42
+ #fetchImpl;
43
+ #win;
44
+ #sessionStore;
45
+ #keyStore;
46
+ /** In-memory owner signers — populated after sign-in or `resume`. */
47
+ #ownerSigners = null;
48
+ /** Active delegate registry. */
49
+ #delegates = new DelegateRegistry();
50
+ constructor(config = {}) {
51
+ this.authBaseUrl = trimSlash(config.authBaseUrl ?? DEFAULT_AUTH_BASE_URL);
52
+ this.apiBaseUrl = trimSlash(config.apiBaseUrl ?? DEFAULT_API_BASE_URL);
53
+ this.#fetchImpl = config.fetch ?? globalThis.fetch.bind(globalThis);
54
+ this.#win =
55
+ config.window ??
56
+ (typeof window !== "undefined" ? window : undefined);
57
+ this.#sessionStore = config.sessionStore ?? defaultSessionStore();
58
+ this.#keyStore = config.keyStore ?? defaultKeyStore();
59
+ }
60
+ /* ------------------------------------------------------------------------ */
61
+ /* Boot-time hydration */
62
+ /* ------------------------------------------------------------------------ */
63
+ /**
64
+ * Reload signing material and JWT session from the configured stores.
65
+ * Must be called once at app boot before relying on
66
+ * {@link getCurrentSession} / {@link getOwnerInfo} / {@link canSignAsOwner}
67
+ * — until then they reflect only what's been done in-memory in the
68
+ * current tab.
69
+ *
70
+ * Strict consistency: if the JWT and the stored owner disagree about
71
+ * who's signed in, both are wiped and the user re-auths. JWT-less
72
+ * owner state (loaded from keyStore but no JWT) is a valid resumed
73
+ * state — the user signed in via recovery or imported a mandate at
74
+ * some earlier moment and never went through the JWT flow.
75
+ */
76
+ async resume() {
77
+ // 1. Owner side.
78
+ const stored = await this.#keyStore.loadOwner().catch(() => null);
79
+ const jwt = this.#sessionStore.get();
80
+ if (stored) {
81
+ this.#ownerSigners = OwnerSigners.fromStoredOwnerKeys(stored);
82
+ // JWT must match the owner DID — otherwise it's stale state.
83
+ if (jwt && jwt.did !== stored.did) {
84
+ this.#sessionStore.clear();
85
+ }
86
+ }
87
+ else {
88
+ // No owner persisted. A lingering JWT is meaningless without local
89
+ // signing capability — wipe it (strict mode).
90
+ if (jwt)
91
+ this.#sessionStore.clear();
92
+ }
93
+ // 2. Delegate side. Independent of owner state — a user may hold
94
+ // only delegate bundles and no owner identity at all.
95
+ const storedDelegates = await this.#keyStore
96
+ .listDelegates()
97
+ .catch(() => []);
98
+ for (const d of storedDelegates) {
99
+ try {
100
+ this.#delegates.add(DelegateActor.fromStored(d));
101
+ }
102
+ catch {
103
+ // Skip corrupted entries silently — the keystore validators
104
+ // already filter most of these. A skip here means the keystore
105
+ // record passed validation but the seed couldn't be re-derived
106
+ // (e.g. zero-length, future migration); ignore and continue.
107
+ }
108
+ }
109
+ }
110
+ /* ------------------------------------------------------------------------ */
111
+ /* State accessors */
112
+ /* ------------------------------------------------------------------------ */
113
+ /** JWT-backed session. Null when signed in via recovery / mandate / not at all. */
114
+ getCurrentSession() {
115
+ const session = this.#sessionStore.get();
116
+ if (!session)
117
+ return null;
118
+ // Belt-and-braces: the session store auto-evicts on expiry, but we
119
+ // also re-check here in case the in-tab clock drifted post-load.
120
+ return session;
121
+ }
122
+ /** Loaded owner identity. Independent of JWT presence. */
123
+ getOwnerInfo() {
124
+ if (!this.#ownerSigners)
125
+ return null;
126
+ return {
127
+ did: this.#ownerSigners.did,
128
+ handle: this.#ownerSigners.handle,
129
+ displayName: this.#ownerSigners.displayName,
130
+ };
131
+ }
132
+ getDelegates() {
133
+ return this.#delegates.list().map(actorToInfo);
134
+ }
135
+ canSignAsOwner() {
136
+ return this.#ownerSigners !== null && !this.#ownerSigners.destroyed;
137
+ }
138
+ canSignAsDelegateFor(did) {
139
+ const a = this.#delegates.findForSubject(did);
140
+ return a !== undefined && !a.destroyed;
141
+ }
142
+ /**
143
+ * Internal accessor used by sibling SDK namespaces (compute, wallet,
144
+ * ethos) when they need to sign on behalf of the owner. Returns null
145
+ * if no owner is loaded.
146
+ *
147
+ * @internal
148
+ */
149
+ _getOwnerSigners() {
150
+ return this.#ownerSigners;
151
+ }
152
+ /**
153
+ * Internal accessor — looks up an active delegate by mandate id.
154
+ * @internal
155
+ */
156
+ _getDelegateActor(mandateId) {
157
+ return this.#delegates.get(mandateId);
158
+ }
159
+ /**
160
+ * Internal accessor — finds the first active delegate whose subject
161
+ * matches `did`. Used by `sdk.ethos.of(did)` when the user holds a
162
+ * mandate for that subject.
163
+ * @internal
164
+ */
165
+ _findDelegateForSubject(did) {
166
+ return this.#delegates.findForSubject(did);
167
+ }
168
+ /* ------------------------------------------------------------------------ */
169
+ /* Email + password — signIn */
170
+ /* ------------------------------------------------------------------------ */
171
+ async signIn(input) {
172
+ if (!input.email || !input.password) {
173
+ throw new AithosSDKError("auth_invalid_input", "signIn: email and password are required");
174
+ }
175
+ const challenge = await loginChallenge({ fetchImpl: this.#fetchImpl, authBaseUrl: this.authBaseUrl }, input.email);
176
+ const { authKey, encKey } = await deriveAuthAndEncKeys(input.password, challenge.authSalt, challenge.encSalt, challenge.kdf);
177
+ let verify;
178
+ try {
179
+ verify = await loginVerify({ fetchImpl: this.#fetchImpl, authBaseUrl: this.authBaseUrl }, input.email, authKey);
180
+ }
181
+ catch (e) {
182
+ // On failure, both keys must be wiped before propagating.
183
+ zeroize(authKey);
184
+ zeroize(encKey);
185
+ throw e;
186
+ }
187
+ zeroize(authKey);
188
+ // Decrypt the vault blob → plaintext seeds + delegate bundles.
189
+ let plaintext;
190
+ try {
191
+ const blobBytes = decryptBlob(encKey, verify.blobNonce, verify.blob);
192
+ try {
193
+ plaintext = parseBlob(blobBytes);
194
+ }
195
+ finally {
196
+ zeroize(blobBytes);
197
+ }
198
+ }
199
+ catch (e) {
200
+ zeroize(encKey);
201
+ throw new AithosSDKError("auth_blob_decrypt_failed", `Could not decrypt the vault blob: ${e.message}`);
202
+ }
203
+ zeroize(encKey);
204
+ // Sanity check: blob's identity must agree with what verify returned.
205
+ if (plaintext.identity.did !== verify.did) {
206
+ throw new AithosSDKError("auth_blob_identity_mismatch", "vault blob's DID does not match the verified login DID", { data: { blobDid: plaintext.identity.did, verifyDid: verify.did } });
207
+ }
208
+ // Hydrate in-memory state.
209
+ if (this.#ownerSigners)
210
+ this.#ownerSigners.destroy();
211
+ this.#ownerSigners = OwnerSigners.fromBlobPlaintext(plaintext);
212
+ // Persist to keyStore — owner first, then delegates.
213
+ const ownerStored = {
214
+ version: "0.1.0-hex",
215
+ did: plaintext.identity.did,
216
+ handle: plaintext.identity.handle,
217
+ displayName: plaintext.identity.displayName,
218
+ seedsHex: plaintext.seeds,
219
+ savedAt: new Date().toISOString(),
220
+ };
221
+ await this.#keyStore.saveOwner(ownerStored);
222
+ // Replace any prior delegate set with what the blob carries.
223
+ await this.#keyStore.clearAllDelegates();
224
+ this.#delegates.destroy();
225
+ for (const d of plaintext.delegates) {
226
+ const stored = storedDelegateFromBlob(d);
227
+ try {
228
+ await this.#keyStore.saveDelegate(stored);
229
+ }
230
+ catch {
231
+ // Persistence failure shouldn't block the sign-in. We still load
232
+ // the actor in memory so the session works for the current tab.
233
+ }
234
+ try {
235
+ this.#delegates.add(DelegateActor.fromStored(stored));
236
+ }
237
+ catch {
238
+ // Skip silently — keep going on remaining delegates.
239
+ }
240
+ }
241
+ const session = {
242
+ session: verify.session,
243
+ exp: verify.exp,
244
+ did: verify.did,
245
+ handle: verify.handle,
246
+ blob_b64: bytesToB64Public(verify.blob),
247
+ blob_nonce_b64: bytesToB64Public(verify.blobNonce),
248
+ blob_version: verify.blobVersion,
249
+ enc_key_b64: "",
250
+ is_first_login: false,
251
+ };
252
+ this.#sessionStore.set(session);
253
+ return session;
254
+ }
255
+ /* ------------------------------------------------------------------------ */
256
+ /* Email + password — signUp */
257
+ /* ------------------------------------------------------------------------ */
258
+ async signUp(input) {
259
+ if (!input.email || !input.password) {
260
+ throw new AithosSDKError("auth_invalid_input", "signUp: email and password are required");
261
+ }
262
+ if (!/^[a-z0-9][a-z0-9_-]{0,62}$/i.test(input.handle)) {
263
+ throw new AithosSDKError("auth_invalid_handle", "signUp: handle must be 1–63 alphanumeric chars + _ -");
264
+ }
265
+ const displayName = input.displayName ?? input.handle;
266
+ const identity = createBrowserIdentity(input.handle, displayName);
267
+ const recoverySerialized = serializeRecoveryFile(identity);
268
+ const recoveryFile = new Blob([recoverySerialized.text], {
269
+ type: "application/json",
270
+ });
271
+ // Derive password-based keys, encrypt the vault blob.
272
+ const authSalt = randomSalt();
273
+ const encSalt = randomSalt();
274
+ const kdf = DEFAULT_KDF;
275
+ const { authKey, encKey } = await deriveAuthAndEncKeys(input.password, authSalt, encSalt, kdf);
276
+ const plaintext = buildBlobPlaintext({
277
+ identity: {
278
+ did: identity.did,
279
+ handle: identity.handle,
280
+ displayName: identity.displayName,
281
+ },
282
+ seeds: {
283
+ root: identity.root.seed,
284
+ public: identity.public.seed,
285
+ circle: identity.circle.seed,
286
+ self: identity.self.seed,
287
+ },
288
+ delegates: [],
289
+ });
290
+ const blobBytes = serializeBlob(plaintext);
291
+ const blobNonce = randomNonce();
292
+ const blob = encryptBlob(encKey, blobNonce, blobBytes);
293
+ let registerResp;
294
+ try {
295
+ registerResp = await registerAccount({ fetchImpl: this.#fetchImpl, authBaseUrl: this.authBaseUrl }, {
296
+ email: input.email,
297
+ handle: identity.handle,
298
+ displayName: identity.displayName,
299
+ did: identity.did,
300
+ authKey,
301
+ authSalt,
302
+ encSalt,
303
+ kdf,
304
+ blob,
305
+ blobNonce,
306
+ blobVersion: 1,
307
+ });
308
+ }
309
+ finally {
310
+ zeroize(authKey);
311
+ zeroize(encKey);
312
+ }
313
+ // Bootstrap the Ethos on api.aithos.be. Without this, every subsequent
314
+ // write (publish_ethos_edition, etc.) errors out with -32020
315
+ // "subject identity not published". We do this BEFORE hydrating local
316
+ // state so a bootstrap failure leaves the SDK in a clean
317
+ // "not signed in" state — the dev shows an error, the user retries.
318
+ // The auth account on auth.aithos.be DOES exist at this point, but
319
+ // without the local hydrate the user can't act on it. Self-heal on
320
+ // signIn (re-attempt publish_identity if missing) is planned for a
321
+ // follow-up release.
322
+ await this.#publishIdentity(identity);
323
+ // Hydrate in-memory state from the fresh identity.
324
+ if (this.#ownerSigners)
325
+ this.#ownerSigners.destroy();
326
+ this.#ownerSigners = OwnerSigners.fromBrowserIdentity(identity);
327
+ await this.#keyStore.saveOwner({
328
+ version: "0.1.0-hex",
329
+ did: identity.did,
330
+ handle: identity.handle,
331
+ displayName: identity.displayName,
332
+ seedsHex: {
333
+ root: bytesToHex(identity.root.seed),
334
+ public: bytesToHex(identity.public.seed),
335
+ circle: bytesToHex(identity.circle.seed),
336
+ self: bytesToHex(identity.self.seed),
337
+ },
338
+ savedAt: new Date().toISOString(),
339
+ });
340
+ const session = {
341
+ session: registerResp.session,
342
+ exp: registerResp.exp,
343
+ did: identity.did,
344
+ handle: identity.handle,
345
+ blob_b64: bytesToB64Public(blob),
346
+ blob_nonce_b64: bytesToB64Public(blobNonce),
347
+ blob_version: 1,
348
+ enc_key_b64: "",
349
+ is_first_login: false,
350
+ };
351
+ this.#sessionStore.set(session);
352
+ return {
353
+ session,
354
+ recoveryFile,
355
+ recoveryFilename: recoverySerialized.filename,
356
+ };
357
+ }
358
+ /* ------------------------------------------------------------------------ */
359
+ /* Recovery file */
360
+ /* ------------------------------------------------------------------------ */
361
+ /**
362
+ * Sign in by uploading a recovery file. Hydrates the owner signers
363
+ * locally — no JWT is obtained on this path because the recovery
364
+ * file alone doesn't authenticate against the auth backend (no
365
+ * password, no Google session). Apps that need compute/wallet
366
+ * access should follow up with an email+password sign-in or with
367
+ * Google SSO.
368
+ *
369
+ * The recovery file is ALWAYS the file produced by `signUp` (or the
370
+ * equivalent one emitted by `protocol-client`'s `runOnboarding`).
371
+ * Both shapes are accepted.
372
+ */
373
+ async signInWithRecovery(input) {
374
+ const text = await readRecoveryFileText(input.file);
375
+ const parsed = parseRecoveryFile(text);
376
+ // Build a StoredOwnerKeys-shape on the spot, then push to keyStore +
377
+ // hydrate signers.
378
+ const stored = {
379
+ version: "0.1.0-hex",
380
+ did: parsed.did,
381
+ handle: parsed.handle,
382
+ displayName: parsed.displayName,
383
+ seedsHex: parsed.seedsHex,
384
+ savedAt: new Date().toISOString(),
385
+ };
386
+ // If a different owner is already loaded, refuse — apps must call
387
+ // signOut() first. Mixing two owners in one auth instance is a
388
+ // nonsense state we don't want to support.
389
+ if (this.#ownerSigners && this.#ownerSigners.did !== parsed.did) {
390
+ throw new AithosSDKError("auth_owner_already_loaded", "another owner is already signed in; call signOut first", { data: { current: this.#ownerSigners.did, incoming: parsed.did } });
391
+ }
392
+ if (this.#ownerSigners)
393
+ this.#ownerSigners.destroy();
394
+ this.#ownerSigners = OwnerSigners.fromStoredOwnerKeys(stored);
395
+ await this.#keyStore.saveOwner(stored);
396
+ // Recovery flow doesn't yield a JWT — wipe any stale one to keep
397
+ // the two stores in sync.
398
+ this.#sessionStore.clear();
399
+ return {
400
+ did: parsed.did,
401
+ handle: parsed.handle,
402
+ displayName: parsed.displayName,
403
+ };
404
+ }
405
+ /* ------------------------------------------------------------------------ */
406
+ /* Mandate import */
407
+ /* ------------------------------------------------------------------------ */
408
+ /**
409
+ * Import a delegate bundle (`.aithos-delegate.json`). Works in any
410
+ * state: with no owner loaded (delegate-only session), or alongside
411
+ * an existing owner (the user holds mandates for other people's
412
+ * ethoses while also being an owner themselves).
413
+ */
414
+ async importMandate(input) {
415
+ const text = await readDelegateBundleText(input.bundle);
416
+ const parsed = parseDelegateBundle(text);
417
+ const stored = {
418
+ version: "0.1.0-hex",
419
+ subjectDid: parsed.subjectDid,
420
+ mandateId: parsed.mandateId,
421
+ mandate: parsed.mandate,
422
+ granteeId: parsed.granteeId,
423
+ granteePubkeyMultibase: parsed.granteePubkeyMultibase,
424
+ delegateSeedHex: parsed.delegateSeedHex,
425
+ importedAt: new Date().toISOString(),
426
+ };
427
+ await this.#keyStore.saveDelegate(stored);
428
+ this.#delegates.add(DelegateActor.fromStored(stored));
429
+ return {
430
+ mandateId: stored.mandateId,
431
+ subjectDid: stored.subjectDid,
432
+ granteeId: stored.granteeId,
433
+ scopes: scopesFromMandate(stored.mandate),
434
+ expiresAt: notAfterFromMandate(stored.mandate),
435
+ };
436
+ }
437
+ async removeMandate(mandateId) {
438
+ this.#delegates.remove(mandateId);
439
+ await this.#keyStore.removeDelegate(mandateId);
440
+ }
441
+ /* ------------------------------------------------------------------------ */
442
+ /* Google SSO */
443
+ /* ------------------------------------------------------------------------ */
444
+ signInWithGoogle(opts) {
445
+ if (!this.#win) {
446
+ throw new AithosSDKError("auth_no_window", "AithosAuth.signInWithGoogle requires a browser window");
447
+ }
448
+ const url = new URL(`${this.authBaseUrl}/auth/sso/google/start`);
449
+ if (opts?.appState) {
450
+ if (opts.appState.length > 1024) {
451
+ throw new AithosSDKError("auth_app_state_too_long", "appState must be ≤ 1024 chars");
452
+ }
453
+ url.searchParams.set("app_state", opts.appState);
454
+ }
455
+ this.#win.location.assign(url.toString());
456
+ throw new AithosSDKError("auth_redirecting", "redirecting to google");
457
+ }
458
+ async handleCallback() {
459
+ if (!this.#win)
460
+ return null;
461
+ const here = new URL(this.#win.location.href);
462
+ const error = here.searchParams.get("aithos_error");
463
+ const code = here.searchParams.get("aithos_code");
464
+ const appState = here.searchParams.get("app_state");
465
+ if (error) {
466
+ cleanCallbackParams(this.#win, here);
467
+ throw new AithosSDKError(`auth_${error}`, `Sign-in failed: ${error}`, { data: appState ? { app_state: appState } : undefined });
468
+ }
469
+ if (!code)
470
+ return null;
471
+ const session = await this.exchange(code);
472
+ cleanCallbackParams(this.#win, here);
473
+ // Hydrate signers if the SSO response carried an enc_key (Google flow
474
+ // gives us the AES-GCM key in plaintext, encrypted only in transit
475
+ // by TLS — see auth.aithos.be design doc).
476
+ if (session.enc_key_b64 &&
477
+ session.blob_b64 &&
478
+ session.blob_nonce_b64 &&
479
+ session.blob_version > 0) {
480
+ try {
481
+ const encKey = b64ToBytes(session.enc_key_b64);
482
+ const blob = b64ToBytes(session.blob_b64);
483
+ const nonce = b64ToBytes(session.blob_nonce_b64);
484
+ try {
485
+ const blobBytes = decryptBlob(encKey, nonce, blob);
486
+ try {
487
+ const plaintext = parseBlob(blobBytes);
488
+ if (plaintext.identity.did === session.did) {
489
+ if (this.#ownerSigners)
490
+ this.#ownerSigners.destroy();
491
+ this.#ownerSigners = OwnerSigners.fromBlobPlaintext(plaintext);
492
+ await this.#keyStore.saveOwner({
493
+ version: "0.1.0-hex",
494
+ did: plaintext.identity.did,
495
+ handle: plaintext.identity.handle,
496
+ displayName: plaintext.identity.displayName,
497
+ seedsHex: plaintext.seeds,
498
+ savedAt: new Date().toISOString(),
499
+ });
500
+ await this.#keyStore.clearAllDelegates();
501
+ this.#delegates.destroy();
502
+ for (const d of plaintext.delegates) {
503
+ const stored = storedDelegateFromBlob(d);
504
+ try {
505
+ await this.#keyStore.saveDelegate(stored);
506
+ }
507
+ catch {
508
+ /* keep going */
509
+ }
510
+ try {
511
+ this.#delegates.add(DelegateActor.fromStored(stored));
512
+ }
513
+ catch {
514
+ /* keep going */
515
+ }
516
+ }
517
+ }
518
+ }
519
+ finally {
520
+ zeroize(blobBytes);
521
+ }
522
+ }
523
+ finally {
524
+ zeroize(encKey);
525
+ }
526
+ }
527
+ catch {
528
+ // Decryption failure is non-fatal here: the JWT still works for
529
+ // compute/wallet, the user will surface the issue if they try to
530
+ // edit their ethos.
531
+ }
532
+ }
533
+ this.#sessionStore.set(session);
534
+ return session;
535
+ }
536
+ async exchange(aithosCode) {
537
+ const res = await this.#fetchImpl(`${this.authBaseUrl}/auth/sso/exchange`, {
538
+ method: "POST",
539
+ headers: { "content-type": "application/json" },
540
+ body: JSON.stringify({ aithos_code: aithosCode }),
541
+ });
542
+ if (!res.ok) {
543
+ let body;
544
+ try {
545
+ body = (await res.json());
546
+ }
547
+ catch {
548
+ // ignore non-JSON error body
549
+ }
550
+ const code = typeof body?.["code"] === "string"
551
+ ? `auth_${body["code"]}`
552
+ : "auth_exchange_failed";
553
+ const message = typeof body?.["error"] === "string"
554
+ ? body["error"]
555
+ : `aithos_code redemption failed (${res.status})`;
556
+ throw new AithosSDKError(code, message, {
557
+ status: res.status,
558
+ ...(body !== undefined ? { data: body } : {}),
559
+ });
560
+ }
561
+ return (await res.json());
562
+ }
563
+ /* ------------------------------------------------------------------------ */
564
+ /* Sign-out */
565
+ /* ------------------------------------------------------------------------ */
566
+ async signOut() {
567
+ if (this.#ownerSigners)
568
+ this.#ownerSigners.destroy();
569
+ this.#ownerSigners = null;
570
+ this.#delegates.destroy();
571
+ this.#sessionStore.clear();
572
+ await this.#keyStore.clearOwner().catch(() => { });
573
+ await this.#keyStore.clearAllDelegates().catch(() => { });
574
+ }
575
+ /* ------------------------------------------------------------------------ */
576
+ /* Internal — Ethos bootstrap */
577
+ /* ------------------------------------------------------------------------ */
578
+ /**
579
+ * Provision the user's Ethos on `api.aithos.be` by signing and POSTing an
580
+ * `aithos.publish_identity` envelope. Required after a fresh sign-up so
581
+ * subsequent edition publishes (`me.publish()`) don't fail with
582
+ * `-32020 subject identity not published`.
583
+ *
584
+ * Retries twice with exponential backoff on transient errors (network or
585
+ * 5xx). Throws {@link AithosSDKError} with code `ethos_bootstrap_failed`
586
+ * on definitive failure — the caller is expected to abort sign-up.
587
+ *
588
+ * @internal
589
+ */
590
+ async #publishIdentity(identity) {
591
+ const url = `${this.apiBaseUrl}/mcp/primitives/write`;
592
+ const signedDoc = signedDidDocument(identity);
593
+ const params = {
594
+ did_document: signedDoc,
595
+ handle: identity.handle,
596
+ display_name: identity.displayName,
597
+ };
598
+ const envelope = buildSignedEnvelope({
599
+ iss: identity.did,
600
+ aud: url,
601
+ method: "aithos.publish_identity",
602
+ verificationMethod: `${identity.did}#root`,
603
+ params,
604
+ signer: identity.root,
605
+ });
606
+ const body = JSON.stringify({
607
+ jsonrpc: "2.0",
608
+ id: "publish_identity",
609
+ method: "aithos.publish_identity",
610
+ params: { ...params, _envelope: envelope },
611
+ });
612
+ // Two retries with backoff (300ms, 1500ms). Idempotent on the server
613
+ // side — replaying the same publish_identity for an existing DID is a
614
+ // no-op, so retries are safe even if the first attempt actually
615
+ // succeeded but the response was lost.
616
+ const delays = [0, 300, 1500];
617
+ let lastError;
618
+ for (const delay of delays) {
619
+ if (delay > 0)
620
+ await sleep(delay);
621
+ try {
622
+ const res = await this.#fetchImpl(url, {
623
+ method: "POST",
624
+ headers: { "content-type": "application/json" },
625
+ body,
626
+ });
627
+ // Transport errors (5xx, no body) — retry. JSON-RPC errors come
628
+ // back with HTTP 200 and an `error` field.
629
+ if (!res.ok && res.status >= 500) {
630
+ lastError = new Error(`HTTP ${res.status}`);
631
+ continue;
632
+ }
633
+ const json = (await res.json());
634
+ if (json.error) {
635
+ // JSON-RPC error: don't retry — these are deterministic
636
+ // (validation, permission, identity-already-tombstoned, …).
637
+ throw new AithosSDKError("ethos_bootstrap_failed", `publish_identity rejected: ${json.error.message}`, {
638
+ status: res.status,
639
+ data: { rpc_code: json.error.code, ...(json.error.data ?? {}) },
640
+ });
641
+ }
642
+ return; // success
643
+ }
644
+ catch (e) {
645
+ if (e instanceof AithosSDKError)
646
+ throw e;
647
+ lastError = e;
648
+ }
649
+ }
650
+ throw new AithosSDKError("ethos_bootstrap_failed", `publish_identity unreachable after ${delays.length} attempts: ${lastError?.message ?? "unknown"}`);
651
+ }
652
+ }
653
+ /* -------------------------------------------------------------------------- */
654
+ /* Helpers */
655
+ /* -------------------------------------------------------------------------- */
656
+ function trimSlash(url) {
657
+ return url.endsWith("/") ? url.slice(0, -1) : url;
658
+ }
659
+ function sleep(ms) {
660
+ return new Promise((resolve) => setTimeout(resolve, ms));
661
+ }
662
+ function cleanCallbackParams(win, url) {
663
+ url.searchParams.delete("aithos_code");
664
+ url.searchParams.delete("aithos_error");
665
+ url.searchParams.delete("app_state");
666
+ win.history.replaceState(null, "", url.toString());
667
+ }
668
+ function bytesToB64Public(bytes) {
669
+ if (bytes.length === 0)
670
+ return "";
671
+ let bin = "";
672
+ for (let i = 0; i < bytes.length; i++)
673
+ bin += String.fromCharCode(bytes[i]);
674
+ return btoa(bin).replace(/=+$/, "");
675
+ }
676
+ function b64ToBytes(b64) {
677
+ if (!b64)
678
+ return new Uint8Array(0);
679
+ // standard b64 — pad to multiple of 4 if needed
680
+ const pad = b64.length % 4 === 0 ? "" : "=".repeat(4 - (b64.length % 4));
681
+ const bin = atob(b64 + pad);
682
+ const out = new Uint8Array(bin.length);
683
+ for (let i = 0; i < bin.length; i++)
684
+ out[i] = bin.charCodeAt(i);
685
+ return out;
686
+ }
687
+ function bytesToHex(b) {
688
+ let out = "";
689
+ for (let i = 0; i < b.length; i++)
690
+ out += b[i].toString(16).padStart(2, "0");
691
+ return out;
692
+ }
693
+ /**
694
+ * Project a delegate as it appears in a `BlobPlaintext` (extension-kit
695
+ * `StoredDelegate` shape) onto the SDK's own {@link StoredDelegateKeys}.
696
+ */
697
+ function storedDelegateFromBlob(d) {
698
+ return {
699
+ version: "0.1.0-hex",
700
+ subjectDid: d.subjectDid,
701
+ mandateId: d.mandateId,
702
+ mandate: d.mandate,
703
+ granteeId: d.granteeId,
704
+ granteePubkeyMultibase: d.granteePubkeyMultibase,
705
+ delegateSeedHex: d.delegateSeedHex,
706
+ importedAt: new Date().toISOString(),
707
+ };
708
+ }
709
+ function actorToInfo(a) {
710
+ return {
711
+ mandateId: a.mandateId,
712
+ subjectDid: a.subjectDid,
713
+ granteeId: a.granteeId,
714
+ scopes: scopesFromMandate(a.mandate),
715
+ expiresAt: notAfterFromMandate(a.mandate),
716
+ };
717
+ }
718
+ function scopesFromMandate(m) {
719
+ const raw = m["scopes"];
720
+ if (!Array.isArray(raw))
721
+ return [];
722
+ return raw.filter((s) => typeof s === "string");
723
+ }
724
+ function notAfterFromMandate(m) {
725
+ const raw = m["not_after"];
726
+ if (typeof raw !== "string")
727
+ return null;
728
+ return raw;
729
+ }
730
+ //# sourceMappingURL=auth.js.map