@enbox/auth 0.5.0 → 0.6.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 (90) hide show
  1. package/dist/esm/auth-manager.js +240 -171
  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 +378 -0
  6. package/dist/esm/connect/lifecycle.js.map +1 -0
  7. package/dist/esm/connect/local.js +105 -0
  8. package/dist/esm/connect/local.js.map +1 -0
  9. package/dist/esm/connect/restore.js +117 -0
  10. package/dist/esm/connect/restore.js.map +1 -0
  11. package/dist/esm/connect/wallet.js +80 -0
  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 +13 -19
  16. package/dist/esm/index.js.map +1 -1
  17. package/dist/esm/permissions.js +41 -0
  18. package/dist/esm/permissions.js.map +1 -0
  19. package/dist/esm/{flows/dwn-registration.js → registration.js} +2 -2
  20. package/dist/esm/registration.js.map +1 -0
  21. package/dist/esm/types.js +4 -0
  22. package/dist/esm/types.js.map +1 -1
  23. package/dist/esm/wallet-connect-client.js +188 -0
  24. package/dist/esm/wallet-connect-client.js.map +1 -0
  25. package/dist/types/auth-manager.d.ts +89 -11
  26. package/dist/types/auth-manager.d.ts.map +1 -1
  27. package/dist/types/connect/import.d.ts +25 -0
  28. package/dist/types/connect/import.d.ts.map +1 -0
  29. package/dist/types/connect/lifecycle.d.ts +199 -0
  30. package/dist/types/connect/lifecycle.d.ts.map +1 -0
  31. package/dist/types/connect/local.d.ts +23 -0
  32. package/dist/types/connect/local.d.ts.map +1 -0
  33. package/dist/types/connect/restore.d.ts +18 -0
  34. package/dist/types/connect/restore.d.ts.map +1 -0
  35. package/dist/types/connect/wallet.d.ts +21 -0
  36. package/dist/types/connect/wallet.d.ts.map +1 -0
  37. package/dist/types/{flows/dwn-discovery.d.ts → discovery.d.ts} +3 -3
  38. package/dist/types/discovery.d.ts.map +1 -0
  39. package/dist/types/index.d.ts +14 -19
  40. package/dist/types/index.d.ts.map +1 -1
  41. package/dist/types/permissions.d.ts +18 -0
  42. package/dist/types/permissions.d.ts.map +1 -0
  43. package/dist/types/{flows/dwn-registration.d.ts → registration.d.ts} +2 -2
  44. package/dist/types/registration.d.ts.map +1 -0
  45. package/dist/types/types.d.ts +154 -4
  46. package/dist/types/types.d.ts.map +1 -1
  47. package/dist/types/wallet-connect-client.d.ts +86 -0
  48. package/dist/types/wallet-connect-client.d.ts.map +1 -0
  49. package/package.json +9 -5
  50. package/src/auth-manager.ts +258 -191
  51. package/src/connect/import.ts +148 -0
  52. package/src/connect/lifecycle.ts +487 -0
  53. package/src/connect/local.ts +116 -0
  54. package/src/connect/restore.ts +133 -0
  55. package/src/connect/wallet.ts +89 -0
  56. package/src/{flows/dwn-discovery.ts → discovery.ts} +4 -3
  57. package/src/index.ts +20 -19
  58. package/src/permissions.ts +48 -0
  59. package/src/{flows/dwn-registration.ts → registration.ts} +2 -2
  60. package/src/types.ts +171 -4
  61. package/src/wallet-connect-client.ts +275 -0
  62. package/dist/esm/flows/dwn-discovery.js.map +0 -1
  63. package/dist/esm/flows/dwn-registration.js.map +0 -1
  64. package/dist/esm/flows/import-identity.js +0 -177
  65. package/dist/esm/flows/import-identity.js.map +0 -1
  66. package/dist/esm/flows/local-connect.js +0 -158
  67. package/dist/esm/flows/local-connect.js.map +0 -1
  68. package/dist/esm/flows/session-restore.js +0 -125
  69. package/dist/esm/flows/session-restore.js.map +0 -1
  70. package/dist/esm/flows/wallet-connect.js +0 -200
  71. package/dist/esm/flows/wallet-connect.js.map +0 -1
  72. package/dist/esm/vault/vault-manager.js +0 -95
  73. package/dist/esm/vault/vault-manager.js.map +0 -1
  74. package/dist/types/flows/dwn-discovery.d.ts.map +0 -1
  75. package/dist/types/flows/dwn-registration.d.ts.map +0 -1
  76. package/dist/types/flows/import-identity.d.ts +0 -35
  77. package/dist/types/flows/import-identity.d.ts.map +0 -1
  78. package/dist/types/flows/local-connect.d.ts +0 -31
  79. package/dist/types/flows/local-connect.d.ts.map +0 -1
  80. package/dist/types/flows/session-restore.d.ts +0 -29
  81. package/dist/types/flows/session-restore.d.ts.map +0 -1
  82. package/dist/types/flows/wallet-connect.d.ts +0 -44
  83. package/dist/types/flows/wallet-connect.d.ts.map +0 -1
  84. package/dist/types/vault/vault-manager.d.ts +0 -57
  85. package/dist/types/vault/vault-manager.d.ts.map +0 -1
  86. package/src/flows/import-identity.ts +0 -219
  87. package/src/flows/local-connect.ts +0 -192
  88. package/src/flows/session-restore.ts +0 -155
  89. package/src/flows/wallet-connect.ts +0 -226
  90. package/src/vault/vault-manager.ts +0 -89
@@ -1,35 +0,0 @@
1
- /**
2
- * Identity import flows.
3
- *
4
- * - Import from BIP-39 recovery phrase (re-derive vault + identity).
5
- * - Import from PortableIdentity JSON.
6
- * @module
7
- */
8
- import type { EnboxUserAgent } from '@enbox/agent';
9
- import type { AuthEventEmitter } from '../events.js';
10
- import { AuthSession } from '../identity-session.js';
11
- import type { ImportFromPhraseOptions, ImportFromPortableOptions, RegistrationOptions, StorageAdapter, SyncOption } from '../types.js';
12
- /** @internal */
13
- export interface ImportContext {
14
- userAgent: EnboxUserAgent;
15
- emitter: AuthEventEmitter;
16
- storage: StorageAdapter;
17
- defaultSync?: SyncOption;
18
- defaultDwnEndpoints?: string[];
19
- registration?: RegistrationOptions;
20
- }
21
- /**
22
- * Import (or recover) an identity from a BIP-39 recovery phrase.
23
- *
24
- * This re-initializes the vault with the given phrase and password,
25
- * recovering the agent DID and all derived keys.
26
- */
27
- export declare function importFromPhrase(ctx: ImportContext, options: ImportFromPhraseOptions): Promise<AuthSession>;
28
- /**
29
- * Import an identity from a PortableIdentity JSON object.
30
- *
31
- * The portable identity contains the DID's private keys and metadata,
32
- * allowing it to be used on this device.
33
- */
34
- export declare function importFromPortable(ctx: ImportContext, options: ImportFromPortableOptions): Promise<AuthSession>;
35
- //# sourceMappingURL=import-identity.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"import-identity.d.ts","sourceRoot":"","sources":["../../../src/flows/import-identity.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEnD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAGrD,OAAO,KAAK,EACV,uBAAuB,EACvB,yBAAyB,EACzB,mBAAmB,EACnB,cAAc,EACd,UAAU,EACX,MAAM,aAAa,CAAC;AAErB,gBAAgB;AAChB,MAAM,WAAW,aAAa;IAC5B,SAAS,EAAE,cAAc,CAAC;IAC1B,OAAO,EAAE,gBAAgB,CAAC;IAC1B,OAAO,EAAE,cAAc,CAAC;IACxB,WAAW,CAAC,EAAE,UAAU,CAAC;IACzB,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC/B,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC;AAED;;;;;GAKG;AACH,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,aAAa,EAClB,OAAO,EAAE,uBAAuB,GAC/B,OAAO,CAAC,WAAW,CAAC,CAwGtB;AAED;;;;;GAKG;AACH,wBAAsB,kBAAkB,CACtC,GAAG,EAAE,aAAa,EAClB,OAAO,EAAE,yBAAyB,GACjC,OAAO,CAAC,WAAW,CAAC,CA8DtB"}
@@ -1,31 +0,0 @@
1
- /**
2
- * Local DID connect flow.
3
- *
4
- * Creates or reconnects a local identity with vault-protected keys.
5
- * This replaces the "Mode D/E" paths in Enbox.connect().
6
- * @module
7
- */
8
- import type { EnboxUserAgent } from '@enbox/agent';
9
- import type { AuthEventEmitter } from '../events.js';
10
- import type { PasswordProvider } from '../password-provider.js';
11
- import type { LocalConnectOptions, RegistrationOptions, StorageAdapter, SyncOption } from '../types.js';
12
- import { AuthSession } from '../identity-session.js';
13
- /** @internal */
14
- export interface LocalConnectContext {
15
- userAgent: EnboxUserAgent;
16
- emitter: AuthEventEmitter;
17
- storage: StorageAdapter;
18
- defaultPassword?: string;
19
- passwordProvider?: PasswordProvider;
20
- defaultSync?: SyncOption;
21
- defaultDwnEndpoints?: string[];
22
- registration?: RegistrationOptions;
23
- }
24
- /**
25
- * Execute the local connect flow.
26
- *
27
- * - On first launch: initializes the vault, creates a new DID, returns recovery phrase.
28
- * - On subsequent launches: unlocks the vault and reconnects to the existing identity.
29
- */
30
- export declare function localConnect(ctx: LocalConnectContext, options?: LocalConnectOptions): Promise<AuthSession>;
31
- //# sourceMappingURL=local-connect.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"local-connect.d.ts","sourceRoot":"","sources":["../../../src/flows/local-connect.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEnD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAChE,OAAO,KAAK,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGxG,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAIrD,gBAAgB;AAChB,MAAM,WAAW,mBAAmB;IAClC,SAAS,EAAE,cAAc,CAAC;IAC1B,OAAO,EAAE,gBAAgB,CAAC;IAC1B,OAAO,EAAE,cAAc,CAAC;IACxB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;IACpC,WAAW,CAAC,EAAE,UAAU,CAAC;IACzB,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC/B,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAChC,GAAG,EAAE,mBAAmB,EACxB,OAAO,GAAE,mBAAwB,GAChC,OAAO,CAAC,WAAW,CAAC,CAuJtB"}
@@ -1,29 +0,0 @@
1
- /**
2
- * Session restore flow.
3
- *
4
- * Restores a previously established session from persisted storage,
5
- * replacing the "previouslyConnected" pattern in apps.
6
- * @module
7
- */
8
- import type { EnboxUserAgent } from '@enbox/agent';
9
- import type { AuthEventEmitter } from '../events.js';
10
- import type { PasswordProvider } from '../password-provider.js';
11
- import type { RestoreSessionOptions, StorageAdapter, SyncOption } from '../types.js';
12
- import { AuthSession } from '../identity-session.js';
13
- /** @internal */
14
- export interface SessionRestoreContext {
15
- userAgent: EnboxUserAgent;
16
- emitter: AuthEventEmitter;
17
- storage: StorageAdapter;
18
- defaultPassword?: string;
19
- passwordProvider?: PasswordProvider;
20
- defaultSync?: SyncOption;
21
- }
22
- /**
23
- * Attempt to restore a previous session.
24
- *
25
- * Returns `undefined` if no previous session exists.
26
- * Returns an `AuthSession` if the session was successfully restored.
27
- */
28
- export declare function restoreSession(ctx: SessionRestoreContext, options?: RestoreSessionOptions): Promise<AuthSession | undefined>;
29
- //# sourceMappingURL=session-restore.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"session-restore.d.ts","sourceRoot":"","sources":["../../../src/flows/session-restore.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEnD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAChE,OAAO,KAAK,EAAE,qBAAqB,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGrF,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAGrD,gBAAgB;AAChB,MAAM,WAAW,qBAAqB;IACpC,SAAS,EAAE,cAAc,CAAC;IAC1B,OAAO,EAAE,gBAAgB,CAAC;IAC1B,OAAO,EAAE,cAAc,CAAC;IACxB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;IACpC,WAAW,CAAC,EAAE,UAAU,CAAC;CAC1B;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,qBAAqB,EAC1B,OAAO,GAAE,qBAA0B,GAClC,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CAqHlC"}
@@ -1,44 +0,0 @@
1
- /**
2
- * Wallet connect (Enbox Connect relay) flow.
3
- *
4
- * Connects to an external wallet via the Enbox Connect relay protocol,
5
- * importing a delegated DID with permission grants.
6
- * This replaces the "Mode B/C" paths in Enbox.connect().
7
- * @module
8
- */
9
- import type { DwnDataEncodedRecordsWriteMessage, EnboxUserAgent } from '@enbox/agent';
10
- import type { AuthEventEmitter } from '../events.js';
11
- import { AuthSession } from '../identity-session.js';
12
- import type { RegistrationOptions, StorageAdapter, SyncOption, WalletConnectOptions } from '../types.js';
13
- /** @internal */
14
- export interface WalletConnectContext {
15
- userAgent: EnboxUserAgent;
16
- emitter: AuthEventEmitter;
17
- storage: StorageAdapter;
18
- defaultSync?: SyncOption;
19
- defaultDwnEndpoints?: string[];
20
- registration?: RegistrationOptions;
21
- }
22
- /**
23
- * Process connected grants by storing them in the local DWN as the owner.
24
- *
25
- * This is the agent-level equivalent of `Enbox.processConnectedGrants()`.
26
- * It stores each grant, signed as owner, and returns the deduplicated
27
- * list of protocol URIs represented by the grants.
28
- *
29
- * @internal
30
- */
31
- export declare function processConnectedGrants(params: {
32
- agent: EnboxUserAgent;
33
- delegateDid: string;
34
- grants: DwnDataEncodedRecordsWriteMessage[];
35
- }): Promise<string[]>;
36
- /**
37
- * Execute the wallet connect flow.
38
- *
39
- * 1. Passes the permission requests directly to `WalletConnect.initClient()`.
40
- * 2. Imports the delegate DID and processes grants.
41
- * 3. Sets up sync and returns an AuthSession.
42
- */
43
- export declare function walletConnect(ctx: WalletConnectContext, options: WalletConnectOptions): Promise<AuthSession>;
44
- //# sourceMappingURL=wallet-connect.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"wallet-connect.d.ts","sourceRoot":"","sources":["../../../src/flows/wallet-connect.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,KAAK,EAAE,iCAAiC,EAAyD,cAAc,EAAE,MAAM,cAAc,CAAC;AAG7I,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAGrD,OAAO,KAAK,EAAE,mBAAmB,EAAE,cAAc,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEzG,gBAAgB;AAChB,MAAM,WAAW,oBAAoB;IACnC,SAAS,EAAE,cAAc,CAAC;IAC1B,OAAO,EAAE,gBAAgB,CAAC;IAC1B,OAAO,EAAE,cAAc,CAAC;IACxB,WAAW,CAAC,EAAE,UAAU,CAAC;IACzB,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC/B,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC;AAED;;;;;;;;GAQG;AACH,wBAAsB,sBAAsB,CAAC,MAAM,EAAE;IACnD,KAAK,EAAE,cAAc,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,iCAAiC,EAAE,CAAC;CAC7C,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAmCpB;AAED;;;;;;GAMG;AACH,wBAAsB,aAAa,CACjC,GAAG,EAAE,oBAAoB,EACzB,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,WAAW,CAAC,CAuItB"}
@@ -1,57 +0,0 @@
1
- /**
2
- * VaultManager 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
@@ -1 +0,0 @@
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"}
@@ -1,219 +0,0 @@
1
- /**
2
- * Identity import flows.
3
- *
4
- * - Import from BIP-39 recovery phrase (re-derive vault + identity).
5
- * - Import from PortableIdentity JSON.
6
- * @module
7
- */
8
-
9
- import type { EnboxUserAgent } from '@enbox/agent';
10
-
11
- import type { AuthEventEmitter } from '../events.js';
12
- import { AuthSession } from '../identity-session.js';
13
- import { registerWithDwnEndpoints } from './dwn-registration.js';
14
- import { STORAGE_KEYS } from '../types.js';
15
- import type {
16
- ImportFromPhraseOptions,
17
- ImportFromPortableOptions,
18
- RegistrationOptions,
19
- StorageAdapter,
20
- SyncOption,
21
- } from '../types.js';
22
-
23
- /** @internal */
24
- export interface ImportContext {
25
- userAgent: EnboxUserAgent;
26
- emitter: AuthEventEmitter;
27
- storage: StorageAdapter;
28
- defaultSync?: SyncOption;
29
- defaultDwnEndpoints?: string[];
30
- registration?: RegistrationOptions;
31
- }
32
-
33
- /**
34
- * Import (or recover) an identity from a BIP-39 recovery phrase.
35
- *
36
- * This re-initializes the vault with the given phrase and password,
37
- * recovering the agent DID and all derived keys.
38
- */
39
- export async function importFromPhrase(
40
- ctx: ImportContext,
41
- options: ImportFromPhraseOptions,
42
- ): Promise<AuthSession> {
43
- const { userAgent, emitter, storage } = ctx;
44
- const { recoveryPhrase, password } = options;
45
- const sync = options.sync ?? ctx.defaultSync;
46
- const dwnEndpoints = options.dwnEndpoints ?? ctx.defaultDwnEndpoints ?? ['https://enbox-dwn.fly.dev'];
47
-
48
- // Initialize the vault with the recovery phrase.
49
- // This re-derives the same agent DID and CEK from the mnemonic.
50
- if (await userAgent.firstLaunch()) {
51
- await userAgent.initialize({
52
- password,
53
- recoveryPhrase,
54
- dwnEndpoints,
55
- });
56
- }
57
-
58
- await userAgent.start({ password });
59
- emitter.emit('vault-unlocked', {});
60
-
61
- // The recovery phrase re-derives the same agent DID,
62
- // but the user identity might not exist yet — create one if needed.
63
- const identities = await userAgent.identity.list();
64
- let identity = identities[0];
65
- let isNewIdentity = false;
66
-
67
- if (!identity) {
68
- isNewIdentity = true;
69
- identity = await userAgent.identity.create({
70
- didMethod : 'dht',
71
- metadata : { name: 'Default' },
72
- didOptions : {
73
- services: [
74
- {
75
- id : 'dwn',
76
- type : 'DecentralizedWebNode',
77
- serviceEndpoint : dwnEndpoints,
78
- enc : '#enc',
79
- sig : '#sig',
80
- }
81
- ],
82
- verificationMethods: [
83
- {
84
- algorithm : 'Ed25519',
85
- id : 'sig',
86
- purposes : ['assertionMethod', 'authentication'],
87
- },
88
- {
89
- algorithm : 'X25519',
90
- id : 'enc',
91
- purposes : ['keyAgreement'],
92
- },
93
- ],
94
- },
95
- });
96
- }
97
-
98
- const connectedDid = identity.did.uri;
99
-
100
- // Register with DWN endpoints (if registration options are provided).
101
- if (ctx.registration) {
102
- await registerWithDwnEndpoints(
103
- {
104
- userAgent : userAgent,
105
- dwnEndpoints,
106
- agentDid : userAgent.agentDid.uri,
107
- connectedDid,
108
- storage : storage,
109
- },
110
- ctx.registration,
111
- );
112
- }
113
-
114
- // Register and start sync.
115
- if (isNewIdentity && sync !== 'off') {
116
- await userAgent.sync.registerIdentity({ did: connectedDid, options: { protocols: [] } });
117
- }
118
-
119
- if (sync !== 'off') {
120
- const syncMode = sync === undefined ? 'live' : 'poll';
121
- const syncInterval = sync ?? (syncMode === 'live' ? '5m' : '2m');
122
- userAgent.sync.startSync({ mode: syncMode, interval: syncInterval })
123
- .catch((err: unknown) => console.error('[@enbox/auth] Sync failed:', err));
124
- }
125
-
126
- await storage.set(STORAGE_KEYS.PREVIOUSLY_CONNECTED, 'true');
127
- await storage.set(STORAGE_KEYS.ACTIVE_IDENTITY, connectedDid);
128
-
129
- const identityInfo = {
130
- didUri : connectedDid,
131
- name : identity.metadata.name,
132
- };
133
-
134
- const session = new AuthSession({
135
- agent : userAgent,
136
- did : connectedDid,
137
- identity : identityInfo,
138
- });
139
-
140
- emitter.emit('identity-added', { identity: identityInfo });
141
- emitter.emit('session-start', {
142
- session: { did: connectedDid, identity: identityInfo },
143
- });
144
-
145
- return session;
146
- }
147
-
148
- /**
149
- * Import an identity from a PortableIdentity JSON object.
150
- *
151
- * The portable identity contains the DID's private keys and metadata,
152
- * allowing it to be used on this device.
153
- */
154
- export async function importFromPortable(
155
- ctx: ImportContext,
156
- options: ImportFromPortableOptions,
157
- ): Promise<AuthSession> {
158
- const { userAgent, emitter, storage } = ctx;
159
- const sync = options.sync ?? ctx.defaultSync;
160
-
161
- const identity = await userAgent.identity.import({
162
- portableIdentity: options.portableIdentity,
163
- });
164
-
165
- const connectedDid = identity.metadata.connectedDid ?? identity.did.uri;
166
- const delegateDid = identity.metadata.connectedDid ? identity.did.uri : undefined;
167
-
168
- // Register with DWN endpoints (if registration options are provided).
169
- // For portable imports, extract endpoints from the DID document's DWN service.
170
- if (ctx.registration) {
171
- const dwnEndpoints = ctx.defaultDwnEndpoints ?? ['https://enbox-dwn.fly.dev'];
172
- await registerWithDwnEndpoints(
173
- {
174
- userAgent : userAgent,
175
- dwnEndpoints,
176
- agentDid : userAgent.agentDid.uri,
177
- connectedDid,
178
- storage : storage,
179
- },
180
- ctx.registration,
181
- );
182
- }
183
-
184
- // Register and start sync.
185
- if (sync !== 'off') {
186
- await userAgent.sync.registerIdentity({
187
- did : connectedDid,
188
- options : { delegateDid, protocols: [] },
189
- });
190
-
191
- const syncMode = sync === undefined ? 'live' : 'poll';
192
- const syncInterval = sync ?? (syncMode === 'live' ? '5m' : '2m');
193
- userAgent.sync.startSync({ mode: syncMode, interval: syncInterval })
194
- .catch((err: unknown) => console.error('[@enbox/auth] Sync failed:', err));
195
- }
196
-
197
- await storage.set(STORAGE_KEYS.PREVIOUSLY_CONNECTED, 'true');
198
- await storage.set(STORAGE_KEYS.ACTIVE_IDENTITY, connectedDid);
199
-
200
- const identityInfo = {
201
- didUri : connectedDid,
202
- name : identity.metadata.name,
203
- connectedDid : identity.metadata.connectedDid,
204
- };
205
-
206
- const session = new AuthSession({
207
- agent : userAgent,
208
- did : connectedDid,
209
- delegateDid,
210
- identity : identityInfo,
211
- });
212
-
213
- emitter.emit('identity-added', { identity: identityInfo });
214
- emitter.emit('session-start', {
215
- session: { did: connectedDid, delegateDid, identity: identityInfo },
216
- });
217
-
218
- return session;
219
- }
@@ -1,192 +0,0 @@
1
- /**
2
- * Local DID connect flow.
3
- *
4
- * Creates or reconnects a local identity with vault-protected keys.
5
- * This replaces the "Mode D/E" paths in Enbox.connect().
6
- * @module
7
- */
8
-
9
- import type { EnboxUserAgent } from '@enbox/agent';
10
-
11
- import type { AuthEventEmitter } from '../events.js';
12
- import type { PasswordProvider } from '../password-provider.js';
13
- import type { LocalConnectOptions, RegistrationOptions, StorageAdapter, SyncOption } from '../types.js';
14
-
15
- import { applyLocalDwnDiscovery } from './dwn-discovery.js';
16
- import { AuthSession } from '../identity-session.js';
17
- import { registerWithDwnEndpoints } from './dwn-registration.js';
18
- import { INSECURE_DEFAULT_PASSWORD, STORAGE_KEYS } from '../types.js';
19
-
20
- /** @internal */
21
- export interface LocalConnectContext {
22
- userAgent: EnboxUserAgent;
23
- emitter: AuthEventEmitter;
24
- storage: StorageAdapter;
25
- defaultPassword?: string;
26
- passwordProvider?: PasswordProvider;
27
- defaultSync?: SyncOption;
28
- defaultDwnEndpoints?: string[];
29
- registration?: RegistrationOptions;
30
- }
31
-
32
- /**
33
- * Execute the local connect flow.
34
- *
35
- * - On first launch: initializes the vault, creates a new DID, returns recovery phrase.
36
- * - On subsequent launches: unlocks the vault and reconnects to the existing identity.
37
- */
38
- export async function localConnect(
39
- ctx: LocalConnectContext,
40
- options: LocalConnectOptions = {},
41
- ): Promise<AuthSession> {
42
- const { userAgent, emitter, storage } = ctx;
43
-
44
- // Resolve password: explicit option → provider → manager default → insecure fallback.
45
- const isFirstLaunch = await userAgent.firstLaunch();
46
- let password = options.password ?? ctx.defaultPassword;
47
-
48
- if (!password && ctx.passwordProvider) {
49
- try {
50
- password = await ctx.passwordProvider.getPassword({
51
- reason: isFirstLaunch ? 'create' : 'unlock',
52
- });
53
- } catch {
54
- // Provider failed — fall through to insecure default.
55
- }
56
- }
57
-
58
- password ??= INSECURE_DEFAULT_PASSWORD;
59
-
60
- const sync = options.sync ?? ctx.defaultSync;
61
- const dwnEndpoints = options.dwnEndpoints ?? ctx.defaultDwnEndpoints ?? ['https://enbox-dwn.fly.dev'];
62
-
63
- // Warn if using insecure default.
64
- if (password === INSECURE_DEFAULT_PASSWORD) {
65
- console.warn(
66
- '[@enbox/auth] SECURITY WARNING: No password set. Using insecure default. ' +
67
- 'Set a password via AuthManager.create({ password }) or connect({ password }) ' +
68
- 'to protect your identity vault.'
69
- );
70
- }
71
-
72
- let recoveryPhrase: string | undefined;
73
-
74
- // Initialize vault on first launch.
75
- if (isFirstLaunch) {
76
- recoveryPhrase = await userAgent.initialize({
77
- password,
78
- recoveryPhrase: options.recoveryPhrase,
79
- dwnEndpoints,
80
- });
81
- }
82
-
83
- // Start the agent (unlocks vault if locked, sets agentDid).
84
- await userAgent.start({ password });
85
- emitter.emit('vault-unlocked', {});
86
-
87
- // Apply local DWN discovery (browser redirect payload or persisted endpoint).
88
- // In remote mode, discovery already ran before agent creation — skip.
89
- if (!userAgent.dwn.isRemoteMode) {
90
- await applyLocalDwnDiscovery(userAgent, storage, emitter);
91
- }
92
-
93
- // Find or create the user identity.
94
- const identities = await userAgent.identity.list();
95
- let identity = identities[0];
96
- let isNewIdentity = false;
97
-
98
- if (!identity) {
99
- isNewIdentity = true;
100
- identity = await userAgent.identity.create({
101
- didMethod : 'dht',
102
- metadata : { name: options.metadata?.name ?? 'Default' },
103
- didOptions : {
104
- services: [
105
- {
106
- id : 'dwn',
107
- type : 'DecentralizedWebNode',
108
- serviceEndpoint : dwnEndpoints,
109
- enc : '#enc',
110
- sig : '#sig',
111
- }
112
- ],
113
- verificationMethods: [
114
- {
115
- algorithm : 'Ed25519',
116
- id : 'sig',
117
- purposes : ['assertionMethod', 'authentication'],
118
- },
119
- {
120
- algorithm : 'X25519',
121
- id : 'enc',
122
- purposes : ['keyAgreement'],
123
- },
124
- ],
125
- },
126
- });
127
- }
128
-
129
- const connectedDid = identity.metadata.connectedDid ?? identity.did.uri;
130
- const delegateDid = identity.metadata.connectedDid ? identity.did.uri : undefined;
131
-
132
- // Register with DWN endpoints (if registration options are provided).
133
- if (ctx.registration) {
134
- await registerWithDwnEndpoints(
135
- {
136
- userAgent : userAgent,
137
- dwnEndpoints,
138
- agentDid : userAgent.agentDid.uri,
139
- connectedDid,
140
- storage : storage,
141
- },
142
- ctx.registration,
143
- );
144
- }
145
-
146
- // Register sync for new identities.
147
- if (isNewIdentity && sync !== 'off') {
148
- await userAgent.sync.registerIdentity({
149
- did : connectedDid,
150
- options : { delegateDid, protocols: [] },
151
- });
152
- }
153
-
154
- // Start sync.
155
- if (sync !== 'off') {
156
- const syncMode = sync === undefined ? 'live' : 'poll';
157
- const syncInterval = sync ?? (syncMode === 'live' ? '5m' : '2m');
158
- userAgent.sync.startSync({ mode: syncMode, interval: syncInterval })
159
- .catch((error: unknown) => {
160
- console.error('[@enbox/auth] Sync failed:', error);
161
- });
162
- }
163
-
164
- // Persist session info.
165
- await storage.set(STORAGE_KEYS.PREVIOUSLY_CONNECTED, 'true');
166
- await storage.set(STORAGE_KEYS.ACTIVE_IDENTITY, connectedDid);
167
-
168
- const identityInfo = {
169
- didUri : connectedDid,
170
- name : identity.metadata.name,
171
- connectedDid : identity.metadata.connectedDid,
172
- };
173
-
174
- const session = new AuthSession({
175
- agent : userAgent,
176
- did : connectedDid,
177
- delegateDid,
178
- recoveryPhrase,
179
- identity : identityInfo,
180
- });
181
-
182
- emitter.emit('identity-added', { identity: identityInfo });
183
- emitter.emit('session-start', {
184
- session: {
185
- did : session.did,
186
- delegateDid,
187
- identity : identityInfo,
188
- },
189
- });
190
-
191
- return session;
192
- }