@tinycloud/node-sdk 1.6.0 → 2.0.0

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