@tinycloud/node-sdk 2.2.0-beta.0 → 2.2.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core-DcJ27GsA.d.cts +1538 -0
- package/dist/core-DcJ27GsA.d.ts +1538 -0
- package/dist/core.cjs +906 -183
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.cts +4 -1423
- package/dist/core.d.ts +4 -1423
- package/dist/core.js +827 -93
- package/dist/core.js.map +1 -1
- package/dist/index.cjs +909 -186
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +822 -93
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
|
@@ -0,0 +1,1538 @@
|
|
|
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
|
+
* DuckDB database operations on this user's space.
|
|
1016
|
+
*/
|
|
1017
|
+
get duckdb(): IDuckDbService;
|
|
1018
|
+
/**
|
|
1019
|
+
* Data Vault operations - client-side encrypted KV storage.
|
|
1020
|
+
* Call `vault.unlock(signer)` after signIn() to derive encryption keys.
|
|
1021
|
+
*/
|
|
1022
|
+
get vault(): IDataVaultService;
|
|
1023
|
+
/**
|
|
1024
|
+
* App-facing secrets API backed by the `secrets` space vault.
|
|
1025
|
+
*/
|
|
1026
|
+
get secrets(): ISecretsService;
|
|
1027
|
+
private getBaseSecrets;
|
|
1028
|
+
/**
|
|
1029
|
+
* Hooks write stream subscription API.
|
|
1030
|
+
*/
|
|
1031
|
+
get hooks(): IHooksService;
|
|
1032
|
+
/**
|
|
1033
|
+
* Get the CapabilityKeyRegistry for managing keys and their capabilities.
|
|
1034
|
+
*
|
|
1035
|
+
* The registry tracks keys (session, main, ingested) and their associated
|
|
1036
|
+
* delegations, enabling automatic key selection for operations.
|
|
1037
|
+
*
|
|
1038
|
+
* @example
|
|
1039
|
+
* ```typescript
|
|
1040
|
+
* const registry = alice.capabilityRegistry;
|
|
1041
|
+
*
|
|
1042
|
+
* // Get the best key for an operation
|
|
1043
|
+
* const key = registry.getKeyForCapability(
|
|
1044
|
+
* "tinycloud://my-space/kv/data",
|
|
1045
|
+
* "tinycloud.kv/get"
|
|
1046
|
+
* );
|
|
1047
|
+
*
|
|
1048
|
+
* // List all capabilities
|
|
1049
|
+
* const capabilities = registry.getAllCapabilities();
|
|
1050
|
+
* ```
|
|
1051
|
+
*/
|
|
1052
|
+
get capabilityRegistry(): ICapabilityKeyRegistry;
|
|
1053
|
+
/**
|
|
1054
|
+
* Access received delegations (recipient view).
|
|
1055
|
+
*
|
|
1056
|
+
* Use this to see what delegations have been received via useDelegation().
|
|
1057
|
+
*
|
|
1058
|
+
* @example
|
|
1059
|
+
* ```typescript
|
|
1060
|
+
* // List all received delegations
|
|
1061
|
+
* const received = bob.delegations.list();
|
|
1062
|
+
* console.log("I have access to:", received.length, "spaces");
|
|
1063
|
+
*
|
|
1064
|
+
* // Get a specific delegation by CID
|
|
1065
|
+
* const delegation = bob.delegations.get(cid);
|
|
1066
|
+
* ```
|
|
1067
|
+
*/
|
|
1068
|
+
get delegations(): {
|
|
1069
|
+
/** List all received delegations */
|
|
1070
|
+
list: () => Delegation[];
|
|
1071
|
+
/** Get a delegation by CID */
|
|
1072
|
+
get: (cid: string) => Delegation | undefined;
|
|
1073
|
+
};
|
|
1074
|
+
/**
|
|
1075
|
+
* Check whether the current session or an approved runtime delegation covers
|
|
1076
|
+
* every requested permission.
|
|
1077
|
+
*/
|
|
1078
|
+
hasRuntimePermissions(permissions: PermissionEntry[]): boolean;
|
|
1079
|
+
/**
|
|
1080
|
+
* Return installed runtime permission delegations. When `permissions` is
|
|
1081
|
+
* provided, only delegations currently covering those permissions are
|
|
1082
|
+
* returned. Base-session manifest permissions are not represented here.
|
|
1083
|
+
*/
|
|
1084
|
+
getRuntimePermissionDelegations(permissions?: PermissionEntry[]): PortableDelegation[];
|
|
1085
|
+
/**
|
|
1086
|
+
* Install a portable runtime permission delegation into this SDK instance so
|
|
1087
|
+
* matching service calls and downstream `delegateTo()` calls can use it.
|
|
1088
|
+
*/
|
|
1089
|
+
useRuntimeDelegation(delegation: PortableDelegation): Promise<void>;
|
|
1090
|
+
/**
|
|
1091
|
+
* Store additional permissions as narrow delegations to the current session
|
|
1092
|
+
* key. Future service invocations automatically use a stored delegation when
|
|
1093
|
+
* its `(space, service, path, action)` covers the request.
|
|
1094
|
+
*/
|
|
1095
|
+
grantRuntimePermissions(permissions: PermissionEntry[], options?: RuntimePermissionGrantOptions): Promise<PortableDelegation[]>;
|
|
1096
|
+
/**
|
|
1097
|
+
* Get the DelegationManager for delegation CRUD operations.
|
|
1098
|
+
*
|
|
1099
|
+
* This is the v2 delegation service providing a cleaner API than
|
|
1100
|
+
* the legacy createDelegation/useDelegation methods.
|
|
1101
|
+
*
|
|
1102
|
+
* @example
|
|
1103
|
+
* ```typescript
|
|
1104
|
+
* const delegations = alice.delegationManager;
|
|
1105
|
+
*
|
|
1106
|
+
* // Create a delegation
|
|
1107
|
+
* const result = await delegations.create({
|
|
1108
|
+
* delegateDID: bob.did,
|
|
1109
|
+
* path: "shared/",
|
|
1110
|
+
* actions: ["tinycloud.kv/get", "tinycloud.kv/put"],
|
|
1111
|
+
* expiry: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 hours
|
|
1112
|
+
* });
|
|
1113
|
+
*
|
|
1114
|
+
* // List delegations
|
|
1115
|
+
* const listResult = await delegations.list();
|
|
1116
|
+
*
|
|
1117
|
+
* // Revoke a delegation
|
|
1118
|
+
* await delegations.revoke(delegationCid);
|
|
1119
|
+
* ```
|
|
1120
|
+
*/
|
|
1121
|
+
get delegationManager(): DelegationManager;
|
|
1122
|
+
/**
|
|
1123
|
+
* Get the SpaceService for managing spaces.
|
|
1124
|
+
*
|
|
1125
|
+
* The SpaceService provides access to owned and delegated spaces,
|
|
1126
|
+
* including space creation, listing, and scoped operations.
|
|
1127
|
+
*
|
|
1128
|
+
* @example
|
|
1129
|
+
* ```typescript
|
|
1130
|
+
* const spaces = alice.spaces;
|
|
1131
|
+
*
|
|
1132
|
+
* // List all accessible spaces
|
|
1133
|
+
* const result = await spaces.list();
|
|
1134
|
+
*
|
|
1135
|
+
* // Create a new space
|
|
1136
|
+
* const createResult = await spaces.create('photos');
|
|
1137
|
+
*
|
|
1138
|
+
* // Get a space object for operations
|
|
1139
|
+
* const mySpace = spaces.get('default');
|
|
1140
|
+
* await mySpace.kv.put('key', 'value');
|
|
1141
|
+
*
|
|
1142
|
+
* // Check if a space exists
|
|
1143
|
+
* const exists = await spaces.exists('photos');
|
|
1144
|
+
* ```
|
|
1145
|
+
*/
|
|
1146
|
+
get spaces(): ISpaceService;
|
|
1147
|
+
/**
|
|
1148
|
+
* Alias for `spaces` - get the SpaceService.
|
|
1149
|
+
* @see spaces
|
|
1150
|
+
*/
|
|
1151
|
+
get spaceService(): ISpaceService;
|
|
1152
|
+
/**
|
|
1153
|
+
* Get a Space object by short name or full URI.
|
|
1154
|
+
*/
|
|
1155
|
+
space(nameOrUri: string): ISpace;
|
|
1156
|
+
/**
|
|
1157
|
+
* Get the SharingService for creating and receiving v2 sharing links.
|
|
1158
|
+
*
|
|
1159
|
+
* The SharingService creates sharing links with embedded private keys,
|
|
1160
|
+
* allowing recipients to exercise delegations without prior session setup.
|
|
1161
|
+
*
|
|
1162
|
+
* @example
|
|
1163
|
+
* ```typescript
|
|
1164
|
+
* const sharing = alice.sharing;
|
|
1165
|
+
*
|
|
1166
|
+
* // Generate a sharing link
|
|
1167
|
+
* const result = await sharing.generate({
|
|
1168
|
+
* path: "/kv/documents/report.pdf",
|
|
1169
|
+
* actions: ["tinycloud.kv/get"],
|
|
1170
|
+
* expiry: new Date(Date.now() + 24 * 60 * 60 * 1000),
|
|
1171
|
+
* });
|
|
1172
|
+
*
|
|
1173
|
+
* if (result.ok) {
|
|
1174
|
+
* console.log("Share URL:", result.data.url);
|
|
1175
|
+
* // Send the URL to the recipient
|
|
1176
|
+
* }
|
|
1177
|
+
*
|
|
1178
|
+
* // Receive a sharing link
|
|
1179
|
+
* const receiveResult = await sharing.receive(shareUrl);
|
|
1180
|
+
* if (receiveResult.ok) {
|
|
1181
|
+
* // Use the pre-configured KV service
|
|
1182
|
+
* const data = await receiveResult.data.kv.get("report.pdf");
|
|
1183
|
+
* }
|
|
1184
|
+
* ```
|
|
1185
|
+
*/
|
|
1186
|
+
get sharing(): ISharingService;
|
|
1187
|
+
/**
|
|
1188
|
+
* Alias for `sharing` - get the SharingService.
|
|
1189
|
+
* @see sharing
|
|
1190
|
+
*/
|
|
1191
|
+
get sharingService(): ISharingService;
|
|
1192
|
+
/**
|
|
1193
|
+
* Ensure the user's public space exists and is accessible.
|
|
1194
|
+
* Creates the space and activates a session delegation for it.
|
|
1195
|
+
* This is the trigger for lazy public space creation — call it
|
|
1196
|
+
* before writing to spaces.get('public').kv.
|
|
1197
|
+
*/
|
|
1198
|
+
ensurePublicSpace(): Promise<void>;
|
|
1199
|
+
/**
|
|
1200
|
+
* Get a KVService scoped to the user's own public space.
|
|
1201
|
+
* Writes require authentication (owner/delegate).
|
|
1202
|
+
*/
|
|
1203
|
+
get publicKV(): IKVService;
|
|
1204
|
+
/**
|
|
1205
|
+
* Create a delegation using the v2 DelegationManager.
|
|
1206
|
+
*
|
|
1207
|
+
* This is a convenience method that wraps DelegationManager.create().
|
|
1208
|
+
* For more control, use `this.delegationManager` directly.
|
|
1209
|
+
*
|
|
1210
|
+
* @param params - Delegation parameters
|
|
1211
|
+
* @returns Result containing the created Delegation
|
|
1212
|
+
*
|
|
1213
|
+
* @example
|
|
1214
|
+
* ```typescript
|
|
1215
|
+
* const result = await alice.delegate({
|
|
1216
|
+
* delegateDID: bob.did,
|
|
1217
|
+
* path: "shared/",
|
|
1218
|
+
* actions: ["tinycloud.kv/get", "tinycloud.kv/put"],
|
|
1219
|
+
* expiry: new Date(Date.now() + 24 * 60 * 60 * 1000),
|
|
1220
|
+
* });
|
|
1221
|
+
*
|
|
1222
|
+
* if (result.ok) {
|
|
1223
|
+
* console.log("Delegation created:", result.data.cid);
|
|
1224
|
+
* }
|
|
1225
|
+
* ```
|
|
1226
|
+
*/
|
|
1227
|
+
delegate(params: CreateDelegationParams): Promise<DelegationResult<Delegation>>;
|
|
1228
|
+
/**
|
|
1229
|
+
* Revoke a delegation using the v2 DelegationManager.
|
|
1230
|
+
*
|
|
1231
|
+
* @param cid - The CID of the delegation to revoke
|
|
1232
|
+
* @returns Result indicating success or failure
|
|
1233
|
+
*/
|
|
1234
|
+
revokeDelegation(cid: string): Promise<DelegationResult<void>>;
|
|
1235
|
+
/**
|
|
1236
|
+
* List all delegations for the current session's space.
|
|
1237
|
+
*
|
|
1238
|
+
* @returns Result containing an array of Delegations
|
|
1239
|
+
*/
|
|
1240
|
+
listDelegations(): Promise<DelegationResult<Delegation[]>>;
|
|
1241
|
+
/**
|
|
1242
|
+
* Check if the current session has permission for a path and action.
|
|
1243
|
+
*
|
|
1244
|
+
* @param path - The resource path to check
|
|
1245
|
+
* @param action - The action to check (e.g., "tinycloud.kv/get")
|
|
1246
|
+
* @returns Result containing boolean permission status
|
|
1247
|
+
*/
|
|
1248
|
+
checkPermission(path: string, action: string): Promise<DelegationResult<boolean>>;
|
|
1249
|
+
/**
|
|
1250
|
+
* Safety margin before the session's own expiry at which {@link delegateTo}
|
|
1251
|
+
* will refuse to issue a derived delegation. Prevents issuing sub-delegations
|
|
1252
|
+
* that would be invalid by the time the recipient used them. Spec: 60 seconds.
|
|
1253
|
+
*
|
|
1254
|
+
* @internal
|
|
1255
|
+
*/
|
|
1256
|
+
private static readonly SESSION_EXPIRY_SAFETY_MARGIN_MS;
|
|
1257
|
+
/**
|
|
1258
|
+
* Issue a delegation using the capability-chain flow.
|
|
1259
|
+
*
|
|
1260
|
+
* When every requested permission is a subset of the current
|
|
1261
|
+
* session's recap, or of one installed runtime permission delegation,
|
|
1262
|
+
* the delegation is signed by the session key via WASM — no wallet
|
|
1263
|
+
* prompt. When at least one is NOT derivable, a
|
|
1264
|
+
* {@link PermissionNotInManifestError} is raised (carrying the
|
|
1265
|
+
* missing entries) so the caller can trigger an escalation flow
|
|
1266
|
+
* (e.g. `TinyCloudWeb.requestPermissions`). Passing
|
|
1267
|
+
* `forceWalletSign: true` bypasses the derivability check and
|
|
1268
|
+
* always uses the wallet-signed SIWE path — used by the legacy
|
|
1269
|
+
* `createDelegation` fallback and by callers that want explicit
|
|
1270
|
+
* wallet confirmation.
|
|
1271
|
+
*
|
|
1272
|
+
* Multi-entry delegations are now emitted as **one** signed UCAN:
|
|
1273
|
+
* the underlying WASM `createDelegation` takes a full
|
|
1274
|
+
* `HashMap<Service, HashMap<Path, Vec<Ability>>>` abilities map
|
|
1275
|
+
* and produces a single attenuation carrying every
|
|
1276
|
+
* `(service, path, actions)` entry. The returned
|
|
1277
|
+
* {@link DelegateToResult.delegation} is that single blob, and
|
|
1278
|
+
* apps can POST it to their backend exactly like a single-entry
|
|
1279
|
+
* delegation (the server verifies all granted resources from one
|
|
1280
|
+
* UCAN).
|
|
1281
|
+
*
|
|
1282
|
+
* For single-entry requests the `PortableDelegation.path` and
|
|
1283
|
+
* `.actions` fields mirror the one granted entry. For
|
|
1284
|
+
* multi-entry requests they mirror the **first** entry (stable
|
|
1285
|
+
* lexicographic order from the Rust side); consumers that need
|
|
1286
|
+
* the full picture read `PortableDelegation.resources`.
|
|
1287
|
+
*
|
|
1288
|
+
* @throws {@link SessionExpiredError} when there is no session or
|
|
1289
|
+
* the current session has expired (or will within the 60s
|
|
1290
|
+
* safety margin).
|
|
1291
|
+
* @throws {@link PermissionNotInManifestError} when any requested
|
|
1292
|
+
* entry is not a subset of the granted session capabilities and
|
|
1293
|
+
* `forceWalletSign` is not set.
|
|
1294
|
+
*/
|
|
1295
|
+
delegateTo(did: string, permissions: PermissionEntry[], options?: DelegateToOptions): Promise<DelegateToResult>;
|
|
1296
|
+
/**
|
|
1297
|
+
* Materialize one manifest-declared delegation using the current session key.
|
|
1298
|
+
* Delivery is intentionally out of band; callers decide how to transmit the
|
|
1299
|
+
* returned UCAN to the delegate.
|
|
1300
|
+
*/
|
|
1301
|
+
materializeDelegation(did: string, request?: ComposedManifestRequest | undefined): Promise<DelegateToResult & {
|
|
1302
|
+
target: ResolvedDelegate;
|
|
1303
|
+
}>;
|
|
1304
|
+
/**
|
|
1305
|
+
* Materialize every delegation target declared by the composed manifest
|
|
1306
|
+
* request. This does not deliver the delegations anywhere.
|
|
1307
|
+
*/
|
|
1308
|
+
materializeDelegations(request?: ComposedManifestRequest | undefined): Promise<Array<DelegateToResult & {
|
|
1309
|
+
target: ResolvedDelegate;
|
|
1310
|
+
}>>;
|
|
1311
|
+
/**
|
|
1312
|
+
* Issue a delegation via the session-key UCAN WASM path.
|
|
1313
|
+
*
|
|
1314
|
+
* The caller has already verified every entry is derivable from
|
|
1315
|
+
* the current session; we build one multi-resource abilities map
|
|
1316
|
+
* and emit one signed UCAN covering them all.
|
|
1317
|
+
*
|
|
1318
|
+
* All entries must share the same target space (the UCAN is
|
|
1319
|
+
* scoped to a single space). If they don't, this throws — mixing
|
|
1320
|
+
* spaces in a single delegation is not supported by the underlying
|
|
1321
|
+
* Rust create_delegation call and the resulting UCAN would be
|
|
1322
|
+
* under-specified.
|
|
1323
|
+
*
|
|
1324
|
+
* @internal
|
|
1325
|
+
*/
|
|
1326
|
+
private createDelegationViaWasmPath;
|
|
1327
|
+
private createDelegationViaRuntimeGrant;
|
|
1328
|
+
private resolvePermissionSpace;
|
|
1329
|
+
private expandPermissionEntries;
|
|
1330
|
+
private shortServiceName;
|
|
1331
|
+
private permissionsToAbilities;
|
|
1332
|
+
private permissionOperations;
|
|
1333
|
+
private sessionCoversPermissionEntries;
|
|
1334
|
+
private permissionEntriesToOperations;
|
|
1335
|
+
private findRuntimeGrantsForPermissionEntries;
|
|
1336
|
+
private runtimeDelegationFromSession;
|
|
1337
|
+
private runtimeGrantFromDelegation;
|
|
1338
|
+
private delegatedResourcesForEntries;
|
|
1339
|
+
private operationsFromDelegation;
|
|
1340
|
+
private flatDelegationResources;
|
|
1341
|
+
private selectInvocationSession;
|
|
1342
|
+
private findGrantForOperations;
|
|
1343
|
+
private findGrantForOperation;
|
|
1344
|
+
private pruneExpiredRuntimePermissionGrants;
|
|
1345
|
+
private operationCovers;
|
|
1346
|
+
private actionContains;
|
|
1347
|
+
private invocationServiceName;
|
|
1348
|
+
private pathContains;
|
|
1349
|
+
/**
|
|
1350
|
+
* Issue a delegation via the legacy wallet-signed SIWE path for a single
|
|
1351
|
+
* {@link PermissionEntry}. Shares the implementation with the public
|
|
1352
|
+
* `createDelegation` method via {@link createDelegationWalletPath} so
|
|
1353
|
+
* both entry points hit exactly the same SIWE / signer / public-space
|
|
1354
|
+
* logic without mutual recursion.
|
|
1355
|
+
*
|
|
1356
|
+
* @internal
|
|
1357
|
+
*/
|
|
1358
|
+
private createDelegationLegacyWalletPath;
|
|
1359
|
+
/**
|
|
1360
|
+
* Create a delegation from this user to another user.
|
|
1361
|
+
*
|
|
1362
|
+
* The delegation grants the recipient access to a specific path and actions
|
|
1363
|
+
* within this user's space.
|
|
1364
|
+
*
|
|
1365
|
+
* @param params - Delegation parameters
|
|
1366
|
+
* @returns A portable delegation that can be sent to the recipient
|
|
1367
|
+
*/
|
|
1368
|
+
createDelegation(params: {
|
|
1369
|
+
/** Path within the space to delegate access to */
|
|
1370
|
+
path: string;
|
|
1371
|
+
/** Actions to allow (e.g., ["tinycloud.kv/get", "tinycloud.kv/put"]) */
|
|
1372
|
+
actions: string[];
|
|
1373
|
+
/** DID of the recipient (from their TinyCloudNode.did) */
|
|
1374
|
+
delegateDID: string;
|
|
1375
|
+
/** Whether to prevent the recipient from creating sub-delegations (default: false) */
|
|
1376
|
+
disableSubDelegation?: boolean;
|
|
1377
|
+
/** Expiration time in milliseconds from now (default: 1 hour) */
|
|
1378
|
+
expiryMs?: number;
|
|
1379
|
+
/** Override space ID (for creating delegations to non-primary spaces like public) */
|
|
1380
|
+
spaceIdOverride?: string;
|
|
1381
|
+
/** Include a companion delegation for the user's public space (default: true) */
|
|
1382
|
+
includePublicSpace?: boolean;
|
|
1383
|
+
}): Promise<PortableDelegation>;
|
|
1384
|
+
/**
|
|
1385
|
+
* Legacy wallet-signed SIWE delegation path. Lifted from the original
|
|
1386
|
+
* `createDelegation` body verbatim so both the legacy public method and
|
|
1387
|
+
* `delegateTo({ forceWalletSign: true })` hit the same code.
|
|
1388
|
+
*
|
|
1389
|
+
* @internal
|
|
1390
|
+
*/
|
|
1391
|
+
private createDelegationWalletPath;
|
|
1392
|
+
/**
|
|
1393
|
+
* Use a delegation received from another user.
|
|
1394
|
+
*
|
|
1395
|
+
* This creates a new session key for this user that chains from the
|
|
1396
|
+
* received delegation, allowing operations on the delegator's space.
|
|
1397
|
+
*
|
|
1398
|
+
* Works in both modes:
|
|
1399
|
+
* - **Wallet mode**: Creates a SIWE sub-delegation from PKH to session key
|
|
1400
|
+
* - **Session-only mode**: Uses the delegation directly (must target session key DID)
|
|
1401
|
+
*
|
|
1402
|
+
* @param delegation - The PortableDelegation to use (from createDelegation or transport)
|
|
1403
|
+
* @returns A DelegatedAccess instance for performing operations
|
|
1404
|
+
*/
|
|
1405
|
+
useDelegation(delegation: PortableDelegation): Promise<DelegatedAccess>;
|
|
1406
|
+
/**
|
|
1407
|
+
* Create a sub-delegation from a received delegation.
|
|
1408
|
+
*
|
|
1409
|
+
* This allows further delegating access that was received from another user,
|
|
1410
|
+
* if the original delegation allows sub-delegation.
|
|
1411
|
+
*
|
|
1412
|
+
* @param parentDelegation - The delegation received from another user
|
|
1413
|
+
* @param params - Sub-delegation parameters (must be within parent's scope)
|
|
1414
|
+
* @returns A portable delegation for the sub-delegate
|
|
1415
|
+
*/
|
|
1416
|
+
createSubDelegation(parentDelegation: PortableDelegation, params: {
|
|
1417
|
+
/** Path within the delegated path to sub-delegate */
|
|
1418
|
+
path: string;
|
|
1419
|
+
/** Actions to allow (must be subset of parent's actions) */
|
|
1420
|
+
actions: string[];
|
|
1421
|
+
/** DID of the recipient */
|
|
1422
|
+
delegateDID: string;
|
|
1423
|
+
/** Whether to prevent the recipient from creating further sub-delegations */
|
|
1424
|
+
disableSubDelegation?: boolean;
|
|
1425
|
+
/** Expiration time in milliseconds from now (must be before parent's expiry) */
|
|
1426
|
+
expiryMs?: number;
|
|
1427
|
+
}): Promise<PortableDelegation>;
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
/**
|
|
1431
|
+
* WasmKeyProvider - KeyProvider implementation using WASM session manager.
|
|
1432
|
+
*
|
|
1433
|
+
* This provider wraps the SessionManager from node-sdk-wasm to provide
|
|
1434
|
+
* cryptographic key operations required by the SharingService.
|
|
1435
|
+
*
|
|
1436
|
+
* @packageDocumentation
|
|
1437
|
+
*/
|
|
1438
|
+
|
|
1439
|
+
/**
|
|
1440
|
+
* Extended session manager with optional key listing support.
|
|
1441
|
+
* The base ISessionManager doesn't include listSessionKeys(),
|
|
1442
|
+
* but concrete implementations (e.g., TCWSessionManager) may provide it.
|
|
1443
|
+
*/
|
|
1444
|
+
interface SessionManagerWithListing extends ISessionManager {
|
|
1445
|
+
listSessionKeys?(): string[];
|
|
1446
|
+
}
|
|
1447
|
+
/**
|
|
1448
|
+
* Configuration for WasmKeyProvider.
|
|
1449
|
+
*/
|
|
1450
|
+
interface WasmKeyProviderConfig {
|
|
1451
|
+
/**
|
|
1452
|
+
* The WASM session manager instance.
|
|
1453
|
+
* Must be created before constructing the KeyProvider.
|
|
1454
|
+
*/
|
|
1455
|
+
sessionManager: SessionManagerWithListing;
|
|
1456
|
+
}
|
|
1457
|
+
/**
|
|
1458
|
+
* KeyProvider implementation that wraps the WASM session manager.
|
|
1459
|
+
*
|
|
1460
|
+
* This allows the SharingService to create new session keys for sharing links
|
|
1461
|
+
* using the same cryptographic operations as the main session management.
|
|
1462
|
+
*
|
|
1463
|
+
* @example
|
|
1464
|
+
* ```typescript
|
|
1465
|
+
* // sessionManager from wasmBindings.createSessionManager()
|
|
1466
|
+
* import { WasmKeyProvider } from "@tinycloud/node-sdk";
|
|
1467
|
+
*
|
|
1468
|
+
* const sessionManager = new SessionManager();
|
|
1469
|
+
* const keyProvider = new WasmKeyProvider({ sessionManager });
|
|
1470
|
+
*
|
|
1471
|
+
* // Create a session key for a sharing link
|
|
1472
|
+
* const keyId = await keyProvider.createSessionKey("share:abc123");
|
|
1473
|
+
* const jwk = keyProvider.getJWK(keyId);
|
|
1474
|
+
* const did = await keyProvider.getDID(keyId);
|
|
1475
|
+
* ```
|
|
1476
|
+
*/
|
|
1477
|
+
declare class WasmKeyProvider implements KeyProvider {
|
|
1478
|
+
private sessionManager;
|
|
1479
|
+
/**
|
|
1480
|
+
* Create a new WasmKeyProvider.
|
|
1481
|
+
*
|
|
1482
|
+
* @param config - Configuration with the WASM session manager
|
|
1483
|
+
*/
|
|
1484
|
+
constructor(config: WasmKeyProviderConfig);
|
|
1485
|
+
/**
|
|
1486
|
+
* Generate a new session key with the given name.
|
|
1487
|
+
*
|
|
1488
|
+
* This creates a new Ed25519 key pair in the WASM session manager.
|
|
1489
|
+
* The key can then be used for signing delegations in sharing links.
|
|
1490
|
+
*
|
|
1491
|
+
* @param name - A unique name/ID for the key (e.g., "share:timestamp:random")
|
|
1492
|
+
* @returns The key ID (same as the name provided)
|
|
1493
|
+
*/
|
|
1494
|
+
createSessionKey(name: string): Promise<string>;
|
|
1495
|
+
/**
|
|
1496
|
+
* Get the JWK (JSON Web Key) for a key.
|
|
1497
|
+
*
|
|
1498
|
+
* Returns the full JWK including the private key (d parameter),
|
|
1499
|
+
* which is required for signing and for embedding in sharing links.
|
|
1500
|
+
*
|
|
1501
|
+
* @param keyId - The key ID to retrieve
|
|
1502
|
+
* @returns The JWK object with public and private key components
|
|
1503
|
+
* @throws Error if the key is not found
|
|
1504
|
+
*/
|
|
1505
|
+
getJWK(keyId: string): JWK;
|
|
1506
|
+
/**
|
|
1507
|
+
* Get the DID (Decentralized Identifier) for a key.
|
|
1508
|
+
*
|
|
1509
|
+
* Returns the did:key format DID derived from the key's public key.
|
|
1510
|
+
* This DID can be used as the delegatee in delegations.
|
|
1511
|
+
*
|
|
1512
|
+
* @param keyId - The key ID to retrieve
|
|
1513
|
+
* @returns The DID in did:key format (e.g., "did:key:z6Mk...")
|
|
1514
|
+
*/
|
|
1515
|
+
getDID(keyId: string): Promise<string>;
|
|
1516
|
+
/**
|
|
1517
|
+
* List all session keys currently held by the provider.
|
|
1518
|
+
*
|
|
1519
|
+
* @returns Array of key IDs
|
|
1520
|
+
*/
|
|
1521
|
+
listKeys(): string[];
|
|
1522
|
+
/**
|
|
1523
|
+
* Check if a key exists in the provider.
|
|
1524
|
+
*
|
|
1525
|
+
* @param keyId - The key ID to check
|
|
1526
|
+
* @returns True if the key exists
|
|
1527
|
+
*/
|
|
1528
|
+
hasKey(keyId: string): boolean;
|
|
1529
|
+
}
|
|
1530
|
+
/**
|
|
1531
|
+
* Create a new WasmKeyProvider instance.
|
|
1532
|
+
*
|
|
1533
|
+
* @param sessionManager - The WASM session manager
|
|
1534
|
+
* @returns A new WasmKeyProvider instance
|
|
1535
|
+
*/
|
|
1536
|
+
declare function createWasmKeyProvider(sessionManager: SessionManagerWithListing): WasmKeyProvider;
|
|
1537
|
+
|
|
1538
|
+
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 };
|