@tinycloud/node-sdk 1.7.0 → 2.0.1

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/core.js ADDED
@@ -0,0 +1,2425 @@
1
+ // src/core.ts
2
+ import { TinyCloud as TinyCloud2 } from "@tinycloud/sdk-core";
3
+ import {
4
+ SilentNotificationHandler as SilentNotificationHandler2,
5
+ AutoApproveSpaceCreationHandler as AutoApproveSpaceCreationHandler2,
6
+ defaultSpaceCreationHandler
7
+ } from "@tinycloud/sdk-core";
8
+
9
+ // src/storage/MemorySessionStorage.ts
10
+ var MemorySessionStorage = class {
11
+ constructor() {
12
+ this.sessions = /* @__PURE__ */ new Map();
13
+ }
14
+ /**
15
+ * Save a session for an address.
16
+ */
17
+ async save(address, session) {
18
+ const normalizedAddress = address.toLowerCase();
19
+ this.sessions.set(normalizedAddress, session);
20
+ }
21
+ /**
22
+ * Load a session for an address.
23
+ */
24
+ async load(address) {
25
+ const normalizedAddress = address.toLowerCase();
26
+ const session = this.sessions.get(normalizedAddress);
27
+ if (!session) {
28
+ return null;
29
+ }
30
+ const expiresAt = new Date(session.expiresAt);
31
+ if (expiresAt < /* @__PURE__ */ new Date()) {
32
+ this.sessions.delete(normalizedAddress);
33
+ return null;
34
+ }
35
+ return session;
36
+ }
37
+ /**
38
+ * Clear a session for an address.
39
+ */
40
+ async clear(address) {
41
+ const normalizedAddress = address.toLowerCase();
42
+ this.sessions.delete(normalizedAddress);
43
+ }
44
+ /**
45
+ * Check if a session exists for an address.
46
+ */
47
+ exists(address) {
48
+ const normalizedAddress = address.toLowerCase();
49
+ const session = this.sessions.get(normalizedAddress);
50
+ if (!session) {
51
+ return false;
52
+ }
53
+ const expiresAt = new Date(session.expiresAt);
54
+ if (expiresAt < /* @__PURE__ */ new Date()) {
55
+ this.sessions.delete(normalizedAddress);
56
+ return false;
57
+ }
58
+ return true;
59
+ }
60
+ /**
61
+ * Memory storage is always available.
62
+ */
63
+ isAvailable() {
64
+ return true;
65
+ }
66
+ /**
67
+ * Clear all sessions.
68
+ */
69
+ clearAll() {
70
+ this.sessions.clear();
71
+ }
72
+ /**
73
+ * Get the number of stored sessions.
74
+ */
75
+ size() {
76
+ return this.sessions.size;
77
+ }
78
+ };
79
+
80
+ // src/storage/FileSessionStorage.ts
81
+ import { validatePersistedSessionData } from "@tinycloud/sdk-core";
82
+ import { readFileSync, writeFileSync, existsSync, mkdirSync, unlinkSync } from "fs";
83
+ import { join } from "path";
84
+ var FileSessionStorage = class {
85
+ /**
86
+ * Create a new FileSessionStorage.
87
+ *
88
+ * @param baseDir - Directory to store session files (default: ~/.tinycloud/sessions)
89
+ */
90
+ constructor(baseDir) {
91
+ this.baseDir = baseDir || this.getDefaultDir();
92
+ this.ensureDirectoryExists();
93
+ }
94
+ /**
95
+ * Get the default session storage directory.
96
+ */
97
+ getDefaultDir() {
98
+ const home = process.env.HOME || process.env.USERPROFILE || "/tmp";
99
+ return join(home, ".tinycloud", "sessions");
100
+ }
101
+ /**
102
+ * Ensure the storage directory exists.
103
+ */
104
+ ensureDirectoryExists() {
105
+ if (!existsSync(this.baseDir)) {
106
+ mkdirSync(this.baseDir, { recursive: true });
107
+ }
108
+ }
109
+ /**
110
+ * Get the file path for an address.
111
+ */
112
+ getFilePath(address) {
113
+ const normalizedAddress = address.toLowerCase();
114
+ const filename = `${normalizedAddress.replace("0x", "")}.json`;
115
+ return join(this.baseDir, filename);
116
+ }
117
+ /**
118
+ * Save a session for an address.
119
+ */
120
+ async save(address, session) {
121
+ const filePath = this.getFilePath(address);
122
+ const data = JSON.stringify(session, null, 2);
123
+ writeFileSync(filePath, data, "utf-8");
124
+ }
125
+ /**
126
+ * Load a session for an address.
127
+ */
128
+ async load(address) {
129
+ const filePath = this.getFilePath(address);
130
+ if (!existsSync(filePath)) {
131
+ return null;
132
+ }
133
+ try {
134
+ const data = readFileSync(filePath, "utf-8");
135
+ const parsed = JSON.parse(data);
136
+ const validation = validatePersistedSessionData(parsed);
137
+ if (!validation.ok) {
138
+ console.warn(`Invalid session data for ${address}:`, validation.error.message);
139
+ unlinkSync(filePath);
140
+ return null;
141
+ }
142
+ const session = validation.data;
143
+ const expiresAt = new Date(session.expiresAt);
144
+ if (expiresAt < /* @__PURE__ */ new Date()) {
145
+ unlinkSync(filePath);
146
+ return null;
147
+ }
148
+ return session;
149
+ } catch (error) {
150
+ try {
151
+ unlinkSync(filePath);
152
+ } catch {
153
+ }
154
+ return null;
155
+ }
156
+ }
157
+ /**
158
+ * Clear a session for an address.
159
+ */
160
+ async clear(address) {
161
+ const filePath = this.getFilePath(address);
162
+ if (existsSync(filePath)) {
163
+ unlinkSync(filePath);
164
+ }
165
+ }
166
+ /**
167
+ * Check if a session exists for an address.
168
+ */
169
+ exists(address) {
170
+ const filePath = this.getFilePath(address);
171
+ if (!existsSync(filePath)) {
172
+ return false;
173
+ }
174
+ try {
175
+ const data = readFileSync(filePath, "utf-8");
176
+ const session = JSON.parse(data);
177
+ const expiresAt = new Date(session.expiresAt);
178
+ if (expiresAt < /* @__PURE__ */ new Date()) {
179
+ unlinkSync(filePath);
180
+ return false;
181
+ }
182
+ return true;
183
+ } catch {
184
+ return false;
185
+ }
186
+ }
187
+ /**
188
+ * Check if file system storage is available.
189
+ */
190
+ isAvailable() {
191
+ try {
192
+ this.ensureDirectoryExists();
193
+ return existsSync(this.baseDir);
194
+ } catch {
195
+ return false;
196
+ }
197
+ }
198
+ };
199
+
200
+ // src/authorization/NodeUserAuthorization.ts
201
+ import {
202
+ fetchPeerId,
203
+ submitHostDelegation,
204
+ activateSessionWithHost,
205
+ checkNodeInfo,
206
+ AutoApproveSpaceCreationHandler
207
+ } from "@tinycloud/sdk-core";
208
+
209
+ // src/authorization/strategies.ts
210
+ var defaultSignStrategy = { type: "auto-sign" };
211
+
212
+ // src/authorization/NodeUserAuthorization.ts
213
+ var NodeUserAuthorization = class {
214
+ constructor(config) {
215
+ this.extensions = [];
216
+ this._nodeFeatures = [];
217
+ this.wasm = config.wasmBindings;
218
+ this.signer = config.signer;
219
+ this.signStrategy = config.signStrategy ?? defaultSignStrategy;
220
+ this.sessionStorage = config.sessionStorage ?? new MemorySessionStorage();
221
+ this.domain = config.domain;
222
+ this.uri = config.uri ?? `https://${config.domain}`;
223
+ this.statement = config.statement;
224
+ this.spacePrefix = config.spacePrefix ?? "default";
225
+ this.defaultActions = config.defaultActions ?? {
226
+ kv: {
227
+ "": [
228
+ "tinycloud.kv/put",
229
+ "tinycloud.kv/get",
230
+ "tinycloud.kv/del",
231
+ "tinycloud.kv/list",
232
+ "tinycloud.kv/metadata"
233
+ ]
234
+ },
235
+ sql: {
236
+ "": [
237
+ "tinycloud.sql/read",
238
+ "tinycloud.sql/write",
239
+ "tinycloud.sql/admin",
240
+ "tinycloud.sql/export"
241
+ ]
242
+ },
243
+ duckdb: {
244
+ "": [
245
+ "tinycloud.duckdb/read",
246
+ "tinycloud.duckdb/write",
247
+ "tinycloud.duckdb/admin",
248
+ "tinycloud.duckdb/describe",
249
+ "tinycloud.duckdb/export",
250
+ "tinycloud.duckdb/import",
251
+ "tinycloud.duckdb/execute"
252
+ ]
253
+ },
254
+ capabilities: {
255
+ "": ["tinycloud.capabilities/read"]
256
+ }
257
+ };
258
+ this.sessionExpirationMs = config.sessionExpirationMs ?? 60 * 60 * 1e3;
259
+ this.autoCreateSpace = config.autoCreateSpace ?? false;
260
+ this.spaceCreationHandler = config.spaceCreationHandler;
261
+ this.tinycloudHosts = config.tinycloudHosts ?? ["https://node.tinycloud.xyz"];
262
+ this.enablePublicSpace = config.enablePublicSpace ?? true;
263
+ this.sessionManager = this.wasm.createSessionManager();
264
+ }
265
+ /**
266
+ * The current active session (web-core compatible).
267
+ */
268
+ get session() {
269
+ return this._session;
270
+ }
271
+ /**
272
+ * The current TinyCloud session with full delegation data.
273
+ * Includes spaceId, delegationHeader, and delegationCid.
274
+ */
275
+ get tinyCloudSession() {
276
+ return this._tinyCloudSession;
277
+ }
278
+ get nodeFeatures() {
279
+ return this._nodeFeatures;
280
+ }
281
+ /**
282
+ * Add an extension to the authorization flow.
283
+ */
284
+ extend(extension) {
285
+ this.extensions.push(extension);
286
+ }
287
+ /**
288
+ * Get the space ID for the current session.
289
+ */
290
+ getSpaceId() {
291
+ return this._tinyCloudSession?.spaceId;
292
+ }
293
+ /**
294
+ * Create the space on the TinyCloud server (host delegation).
295
+ * This registers the user as the owner of the space.
296
+ */
297
+ async hostSpace(targetSpaceId) {
298
+ if (!this._tinyCloudSession || !this._address || !this._chainId) {
299
+ throw new Error("Must be signed in to host space");
300
+ }
301
+ const host = this.tinycloudHosts[0];
302
+ const spaceId = targetSpaceId ?? this._tinyCloudSession.spaceId;
303
+ const peerId = await fetchPeerId(host, spaceId);
304
+ const siwe = this.wasm.generateHostSIWEMessage({
305
+ address: this._address,
306
+ chainId: this._chainId,
307
+ domain: this.domain,
308
+ issuedAt: (/* @__PURE__ */ new Date()).toISOString(),
309
+ spaceId,
310
+ peerId
311
+ });
312
+ const signature = await this.signMessage(siwe);
313
+ const headers = this.wasm.siweToDelegationHeaders({ siwe, signature });
314
+ const result = await submitHostDelegation(host, headers);
315
+ return result.success;
316
+ }
317
+ /**
318
+ * Create a specific space on the server via host delegation.
319
+ * Used for lazy creation of additional spaces (e.g., public).
320
+ */
321
+ async hostPublicSpace(spaceId) {
322
+ return this.hostSpace(spaceId);
323
+ }
324
+ /**
325
+ * Ensure the user's space exists on the TinyCloud server.
326
+ * Creates the space if it doesn't exist and autoCreateSpace is enabled.
327
+ * If autoCreateSpace is false and space doesn't exist, silently returns
328
+ * (user may be using delegations to access other spaces).
329
+ *
330
+ * @throws Error if space creation fails
331
+ */
332
+ async ensureSpaceExists() {
333
+ if (!this._tinyCloudSession) {
334
+ throw new Error("Must be signed in to ensure space exists");
335
+ }
336
+ const host = this.tinycloudHosts[0];
337
+ const primarySpaceId = this._tinyCloudSession.spaceId;
338
+ const result = await activateSessionWithHost(
339
+ host,
340
+ this._tinyCloudSession.delegationHeader
341
+ );
342
+ const handler = this.spaceCreationHandler ?? (this.autoCreateSpace ? new AutoApproveSpaceCreationHandler() : void 0);
343
+ const creationContext = {
344
+ spaceId: primarySpaceId,
345
+ address: this._address,
346
+ chainId: this._chainId,
347
+ host
348
+ };
349
+ if (result.success) {
350
+ const primarySkipped = result.skipped?.includes(primarySpaceId);
351
+ if (!primarySkipped) {
352
+ return;
353
+ }
354
+ if (!handler) {
355
+ return;
356
+ }
357
+ const confirmed = await handler.confirmSpaceCreation(creationContext);
358
+ if (!confirmed) {
359
+ return;
360
+ }
361
+ try {
362
+ const created = await this.hostSpace();
363
+ if (!created) {
364
+ const err = new Error(`Failed to create space: ${primarySpaceId}`);
365
+ handler.onSpaceCreationFailed?.(creationContext, err);
366
+ throw err;
367
+ }
368
+ } catch (error) {
369
+ handler.onSpaceCreationFailed?.(creationContext, error instanceof Error ? error : new Error(String(error)));
370
+ throw error;
371
+ }
372
+ await new Promise((resolve) => setTimeout(resolve, 100));
373
+ const retryResult = await activateSessionWithHost(
374
+ host,
375
+ this._tinyCloudSession.delegationHeader
376
+ );
377
+ if (!retryResult.success) {
378
+ const err = new Error(
379
+ `Failed to activate session after creating space: ${retryResult.error}`
380
+ );
381
+ handler.onSpaceCreationFailed?.(creationContext, err);
382
+ throw err;
383
+ }
384
+ handler.onSpaceCreated?.(creationContext);
385
+ return;
386
+ }
387
+ if (result.status === 404) {
388
+ if (!handler) {
389
+ return;
390
+ }
391
+ const confirmed = await handler.confirmSpaceCreation(creationContext);
392
+ if (!confirmed) {
393
+ return;
394
+ }
395
+ try {
396
+ const created = await this.hostSpace();
397
+ if (!created) {
398
+ const err = new Error(`Failed to create space: ${primarySpaceId}`);
399
+ handler.onSpaceCreationFailed?.(creationContext, err);
400
+ throw err;
401
+ }
402
+ } catch (error) {
403
+ handler.onSpaceCreationFailed?.(creationContext, error instanceof Error ? error : new Error(String(error)));
404
+ throw error;
405
+ }
406
+ await new Promise((resolve) => setTimeout(resolve, 100));
407
+ const retryResult = await activateSessionWithHost(
408
+ host,
409
+ this._tinyCloudSession.delegationHeader
410
+ );
411
+ if (!retryResult.success) {
412
+ const err = new Error(
413
+ `Failed to activate session after creating space: ${retryResult.error}`
414
+ );
415
+ handler.onSpaceCreationFailed?.(creationContext, err);
416
+ throw err;
417
+ }
418
+ handler.onSpaceCreated?.(creationContext);
419
+ return;
420
+ }
421
+ throw new Error(`Failed to activate session: ${result.error}`);
422
+ }
423
+ /**
424
+ * Sign in and create a new session.
425
+ *
426
+ * This follows the correct SIWE-ReCap flow:
427
+ * 1. Create session key and get JWK
428
+ * 2. Call prepareSession() which generates the SIWE with ReCap capabilities
429
+ * 3. Sign the SIWE string from prepareSession
430
+ * 4. Call completeSessionSetup() with the prepared session + signature
431
+ */
432
+ async signIn() {
433
+ this._address = await this.signer.getAddress();
434
+ this._chainId = await this.signer.getChainId();
435
+ const address = this.wasm.ensureEip55(this._address);
436
+ const chainId = this._chainId;
437
+ const keyId = `session-${Date.now()}`;
438
+ this.sessionManager.renameSessionKeyId("default", keyId);
439
+ const jwkString = this.sessionManager.jwk(keyId);
440
+ if (!jwkString) {
441
+ throw new Error("Failed to create session key");
442
+ }
443
+ const jwk = JSON.parse(jwkString);
444
+ const spaceId = this.wasm.makeSpaceId(address, chainId, this.spacePrefix);
445
+ const now = /* @__PURE__ */ new Date();
446
+ const expirationTime = new Date(now.getTime() + this.sessionExpirationMs);
447
+ const prepared = this.wasm.prepareSession({
448
+ abilities: this.defaultActions,
449
+ address,
450
+ chainId,
451
+ domain: this.domain,
452
+ issuedAt: now.toISOString(),
453
+ expirationTime: expirationTime.toISOString(),
454
+ spaceId,
455
+ jwk
456
+ });
457
+ const signature = await this.requestSignature({
458
+ address,
459
+ chainId,
460
+ message: prepared.siwe,
461
+ type: "siwe"
462
+ });
463
+ const session = this.wasm.completeSessionSetup({
464
+ ...prepared,
465
+ signature
466
+ });
467
+ const clientSession = {
468
+ address,
469
+ walletAddress: address,
470
+ chainId,
471
+ sessionKey: keyId,
472
+ siwe: prepared.siwe,
473
+ signature
474
+ };
475
+ const spacesMetadata = this.enablePublicSpace ? { public: this.wasm.makeSpaceId(address, chainId, "public") } : void 0;
476
+ const tinyCloudSession = {
477
+ address,
478
+ chainId,
479
+ sessionKey: keyId,
480
+ spaceId,
481
+ spaces: spacesMetadata,
482
+ delegationCid: session.delegationCid,
483
+ delegationHeader: session.delegationHeader,
484
+ verificationMethod: this.sessionManager.getDID(keyId),
485
+ jwk,
486
+ siwe: prepared.siwe,
487
+ signature
488
+ };
489
+ const persistedData = {
490
+ address,
491
+ chainId,
492
+ sessionKey: JSON.stringify(jwk),
493
+ siwe: prepared.siwe,
494
+ signature,
495
+ tinycloudSession: {
496
+ delegationHeader: session.delegationHeader,
497
+ delegationCid: session.delegationCid,
498
+ spaceId,
499
+ spaces: spacesMetadata,
500
+ verificationMethod: this.sessionManager.getDID(keyId)
501
+ },
502
+ expiresAt: expirationTime.toISOString(),
503
+ createdAt: now.toISOString(),
504
+ version: "1.0"
505
+ };
506
+ await this.sessionStorage.save(address, persistedData);
507
+ this._session = clientSession;
508
+ this._tinyCloudSession = tinyCloudSession;
509
+ this._address = address;
510
+ this._chainId = chainId;
511
+ const nodeInfo = await checkNodeInfo(this.tinycloudHosts[0], this.wasm.protocolVersion());
512
+ this._nodeFeatures = nodeInfo.features;
513
+ for (const ext of this.extensions) {
514
+ if (ext.afterSignIn) {
515
+ await ext.afterSignIn(clientSession);
516
+ }
517
+ }
518
+ await this.ensureSpaceExists();
519
+ return clientSession;
520
+ }
521
+ /**
522
+ * Sign out and clear the current session.
523
+ */
524
+ async signOut() {
525
+ if (this._address) {
526
+ await this.clearPersistedSession(this._address);
527
+ }
528
+ this._session = void 0;
529
+ }
530
+ /**
531
+ * Get the current wallet/signer address.
532
+ */
533
+ address() {
534
+ return this._address;
535
+ }
536
+ /**
537
+ * Get the current chain ID.
538
+ */
539
+ chainId() {
540
+ return this._chainId;
541
+ }
542
+ /**
543
+ * Sign a message with the connected signer.
544
+ */
545
+ async signMessage(message) {
546
+ if (!this._address) {
547
+ this._address = await this.signer.getAddress();
548
+ }
549
+ if (!this._chainId) {
550
+ this._chainId = await this.signer.getChainId();
551
+ }
552
+ return this.requestSignature({
553
+ address: this._address,
554
+ chainId: this._chainId,
555
+ message,
556
+ type: "message"
557
+ });
558
+ }
559
+ /**
560
+ * Prepare a session for external signing.
561
+ *
562
+ * Use this method when you need to sign the SIWE message externally (e.g., via
563
+ * a hardware wallet, multi-sig, or external service). After obtaining the signature,
564
+ * call `signInWithPreparedSession()` to complete the sign-in.
565
+ *
566
+ * @example
567
+ * ```typescript
568
+ * const { prepared, keyId, jwk } = await auth.prepareSessionForSigning();
569
+ * const signature = await externalSigner.signMessage(prepared.siwe);
570
+ * const session = await auth.signInWithPreparedSession(prepared, signature, keyId, jwk);
571
+ * ```
572
+ */
573
+ async prepareSessionForSigning() {
574
+ const address = this.wasm.ensureEip55(await this.signer.getAddress());
575
+ const chainId = await this.signer.getChainId();
576
+ const keyId = `session-${Date.now()}`;
577
+ this.sessionManager.renameSessionKeyId("default", keyId);
578
+ const jwkString = this.sessionManager.jwk(keyId);
579
+ if (!jwkString) {
580
+ throw new Error("Failed to create session key");
581
+ }
582
+ const jwk = JSON.parse(jwkString);
583
+ const spaceId = this.wasm.makeSpaceId(address, chainId, this.spacePrefix);
584
+ const now = /* @__PURE__ */ new Date();
585
+ const expirationTime = new Date(now.getTime() + this.sessionExpirationMs);
586
+ const prepared = this.wasm.prepareSession({
587
+ abilities: this.defaultActions,
588
+ address,
589
+ chainId,
590
+ domain: this.domain,
591
+ issuedAt: now.toISOString(),
592
+ expirationTime: expirationTime.toISOString(),
593
+ spaceId,
594
+ jwk
595
+ });
596
+ return {
597
+ prepared,
598
+ keyId,
599
+ jwk,
600
+ address,
601
+ chainId
602
+ };
603
+ }
604
+ /**
605
+ * Complete sign-in with a prepared session and signature.
606
+ *
607
+ * Use this method after obtaining a signature for the SIWE message from
608
+ * `prepareSessionForSigning()`. The signature MUST be over `prepared.siwe`.
609
+ *
610
+ * @param prepared - The prepared session from `prepareSessionForSigning()`
611
+ * @param signature - The signature over `prepared.siwe`
612
+ * @param keyId - The session key ID from `prepareSessionForSigning()`
613
+ * @param jwk - The JWK from `prepareSessionForSigning()`
614
+ */
615
+ async signInWithPreparedSession(prepared, signature, keyId, jwk) {
616
+ const session = this.wasm.completeSessionSetup({
617
+ ...prepared,
618
+ signature
619
+ });
620
+ const address = this.wasm.ensureEip55(await this.signer.getAddress());
621
+ const chainId = await this.signer.getChainId();
622
+ const clientSession = {
623
+ address,
624
+ walletAddress: address,
625
+ chainId,
626
+ sessionKey: keyId,
627
+ siwe: prepared.siwe,
628
+ signature
629
+ };
630
+ const spacesMetadata = this.enablePublicSpace ? { public: this.wasm.makeSpaceId(address, chainId, "public") } : void 0;
631
+ const tinyCloudSession = {
632
+ address,
633
+ chainId,
634
+ sessionKey: keyId,
635
+ spaceId: prepared.spaceId,
636
+ spaces: spacesMetadata,
637
+ delegationCid: session.delegationCid,
638
+ delegationHeader: session.delegationHeader,
639
+ verificationMethod: this.sessionManager.getDID(keyId),
640
+ jwk,
641
+ siwe: prepared.siwe,
642
+ signature
643
+ };
644
+ const expirationMatch = prepared.siwe.match(/Expiration Time: (.+)/);
645
+ const issuedAtMatch = prepared.siwe.match(/Issued At: (.+)/);
646
+ const expiresAt = expirationMatch?.[1] ?? new Date(Date.now() + this.sessionExpirationMs).toISOString();
647
+ const createdAt = issuedAtMatch?.[1] ?? (/* @__PURE__ */ new Date()).toISOString();
648
+ const persistedData = {
649
+ address,
650
+ chainId,
651
+ sessionKey: JSON.stringify(jwk),
652
+ siwe: prepared.siwe,
653
+ signature,
654
+ tinycloudSession: {
655
+ delegationHeader: session.delegationHeader,
656
+ delegationCid: session.delegationCid,
657
+ spaceId: prepared.spaceId,
658
+ spaces: spacesMetadata,
659
+ verificationMethod: this.sessionManager.getDID(keyId)
660
+ },
661
+ expiresAt,
662
+ createdAt,
663
+ version: "1.0"
664
+ };
665
+ await this.sessionStorage.save(address, persistedData);
666
+ this._session = clientSession;
667
+ this._tinyCloudSession = tinyCloudSession;
668
+ this._address = address;
669
+ this._chainId = chainId;
670
+ const nodeInfo = await checkNodeInfo(this.tinycloudHosts[0], this.wasm.protocolVersion());
671
+ this._nodeFeatures = nodeInfo.features;
672
+ for (const ext of this.extensions) {
673
+ if (ext.afterSignIn) {
674
+ await ext.afterSignIn(clientSession);
675
+ }
676
+ }
677
+ await this.ensureSpaceExists();
678
+ return clientSession;
679
+ }
680
+ /**
681
+ * Clear persisted session data.
682
+ */
683
+ async clearPersistedSession(address) {
684
+ const targetAddress = address ?? this._address;
685
+ if (targetAddress) {
686
+ await this.sessionStorage.clear(targetAddress);
687
+ }
688
+ }
689
+ /**
690
+ * Check if a session is persisted for an address.
691
+ */
692
+ isSessionPersisted(address) {
693
+ return this.sessionStorage.exists(address);
694
+ }
695
+ /**
696
+ * Request a signature based on the configured strategy.
697
+ */
698
+ async requestSignature(request) {
699
+ switch (this.signStrategy.type) {
700
+ case "auto-sign":
701
+ return this.signer.signMessage(request.message);
702
+ case "auto-reject":
703
+ throw new Error("Sign request rejected by auto-reject strategy");
704
+ case "callback": {
705
+ const response = await this.signStrategy.handler(request);
706
+ if (!response.approved) {
707
+ throw new Error(
708
+ response.reason ?? "Sign request rejected by callback"
709
+ );
710
+ }
711
+ return response.signature ?? await this.signer.signMessage(request.message);
712
+ }
713
+ case "event-emitter": {
714
+ return this.requestSignatureViaEmitter(
715
+ request,
716
+ this.signStrategy.emitter,
717
+ this.signStrategy.timeout ?? 6e4
718
+ );
719
+ }
720
+ default:
721
+ throw new Error(`Unknown sign strategy: ${this.signStrategy.type}`);
722
+ }
723
+ }
724
+ /**
725
+ * Request signature via event emitter with timeout.
726
+ */
727
+ requestSignatureViaEmitter(request, emitter, timeout) {
728
+ return new Promise((resolve, reject) => {
729
+ const timeoutId = setTimeout(() => {
730
+ reject(new Error("Sign request timed out"));
731
+ }, timeout);
732
+ const respond = async (response) => {
733
+ clearTimeout(timeoutId);
734
+ if (!response.approved) {
735
+ reject(
736
+ new Error(response.reason ?? "Sign request rejected via emitter")
737
+ );
738
+ } else {
739
+ const signature = response.signature ?? await this.signer.signMessage(request.message);
740
+ resolve(signature);
741
+ }
742
+ };
743
+ emitter.emit("sign-request", request, respond);
744
+ });
745
+ }
746
+ };
747
+
748
+ // src/TinyCloudNode.ts
749
+ import {
750
+ TinyCloud,
751
+ activateSessionWithHost as activateSessionWithHost2,
752
+ KVService as KVService2,
753
+ SQLService as SQLService2,
754
+ DuckDbService as DuckDbService2,
755
+ DataVaultService,
756
+ createVaultCrypto,
757
+ ServiceContext as ServiceContext2,
758
+ SilentNotificationHandler,
759
+ DelegationManager,
760
+ SpaceService,
761
+ CapabilityKeyRegistry,
762
+ SharingService,
763
+ UnsupportedFeatureError,
764
+ makePublicSpaceId
765
+ } from "@tinycloud/sdk-core";
766
+
767
+ // src/DelegatedAccess.ts
768
+ import {
769
+ KVService,
770
+ SQLService,
771
+ DuckDbService,
772
+ ServiceContext
773
+ } from "@tinycloud/sdk-core";
774
+ var DelegatedAccess = class {
775
+ constructor(session, delegation, host, invoke) {
776
+ this.session = session;
777
+ this._delegation = delegation;
778
+ this.host = host;
779
+ this._serviceContext = new ServiceContext({
780
+ invoke,
781
+ fetch: globalThis.fetch.bind(globalThis),
782
+ hosts: [host]
783
+ });
784
+ const prefix = this._delegation.path.replace(/\/$/, "");
785
+ this._kv = new KVService({ prefix });
786
+ this._kv.initialize(this._serviceContext);
787
+ this._serviceContext.registerService("kv", this._kv);
788
+ this._sql = new SQLService({});
789
+ this._sql.initialize(this._serviceContext);
790
+ this._serviceContext.registerService("sql", this._sql);
791
+ this._duckdb = new DuckDbService({});
792
+ this._duckdb.initialize(this._serviceContext);
793
+ this._serviceContext.registerService("duckdb", this._duckdb);
794
+ const serviceSession = {
795
+ delegationHeader: session.delegationHeader,
796
+ delegationCid: session.delegationCid,
797
+ spaceId: session.spaceId,
798
+ verificationMethod: session.verificationMethod,
799
+ jwk: session.jwk
800
+ };
801
+ this._serviceContext.setSession(serviceSession);
802
+ }
803
+ /**
804
+ * Get the delegation this access was created from.
805
+ */
806
+ get delegation() {
807
+ return this._delegation;
808
+ }
809
+ /**
810
+ * The space ID this access is for.
811
+ */
812
+ get spaceId() {
813
+ return this._delegation.spaceId;
814
+ }
815
+ /**
816
+ * The path this access is scoped to.
817
+ */
818
+ get path() {
819
+ return this._delegation.path;
820
+ }
821
+ /**
822
+ * KV operations on the delegated space.
823
+ */
824
+ get kv() {
825
+ return this._kv;
826
+ }
827
+ /**
828
+ * SQL operations on the delegated space.
829
+ */
830
+ get sql() {
831
+ return this._sql;
832
+ }
833
+ /**
834
+ * DuckDB operations on the delegated space.
835
+ */
836
+ get duckdb() {
837
+ return this._duckdb;
838
+ }
839
+ };
840
+
841
+ // src/keys/WasmKeyProvider.ts
842
+ var WasmKeyProvider = class {
843
+ /**
844
+ * Create a new WasmKeyProvider.
845
+ *
846
+ * @param config - Configuration with the WASM session manager
847
+ */
848
+ constructor(config) {
849
+ this.sessionManager = config.sessionManager;
850
+ }
851
+ /**
852
+ * Generate a new session key with the given name.
853
+ *
854
+ * This creates a new Ed25519 key pair in the WASM session manager.
855
+ * The key can then be used for signing delegations in sharing links.
856
+ *
857
+ * @param name - A unique name/ID for the key (e.g., "share:timestamp:random")
858
+ * @returns The key ID (same as the name provided)
859
+ */
860
+ async createSessionKey(name) {
861
+ return this.sessionManager.createSessionKey(name);
862
+ }
863
+ /**
864
+ * Get the JWK (JSON Web Key) for a key.
865
+ *
866
+ * Returns the full JWK including the private key (d parameter),
867
+ * which is required for signing and for embedding in sharing links.
868
+ *
869
+ * @param keyId - The key ID to retrieve
870
+ * @returns The JWK object with public and private key components
871
+ * @throws Error if the key is not found
872
+ */
873
+ getJWK(keyId) {
874
+ const jwkJson = this.sessionManager.jwk(keyId);
875
+ if (!jwkJson) {
876
+ throw new Error(`Key not found: ${keyId}`);
877
+ }
878
+ return JSON.parse(jwkJson);
879
+ }
880
+ /**
881
+ * Get the DID (Decentralized Identifier) for a key.
882
+ *
883
+ * Returns the did:key format DID derived from the key's public key.
884
+ * This DID can be used as the delegatee in delegations.
885
+ *
886
+ * @param keyId - The key ID to retrieve
887
+ * @returns The DID in did:key format (e.g., "did:key:z6Mk...")
888
+ */
889
+ async getDID(keyId) {
890
+ return this.sessionManager.getDID(keyId);
891
+ }
892
+ /**
893
+ * List all session keys currently held by the provider.
894
+ *
895
+ * @returns Array of key IDs
896
+ */
897
+ listKeys() {
898
+ const keys = this.sessionManager.listSessionKeys?.();
899
+ return Array.isArray(keys) ? keys : [];
900
+ }
901
+ /**
902
+ * Check if a key exists in the provider.
903
+ *
904
+ * @param keyId - The key ID to check
905
+ * @returns True if the key exists
906
+ */
907
+ hasKey(keyId) {
908
+ const jwk = this.sessionManager.jwk(keyId);
909
+ return jwk !== void 0;
910
+ }
911
+ };
912
+ function createWasmKeyProvider(sessionManager) {
913
+ return new WasmKeyProvider({ sessionManager });
914
+ }
915
+
916
+ // src/TinyCloudNode.ts
917
+ var DEFAULT_HOST = "https://node.tinycloud.xyz";
918
+ var TinyCloudNode = class _TinyCloudNode {
919
+ /**
920
+ * Create a new TinyCloudNode instance.
921
+ *
922
+ * All configuration is optional. Without a privateKey, the instance operates
923
+ * in "session-only" mode where it can receive delegations but cannot create
924
+ * its own space via signIn().
925
+ *
926
+ * @param config - Configuration options (all optional)
927
+ *
928
+ * @example
929
+ * ```typescript
930
+ * // Session-only mode - can receive delegations
931
+ * const bob = new TinyCloudNode();
932
+ * console.log(bob.did); // did:key:z6Mk... - available immediately
933
+ *
934
+ * // Wallet mode - can create own space
935
+ * const alice = new TinyCloudNode({
936
+ * privateKey: process.env.ALICE_PRIVATE_KEY,
937
+ * prefix: "myapp",
938
+ * });
939
+ * await alice.signIn();
940
+ * ```
941
+ */
942
+ constructor(config = {}) {
943
+ this.signer = null;
944
+ this.auth = null;
945
+ this.tc = null;
946
+ this._chainId = 1;
947
+ this.config = {
948
+ ...config,
949
+ host: config.host ?? DEFAULT_HOST
950
+ };
951
+ if (config.wasmBindings) {
952
+ this.wasmBindings = config.wasmBindings;
953
+ } else if (_TinyCloudNode.nodeDefaults) {
954
+ this.wasmBindings = _TinyCloudNode.nodeDefaults.createWasmBindings();
955
+ } else {
956
+ throw new Error(
957
+ "wasmBindings must be provided in config. Import from '@tinycloud/node-sdk' (not '/core') for automatic Node.js defaults."
958
+ );
959
+ }
960
+ this.sessionManager = this.wasmBindings.createSessionManager();
961
+ const defaultKeyId = "default";
962
+ let jwkStr = this.sessionManager.jwk(defaultKeyId);
963
+ if (jwkStr) {
964
+ this.sessionKeyId = defaultKeyId;
965
+ } else {
966
+ this.sessionKeyId = this.sessionManager.createSessionKey(defaultKeyId);
967
+ jwkStr = this.sessionManager.jwk(this.sessionKeyId);
968
+ }
969
+ if (!jwkStr) {
970
+ throw new Error("Failed to get session key JWK");
971
+ }
972
+ this.sessionKeyJwk = JSON.parse(jwkStr);
973
+ this._capabilityRegistry = new CapabilityKeyRegistry();
974
+ this._keyProvider = new WasmKeyProvider({
975
+ sessionManager: this.sessionManager
976
+ });
977
+ this.notificationHandler = config.notificationHandler ?? new SilentNotificationHandler();
978
+ this._sharingService = new SharingService({
979
+ hosts: [this.config.host],
980
+ // session: undefined - not needed for receive()
981
+ invoke: this.wasmBindings.invoke,
982
+ fetch: globalThis.fetch.bind(globalThis),
983
+ keyProvider: this._keyProvider,
984
+ registry: this._capabilityRegistry,
985
+ // delegationManager: undefined - not needed for receive()
986
+ createKVService: (config2) => {
987
+ const prefix = config2.pathPrefix?.replace(/\/$/, "");
988
+ const kvService = new KVService2({ prefix });
989
+ const kvContext = new ServiceContext2({
990
+ invoke: config2.invoke,
991
+ fetch: config2.fetch ?? globalThis.fetch.bind(globalThis),
992
+ hosts: config2.hosts
993
+ });
994
+ kvContext.setSession(config2.session);
995
+ kvService.initialize(kvContext);
996
+ return kvService;
997
+ }
998
+ });
999
+ if (config.signer) {
1000
+ this.signer = config.signer;
1001
+ this.setupAuth(config);
1002
+ } else if (config.privateKey) {
1003
+ if (!_TinyCloudNode.nodeDefaults) {
1004
+ throw new Error(
1005
+ "privateKey requires PrivateKeySigner. Either provide a signer in config, or import from '@tinycloud/node-sdk' (not '/core') for automatic Node.js defaults."
1006
+ );
1007
+ }
1008
+ this.signer = _TinyCloudNode.nodeDefaults.createSigner(config.privateKey, this._chainId);
1009
+ this.setupAuth(config);
1010
+ }
1011
+ }
1012
+ /** @internal Register Node.js-specific defaults (NodeWasmBindings, PrivateKeySigner) */
1013
+ static registerNodeDefaults(defaults) {
1014
+ _TinyCloudNode.nodeDefaults = defaults;
1015
+ }
1016
+ get nodeFeatures() {
1017
+ return this.auth?.nodeFeatures ?? [];
1018
+ }
1019
+ /**
1020
+ * Set up authorization handler and TinyCloud instance.
1021
+ * @internal
1022
+ */
1023
+ setupAuth(config) {
1024
+ const host = this.config.host;
1025
+ const domain = config.domain ?? new URL(host).hostname;
1026
+ this.auth = new NodeUserAuthorization({
1027
+ signer: this.signer,
1028
+ signStrategy: { type: "auto-sign" },
1029
+ wasmBindings: this.wasmBindings,
1030
+ sessionStorage: config.sessionStorage ?? new MemorySessionStorage(),
1031
+ domain,
1032
+ spacePrefix: config.prefix,
1033
+ sessionExpirationMs: config.sessionExpirationMs ?? 60 * 60 * 1e3,
1034
+ tinycloudHosts: [host],
1035
+ autoCreateSpace: config.autoCreateSpace,
1036
+ enablePublicSpace: config.enablePublicSpace ?? true,
1037
+ spaceCreationHandler: config.spaceCreationHandler
1038
+ });
1039
+ this.tc = new TinyCloud(this.auth);
1040
+ }
1041
+ /**
1042
+ * Get the primary identity DID for this user.
1043
+ * - If wallet connected and signed in: returns PKH DID (did:pkh:eip155:{chainId}:{address})
1044
+ * - If session-only mode: returns session key DID (did:key:z6Mk...)
1045
+ *
1046
+ * Use this for delegations - it always returns the appropriate identity.
1047
+ */
1048
+ get did() {
1049
+ if (this._address) {
1050
+ return `did:pkh:eip155:${this._chainId}:${this._address}`;
1051
+ }
1052
+ return this.sessionManager.getDID(this.sessionKeyId);
1053
+ }
1054
+ /**
1055
+ * Get the session key DID. Always available.
1056
+ * Format: did:key:z6Mk...#z6Mk...
1057
+ *
1058
+ * Use this when you specifically need the session key, not the user identity.
1059
+ */
1060
+ get sessionDid() {
1061
+ return this.sessionManager.getDID(this.sessionKeyId);
1062
+ }
1063
+ /**
1064
+ * Get the Ethereum address for this user.
1065
+ */
1066
+ get address() {
1067
+ return this._address;
1068
+ }
1069
+ /**
1070
+ * Check if this instance is in session-only mode (no wallet).
1071
+ * In session-only mode, the instance can receive delegations but cannot
1072
+ * create its own space via signIn().
1073
+ */
1074
+ get isSessionOnly() {
1075
+ return this.signer === null;
1076
+ }
1077
+ /**
1078
+ * Get the space ID for this user.
1079
+ * Available after signIn().
1080
+ */
1081
+ get spaceId() {
1082
+ return this.auth?.tinyCloudSession?.spaceId;
1083
+ }
1084
+ /**
1085
+ * Get the current TinyCloud session.
1086
+ * Available after signIn().
1087
+ */
1088
+ get session() {
1089
+ return this.auth?.tinyCloudSession;
1090
+ }
1091
+ /**
1092
+ * Sign in and create a new session.
1093
+ * This creates the user's space if it doesn't exist.
1094
+ * Requires wallet mode (privateKey in config).
1095
+ */
1096
+ async signIn() {
1097
+ if (!this.signer || !this.tc) {
1098
+ throw new Error(
1099
+ "Cannot signIn() in session-only mode. Provide a privateKey in config to create your own space."
1100
+ );
1101
+ }
1102
+ await this.wasmBindings.ensureInitialized?.();
1103
+ this._address = await this.signer.getAddress();
1104
+ this._chainId = await this.signer.getChainId();
1105
+ this._kv = void 0;
1106
+ this._sql = void 0;
1107
+ this._duckdb = void 0;
1108
+ this._serviceContext = void 0;
1109
+ await this.tc.signIn();
1110
+ this.initializeServices();
1111
+ this.notificationHandler.success("Successfully signed in");
1112
+ }
1113
+ /**
1114
+ * Restore a previously established session from stored delegation data.
1115
+ *
1116
+ * This is used by the CLI to restore a session that was created via the
1117
+ * browser-based delegation flow (OpenKey `/delegate` page). Instead of
1118
+ * signing in with a private key, it injects the delegation data directly.
1119
+ *
1120
+ * @param sessionData - The stored delegation data from the browser flow
1121
+ */
1122
+ async restoreSession(sessionData) {
1123
+ await this.wasmBindings.ensureInitialized?.();
1124
+ this._kv = void 0;
1125
+ this._sql = void 0;
1126
+ this._duckdb = void 0;
1127
+ this._serviceContext = void 0;
1128
+ if (sessionData.address) {
1129
+ this._address = sessionData.address;
1130
+ }
1131
+ if (sessionData.chainId) {
1132
+ this._chainId = sessionData.chainId;
1133
+ }
1134
+ this._serviceContext = new ServiceContext2({
1135
+ invoke: this.wasmBindings.invoke,
1136
+ fetch: globalThis.fetch.bind(globalThis),
1137
+ hosts: [this.config.host]
1138
+ });
1139
+ this._kv = new KVService2({});
1140
+ this._kv.initialize(this._serviceContext);
1141
+ this._serviceContext.registerService("kv", this._kv);
1142
+ this._sql = new SQLService2({});
1143
+ this._sql.initialize(this._serviceContext);
1144
+ this._serviceContext.registerService("sql", this._sql);
1145
+ this._duckdb = new DuckDbService2({});
1146
+ this._duckdb.initialize(this._serviceContext);
1147
+ this._serviceContext.registerService("duckdb", this._duckdb);
1148
+ const serviceSession = {
1149
+ delegationHeader: sessionData.delegationHeader,
1150
+ delegationCid: sessionData.delegationCid,
1151
+ spaceId: sessionData.spaceId,
1152
+ verificationMethod: sessionData.verificationMethod,
1153
+ jwk: sessionData.jwk
1154
+ };
1155
+ this._serviceContext.setSession(serviceSession);
1156
+ const wasm = this.wasmBindings;
1157
+ const vaultCrypto = createVaultCrypto({
1158
+ vault_encrypt: wasm.vault_encrypt,
1159
+ vault_decrypt: wasm.vault_decrypt,
1160
+ vault_derive_key: wasm.vault_derive_key,
1161
+ vault_x25519_from_seed: wasm.vault_x25519_from_seed,
1162
+ vault_x25519_dh: wasm.vault_x25519_dh,
1163
+ vault_random_bytes: wasm.vault_random_bytes,
1164
+ vault_sha256: wasm.vault_sha256
1165
+ });
1166
+ const self = this;
1167
+ this._vault = new DataVaultService({
1168
+ spaceId: sessionData.spaceId,
1169
+ crypto: vaultCrypto,
1170
+ tc: {
1171
+ kv: this._kv,
1172
+ ensurePublicSpace: async () => {
1173
+ try {
1174
+ await self.ensurePublicSpace();
1175
+ return { ok: true, data: void 0 };
1176
+ } catch (error) {
1177
+ return { ok: false, error: { code: "STORAGE_ERROR", message: error instanceof Error ? error.message : String(error), service: "vault" } };
1178
+ }
1179
+ },
1180
+ get publicKV() {
1181
+ return self._publicKV ?? self.tc.publicKV;
1182
+ },
1183
+ readPublicSpace: (host, spaceId, key) => TinyCloud.readPublicSpace(host, spaceId, key),
1184
+ makePublicSpaceId: TinyCloud.makePublicSpaceId,
1185
+ did: this.did,
1186
+ address: sessionData.address ?? this._address ?? "",
1187
+ chainId: sessionData.chainId ?? this._chainId,
1188
+ hosts: [this.config.host]
1189
+ }
1190
+ });
1191
+ this._vault.initialize(this._serviceContext);
1192
+ this._serviceContext.registerService("vault", this._vault);
1193
+ this.initializeV2Services(serviceSession);
1194
+ }
1195
+ /**
1196
+ * Connect a wallet to upgrade from session-only mode to wallet mode.
1197
+ *
1198
+ * This allows a user who started in session-only mode to later connect
1199
+ * a wallet and gain the ability to create their own space.
1200
+ *
1201
+ * Note: This does NOT automatically sign in. Call signIn() after connecting
1202
+ * the wallet to create your space.
1203
+ *
1204
+ * @param privateKey - The Ethereum private key (hex string, no 0x prefix)
1205
+ * @param options - Optional configuration
1206
+ * @param options.prefix - Space name prefix (defaults to "default")
1207
+ *
1208
+ * @example
1209
+ * ```typescript
1210
+ * // Start in session-only mode
1211
+ * const node = new TinyCloudNode({ host: "https://node.tinycloud.xyz" });
1212
+ * console.log(node.did); // did:key:z6Mk... (session key)
1213
+ *
1214
+ * // Later, connect a wallet
1215
+ * node.connectWallet(privateKey);
1216
+ * await node.signIn();
1217
+ * console.log(node.did); // did:pkh:eip155:1:0x... (PKH)
1218
+ * ```
1219
+ */
1220
+ connectWallet(privateKey, options) {
1221
+ if (this.signer) {
1222
+ throw new Error("Wallet already connected. Cannot connect another wallet.");
1223
+ }
1224
+ const prefix = options?.prefix ?? "default";
1225
+ const host = this.config.host;
1226
+ const domain = new URL(host).hostname;
1227
+ if (!_TinyCloudNode.nodeDefaults) {
1228
+ throw new Error(
1229
+ "connectWallet() requires PrivateKeySigner. Use connectSigner() instead, or import from '@tinycloud/node-sdk' (not '/core') for automatic Node.js defaults."
1230
+ );
1231
+ }
1232
+ this.signer = _TinyCloudNode.nodeDefaults.createSigner(privateKey);
1233
+ this.auth = new NodeUserAuthorization({
1234
+ signer: this.signer,
1235
+ signStrategy: { type: "auto-sign" },
1236
+ wasmBindings: this.wasmBindings,
1237
+ sessionStorage: options?.sessionStorage ?? this.config.sessionStorage ?? new MemorySessionStorage(),
1238
+ domain,
1239
+ spacePrefix: prefix,
1240
+ sessionExpirationMs: this.config.sessionExpirationMs ?? 60 * 60 * 1e3,
1241
+ tinycloudHosts: [host],
1242
+ autoCreateSpace: this.config.autoCreateSpace,
1243
+ enablePublicSpace: this.config.enablePublicSpace ?? true,
1244
+ spaceCreationHandler: this.config.spaceCreationHandler
1245
+ });
1246
+ this.tc = new TinyCloud(this.auth);
1247
+ this.config.prefix = prefix;
1248
+ }
1249
+ /**
1250
+ * Connect any ISigner to upgrade from session-only mode to wallet mode.
1251
+ *
1252
+ * Same as connectWallet() but accepts any ISigner implementation instead
1253
+ * of a raw private key string. Use this for browser wallets, hardware wallets,
1254
+ * or custom signing backends.
1255
+ *
1256
+ * Note: This does NOT automatically sign in. Call signIn() after connecting.
1257
+ *
1258
+ * @param signer - Any ISigner implementation
1259
+ * @param options - Optional configuration
1260
+ * @param options.prefix - Space name prefix (defaults to "default")
1261
+ */
1262
+ connectSigner(signer, options) {
1263
+ if (this.signer) {
1264
+ throw new Error("Signer already connected. Cannot connect another signer.");
1265
+ }
1266
+ const prefix = options?.prefix ?? "default";
1267
+ const host = this.config.host;
1268
+ const domain = new URL(host).hostname;
1269
+ this.signer = signer;
1270
+ this.auth = new NodeUserAuthorization({
1271
+ signer: this.signer,
1272
+ signStrategy: { type: "auto-sign" },
1273
+ wasmBindings: this.wasmBindings,
1274
+ sessionStorage: options?.sessionStorage ?? this.config.sessionStorage ?? new MemorySessionStorage(),
1275
+ domain,
1276
+ spacePrefix: prefix,
1277
+ sessionExpirationMs: this.config.sessionExpirationMs ?? 60 * 60 * 1e3,
1278
+ tinycloudHosts: [host],
1279
+ autoCreateSpace: this.config.autoCreateSpace,
1280
+ enablePublicSpace: this.config.enablePublicSpace ?? true,
1281
+ spaceCreationHandler: this.config.spaceCreationHandler
1282
+ });
1283
+ this.tc = new TinyCloud(this.auth);
1284
+ this.config.prefix = prefix;
1285
+ }
1286
+ /**
1287
+ * Initialize the service context and KV service after sign-in.
1288
+ * @internal
1289
+ */
1290
+ initializeServices() {
1291
+ const session = this.auth?.tinyCloudSession;
1292
+ if (!session) {
1293
+ return;
1294
+ }
1295
+ this.tc.initializeServices(this.wasmBindings.invoke, [this.config.host]);
1296
+ this._serviceContext = new ServiceContext2({
1297
+ invoke: this.wasmBindings.invoke,
1298
+ fetch: globalThis.fetch.bind(globalThis),
1299
+ hosts: [this.config.host]
1300
+ });
1301
+ this._kv = new KVService2({});
1302
+ this._kv.initialize(this._serviceContext);
1303
+ this._serviceContext.registerService("kv", this._kv);
1304
+ const features = this.nodeFeatures;
1305
+ if (features.length === 0 || features.includes("sql")) {
1306
+ this._sql = new SQLService2({});
1307
+ this._sql.initialize(this._serviceContext);
1308
+ this._serviceContext.registerService("sql", this._sql);
1309
+ }
1310
+ if (features.length === 0 || features.includes("duckdb")) {
1311
+ this._duckdb = new DuckDbService2({});
1312
+ this._duckdb.initialize(this._serviceContext);
1313
+ this._serviceContext.registerService("duckdb", this._duckdb);
1314
+ }
1315
+ const serviceSession = {
1316
+ delegationHeader: session.delegationHeader,
1317
+ delegationCid: session.delegationCid,
1318
+ spaceId: session.spaceId,
1319
+ verificationMethod: session.verificationMethod,
1320
+ jwk: session.jwk
1321
+ };
1322
+ this._serviceContext.setSession(serviceSession);
1323
+ this.tc.serviceContext.setSession(serviceSession);
1324
+ const wasm = this.wasmBindings;
1325
+ const vaultCrypto = createVaultCrypto({
1326
+ vault_encrypt: wasm.vault_encrypt,
1327
+ vault_decrypt: wasm.vault_decrypt,
1328
+ vault_derive_key: wasm.vault_derive_key,
1329
+ vault_x25519_from_seed: wasm.vault_x25519_from_seed,
1330
+ vault_x25519_dh: wasm.vault_x25519_dh,
1331
+ vault_random_bytes: wasm.vault_random_bytes,
1332
+ vault_sha256: wasm.vault_sha256
1333
+ });
1334
+ const self = this;
1335
+ this._vault = new DataVaultService({
1336
+ spaceId: session.spaceId,
1337
+ crypto: vaultCrypto,
1338
+ tc: {
1339
+ kv: this._kv,
1340
+ ensurePublicSpace: async () => {
1341
+ try {
1342
+ await self.ensurePublicSpace();
1343
+ return { ok: true, data: void 0 };
1344
+ } catch (error) {
1345
+ return { ok: false, error: { code: "STORAGE_ERROR", message: error instanceof Error ? error.message : String(error), service: "vault" } };
1346
+ }
1347
+ },
1348
+ get publicKV() {
1349
+ return self._publicKV ?? self.tc.publicKV;
1350
+ },
1351
+ readPublicSpace: (host, spaceId, key) => TinyCloud.readPublicSpace(host, spaceId, key),
1352
+ makePublicSpaceId: TinyCloud.makePublicSpaceId,
1353
+ did: this.did,
1354
+ address: this._address,
1355
+ chainId: this._chainId,
1356
+ hosts: [this.config.host]
1357
+ }
1358
+ });
1359
+ this._vault.initialize(this._serviceContext);
1360
+ this._serviceContext.registerService("vault", this._vault);
1361
+ this.initializeV2Services(serviceSession);
1362
+ }
1363
+ /**
1364
+ * Initialize the v2 delegation system services.
1365
+ * @internal
1366
+ */
1367
+ initializeV2Services(serviceSession) {
1368
+ this._capabilityRegistry = new CapabilityKeyRegistry();
1369
+ const tcSession = this.auth?.tinyCloudSession;
1370
+ if (tcSession && this._address) {
1371
+ const sessionKey = {
1372
+ id: tcSession.sessionKey,
1373
+ did: tcSession.verificationMethod,
1374
+ type: "session",
1375
+ // Cast jwk from generic object to JWK - we know it has the required structure
1376
+ jwk: tcSession.jwk,
1377
+ priority: 0
1378
+ // Session keys have highest priority
1379
+ };
1380
+ const rootDelegation = {
1381
+ cid: tcSession.delegationCid,
1382
+ delegateDID: tcSession.verificationMethod,
1383
+ spaceId: tcSession.spaceId,
1384
+ path: "",
1385
+ // Root access
1386
+ actions: [
1387
+ "tinycloud.kv/put",
1388
+ "tinycloud.kv/get",
1389
+ "tinycloud.kv/del",
1390
+ "tinycloud.kv/list",
1391
+ "tinycloud.kv/metadata",
1392
+ "tinycloud.sql/read",
1393
+ "tinycloud.sql/write",
1394
+ "tinycloud.sql/admin",
1395
+ "tinycloud.sql/*",
1396
+ "tinycloud.duckdb/read",
1397
+ "tinycloud.duckdb/write",
1398
+ "tinycloud.duckdb/admin",
1399
+ "tinycloud.duckdb/describe",
1400
+ "tinycloud.duckdb/export",
1401
+ "tinycloud.duckdb/import",
1402
+ "tinycloud.duckdb/*"
1403
+ ],
1404
+ expiry: this.getSessionExpiry(),
1405
+ isRevoked: false,
1406
+ allowSubDelegation: true
1407
+ };
1408
+ const delegations = [rootDelegation];
1409
+ if (tcSession.spaces) {
1410
+ for (const [spaceName, spaceId] of Object.entries(tcSession.spaces)) {
1411
+ delegations.push({
1412
+ cid: tcSession.delegationCid,
1413
+ delegateDID: tcSession.verificationMethod,
1414
+ spaceId,
1415
+ path: "",
1416
+ actions: [
1417
+ "tinycloud.kv/put",
1418
+ "tinycloud.kv/get",
1419
+ "tinycloud.kv/del",
1420
+ "tinycloud.kv/list",
1421
+ "tinycloud.kv/metadata",
1422
+ "tinycloud.sql/read",
1423
+ "tinycloud.sql/write",
1424
+ "tinycloud.sql/admin",
1425
+ "tinycloud.sql/*",
1426
+ "tinycloud.duckdb/read",
1427
+ "tinycloud.duckdb/write",
1428
+ "tinycloud.duckdb/admin",
1429
+ "tinycloud.duckdb/describe",
1430
+ "tinycloud.duckdb/export",
1431
+ "tinycloud.duckdb/import",
1432
+ "tinycloud.duckdb/*"
1433
+ ],
1434
+ expiry: this.getSessionExpiry(),
1435
+ isRevoked: false,
1436
+ allowSubDelegation: true
1437
+ });
1438
+ }
1439
+ }
1440
+ this._capabilityRegistry.registerKey(sessionKey, delegations);
1441
+ }
1442
+ this._delegationManager = new DelegationManager({
1443
+ hosts: [this.config.host],
1444
+ session: serviceSession,
1445
+ invoke: this.wasmBindings.invoke,
1446
+ fetch: globalThis.fetch.bind(globalThis)
1447
+ });
1448
+ this._spaceService = new SpaceService({
1449
+ hosts: [this.config.host],
1450
+ session: serviceSession,
1451
+ invoke: this.wasmBindings.invoke,
1452
+ fetch: globalThis.fetch.bind(globalThis),
1453
+ capabilityRegistry: this._capabilityRegistry,
1454
+ userDid: this.did,
1455
+ createKVService: (spaceId) => {
1456
+ const kvService = new KVService2({});
1457
+ if (this._serviceContext) {
1458
+ const spaceScopedContext = new ServiceContext2({
1459
+ invoke: this._serviceContext.invoke,
1460
+ fetch: this._serviceContext.fetch,
1461
+ hosts: this._serviceContext.hosts
1462
+ });
1463
+ const session = this._serviceContext.session;
1464
+ if (session) {
1465
+ spaceScopedContext.setSession({ ...session, spaceId });
1466
+ }
1467
+ kvService.initialize(spaceScopedContext);
1468
+ }
1469
+ return kvService;
1470
+ },
1471
+ // Enable space.delegations.create() via SIWE-based delegation
1472
+ createDelegation: async (params) => {
1473
+ try {
1474
+ const portableDelegation = await this.createDelegation({
1475
+ delegateDID: params.delegateDID,
1476
+ path: params.path,
1477
+ actions: params.actions,
1478
+ disableSubDelegation: params.disableSubDelegation,
1479
+ expiryMs: params.expiry ? params.expiry.getTime() - Date.now() : void 0
1480
+ });
1481
+ const delegation = {
1482
+ cid: portableDelegation.cid,
1483
+ delegateDID: portableDelegation.delegateDID,
1484
+ delegatorDID: this.did,
1485
+ spaceId: portableDelegation.spaceId,
1486
+ path: portableDelegation.path,
1487
+ actions: portableDelegation.actions,
1488
+ expiry: portableDelegation.expiry,
1489
+ isRevoked: false,
1490
+ allowSubDelegation: !portableDelegation.disableSubDelegation,
1491
+ createdAt: /* @__PURE__ */ new Date(),
1492
+ authHeader: portableDelegation.delegationHeader.Authorization
1493
+ };
1494
+ return { ok: true, data: delegation };
1495
+ } catch (error) {
1496
+ return {
1497
+ ok: false,
1498
+ error: {
1499
+ code: "CREATION_FAILED",
1500
+ message: error instanceof Error ? error.message : String(error),
1501
+ service: "delegation"
1502
+ }
1503
+ };
1504
+ }
1505
+ }
1506
+ });
1507
+ this._sharingService.updateConfig({
1508
+ session: serviceSession,
1509
+ delegationManager: this._delegationManager,
1510
+ sessionExpiry: this.getSessionExpiry(),
1511
+ // WASM-based delegation creation (preferred - no server roundtrip)
1512
+ createDelegationWasm: (params) => this.createDelegationWrapper(params),
1513
+ // Root delegation for long-lived share links (bypasses session expiry)
1514
+ // In node-sdk we have direct signer access, so no popup needed
1515
+ onRootDelegationNeeded: this.signer ? async (params) => this.createRootDelegationForSharing(params) : void 0
1516
+ });
1517
+ this._spaceService.updateConfig({
1518
+ sharingService: this._sharingService
1519
+ });
1520
+ }
1521
+ /**
1522
+ * Get the session expiry time.
1523
+ * @internal
1524
+ */
1525
+ getSessionExpiry() {
1526
+ const expirationMs = this.config.sessionExpirationMs ?? 60 * 60 * 1e3;
1527
+ return new Date(Date.now() + expirationMs);
1528
+ }
1529
+ /**
1530
+ * Wrapper for the WASM createDelegation function.
1531
+ * Adapts the WASM interface to what SharingService expects.
1532
+ * @internal
1533
+ */
1534
+ createDelegationWrapper(params) {
1535
+ const wasmSession = {
1536
+ delegationHeader: params.session.delegationHeader,
1537
+ delegationCid: params.session.delegationCid,
1538
+ jwk: params.session.jwk,
1539
+ spaceId: params.session.spaceId,
1540
+ verificationMethod: params.session.verificationMethod
1541
+ };
1542
+ const result = this.wasmBindings.createDelegation(
1543
+ wasmSession,
1544
+ params.delegateDID,
1545
+ params.spaceId,
1546
+ params.path,
1547
+ params.actions,
1548
+ params.expirationSecs,
1549
+ params.notBeforeSecs
1550
+ );
1551
+ return {
1552
+ delegation: result.delegation,
1553
+ cid: result.cid,
1554
+ delegateDID: result.delegateDid,
1555
+ path: result.path,
1556
+ actions: result.actions,
1557
+ expiry: new Date(result.expiry * 1e3)
1558
+ };
1559
+ }
1560
+ /**
1561
+ * Create a direct root delegation from the wallet to a share key.
1562
+ * This bypasses the session delegation chain, allowing share links
1563
+ * with expiry longer than the current session.
1564
+ * @internal
1565
+ */
1566
+ async createRootDelegationForSharing(params) {
1567
+ if (!this.signer) {
1568
+ return void 0;
1569
+ }
1570
+ const session = this.auth?.tinyCloudSession;
1571
+ if (!session) {
1572
+ return void 0;
1573
+ }
1574
+ try {
1575
+ const host = this.config.host;
1576
+ const now = /* @__PURE__ */ new Date();
1577
+ const abilities = {
1578
+ kv: {
1579
+ [params.path]: params.actions
1580
+ }
1581
+ };
1582
+ const prepared = this.wasmBindings.prepareSession({
1583
+ abilities,
1584
+ address: this.wasmBindings.ensureEip55(session.address),
1585
+ chainId: session.chainId,
1586
+ domain: new URL(host).hostname,
1587
+ issuedAt: now.toISOString(),
1588
+ expirationTime: params.requestedExpiry.toISOString(),
1589
+ spaceId: params.spaceId,
1590
+ delegateUri: params.shareKeyDID
1591
+ });
1592
+ const signature = await this.signer.signMessage(prepared.siwe);
1593
+ const delegationSession = this.wasmBindings.completeSessionSetup({
1594
+ ...prepared,
1595
+ signature
1596
+ });
1597
+ const activateResult = await activateSessionWithHost2(
1598
+ host,
1599
+ delegationSession.delegationHeader
1600
+ );
1601
+ if (!activateResult.success) {
1602
+ return void 0;
1603
+ }
1604
+ return {
1605
+ cid: delegationSession.delegationCid,
1606
+ delegateDID: params.shareKeyDID,
1607
+ delegatorDID: `did:pkh:eip155:${session.chainId}:${session.address}`,
1608
+ spaceId: params.spaceId,
1609
+ path: params.path,
1610
+ actions: params.actions,
1611
+ expiry: params.requestedExpiry,
1612
+ isRevoked: false,
1613
+ allowSubDelegation: true,
1614
+ createdAt: now,
1615
+ authHeader: delegationSession.delegationHeader.Authorization
1616
+ };
1617
+ } catch {
1618
+ return void 0;
1619
+ }
1620
+ }
1621
+ /**
1622
+ * Track a received delegation in the capability registry.
1623
+ * @internal
1624
+ */
1625
+ trackReceivedDelegation(delegation, jwk) {
1626
+ if (!this._capabilityRegistry) {
1627
+ return;
1628
+ }
1629
+ const keyInfo = {
1630
+ id: `received:${delegation.cid}`,
1631
+ did: this.sessionDid,
1632
+ type: "ingested",
1633
+ jwk,
1634
+ priority: 2
1635
+ };
1636
+ const delegationRecord = {
1637
+ cid: delegation.cid,
1638
+ delegateDID: delegation.delegateDID,
1639
+ spaceId: delegation.spaceId,
1640
+ path: delegation.path,
1641
+ actions: delegation.actions,
1642
+ expiry: delegation.expiry,
1643
+ isRevoked: false,
1644
+ allowSubDelegation: !delegation.disableSubDelegation
1645
+ };
1646
+ this._capabilityRegistry.ingestKey(keyInfo, delegationRecord);
1647
+ }
1648
+ /**
1649
+ * Key-value storage operations on this user's space.
1650
+ */
1651
+ get kv() {
1652
+ if (!this._kv) {
1653
+ throw new Error("Not signed in. Call signIn() first.");
1654
+ }
1655
+ return this._kv;
1656
+ }
1657
+ /**
1658
+ * SQL database operations on this user's space.
1659
+ */
1660
+ get sql() {
1661
+ if (!this._sql) {
1662
+ const features = this.nodeFeatures;
1663
+ if (features.length > 0 && !features.includes("sql")) {
1664
+ throw new UnsupportedFeatureError("sql", this.config.host, features);
1665
+ }
1666
+ throw new Error("Not signed in. Call signIn() first.");
1667
+ }
1668
+ return this._sql;
1669
+ }
1670
+ /**
1671
+ * DuckDB database operations on this user's space.
1672
+ */
1673
+ get duckdb() {
1674
+ if (!this._duckdb) {
1675
+ const features = this.nodeFeatures;
1676
+ if (features.length > 0 && !features.includes("duckdb")) {
1677
+ throw new UnsupportedFeatureError("duckdb", this.config.host, features);
1678
+ }
1679
+ throw new Error("Not signed in. Call signIn() first.");
1680
+ }
1681
+ return this._duckdb;
1682
+ }
1683
+ /**
1684
+ * Data Vault operations - client-side encrypted KV storage.
1685
+ * Call `vault.unlock(signer)` after signIn() to derive encryption keys.
1686
+ */
1687
+ get vault() {
1688
+ if (!this._vault) {
1689
+ throw new Error("Not signed in. Call signIn() first.");
1690
+ }
1691
+ return this._vault;
1692
+ }
1693
+ // ===========================================================================
1694
+ // v2 Service Accessors
1695
+ // ===========================================================================
1696
+ /**
1697
+ * Get the CapabilityKeyRegistry for managing keys and their capabilities.
1698
+ *
1699
+ * The registry tracks keys (session, main, ingested) and their associated
1700
+ * delegations, enabling automatic key selection for operations.
1701
+ *
1702
+ * @example
1703
+ * ```typescript
1704
+ * const registry = alice.capabilityRegistry;
1705
+ *
1706
+ * // Get the best key for an operation
1707
+ * const key = registry.getKeyForCapability(
1708
+ * "tinycloud://my-space/kv/data",
1709
+ * "tinycloud.kv/get"
1710
+ * );
1711
+ *
1712
+ * // List all capabilities
1713
+ * const capabilities = registry.getAllCapabilities();
1714
+ * ```
1715
+ */
1716
+ get capabilityRegistry() {
1717
+ if (!this._capabilityRegistry) {
1718
+ throw new Error("CapabilityKeyRegistry not initialized.");
1719
+ }
1720
+ return this._capabilityRegistry;
1721
+ }
1722
+ /**
1723
+ * Access received delegations (recipient view).
1724
+ *
1725
+ * Use this to see what delegations have been received via useDelegation().
1726
+ *
1727
+ * @example
1728
+ * ```typescript
1729
+ * // List all received delegations
1730
+ * const received = bob.delegations.list();
1731
+ * console.log("I have access to:", received.length, "spaces");
1732
+ *
1733
+ * // Get a specific delegation by CID
1734
+ * const delegation = bob.delegations.get(cid);
1735
+ * ```
1736
+ */
1737
+ get delegations() {
1738
+ const registry = this._capabilityRegistry;
1739
+ if (!registry) {
1740
+ return {
1741
+ list: () => [],
1742
+ get: () => void 0
1743
+ };
1744
+ }
1745
+ return {
1746
+ list: () => registry.getAllCapabilities().map((entry) => entry.delegation),
1747
+ get: (cid) => {
1748
+ const capabilities = registry.getAllCapabilities();
1749
+ const entry = capabilities.find((e) => e.delegation.cid === cid);
1750
+ return entry?.delegation;
1751
+ }
1752
+ };
1753
+ }
1754
+ /**
1755
+ * Get the DelegationManager for delegation CRUD operations.
1756
+ *
1757
+ * This is the v2 delegation service providing a cleaner API than
1758
+ * the legacy createDelegation/useDelegation methods.
1759
+ *
1760
+ * @example
1761
+ * ```typescript
1762
+ * const delegations = alice.delegationManager;
1763
+ *
1764
+ * // Create a delegation
1765
+ * const result = await delegations.create({
1766
+ * delegateDID: bob.did,
1767
+ * path: "shared/",
1768
+ * actions: ["tinycloud.kv/get", "tinycloud.kv/put"],
1769
+ * expiry: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 hours
1770
+ * });
1771
+ *
1772
+ * // List delegations
1773
+ * const listResult = await delegations.list();
1774
+ *
1775
+ * // Revoke a delegation
1776
+ * await delegations.revoke(delegationCid);
1777
+ * ```
1778
+ */
1779
+ get delegationManager() {
1780
+ if (!this._delegationManager) {
1781
+ throw new Error("Not signed in. Call signIn() first.");
1782
+ }
1783
+ return this._delegationManager;
1784
+ }
1785
+ /**
1786
+ * Get the SpaceService for managing spaces.
1787
+ *
1788
+ * The SpaceService provides access to owned and delegated spaces,
1789
+ * including space creation, listing, and scoped operations.
1790
+ *
1791
+ * @example
1792
+ * ```typescript
1793
+ * const spaces = alice.spaces;
1794
+ *
1795
+ * // List all accessible spaces
1796
+ * const result = await spaces.list();
1797
+ *
1798
+ * // Create a new space
1799
+ * const createResult = await spaces.create('photos');
1800
+ *
1801
+ * // Get a space object for operations
1802
+ * const mySpace = spaces.get('default');
1803
+ * await mySpace.kv.put('key', 'value');
1804
+ *
1805
+ * // Check if a space exists
1806
+ * const exists = await spaces.exists('photos');
1807
+ * ```
1808
+ */
1809
+ get spaces() {
1810
+ if (!this._spaceService) {
1811
+ throw new Error("Not signed in. Call signIn() first.");
1812
+ }
1813
+ return this._spaceService;
1814
+ }
1815
+ /**
1816
+ * Alias for `spaces` - get the SpaceService.
1817
+ * @see spaces
1818
+ */
1819
+ get spaceService() {
1820
+ return this.spaces;
1821
+ }
1822
+ /**
1823
+ * Get the SharingService for creating and receiving v2 sharing links.
1824
+ *
1825
+ * The SharingService creates sharing links with embedded private keys,
1826
+ * allowing recipients to exercise delegations without prior session setup.
1827
+ *
1828
+ * @example
1829
+ * ```typescript
1830
+ * const sharing = alice.sharing;
1831
+ *
1832
+ * // Generate a sharing link
1833
+ * const result = await sharing.generate({
1834
+ * path: "/kv/documents/report.pdf",
1835
+ * actions: ["tinycloud.kv/get"],
1836
+ * expiry: new Date(Date.now() + 24 * 60 * 60 * 1000),
1837
+ * });
1838
+ *
1839
+ * if (result.ok) {
1840
+ * console.log("Share URL:", result.data.url);
1841
+ * // Send the URL to the recipient
1842
+ * }
1843
+ *
1844
+ * // Receive a sharing link
1845
+ * const receiveResult = await sharing.receive(shareUrl);
1846
+ * if (receiveResult.ok) {
1847
+ * // Use the pre-configured KV service
1848
+ * const data = await receiveResult.data.kv.get("report.pdf");
1849
+ * }
1850
+ * ```
1851
+ */
1852
+ get sharing() {
1853
+ return this._sharingService;
1854
+ }
1855
+ /**
1856
+ * Alias for `sharing` - get the SharingService.
1857
+ * @see sharing
1858
+ */
1859
+ get sharingService() {
1860
+ return this.sharing;
1861
+ }
1862
+ // ===========================================================================
1863
+ // Public Space Methods
1864
+ // ===========================================================================
1865
+ /**
1866
+ * Ensure the user's public space exists and is accessible.
1867
+ * Creates the space and activates a session delegation for it.
1868
+ * This is the trigger for lazy public space creation — call it
1869
+ * before writing to spaces.get('public').kv.
1870
+ */
1871
+ async ensurePublicSpace() {
1872
+ if (!this.auth || !this.session || !this.signer) {
1873
+ throw new Error("Not signed in. Call signIn() first.");
1874
+ }
1875
+ const publicSpaceId = this.session.spaces?.public;
1876
+ if (!publicSpaceId) {
1877
+ throw new Error("Public space not enabled. Set enablePublicSpace: true in config.");
1878
+ }
1879
+ await this.auth.hostPublicSpace(publicSpaceId);
1880
+ const kvActions = [
1881
+ "tinycloud.kv/put",
1882
+ "tinycloud.kv/get",
1883
+ "tinycloud.kv/del",
1884
+ "tinycloud.kv/list",
1885
+ "tinycloud.kv/metadata"
1886
+ ];
1887
+ const abilities = { kv: { "": kvActions } };
1888
+ const now = /* @__PURE__ */ new Date();
1889
+ const expiryMs = 60 * 60 * 1e3;
1890
+ const expirationTime = new Date(now.getTime() + expiryMs);
1891
+ const prepared = this.wasmBindings.prepareSession({
1892
+ abilities,
1893
+ address: this.wasmBindings.ensureEip55(this.session.address),
1894
+ chainId: this.session.chainId,
1895
+ domain: new URL(this.config.host).hostname,
1896
+ issuedAt: now.toISOString(),
1897
+ expirationTime: expirationTime.toISOString(),
1898
+ spaceId: publicSpaceId,
1899
+ jwk: this.session.jwk,
1900
+ parents: [this.session.delegationCid]
1901
+ });
1902
+ const signature = await this.signer.signMessage(prepared.siwe);
1903
+ const delegationSession = this.wasmBindings.completeSessionSetup({
1904
+ ...prepared,
1905
+ signature
1906
+ });
1907
+ const activateResult = await activateSessionWithHost2(
1908
+ this.config.host,
1909
+ delegationSession.delegationHeader
1910
+ );
1911
+ if (!activateResult.success) {
1912
+ throw new Error(`Failed to activate public space delegation: ${activateResult.error}`);
1913
+ }
1914
+ if (this._capabilityRegistry && this.session) {
1915
+ const sessionKey = {
1916
+ id: this.session.sessionKey,
1917
+ did: this.session.verificationMethod,
1918
+ type: "session",
1919
+ jwk: this.session.jwk,
1920
+ priority: 0
1921
+ };
1922
+ this._capabilityRegistry.registerKey(sessionKey, [{
1923
+ cid: delegationSession.delegationCid,
1924
+ delegateDID: this.session.verificationMethod,
1925
+ spaceId: publicSpaceId,
1926
+ path: "",
1927
+ actions: kvActions,
1928
+ expiry: expirationTime,
1929
+ isRevoked: false,
1930
+ allowSubDelegation: true
1931
+ }]);
1932
+ }
1933
+ if (this._serviceContext) {
1934
+ const publicKV = new KVService2({ prefix: "" });
1935
+ const publicContext = new ServiceContext2({
1936
+ invoke: this.wasmBindings.invoke,
1937
+ fetch: this._serviceContext.fetch,
1938
+ hosts: this._serviceContext.hosts
1939
+ });
1940
+ publicContext.setSession({
1941
+ delegationHeader: delegationSession.delegationHeader,
1942
+ delegationCid: delegationSession.delegationCid,
1943
+ spaceId: publicSpaceId,
1944
+ verificationMethod: this.session.verificationMethod,
1945
+ jwk: this.session.jwk
1946
+ });
1947
+ publicKV.initialize(publicContext);
1948
+ this._publicKV = publicKV;
1949
+ }
1950
+ }
1951
+ /**
1952
+ * Get a KVService scoped to the user's own public space.
1953
+ * Writes require authentication (owner/delegate).
1954
+ */
1955
+ get publicKV() {
1956
+ if (this._publicKV) {
1957
+ return this._publicKV;
1958
+ }
1959
+ if (!this.tc) {
1960
+ throw new Error("Not signed in. Call signIn() first.");
1961
+ }
1962
+ return this.tc.publicKV;
1963
+ }
1964
+ // ===========================================================================
1965
+ // v2 Delegation Convenience Methods
1966
+ // ===========================================================================
1967
+ /**
1968
+ * Create a delegation using the v2 DelegationManager.
1969
+ *
1970
+ * This is a convenience method that wraps DelegationManager.create().
1971
+ * For more control, use `this.delegationManager` directly.
1972
+ *
1973
+ * @param params - Delegation parameters
1974
+ * @returns Result containing the created Delegation
1975
+ *
1976
+ * @example
1977
+ * ```typescript
1978
+ * const result = await alice.delegate({
1979
+ * delegateDID: bob.did,
1980
+ * path: "shared/",
1981
+ * actions: ["tinycloud.kv/get", "tinycloud.kv/put"],
1982
+ * expiry: new Date(Date.now() + 24 * 60 * 60 * 1000),
1983
+ * });
1984
+ *
1985
+ * if (result.ok) {
1986
+ * console.log("Delegation created:", result.data.cid);
1987
+ * }
1988
+ * ```
1989
+ */
1990
+ async delegate(params) {
1991
+ return this.delegationManager.create(params);
1992
+ }
1993
+ /**
1994
+ * Revoke a delegation using the v2 DelegationManager.
1995
+ *
1996
+ * @param cid - The CID of the delegation to revoke
1997
+ * @returns Result indicating success or failure
1998
+ */
1999
+ async revokeDelegation(cid) {
2000
+ return this.delegationManager.revoke(cid);
2001
+ }
2002
+ /**
2003
+ * List all delegations for the current session's space.
2004
+ *
2005
+ * @returns Result containing an array of Delegations
2006
+ */
2007
+ async listDelegations() {
2008
+ return this.delegationManager.list();
2009
+ }
2010
+ /**
2011
+ * Check if the current session has permission for a path and action.
2012
+ *
2013
+ * @param path - The resource path to check
2014
+ * @param action - The action to check (e.g., "tinycloud.kv/get")
2015
+ * @returns Result containing boolean permission status
2016
+ */
2017
+ async checkPermission(path, action) {
2018
+ return this.delegationManager.checkPermission(path, action);
2019
+ }
2020
+ /**
2021
+ * Create a delegation from this user to another user.
2022
+ *
2023
+ * The delegation grants the recipient access to a specific path and actions
2024
+ * within this user's space.
2025
+ *
2026
+ * @param params - Delegation parameters
2027
+ * @returns A portable delegation that can be sent to the recipient
2028
+ */
2029
+ async createDelegation(params) {
2030
+ if (!this.signer) {
2031
+ throw new Error("Cannot createDelegation() in session-only mode. Requires wallet mode.");
2032
+ }
2033
+ const session = this.auth?.tinyCloudSession;
2034
+ if (!session) {
2035
+ throw new Error("Not signed in. Call signIn() first.");
2036
+ }
2037
+ if (params.delegateDID.endsWith(".eth") && this.config.ensResolver) {
2038
+ const address = await this.config.ensResolver.resolveAddress(params.delegateDID);
2039
+ if (!address) throw new Error(`Could not resolve ENS name: ${params.delegateDID}`);
2040
+ params = { ...params, delegateDID: `did:pkh:eip155:1:${address}` };
2041
+ }
2042
+ const abilities = {};
2043
+ const kvActions = params.actions.filter((a) => a.startsWith("tinycloud.kv/"));
2044
+ const sqlActions = params.actions.filter((a) => a.startsWith("tinycloud.sql/"));
2045
+ const duckdbActions = params.actions.filter((a) => a.startsWith("tinycloud.duckdb/"));
2046
+ if (kvActions.length > 0) {
2047
+ abilities.kv = { [params.path]: kvActions };
2048
+ }
2049
+ if (sqlActions.length > 0) {
2050
+ abilities.sql = { [params.path]: sqlActions };
2051
+ }
2052
+ if (duckdbActions.length > 0) {
2053
+ abilities.duckdb = { [params.path]: duckdbActions };
2054
+ }
2055
+ const now = /* @__PURE__ */ new Date();
2056
+ const expiryMs = params.expiryMs ?? 60 * 60 * 1e3;
2057
+ const expirationTime = new Date(now.getTime() + expiryMs);
2058
+ const prepared = this.wasmBindings.prepareSession({
2059
+ abilities,
2060
+ address: this.wasmBindings.ensureEip55(session.address),
2061
+ chainId: session.chainId,
2062
+ domain: new URL(this.config.host).hostname,
2063
+ issuedAt: now.toISOString(),
2064
+ expirationTime: expirationTime.toISOString(),
2065
+ spaceId: params.spaceIdOverride ?? session.spaceId,
2066
+ delegateUri: params.delegateDID,
2067
+ parents: [session.delegationCid]
2068
+ });
2069
+ const signature = await this.signer.signMessage(prepared.siwe);
2070
+ const delegationSession = this.wasmBindings.completeSessionSetup({
2071
+ ...prepared,
2072
+ signature
2073
+ });
2074
+ const activateResult = await activateSessionWithHost2(
2075
+ this.config.host,
2076
+ delegationSession.delegationHeader
2077
+ );
2078
+ if (!activateResult.success) {
2079
+ throw new Error(`Failed to activate delegation: ${activateResult.error}`);
2080
+ }
2081
+ const result = {
2082
+ cid: delegationSession.delegationCid,
2083
+ delegationHeader: delegationSession.delegationHeader,
2084
+ spaceId: params.spaceIdOverride ?? session.spaceId,
2085
+ path: params.path,
2086
+ actions: params.actions,
2087
+ disableSubDelegation: params.disableSubDelegation ?? false,
2088
+ expiry: expirationTime,
2089
+ delegateDID: params.delegateDID,
2090
+ ownerAddress: session.address,
2091
+ chainId: session.chainId,
2092
+ host: this.config.host
2093
+ };
2094
+ const hasKvActions = params.actions.some((a) => a.startsWith("tinycloud.kv/"));
2095
+ if (hasKvActions && params.includePublicSpace !== false) {
2096
+ const publicSpaceId = makePublicSpaceId(
2097
+ this.wasmBindings.ensureEip55(session.address),
2098
+ session.chainId
2099
+ );
2100
+ const publicAbilities = {
2101
+ kv: { "": ["tinycloud.kv/get", "tinycloud.kv/put", "tinycloud.kv/metadata"] }
2102
+ };
2103
+ const publicPrepared = this.wasmBindings.prepareSession({
2104
+ abilities: publicAbilities,
2105
+ address: this.wasmBindings.ensureEip55(session.address),
2106
+ chainId: session.chainId,
2107
+ domain: new URL(this.config.host).hostname,
2108
+ issuedAt: now.toISOString(),
2109
+ expirationTime: expirationTime.toISOString(),
2110
+ spaceId: publicSpaceId,
2111
+ delegateUri: params.delegateDID,
2112
+ parents: [session.delegationCid]
2113
+ });
2114
+ const publicSignature = await this.signer.signMessage(publicPrepared.siwe);
2115
+ const publicSession = this.wasmBindings.completeSessionSetup({
2116
+ ...publicPrepared,
2117
+ signature: publicSignature
2118
+ });
2119
+ const publicActivateResult = await activateSessionWithHost2(
2120
+ this.config.host,
2121
+ publicSession.delegationHeader
2122
+ );
2123
+ if (publicActivateResult.success) {
2124
+ result.publicDelegation = {
2125
+ cid: publicSession.delegationCid,
2126
+ delegationHeader: publicSession.delegationHeader,
2127
+ spaceId: publicSpaceId,
2128
+ path: "",
2129
+ actions: ["tinycloud.kv/get", "tinycloud.kv/put", "tinycloud.kv/metadata"],
2130
+ disableSubDelegation: params.disableSubDelegation ?? false,
2131
+ expiry: expirationTime,
2132
+ delegateDID: params.delegateDID,
2133
+ ownerAddress: session.address,
2134
+ chainId: session.chainId,
2135
+ host: this.config.host
2136
+ };
2137
+ }
2138
+ }
2139
+ return result;
2140
+ }
2141
+ /**
2142
+ * Use a delegation received from another user.
2143
+ *
2144
+ * This creates a new session key for this user that chains from the
2145
+ * received delegation, allowing operations on the delegator's space.
2146
+ *
2147
+ * Works in both modes:
2148
+ * - **Wallet mode**: Creates a SIWE sub-delegation from PKH to session key
2149
+ * - **Session-only mode**: Uses the delegation directly (must target session key DID)
2150
+ *
2151
+ * @param delegation - The PortableDelegation to use (from createDelegation or transport)
2152
+ * @returns A DelegatedAccess instance for performing operations
2153
+ */
2154
+ async useDelegation(delegation) {
2155
+ const delegationHeader = delegation.delegationHeader;
2156
+ const targetHost = delegation.host ?? this.config.host;
2157
+ if (this.isSessionOnly) {
2158
+ const myDid = this.did;
2159
+ if (delegation.delegateDID !== myDid) {
2160
+ throw new Error(
2161
+ `Delegation targets ${delegation.delegateDID} but this user's DID is ${myDid}. The delegation must target this user's DID.`
2162
+ );
2163
+ }
2164
+ const session2 = {
2165
+ address: delegation.ownerAddress,
2166
+ chainId: delegation.chainId,
2167
+ sessionKey: JSON.stringify(this.sessionKeyJwk),
2168
+ spaceId: delegation.spaceId,
2169
+ delegationCid: delegation.cid,
2170
+ delegationHeader,
2171
+ verificationMethod: this.sessionDid,
2172
+ jwk: this.sessionKeyJwk,
2173
+ siwe: "",
2174
+ // Not used in session-only mode
2175
+ signature: ""
2176
+ // Not used in session-only mode
2177
+ };
2178
+ this.trackReceivedDelegation(delegation, this.sessionKeyJwk);
2179
+ return new DelegatedAccess(session2, delegation, targetHost, this.wasmBindings.invoke);
2180
+ }
2181
+ const mySession = this.auth?.tinyCloudSession;
2182
+ if (!mySession) {
2183
+ throw new Error("Not signed in. Call signIn() first.");
2184
+ }
2185
+ const jwk = mySession.jwk;
2186
+ const abilities = {};
2187
+ const kvActions = delegation.actions.filter((a) => a.startsWith("tinycloud.kv/"));
2188
+ const sqlActions = delegation.actions.filter((a) => a.startsWith("tinycloud.sql/"));
2189
+ const duckdbActions = delegation.actions.filter((a) => a.startsWith("tinycloud.duckdb/"));
2190
+ if (kvActions.length > 0) {
2191
+ abilities.kv = { [delegation.path]: kvActions };
2192
+ }
2193
+ if (sqlActions.length > 0) {
2194
+ abilities.sql = { [delegation.path]: sqlActions };
2195
+ }
2196
+ if (duckdbActions.length > 0) {
2197
+ abilities.duckdb = { [delegation.path]: duckdbActions };
2198
+ }
2199
+ const now = /* @__PURE__ */ new Date();
2200
+ const maxExpiry = new Date(now.getTime() + 60 * 60 * 1e3);
2201
+ const expirationTime = delegation.expiry < maxExpiry ? delegation.expiry : maxExpiry;
2202
+ const prepared = this.wasmBindings.prepareSession({
2203
+ abilities,
2204
+ address: this.wasmBindings.ensureEip55(mySession.address),
2205
+ chainId: mySession.chainId,
2206
+ domain: new URL(targetHost).hostname,
2207
+ issuedAt: now.toISOString(),
2208
+ expirationTime: expirationTime.toISOString(),
2209
+ spaceId: delegation.spaceId,
2210
+ jwk,
2211
+ parents: [delegation.cid]
2212
+ });
2213
+ const signature = await this.signer.signMessage(prepared.siwe);
2214
+ const invokerSession = this.wasmBindings.completeSessionSetup({
2215
+ ...prepared,
2216
+ signature
2217
+ });
2218
+ const activateResult = await activateSessionWithHost2(
2219
+ targetHost,
2220
+ invokerSession.delegationHeader
2221
+ );
2222
+ if (!activateResult.success) {
2223
+ throw new Error(`Failed to activate delegated session: ${activateResult.error}`);
2224
+ }
2225
+ const session = {
2226
+ address: mySession.address,
2227
+ chainId: mySession.chainId,
2228
+ sessionKey: mySession.sessionKey,
2229
+ spaceId: delegation.spaceId,
2230
+ delegationCid: invokerSession.delegationCid,
2231
+ delegationHeader: invokerSession.delegationHeader,
2232
+ verificationMethod: mySession.verificationMethod,
2233
+ jwk,
2234
+ siwe: prepared.siwe,
2235
+ signature
2236
+ };
2237
+ this.trackReceivedDelegation(delegation, jwk);
2238
+ return new DelegatedAccess(session, delegation, targetHost, this.wasmBindings.invoke);
2239
+ }
2240
+ /**
2241
+ * Create a sub-delegation from a received delegation.
2242
+ *
2243
+ * This allows further delegating access that was received from another user,
2244
+ * if the original delegation allows sub-delegation.
2245
+ *
2246
+ * @param parentDelegation - The delegation received from another user
2247
+ * @param params - Sub-delegation parameters (must be within parent's scope)
2248
+ * @returns A portable delegation for the sub-delegate
2249
+ */
2250
+ async createSubDelegation(parentDelegation, params) {
2251
+ if (!this.signer) {
2252
+ throw new Error("Cannot createSubDelegation() in session-only mode. Requires wallet mode.");
2253
+ }
2254
+ if (!this._address) {
2255
+ throw new Error("Not signed in. Call signIn() first.");
2256
+ }
2257
+ if (parentDelegation.disableSubDelegation) {
2258
+ throw new Error("Parent delegation does not allow sub-delegation");
2259
+ }
2260
+ if (!params.path.startsWith(parentDelegation.path)) {
2261
+ throw new Error(
2262
+ `Sub-delegation path "${params.path}" must be within parent path "${parentDelegation.path}"`
2263
+ );
2264
+ }
2265
+ const parentActions = new Set(parentDelegation.actions);
2266
+ for (const action of params.actions) {
2267
+ if (!parentActions.has(action)) {
2268
+ throw new Error(
2269
+ `Sub-delegation action "${action}" is not in parent's actions: ${parentDelegation.actions.join(", ")}`
2270
+ );
2271
+ }
2272
+ }
2273
+ const now = /* @__PURE__ */ new Date();
2274
+ const expiryMs = params.expiryMs ?? 60 * 60 * 1e3;
2275
+ const requestedExpiry = new Date(now.getTime() + expiryMs);
2276
+ const actualExpiry = requestedExpiry > parentDelegation.expiry ? parentDelegation.expiry : requestedExpiry;
2277
+ const abilities = {};
2278
+ const kvActions = params.actions.filter((a) => a.startsWith("tinycloud.kv/"));
2279
+ const sqlActions = params.actions.filter((a) => a.startsWith("tinycloud.sql/"));
2280
+ const duckdbActions = params.actions.filter((a) => a.startsWith("tinycloud.duckdb/"));
2281
+ if (kvActions.length > 0) {
2282
+ abilities.kv = { [params.path]: kvActions };
2283
+ }
2284
+ if (sqlActions.length > 0) {
2285
+ abilities.sql = { [params.path]: sqlActions };
2286
+ }
2287
+ if (duckdbActions.length > 0) {
2288
+ abilities.duckdb = { [params.path]: duckdbActions };
2289
+ }
2290
+ const targetHost = parentDelegation.host ?? this.config.host;
2291
+ const prepared = this.wasmBindings.prepareSession({
2292
+ abilities,
2293
+ address: this.wasmBindings.ensureEip55(this._address),
2294
+ chainId: this._chainId,
2295
+ domain: new URL(targetHost).hostname,
2296
+ issuedAt: now.toISOString(),
2297
+ expirationTime: actualExpiry.toISOString(),
2298
+ spaceId: parentDelegation.spaceId,
2299
+ delegateUri: params.delegateDID,
2300
+ parents: [parentDelegation.cid]
2301
+ });
2302
+ const signature = await this.signer.signMessage(prepared.siwe);
2303
+ const subDelegationSession = this.wasmBindings.completeSessionSetup({
2304
+ ...prepared,
2305
+ signature
2306
+ });
2307
+ const activateResult = await activateSessionWithHost2(
2308
+ targetHost,
2309
+ subDelegationSession.delegationHeader
2310
+ );
2311
+ if (!activateResult.success) {
2312
+ throw new Error(`Failed to activate sub-delegation: ${activateResult.error}`);
2313
+ }
2314
+ return {
2315
+ cid: subDelegationSession.delegationCid,
2316
+ delegationHeader: subDelegationSession.delegationHeader,
2317
+ spaceId: parentDelegation.spaceId,
2318
+ path: params.path,
2319
+ actions: params.actions,
2320
+ disableSubDelegation: params.disableSubDelegation ?? false,
2321
+ expiry: actualExpiry,
2322
+ delegateDID: params.delegateDID,
2323
+ ownerAddress: parentDelegation.ownerAddress,
2324
+ chainId: parentDelegation.chainId,
2325
+ host: targetHost
2326
+ };
2327
+ }
2328
+ };
2329
+
2330
+ // src/delegation.ts
2331
+ function serializeDelegation(delegation) {
2332
+ return JSON.stringify({
2333
+ ...delegation,
2334
+ expiry: delegation.expiry.toISOString()
2335
+ });
2336
+ }
2337
+ function deserializeDelegation(data) {
2338
+ const parsed = JSON.parse(data);
2339
+ return {
2340
+ ...parsed,
2341
+ cid: parsed.cid,
2342
+ expiry: new Date(parsed.expiry)
2343
+ };
2344
+ }
2345
+
2346
+ // src/core.ts
2347
+ import { KVService as KVService3, PrefixedKVService } from "@tinycloud/sdk-core";
2348
+ import { SQLService as SQLService3, SQLAction, DatabaseHandle } from "@tinycloud/sdk-core";
2349
+ import { DuckDbService as DuckDbService3, DuckDbDatabaseHandle, DuckDbAction } from "@tinycloud/sdk-core";
2350
+ import { DataVaultService as DataVaultService2, VaultHeaders, VaultPublicSpaceKVActions, createVaultCrypto as createVaultCrypto2 } from "@tinycloud/sdk-core";
2351
+ import {
2352
+ DelegationManager as DelegationManager2,
2353
+ SharingService as SharingService2,
2354
+ createSharingService,
2355
+ DelegationErrorCodes
2356
+ } from "@tinycloud/sdk-core";
2357
+ import {
2358
+ CapabilityKeyRegistry as CapabilityKeyRegistry2,
2359
+ createCapabilityKeyRegistry,
2360
+ CapabilityKeyRegistryErrorCodes
2361
+ } from "@tinycloud/sdk-core";
2362
+ import {
2363
+ SpaceService as SpaceService2,
2364
+ SpaceErrorCodes,
2365
+ createSpaceService,
2366
+ parseSpaceUri,
2367
+ buildSpaceUri,
2368
+ makePublicSpaceId as makePublicSpaceId2,
2369
+ Space
2370
+ } from "@tinycloud/sdk-core";
2371
+ import {
2372
+ ProtocolMismatchError,
2373
+ VersionCheckError,
2374
+ UnsupportedFeatureError as UnsupportedFeatureError2,
2375
+ checkNodeInfo as checkNodeInfo2
2376
+ } from "@tinycloud/sdk-core";
2377
+ import { ServiceContext as ServiceContext3 } from "@tinycloud/sdk-core";
2378
+ export {
2379
+ AutoApproveSpaceCreationHandler2 as AutoApproveSpaceCreationHandler,
2380
+ CapabilityKeyRegistry2 as CapabilityKeyRegistry,
2381
+ CapabilityKeyRegistryErrorCodes,
2382
+ DataVaultService2 as DataVaultService,
2383
+ DatabaseHandle,
2384
+ DelegatedAccess,
2385
+ DelegationErrorCodes,
2386
+ DelegationManager2 as DelegationManager,
2387
+ DuckDbAction,
2388
+ DuckDbDatabaseHandle,
2389
+ DuckDbService3 as DuckDbService,
2390
+ FileSessionStorage,
2391
+ KVService3 as KVService,
2392
+ MemorySessionStorage,
2393
+ NodeUserAuthorization,
2394
+ PrefixedKVService,
2395
+ ProtocolMismatchError,
2396
+ SQLAction,
2397
+ SQLService3 as SQLService,
2398
+ ServiceContext3 as ServiceContext,
2399
+ SharingService2 as SharingService,
2400
+ SilentNotificationHandler2 as SilentNotificationHandler,
2401
+ Space,
2402
+ SpaceErrorCodes,
2403
+ SpaceService2 as SpaceService,
2404
+ TinyCloud2 as TinyCloud,
2405
+ TinyCloudNode,
2406
+ UnsupportedFeatureError2 as UnsupportedFeatureError,
2407
+ VaultHeaders,
2408
+ VaultPublicSpaceKVActions,
2409
+ VersionCheckError,
2410
+ WasmKeyProvider,
2411
+ buildSpaceUri,
2412
+ checkNodeInfo2 as checkNodeInfo,
2413
+ createCapabilityKeyRegistry,
2414
+ createSharingService,
2415
+ createSpaceService,
2416
+ createVaultCrypto2 as createVaultCrypto,
2417
+ createWasmKeyProvider,
2418
+ defaultSignStrategy,
2419
+ defaultSpaceCreationHandler,
2420
+ deserializeDelegation,
2421
+ makePublicSpaceId2 as makePublicSpaceId,
2422
+ parseSpaceUri,
2423
+ serializeDelegation
2424
+ };
2425
+ //# sourceMappingURL=core.js.map