@tinycloud/node-sdk 2.2.0-beta.1 → 2.2.0-beta.11

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.
@@ -0,0 +1,1553 @@
1
+ import { ISessionStorage, PersistedSessionData, AutoSignStrategy, AutoRejectStrategy, CallbackStrategy, IUserAuthorization, ISigner, ISpaceCreationHandler, IWasmBindings, SiweConfig, Manifest, ComposedManifestRequest, ClientSession, TinyCloudSession, Extension, SignInOptions, Delegation, DelegatedResource, IKVService, ISQLService, IDuckDbService, IHooksService, INotificationHandler, IENSResolver, IDataVaultService, ISecretsService, ICapabilityKeyRegistry, PermissionEntry, DelegationManager, ISpaceService, ISpace, ISharingService, CreateDelegationParams, DelegationResult, ResolvedDelegate, KeyProvider, ISessionManager, JWK } from '@tinycloud/sdk-core';
2
+ import { EventEmitter } from 'events';
3
+ import { InvokeFunction } from '@tinycloud/sdk-services';
4
+
5
+ /**
6
+ * In-memory session storage for Node.js.
7
+ *
8
+ * Sessions are stored in memory and lost when the process exits.
9
+ * Suitable for:
10
+ * - Development and testing
11
+ * - Stateless server deployments
12
+ * - Short-lived processes
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const storage = new MemorySessionStorage();
17
+ * await storage.save("0x123...", sessionData);
18
+ * const session = await storage.load("0x123...");
19
+ * ```
20
+ */
21
+ declare class MemorySessionStorage implements ISessionStorage {
22
+ private sessions;
23
+ /**
24
+ * Save a session for an address.
25
+ */
26
+ save(address: string, session: PersistedSessionData): Promise<void>;
27
+ /**
28
+ * Load a session for an address.
29
+ */
30
+ load(address: string): Promise<PersistedSessionData | null>;
31
+ /**
32
+ * Clear a session for an address.
33
+ */
34
+ clear(address: string): Promise<void>;
35
+ /**
36
+ * Check if a session exists for an address.
37
+ */
38
+ exists(address: string): boolean;
39
+ /**
40
+ * Memory storage is always available.
41
+ */
42
+ isAvailable(): boolean;
43
+ /**
44
+ * Clear all sessions.
45
+ */
46
+ clearAll(): void;
47
+ /**
48
+ * Get the number of stored sessions.
49
+ */
50
+ size(): number;
51
+ }
52
+
53
+ /**
54
+ * File-based session storage for Node.js.
55
+ *
56
+ * Sessions are persisted to the file system and survive process restarts.
57
+ * Suitable for:
58
+ * - CLI applications
59
+ * - Long-running server processes
60
+ * - Development environments
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * const storage = new FileSessionStorage("/tmp/tinycloud-sessions");
65
+ * await storage.save("0x123...", sessionData);
66
+ * // Session persists across process restarts
67
+ * ```
68
+ */
69
+ declare class FileSessionStorage implements ISessionStorage {
70
+ private readonly baseDir;
71
+ /**
72
+ * Create a new FileSessionStorage.
73
+ *
74
+ * @param baseDir - Directory to store session files (default: ~/.tinycloud/sessions)
75
+ */
76
+ constructor(baseDir?: string);
77
+ /**
78
+ * Get the default session storage directory.
79
+ */
80
+ private getDefaultDir;
81
+ /**
82
+ * Ensure the storage directory exists.
83
+ */
84
+ private ensureDirectoryExists;
85
+ /**
86
+ * Get the file path for an address.
87
+ */
88
+ private getFilePath;
89
+ /**
90
+ * Save a session for an address.
91
+ */
92
+ save(address: string, session: PersistedSessionData): Promise<void>;
93
+ /**
94
+ * Load a session for an address.
95
+ */
96
+ load(address: string): Promise<PersistedSessionData | null>;
97
+ /**
98
+ * Clear a session for an address.
99
+ */
100
+ clear(address: string): Promise<void>;
101
+ /**
102
+ * Check if a session exists for an address.
103
+ */
104
+ exists(address: string): boolean;
105
+ /**
106
+ * Check if file system storage is available.
107
+ */
108
+ isAvailable(): boolean;
109
+ }
110
+
111
+ /**
112
+ * Node.js-specific SignStrategy types for TinyCloud authorization.
113
+ *
114
+ * This module re-exports common types from sdk-core and provides
115
+ * Node.js-specific implementations (e.g., NodeEventEmitterStrategy
116
+ * using Node's EventEmitter instead of browser EventTarget).
117
+ *
118
+ * @packageDocumentation
119
+ */
120
+
121
+ /**
122
+ * Node.js event emitter strategy: emits sign requests as events.
123
+ *
124
+ * Uses Node.js EventEmitter for compatibility with Node.js applications.
125
+ * For browser environments, use the EventEmitterStrategy from sdk-core
126
+ * which uses EventTarget.
127
+ *
128
+ * Events emitted:
129
+ * - 'sign-request': When a sign request is received
130
+ *
131
+ * Use cases:
132
+ * - Async approval workflows in Node.js
133
+ * - External signing services
134
+ * - Multi-step authorization flows
135
+ *
136
+ * @example
137
+ * ```typescript
138
+ * const emitter = new EventEmitter();
139
+ * const strategy: NodeEventEmitterStrategy = { type: 'event-emitter', emitter };
140
+ *
141
+ * emitter.on('sign-request', async (req, respond) => {
142
+ * const approved = await externalApprovalService.check(req);
143
+ * respond({ approved, signature: approved ? await sign(req.message) : undefined });
144
+ * });
145
+ * ```
146
+ */
147
+ interface NodeEventEmitterStrategy {
148
+ type: "event-emitter";
149
+ emitter: EventEmitter;
150
+ /** Timeout in milliseconds for waiting on event response (default: 60000) */
151
+ timeout?: number;
152
+ }
153
+ /**
154
+ * Node.js sign strategy union type.
155
+ *
156
+ * Determines how sign requests are handled in NodeUserAuthorization.
157
+ * Uses Node.js EventEmitter for the event-emitter strategy.
158
+ */
159
+ type SignStrategy = AutoSignStrategy | AutoRejectStrategy | CallbackStrategy | NodeEventEmitterStrategy;
160
+ /**
161
+ * Default sign strategy is auto-sign for convenience.
162
+ * This is the Node.js-specific version typed with SignStrategy.
163
+ */
164
+ declare const defaultSignStrategy: SignStrategy;
165
+
166
+ /**
167
+ * Configuration for NodeUserAuthorization.
168
+ */
169
+ interface NodeUserAuthorizationConfig {
170
+ /** The signer used for signing messages */
171
+ signer: ISigner;
172
+ /** Sign strategy for handling sign requests */
173
+ signStrategy?: SignStrategy;
174
+ /** Session storage implementation */
175
+ sessionStorage?: ISessionStorage;
176
+ /** Domain for SIWE messages */
177
+ domain: string;
178
+ /** URI for SIWE messages (default: domain) */
179
+ uri?: string;
180
+ /** Statement included in SIWE messages */
181
+ statement?: string;
182
+ /** Space prefix for new sessions */
183
+ spacePrefix?: string;
184
+ /** Default actions for sessions */
185
+ defaultActions?: Record<string, Record<string, string[]>>;
186
+ /** Session expiration time in milliseconds (default: 1 hour) */
187
+ sessionExpirationMs?: number;
188
+ /** Automatically create space if it doesn't exist (default: false) */
189
+ autoCreateSpace?: boolean;
190
+ /** Custom space creation handler. If provided, takes precedence over autoCreateSpace. */
191
+ spaceCreationHandler?: ISpaceCreationHandler;
192
+ /** Explicit TinyCloud server endpoints. When omitted, signIn resolves the user's host. */
193
+ tinycloudHosts?: string[];
194
+ /** TinyCloud location registry URL. Default: https://registry.tinycloud.xyz. */
195
+ tinycloudRegistryUrl?: string | null;
196
+ /** Fallback TinyCloud hosts. Default: hosted TinyCloud node. */
197
+ tinycloudFallbackHosts?: string[] | null;
198
+ /** Whether to include public space capabilities in the session (default: true) */
199
+ enablePublicSpace?: boolean;
200
+ /** WASM bindings for cryptographic operations. Required. */
201
+ wasmBindings: IWasmBindings;
202
+ /**
203
+ * SIWE nonce override. If omitted, the WASM layer generates a random nonce.
204
+ * If `siweConfig.nonce` is also provided, `siweConfig.nonce` wins.
205
+ */
206
+ nonce?: string;
207
+ /** Optional SIWE configuration overrides (e.g., nonce for server-provided nonces) */
208
+ siweConfig?: SiweConfig;
209
+ /**
210
+ * App manifest used to drive the SIWE recap at sign-in.
211
+ *
212
+ * When set, `signIn` resolves the manifest (via
213
+ * {@link resolveManifest}), unions the app's own permissions with
214
+ * every `delegations[*].permissions` list, converts to the WASM
215
+ * abilities shape, and uses that map as the session's granted
216
+ * capabilities — *instead* of `defaultActions`.
217
+ *
218
+ * This is what makes manifest-declared pre-delegations usable: the
219
+ * session key's recap covers both the app's runtime needs and the
220
+ * downstream delegation targets, so `delegateTo` can issue the
221
+ * sub-delegation via the session-key UCAN path without a wallet
222
+ * prompt.
223
+ *
224
+ * When omitted, `signIn` falls back to `defaultActions` for
225
+ * backwards compatibility.
226
+ */
227
+ manifest?: Manifest | Manifest[];
228
+ /** Pre-composed manifest request. Takes precedence over `manifest`. */
229
+ capabilityRequest?: ComposedManifestRequest;
230
+ /** Include implicit account registry permissions when composing `manifest`. Default true. */
231
+ includeAccountRegistryPermissions?: boolean;
232
+ }
233
+ /**
234
+ * Node.js implementation of IUserAuthorization.
235
+ *
236
+ * Supports multiple sign strategies for different use cases:
237
+ * - auto-sign: Automatically approve all sign requests (trusted backends)
238
+ * - auto-reject: Reject all sign requests (read-only mode)
239
+ * - callback: Delegate to a custom callback function (CLI prompts)
240
+ * - event-emitter: Emit sign requests as events (async workflows)
241
+ *
242
+ * @example
243
+ * ```typescript
244
+ * // Auto-sign for backend services
245
+ * const auth = new NodeUserAuthorization({
246
+ * signer: new PrivateKeySigner(process.env.PRIVATE_KEY),
247
+ * signStrategy: { type: 'auto-sign' },
248
+ * domain: 'api.myapp.com',
249
+ * });
250
+ *
251
+ * // Callback for CLI prompts
252
+ * const auth = new NodeUserAuthorization({
253
+ * signer,
254
+ * signStrategy: {
255
+ * type: 'callback',
256
+ * handler: async (req) => {
257
+ * const approved = await promptUser(`Sign for ${req.address}?`);
258
+ * return { approved };
259
+ * }
260
+ * },
261
+ * domain: 'cli.myapp.com',
262
+ * });
263
+ * ```
264
+ */
265
+ declare class NodeUserAuthorization implements IUserAuthorization {
266
+ private readonly signer;
267
+ private readonly signStrategy;
268
+ private readonly sessionStorage;
269
+ private readonly domain;
270
+ private readonly uri;
271
+ private readonly statement?;
272
+ private readonly spacePrefix;
273
+ private readonly defaultActions;
274
+ private readonly sessionExpirationMs;
275
+ private readonly autoCreateSpace;
276
+ private readonly spaceCreationHandler?;
277
+ private tinycloudHosts?;
278
+ private readonly tinycloudRegistryUrl?;
279
+ private readonly tinycloudFallbackHosts?;
280
+ private readonly enablePublicSpace;
281
+ private readonly nonce?;
282
+ private readonly siweConfig?;
283
+ private readonly wasm;
284
+ /**
285
+ * Stored manifest, if one was provided at construction time. Used by
286
+ * {@link signIn} to derive the session's granted capabilities instead
287
+ * of falling back to {@link defaultActions}.
288
+ */
289
+ private _manifest?;
290
+ private _capabilityRequest?;
291
+ private readonly includeAccountRegistryPermissions;
292
+ private sessionManager;
293
+ private extensions;
294
+ private _session?;
295
+ private _tinyCloudSession?;
296
+ private _address?;
297
+ private _chainId?;
298
+ private _nodeFeatures;
299
+ constructor(config: NodeUserAuthorizationConfig);
300
+ /**
301
+ * Return the manifest currently driving sign-in behavior, or
302
+ * `undefined` if none is set. Used by TinyCloudWeb/TinyCloudNode
303
+ * internals to surface the manifest for requestPermissions flows
304
+ * without forcing the caller to track it separately.
305
+ */
306
+ get manifest(): Manifest | Manifest[] | undefined;
307
+ get capabilityRequest(): ComposedManifestRequest | undefined;
308
+ get hosts(): string[];
309
+ /**
310
+ * Install or replace the stored manifest. Takes effect on the next
311
+ * `signIn()` call — the current session (if any) is not touched.
312
+ */
313
+ setManifest(manifest: Manifest | Manifest[] | undefined): void;
314
+ setCapabilityRequest(request: ComposedManifestRequest | undefined): void;
315
+ /**
316
+ * The current active session (web-core compatible).
317
+ */
318
+ get session(): ClientSession | undefined;
319
+ /**
320
+ * The current TinyCloud session with full delegation data.
321
+ * Includes spaceId, delegationHeader, and delegationCid.
322
+ */
323
+ get tinyCloudSession(): TinyCloudSession | undefined;
324
+ private resolveTinyCloudHostsForSignIn;
325
+ private requireTinyCloudHosts;
326
+ private get primaryTinyCloudHost();
327
+ get nodeFeatures(): string[];
328
+ /**
329
+ * Compute the `abilities` map the WASM `prepareSession` call should
330
+ * see at sign-in time.
331
+ *
332
+ * When a manifest is installed, we resolve it and union together:
333
+ * - the app's own `resources` (what it needs at runtime)
334
+ * - every `additionalDelegates[*].permissions` list (what it will
335
+ * re-delegate to other DIDs post sign-in)
336
+ *
337
+ * into the short-service / path / full-URN-actions shape the WASM
338
+ * layer expects. This is the key invariant that lets
339
+ * {@link TinyCloudNode.delegateTo} issue manifest-declared
340
+ * delegations via the session key (no wallet prompt): the session's
341
+ * own recap already covers every action those delegations need.
342
+ *
343
+ * When no manifest is installed, we fall back to the
344
+ * {@link defaultActions} table so existing callers see no change.
345
+ *
346
+ * This is a pure function of `this._manifest` + `this.defaultActions`
347
+ * — the manifest resolution performs no I/O and throws a
348
+ * {@link ManifestValidationError} on structural problems (missing
349
+ * id/name, unparseable expiry, etc), which will surface at sign-in
350
+ * rather than being silently swallowed.
351
+ *
352
+ * @internal
353
+ */
354
+ private getCapabilityRequest;
355
+ private resolveSpaceName;
356
+ private resolveSignInCapabilities;
357
+ /**
358
+ * Build SIWE overrides from the top-level nonce and siweConfig.
359
+ * - Top-level `nonce` is seeded first so `siweConfig.nonce` wins if both are set.
360
+ * - statement is prepended to the default statement
361
+ * - resources are appended to the default resources
362
+ * - uri triggers a warning (overwriting delegation target)
363
+ * - all other fields override directly
364
+ * - per-call nonce overrides siweConfig.nonce when provided
365
+ */
366
+ private buildSiweOverrides;
367
+ /**
368
+ * Add an extension to the authorization flow.
369
+ */
370
+ extend(extension: Extension): void;
371
+ /**
372
+ * Get the space ID for the current session.
373
+ */
374
+ getSpaceId(): string | undefined;
375
+ /**
376
+ * Create the space on the TinyCloud server (host delegation).
377
+ * This registers the user as the owner of the space.
378
+ */
379
+ private hostSpace;
380
+ /**
381
+ * Create a specific space on the server via host delegation.
382
+ * Used for lazy creation of additional spaces (e.g., public).
383
+ */
384
+ hostPublicSpace(spaceId: string): Promise<boolean>;
385
+ /**
386
+ * Create a specific owned space on the server via host delegation.
387
+ * Used by manifest registry setup for the account space.
388
+ */
389
+ hostOwnedSpace(spaceId: string): Promise<boolean>;
390
+ /**
391
+ * Ensure the user's space exists on the TinyCloud server.
392
+ * Creates the space if it doesn't exist and autoCreateSpace is enabled.
393
+ * If autoCreateSpace is false and space doesn't exist, silently returns
394
+ * (user may be using delegations to access other spaces).
395
+ *
396
+ * @throws Error if space creation fails
397
+ */
398
+ ensureSpaceExists(): Promise<void>;
399
+ /**
400
+ * Sign in and create a new session.
401
+ *
402
+ * This follows the correct SIWE-ReCap flow:
403
+ * 1. Create session key and get JWK
404
+ * 2. Call prepareSession() which generates the SIWE with ReCap capabilities
405
+ * 3. Sign the SIWE string from prepareSession
406
+ * 4. Call completeSessionSetup() with the prepared session + signature
407
+ *
408
+ * @param options - Optional per-call SIWE overrides for this sign-in only
409
+ */
410
+ signIn(options?: SignInOptions): Promise<ClientSession>;
411
+ /**
412
+ * Sign out and clear the current session.
413
+ */
414
+ signOut(): Promise<void>;
415
+ /**
416
+ * Get the current wallet/signer address.
417
+ */
418
+ address(): string | undefined;
419
+ /**
420
+ * Get the current chain ID.
421
+ */
422
+ chainId(): number | undefined;
423
+ /**
424
+ * Sign a message with the connected signer.
425
+ */
426
+ signMessage(message: string): Promise<string>;
427
+ /**
428
+ * Prepare a session for external signing.
429
+ *
430
+ * Use this method when you need to sign the SIWE message externally (e.g., via
431
+ * a hardware wallet, multi-sig, or external service). After obtaining the signature,
432
+ * call `signInWithPreparedSession()` to complete the sign-in.
433
+ *
434
+ * @example
435
+ * ```typescript
436
+ * const { prepared, keyId, jwk } = await auth.prepareSessionForSigning();
437
+ * const signature = await externalSigner.signMessage(prepared.siwe);
438
+ * const session = await auth.signInWithPreparedSession(prepared, signature, keyId, jwk);
439
+ * ```
440
+ */
441
+ prepareSessionForSigning(): Promise<{
442
+ prepared: {
443
+ siwe: string;
444
+ jwk: Record<string, unknown>;
445
+ spaceId: string;
446
+ verificationMethod: string;
447
+ };
448
+ keyId: string;
449
+ jwk: Record<string, unknown>;
450
+ address: string;
451
+ chainId: number;
452
+ }>;
453
+ /**
454
+ * Complete sign-in with a prepared session and signature.
455
+ *
456
+ * Use this method after obtaining a signature for the SIWE message from
457
+ * `prepareSessionForSigning()`. The signature MUST be over `prepared.siwe`.
458
+ *
459
+ * @param prepared - The prepared session from `prepareSessionForSigning()`
460
+ * @param signature - The signature over `prepared.siwe`
461
+ * @param keyId - The session key ID from `prepareSessionForSigning()`
462
+ * @param jwk - The JWK from `prepareSessionForSigning()`
463
+ */
464
+ signInWithPreparedSession(prepared: {
465
+ siwe: string;
466
+ jwk: Record<string, unknown>;
467
+ spaceId: string;
468
+ verificationMethod: string;
469
+ }, signature: string, keyId: string, jwk: Record<string, unknown>): Promise<ClientSession>;
470
+ /**
471
+ * Clear persisted session data.
472
+ */
473
+ clearPersistedSession(address?: string): Promise<void>;
474
+ /**
475
+ * Check if a session is persisted for an address.
476
+ */
477
+ isSessionPersisted(address: string): boolean;
478
+ /**
479
+ * Request a signature based on the configured strategy.
480
+ */
481
+ private requestSignature;
482
+ /**
483
+ * Request signature via event emitter with timeout.
484
+ */
485
+ private requestSignatureViaEmitter;
486
+ }
487
+
488
+ /**
489
+ * A portable delegation that can be transported between users.
490
+ * Extends the base Delegation type with fields required for transport.
491
+ *
492
+ * @remarks
493
+ * PortableDelegation adds transport fields to Delegation:
494
+ * - `delegationHeader`: Structured authorization header for API calls
495
+ * - `ownerAddress`: Space owner's address for session creation
496
+ * - `chainId`: Chain ID for session creation
497
+ * - `host`: Optional server URL
498
+ * - `resources`: Multi-resource grant breakdown (present when the
499
+ * delegation was issued via the multi-resource WASM path, i.e. one
500
+ * UCAN covering multiple `(service, path, actions)` entries). The
501
+ * flat `path` + `actions` fields mirror the first entry for
502
+ * single-resource callers; consumers that need the full picture
503
+ * read `resources`.
504
+ */
505
+ interface PortableDelegation extends Omit<Delegation, "isRevoked"> {
506
+ /** The authorization header for this delegation (structured format) */
507
+ delegationHeader: {
508
+ Authorization: string;
509
+ };
510
+ /** The address of the space owner */
511
+ ownerAddress: string;
512
+ /** The chain ID */
513
+ chainId: number;
514
+ /** TinyCloud server URL where this delegation was created */
515
+ host?: string;
516
+ /** Whether the recipient is prevented from creating sub-delegations */
517
+ disableSubDelegation?: boolean;
518
+ /** Companion delegation for the user's public space (auto-created when includePublicSpace is true) */
519
+ publicDelegation?: PortableDelegation;
520
+ /**
521
+ * Full multi-resource grant breakdown. Present when the delegation
522
+ * was issued via the multi-resource WASM path; each entry describes
523
+ * one `(service, space, path, actions)` grant carried by the single
524
+ * underlying UCAN. When absent, only the flat `path` + `actions`
525
+ * fields are authoritative (legacy single-resource shape).
526
+ */
527
+ resources?: DelegatedResource[];
528
+ }
529
+ /**
530
+ * Serialize a PortableDelegation for transport (e.g., over network).
531
+ */
532
+ declare function serializeDelegation(delegation: PortableDelegation): string;
533
+ /**
534
+ * Deserialize a PortableDelegation from transport.
535
+ */
536
+ declare function deserializeDelegation(data: string): PortableDelegation;
537
+
538
+ /**
539
+ * The handles needed to rehydrate this delegation activation in a fresh
540
+ * `TinyCloudNode` via `TinyCloudNode.restoreSession(...)` in another process
541
+ * or after a restart.
542
+ *
543
+ * In wallet mode, `delegationHeader` and `delegationCid` are bound to the
544
+ * session key that ran `useDelegation`. They are NOT intrinsic to the
545
+ * portable delegation — they expire with the server-side session (typically
546
+ * ~1h). To keep a restored node alive longer, re-run `useDelegation` with
547
+ * the original portable delegation and call `restoreSession` again with the
548
+ * fresh `RestorableSession`.
549
+ */
550
+ interface RestorableSession {
551
+ delegationHeader: {
552
+ Authorization: string;
553
+ };
554
+ delegationCid: string;
555
+ spaceId: string;
556
+ jwk: object;
557
+ verificationMethod: string;
558
+ address: string;
559
+ chainId: number;
560
+ }
561
+ /**
562
+ * Provides access to a space via a received delegation.
563
+ *
564
+ * This is returned by TinyCloudNode.useDelegation() and provides
565
+ * KV operations on the delegated space.
566
+ */
567
+ declare class DelegatedAccess {
568
+ private session;
569
+ private _delegation;
570
+ private host;
571
+ private _serviceContext;
572
+ private _kv;
573
+ private _sql;
574
+ private _duckdb;
575
+ private _hooks;
576
+ constructor(session: TinyCloudSession, delegation: PortableDelegation, host: string, invoke: InvokeFunction);
577
+ /**
578
+ * Get the delegation this access was created from.
579
+ */
580
+ get delegation(): PortableDelegation;
581
+ /**
582
+ * The space ID this access is for.
583
+ */
584
+ get spaceId(): string;
585
+ /**
586
+ * The path this access is scoped to.
587
+ */
588
+ get path(): string;
589
+ /**
590
+ * KV operations on the delegated space.
591
+ */
592
+ get kv(): IKVService;
593
+ /**
594
+ * SQL operations on the delegated space.
595
+ */
596
+ get sql(): ISQLService;
597
+ /**
598
+ * DuckDB operations on the delegated space.
599
+ */
600
+ get duckdb(): IDuckDbService;
601
+ /**
602
+ * Hooks write-stream subscriptions on the delegated space.
603
+ */
604
+ get hooks(): IHooksService;
605
+ /**
606
+ * Export the handles needed to rehydrate this activated delegation via
607
+ * `TinyCloudNode.restoreSession(...)` in another process or after a
608
+ * restart.
609
+ *
610
+ * See `RestorableSession` for lifetime caveats.
611
+ */
612
+ get restorable(): RestorableSession;
613
+ }
614
+
615
+ /**
616
+ * TinyCloudNode - High-level API for Node.js users.
617
+ *
618
+ * Each user has their own TinyCloudNode instance with their own key.
619
+ * This class provides a simplified interface for:
620
+ * - Signing in and managing sessions
621
+ * - Key-value storage operations on own space
622
+ * - Creating and using delegations
623
+ *
624
+ * @example
625
+ * ```typescript
626
+ * const alice = new TinyCloudNode({
627
+ * privateKey: process.env.ALICE_PRIVATE_KEY,
628
+ * host: "https://node.tinycloud.xyz",
629
+ * prefix: "myapp",
630
+ * });
631
+ *
632
+ * await alice.signIn();
633
+ * await alice.kv.put("greeting", "Hello, world!");
634
+ *
635
+ * // Delegate access to Bob
636
+ * const delegation = await alice.createDelegation({
637
+ * path: "shared/",
638
+ * actions: ["tinycloud.kv/get", "tinycloud.kv/put"],
639
+ * delegateDID: bob.did,
640
+ * });
641
+ *
642
+ * // Bob uses the delegation
643
+ * const access = await bob.useDelegation(delegation);
644
+ * const data = await access.kv.get("shared/data");
645
+ * ```
646
+ */
647
+
648
+ /**
649
+ * Configuration for TinyCloudNode.
650
+ * All fields are optional - TinyCloudNode can work with zero configuration.
651
+ */
652
+ interface TinyCloudNodeConfig {
653
+ /** Hex-encoded private key (with or without 0x prefix). Optional - only needed for wallet mode and signIn() */
654
+ privateKey?: string;
655
+ /** Custom signer implementation. If provided, takes precedence over privateKey. */
656
+ signer?: ISigner;
657
+ /** Explicit TinyCloud server URL. When omitted, signIn resolves the user's host. */
658
+ host?: string;
659
+ /** TinyCloud location registry URL. Default: https://registry.tinycloud.xyz. */
660
+ tinycloudRegistryUrl?: string | null;
661
+ /** Fallback TinyCloud hosts. Default: hosted TinyCloud node. */
662
+ tinycloudFallbackHosts?: string[] | null;
663
+ /** Space prefix for this user's space. Optional - only needed for signIn() */
664
+ prefix?: string;
665
+ /** Domain for SIWE messages (default: derived from host) */
666
+ domain?: string;
667
+ /** Session expiration time in milliseconds (default: 1 hour) */
668
+ sessionExpirationMs?: number;
669
+ /** Whether to automatically create space if it doesn't exist (default: false) */
670
+ autoCreateSpace?: boolean;
671
+ /** Custom session storage implementation (default: MemorySessionStorage) */
672
+ sessionStorage?: ISessionStorage;
673
+ /** Whether to include public space capabilities in the session (default: true).
674
+ * When true, signIn() automatically includes capabilities for the user's public space,
675
+ * accessible via spaces.get('public').kv */
676
+ enablePublicSpace?: boolean;
677
+ /** Custom WASM bindings (default: @tinycloud/node-sdk-wasm). Used by browser wrapper. */
678
+ wasmBindings?: IWasmBindings;
679
+ /** Notification handler for sign-in/sign-out/error events (default: SilentNotificationHandler) */
680
+ notificationHandler?: INotificationHandler;
681
+ /** ENS resolver for resolving .eth names in delegation methods */
682
+ ensResolver?: IENSResolver;
683
+ /** Custom space creation handler (default: auto-approve when autoCreateSpace is true) */
684
+ spaceCreationHandler?: ISpaceCreationHandler;
685
+ /**
686
+ * SIWE nonce override. If omitted, the WASM layer generates a random nonce.
687
+ * If `siweConfig.nonce` is also provided, `siweConfig.nonce` wins.
688
+ */
689
+ nonce?: string;
690
+ /** Optional SIWE configuration overrides (e.g., nonce for server-provided nonces) */
691
+ siweConfig?: SiweConfig;
692
+ /**
693
+ * App manifest driving the SIWE recap at sign-in.
694
+ *
695
+ * When set, `signIn()` resolves the manifest, unions the app's own
696
+ * permissions with every manifest-declared delegation's permissions,
697
+ * and uses that union as the session's granted capabilities — NOT
698
+ * the legacy `defaultActions` table. This is what makes
699
+ * `delegateTo(manifestDeclaredDid, permissions)` work without a
700
+ * wallet prompt: the session key's recap already covers the
701
+ * delegation target's needs at sign-in time.
702
+ *
703
+ * When omitted, `signIn()` falls back to `defaultActions` for
704
+ * backwards compatibility with callers that pre-date the manifest
705
+ * flow.
706
+ */
707
+ manifest?: Manifest | Manifest[];
708
+ /** Pre-composed manifest request. Takes precedence over `manifest`. */
709
+ capabilityRequest?: ComposedManifestRequest;
710
+ /** Include implicit account registry permissions when composing `manifest`. Default true. */
711
+ includeAccountRegistryPermissions?: boolean;
712
+ }
713
+ /**
714
+ * Options for {@link TinyCloudNode.delegateTo}.
715
+ *
716
+ * `expiry` accepts either an ms-format duration string (e.g. `"7d"`, `"1h"`)
717
+ * or a raw number of milliseconds. When omitted, the default is 1 hour.
718
+ *
719
+ * `forceWalletSign` bypasses the derivability check and sends the
720
+ * delegation through the legacy wallet-signed SIWE path, which always
721
+ * triggers a wallet prompt. Used for testing, for explicit wallet
722
+ * confirmation flows, and by the legacy `createDelegation` fallback.
723
+ */
724
+ interface DelegateToOptions {
725
+ /** Override expiry. ms-format string ("7d", "1h") or raw milliseconds. */
726
+ expiry?: string | number;
727
+ /** Force the wallet-signed SIWE path even if the caps are derivable. Default false. */
728
+ forceWalletSign?: boolean;
729
+ }
730
+ /**
731
+ * Result of {@link TinyCloudNode.delegateTo}.
732
+ *
733
+ * `prompted` indicates whether a wallet prompt was shown — `true` for the
734
+ * legacy wallet path (always), `false` for the session-key UCAN path (never).
735
+ * Callers wiring single-prompt sign-in flows use this to assert that their
736
+ * capability chain was derivable.
737
+ */
738
+ interface DelegateToResult {
739
+ delegation: PortableDelegation;
740
+ prompted: boolean;
741
+ }
742
+ /**
743
+ * Options for runtime permission escalation.
744
+ */
745
+ interface RuntimePermissionGrantOptions {
746
+ /** Override expiry. ms-format string ("7d", "1h") or raw milliseconds. */
747
+ expiry?: string | number;
748
+ }
749
+ /**
750
+ * High-level TinyCloud API for Node.js environments.
751
+ *
752
+ * Each user creates their own TinyCloudNode instance with their private key.
753
+ * The instance manages the user's session and provides access to their space.
754
+ */
755
+ /** @internal */
756
+ interface NodeDefaults {
757
+ createWasmBindings: () => IWasmBindings;
758
+ createSigner: (privateKey: string, chainId?: number) => ISigner;
759
+ }
760
+ declare class TinyCloudNode {
761
+ /** @internal Registered by importing @tinycloud/node-sdk (not /core) */
762
+ private static nodeDefaults?;
763
+ /** @internal Register Node.js-specific defaults (NodeWasmBindings, PrivateKeySigner) */
764
+ static registerNodeDefaults(defaults: NodeDefaults): void;
765
+ private config;
766
+ private readonly explicitHost?;
767
+ private signer;
768
+ private auth;
769
+ private tc;
770
+ private _address?;
771
+ private _chainId;
772
+ private wasmBindings;
773
+ private sessionManager;
774
+ private _serviceContext?;
775
+ private _kv?;
776
+ private _sql?;
777
+ private _duckdb?;
778
+ private _hooks?;
779
+ private _vault?;
780
+ private _baseSecrets?;
781
+ private _secrets?;
782
+ /** Cached public KV with proper delegation (set by ensurePublicSpace) */
783
+ private _publicKV?;
784
+ /** Session key ID - always available */
785
+ private sessionKeyId;
786
+ /** Session key JWK as object - always available */
787
+ private sessionKeyJwk;
788
+ /** Notification handler for user-facing events */
789
+ private notificationHandler;
790
+ private _capabilityRegistry;
791
+ private _keyProvider;
792
+ private _sharingService;
793
+ private _delegationManager?;
794
+ private _spaceService?;
795
+ private runtimePermissionGrants;
796
+ private get nodeFeatures();
797
+ /** SIWE domain — uses config override or defaults to app.tinycloud.xyz */
798
+ private get siweDomain();
799
+ private readonly invokeWithRuntimePermissions;
800
+ private readonly invokeAnyWithRuntimePermissions;
801
+ /**
802
+ * Create a new TinyCloudNode instance.
803
+ *
804
+ * All configuration is optional. Without a privateKey, the instance operates
805
+ * in "session-only" mode where it can receive delegations but cannot create
806
+ * its own space via signIn().
807
+ *
808
+ * @param config - Configuration options (all optional)
809
+ *
810
+ * @example
811
+ * ```typescript
812
+ * // Session-only mode - can receive delegations
813
+ * const bob = new TinyCloudNode();
814
+ * console.log(bob.did); // did:key:z6Mk... - available immediately
815
+ *
816
+ * // Wallet mode - can create own space
817
+ * const alice = new TinyCloudNode({
818
+ * privateKey: process.env.ALICE_PRIVATE_KEY,
819
+ * prefix: "myapp",
820
+ * });
821
+ * await alice.signIn();
822
+ * ```
823
+ */
824
+ constructor(config?: TinyCloudNodeConfig);
825
+ /**
826
+ * Set up authorization handler and TinyCloud instance.
827
+ * @internal
828
+ */
829
+ private setupAuth;
830
+ private syncResolvedHostFromAuth;
831
+ /**
832
+ * Install or replace the manifest that drives the SIWE recap at
833
+ * sign-in. Takes effect on the next `signIn()` call — the current
834
+ * session (if any) is not touched. Wire this up from a higher
835
+ * layer (e.g. TinyCloudWeb.setManifest) so the manifest is kept
836
+ * in sync across the stack.
837
+ */
838
+ setManifest(manifest: Manifest | Manifest[] | undefined): void;
839
+ setCapabilityRequest(request: ComposedManifestRequest | undefined): void;
840
+ /**
841
+ * Return the manifest currently installed on the auth handler,
842
+ * or `undefined` if none is set.
843
+ */
844
+ get manifest(): Manifest | Manifest[] | undefined;
845
+ get capabilityRequest(): ComposedManifestRequest | undefined;
846
+ get hosts(): string[];
847
+ /**
848
+ * Get the primary identity DID for this user.
849
+ * - If wallet connected and signed in: returns PKH DID (did:pkh:eip155:{chainId}:{address})
850
+ * - If session-only mode: returns session key DID (did:key:z6Mk...)
851
+ *
852
+ * Use this for delegations - it always returns the appropriate identity.
853
+ */
854
+ get did(): string;
855
+ /**
856
+ * Get the session key DID. Always available.
857
+ * Format: did:key:z6Mk...#z6Mk...
858
+ *
859
+ * Use this when you specifically need the session key, not the user identity.
860
+ */
861
+ get sessionDid(): string;
862
+ /**
863
+ * Get the Ethereum address for this user.
864
+ */
865
+ get address(): string | undefined;
866
+ /**
867
+ * Check if this instance is in session-only mode (no wallet).
868
+ * In session-only mode, the instance can receive delegations but cannot
869
+ * create its own space via signIn().
870
+ */
871
+ get isSessionOnly(): boolean;
872
+ /**
873
+ * Get the space ID for this user.
874
+ * Available after signIn().
875
+ */
876
+ get spaceId(): string | undefined;
877
+ /**
878
+ * Get the current TinyCloud session.
879
+ * Available after signIn().
880
+ */
881
+ get session(): TinyCloudSession | undefined;
882
+ /**
883
+ * Sign in and create a new session.
884
+ * This creates the user's space if it doesn't exist.
885
+ * Requires wallet mode (privateKey in config).
886
+ *
887
+ * @param options - Optional per-call SIWE overrides for this sign-in only
888
+ */
889
+ signIn(options?: SignInOptions): Promise<void>;
890
+ private ownedSpaceId;
891
+ private writeManifestRegistryRecords;
892
+ private ensureOwnedSpaceHosted;
893
+ /**
894
+ * Restore a previously established session from stored delegation data.
895
+ *
896
+ * This is used by the CLI to restore a session that was created via the
897
+ * browser-based delegation flow (OpenKey `/delegate` page). Instead of
898
+ * signing in with a private key, it injects the delegation data directly.
899
+ *
900
+ * @param sessionData - The stored delegation data from the browser flow
901
+ */
902
+ restoreSession(sessionData: {
903
+ delegationHeader: {
904
+ Authorization: string;
905
+ };
906
+ delegationCid: string;
907
+ spaceId: string;
908
+ jwk: object;
909
+ verificationMethod: string;
910
+ address?: string;
911
+ chainId?: number;
912
+ }): Promise<void>;
913
+ /**
914
+ * Connect a wallet to upgrade from session-only mode to wallet mode.
915
+ *
916
+ * This allows a user who started in session-only mode to later connect
917
+ * a wallet and gain the ability to create their own space.
918
+ *
919
+ * Note: This does NOT automatically sign in. Call signIn() after connecting
920
+ * the wallet to create your space.
921
+ *
922
+ * @param privateKey - The Ethereum private key (hex string, no 0x prefix)
923
+ * @param options - Optional configuration
924
+ * @param options.prefix - Space name prefix (defaults to "default")
925
+ *
926
+ * @example
927
+ * ```typescript
928
+ * // Start in session-only mode
929
+ * const node = new TinyCloudNode({ host: "https://node.tinycloud.xyz" });
930
+ * console.log(node.did); // did:key:z6Mk... (session key)
931
+ *
932
+ * // Later, connect a wallet
933
+ * node.connectWallet(privateKey);
934
+ * await node.signIn();
935
+ * console.log(node.did); // did:pkh:eip155:1:0x... (PKH)
936
+ * ```
937
+ */
938
+ connectWallet(privateKey: string, options?: {
939
+ prefix?: string;
940
+ sessionStorage?: ISessionStorage;
941
+ }): void;
942
+ /**
943
+ * Connect any ISigner to upgrade from session-only mode to wallet mode.
944
+ *
945
+ * Same as connectWallet() but accepts any ISigner implementation instead
946
+ * of a raw private key string. Use this for browser wallets, hardware wallets,
947
+ * or custom signing backends.
948
+ *
949
+ * Note: This does NOT automatically sign in. Call signIn() after connecting.
950
+ *
951
+ * @param signer - Any ISigner implementation
952
+ * @param options - Optional configuration
953
+ * @param options.prefix - Space name prefix (defaults to "default")
954
+ */
955
+ connectSigner(signer: ISigner, options?: {
956
+ prefix?: string;
957
+ sessionStorage?: ISessionStorage;
958
+ }): void;
959
+ /**
960
+ * Initialize the service context and KV service after sign-in.
961
+ * @internal
962
+ */
963
+ private initializeServices;
964
+ private createSpaceScopedKVService;
965
+ private createVaultService;
966
+ /**
967
+ * Initialize the v2 delegation system services.
968
+ * @internal
969
+ */
970
+ private initializeV2Services;
971
+ /**
972
+ * Get the session expiry time.
973
+ * @internal
974
+ */
975
+ private getSessionExpiry;
976
+ /**
977
+ * Wrapper for the WASM createDelegation function.
978
+ *
979
+ * The WASM call now takes a multi-resource `abilities` map
980
+ * (matching `prepareSession`'s shape) and emits ONE UCAN that
981
+ * covers every `(service, path, actions)` entry. We mirror the raw
982
+ * result back through `CreateDelegationWasmResult`, converting the
983
+ * seconds-since-epoch `expiry` to a Date and normalizing the
984
+ * `delegateDid` → `delegateDID` case.
985
+ *
986
+ * Both SharingService (single-entry) and
987
+ * {@link TinyCloudNode.delegateTo} (multi-entry) drive this through
988
+ * the same code path so there's exactly one place that touches the
989
+ * WASM boundary.
990
+ *
991
+ * @internal
992
+ */
993
+ private createDelegationWrapper;
994
+ /**
995
+ * Create a direct root delegation from the wallet to a share key.
996
+ * This bypasses the session delegation chain, allowing share links
997
+ * with expiry longer than the current session.
998
+ * @internal
999
+ */
1000
+ private createRootDelegationForSharing;
1001
+ /**
1002
+ * Track a received delegation in the capability registry.
1003
+ * @internal
1004
+ */
1005
+ private trackReceivedDelegation;
1006
+ /**
1007
+ * Key-value storage operations on this user's space.
1008
+ */
1009
+ get kv(): IKVService;
1010
+ /**
1011
+ * SQL database operations on this user's space.
1012
+ */
1013
+ get sql(): ISQLService;
1014
+ /**
1015
+ * Get an SQL service scoped to a specific space.
1016
+ *
1017
+ * Mirrors {@link SpaceService}'s per-space KV factory: clones the active
1018
+ * service context and overrides its session's spaceId so that subsequent
1019
+ * `sql/<dbName>/<action>` invocations route to that space. Useful when
1020
+ * the caller already holds a delegation covering the target space (e.g.
1021
+ * via {@link grantRuntimePermissions} or {@link useRuntimeDelegation})
1022
+ * but the SDK's per-space SQL surface isn't otherwise exposed.
1023
+ *
1024
+ * Does NOT auto-create the space.
1025
+ *
1026
+ * @param spaceId - Full space URI (`tinycloud:pkh:eip155:<chain>:<addr>:<name>`).
1027
+ */
1028
+ sqlForSpace(spaceId: string): ISQLService;
1029
+ /**
1030
+ * DuckDB database operations on this user's space.
1031
+ */
1032
+ get duckdb(): IDuckDbService;
1033
+ /**
1034
+ * Data Vault operations - client-side encrypted KV storage.
1035
+ * Call `vault.unlock(signer)` after signIn() to derive encryption keys.
1036
+ */
1037
+ get vault(): IDataVaultService;
1038
+ /**
1039
+ * App-facing secrets API backed by the `secrets` space vault.
1040
+ */
1041
+ get secrets(): ISecretsService;
1042
+ private getBaseSecrets;
1043
+ /**
1044
+ * Hooks write stream subscription API.
1045
+ */
1046
+ get hooks(): IHooksService;
1047
+ /**
1048
+ * Get the CapabilityKeyRegistry for managing keys and their capabilities.
1049
+ *
1050
+ * The registry tracks keys (session, main, ingested) and their associated
1051
+ * delegations, enabling automatic key selection for operations.
1052
+ *
1053
+ * @example
1054
+ * ```typescript
1055
+ * const registry = alice.capabilityRegistry;
1056
+ *
1057
+ * // Get the best key for an operation
1058
+ * const key = registry.getKeyForCapability(
1059
+ * "tinycloud://my-space/kv/data",
1060
+ * "tinycloud.kv/get"
1061
+ * );
1062
+ *
1063
+ * // List all capabilities
1064
+ * const capabilities = registry.getAllCapabilities();
1065
+ * ```
1066
+ */
1067
+ get capabilityRegistry(): ICapabilityKeyRegistry;
1068
+ /**
1069
+ * Access received delegations (recipient view).
1070
+ *
1071
+ * Use this to see what delegations have been received via useDelegation().
1072
+ *
1073
+ * @example
1074
+ * ```typescript
1075
+ * // List all received delegations
1076
+ * const received = bob.delegations.list();
1077
+ * console.log("I have access to:", received.length, "spaces");
1078
+ *
1079
+ * // Get a specific delegation by CID
1080
+ * const delegation = bob.delegations.get(cid);
1081
+ * ```
1082
+ */
1083
+ get delegations(): {
1084
+ /** List all received delegations */
1085
+ list: () => Delegation[];
1086
+ /** Get a delegation by CID */
1087
+ get: (cid: string) => Delegation | undefined;
1088
+ };
1089
+ /**
1090
+ * Check whether the current session or an approved runtime delegation covers
1091
+ * every requested permission.
1092
+ */
1093
+ hasRuntimePermissions(permissions: PermissionEntry[]): boolean;
1094
+ /**
1095
+ * Return installed runtime permission delegations. When `permissions` is
1096
+ * provided, only delegations currently covering those permissions are
1097
+ * returned. Base-session manifest permissions are not represented here.
1098
+ */
1099
+ getRuntimePermissionDelegations(permissions?: PermissionEntry[]): PortableDelegation[];
1100
+ /**
1101
+ * Install a portable runtime permission delegation into this SDK instance so
1102
+ * matching service calls and downstream `delegateTo()` calls can use it.
1103
+ */
1104
+ useRuntimeDelegation(delegation: PortableDelegation): Promise<void>;
1105
+ /**
1106
+ * Store additional permissions as narrow delegations to the current session
1107
+ * key. Future service invocations automatically use a stored delegation when
1108
+ * its `(space, service, path, action)` covers the request.
1109
+ */
1110
+ grantRuntimePermissions(permissions: PermissionEntry[], options?: RuntimePermissionGrantOptions): Promise<PortableDelegation[]>;
1111
+ /**
1112
+ * Get the DelegationManager for delegation CRUD operations.
1113
+ *
1114
+ * This is the v2 delegation service providing a cleaner API than
1115
+ * the legacy createDelegation/useDelegation methods.
1116
+ *
1117
+ * @example
1118
+ * ```typescript
1119
+ * const delegations = alice.delegationManager;
1120
+ *
1121
+ * // Create a delegation
1122
+ * const result = await delegations.create({
1123
+ * delegateDID: bob.did,
1124
+ * path: "shared/",
1125
+ * actions: ["tinycloud.kv/get", "tinycloud.kv/put"],
1126
+ * expiry: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 hours
1127
+ * });
1128
+ *
1129
+ * // List delegations
1130
+ * const listResult = await delegations.list();
1131
+ *
1132
+ * // Revoke a delegation
1133
+ * await delegations.revoke(delegationCid);
1134
+ * ```
1135
+ */
1136
+ get delegationManager(): DelegationManager;
1137
+ /**
1138
+ * Get the SpaceService for managing spaces.
1139
+ *
1140
+ * The SpaceService provides access to owned and delegated spaces,
1141
+ * including space creation, listing, and scoped operations.
1142
+ *
1143
+ * @example
1144
+ * ```typescript
1145
+ * const spaces = alice.spaces;
1146
+ *
1147
+ * // List all accessible spaces
1148
+ * const result = await spaces.list();
1149
+ *
1150
+ * // Create a new space
1151
+ * const createResult = await spaces.create('photos');
1152
+ *
1153
+ * // Get a space object for operations
1154
+ * const mySpace = spaces.get('default');
1155
+ * await mySpace.kv.put('key', 'value');
1156
+ *
1157
+ * // Check if a space exists
1158
+ * const exists = await spaces.exists('photos');
1159
+ * ```
1160
+ */
1161
+ get spaces(): ISpaceService;
1162
+ /**
1163
+ * Alias for `spaces` - get the SpaceService.
1164
+ * @see spaces
1165
+ */
1166
+ get spaceService(): ISpaceService;
1167
+ /**
1168
+ * Get a Space object by short name or full URI.
1169
+ */
1170
+ space(nameOrUri: string): ISpace;
1171
+ /**
1172
+ * Get the SharingService for creating and receiving v2 sharing links.
1173
+ *
1174
+ * The SharingService creates sharing links with embedded private keys,
1175
+ * allowing recipients to exercise delegations without prior session setup.
1176
+ *
1177
+ * @example
1178
+ * ```typescript
1179
+ * const sharing = alice.sharing;
1180
+ *
1181
+ * // Generate a sharing link
1182
+ * const result = await sharing.generate({
1183
+ * path: "/kv/documents/report.pdf",
1184
+ * actions: ["tinycloud.kv/get"],
1185
+ * expiry: new Date(Date.now() + 24 * 60 * 60 * 1000),
1186
+ * });
1187
+ *
1188
+ * if (result.ok) {
1189
+ * console.log("Share URL:", result.data.url);
1190
+ * // Send the URL to the recipient
1191
+ * }
1192
+ *
1193
+ * // Receive a sharing link
1194
+ * const receiveResult = await sharing.receive(shareUrl);
1195
+ * if (receiveResult.ok) {
1196
+ * // Use the pre-configured KV service
1197
+ * const data = await receiveResult.data.kv.get("report.pdf");
1198
+ * }
1199
+ * ```
1200
+ */
1201
+ get sharing(): ISharingService;
1202
+ /**
1203
+ * Alias for `sharing` - get the SharingService.
1204
+ * @see sharing
1205
+ */
1206
+ get sharingService(): ISharingService;
1207
+ /**
1208
+ * Ensure the user's public space exists and is accessible.
1209
+ * Creates the space and activates a session delegation for it.
1210
+ * This is the trigger for lazy public space creation — call it
1211
+ * before writing to spaces.get('public').kv.
1212
+ */
1213
+ ensurePublicSpace(): Promise<void>;
1214
+ /**
1215
+ * Get a KVService scoped to the user's own public space.
1216
+ * Writes require authentication (owner/delegate).
1217
+ */
1218
+ get publicKV(): IKVService;
1219
+ /**
1220
+ * Create a delegation using the v2 DelegationManager.
1221
+ *
1222
+ * This is a convenience method that wraps DelegationManager.create().
1223
+ * For more control, use `this.delegationManager` directly.
1224
+ *
1225
+ * @param params - Delegation parameters
1226
+ * @returns Result containing the created Delegation
1227
+ *
1228
+ * @example
1229
+ * ```typescript
1230
+ * const result = await alice.delegate({
1231
+ * delegateDID: bob.did,
1232
+ * path: "shared/",
1233
+ * actions: ["tinycloud.kv/get", "tinycloud.kv/put"],
1234
+ * expiry: new Date(Date.now() + 24 * 60 * 60 * 1000),
1235
+ * });
1236
+ *
1237
+ * if (result.ok) {
1238
+ * console.log("Delegation created:", result.data.cid);
1239
+ * }
1240
+ * ```
1241
+ */
1242
+ delegate(params: CreateDelegationParams): Promise<DelegationResult<Delegation>>;
1243
+ /**
1244
+ * Revoke a delegation using the v2 DelegationManager.
1245
+ *
1246
+ * @param cid - The CID of the delegation to revoke
1247
+ * @returns Result indicating success or failure
1248
+ */
1249
+ revokeDelegation(cid: string): Promise<DelegationResult<void>>;
1250
+ /**
1251
+ * List all delegations for the current session's space.
1252
+ *
1253
+ * @returns Result containing an array of Delegations
1254
+ */
1255
+ listDelegations(): Promise<DelegationResult<Delegation[]>>;
1256
+ /**
1257
+ * Check if the current session has permission for a path and action.
1258
+ *
1259
+ * @param path - The resource path to check
1260
+ * @param action - The action to check (e.g., "tinycloud.kv/get")
1261
+ * @returns Result containing boolean permission status
1262
+ */
1263
+ checkPermission(path: string, action: string): Promise<DelegationResult<boolean>>;
1264
+ /**
1265
+ * Safety margin before the session's own expiry at which {@link delegateTo}
1266
+ * will refuse to issue a derived delegation. Prevents issuing sub-delegations
1267
+ * that would be invalid by the time the recipient used them. Spec: 60 seconds.
1268
+ *
1269
+ * @internal
1270
+ */
1271
+ private static readonly SESSION_EXPIRY_SAFETY_MARGIN_MS;
1272
+ /**
1273
+ * Issue a delegation using the capability-chain flow.
1274
+ *
1275
+ * When every requested permission is a subset of the current
1276
+ * session's recap, or of one installed runtime permission delegation,
1277
+ * the delegation is signed by the session key via WASM — no wallet
1278
+ * prompt. When at least one is NOT derivable, a
1279
+ * {@link PermissionNotInManifestError} is raised (carrying the
1280
+ * missing entries) so the caller can trigger an escalation flow
1281
+ * (e.g. `TinyCloudWeb.requestPermissions`). Passing
1282
+ * `forceWalletSign: true` bypasses the derivability check and
1283
+ * always uses the wallet-signed SIWE path — used by the legacy
1284
+ * `createDelegation` fallback and by callers that want explicit
1285
+ * wallet confirmation.
1286
+ *
1287
+ * Multi-entry delegations are now emitted as **one** signed UCAN:
1288
+ * the underlying WASM `createDelegation` takes a full
1289
+ * `HashMap<Service, HashMap<Path, Vec<Ability>>>` abilities map
1290
+ * and produces a single attenuation carrying every
1291
+ * `(service, path, actions)` entry. The returned
1292
+ * {@link DelegateToResult.delegation} is that single blob, and
1293
+ * apps can POST it to their backend exactly like a single-entry
1294
+ * delegation (the server verifies all granted resources from one
1295
+ * UCAN).
1296
+ *
1297
+ * For single-entry requests the `PortableDelegation.path` and
1298
+ * `.actions` fields mirror the one granted entry. For
1299
+ * multi-entry requests they mirror the **first** entry (stable
1300
+ * lexicographic order from the Rust side); consumers that need
1301
+ * the full picture read `PortableDelegation.resources`.
1302
+ *
1303
+ * @throws {@link SessionExpiredError} when there is no session or
1304
+ * the current session has expired (or will within the 60s
1305
+ * safety margin).
1306
+ * @throws {@link PermissionNotInManifestError} when any requested
1307
+ * entry is not a subset of the granted session capabilities and
1308
+ * `forceWalletSign` is not set.
1309
+ */
1310
+ delegateTo(did: string, permissions: PermissionEntry[], options?: DelegateToOptions): Promise<DelegateToResult>;
1311
+ /**
1312
+ * Materialize one manifest-declared delegation using the current session key.
1313
+ * Delivery is intentionally out of band; callers decide how to transmit the
1314
+ * returned UCAN to the delegate.
1315
+ */
1316
+ materializeDelegation(did: string, request?: ComposedManifestRequest | undefined): Promise<DelegateToResult & {
1317
+ target: ResolvedDelegate;
1318
+ }>;
1319
+ /**
1320
+ * Materialize every delegation target declared by the composed manifest
1321
+ * request. This does not deliver the delegations anywhere.
1322
+ */
1323
+ materializeDelegations(request?: ComposedManifestRequest | undefined): Promise<Array<DelegateToResult & {
1324
+ target: ResolvedDelegate;
1325
+ }>>;
1326
+ /**
1327
+ * Issue a delegation via the session-key UCAN WASM path.
1328
+ *
1329
+ * The caller has already verified every entry is derivable from
1330
+ * the current session; we build one multi-resource abilities map
1331
+ * and emit one signed UCAN covering them all.
1332
+ *
1333
+ * All entries must share the same target space (the UCAN is
1334
+ * scoped to a single space). If they don't, this throws — mixing
1335
+ * spaces in a single delegation is not supported by the underlying
1336
+ * Rust create_delegation call and the resulting UCAN would be
1337
+ * under-specified.
1338
+ *
1339
+ * @internal
1340
+ */
1341
+ private createDelegationViaWasmPath;
1342
+ private createDelegationViaRuntimeGrant;
1343
+ private resolvePermissionSpace;
1344
+ private expandPermissionEntries;
1345
+ private shortServiceName;
1346
+ private permissionsToAbilities;
1347
+ private permissionOperations;
1348
+ private sessionCoversPermissionEntries;
1349
+ private permissionEntriesToOperations;
1350
+ private findRuntimeGrantsForPermissionEntries;
1351
+ private runtimeDelegationFromSession;
1352
+ private runtimeGrantFromDelegation;
1353
+ private delegatedResourcesForEntries;
1354
+ private operationsFromDelegation;
1355
+ private flatDelegationResources;
1356
+ private selectInvocationSession;
1357
+ private findGrantForOperations;
1358
+ private findGrantForOperation;
1359
+ private pruneExpiredRuntimePermissionGrants;
1360
+ private operationCovers;
1361
+ private actionContains;
1362
+ private invocationServiceName;
1363
+ private pathContains;
1364
+ /**
1365
+ * Issue a delegation via the legacy wallet-signed SIWE path for a single
1366
+ * {@link PermissionEntry}. Shares the implementation with the public
1367
+ * `createDelegation` method via {@link createDelegationWalletPath} so
1368
+ * both entry points hit exactly the same SIWE / signer / public-space
1369
+ * logic without mutual recursion.
1370
+ *
1371
+ * @internal
1372
+ */
1373
+ private createDelegationLegacyWalletPath;
1374
+ /**
1375
+ * Create a delegation from this user to another user.
1376
+ *
1377
+ * The delegation grants the recipient access to a specific path and actions
1378
+ * within this user's space.
1379
+ *
1380
+ * @param params - Delegation parameters
1381
+ * @returns A portable delegation that can be sent to the recipient
1382
+ */
1383
+ createDelegation(params: {
1384
+ /** Path within the space to delegate access to */
1385
+ path: string;
1386
+ /** Actions to allow (e.g., ["tinycloud.kv/get", "tinycloud.kv/put"]) */
1387
+ actions: string[];
1388
+ /** DID of the recipient (from their TinyCloudNode.did) */
1389
+ delegateDID: string;
1390
+ /** Whether to prevent the recipient from creating sub-delegations (default: false) */
1391
+ disableSubDelegation?: boolean;
1392
+ /** Expiration time in milliseconds from now (default: 1 hour) */
1393
+ expiryMs?: number;
1394
+ /** Override space ID (for creating delegations to non-primary spaces like public) */
1395
+ spaceIdOverride?: string;
1396
+ /** Include a companion delegation for the user's public space (default: true) */
1397
+ includePublicSpace?: boolean;
1398
+ }): Promise<PortableDelegation>;
1399
+ /**
1400
+ * Legacy wallet-signed SIWE delegation path. Lifted from the original
1401
+ * `createDelegation` body verbatim so both the legacy public method and
1402
+ * `delegateTo({ forceWalletSign: true })` hit the same code.
1403
+ *
1404
+ * @internal
1405
+ */
1406
+ private createDelegationWalletPath;
1407
+ /**
1408
+ * Use a delegation received from another user.
1409
+ *
1410
+ * This creates a new session key for this user that chains from the
1411
+ * received delegation, allowing operations on the delegator's space.
1412
+ *
1413
+ * Works in both modes:
1414
+ * - **Wallet mode**: Creates a SIWE sub-delegation from PKH to session key
1415
+ * - **Session-only mode**: Uses the delegation directly (must target session key DID)
1416
+ *
1417
+ * @param delegation - The PortableDelegation to use (from createDelegation or transport)
1418
+ * @returns A DelegatedAccess instance for performing operations
1419
+ */
1420
+ useDelegation(delegation: PortableDelegation): Promise<DelegatedAccess>;
1421
+ /**
1422
+ * Create a sub-delegation from a received delegation.
1423
+ *
1424
+ * This allows further delegating access that was received from another user,
1425
+ * if the original delegation allows sub-delegation.
1426
+ *
1427
+ * @param parentDelegation - The delegation received from another user
1428
+ * @param params - Sub-delegation parameters (must be within parent's scope)
1429
+ * @returns A portable delegation for the sub-delegate
1430
+ */
1431
+ createSubDelegation(parentDelegation: PortableDelegation, params: {
1432
+ /** Path within the delegated path to sub-delegate */
1433
+ path: string;
1434
+ /** Actions to allow (must be subset of parent's actions) */
1435
+ actions: string[];
1436
+ /** DID of the recipient */
1437
+ delegateDID: string;
1438
+ /** Whether to prevent the recipient from creating further sub-delegations */
1439
+ disableSubDelegation?: boolean;
1440
+ /** Expiration time in milliseconds from now (must be before parent's expiry) */
1441
+ expiryMs?: number;
1442
+ }): Promise<PortableDelegation>;
1443
+ }
1444
+
1445
+ /**
1446
+ * WasmKeyProvider - KeyProvider implementation using WASM session manager.
1447
+ *
1448
+ * This provider wraps the SessionManager from node-sdk-wasm to provide
1449
+ * cryptographic key operations required by the SharingService.
1450
+ *
1451
+ * @packageDocumentation
1452
+ */
1453
+
1454
+ /**
1455
+ * Extended session manager with optional key listing support.
1456
+ * The base ISessionManager doesn't include listSessionKeys(),
1457
+ * but concrete implementations (e.g., TCWSessionManager) may provide it.
1458
+ */
1459
+ interface SessionManagerWithListing extends ISessionManager {
1460
+ listSessionKeys?(): string[];
1461
+ }
1462
+ /**
1463
+ * Configuration for WasmKeyProvider.
1464
+ */
1465
+ interface WasmKeyProviderConfig {
1466
+ /**
1467
+ * The WASM session manager instance.
1468
+ * Must be created before constructing the KeyProvider.
1469
+ */
1470
+ sessionManager: SessionManagerWithListing;
1471
+ }
1472
+ /**
1473
+ * KeyProvider implementation that wraps the WASM session manager.
1474
+ *
1475
+ * This allows the SharingService to create new session keys for sharing links
1476
+ * using the same cryptographic operations as the main session management.
1477
+ *
1478
+ * @example
1479
+ * ```typescript
1480
+ * // sessionManager from wasmBindings.createSessionManager()
1481
+ * import { WasmKeyProvider } from "@tinycloud/node-sdk";
1482
+ *
1483
+ * const sessionManager = new SessionManager();
1484
+ * const keyProvider = new WasmKeyProvider({ sessionManager });
1485
+ *
1486
+ * // Create a session key for a sharing link
1487
+ * const keyId = await keyProvider.createSessionKey("share:abc123");
1488
+ * const jwk = keyProvider.getJWK(keyId);
1489
+ * const did = await keyProvider.getDID(keyId);
1490
+ * ```
1491
+ */
1492
+ declare class WasmKeyProvider implements KeyProvider {
1493
+ private sessionManager;
1494
+ /**
1495
+ * Create a new WasmKeyProvider.
1496
+ *
1497
+ * @param config - Configuration with the WASM session manager
1498
+ */
1499
+ constructor(config: WasmKeyProviderConfig);
1500
+ /**
1501
+ * Generate a new session key with the given name.
1502
+ *
1503
+ * This creates a new Ed25519 key pair in the WASM session manager.
1504
+ * The key can then be used for signing delegations in sharing links.
1505
+ *
1506
+ * @param name - A unique name/ID for the key (e.g., "share:timestamp:random")
1507
+ * @returns The key ID (same as the name provided)
1508
+ */
1509
+ createSessionKey(name: string): Promise<string>;
1510
+ /**
1511
+ * Get the JWK (JSON Web Key) for a key.
1512
+ *
1513
+ * Returns the full JWK including the private key (d parameter),
1514
+ * which is required for signing and for embedding in sharing links.
1515
+ *
1516
+ * @param keyId - The key ID to retrieve
1517
+ * @returns The JWK object with public and private key components
1518
+ * @throws Error if the key is not found
1519
+ */
1520
+ getJWK(keyId: string): JWK;
1521
+ /**
1522
+ * Get the DID (Decentralized Identifier) for a key.
1523
+ *
1524
+ * Returns the did:key format DID derived from the key's public key.
1525
+ * This DID can be used as the delegatee in delegations.
1526
+ *
1527
+ * @param keyId - The key ID to retrieve
1528
+ * @returns The DID in did:key format (e.g., "did:key:z6Mk...")
1529
+ */
1530
+ getDID(keyId: string): Promise<string>;
1531
+ /**
1532
+ * List all session keys currently held by the provider.
1533
+ *
1534
+ * @returns Array of key IDs
1535
+ */
1536
+ listKeys(): string[];
1537
+ /**
1538
+ * Check if a key exists in the provider.
1539
+ *
1540
+ * @param keyId - The key ID to check
1541
+ * @returns True if the key exists
1542
+ */
1543
+ hasKey(keyId: string): boolean;
1544
+ }
1545
+ /**
1546
+ * Create a new WasmKeyProvider instance.
1547
+ *
1548
+ * @param sessionManager - The WASM session manager
1549
+ * @returns A new WasmKeyProvider instance
1550
+ */
1551
+ declare function createWasmKeyProvider(sessionManager: SessionManagerWithListing): WasmKeyProvider;
1552
+
1553
+ export { type DelegateToOptions as D, FileSessionStorage as F, MemorySessionStorage as M, type NodeEventEmitterStrategy as N, type PortableDelegation as P, type RestorableSession as R, type SignStrategy as S, TinyCloudNode as T, WasmKeyProvider as W, type DelegateToResult as a, DelegatedAccess as b, NodeUserAuthorization as c, type NodeUserAuthorizationConfig as d, type RuntimePermissionGrantOptions as e, type TinyCloudNodeConfig as f, type WasmKeyProviderConfig as g, createWasmKeyProvider as h, defaultSignStrategy as i, deserializeDelegation as j, serializeDelegation as s };