@enbox/auth 0.3.1

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 (66) hide show
  1. package/dist/esm/auth-manager.js +496 -0
  2. package/dist/esm/auth-manager.js.map +1 -0
  3. package/dist/esm/events.js +65 -0
  4. package/dist/esm/events.js.map +1 -0
  5. package/dist/esm/flows/dwn-discovery.js +281 -0
  6. package/dist/esm/flows/dwn-discovery.js.map +1 -0
  7. package/dist/esm/flows/dwn-registration.js +122 -0
  8. package/dist/esm/flows/dwn-registration.js.map +1 -0
  9. package/dist/esm/flows/import-identity.js +175 -0
  10. package/dist/esm/flows/import-identity.js.map +1 -0
  11. package/dist/esm/flows/local-connect.js +141 -0
  12. package/dist/esm/flows/local-connect.js.map +1 -0
  13. package/dist/esm/flows/session-restore.js +109 -0
  14. package/dist/esm/flows/session-restore.js.map +1 -0
  15. package/dist/esm/flows/wallet-connect.js +199 -0
  16. package/dist/esm/flows/wallet-connect.js.map +1 -0
  17. package/dist/esm/identity-session.js +33 -0
  18. package/dist/esm/identity-session.js.map +1 -0
  19. package/dist/esm/index.js +50 -0
  20. package/dist/esm/index.js.map +1 -0
  21. package/dist/esm/storage/storage.js +152 -0
  22. package/dist/esm/storage/storage.js.map +1 -0
  23. package/dist/esm/types.js +30 -0
  24. package/dist/esm/types.js.map +1 -0
  25. package/dist/esm/vault/vault-manager.js +95 -0
  26. package/dist/esm/vault/vault-manager.js.map +1 -0
  27. package/dist/types/auth-manager.d.ts +176 -0
  28. package/dist/types/auth-manager.d.ts.map +1 -0
  29. package/dist/types/events.d.ts +36 -0
  30. package/dist/types/events.d.ts.map +1 -0
  31. package/dist/types/flows/dwn-discovery.d.ts +157 -0
  32. package/dist/types/flows/dwn-discovery.d.ts.map +1 -0
  33. package/dist/types/flows/dwn-registration.d.ts +39 -0
  34. package/dist/types/flows/dwn-registration.d.ts.map +1 -0
  35. package/dist/types/flows/import-identity.d.ts +35 -0
  36. package/dist/types/flows/import-identity.d.ts.map +1 -0
  37. package/dist/types/flows/local-connect.d.ts +29 -0
  38. package/dist/types/flows/local-connect.d.ts.map +1 -0
  39. package/dist/types/flows/session-restore.d.ts +27 -0
  40. package/dist/types/flows/session-restore.d.ts.map +1 -0
  41. package/dist/types/flows/wallet-connect.d.ts +44 -0
  42. package/dist/types/flows/wallet-connect.d.ts.map +1 -0
  43. package/dist/types/identity-session.d.ts +52 -0
  44. package/dist/types/identity-session.d.ts.map +1 -0
  45. package/dist/types/index.d.ts +45 -0
  46. package/dist/types/index.d.ts.map +1 -0
  47. package/dist/types/storage/storage.d.ts +54 -0
  48. package/dist/types/storage/storage.d.ts.map +1 -0
  49. package/dist/types/types.d.ts +312 -0
  50. package/dist/types/types.d.ts.map +1 -0
  51. package/dist/types/vault/vault-manager.d.ts +57 -0
  52. package/dist/types/vault/vault-manager.d.ts.map +1 -0
  53. package/package.json +71 -0
  54. package/src/auth-manager.ts +569 -0
  55. package/src/events.ts +66 -0
  56. package/src/flows/dwn-discovery.ts +300 -0
  57. package/src/flows/dwn-registration.ts +157 -0
  58. package/src/flows/import-identity.ts +217 -0
  59. package/src/flows/local-connect.ts +171 -0
  60. package/src/flows/session-restore.ts +135 -0
  61. package/src/flows/wallet-connect.ts +225 -0
  62. package/src/identity-session.ts +65 -0
  63. package/src/index.ts +89 -0
  64. package/src/storage/storage.ts +136 -0
  65. package/src/types.ts +388 -0
  66. package/src/vault/vault-manager.ts +89 -0
@@ -0,0 +1,312 @@
1
+ /**
2
+ * @module @enbox/auth
3
+ * Public types for the authentication and identity management SDK.
4
+ */
5
+ import type { ConnectPermissionRequest, EnboxUserAgent, HdIdentityVault, LocalDwnStrategy, PortableIdentity } from '@enbox/agent';
6
+ export type { ConnectPermissionRequest, HdIdentityVault, IdentityVaultBackup, LocalDwnStrategy, PortableIdentity } from '@enbox/agent';
7
+ export type { EnboxUserAgent } from '@enbox/agent';
8
+ /**
9
+ * Controls DWN synchronisation behaviour.
10
+ *
11
+ * - `'off'` — Sync disabled entirely.
12
+ * - An interval string such as `'30s'`, `'2m'`, `'1h'` — Poll mode at the
13
+ * specified interval.
14
+ * - `undefined` (omitted) — Live WebSocket sync (default).
15
+ */
16
+ export type SyncOption = 'off' | `${number}${'s' | 'm' | 'h'}`;
17
+ /**
18
+ * The possible states of the auth manager.
19
+ *
20
+ * State transitions:
21
+ * ```
22
+ * uninitialized → locked → unlocked → connected
23
+ * ↑ ↑ │
24
+ * └──────────┴──────────┘ (disconnect / lock)
25
+ * ```
26
+ */
27
+ export type AuthState = 'uninitialized' | 'locked' | 'unlocked' | 'connected';
28
+ /** All event names emitted by the auth manager. */
29
+ export type AuthEvent = 'state-change' | 'session-start' | 'session-end' | 'identity-added' | 'identity-removed' | 'vault-locked' | 'vault-unlocked' | 'local-dwn-available' | 'local-dwn-unavailable';
30
+ /** Payload type for each event, keyed by event name. */
31
+ export interface AuthEventMap {
32
+ 'state-change': {
33
+ previous: AuthState;
34
+ current: AuthState;
35
+ };
36
+ 'session-start': {
37
+ session: AuthSessionInfo;
38
+ };
39
+ 'session-end': {
40
+ did: string;
41
+ };
42
+ 'identity-added': {
43
+ identity: IdentityInfo;
44
+ };
45
+ 'identity-removed': {
46
+ didUri: string;
47
+ };
48
+ 'vault-locked': Record<string, never>;
49
+ 'vault-unlocked': Record<string, never>;
50
+ /** Emitted when a local DWN server is discovered and validated. */
51
+ 'local-dwn-available': {
52
+ endpoint: string;
53
+ };
54
+ /** Emitted when no local DWN server could be discovered or a previously known one is no longer reachable. */
55
+ 'local-dwn-unavailable': Record<string, never>;
56
+ }
57
+ /** A type-safe event handler for a specific event. */
58
+ export type AuthEventHandler<E extends AuthEvent = AuthEvent> = (payload: AuthEventMap[E]) => void;
59
+ /** Lightweight metadata about a stored identity. */
60
+ export interface IdentityInfo {
61
+ /** The DID URI for this identity. */
62
+ didUri: string;
63
+ /** Human-readable name. */
64
+ name: string;
65
+ /**
66
+ * Present when this identity is a delegate of another DID
67
+ * (i.e. connected via wallet connect).
68
+ */
69
+ connectedDid?: string;
70
+ }
71
+ /** Serializable session info for the `session-start` event. */
72
+ export interface AuthSessionInfo {
73
+ did: string;
74
+ delegateDid?: string;
75
+ identity: IdentityInfo;
76
+ }
77
+ /** Parameters passed to the onProviderAuthRequired callback. */
78
+ export interface ProviderAuthParams {
79
+ /** Full authorize URL to open in a browser (query params already appended). */
80
+ authorizeUrl: string;
81
+ /** The DWN endpoint URL this auth is for (informational). */
82
+ dwnEndpoint: string;
83
+ /** CSRF nonce — the provider will return this unchanged in the redirect. */
84
+ state: string;
85
+ }
86
+ /** Result returned by the app after the user completes provider auth. */
87
+ export interface ProviderAuthResult {
88
+ /** Authorization code from the provider's redirect. */
89
+ code: string;
90
+ /** Must match the state from ProviderAuthParams (CSRF validation). */
91
+ state: string;
92
+ }
93
+ /** Persisted registration token data for a DWN endpoint. */
94
+ export interface RegistrationTokenData {
95
+ /** Opaque registration token for POST /registration. */
96
+ registrationToken: string;
97
+ /** Refresh token for obtaining new registration tokens. */
98
+ refreshToken?: string;
99
+ /** Unix timestamp (ms) when the token expires. Undefined = never expires. */
100
+ expiresAt?: number;
101
+ /** Provider's token exchange URL (needed for code exchange). */
102
+ tokenUrl: string;
103
+ /** Provider's refresh URL (needed for token refresh). */
104
+ refreshUrl?: string;
105
+ }
106
+ /**
107
+ * DWN registration configuration.
108
+ *
109
+ * When provided, the agent DID and connected DID will be registered with
110
+ * DWN endpoints after identity creation. Supports two paths:
111
+ *
112
+ * 1. **Provider auth** (`provider-auth-v0`) — the DWN endpoint requires
113
+ * OAuth-style auth. If {@link onProviderAuthRequired} is provided and
114
+ * the server advertises provider auth, the app handles the auth flow.
115
+ * 2. **Proof of Work** (default fallback) — the DWN endpoint requires
116
+ * solving a PoW challenge to register.
117
+ */
118
+ export interface RegistrationOptions {
119
+ /** Called when all DWN registrations complete successfully. */
120
+ onSuccess: () => void;
121
+ /** Called when any DWN registration fails. */
122
+ onFailure: (error: unknown) => void;
123
+ /**
124
+ * Called when a DWN endpoint requires provider auth (`'provider-auth-v0'`).
125
+ *
126
+ * The app should open the `authorizeUrl` in a browser, capture the
127
+ * redirect with the auth code, and return the result. If not provided,
128
+ * endpoints requiring provider auth fall back to PoW registration.
129
+ */
130
+ onProviderAuthRequired?: (params: ProviderAuthParams) => Promise<ProviderAuthResult>;
131
+ /**
132
+ * Pre-existing registration tokens from a previous session, keyed by
133
+ * DWN endpoint URL. If a valid (non-expired) token exists for an
134
+ * endpoint, it is used directly without re-running the auth flow.
135
+ */
136
+ registrationTokens?: Record<string, RegistrationTokenData>;
137
+ /**
138
+ * Called when new or refreshed registration tokens are obtained.
139
+ * The app should persist these for future sessions.
140
+ */
141
+ onRegistrationTokens?: (tokens: Record<string, RegistrationTokenData>) => void;
142
+ }
143
+ /** Options for {@link AuthManager.create}. */
144
+ export interface AuthManagerOptions {
145
+ /**
146
+ * Provide a pre-built {@link EnboxUserAgent} instance.
147
+ *
148
+ * When provided, `dataPath`, `agentVault`, and `localDwnStrategy` are
149
+ * ignored — the agent is used as-is. This is the escape hatch for
150
+ * advanced scenarios like custom DWN stores (e.g., SQLite-backed DWN).
151
+ *
152
+ * @example
153
+ * ```ts
154
+ * const agent = await EnboxUserAgent.create({ dwnApi: myCustomDwnApi });
155
+ * const auth = await AuthManager.create({ agent });
156
+ * ```
157
+ */
158
+ agent?: EnboxUserAgent;
159
+ /**
160
+ * Provide a custom {@link HdIdentityVault} implementation.
161
+ * Defaults to a LevelDB-backed vault with PBES2-HS512+A256KW encryption.
162
+ * Ignored when `agent` is provided.
163
+ */
164
+ agentVault?: HdIdentityVault;
165
+ /**
166
+ * Controls local DWN discovery behavior for remote-target DWN sends/sync.
167
+ * `'off'` (default) disables local probing, `'prefer'` tries local first
168
+ * then falls back to DID-document endpoints, `'only'` requires a local server.
169
+ * Ignored when `agent` is provided.
170
+ */
171
+ localDwnStrategy?: LocalDwnStrategy;
172
+ /**
173
+ * Data path for agent storage.
174
+ * - Browser default: `'DATA/AGENT'`
175
+ * - CLI default: `'~/.enbox'`
176
+ *
177
+ * Ignored when `agent` is provided.
178
+ */
179
+ dataPath?: string;
180
+ /** Storage adapter for session persistence. Auto-detected if not provided. */
181
+ storage?: StorageAdapter;
182
+ /**
183
+ * Default password for vault operations.
184
+ * If not provided, an insecure default is used (with a console warning).
185
+ */
186
+ password?: string;
187
+ /**
188
+ * Sync interval for DWN synchronization.
189
+ * - `'off'` — disable sync
190
+ * - `'15s'`, `'1m'`, etc. — poll at interval
191
+ * - `undefined` — live WebSocket sync
192
+ */
193
+ sync?: SyncOption;
194
+ /** Default DWN endpoints for new identities. */
195
+ dwnEndpoints?: string[];
196
+ /** DWN registration configuration. */
197
+ registration?: RegistrationOptions;
198
+ }
199
+ /** Options for {@link AuthManager.connect}. */
200
+ export interface LocalConnectOptions {
201
+ /** Vault password (overrides manager default). */
202
+ password?: string;
203
+ /** Re-derive identity from an existing BIP-39 recovery phrase. */
204
+ recoveryPhrase?: string;
205
+ /** Override manager default sync interval. */
206
+ sync?: SyncOption;
207
+ /** Override manager default DWN endpoints. */
208
+ dwnEndpoints?: string[];
209
+ /** Identity metadata. */
210
+ metadata?: {
211
+ name?: string;
212
+ };
213
+ }
214
+ /** Options for {@link AuthManager.walletConnect}. */
215
+ export interface WalletConnectOptions {
216
+ /** Display name shown in the wallet during the connect flow. */
217
+ displayName: string;
218
+ /** URL of the connect relay server. */
219
+ connectServerUrl: string;
220
+ /** Wallet URI scheme. Defaults to `'web5://connect'`. */
221
+ walletUri?: string;
222
+ /**
223
+ * Protocol permission requests for the wallet connect flow.
224
+ *
225
+ * Each entry is a `ConnectPermissionRequest` from `@enbox/agent` containing
226
+ * a `protocolDefinition` and `permissionScopes`. Use
227
+ * `WalletConnect.createPermissionRequestForProtocol()` to build these.
228
+ */
229
+ permissionRequests: ConnectPermissionRequest[];
230
+ /** Called when the wallet URI is ready (render as QR code). */
231
+ onWalletUriReady: (uri: string) => void;
232
+ /** Called to collect the PIN from the user. */
233
+ validatePin: () => Promise<string>;
234
+ /** Override manager default sync interval. */
235
+ sync?: SyncOption;
236
+ }
237
+ /** Options for {@link AuthManager.importFromPhrase}. */
238
+ export interface ImportFromPhraseOptions {
239
+ /** The BIP-39 recovery phrase. */
240
+ recoveryPhrase: string;
241
+ /** Password to protect the vault. */
242
+ password: string;
243
+ /** Override manager default sync interval. */
244
+ sync?: SyncOption;
245
+ /** Override manager default DWN endpoints. */
246
+ dwnEndpoints?: string[];
247
+ }
248
+ /** Options for {@link AuthManager.importFromPortable}. */
249
+ export interface ImportFromPortableOptions {
250
+ /** The portable identity JSON to import. */
251
+ portableIdentity: PortableIdentity;
252
+ /** Override manager default sync interval. */
253
+ sync?: SyncOption;
254
+ }
255
+ /** Options for {@link AuthManager.restoreSession}. */
256
+ export interface RestoreSessionOptions {
257
+ /** Password to unlock the vault (needed if vault is locked). */
258
+ password?: string;
259
+ }
260
+ /** Options for {@link AuthManager.disconnect}. */
261
+ export interface DisconnectOptions {
262
+ /**
263
+ * If `true`, performs a nuclear wipe: clears all localStorage keys,
264
+ * deletes all IndexedDB databases, and removes persisted session data.
265
+ * Default: `false` (clean disconnect — keeps vault and identities).
266
+ */
267
+ clearStorage?: boolean;
268
+ /**
269
+ * Milliseconds to wait for pending sync operations before disconnecting.
270
+ * Default: `2000`.
271
+ */
272
+ timeout?: number;
273
+ }
274
+ /**
275
+ * Platform-agnostic key-value storage adapter for session persistence.
276
+ * Implementations are provided for browser (localStorage) and CLI (file system).
277
+ */
278
+ export interface StorageAdapter {
279
+ /** Get a value by key. Returns `null` if not found. */
280
+ get(key: string): Promise<string | null>;
281
+ /** Set a key-value pair. */
282
+ set(key: string, value: string): Promise<void>;
283
+ /** Remove a key. */
284
+ remove(key: string): Promise<void>;
285
+ /** Clear all stored data. */
286
+ clear(): Promise<void>;
287
+ }
288
+ /** The insecure default password used when none is provided. */
289
+ export declare const INSECURE_DEFAULT_PASSWORD = "insecure-static-phrase";
290
+ /**
291
+ * Storage keys used by the auth manager for session persistence.
292
+ * @internal
293
+ */
294
+ export declare const STORAGE_KEYS: {
295
+ /** Whether a session was previously established. */
296
+ readonly PREVIOUSLY_CONNECTED: "enbox:auth:previouslyConnected";
297
+ /** The DID URI of the last active identity. */
298
+ readonly ACTIVE_IDENTITY: "enbox:auth:activeIdentity";
299
+ /** The delegate DID URI (for wallet-connected sessions). */
300
+ readonly DELEGATE_DID: "enbox:auth:delegateDid";
301
+ /** The connected DID (for wallet-connected sessions). */
302
+ readonly CONNECTED_DID: "enbox:auth:connectedDid";
303
+ /**
304
+ * The base URL of the local DWN server discovered via the `dwn://register`
305
+ * browser redirect flow. Persisted so subsequent page loads can skip the
306
+ * redirect and inject the endpoint directly.
307
+ *
308
+ * @see https://github.com/enboxorg/enbox/issues/589
309
+ */
310
+ readonly LOCAL_DWN_ENDPOINT: "enbox:auth:localDwnEndpoint";
311
+ };
312
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,wBAAwB,EAAE,cAAc,EAAE,eAAe,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGlI,YAAY,EAAE,wBAAwB,EAAE,eAAe,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGvI,YAAY,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAInD;;;;;;;GAOG;AACH,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,GAAG,MAAM,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,EAAE,CAAC;AAI/D;;;;;;;;;GASG;AACH,MAAM,MAAM,SAAS,GACjB,eAAe,GACf,QAAQ,GACR,UAAU,GACV,WAAW,CAAC;AAIhB,mDAAmD;AACnD,MAAM,MAAM,SAAS,GACjB,cAAc,GACd,eAAe,GACf,aAAa,GACb,gBAAgB,GAChB,kBA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wraps {@link HdIdentityVault} with a high-level API
3
+ * and emits events on lock/unlock.
4
+ * @module
5
+ */
6
+ import type { HdIdentityVault, IdentityVaultBackup } from '@enbox/agent';
7
+ import type { AuthEventEmitter } from '../events.js';
8
+ /**
9
+ * Manages the encrypted identity vault lifecycle.
10
+ *
11
+ * The vault stores the agent's DID and content encryption key (CEK),
12
+ * protected by a user password using PBES2-HS512+A256KW with a 210K
13
+ * iteration work factor. The vault supports HD key derivation from
14
+ * a BIP-39 mnemonic for recovery.
15
+ */
16
+ export declare class VaultManager {
17
+ private readonly _vault;
18
+ private readonly _emitter;
19
+ constructor(vault: HdIdentityVault, emitter: AuthEventEmitter);
20
+ /** The underlying vault instance (for advanced usage). */
21
+ get raw(): HdIdentityVault;
22
+ /** Whether the vault has been initialized (has encrypted data). */
23
+ isInitialized(): Promise<boolean>;
24
+ /** Whether the vault is currently locked (synchronous check). */
25
+ get isLocked(): boolean;
26
+ /**
27
+ * Unlock the vault with the given password.
28
+ * Decrypts the CEK into memory so the agent DID can be retrieved.
29
+ *
30
+ * @throws If the password is incorrect or vault is not initialized.
31
+ */
32
+ unlock(password: string): Promise<void>;
33
+ /**
34
+ * Lock the vault, clearing the CEK from memory.
35
+ * After locking, the password must be provided again to unlock.
36
+ */
37
+ lock(): Promise<void>;
38
+ /**
39
+ * Change the vault password. Re-encrypts the CEK with the new password.
40
+ *
41
+ * @throws If the old password is incorrect or vault is locked.
42
+ */
43
+ changePassword(oldPassword: string, newPassword: string): Promise<void>;
44
+ /**
45
+ * Create a backup of the vault.
46
+ *
47
+ * @throws If the vault is not initialized or is locked.
48
+ */
49
+ backup(): Promise<IdentityVaultBackup>;
50
+ /**
51
+ * Restore the vault from a backup.
52
+ *
53
+ * @throws If the password doesn't match the backup's encryption.
54
+ */
55
+ restore(backup: IdentityVaultBackup, password: string): Promise<void>;
56
+ }
57
+ //# sourceMappingURL=vault-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vault-manager.d.ts","sourceRoot":"","sources":["../../../src/vault/vault-manager.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAEzE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD;;;;;;;GAOG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAkB;IACzC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAmB;gBAEhC,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,gBAAgB;IAK7D,0DAA0D;IAC1D,IAAI,GAAG,IAAI,eAAe,CAEzB;IAED,mEAAmE;IAC7D,aAAa,IAAI,OAAO,CAAC,OAAO,CAAC;IAIvC,iEAAiE;IACjE,IAAI,QAAQ,IAAI,OAAO,CAEtB;IAED;;;;;OAKG;IACG,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAK7C;;;OAGG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAK3B;;;;OAIG;IACG,cAAc,CAAC,WAAW,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI7E;;;;OAIG;IACG,MAAM,IAAI,OAAO,CAAC,mBAAmB,CAAC;IAI5C;;;;OAIG;IACG,OAAO,CAAC,MAAM,EAAE,mBAAmB,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAG5E"}
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "@enbox/auth",
3
+ "version": "0.3.1",
4
+ "description": "Headless authentication and identity management SDK for Enbox",
5
+ "type": "module",
6
+ "main": "./dist/esm/index.js",
7
+ "module": "./dist/esm/index.js",
8
+ "types": "./dist/types/index.d.ts",
9
+ "scripts": {
10
+ "clean": "rimraf dist",
11
+ "build:esm": "rimraf dist/esm dist/types && bun tsc -p tsconfig.json",
12
+ "build": "bun run clean && bun run build:esm",
13
+ "lint": "eslint . --max-warnings 0",
14
+ "lint:fix": "eslint . --fix",
15
+ "test:node": "bun test --timeout 10000 .spec.ts",
16
+ "test:node:coverage": "bun test --coverage --coverage-reporter=text --coverage-reporter=lcov --coverage-dir=coverage .spec.ts"
17
+ },
18
+ "homepage": "https://github.com/enboxorg/enbox/tree/main/packages/auth#readme",
19
+ "bugs": "https://github.com/enboxorg/enbox/issues",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/enboxorg/enbox.git",
23
+ "directory": "packages/auth"
24
+ },
25
+ "license": "Apache-2.0",
26
+ "contributors": [
27
+ {
28
+ "name": "Liran Cohen",
29
+ "url": "https://github.com/LiranCohen"
30
+ }
31
+ ],
32
+ "files": [
33
+ "dist",
34
+ "src"
35
+ ],
36
+ "exports": {
37
+ ".": {
38
+ "types": "./dist/types/index.d.ts",
39
+ "import": "./dist/esm/index.js"
40
+ }
41
+ },
42
+ "keywords": [
43
+ "auth",
44
+ "authentication",
45
+ "identity",
46
+ "decentralized",
47
+ "decentralized-identity",
48
+ "DID",
49
+ "web5",
50
+ "enbox"
51
+ ],
52
+ "publishConfig": {
53
+ "access": "public"
54
+ },
55
+ "engines": {
56
+ "bun": ">=1.0.0"
57
+ },
58
+ "dependencies": {
59
+ "@enbox/agent": "0.3.1",
60
+ "@enbox/common": "0.0.7",
61
+ "@enbox/dids": "0.0.9",
62
+ "@enbox/dwn-clients": "0.1.0",
63
+ "level": "8.0.0"
64
+ },
65
+ "devDependencies": {
66
+ "@types/node": "20.14.8",
67
+ "bun-types": "latest",
68
+ "rimraf": "4.4.0",
69
+ "typescript": "5.5.4"
70
+ }
71
+ }