@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.
- package/dist/esm/auth-manager.js +240 -171
- package/dist/esm/auth-manager.js.map +1 -1
- package/dist/esm/connect/import.js +131 -0
- package/dist/esm/connect/import.js.map +1 -0
- package/dist/esm/connect/lifecycle.js +378 -0
- package/dist/esm/connect/lifecycle.js.map +1 -0
- package/dist/esm/connect/local.js +105 -0
- package/dist/esm/connect/local.js.map +1 -0
- package/dist/esm/connect/restore.js +117 -0
- package/dist/esm/connect/restore.js.map +1 -0
- package/dist/esm/connect/wallet.js +80 -0
- package/dist/esm/connect/wallet.js.map +1 -0
- package/dist/esm/{flows/dwn-discovery.js → discovery.js} +2 -2
- package/dist/esm/discovery.js.map +1 -0
- package/dist/esm/index.js +13 -19
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/permissions.js +41 -0
- package/dist/esm/permissions.js.map +1 -0
- package/dist/esm/{flows/dwn-registration.js → registration.js} +2 -2
- package/dist/esm/registration.js.map +1 -0
- package/dist/esm/types.js +4 -0
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/wallet-connect-client.js +188 -0
- package/dist/esm/wallet-connect-client.js.map +1 -0
- package/dist/types/auth-manager.d.ts +89 -11
- package/dist/types/auth-manager.d.ts.map +1 -1
- package/dist/types/connect/import.d.ts +25 -0
- package/dist/types/connect/import.d.ts.map +1 -0
- package/dist/types/connect/lifecycle.d.ts +199 -0
- package/dist/types/connect/lifecycle.d.ts.map +1 -0
- package/dist/types/connect/local.d.ts +23 -0
- package/dist/types/connect/local.d.ts.map +1 -0
- package/dist/types/connect/restore.d.ts +18 -0
- package/dist/types/connect/restore.d.ts.map +1 -0
- package/dist/types/connect/wallet.d.ts +21 -0
- package/dist/types/connect/wallet.d.ts.map +1 -0
- package/dist/types/{flows/dwn-discovery.d.ts → discovery.d.ts} +3 -3
- package/dist/types/discovery.d.ts.map +1 -0
- package/dist/types/index.d.ts +14 -19
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/permissions.d.ts +18 -0
- package/dist/types/permissions.d.ts.map +1 -0
- package/dist/types/{flows/dwn-registration.d.ts → registration.d.ts} +2 -2
- package/dist/types/registration.d.ts.map +1 -0
- package/dist/types/types.d.ts +154 -4
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/wallet-connect-client.d.ts +86 -0
- package/dist/types/wallet-connect-client.d.ts.map +1 -0
- package/package.json +9 -5
- package/src/auth-manager.ts +258 -191
- package/src/connect/import.ts +148 -0
- package/src/connect/lifecycle.ts +487 -0
- package/src/connect/local.ts +116 -0
- package/src/connect/restore.ts +133 -0
- package/src/connect/wallet.ts +89 -0
- package/src/{flows/dwn-discovery.ts → discovery.ts} +4 -3
- package/src/index.ts +20 -19
- package/src/permissions.ts +48 -0
- package/src/{flows/dwn-registration.ts → registration.ts} +2 -2
- package/src/types.ts +171 -4
- package/src/wallet-connect-client.ts +275 -0
- package/dist/esm/flows/dwn-discovery.js.map +0 -1
- package/dist/esm/flows/dwn-registration.js.map +0 -1
- package/dist/esm/flows/import-identity.js +0 -177
- package/dist/esm/flows/import-identity.js.map +0 -1
- package/dist/esm/flows/local-connect.js +0 -158
- package/dist/esm/flows/local-connect.js.map +0 -1
- package/dist/esm/flows/session-restore.js +0 -125
- package/dist/esm/flows/session-restore.js.map +0 -1
- package/dist/esm/flows/wallet-connect.js +0 -200
- package/dist/esm/flows/wallet-connect.js.map +0 -1
- package/dist/esm/vault/vault-manager.js +0 -95
- package/dist/esm/vault/vault-manager.js.map +0 -1
- package/dist/types/flows/dwn-discovery.d.ts.map +0 -1
- package/dist/types/flows/dwn-registration.d.ts.map +0 -1
- package/dist/types/flows/import-identity.d.ts +0 -35
- package/dist/types/flows/import-identity.d.ts.map +0 -1
- package/dist/types/flows/local-connect.d.ts +0 -31
- package/dist/types/flows/local-connect.d.ts.map +0 -1
- package/dist/types/flows/session-restore.d.ts +0 -29
- package/dist/types/flows/session-restore.d.ts.map +0 -1
- package/dist/types/flows/wallet-connect.d.ts +0 -44
- package/dist/types/flows/wallet-connect.d.ts.map +0 -1
- package/dist/types/vault/vault-manager.d.ts +0 -57
- package/dist/types/vault/vault-manager.d.ts.map +0 -1
- package/src/flows/import-identity.ts +0 -219
- package/src/flows/local-connect.ts +0 -192
- package/src/flows/session-restore.ts +0 -155
- package/src/flows/wallet-connect.ts +0 -226
- package/src/vault/vault-manager.ts +0 -89
|
@@ -0,0 +1,116 @@
|
|
|
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 { AuthSession } from '../identity-session.js';
|
|
10
|
+
import type { FlowContext } from './lifecycle.js';
|
|
11
|
+
import type { LocalConnectOptions } from '../types.js';
|
|
12
|
+
|
|
13
|
+
import { applyLocalDwnDiscovery } from '../discovery.js';
|
|
14
|
+
import { DEFAULT_DWN_ENDPOINTS } from '../types.js';
|
|
15
|
+
import { registerWithDwnEndpoints } from '../registration.js';
|
|
16
|
+
import { createDefaultIdentity, ensureVaultReady, finalizeSession, resolveIdentityDids, resolvePassword, startSyncIfEnabled } from './lifecycle.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Execute the local connect flow.
|
|
20
|
+
*
|
|
21
|
+
* - On first launch: initializes the vault. Identity creation is opt-in via
|
|
22
|
+
* `options.createIdentity: true`.
|
|
23
|
+
* - On subsequent launches: unlocks the vault and reconnects to the existing identity.
|
|
24
|
+
*
|
|
25
|
+
* When no identities exist and `createIdentity` is not `true`, the session
|
|
26
|
+
* is returned with the **agent DID** as the connected DID. This allows apps to
|
|
27
|
+
* manage identity creation separately from vault setup.
|
|
28
|
+
*/
|
|
29
|
+
export async function localConnect(
|
|
30
|
+
ctx: FlowContext,
|
|
31
|
+
options: LocalConnectOptions = {},
|
|
32
|
+
): Promise<AuthSession> {
|
|
33
|
+
const { userAgent, emitter, storage } = ctx;
|
|
34
|
+
|
|
35
|
+
// Resolve password through the standard chain.
|
|
36
|
+
const isFirstLaunch = await userAgent.firstLaunch();
|
|
37
|
+
const password = await resolvePassword(ctx, options.password, isFirstLaunch);
|
|
38
|
+
|
|
39
|
+
const sync = options.sync ?? ctx.defaultSync;
|
|
40
|
+
const dwnEndpoints = options.dwnEndpoints ?? ctx.defaultDwnEndpoints ?? DEFAULT_DWN_ENDPOINTS;
|
|
41
|
+
const shouldCreateIdentity = options.createIdentity === true;
|
|
42
|
+
|
|
43
|
+
// Initialize vault on first launch and start the agent.
|
|
44
|
+
const recoveryPhrase = await ensureVaultReady({
|
|
45
|
+
userAgent,
|
|
46
|
+
emitter,
|
|
47
|
+
password,
|
|
48
|
+
isFirstLaunch,
|
|
49
|
+
recoveryPhrase: options.recoveryPhrase,
|
|
50
|
+
dwnEndpoints,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Apply local DWN discovery (browser redirect payload or persisted endpoint).
|
|
54
|
+
// In remote mode, discovery already ran before agent creation — skip.
|
|
55
|
+
if (!userAgent.dwn.isRemoteMode) {
|
|
56
|
+
await applyLocalDwnDiscovery(userAgent, storage, emitter);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Find or create the user identity.
|
|
60
|
+
const identities = await userAgent.identity.list();
|
|
61
|
+
let identity = identities[0];
|
|
62
|
+
let isNewIdentity = false;
|
|
63
|
+
|
|
64
|
+
if (!identity && shouldCreateIdentity) {
|
|
65
|
+
isNewIdentity = true;
|
|
66
|
+
identity = await createDefaultIdentity(userAgent, dwnEndpoints, options.metadata?.name ?? 'Default');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// When no identity exists (createIdentity: false on first launch), use the
|
|
70
|
+
// agent DID as the session's connected DID. The session is still valid but
|
|
71
|
+
// operates in the agent's context rather than a user identity's context.
|
|
72
|
+
const connectedDid = identity
|
|
73
|
+
? resolveIdentityDids(identity).connectedDid
|
|
74
|
+
: userAgent.agentDid.uri;
|
|
75
|
+
|
|
76
|
+
const delegateDid = identity
|
|
77
|
+
? resolveIdentityDids(identity).delegateDid
|
|
78
|
+
: undefined;
|
|
79
|
+
|
|
80
|
+
// Register with DWN endpoints (if registration options are provided).
|
|
81
|
+
if (ctx.registration) {
|
|
82
|
+
await registerWithDwnEndpoints(
|
|
83
|
+
{
|
|
84
|
+
userAgent : userAgent,
|
|
85
|
+
dwnEndpoints,
|
|
86
|
+
agentDid : userAgent.agentDid.uri,
|
|
87
|
+
connectedDid,
|
|
88
|
+
storage : storage,
|
|
89
|
+
},
|
|
90
|
+
ctx.registration,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Register sync for new identities.
|
|
95
|
+
if (isNewIdentity && sync !== 'off') {
|
|
96
|
+
await userAgent.sync.registerIdentity({
|
|
97
|
+
did : connectedDid,
|
|
98
|
+
options : { delegateDid, protocols: [] },
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Start sync.
|
|
103
|
+
startSyncIfEnabled(userAgent, sync);
|
|
104
|
+
|
|
105
|
+
// Persist session info, build AuthSession, and emit lifecycle events.
|
|
106
|
+
return finalizeSession({
|
|
107
|
+
userAgent,
|
|
108
|
+
emitter,
|
|
109
|
+
storage,
|
|
110
|
+
connectedDid,
|
|
111
|
+
delegateDid,
|
|
112
|
+
recoveryPhrase,
|
|
113
|
+
identityName : identity?.metadata.name,
|
|
114
|
+
identityConnectedDid : identity?.metadata.connectedDid,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
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
|
+
|
|
9
|
+
import type { AuthSession } from '../identity-session.js';
|
|
10
|
+
import type { FlowContext } from './lifecycle.js';
|
|
11
|
+
import type { RestoreSessionOptions } from '../types.js';
|
|
12
|
+
|
|
13
|
+
import { applyLocalDwnDiscovery } from '../discovery.js';
|
|
14
|
+
import { STORAGE_KEYS } from '../types.js';
|
|
15
|
+
import { ensureVaultReady, finalizeSession, resolveIdentityDids, resolvePassword, startSyncIfEnabled } from './lifecycle.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Attempt to restore a previous session.
|
|
19
|
+
*
|
|
20
|
+
* Returns `undefined` if no previous session exists.
|
|
21
|
+
* Returns an `AuthSession` if the session was successfully restored.
|
|
22
|
+
*/
|
|
23
|
+
export async function restoreSession(
|
|
24
|
+
ctx: FlowContext,
|
|
25
|
+
options: RestoreSessionOptions = {},
|
|
26
|
+
): Promise<AuthSession | undefined> {
|
|
27
|
+
const { userAgent, emitter, storage } = ctx;
|
|
28
|
+
|
|
29
|
+
// Check if there was a previous session.
|
|
30
|
+
const previouslyConnected = await storage.get(STORAGE_KEYS.PREVIOUSLY_CONNECTED);
|
|
31
|
+
if (previouslyConnected !== 'true') {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Resolve password: explicit option → callback → provider → manager default → insecure fallback.
|
|
36
|
+
// Note: restoreSession has an extra `onPasswordRequired` callback that sits between
|
|
37
|
+
// the explicit password and the provider. We handle that here, then delegate the
|
|
38
|
+
// remainder of the chain to `resolvePassword()`.
|
|
39
|
+
let explicitPassword = options.password;
|
|
40
|
+
|
|
41
|
+
if (!explicitPassword && !ctx.defaultPassword && options.onPasswordRequired) {
|
|
42
|
+
explicitPassword = await options.onPasswordRequired();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Check for stale session marker: if the vault was never initialized,
|
|
46
|
+
// previouslyConnected is a leftover — clean up and bail.
|
|
47
|
+
const isFirstLaunch = await userAgent.firstLaunch();
|
|
48
|
+
if (isFirstLaunch) {
|
|
49
|
+
await storage.remove(STORAGE_KEYS.PREVIOUSLY_CONNECTED);
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const password = await resolvePassword(ctx, explicitPassword, false);
|
|
54
|
+
|
|
55
|
+
// Start the agent (vault is known to exist).
|
|
56
|
+
await ensureVaultReady({
|
|
57
|
+
userAgent,
|
|
58
|
+
emitter,
|
|
59
|
+
password,
|
|
60
|
+
isFirstLaunch: false,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Apply local DWN discovery (browser redirect payload or persisted endpoint).
|
|
64
|
+
// In remote mode, discovery already ran before agent creation — skip.
|
|
65
|
+
if (!userAgent.dwn.isRemoteMode) {
|
|
66
|
+
await applyLocalDwnDiscovery(userAgent, storage, emitter);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Determine which identity to reconnect.
|
|
70
|
+
const activeIdentityDid = await storage.get(STORAGE_KEYS.ACTIVE_IDENTITY);
|
|
71
|
+
const storedDelegateDid = await storage.get(STORAGE_KEYS.DELEGATE_DID);
|
|
72
|
+
|
|
73
|
+
// First try the connected identity (wallet-connected sessions).
|
|
74
|
+
let identity = await userAgent.identity.connectedIdentity();
|
|
75
|
+
|
|
76
|
+
if (!identity) {
|
|
77
|
+
// Try to find the specific active identity.
|
|
78
|
+
if (activeIdentityDid) {
|
|
79
|
+
identity = await userAgent.identity.get({ didUri: activeIdentityDid });
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Fall back to the first available identity.
|
|
83
|
+
if (!identity) {
|
|
84
|
+
const identities = await userAgent.identity.list();
|
|
85
|
+
identity = identities[0];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Start sync.
|
|
90
|
+
startSyncIfEnabled(userAgent, ctx.defaultSync);
|
|
91
|
+
|
|
92
|
+
if (!identity) {
|
|
93
|
+
// No identity found — this is valid for agent-only sessions created
|
|
94
|
+
// with `createIdentity: false`. Restore a session using the agent DID.
|
|
95
|
+
// If the active identity stored was the agent DID, this is an
|
|
96
|
+
// intentional agent-only session rather than stale data.
|
|
97
|
+
const isAgentOnlySession = activeIdentityDid === userAgent.agentDid.uri;
|
|
98
|
+
|
|
99
|
+
if (!isAgentOnlySession) {
|
|
100
|
+
// Truly stale session data — clean up and bail.
|
|
101
|
+
await storage.remove(STORAGE_KEYS.PREVIOUSLY_CONNECTED);
|
|
102
|
+
await storage.remove(STORAGE_KEYS.ACTIVE_IDENTITY);
|
|
103
|
+
await storage.remove(STORAGE_KEYS.DELEGATE_DID);
|
|
104
|
+
await storage.remove(STORAGE_KEYS.CONNECTED_DID);
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return finalizeSession({
|
|
109
|
+
userAgent,
|
|
110
|
+
emitter,
|
|
111
|
+
storage,
|
|
112
|
+
connectedDid : userAgent.agentDid.uri,
|
|
113
|
+
emitIdentityAdded : false,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const { connectedDid, delegateDid } = resolveIdentityDids(
|
|
118
|
+
identity, storedDelegateDid ?? undefined,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
// Persist session info, build AuthSession, and emit lifecycle events.
|
|
122
|
+
// Session restore does not emit `identity-added` (identity was already added in the original flow).
|
|
123
|
+
return finalizeSession({
|
|
124
|
+
userAgent,
|
|
125
|
+
emitter,
|
|
126
|
+
storage,
|
|
127
|
+
connectedDid,
|
|
128
|
+
delegateDid,
|
|
129
|
+
identityName : identity.metadata.name,
|
|
130
|
+
identityConnectedDid : identity.metadata.connectedDid,
|
|
131
|
+
emitIdentityAdded : false,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
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
|
+
|
|
10
|
+
import type { AuthSession } from '../identity-session.js';
|
|
11
|
+
import type { FlowContext } from './lifecycle.js';
|
|
12
|
+
import type { WalletConnectOptions } from '../types.js';
|
|
13
|
+
|
|
14
|
+
import { DEFAULT_DWN_ENDPOINTS } from '../types.js';
|
|
15
|
+
import { registerWithDwnEndpoints } from '../registration.js';
|
|
16
|
+
import { WalletConnect } from '../wallet-connect-client.js';
|
|
17
|
+
import { ensureVaultReady, finalizeDelegateSession, importDelegateAndSetupSync, resolvePassword } from './lifecycle.js';
|
|
18
|
+
|
|
19
|
+
// Re-export for backward compatibility — processConnectedGrants moved to lifecycle.ts.
|
|
20
|
+
export { processConnectedGrants } from './lifecycle.js';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Execute the wallet connect flow.
|
|
24
|
+
*
|
|
25
|
+
* 1. Passes the permission requests directly to `WalletConnect.initClient()`.
|
|
26
|
+
* 2. Imports the delegate DID and processes grants.
|
|
27
|
+
* 3. Sets up sync and returns an AuthSession.
|
|
28
|
+
*/
|
|
29
|
+
export async function walletConnect(
|
|
30
|
+
ctx: FlowContext,
|
|
31
|
+
options: WalletConnectOptions,
|
|
32
|
+
): Promise<AuthSession> {
|
|
33
|
+
const { userAgent, emitter, storage } = ctx;
|
|
34
|
+
const sync = options.sync ?? ctx.defaultSync;
|
|
35
|
+
|
|
36
|
+
if (sync === 'off') {
|
|
37
|
+
throw new Error(
|
|
38
|
+
'[@enbox/auth] Sync must be enabled when using wallet connect. ' +
|
|
39
|
+
'Remove sync: "off" or set an interval like "15s".'
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Ensure the agent is initialized and started before the relay flow.
|
|
44
|
+
const isFirstLaunch = await userAgent.firstLaunch();
|
|
45
|
+
const password = await resolvePassword(ctx, undefined, isFirstLaunch);
|
|
46
|
+
await ensureVaultReady({ userAgent, emitter, password, isFirstLaunch });
|
|
47
|
+
|
|
48
|
+
// Run the Enbox Connect relay flow.
|
|
49
|
+
const result = await WalletConnect.initClient({
|
|
50
|
+
displayName : options.displayName,
|
|
51
|
+
connectServerUrl : options.connectServerUrl,
|
|
52
|
+
walletUri : options.walletUri ?? 'enbox://connect',
|
|
53
|
+
permissionRequests : options.permissionRequests,
|
|
54
|
+
onWalletUriReady : options.onWalletUriReady,
|
|
55
|
+
validatePin : options.validatePin,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
if (!result) {
|
|
59
|
+
throw new Error('[@enbox/auth] Wallet connect flow was cancelled or returned no result.');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Import delegate DID, process grants, and set up sync.
|
|
63
|
+
const { delegatePortableDid, connectedDid, delegateGrants } = result;
|
|
64
|
+
const identity = await importDelegateAndSetupSync({
|
|
65
|
+
userAgent, delegatePortableDid, connectedDid, delegateGrants,
|
|
66
|
+
flowName: 'Wallet connect',
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Register with DWN endpoints (if registration options are provided).
|
|
70
|
+
if (ctx.registration) {
|
|
71
|
+
const dwnEndpoints = ctx.defaultDwnEndpoints ?? DEFAULT_DWN_ENDPOINTS;
|
|
72
|
+
await registerWithDwnEndpoints(
|
|
73
|
+
{
|
|
74
|
+
userAgent,
|
|
75
|
+
dwnEndpoints,
|
|
76
|
+
agentDid: userAgent.agentDid.uri,
|
|
77
|
+
connectedDid,
|
|
78
|
+
storage,
|
|
79
|
+
},
|
|
80
|
+
ctx.registration,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Finalize session.
|
|
85
|
+
return finalizeDelegateSession({
|
|
86
|
+
userAgent, emitter, storage, identity,
|
|
87
|
+
connectedDid, delegateDid: delegatePortableDid.uri, sync,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
@@ -30,9 +30,10 @@ import type { EnboxUserAgent } from '@enbox/agent';
|
|
|
30
30
|
import { EnboxRpcClient } from '@enbox/dwn-clients';
|
|
31
31
|
import { buildDwnConnectUrl, localDwnServerName, normalizeBaseUrl, readDwnDiscoveryPayloadFromUrl } from '@enbox/agent';
|
|
32
32
|
|
|
33
|
-
import type { AuthEventEmitter } from '
|
|
34
|
-
import {
|
|
35
|
-
|
|
33
|
+
import type { AuthEventEmitter } from './events.js';
|
|
34
|
+
import type { StorageAdapter } from './types.js';
|
|
35
|
+
|
|
36
|
+
import { STORAGE_KEYS } from './types.js';
|
|
36
37
|
|
|
37
38
|
/**
|
|
38
39
|
* Check the current page URL for a `DwnDiscoveryPayload` in the fragment.
|
package/src/index.ts
CHANGED
|
@@ -5,30 +5,23 @@
|
|
|
5
5
|
* in both browser and CLI environments. Depends only on `@enbox/agent`
|
|
6
6
|
* and can be used standalone or consumed by `@enbox/api`.
|
|
7
7
|
*
|
|
8
|
-
* @example Standalone auth
|
|
8
|
+
* @example Standalone auth (wallet app)
|
|
9
9
|
* ```ts
|
|
10
10
|
* import { AuthManager } from '@enbox/auth';
|
|
11
11
|
*
|
|
12
12
|
* const auth = await AuthManager.create({ sync: '15s' });
|
|
13
|
-
* const session = await auth.
|
|
14
|
-
*
|
|
15
|
-
* // session.agent — the authenticated Enbox agent
|
|
16
|
-
* // session.did — the connected DID URI
|
|
13
|
+
* const session = await auth.connectLocal({ password: userPin });
|
|
17
14
|
* ```
|
|
18
15
|
*
|
|
19
|
-
* @example
|
|
16
|
+
* @example Dapp with browser connect handler
|
|
20
17
|
* ```ts
|
|
21
18
|
* import { AuthManager } from '@enbox/auth';
|
|
22
|
-
* import {
|
|
23
|
-
*
|
|
24
|
-
* const auth = await AuthManager.create({ sync: '15s' });
|
|
25
|
-
* const session = await auth.connect();
|
|
19
|
+
* import { BrowserConnectHandler } from '@enbox/browser';
|
|
26
20
|
*
|
|
27
|
-
* const
|
|
28
|
-
*
|
|
29
|
-
* connectedDid: session.did,
|
|
30
|
-
* delegateDid: session.delegateDid,
|
|
21
|
+
* const auth = await AuthManager.create({
|
|
22
|
+
* connectHandler: BrowserConnectHandler(),
|
|
31
23
|
* });
|
|
24
|
+
* const session = await auth.connect({ protocols: [NotesProtocol] });
|
|
32
25
|
* ```
|
|
33
26
|
*
|
|
34
27
|
* @packageDocumentation
|
|
@@ -37,7 +30,6 @@
|
|
|
37
30
|
// Core classes
|
|
38
31
|
export { AuthManager } from './auth-manager.js';
|
|
39
32
|
export { AuthSession } from './identity-session.js';
|
|
40
|
-
export { VaultManager } from './vault/vault-manager.js';
|
|
41
33
|
export { AuthEventEmitter } from './events.js';
|
|
42
34
|
|
|
43
35
|
// Password providers
|
|
@@ -48,11 +40,14 @@ export type { PasswordContext } from './password-provider.js';
|
|
|
48
40
|
// without a direct @enbox/agent dependency.
|
|
49
41
|
export { EnboxUserAgent, HdIdentityVault } from '@enbox/agent';
|
|
50
42
|
|
|
51
|
-
//
|
|
52
|
-
export { processConnectedGrants } from './
|
|
43
|
+
// Connect helpers
|
|
44
|
+
export { processConnectedGrants } from './connect/wallet.js';
|
|
45
|
+
export { normalizeProtocolRequests } from './permissions.js';
|
|
46
|
+
export { WalletConnect } from './wallet-connect-client.js';
|
|
47
|
+
export type { ProtocolPermissionOptions, WalletConnectClientOptions } from './wallet-connect-client.js';
|
|
53
48
|
|
|
54
49
|
// Registration token storage helpers
|
|
55
|
-
export { loadTokensFromStorage, saveTokensToStorage } from './
|
|
50
|
+
export { loadTokensFromStorage, saveTokensToStorage } from './registration.js';
|
|
56
51
|
|
|
57
52
|
// Local DWN discovery (browser dwn:// protocol integration)
|
|
58
53
|
export {
|
|
@@ -63,7 +58,7 @@ export {
|
|
|
63
58
|
persistLocalDwnEndpoint,
|
|
64
59
|
requestLocalDwnDiscovery,
|
|
65
60
|
restoreLocalDwnEndpoint,
|
|
66
|
-
} from './
|
|
61
|
+
} from './discovery.js';
|
|
67
62
|
|
|
68
63
|
// Storage adapters
|
|
69
64
|
export { BrowserStorage, LevelStorage, MemoryStorage, createDefaultStorage } from './storage/storage.js';
|
|
@@ -76,8 +71,12 @@ export type {
|
|
|
76
71
|
AuthManagerOptions,
|
|
77
72
|
AuthSessionInfo,
|
|
78
73
|
AuthState,
|
|
74
|
+
ConnectHandler,
|
|
75
|
+
ConnectOptions,
|
|
79
76
|
ConnectPermissionRequest,
|
|
77
|
+
ConnectResult,
|
|
80
78
|
DisconnectOptions,
|
|
79
|
+
HandlerConnectOptions,
|
|
81
80
|
HeadlessConnectOptions,
|
|
82
81
|
IdentityInfo,
|
|
83
82
|
IdentityVaultBackup,
|
|
@@ -85,7 +84,9 @@ export type {
|
|
|
85
84
|
ImportFromPortableOptions,
|
|
86
85
|
LocalConnectOptions,
|
|
87
86
|
LocalDwnStrategy,
|
|
87
|
+
Permission,
|
|
88
88
|
PortableIdentity,
|
|
89
|
+
ProtocolRequest,
|
|
89
90
|
ProviderAuthParams,
|
|
90
91
|
ProviderAuthResult,
|
|
91
92
|
RegistrationOptions,
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Permission request normalization utilities.
|
|
3
|
+
*
|
|
4
|
+
* Converts simplified `ProtocolRequest` entries (just a protocol definition
|
|
5
|
+
* or `{ definition, permissions }`) into agent-level `ConnectPermissionRequest`
|
|
6
|
+
* objects used by connect handlers.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { ConnectPermissionRequest, DwnProtocolDefinition } from '@enbox/agent';
|
|
13
|
+
|
|
14
|
+
import type { ProtocolRequest } from './types.js';
|
|
15
|
+
|
|
16
|
+
import { DEFAULT_PERMISSIONS } from './types.js';
|
|
17
|
+
import { WalletConnect } from './wallet-connect-client.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Normalize simplified `ProtocolRequest[]` into agent-level
|
|
21
|
+
* `ConnectPermissionRequest[]`.
|
|
22
|
+
*/
|
|
23
|
+
export function normalizeProtocolRequests(
|
|
24
|
+
protocols: ProtocolRequest[] | undefined,
|
|
25
|
+
): ConnectPermissionRequest[] {
|
|
26
|
+
if (!protocols || protocols.length === 0) { return []; }
|
|
27
|
+
|
|
28
|
+
return protocols.map((entry) => {
|
|
29
|
+
let definition: DwnProtocolDefinition;
|
|
30
|
+
let permissions: string[];
|
|
31
|
+
|
|
32
|
+
if ('protocol' in entry && 'types' in entry && 'structure' in entry) {
|
|
33
|
+
// Bare protocol definition — use default permissions.
|
|
34
|
+
definition = entry as DwnProtocolDefinition;
|
|
35
|
+
permissions = [...DEFAULT_PERMISSIONS];
|
|
36
|
+
} else {
|
|
37
|
+
// Object with explicit permissions.
|
|
38
|
+
const explicit = entry as { definition: DwnProtocolDefinition; permissions: string[] };
|
|
39
|
+
definition = explicit.definition;
|
|
40
|
+
permissions = explicit.permissions;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return WalletConnect.createPermissionRequestForProtocol({
|
|
44
|
+
definition,
|
|
45
|
+
permissions: permissions as Parameters<typeof WalletConnect.createPermissionRequestForProtocol>[0]['permissions'],
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
}
|
|
@@ -15,13 +15,13 @@ import type { EnboxUserAgent } from '@enbox/agent';
|
|
|
15
15
|
|
|
16
16
|
import { DwnRegistrar } from '@enbox/dwn-clients';
|
|
17
17
|
|
|
18
|
-
import { STORAGE_KEYS } from '
|
|
18
|
+
import { STORAGE_KEYS } from './types.js';
|
|
19
19
|
|
|
20
20
|
import type {
|
|
21
21
|
RegistrationOptions,
|
|
22
22
|
RegistrationTokenData,
|
|
23
23
|
StorageAdapter,
|
|
24
|
-
} from '
|
|
24
|
+
} from './types.js';
|
|
25
25
|
|
|
26
26
|
/** @internal */
|
|
27
27
|
export interface RegistrationContext {
|