@tinycloudlabs/sdk-core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/LICENSE.md +320 -0
  2. package/dist/TinyCloud.d.ts +206 -0
  3. package/dist/TinyCloud.d.ts.map +1 -0
  4. package/dist/TinyCloud.js +244 -0
  5. package/dist/TinyCloud.js.map +1 -0
  6. package/dist/TinyCloud.schema.d.ts +173 -0
  7. package/dist/TinyCloud.schema.d.ts.map +1 -0
  8. package/dist/TinyCloud.schema.js +136 -0
  9. package/dist/TinyCloud.schema.js.map +1 -0
  10. package/dist/TinyCloud.schema.test.d.ts +5 -0
  11. package/dist/TinyCloud.schema.test.d.ts.map +1 -0
  12. package/dist/TinyCloud.schema.test.js +286 -0
  13. package/dist/TinyCloud.schema.test.js.map +1 -0
  14. package/dist/authorization/CapabilityKeyRegistry.d.ts +317 -0
  15. package/dist/authorization/CapabilityKeyRegistry.d.ts.map +1 -0
  16. package/dist/authorization/CapabilityKeyRegistry.js +509 -0
  17. package/dist/authorization/CapabilityKeyRegistry.js.map +1 -0
  18. package/dist/authorization/authorization.schema.d.ts +233 -0
  19. package/dist/authorization/authorization.schema.d.ts.map +1 -0
  20. package/dist/authorization/authorization.schema.js +220 -0
  21. package/dist/authorization/authorization.schema.js.map +1 -0
  22. package/dist/authorization/authorization.schema.test.d.ts +5 -0
  23. package/dist/authorization/authorization.schema.test.d.ts.map +1 -0
  24. package/dist/authorization/authorization.schema.test.js +618 -0
  25. package/dist/authorization/authorization.schema.test.js.map +1 -0
  26. package/dist/authorization/index.d.ts +38 -0
  27. package/dist/authorization/index.d.ts.map +1 -0
  28. package/dist/authorization/index.js +52 -0
  29. package/dist/authorization/index.js.map +1 -0
  30. package/dist/authorization/spaceCreation.d.ts +96 -0
  31. package/dist/authorization/spaceCreation.d.ts.map +1 -0
  32. package/dist/authorization/spaceCreation.js +35 -0
  33. package/dist/authorization/spaceCreation.js.map +1 -0
  34. package/dist/authorization/spaceCreation.schema.d.ts +67 -0
  35. package/dist/authorization/spaceCreation.schema.d.ts.map +1 -0
  36. package/dist/authorization/spaceCreation.schema.js +95 -0
  37. package/dist/authorization/spaceCreation.schema.js.map +1 -0
  38. package/dist/authorization/spaceCreation.schema.test.d.ts +5 -0
  39. package/dist/authorization/spaceCreation.schema.test.d.ts.map +1 -0
  40. package/dist/authorization/spaceCreation.schema.test.js +168 -0
  41. package/dist/authorization/spaceCreation.schema.test.js.map +1 -0
  42. package/dist/authorization/strategies.d.ts +134 -0
  43. package/dist/authorization/strategies.d.ts.map +1 -0
  44. package/dist/authorization/strategies.js +15 -0
  45. package/dist/authorization/strategies.js.map +1 -0
  46. package/dist/authorization/strategies.schema.d.ts +185 -0
  47. package/dist/authorization/strategies.schema.d.ts.map +1 -0
  48. package/dist/authorization/strategies.schema.js +147 -0
  49. package/dist/authorization/strategies.schema.js.map +1 -0
  50. package/dist/authorization/strategies.schema.test.d.ts +5 -0
  51. package/dist/authorization/strategies.schema.test.d.ts.map +1 -0
  52. package/dist/authorization/strategies.schema.test.js +253 -0
  53. package/dist/authorization/strategies.schema.test.js.map +1 -0
  54. package/dist/delegations/DelegationManager.d.ts +164 -0
  55. package/dist/delegations/DelegationManager.d.ts.map +1 -0
  56. package/dist/delegations/DelegationManager.js +428 -0
  57. package/dist/delegations/DelegationManager.js.map +1 -0
  58. package/dist/delegations/SharingService.d.ts +279 -0
  59. package/dist/delegations/SharingService.d.ts.map +1 -0
  60. package/dist/delegations/SharingService.js +558 -0
  61. package/dist/delegations/SharingService.js.map +1 -0
  62. package/dist/delegations/SharingService.schema.d.ts +401 -0
  63. package/dist/delegations/SharingService.schema.d.ts.map +1 -0
  64. package/dist/delegations/SharingService.schema.js +211 -0
  65. package/dist/delegations/SharingService.schema.js.map +1 -0
  66. package/dist/delegations/index.d.ts +38 -0
  67. package/dist/delegations/index.d.ts.map +1 -0
  68. package/dist/delegations/index.js +42 -0
  69. package/dist/delegations/index.js.map +1 -0
  70. package/dist/delegations/types.d.ts +13 -0
  71. package/dist/delegations/types.d.ts.map +1 -0
  72. package/dist/delegations/types.js +42 -0
  73. package/dist/delegations/types.js.map +1 -0
  74. package/dist/delegations/types.schema.d.ts +1641 -0
  75. package/dist/delegations/types.schema.d.ts.map +1 -0
  76. package/dist/delegations/types.schema.js +535 -0
  77. package/dist/delegations/types.schema.js.map +1 -0
  78. package/dist/delegations/types.schema.test.d.ts +5 -0
  79. package/dist/delegations/types.schema.test.d.ts.map +1 -0
  80. package/dist/delegations/types.schema.test.js +627 -0
  81. package/dist/delegations/types.schema.test.js.map +1 -0
  82. package/dist/index.d.ts +22 -0
  83. package/dist/index.d.ts.map +1 -0
  84. package/dist/index.js +52 -0
  85. package/dist/index.js.map +1 -0
  86. package/dist/json-schema.d.ts +327 -0
  87. package/dist/json-schema.d.ts.map +1 -0
  88. package/dist/json-schema.js +703 -0
  89. package/dist/json-schema.js.map +1 -0
  90. package/dist/json-schema.test.d.ts +7 -0
  91. package/dist/json-schema.test.d.ts.map +1 -0
  92. package/dist/json-schema.test.js +365 -0
  93. package/dist/json-schema.test.js.map +1 -0
  94. package/dist/signer.d.ts +28 -0
  95. package/dist/signer.d.ts.map +1 -0
  96. package/dist/signer.js +2 -0
  97. package/dist/signer.js.map +1 -0
  98. package/dist/space.d.ts +53 -0
  99. package/dist/space.d.ts.map +1 -0
  100. package/dist/space.js +67 -0
  101. package/dist/space.js.map +1 -0
  102. package/dist/space.schema.d.ts +65 -0
  103. package/dist/space.schema.d.ts.map +1 -0
  104. package/dist/space.schema.js +65 -0
  105. package/dist/space.schema.js.map +1 -0
  106. package/dist/space.schema.test.d.ts +5 -0
  107. package/dist/space.schema.test.d.ts.map +1 -0
  108. package/dist/space.schema.test.js +148 -0
  109. package/dist/space.schema.test.js.map +1 -0
  110. package/dist/spaces/Space.d.ts +175 -0
  111. package/dist/spaces/Space.d.ts.map +1 -0
  112. package/dist/spaces/Space.js +84 -0
  113. package/dist/spaces/Space.js.map +1 -0
  114. package/dist/spaces/SpaceService.d.ts +271 -0
  115. package/dist/spaces/SpaceService.d.ts.map +1 -0
  116. package/dist/spaces/SpaceService.js +715 -0
  117. package/dist/spaces/SpaceService.js.map +1 -0
  118. package/dist/spaces/index.d.ts +11 -0
  119. package/dist/spaces/index.d.ts.map +1 -0
  120. package/dist/spaces/index.js +20 -0
  121. package/dist/spaces/index.js.map +1 -0
  122. package/dist/spaces/spaces.schema.d.ts +421 -0
  123. package/dist/spaces/spaces.schema.d.ts.map +1 -0
  124. package/dist/spaces/spaces.schema.js +342 -0
  125. package/dist/spaces/spaces.schema.js.map +1 -0
  126. package/dist/spaces/spaces.schema.test.d.ts +5 -0
  127. package/dist/spaces/spaces.schema.test.d.ts.map +1 -0
  128. package/dist/spaces/spaces.schema.test.js +471 -0
  129. package/dist/spaces/spaces.schema.test.js.map +1 -0
  130. package/dist/storage.d.ts +47 -0
  131. package/dist/storage.d.ts.map +1 -0
  132. package/dist/storage.js +14 -0
  133. package/dist/storage.js.map +1 -0
  134. package/dist/storage.schema.d.ts +277 -0
  135. package/dist/storage.schema.d.ts.map +1 -0
  136. package/dist/storage.schema.js +185 -0
  137. package/dist/storage.schema.js.map +1 -0
  138. package/dist/storage.schema.test.d.ts +5 -0
  139. package/dist/storage.schema.test.d.ts.map +1 -0
  140. package/dist/storage.schema.test.js +346 -0
  141. package/dist/storage.schema.test.js.map +1 -0
  142. package/dist/userAuthorization.d.ts +99 -0
  143. package/dist/userAuthorization.d.ts.map +1 -0
  144. package/dist/userAuthorization.js +3 -0
  145. package/dist/userAuthorization.js.map +1 -0
  146. package/dist/userAuthorization.schema.d.ts +259 -0
  147. package/dist/userAuthorization.schema.d.ts.map +1 -0
  148. package/dist/userAuthorization.schema.js +175 -0
  149. package/dist/userAuthorization.schema.js.map +1 -0
  150. package/dist/userAuthorization.schema.test.d.ts +5 -0
  151. package/dist/userAuthorization.schema.test.d.ts.map +1 -0
  152. package/dist/userAuthorization.schema.test.js +356 -0
  153. package/dist/userAuthorization.schema.test.js.map +1 -0
  154. package/dist/version.d.ts +30 -0
  155. package/dist/version.d.ts.map +1 -0
  156. package/dist/version.js +54 -0
  157. package/dist/version.js.map +1 -0
  158. package/dist/wasm-validation.d.ts +287 -0
  159. package/dist/wasm-validation.d.ts.map +1 -0
  160. package/dist/wasm-validation.js +219 -0
  161. package/dist/wasm-validation.js.map +1 -0
  162. package/dist/wasm-validation.test.d.ts +5 -0
  163. package/dist/wasm-validation.test.d.ts.map +1 -0
  164. package/dist/wasm-validation.test.js +233 -0
  165. package/dist/wasm-validation.test.js.map +1 -0
  166. package/package.json +40 -0
@@ -0,0 +1,558 @@
1
+ /**
2
+ * SharingService - v2 sharing link service with embedded private keys.
3
+ *
4
+ * This service implements the v2 sharing specification, which embeds private keys
5
+ * directly in sharing links. This allows recipients to exercise delegations
6
+ * without requiring prior session setup.
7
+ *
8
+ * Key differences from v1 SharingLinks:
9
+ * - Private keys are embedded in the link (not just tokens)
10
+ * - Recipients can optionally sub-delegate to their own session key
11
+ * - Pre-configured KV service returned for immediate use
12
+ *
13
+ * @packageDocumentation
14
+ */
15
+ import { DelegationErrorCodes } from "./types";
16
+ import { validateEncodedShareData } from "./SharingService.schema.js";
17
+ // =============================================================================
18
+ // Constants
19
+ // =============================================================================
20
+ /**
21
+ * Default actions for read-only sharing links.
22
+ */
23
+ const DEFAULT_READ_ACTIONS = ["tinycloud.kv/get", "tinycloud.kv/metadata"];
24
+ /**
25
+ * Default expiry for sharing links (24 hours).
26
+ */
27
+ const DEFAULT_EXPIRY_MS = 24 * 60 * 60 * 1000;
28
+ /**
29
+ * Prefix for the base64 schema.
30
+ */
31
+ const BASE64_PREFIX = "tc1:";
32
+ // =============================================================================
33
+ // Helper Functions
34
+ // =============================================================================
35
+ /**
36
+ * Creates a DelegationError with the given parameters.
37
+ */
38
+ function createError(code, message, cause, meta) {
39
+ return {
40
+ code,
41
+ message,
42
+ service: "delegation",
43
+ cause,
44
+ meta,
45
+ };
46
+ }
47
+ /**
48
+ * Base64 encode for URLs (URL-safe base64).
49
+ */
50
+ function base64UrlEncode(data) {
51
+ // Use btoa for browser, Buffer for Node.js
52
+ let base64;
53
+ if (typeof btoa !== "undefined") {
54
+ base64 = btoa(unescape(encodeURIComponent(data)));
55
+ }
56
+ else if (typeof Buffer !== "undefined") {
57
+ base64 = Buffer.from(data, "utf-8").toString("base64");
58
+ }
59
+ else {
60
+ throw new Error("No base64 encoding available");
61
+ }
62
+ // Make URL-safe
63
+ return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
64
+ }
65
+ /**
66
+ * Base64 decode for URLs (URL-safe base64).
67
+ */
68
+ function base64UrlDecode(encoded) {
69
+ // Restore standard base64
70
+ let base64 = encoded.replace(/-/g, "+").replace(/_/g, "/");
71
+ // Add padding if needed
72
+ while (base64.length % 4) {
73
+ base64 += "=";
74
+ }
75
+ // Decode
76
+ if (typeof atob !== "undefined") {
77
+ return decodeURIComponent(escape(atob(base64)));
78
+ }
79
+ else if (typeof Buffer !== "undefined") {
80
+ return Buffer.from(base64, "base64").toString("utf-8");
81
+ }
82
+ else {
83
+ throw new Error("No base64 decoding available");
84
+ }
85
+ }
86
+ // =============================================================================
87
+ // Implementation
88
+ // =============================================================================
89
+ /**
90
+ * SharingService - v2 sharing link service with embedded private keys.
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * import { SharingService } from "@tinycloudlabs/sdk-core/delegations";
95
+ *
96
+ * const sharing = new SharingService({
97
+ * hosts: ["https://node.tinycloud.xyz"],
98
+ * session,
99
+ * invoke,
100
+ * keyProvider,
101
+ * registry,
102
+ * delegationManager,
103
+ * createKVService,
104
+ * baseUrl: "https://share.myapp.com"
105
+ * });
106
+ *
107
+ * // Generate a sharing link
108
+ * const result = await sharing.generate({
109
+ * path: "/kv/documents/report.pdf",
110
+ * actions: ["tinycloud.kv/get"],
111
+ * expiry: new Date("2024-12-31")
112
+ * });
113
+ *
114
+ * if (result.ok) {
115
+ * console.log("Share this URL:", result.data.url);
116
+ * }
117
+ *
118
+ * // Receive a sharing link
119
+ * const receiveResult = await sharing.receive(shareUrl);
120
+ * if (receiveResult.ok) {
121
+ * // Use the pre-configured KV service
122
+ * const data = await receiveResult.data.kv.get("report.pdf");
123
+ * }
124
+ * ```
125
+ */
126
+ export class SharingService {
127
+ /**
128
+ * Creates a new SharingService instance.
129
+ */
130
+ constructor(config) {
131
+ this.hosts = config.hosts;
132
+ this.session = config.session;
133
+ this.invoke = config.invoke;
134
+ this.fetchFn = config.fetch ?? globalThis.fetch.bind(globalThis);
135
+ this.keyProvider = config.keyProvider;
136
+ this.registry = config.registry;
137
+ this.delegationManager = config.delegationManager;
138
+ this.createKVService = config.createKVService;
139
+ this.baseUrl = (config.baseUrl ?? "").replace(/\/$/, ""); // Remove trailing slash
140
+ this.createDelegationFn = config.createDelegation;
141
+ this.createDelegationWasmFn = config.createDelegationWasm;
142
+ this.pathPrefix = config.pathPrefix ?? "";
143
+ this.sessionExpiry = config.sessionExpiry;
144
+ }
145
+ /**
146
+ * Gets the primary host URL.
147
+ */
148
+ get host() {
149
+ return this.hosts[0];
150
+ }
151
+ /**
152
+ * Updates the session (e.g., after re-authentication).
153
+ */
154
+ updateSession(session) {
155
+ this.session = session;
156
+ }
157
+ /**
158
+ * Updates the service configuration.
159
+ * Used to add full capabilities (session, delegationManager, createDelegation, createDelegationWasm) after signIn.
160
+ */
161
+ updateConfig(config) {
162
+ if (config.session !== undefined) {
163
+ this.session = config.session;
164
+ }
165
+ if (config.delegationManager !== undefined) {
166
+ this.delegationManager = config.delegationManager;
167
+ }
168
+ if (config.createDelegation !== undefined) {
169
+ this.createDelegationFn = config.createDelegation;
170
+ }
171
+ if (config.createDelegationWasm !== undefined) {
172
+ this.createDelegationWasmFn = config.createDelegationWasm;
173
+ }
174
+ if (config.sessionExpiry !== undefined) {
175
+ this.sessionExpiry = config.sessionExpiry;
176
+ }
177
+ }
178
+ /**
179
+ * Generate a sharing link with an embedded private key.
180
+ *
181
+ * Flow:
182
+ * 1. Spawn new session key (unique per share)
183
+ * 2. Create delegation from current session to spawned key
184
+ * 3. Package: { key (with private!), delegation, path, host }
185
+ * 4. Encode based on schema (base64 for now)
186
+ * 5. Return link string
187
+ */
188
+ async generate(params) {
189
+ // Require session for generating (not for receiving)
190
+ if (!this.session) {
191
+ return {
192
+ ok: false,
193
+ error: createError(DelegationErrorCodes.NOT_INITIALIZED, "Session required for generating sharing links. Call signIn() first."),
194
+ };
195
+ }
196
+ // Require delegation capability
197
+ if (!this.createDelegationWasmFn && !this.createDelegationFn && !this.delegationManager) {
198
+ return {
199
+ ok: false,
200
+ error: createError(DelegationErrorCodes.NOT_INITIALIZED, "DelegationManager, createDelegation, or createDelegationWasm function required for generating sharing links."),
201
+ };
202
+ }
203
+ // Validate path
204
+ if (!params.path) {
205
+ return {
206
+ ok: false,
207
+ error: createError(DelegationErrorCodes.INVALID_INPUT, "path is required"),
208
+ };
209
+ }
210
+ const actions = params.actions ?? DEFAULT_READ_ACTIONS;
211
+ const requestedExpiry = params.expiry ?? new Date(Date.now() + DEFAULT_EXPIRY_MS);
212
+ // Clamp expiry to session expiry if set
213
+ const expiry = this.sessionExpiry && requestedExpiry > this.sessionExpiry
214
+ ? this.sessionExpiry
215
+ : requestedExpiry;
216
+ const schema = params.schema ?? "base64";
217
+ // Build full path with prefix (matches how KVService stores data)
218
+ // If pathPrefix is "demo-app" and path is "hello", fullPath is "demo-app/hello"
219
+ const fullPath = this.pathPrefix
220
+ ? `${this.pathPrefix}/${params.path}`.replace(/\/+/g, "/") // Normalize slashes
221
+ : params.path;
222
+ // Only base64 schema is implemented in v1
223
+ if (schema !== "base64") {
224
+ return {
225
+ ok: false,
226
+ error: createError(DelegationErrorCodes.INVALID_INPUT, `Schema '${schema}' not implemented. Only 'base64' is supported.`),
227
+ };
228
+ }
229
+ // Step 1: Spawn a new session key unique to this share
230
+ let keyId;
231
+ let keyDid;
232
+ let keyJwk;
233
+ try {
234
+ const shareKeyName = `share:${Date.now()}:${Math.random().toString(36).substring(2, 10)}`;
235
+ keyId = await this.keyProvider.createSessionKey(shareKeyName);
236
+ keyDid = await this.keyProvider.getDID(keyId);
237
+ keyJwk = this.keyProvider.getJWK(keyId);
238
+ // Ensure the private key is included
239
+ if (!keyJwk.d) {
240
+ return {
241
+ ok: false,
242
+ error: createError(DelegationErrorCodes.CREATION_FAILED, "KeyProvider did not return private key (d parameter) in JWK"),
243
+ };
244
+ }
245
+ }
246
+ catch (err) {
247
+ return {
248
+ ok: false,
249
+ error: createError(DelegationErrorCodes.CREATION_FAILED, `Failed to generate session key for share: ${err instanceof Error ? err.message : String(err)}`, err instanceof Error ? err : undefined),
250
+ };
251
+ }
252
+ // Step 2: Create delegation from current session to spawned key
253
+ // Prefer client-side WASM creation, fall back to server-side
254
+ let delegation;
255
+ if (this.createDelegationWasmFn) {
256
+ // Client-side delegation creation via WASM
257
+ try {
258
+ // Strip fragment from DID URL to get plain DID for UCAN audience
259
+ // getDID() returns "did:key:z6Mk...#z6Mk..." but audience needs "did:key:z6Mk..."
260
+ const plainDID = keyDid.split('#')[0];
261
+ const wasmResult = this.createDelegationWasmFn({
262
+ session: this.session,
263
+ delegateDID: plainDID,
264
+ spaceId: this.session.spaceId,
265
+ path: fullPath,
266
+ actions,
267
+ expirationSecs: Math.floor(expiry.getTime() / 1000),
268
+ });
269
+ // Register the delegation with the server
270
+ // The server needs to know about this delegation for proof chain validation
271
+ const registerRes = await this.fetchFn(`${this.host}/delegate`, {
272
+ method: "POST",
273
+ headers: {
274
+ Authorization: wasmResult.delegation, // The UCAN JWT
275
+ },
276
+ });
277
+ if (!registerRes.ok) {
278
+ const errorText = await registerRes.text();
279
+ return {
280
+ ok: false,
281
+ error: createError(DelegationErrorCodes.CREATION_FAILED, `Failed to register delegation with server: ${registerRes.status} ${errorText}`),
282
+ };
283
+ }
284
+ delegation = {
285
+ cid: wasmResult.cid,
286
+ delegateDID: wasmResult.delegateDID,
287
+ spaceId: this.session.spaceId,
288
+ path: wasmResult.path,
289
+ actions: wasmResult.actions,
290
+ expiry: wasmResult.expiry,
291
+ isRevoked: false,
292
+ authHeader: wasmResult.delegation, // The UCAN JWT (no Bearer prefix - SDK adds it internally)
293
+ allowSubDelegation: true,
294
+ createdAt: new Date(),
295
+ };
296
+ }
297
+ catch (err) {
298
+ return {
299
+ ok: false,
300
+ error: createError(DelegationErrorCodes.CREATION_FAILED, `Failed to create delegation via WASM: ${err instanceof Error ? err.message : String(err)}`, err instanceof Error ? err : undefined),
301
+ };
302
+ }
303
+ }
304
+ else {
305
+ // Server-side delegation creation (fallback)
306
+ const delegationParams = {
307
+ delegateDID: keyDid,
308
+ path: fullPath,
309
+ actions,
310
+ expiry,
311
+ statement: params.description ?? `Share access for ${params.path}`,
312
+ disableSubDelegation: false, // Allow sub-delegation for auto-subdelegate flow
313
+ };
314
+ const delegationResult = this.createDelegationFn
315
+ ? await this.createDelegationFn(delegationParams)
316
+ // delegationManager is guaranteed to exist by the guard check above
317
+ : await this.delegationManager.create(delegationParams);
318
+ if (!delegationResult.ok) {
319
+ return {
320
+ ok: false,
321
+ error: createError(DelegationErrorCodes.CREATION_FAILED, `Failed to create delegation for share: ${delegationResult.error.message}`, delegationResult.error.cause, delegationResult.error.meta),
322
+ };
323
+ }
324
+ delegation = delegationResult.data;
325
+ }
326
+ // Step 3: Package the share data
327
+ const shareData = {
328
+ key: keyJwk,
329
+ keyDid,
330
+ delegation,
331
+ path: fullPath,
332
+ host: this.host,
333
+ spaceId: this.session.spaceId,
334
+ version: 1,
335
+ };
336
+ // Step 4: Encode the link
337
+ const encodedData = this.encodeLink(shareData, schema);
338
+ // Step 5: Build the full URL
339
+ const baseUrl = params.baseUrl ?? this.baseUrl;
340
+ const url = baseUrl ? `${baseUrl}/share/${encodedData}` : encodedData;
341
+ const shareLink = {
342
+ token: encodedData,
343
+ url,
344
+ delegation,
345
+ schema,
346
+ expiresAt: expiry,
347
+ description: params.description,
348
+ };
349
+ return { ok: true, data: shareLink };
350
+ }
351
+ /**
352
+ * Receive and activate a sharing link.
353
+ *
354
+ * Flow:
355
+ * 1. Decode link -> extract { key, delegation, path, host }
356
+ * 2. Ingest key into CapabilityKeyRegistry
357
+ * 3. If autoSubdelegate (default true) + useSessionKey:
358
+ * - Create sub-delegation from ingested key -> current session
359
+ * - Register sub-delegation capabilities
360
+ * 4. Return ShareAccess with pre-configured KV service
361
+ */
362
+ async receive(link, options = {}) {
363
+ const { autoSubdelegate = true, useSessionKey = true, ingestOptions, } = options;
364
+ // Step 1: Decode and validate the link
365
+ const decodeResult = this.decodeLinkWithValidation(link);
366
+ if (!decodeResult.ok) {
367
+ return decodeResult;
368
+ }
369
+ const shareData = decodeResult.data;
370
+ // Schema validation ensures key.d and delegation exist, but we need
371
+ // to check business rules (expiry, revocation) separately
372
+ // Check delegation expiry
373
+ const delegationExpiry = new Date(shareData.delegation.expiry);
374
+ if (delegationExpiry < new Date()) {
375
+ return {
376
+ ok: false,
377
+ error: createError(DelegationErrorCodes.AUTH_EXPIRED, "Sharing link has expired"),
378
+ };
379
+ }
380
+ // Check delegation revocation
381
+ if (shareData.delegation.isRevoked) {
382
+ return {
383
+ ok: false,
384
+ error: createError(DelegationErrorCodes.REVOKED, "Sharing link has been revoked"),
385
+ };
386
+ }
387
+ // Step 2: Create KeyInfo and ingest into registry
388
+ const keyInfo = {
389
+ id: `ingested:${shareData.keyDid}`,
390
+ did: shareData.keyDid,
391
+ type: "ingested",
392
+ jwk: shareData.key,
393
+ priority: 2, // Ingested keys have lowest priority
394
+ };
395
+ this.registry.ingestKey(keyInfo, shareData.delegation, ingestOptions);
396
+ // The delegation and key to use for operations
397
+ let activeDelegation = shareData.delegation;
398
+ let activeKey = keyInfo;
399
+ // Step 3: Auto-subdelegate if requested
400
+ if (autoSubdelegate && useSessionKey && this.session) {
401
+ try {
402
+ // Get current session key DID
403
+ // Note: We need to create a sub-delegation from the ingested key to the session key
404
+ // This requires the session key DID, which should be available from the session
405
+ // For now, we'll register the ingested key's capabilities directly
406
+ // The auto-subdelegation would require additional infrastructure to sign with the ingested key
407
+ // This is a simplification - full implementation would sign a new delegation with the ingested key
408
+ // TODO: Implement full auto-subdelegation when signing infrastructure is available
409
+ // For now, the ingested key can be used directly via the registry
410
+ }
411
+ catch (err) {
412
+ // Log but don't fail - can still use the ingested key directly
413
+ console.warn("Auto-subdelegation failed, using ingested key directly:", err);
414
+ }
415
+ }
416
+ // Step 4: Create pre-configured KV service for the shared path
417
+ // Construct session from share data - no need for existing session
418
+ // Use the authHeader if available, otherwise fall back to constructing from CID
419
+ const authHeader = shareData.delegation.authHeader ?? `Bearer ${shareData.delegation.cid}`;
420
+ const shareSession = {
421
+ delegationHeader: { Authorization: authHeader },
422
+ delegationCid: shareData.delegation.cid,
423
+ spaceId: shareData.spaceId,
424
+ verificationMethod: shareData.keyDid,
425
+ jwk: shareData.key,
426
+ };
427
+ const kvService = this.createKVService({
428
+ hosts: [shareData.host],
429
+ session: shareSession,
430
+ invoke: this.invoke,
431
+ fetch: this.fetchFn,
432
+ pathPrefix: shareData.path,
433
+ });
434
+ const shareAccess = {
435
+ delegation: activeDelegation,
436
+ key: activeKey,
437
+ kv: kvService,
438
+ spaceId: shareData.spaceId,
439
+ path: shareData.path,
440
+ };
441
+ return { ok: true, data: shareAccess };
442
+ }
443
+ /**
444
+ * Encode sharing data into a link string.
445
+ *
446
+ * @param data - The share data to encode
447
+ * @param schema - The encoding schema (default: "base64")
448
+ * @returns Encoded link string
449
+ */
450
+ encodeLink(data, schema = "base64") {
451
+ if (schema !== "base64") {
452
+ throw new Error(`Schema '${schema}' not implemented. Only 'base64' is supported.`);
453
+ }
454
+ const jsonString = JSON.stringify(data);
455
+ const encoded = base64UrlEncode(jsonString);
456
+ return `${BASE64_PREFIX}${encoded}`;
457
+ }
458
+ /**
459
+ * Decode a link string into sharing data.
460
+ *
461
+ * @param link - The encoded link string (may include URL prefix)
462
+ * @returns Decoded share data
463
+ * @throws Error if link format is invalid or data fails validation
464
+ */
465
+ decodeLink(link) {
466
+ const result = this.decodeLinkWithValidation(link);
467
+ if (!result.ok) {
468
+ throw new Error(result.error.message);
469
+ }
470
+ return result.data;
471
+ }
472
+ /**
473
+ * Decode and validate a link string into sharing data.
474
+ *
475
+ * Internal method that returns a Result instead of throwing.
476
+ * Used by receive() for proper error handling.
477
+ *
478
+ * @param link - The encoded link string (may include URL prefix)
479
+ * @returns Result with decoded share data or validation error
480
+ */
481
+ decodeLinkWithValidation(link) {
482
+ // Extract the encoded data from the link
483
+ let encoded = link;
484
+ // Handle full URL format: https://share.example.com/share/tc1:...
485
+ if (link.includes("/share/")) {
486
+ const parts = link.split("/share/");
487
+ encoded = parts[parts.length - 1];
488
+ }
489
+ // Handle query parameter format: ?share=tc1:...
490
+ if (link.includes("?share=")) {
491
+ try {
492
+ const url = new URL(link);
493
+ encoded = url.searchParams.get("share") ?? encoded;
494
+ }
495
+ catch {
496
+ return {
497
+ ok: false,
498
+ error: createError(DelegationErrorCodes.INVALID_TOKEN, "Invalid URL format in sharing link"),
499
+ };
500
+ }
501
+ }
502
+ // Remove the schema prefix
503
+ if (!encoded.startsWith(BASE64_PREFIX)) {
504
+ return {
505
+ ok: false,
506
+ error: createError(DelegationErrorCodes.INVALID_TOKEN, `Invalid sharing link format. Expected prefix '${BASE64_PREFIX}'`),
507
+ };
508
+ }
509
+ const base64Data = encoded.slice(BASE64_PREFIX.length);
510
+ let jsonString;
511
+ try {
512
+ jsonString = base64UrlDecode(base64Data);
513
+ }
514
+ catch (err) {
515
+ return {
516
+ ok: false,
517
+ error: createError(DelegationErrorCodes.INVALID_TOKEN, `Failed to decode base64 data: ${err instanceof Error ? err.message : String(err)}`, err instanceof Error ? err : undefined),
518
+ };
519
+ }
520
+ let parsed;
521
+ try {
522
+ parsed = JSON.parse(jsonString);
523
+ }
524
+ catch (err) {
525
+ return {
526
+ ok: false,
527
+ error: createError(DelegationErrorCodes.INVALID_TOKEN, `Failed to parse share data JSON: ${err instanceof Error ? err.message : String(err)}`, err instanceof Error ? err : undefined),
528
+ };
529
+ }
530
+ // Convert delegation expiry to Date before validation if it's a string
531
+ // This is needed because JSON.parse doesn't restore Date objects
532
+ if (parsed &&
533
+ typeof parsed === "object" &&
534
+ "delegation" in parsed &&
535
+ parsed.delegation &&
536
+ typeof parsed.delegation === "object" &&
537
+ "expiry" in parsed.delegation &&
538
+ typeof parsed.delegation.expiry === "string") {
539
+ parsed.delegation.expiry = new Date(parsed.delegation.expiry);
540
+ }
541
+ // Validate against schema
542
+ const validationResult = validateEncodedShareData(parsed);
543
+ if (!validationResult.ok) {
544
+ return {
545
+ ok: false,
546
+ error: createError(DelegationErrorCodes.INVALID_TOKEN, validationResult.error.message, undefined, validationResult.error.meta),
547
+ };
548
+ }
549
+ return { ok: true, data: validationResult.data };
550
+ }
551
+ }
552
+ /**
553
+ * Create a new SharingService instance.
554
+ */
555
+ export function createSharingService(config) {
556
+ return new SharingService(config);
557
+ }
558
+ //# sourceMappingURL=SharingService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SharingService.js","sourceRoot":"","sources":["../../src/delegations/SharingService.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAwBH,OAAO,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAG/C,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AAEtE,gFAAgF;AAChF,YAAY;AACZ,gFAAgF;AAEhF;;GAEG;AACH,MAAM,oBAAoB,GAAG,CAAC,kBAAkB,EAAE,uBAAuB,CAAC,CAAC;AAE3E;;GAEG;AACH,MAAM,iBAAiB,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAE9C;;GAEG;AACH,MAAM,aAAa,GAAG,MAAM,CAAC;AAE7B,gFAAgF;AAChF,mBAAmB;AACnB,gFAAgF;AAEhF;;GAEG;AACH,SAAS,WAAW,CAClB,IAAY,EACZ,OAAe,EACf,KAAa,EACb,IAA8B;IAE9B,OAAO;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