@enbox/auth 0.6.0 → 0.6.2

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 (45) hide show
  1. package/dist/esm/auth-manager.js +147 -5
  2. package/dist/esm/auth-manager.js.map +1 -1
  3. package/dist/esm/connect/lifecycle.js +145 -2
  4. package/dist/esm/connect/lifecycle.js.map +1 -1
  5. package/dist/esm/connect/local.js +19 -5
  6. package/dist/esm/connect/local.js.map +1 -1
  7. package/dist/esm/connect/restore.js +22 -8
  8. package/dist/esm/connect/restore.js.map +1 -1
  9. package/dist/esm/connect/wallet.js +24 -137
  10. package/dist/esm/connect/wallet.js.map +1 -1
  11. package/dist/esm/index.js +9 -15
  12. package/dist/esm/index.js.map +1 -1
  13. package/dist/esm/permissions.js +41 -0
  14. package/dist/esm/permissions.js.map +1 -0
  15. package/dist/esm/types.js +2 -0
  16. package/dist/esm/types.js.map +1 -1
  17. package/dist/esm/wallet-connect-client.js +4 -2
  18. package/dist/esm/wallet-connect-client.js.map +1 -1
  19. package/dist/types/auth-manager.d.ts +70 -6
  20. package/dist/types/auth-manager.d.ts.map +1 -1
  21. package/dist/types/connect/lifecycle.d.ts +49 -2
  22. package/dist/types/connect/lifecycle.d.ts.map +1 -1
  23. package/dist/types/connect/local.d.ts +6 -1
  24. package/dist/types/connect/local.d.ts.map +1 -1
  25. package/dist/types/connect/restore.d.ts.map +1 -1
  26. package/dist/types/connect/wallet.d.ts +1 -15
  27. package/dist/types/connect/wallet.d.ts.map +1 -1
  28. package/dist/types/index.d.ts +10 -16
  29. package/dist/types/index.d.ts.map +1 -1
  30. package/dist/types/permissions.d.ts +18 -0
  31. package/dist/types/permissions.d.ts.map +1 -0
  32. package/dist/types/types.d.ts +148 -1
  33. package/dist/types/types.d.ts.map +1 -1
  34. package/dist/types/wallet-connect-client.d.ts +5 -6
  35. package/dist/types/wallet-connect-client.d.ts.map +1 -1
  36. package/package.json +3 -3
  37. package/src/auth-manager.ts +167 -6
  38. package/src/connect/lifecycle.ts +170 -4
  39. package/src/connect/local.ts +20 -5
  40. package/src/connect/restore.ts +25 -9
  41. package/src/connect/wallet.ts +26 -146
  42. package/src/index.ts +16 -16
  43. package/src/permissions.ts +48 -0
  44. package/src/types.ts +164 -1
  45. package/src/wallet-connect-client.ts +5 -6
package/src/types.ts CHANGED
@@ -3,7 +3,8 @@
3
3
  * Public types for the authentication and identity management SDK.
4
4
  */
5
5
 
6
- import type { ConnectPermissionRequest, EnboxUserAgent, HdIdentityVault, LocalDwnStrategy, PortableIdentity } from '@enbox/agent';
6
+ import type { PortableDid } from '@enbox/dids';
7
+ import type { ConnectPermissionRequest, DwnDataEncodedRecordsWriteMessage, DwnProtocolDefinition, EnboxUserAgent, HdIdentityVault, LocalDwnStrategy, PortableIdentity } from '@enbox/agent';
7
8
 
8
9
  import type { PasswordProvider } from './password-provider.js';
9
10
 
@@ -210,6 +211,55 @@ export interface RegistrationOptions {
210
211
  persistTokens?: boolean;
211
212
  }
212
213
 
214
+ // ─── Connect Handler ─────────────────────────────────────────────
215
+
216
+ /**
217
+ * Result of a successful connect handler invocation.
218
+ *
219
+ * Contains the delegated credentials returned by the wallet.
220
+ * All connect handlers (browser popup, relay, CLI, etc.) must
221
+ * return this shape on success.
222
+ */
223
+ export interface ConnectResult {
224
+ /** The portable delegate DID (includes private keys). */
225
+ delegatePortableDid: PortableDid;
226
+
227
+ /** Permission grants for the requested protocols. */
228
+ delegateGrants: DwnDataEncodedRecordsWriteMessage[];
229
+
230
+ /** The DID of the identity the user approved (the wallet owner's DID). */
231
+ connectedDid: string;
232
+ }
233
+
234
+ /**
235
+ * A connect handler obtains delegated credentials from a wallet.
236
+ *
237
+ * Different environments provide different implementations:
238
+ * - **Browser**: popup + postMessage (`BrowserConnectHandler` from `@enbox/browser`)
239
+ * - **Relay**: QR/PIN relay flow (`WalletConnect.initClient` from `@enbox/auth`)
240
+ * - **CLI**: terminal QR/URL + polling (custom handler)
241
+ * - **Desktop**: native window management (custom handler)
242
+ *
243
+ * @example
244
+ * ```ts
245
+ * import { BrowserConnectHandler } from '@enbox/browser';
246
+ * const auth = await AuthManager.create({
247
+ * connectHandler: BrowserConnectHandler(),
248
+ * });
249
+ * ```
250
+ */
251
+ export interface ConnectHandler {
252
+ /**
253
+ * Obtain delegated credentials from a wallet.
254
+ *
255
+ * @param params.permissionRequests - Agent-level permission requests.
256
+ * @returns The delegate credentials, or `undefined` if the user denied.
257
+ */
258
+ requestAccess(params: {
259
+ permissionRequests: ConnectPermissionRequest[];
260
+ }): Promise<ConnectResult | undefined>;
261
+ }
262
+
213
263
  /** Options for {@link AuthManager.create}. */
214
264
  export interface AuthManagerOptions {
215
265
  /**
@@ -298,6 +348,26 @@ export interface AuthManagerOptions {
298
348
 
299
349
  /** DWN registration configuration. */
300
350
  registration?: RegistrationOptions;
351
+
352
+ /**
353
+ * Default connect handler for delegated connect flows.
354
+ *
355
+ * Used by `connect()` when the caller provides `protocols` (or other
356
+ * non-local-connect options) but does not pass a per-call handler.
357
+ *
358
+ * @example
359
+ * ```ts
360
+ * import { BrowserConnectHandler } from '@enbox/browser';
361
+ *
362
+ * const auth = await AuthManager.create({
363
+ * connectHandler: BrowserConnectHandler(),
364
+ * });
365
+ *
366
+ * // Later — uses the default handler automatically
367
+ * const session = await auth.connect({ protocols: [NotesProtocol] });
368
+ * ```
369
+ */
370
+ connectHandler?: ConnectHandler;
301
371
  }
302
372
 
303
373
  /** Options for {@link AuthManager.connect}. */
@@ -316,8 +386,101 @@ export interface LocalConnectOptions {
316
386
 
317
387
  /** Identity metadata. */
318
388
  metadata?: { name?: string };
389
+
390
+ /**
391
+ * Whether to create a default identity if none exist.
392
+ *
393
+ * - `false` (default) — Skip automatic identity creation. The session is
394
+ * returned with the **agent DID** as the connected DID and no identity
395
+ * metadata. Use this when the app manages identity creation separately
396
+ * (e.g. a web wallet with an explicit "Create Identity" flow after
397
+ * vault setup).
398
+ *
399
+ * - `true` — If no identities exist after vault initialisation, a new
400
+ * `did:dht` identity is created automatically. Use this when vault
401
+ * setup and identity creation are combined into a single step (e.g.
402
+ * Electrobun's create wizard).
403
+ *
404
+ * @default false
405
+ */
406
+ createIdentity?: boolean;
407
+ }
408
+
409
+ // ─── DWeb Connect ────────────────────────────────────────────────
410
+
411
+ /**
412
+ * A protocol permission request in simplified form.
413
+ *
414
+ * Dapp developers can pass just a protocol definition (default permissions:
415
+ * `['read', 'write', 'query', 'subscribe']`), or an object with explicit
416
+ * permissions.
417
+ */
418
+ export type ProtocolRequest =
419
+ | DwnProtocolDefinition
420
+ | { definition: DwnProtocolDefinition; permissions: Permission[] };
421
+
422
+ /** Shorthand permission names for DWN protocol scopes. */
423
+ export type Permission = 'write' | 'read' | 'delete' | 'query' | 'subscribe' | 'configure';
424
+
425
+ /** Default permissions granted when only a protocol definition is provided. */
426
+ export const DEFAULT_PERMISSIONS: Permission[] = ['read', 'write', 'query', 'subscribe', 'configure'];
427
+
428
+ /**
429
+ * Options for a handler-based (delegated) connect flow.
430
+ *
431
+ * Used when `connect()` delegates credential acquisition to a
432
+ * {@link ConnectHandler}. The handler is responsible for the
433
+ * environment-specific transport (popup, relay, CLI, etc.).
434
+ */
435
+ export interface HandlerConnectOptions {
436
+ /**
437
+ * Protocols to request access to.
438
+ *
439
+ * Each entry can be either a protocol definition (uses default permissions)
440
+ * or an object with `{ definition, permissions }` for explicit control.
441
+ *
442
+ * @example
443
+ * ```ts
444
+ * // Default permissions (read, write, query, subscribe)
445
+ * protocols: [NotesProtocol]
446
+ *
447
+ * // Explicit permissions
448
+ * protocols: [
449
+ * { definition: NotesProtocol, permissions: ['read', 'write'] },
450
+ * { definition: PhotosProtocol, permissions: ['read'] },
451
+ * ]
452
+ * ```
453
+ */
454
+ protocols?: ProtocolRequest[];
455
+
456
+ /**
457
+ * Connect handler for this call. Overrides the default handler set
458
+ * on `AuthManager.create()`.
459
+ */
460
+ connectHandler?: ConnectHandler;
461
+
462
+ /** Override manager default sync interval. */
463
+ sync?: SyncOption;
319
464
  }
320
465
 
466
+ /**
467
+ * Unified options for {@link AuthManager.connect}.
468
+ *
469
+ * `connect()` routes to the appropriate flow based on the options:
470
+ *
471
+ * - **Handler-based connect** (dapps): triggered when `protocols` or
472
+ * `connectHandler` is provided. Delegates to the connect handler
473
+ * for credential acquisition.
474
+ *
475
+ * - **Local connect** (wallets / CLI): triggered when `password`,
476
+ * `createIdentity`, or `recoveryPhrase` is provided.
477
+ *
478
+ * In both cases, `connect()` first attempts to restore a previous session
479
+ * from storage. If a valid session exists, it is returned immediately
480
+ * without any user interaction.
481
+ */
482
+ export type ConnectOptions = HandlerConnectOptions | LocalConnectOptions;
483
+
321
484
  /** Options for {@link AuthManager.walletConnect}. */
322
485
  export interface WalletConnectOptions {
323
486
  /** Display name shown in the wallet during the connect flow. */
@@ -66,10 +66,7 @@ export type WalletConnectClientOptions = {
66
66
  validatePin: () => Promise<string>;
67
67
  };
68
68
 
69
- /**
70
- * Shorthand for the types of permissions that can be requested.
71
- */
72
- export type Permission = 'write' | 'read' | 'delete' | 'query' | 'subscribe' | 'configure';
69
+ import type { Permission } from './types.js';
73
70
 
74
71
  /**
75
72
  * The options for creating a permission request for a given protocol.
@@ -200,8 +197,10 @@ async function initClient({
200
197
  /**
201
198
  * Creates a set of Dwn Permission Scopes to request for a given protocol.
202
199
  *
203
- * If no permissions are provided, the default is to request all relevant record permissions (write, read, delete, query, subscribe).
204
- * 'configure' is not included by default, as this gives the application a lot of control over the protocol.
200
+ * If no permissions are provided, the default permissions from
201
+ * {@link DEFAULT_PERMISSIONS} are used (read, write, query, subscribe, configure).
202
+ * The 'configure' permission is included because dapps using the TypedEnbox API
203
+ * need it to auto-install the protocol on their local DWN via _autoConfigureOnce().
205
204
  */
206
205
  function createPermissionRequestForProtocol({ definition, permissions }: ProtocolPermissionOptions): ConnectPermissionRequest {
207
206
  const requests: DwnPermissionScope[] = [];