@enbox/auth 0.5.0 → 0.6.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 (81) hide show
  1. package/dist/esm/auth-manager.js +94 -167
  2. package/dist/esm/auth-manager.js.map +1 -1
  3. package/dist/esm/connect/import.js +131 -0
  4. package/dist/esm/connect/import.js.map +1 -0
  5. package/dist/esm/connect/lifecycle.js +235 -0
  6. package/dist/esm/connect/lifecycle.js.map +1 -0
  7. package/dist/esm/connect/local.js +91 -0
  8. package/dist/esm/connect/local.js.map +1 -0
  9. package/dist/esm/{flows/session-restore.js → connect/restore.js} +34 -56
  10. package/dist/esm/connect/restore.js.map +1 -0
  11. package/dist/esm/{flows/wallet-connect.js → connect/wallet.js} +28 -35
  12. package/dist/esm/connect/wallet.js.map +1 -0
  13. package/dist/esm/{flows/dwn-discovery.js → discovery.js} +2 -2
  14. package/dist/esm/discovery.js.map +1 -0
  15. package/dist/esm/index.js +4 -4
  16. package/dist/esm/index.js.map +1 -1
  17. package/dist/esm/{flows/dwn-registration.js → registration.js} +2 -2
  18. package/dist/esm/registration.js.map +1 -0
  19. package/dist/esm/types.js +2 -0
  20. package/dist/esm/types.js.map +1 -1
  21. package/dist/esm/wallet-connect-client.js +188 -0
  22. package/dist/esm/wallet-connect-client.js.map +1 -0
  23. package/dist/types/auth-manager.d.ts +20 -6
  24. package/dist/types/auth-manager.d.ts.map +1 -1
  25. package/dist/types/connect/import.d.ts +25 -0
  26. package/dist/types/connect/import.d.ts.map +1 -0
  27. package/dist/types/connect/lifecycle.d.ts +152 -0
  28. package/dist/types/connect/lifecycle.d.ts.map +1 -0
  29. package/dist/types/connect/local.d.ts +18 -0
  30. package/dist/types/connect/local.d.ts.map +1 -0
  31. package/dist/types/connect/restore.d.ts +18 -0
  32. package/dist/types/connect/restore.d.ts.map +1 -0
  33. package/dist/types/{flows/wallet-connect.d.ts → connect/wallet.d.ts} +5 -14
  34. package/dist/types/connect/wallet.d.ts.map +1 -0
  35. package/dist/types/{flows/dwn-discovery.d.ts → discovery.d.ts} +3 -3
  36. package/dist/types/discovery.d.ts.map +1 -0
  37. package/dist/types/index.d.ts +5 -4
  38. package/dist/types/index.d.ts.map +1 -1
  39. package/dist/types/{flows/dwn-registration.d.ts → registration.d.ts} +2 -2
  40. package/dist/types/registration.d.ts.map +1 -0
  41. package/dist/types/types.d.ts +6 -3
  42. package/dist/types/types.d.ts.map +1 -1
  43. package/dist/types/wallet-connect-client.d.ts +89 -0
  44. package/dist/types/wallet-connect-client.d.ts.map +1 -0
  45. package/package.json +9 -5
  46. package/src/auth-manager.ts +94 -188
  47. package/src/connect/import.ts +148 -0
  48. package/src/connect/lifecycle.ts +321 -0
  49. package/src/connect/local.ts +101 -0
  50. package/src/connect/restore.ts +117 -0
  51. package/src/{flows/wallet-connect.ts → connect/wallet.ts} +37 -54
  52. package/src/{flows/dwn-discovery.ts → discovery.ts} +4 -3
  53. package/src/index.ts +5 -4
  54. package/src/{flows/dwn-registration.ts → registration.ts} +2 -2
  55. package/src/types.ts +7 -3
  56. package/src/wallet-connect-client.ts +278 -0
  57. package/dist/esm/flows/dwn-discovery.js.map +0 -1
  58. package/dist/esm/flows/dwn-registration.js.map +0 -1
  59. package/dist/esm/flows/import-identity.js +0 -177
  60. package/dist/esm/flows/import-identity.js.map +0 -1
  61. package/dist/esm/flows/local-connect.js +0 -158
  62. package/dist/esm/flows/local-connect.js.map +0 -1
  63. package/dist/esm/flows/session-restore.js.map +0 -1
  64. package/dist/esm/flows/wallet-connect.js.map +0 -1
  65. package/dist/esm/vault/vault-manager.js +0 -95
  66. package/dist/esm/vault/vault-manager.js.map +0 -1
  67. package/dist/types/flows/dwn-discovery.d.ts.map +0 -1
  68. package/dist/types/flows/dwn-registration.d.ts.map +0 -1
  69. package/dist/types/flows/import-identity.d.ts +0 -35
  70. package/dist/types/flows/import-identity.d.ts.map +0 -1
  71. package/dist/types/flows/local-connect.d.ts +0 -31
  72. package/dist/types/flows/local-connect.d.ts.map +0 -1
  73. package/dist/types/flows/session-restore.d.ts +0 -29
  74. package/dist/types/flows/session-restore.d.ts.map +0 -1
  75. package/dist/types/flows/wallet-connect.d.ts.map +0 -1
  76. package/dist/types/vault/vault-manager.d.ts +0 -57
  77. package/dist/types/vault/vault-manager.d.ts.map +0 -1
  78. package/src/flows/import-identity.ts +0 -219
  79. package/src/flows/local-connect.ts +0 -192
  80. package/src/flows/session-restore.ts +0 -155
  81. package/src/vault/vault-manager.ts +0 -89
@@ -11,7 +11,7 @@
11
11
  * @module
12
12
  */
13
13
  import type { EnboxUserAgent } from '@enbox/agent';
14
- import type { RegistrationOptions, RegistrationTokenData, StorageAdapter } from '../types.js';
14
+ import type { RegistrationOptions, RegistrationTokenData, StorageAdapter } from './types.js';
15
15
  /** @internal */
16
16
  export interface RegistrationContext {
17
17
  /** The user agent with RPC access for getServerInfo(). */
@@ -55,4 +55,4 @@ export declare function loadTokensFromStorage(storage: StorageAdapter): Promise<
55
55
  * @internal
56
56
  */
57
57
  export declare function saveTokensToStorage(storage: StorageAdapter, tokens: Record<string, RegistrationTokenData>): Promise<void>;
58
- //# sourceMappingURL=dwn-registration.d.ts.map
58
+ //# sourceMappingURL=registration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registration.d.ts","sourceRoot":"","sources":["../../src/registration.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAMnD,OAAO,KAAK,EACV,mBAAmB,EACnB,qBAAqB,EACrB,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB,gBAAgB;AAChB,MAAM,WAAW,mBAAmB;IAClC,0DAA0D;IAC1D,SAAS,EAAE,cAAc,CAAC;IAE1B,sCAAsC;IACtC,YAAY,EAAE,MAAM,EAAE,CAAC;IAEvB,yBAAyB;IACzB,QAAQ,EAAE,MAAM,CAAC;IAEjB,kDAAkD;IAClD,YAAY,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,OAAO,CAAC,EAAE,cAAc,CAAC;CAC1B;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,wBAAwB,CAC5C,GAAG,EAAE,mBAAmB,EACxB,YAAY,EAAE,mBAAmB,GAChC,OAAO,CAAC,IAAI,CAAC,CAqHf;AAID;;;;;;;GAOG;AACH,wBAAsB,qBAAqB,CACzC,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,qBAAqB,CAAC,CAAC,CAQhD;AAED;;;GAGG;AACH,wBAAsB,mBAAmB,CACvC,OAAO,EAAE,cAAc,EACvB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,qBAAqB,CAAC,GAC5C,OAAO,CAAC,IAAI,CAAC,CAEf"}
@@ -278,9 +278,10 @@ export interface WalletConnectOptions {
278
278
  /**
279
279
  * Protocol permission requests for the wallet connect flow.
280
280
  *
281
- * Each entry is a `ConnectPermissionRequest` from `@enbox/agent` containing
282
- * a `protocolDefinition` and `permissionScopes`. Use
283
- * `WalletConnect.createPermissionRequestForProtocol()` to build these.
281
+ * Each entry is a `ConnectPermissionRequest` containing a
282
+ * `protocolDefinition` and `permissionScopes`. Use
283
+ * `WalletConnect.createPermissionRequestForProtocol()` from `@enbox/auth`
284
+ * to build these.
284
285
  */
285
286
  permissionRequests: ConnectPermissionRequest[];
286
287
  /** Called when the wallet URI is ready (render as QR code). */
@@ -384,6 +385,8 @@ export interface StorageAdapter {
384
385
  }
385
386
  /** The insecure default password used when none is provided. */
386
387
  export declare const INSECURE_DEFAULT_PASSWORD = "insecure-static-phrase";
388
+ /** Default DWN endpoints for new identities when none are configured. */
389
+ export declare const DEFAULT_DWN_ENDPOINTS: string[];
387
390
  /**
388
391
  * Storage keys used by the auth manager for session persistence.
389
392
  * @internal
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,O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connect flow.
3
+ *
4
+ * Moved from `@enbox/agent/src/connect.ts` because `initClient` has zero
5
+ * coupling to agent internals (no vault, no key store, no DWN processing,
6
+ * no sync). Its only consumer is `auth/src/connect/wallet.ts`.
7
+ *
8
+ * The server-side counterpart (`EnboxConnectProtocol`) correctly stays in
9
+ * `@enbox/agent` because it uses `agent.processDwnRequest()`,
10
+ * `agent.sendDwnRequest()`, and `AgentPermissionsApi`.
11
+ *
12
+ * @module
13
+ */
14
+ import type { ConnectPermissionRequest, DwnProtocolDefinition } from '@enbox/agent';
15
+ import type { EnboxConnectResponse } from '@enbox/agent';
16
+ /**
17
+ * Options for initiating a wallet connect flow (remote, relay-mediated).
18
+ *
19
+ * This is the agent-level options type used by `initClient()`. The auth-level
20
+ * `WalletConnectOptions` (in `types.ts`) wraps this with additional fields
21
+ * like `sync`.
22
+ */
23
+ export type WalletConnectClientOptions = {
24
+ /** The user-friendly name of the app, displayed in the wallet consent UI. */
25
+ displayName: string;
26
+ /** The URL of the connect server which relays messages between the app and wallet. */
27
+ connectServerUrl: string;
28
+ /**
29
+ * The URI of the wallet app. Query params (`request_uri`, `encryption_key`)
30
+ * are appended and passed to `onWalletUriReady`.
31
+ * @example `enbox://connect` or `http://localhost:3000/`
32
+ */
33
+ walletUri: string;
34
+ /**
35
+ * The protocols of permissions requested, along with the definition and
36
+ * permission scopes for each protocol. The key is the protocol URL and
37
+ * the value is an object with the protocol definition and the permission scopes.
38
+ */
39
+ permissionRequests: ConnectPermissionRequest[];
40
+ /**
41
+ * Called with the wallet URI including query params (`request_uri`, `encryption_key`).
42
+ * The app should render this as a QR code or use it as a deep link.
43
+ *
44
+ * @param uri - The wallet URI with connect payload.
45
+ */
46
+ onWalletUriReady: (uri: string) => void;
47
+ /**
48
+ * Called to collect the PIN from the user. The PIN is used as AAD
49
+ * when decrypting the connect response from the relay.
50
+ *
51
+ * @returns A promise that resolves to the PIN as a string.
52
+ */
53
+ validatePin: () => Promise<string>;
54
+ };
55
+ /**
56
+ * Shorthand for the types of permissions that can be requested.
57
+ */
58
+ export type Permission = 'write' | 'read' | 'delete' | 'query' | 'subscribe' | 'configure';
59
+ /**
60
+ * The options for creating a permission request for a given protocol.
61
+ */
62
+ export type ProtocolPermissionOptions = {
63
+ /** The protocol definition for the protocol being requested */
64
+ definition: DwnProtocolDefinition;
65
+ /** The permissions being requested for the protocol */
66
+ permissions: Permission[];
67
+ };
68
+ /**
69
+ * Initiates the wallet connect process. Used when a client wants to obtain
70
+ * a did from a provider.
71
+ */
72
+ declare function initClient({ displayName, connectServerUrl, walletUri, permissionRequests, onWalletUriReady, validatePin, }: WalletConnectClientOptions): Promise<{
73
+ delegateGrants: EnboxConnectResponse['delegateGrants'];
74
+ delegatePortableDid: EnboxConnectResponse['delegatePortableDid'];
75
+ connectedDid: string;
76
+ } | undefined>;
77
+ /**
78
+ * Creates a set of Dwn Permission Scopes to request for a given protocol.
79
+ *
80
+ * If no permissions are provided, the default is to request all relevant record permissions (write, read, delete, query, subscribe).
81
+ * 'configure' is not included by default, as this gives the application a lot of control over the protocol.
82
+ */
83
+ declare function createPermissionRequestForProtocol({ definition, permissions }: ProtocolPermissionOptions): ConnectPermissionRequest;
84
+ export declare const WalletConnect: {
85
+ initClient: typeof initClient;
86
+ createPermissionRequestForProtocol: typeof createPermissionRequestForProtocol;
87
+ };
88
+ export {};
89
+ //# sourceMappingURL=wallet-connect-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wallet-connect-client.d.ts","sourceRoot":"","sources":["../../src/wallet-connect-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,wBAAwB,EAAsB,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACxG,OAAO,KAAK,EAAyB,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAQhF;;;;;;GAMG;AACH,MAAM,MAAM,0BAA0B,GAAG;IACvC,6EAA6E;IAC7E,WAAW,EAAE,MAAM,CAAC;IAEpB,sFAAsF;IACtF,gBAAgB,EAAE,MAAM,CAAC;IAEzB;;;;OAIG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;;;OAIG;IACH,kBAAkB,EAAE,wBAAwB,EAAE,CAAC;IAE/C;;;;;OAKG;IACH,gBAAgB,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAExC;;;;;OAKG;IACH,WAAW,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;CACpC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,OAAO,GAAG,MAAM,GAAG,QAAQ,GAAG,OAAO,GAAG,WAAW,GAAG,WAAW,CAAC;AAE3F;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG;IACtC,+DAA+D;IAC/D,UAAU,EAAE,qBAAqB,CAAC;IAElC,uDAAuD;IACvD,WAAW,EAAE,UAAU,EAAE,CAAC;CAC3B,CAAC;AAEF;;;GAGG;AACH,iBAAe,UAAU,CAAC,EACxB,WAAW,EACX,gBAAgB,EAChB,SAAS,EACT,kBAAkB,EAClB,gBAAgB,EAChB,WAAW,GACZ,EAAE,0BAA0B,GAAG,OAAO,CAAC;IACtC,cAAc,EAAE,oBAAoB,CAAC,gBAAgB,CAAC,CAAC;IACvD,mBAAmB,EAAE,oBAAoB,CAAC,qBAAqB,CAAC,CAAC;IACjE,YAAY,EAAE,MAAM,CAAC;CACtB,GAAG,SAAS,CAAC,CAkGb;AAED;;;;;GAKG;AACH,iBAAS,kCAAkC,CAAC,EAAE,UAAU,EAAE,WAAW,EAAE,EAAE,yBAAyB,GAAG,wBAAwB,CAsE5H;AAED,eAAO,MAAM,aAAa;;;CAAqD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enbox/auth",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Headless authentication and identity management SDK for Enbox",
5
5
  "type": "module",
6
6
  "main": "./dist/esm/index.js",
@@ -56,15 +56,19 @@
56
56
  "bun": ">=1.0.0"
57
57
  },
58
58
  "dependencies": {
59
- "@enbox/agent": "0.4.0",
60
- "@enbox/common": "0.0.7",
61
- "@enbox/dids": "0.0.9",
62
- "@enbox/dwn-clients": "0.1.0",
59
+ "@enbox/agent": "0.5.0",
60
+ "@enbox/common": "0.1.0",
61
+ "@enbox/crypto": "0.1.0",
62
+ "@enbox/dids": "0.1.0",
63
+ "@enbox/dwn-clients": "0.2.0",
64
+ "@enbox/dwn-sdk-js": "0.2.0",
63
65
  "level": "8.0.1"
64
66
  },
65
67
  "devDependencies": {
66
68
  "@types/node": "22.19.15",
69
+ "@types/sinon": "17.0.3",
67
70
  "bun-types": "1.3.10",
71
+ "sinon": "18.0.0",
68
72
  "typescript": "5.9.3"
69
73
  }
70
74
  }
@@ -6,19 +6,9 @@
6
6
  * @module
7
7
  */
8
8
 
9
- import { EnboxUserAgent } from '@enbox/agent';
10
- import type { BearerIdentity, PortableIdentity } from '@enbox/agent';
11
-
12
- import { AuthEventEmitter } from './events.js';
13
- import { AuthSession } from './identity-session.js';
14
- import { createDefaultStorage } from './storage/storage.js';
15
- import { discoverLocalDwn } from './flows/dwn-discovery.js';
16
- import { localConnect } from './flows/local-connect.js';
17
- import { restoreSession } from './flows/session-restore.js';
18
- import { STORAGE_KEYS } from './types.js';
19
- import { VaultManager } from './vault/vault-manager.js';
20
- import { walletConnect } from './flows/wallet-connect.js';
9
+ import type { BearerIdentity, HdIdentityVault, PortableIdentity } from '@enbox/agent';
21
10
 
11
+ import type { FlowContext } from './connect/lifecycle.js';
22
12
  import type { PasswordProvider } from './password-provider.js';
23
13
  import type {
24
14
  AuthEvent,
@@ -38,7 +28,19 @@ import type {
38
28
  SyncOption,
39
29
  WalletConnectOptions,
40
30
  } from './types.js';
41
- import { importFromPhrase, importFromPortable } from './flows/import-identity.js';
31
+
32
+ import { EnboxUserAgent } from '@enbox/agent';
33
+
34
+ import { AuthEventEmitter } from './events.js';
35
+ import { AuthSession } from './identity-session.js';
36
+ import { createDefaultStorage } from './storage/storage.js';
37
+ import { discoverLocalDwn } from './discovery.js';
38
+ import { localConnect } from './connect/local.js';
39
+ import { restoreSession } from './connect/restore.js';
40
+ import { STORAGE_KEYS } from './types.js';
41
+ import { walletConnect } from './connect/wallet.js';
42
+ import { ensureVaultReady, resolveIdentityDids, startSyncIfEnabled } from './connect/lifecycle.js';
43
+ import { importFromPhrase, importFromPortable } from './connect/import.js';
42
44
 
43
45
  /**
44
46
  * The primary entry point for authentication and identity management.
@@ -77,7 +79,6 @@ export class AuthManager {
77
79
  private _userAgent: EnboxUserAgent;
78
80
  private _emitter: AuthEventEmitter;
79
81
  private _storage: StorageAdapter;
80
- private _vault: VaultManager;
81
82
  private _session: AuthSession | undefined;
82
83
  private _state: AuthState = 'uninitialized';
83
84
  private _isConnecting = false;
@@ -102,7 +103,6 @@ export class AuthManager {
102
103
  userAgent: EnboxUserAgent;
103
104
  emitter: AuthEventEmitter;
104
105
  storage: StorageAdapter;
105
- vault: VaultManager;
106
106
  defaultPassword?: string;
107
107
  passwordProvider?: PasswordProvider;
108
108
  defaultSync?: SyncOption;
@@ -113,7 +113,6 @@ export class AuthManager {
113
113
  this._userAgent = params.userAgent;
114
114
  this._emitter = params.emitter;
115
115
  this._storage = params.storage;
116
- this._vault = params.vault;
117
116
  this._defaultPassword = params.defaultPassword;
118
117
  this._passwordProvider = params.passwordProvider;
119
118
  this._defaultSync = params.defaultSync;
@@ -159,13 +158,10 @@ export class AuthManager {
159
158
  localDwnEndpoint,
160
159
  });
161
160
 
162
- const vault = new VaultManager(userAgent.vault, emitter);
163
-
164
161
  const manager = new AuthManager({
165
162
  userAgent,
166
163
  emitter,
167
164
  storage,
168
- vault,
169
165
  defaultPassword : options.password,
170
166
  passwordProvider : options.passwordProvider,
171
167
  defaultSync : options.sync,
@@ -175,8 +171,8 @@ export class AuthManager {
175
171
  });
176
172
 
177
173
  // Determine initial state.
178
- if (await vault.isInitialized()) {
179
- manager._setState(vault.isLocked ? 'locked' : 'unlocked');
174
+ if (await userAgent.vault.isInitialized()) {
175
+ manager._setState(userAgent.vault.isLocked() ? 'locked' : 'unlocked');
180
176
  } else {
181
177
  manager._setState('uninitialized');
182
178
  }
@@ -197,30 +193,7 @@ export class AuthManager {
197
193
  * @throws If a connection attempt is already in progress.
198
194
  */
199
195
  async connect(options?: LocalConnectOptions): Promise<AuthSession> {
200
- this._guardConcurrency();
201
- this._isConnecting = true;
202
-
203
- try {
204
- const session = await localConnect(
205
- {
206
- userAgent : this._userAgent,
207
- emitter : this._emitter,
208
- storage : this._storage,
209
- defaultPassword : this._defaultPassword,
210
- passwordProvider : this._passwordProvider,
211
- defaultSync : this._defaultSync,
212
- defaultDwnEndpoints : this._defaultDwnEndpoints,
213
- registration : this._registration,
214
- },
215
- options,
216
- );
217
-
218
- this._session = session;
219
- this._setState('connected');
220
- return session;
221
- } finally {
222
- this._isConnecting = false;
223
- }
196
+ return this._withConnect(() => localConnect(this._flowContext(), options));
224
197
  }
225
198
 
226
199
  /**
@@ -235,50 +208,7 @@ export class AuthManager {
235
208
  * @throws If a connection attempt is already in progress.
236
209
  */
237
210
  async walletConnect(options: WalletConnectOptions): Promise<AuthSession> {
238
- this._guardConcurrency();
239
- this._isConnecting = true;
240
-
241
- try {
242
- // Ensure the agent is initialized and started before wallet connect.
243
- const isFirstLaunch = await this._userAgent.firstLaunch();
244
- let password = this._defaultPassword;
245
-
246
- if (!password && this._passwordProvider) {
247
- try {
248
- password = await this._passwordProvider.getPassword({
249
- reason: isFirstLaunch ? 'create' : 'unlock',
250
- });
251
- } catch {
252
- // Provider failed — fall through to insecure default.
253
- }
254
- }
255
-
256
- password ??= 'insecure-static-phrase';
257
-
258
- if (isFirstLaunch) {
259
- await this._userAgent.initialize({ password });
260
- }
261
- await this._userAgent.start({ password });
262
- this._emitter.emit('vault-unlocked', {});
263
-
264
- const session = await walletConnect(
265
- {
266
- userAgent : this._userAgent,
267
- emitter : this._emitter,
268
- storage : this._storage,
269
- defaultSync : this._defaultSync,
270
- defaultDwnEndpoints : this._defaultDwnEndpoints,
271
- registration : this._registration,
272
- },
273
- options,
274
- );
275
-
276
- this._session = session;
277
- this._setState('connected');
278
- return session;
279
- } finally {
280
- this._isConnecting = false;
281
- }
211
+ return this._withConnect(() => walletConnect(this._flowContext(), options));
282
212
  }
283
213
 
284
214
  /**
@@ -288,28 +218,7 @@ export class AuthManager {
288
218
  * recovering the identity on this device.
289
219
  */
290
220
  async importFromPhrase(options: ImportFromPhraseOptions): Promise<AuthSession> {
291
- this._guardConcurrency();
292
- this._isConnecting = true;
293
-
294
- try {
295
- const session = await importFromPhrase(
296
- {
297
- userAgent : this._userAgent,
298
- emitter : this._emitter,
299
- storage : this._storage,
300
- defaultSync : this._defaultSync,
301
- defaultDwnEndpoints : this._defaultDwnEndpoints,
302
- registration : this._registration,
303
- },
304
- options,
305
- );
306
-
307
- this._session = session;
308
- this._setState('connected');
309
- return session;
310
- } finally {
311
- this._isConnecting = false;
312
- }
221
+ return this._withConnect(() => importFromPhrase(this._flowContext(), options));
313
222
  }
314
223
 
315
224
  /**
@@ -318,28 +227,7 @@ export class AuthManager {
318
227
  * The portable identity contains the DID's private keys and metadata.
319
228
  */
320
229
  async importFromPortable(options: ImportFromPortableOptions): Promise<AuthSession> {
321
- this._guardConcurrency();
322
- this._isConnecting = true;
323
-
324
- try {
325
- const session = await importFromPortable(
326
- {
327
- userAgent : this._userAgent,
328
- emitter : this._emitter,
329
- storage : this._storage,
330
- defaultSync : this._defaultSync,
331
- defaultDwnEndpoints : this._defaultDwnEndpoints,
332
- registration : this._registration,
333
- },
334
- options,
335
- );
336
-
337
- this._session = session;
338
- this._setState('connected');
339
- return session;
340
- } finally {
341
- this._isConnecting = false;
342
- }
230
+ return this._withConnect(() => importFromPortable(this._flowContext(), options));
343
231
  }
344
232
 
345
233
  /**
@@ -353,17 +241,7 @@ export class AuthManager {
353
241
  this._isConnecting = true;
354
242
 
355
243
  try {
356
- const session = await restoreSession(
357
- {
358
- userAgent : this._userAgent,
359
- emitter : this._emitter,
360
- storage : this._storage,
361
- defaultPassword : this._defaultPassword,
362
- passwordProvider : this._passwordProvider,
363
- defaultSync : this._defaultSync,
364
- },
365
- options,
366
- );
244
+ const session = await restoreSession(this._flowContext(), options);
367
245
 
368
246
  if (session) {
369
247
  this._session = session;
@@ -400,10 +278,10 @@ export class AuthManager {
400
278
  */
401
279
  async connectHeadless(options?: HeadlessConnectOptions): Promise<AuthSession> {
402
280
  let password = options?.password ?? this._defaultPassword;
281
+ const isFirstLaunch = await this._userAgent.firstLaunch();
403
282
 
404
283
  // Try the password provider if no explicit password.
405
284
  if (!password && this._passwordProvider) {
406
- const isFirstLaunch = await this._userAgent.firstLaunch();
407
285
  password = await this._passwordProvider.getPassword({
408
286
  reason: isFirstLaunch ? 'create' : 'unlock',
409
287
  });
@@ -416,13 +294,13 @@ export class AuthManager {
416
294
  );
417
295
  }
418
296
 
419
- // Unlock the vault (initialise on first launch).
420
- if (await this._userAgent.firstLaunch()) {
421
- await this._userAgent.initialize({ password });
422
- } else {
423
- await this._userAgent.start({ password });
424
- }
425
- this._emitter.emit('vault-unlocked', {});
297
+ // Unlock the vault (initialise on first launch, always start).
298
+ await ensureVaultReady({
299
+ userAgent : this._userAgent,
300
+ emitter : this._emitter,
301
+ password,
302
+ isFirstLaunch,
303
+ });
426
304
 
427
305
  // Find the active identity.
428
306
  const identities = await this._userAgent.identity.list();
@@ -437,8 +315,7 @@ export class AuthManager {
437
315
  : undefined
438
316
  ) ?? identities[0];
439
317
 
440
- const connectedDid = identity.metadata.connectedDid ?? identity.did.uri;
441
- const delegateDid = identity.metadata.connectedDid ? identity.did.uri : undefined;
318
+ const { connectedDid, delegateDid } = resolveIdentityDids(identity);
442
319
 
443
320
  const identityInfo: IdentityInfo = {
444
321
  didUri : connectedDid,
@@ -484,15 +361,14 @@ export class AuthManager {
484
361
  const did = this._session?.did;
485
362
 
486
363
  // 1. Stop sync.
487
- if ('sync' in this._userAgent && typeof (this._userAgent as any).sync?.stopSync === 'function') {
488
- await (this._userAgent as any).sync.stopSync(timeout);
489
- }
364
+ await this._userAgent.sync.stopSync(timeout);
490
365
 
491
366
  // 2. Clear the session (but keep storage markers for restore).
492
367
  this._session = undefined;
493
368
 
494
- // 3. Lock the vault (also emits 'vault-locked').
495
- await this._vault.lock();
369
+ // 3. Lock the vault.
370
+ await this._userAgent.vault.lock();
371
+ this._emitter.emit('vault-locked', {});
496
372
 
497
373
  // 4. Transition state.
498
374
  this._setState('locked');
@@ -517,9 +393,7 @@ export class AuthManager {
517
393
 
518
394
  // Stop sync.
519
395
  if (this._session) {
520
- if ('sync' in this._userAgent && typeof (this._userAgent as any).sync?.stopSync === 'function') {
521
- await (this._userAgent as any).sync.stopSync(timeout);
522
- }
396
+ await this._userAgent.sync.stopSync(timeout);
523
397
  }
524
398
 
525
399
  this._session = undefined;
@@ -591,33 +465,28 @@ export class AuthManager {
591
465
  const did = this._session?.did;
592
466
 
593
467
  // 1. Stop sync.
594
- if ('sync' in this._userAgent &&
595
- typeof (this._userAgent as any).sync?.stopSync === 'function') {
596
- try {
597
- await (this._userAgent as any).sync.stopSync(timeout);
598
- } catch {
599
- // Best-effort — don't block shutdown on sync errors.
600
- }
468
+ try {
469
+ await this._userAgent.sync.stopSync(timeout);
470
+ } catch {
471
+ // Best-effort don't block shutdown on sync errors.
601
472
  }
602
473
 
603
474
  // 2. Clear the active session.
604
475
  this._session = undefined;
605
476
 
606
- // 3. Lock the vault (emits 'vault-locked').
477
+ // 3. Lock the vault.
607
478
  try {
608
- await this._vault.lock();
479
+ await this._userAgent.vault.lock();
480
+ this._emitter.emit('vault-locked', {});
609
481
  } catch {
610
482
  // Vault may already be locked or uninitialised — safe to ignore.
611
483
  }
612
484
 
613
485
  // 4. Close the sync engine (releases LevelDB handles, timers).
614
- if ('sync' in this._userAgent &&
615
- typeof (this._userAgent as any).sync?.close === 'function') {
616
- try {
617
- await (this._userAgent as any).sync.close();
618
- } catch {
619
- // Best-effort.
620
- }
486
+ try {
487
+ await this._userAgent.sync.close();
488
+ } catch {
489
+ // Best-effort.
621
490
  }
622
491
 
623
492
  // 5. Close the storage adapter (e.g. LevelDB session store).
@@ -673,8 +542,7 @@ export class AuthManager {
673
542
  throw new Error(`[@enbox/auth] Identity not found: ${didUri}`);
674
543
  }
675
544
 
676
- const connectedDid = identity.metadata.connectedDid ?? identity.did.uri;
677
- const delegateDid = identity.metadata.connectedDid ? identity.did.uri : undefined;
545
+ const { connectedDid, delegateDid } = resolveIdentityDids(identity);
678
546
 
679
547
  // Persist the switch.
680
548
  await this._storage.set(STORAGE_KEYS.PREVIOUSLY_CONNECTED, 'true');
@@ -699,10 +567,7 @@ export class AuthManager {
699
567
  // Already registered — safe to ignore.
700
568
  }
701
569
 
702
- const syncMode = sync === undefined ? 'live' : 'poll';
703
- const syncInterval = sync ?? (syncMode === 'live' ? '5m' : '2m');
704
- this._userAgent.sync.startSync({ mode: syncMode, interval: syncInterval })
705
- .catch((err: unknown) => console.error('[@enbox/auth] Sync failed:', err));
570
+ startSyncIfEnabled(this._userAgent, sync);
706
571
  }
707
572
 
708
573
  this._session = new AuthSession({
@@ -768,9 +633,9 @@ export class AuthManager {
768
633
 
769
634
  // ─── Vault ─────────────────────────────────────────────────────
770
635
 
771
- /** Access the vault manager for lock/unlock/backup operations. */
772
- get vault(): VaultManager {
773
- return this._vault;
636
+ /** Access the underlying identity vault for lock/unlock/backup operations. */
637
+ get vault(): HdIdentityVault {
638
+ return this._userAgent.vault;
774
639
  }
775
640
 
776
641
  // ─── Events ────────────────────────────────────────────────────
@@ -800,7 +665,7 @@ export class AuthManager {
800
665
 
801
666
  /** Whether the vault is currently locked. */
802
667
  get isLocked(): boolean {
803
- return this._vault.isLocked;
668
+ return this._userAgent.vault.isLocked();
804
669
  }
805
670
 
806
671
  /** Whether a connection attempt is in progress. */
@@ -826,6 +691,47 @@ export class AuthManager {
826
691
 
827
692
  // ─── Private helpers ───────────────────────────────────────────
828
693
 
694
+ /**
695
+ * Build a `FlowContext` from the manager's current state.
696
+ *
697
+ * Replaces the 5 manual inline context constructions that were
698
+ * previously duplicated across `connect()`, `walletConnect()`,
699
+ * `importFromPhrase()`, `importFromPortable()`, and `restoreSession()`.
700
+ */
701
+ private _flowContext(): FlowContext {
702
+ return {
703
+ userAgent : this._userAgent,
704
+ emitter : this._emitter,
705
+ storage : this._storage,
706
+ defaultPassword : this._defaultPassword,
707
+ passwordProvider : this._passwordProvider,
708
+ defaultSync : this._defaultSync,
709
+ defaultDwnEndpoints : this._defaultDwnEndpoints,
710
+ registration : this._registration,
711
+ };
712
+ }
713
+
714
+ /**
715
+ * Template for connection flows that follow the guard → try/finally → setState pattern.
716
+ *
717
+ * Consolidates the duplicated concurrency guard, `_isConnecting` flag management,
718
+ * session assignment, and state transition across `connect()`, `walletConnect()`,
719
+ * `importFromPhrase()`, and `importFromPortable()`.
720
+ */
721
+ private async _withConnect(fn: () => Promise<AuthSession>): Promise<AuthSession> {
722
+ this._guardConcurrency();
723
+ this._isConnecting = true;
724
+
725
+ try {
726
+ const session = await fn();
727
+ this._session = session;
728
+ this._setState('connected');
729
+ return session;
730
+ } finally {
731
+ this._isConnecting = false;
732
+ }
733
+ }
734
+
829
735
  private _setState(state: AuthState): void {
830
736
  if (state === this._state) {return;}
831
737
  const previous = this._state;