@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,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WalletConnect client — initiates the relay-mediated 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
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
15
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
16
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
17
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
18
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
19
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
20
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
import { CryptoUtils } from '@enbox/crypto';
|
|
24
|
+
import { DidJwk } from '@enbox/dids';
|
|
25
|
+
import { Convert, logger } from '@enbox/common';
|
|
26
|
+
import { DwnInterfaceName, DwnMethodName } from '@enbox/dwn-sdk-js';
|
|
27
|
+
import { EnboxConnectProtocol, pollWithTtl } from '@enbox/agent';
|
|
28
|
+
/**
|
|
29
|
+
* Initiates the wallet connect process. Used when a client wants to obtain
|
|
30
|
+
* a did from a provider.
|
|
31
|
+
*/
|
|
32
|
+
function initClient(_a) {
|
|
33
|
+
return __awaiter(this, arguments, void 0, function* ({ displayName, connectServerUrl, walletUri, permissionRequests, onWalletUriReady, validatePin, }) {
|
|
34
|
+
// ephemeral client did for ECDH, signing, verification
|
|
35
|
+
const clientDid = yield DidJwk.create();
|
|
36
|
+
// TODO: properly implement PKCE. this implementation is lacking server side validations and more.
|
|
37
|
+
// https://github.com/enboxorg/enbox/issues/829
|
|
38
|
+
// Derive the code challenge based on the code verifier
|
|
39
|
+
// const { codeChallengeBytes, codeChallengeBase64Url } =
|
|
40
|
+
// await Oidc.generateCodeChallenge();
|
|
41
|
+
const encryptionKey = CryptoUtils.randomBytes(32);
|
|
42
|
+
// Build callback URL for the connect request.
|
|
43
|
+
const callbackEndpoint = EnboxConnectProtocol.buildConnectUrl({
|
|
44
|
+
baseURL: connectServerUrl,
|
|
45
|
+
endpoint: 'callback',
|
|
46
|
+
});
|
|
47
|
+
// Build the connect request.
|
|
48
|
+
const request = yield EnboxConnectProtocol.createConnectRequest({
|
|
49
|
+
clientDid: clientDid.uri,
|
|
50
|
+
callbackUrl: callbackEndpoint,
|
|
51
|
+
permissionRequests: permissionRequests,
|
|
52
|
+
appName: displayName,
|
|
53
|
+
});
|
|
54
|
+
// Sign the request as a JWT.
|
|
55
|
+
const requestJwt = yield EnboxConnectProtocol.signJwt({
|
|
56
|
+
did: clientDid,
|
|
57
|
+
data: request,
|
|
58
|
+
});
|
|
59
|
+
if (!requestJwt) {
|
|
60
|
+
throw new Error('Unable to sign requestObject');
|
|
61
|
+
}
|
|
62
|
+
// Encrypt the request JWT with the symmetric key.
|
|
63
|
+
const requestObjectJwe = yield EnboxConnectProtocol.encryptRequest({
|
|
64
|
+
jwt: requestJwt,
|
|
65
|
+
encryptionKey,
|
|
66
|
+
});
|
|
67
|
+
const pushedAuthorizationRequestEndpoint = EnboxConnectProtocol.buildConnectUrl({
|
|
68
|
+
baseURL: connectServerUrl,
|
|
69
|
+
endpoint: 'pushedAuthorizationRequest',
|
|
70
|
+
});
|
|
71
|
+
const parResponse = yield fetch(pushedAuthorizationRequestEndpoint, {
|
|
72
|
+
body: JSON.stringify({ request: requestObjectJwe }),
|
|
73
|
+
method: 'POST',
|
|
74
|
+
headers: {
|
|
75
|
+
'Content-Type': 'application/json',
|
|
76
|
+
},
|
|
77
|
+
signal: AbortSignal.timeout(30000),
|
|
78
|
+
});
|
|
79
|
+
if (!parResponse.ok) {
|
|
80
|
+
throw new Error(`${parResponse.status}: ${parResponse.statusText}`);
|
|
81
|
+
}
|
|
82
|
+
const parData = yield parResponse.json();
|
|
83
|
+
// a deeplink to a compatible wallet. if the wallet scans this link it should receive
|
|
84
|
+
// a route to its Connect provider flow and the params of where to fetch the auth request.
|
|
85
|
+
logger.log(`Wallet URI: ${walletUri}`);
|
|
86
|
+
const generatedWalletUri = new URL(walletUri);
|
|
87
|
+
generatedWalletUri.searchParams.set('request_uri', parData.request_uri);
|
|
88
|
+
generatedWalletUri.searchParams.set('encryption_key', Convert.uint8Array(encryptionKey).toBase64Url());
|
|
89
|
+
// call user's callback so they can send the URI to the wallet as they see fit
|
|
90
|
+
onWalletUriReady(generatedWalletUri.toString());
|
|
91
|
+
const tokenUrl = EnboxConnectProtocol.buildConnectUrl({
|
|
92
|
+
baseURL: connectServerUrl,
|
|
93
|
+
endpoint: 'token',
|
|
94
|
+
tokenParam: request.state,
|
|
95
|
+
});
|
|
96
|
+
// subscribe to receiving a response from the wallet with default TTL. receive ciphertext of {@link EnboxConnectResponse}
|
|
97
|
+
const authResponse = yield pollWithTtl(() => fetch(tokenUrl, { signal: AbortSignal.timeout(30000) }));
|
|
98
|
+
if (authResponse) {
|
|
99
|
+
const jwe = yield (authResponse === null || authResponse === void 0 ? void 0 : authResponse.text());
|
|
100
|
+
// Get the PIN from the user and use it as AAD to decrypt.
|
|
101
|
+
const pin = yield validatePin();
|
|
102
|
+
const jwt = yield EnboxConnectProtocol.decryptResponse(clientDid, jwe, pin);
|
|
103
|
+
const verifiedResponse = (yield EnboxConnectProtocol.verifyJwt({
|
|
104
|
+
jwt,
|
|
105
|
+
}));
|
|
106
|
+
return {
|
|
107
|
+
delegateGrants: verifiedResponse.delegateGrants,
|
|
108
|
+
delegatePortableDid: verifiedResponse.delegatePortableDid,
|
|
109
|
+
connectedDid: verifiedResponse.providerDid,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Creates a set of Dwn Permission Scopes to request for a given protocol.
|
|
116
|
+
*
|
|
117
|
+
* If no permissions are provided, the default is to request all relevant record permissions (write, read, delete, query, subscribe).
|
|
118
|
+
* 'configure' is not included by default, as this gives the application a lot of control over the protocol.
|
|
119
|
+
*/
|
|
120
|
+
function createPermissionRequestForProtocol({ definition, permissions }) {
|
|
121
|
+
const requests = [];
|
|
122
|
+
// Add the ability to query for the specific protocol
|
|
123
|
+
requests.push({
|
|
124
|
+
protocol: definition.protocol,
|
|
125
|
+
interface: DwnInterfaceName.Protocols,
|
|
126
|
+
method: DwnMethodName.Query,
|
|
127
|
+
});
|
|
128
|
+
// A Messages.Read grant is a unified scope that covers MessagesRead, MessagesSync, and MessagesSubscribe.
|
|
129
|
+
// This single grant enables sync and real-time subscriptions for the protocol.
|
|
130
|
+
requests.push({
|
|
131
|
+
protocol: definition.protocol,
|
|
132
|
+
interface: DwnInterfaceName.Messages,
|
|
133
|
+
method: DwnMethodName.Read,
|
|
134
|
+
});
|
|
135
|
+
// We also request any additional permissions the user has requested for this protocol
|
|
136
|
+
for (const permission of permissions) {
|
|
137
|
+
switch (permission) {
|
|
138
|
+
case 'write':
|
|
139
|
+
requests.push({
|
|
140
|
+
protocol: definition.protocol,
|
|
141
|
+
interface: DwnInterfaceName.Records,
|
|
142
|
+
method: DwnMethodName.Write,
|
|
143
|
+
});
|
|
144
|
+
break;
|
|
145
|
+
case 'read':
|
|
146
|
+
requests.push({
|
|
147
|
+
protocol: definition.protocol,
|
|
148
|
+
interface: DwnInterfaceName.Records,
|
|
149
|
+
method: DwnMethodName.Read,
|
|
150
|
+
});
|
|
151
|
+
break;
|
|
152
|
+
case 'delete':
|
|
153
|
+
requests.push({
|
|
154
|
+
protocol: definition.protocol,
|
|
155
|
+
interface: DwnInterfaceName.Records,
|
|
156
|
+
method: DwnMethodName.Delete,
|
|
157
|
+
});
|
|
158
|
+
break;
|
|
159
|
+
case 'query':
|
|
160
|
+
requests.push({
|
|
161
|
+
protocol: definition.protocol,
|
|
162
|
+
interface: DwnInterfaceName.Records,
|
|
163
|
+
method: DwnMethodName.Query,
|
|
164
|
+
});
|
|
165
|
+
break;
|
|
166
|
+
case 'subscribe':
|
|
167
|
+
requests.push({
|
|
168
|
+
protocol: definition.protocol,
|
|
169
|
+
interface: DwnInterfaceName.Records,
|
|
170
|
+
method: DwnMethodName.Subscribe,
|
|
171
|
+
});
|
|
172
|
+
break;
|
|
173
|
+
case 'configure':
|
|
174
|
+
requests.push({
|
|
175
|
+
protocol: definition.protocol,
|
|
176
|
+
interface: DwnInterfaceName.Protocols,
|
|
177
|
+
method: DwnMethodName.Configure,
|
|
178
|
+
});
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
protocolDefinition: definition,
|
|
184
|
+
permissionScopes: requests,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
export const WalletConnect = { initClient, createPermissionRequestForProtocol };
|
|
188
|
+
//# sourceMappingURL=wallet-connect-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet-connect-client.js","sourceRoot":"","sources":["../../src/wallet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BACpC,CAAC,CAAC;gBACH,MAAM;YACR,KAAK,WAAW;gBACd,QAAQ,CAAC,IAAI,CAAC;oBACZ,QAAQ,EAAI,UAAU,CAAC,QAAQ;oBAC/B,SAAS,EAAG,gBAAgB,CAAC,SAAS;oBACtC,MAAM,EAAM,aAAa,CAAC,SAAS;iBACpC,CAAC,CAAC;gBACH,MAAM;QACV,CAAC;IACH,CAAC;IAED,OAAO;QACL,kBAAkB,EAAG,UAAU;QAC/B,gBAAgB,EAAK,QAAQ;KAC9B,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,MAAM,aAAa,GAAG,EAAE,UAAU,EAAE,kCAAkC,EAAE,CAAC"}
|
|
@@ -5,11 +5,10 @@
|
|
|
5
5
|
* multi-identity-aware auth system that works in both browser and CLI environments.
|
|
6
6
|
* @module
|
|
7
7
|
*/
|
|
8
|
+
import type { HdIdentityVault, PortableIdentity } from '@enbox/agent';
|
|
9
|
+
import type { AuthEvent, AuthEventHandler, AuthManagerOptions, AuthState, ConnectOptions, DisconnectOptions, HeadlessConnectOptions, IdentityInfo, ImportFromPhraseOptions, ImportFromPortableOptions, LocalConnectOptions, RestoreSessionOptions, ShutdownOptions, WalletConnectOptions } from './types.js';
|
|
8
10
|
import { EnboxUserAgent } from '@enbox/agent';
|
|
9
|
-
import type { PortableIdentity } from '@enbox/agent';
|
|
10
11
|
import { AuthSession } from './identity-session.js';
|
|
11
|
-
import { VaultManager } from './vault/vault-manager.js';
|
|
12
|
-
import type { AuthEvent, AuthEventHandler, AuthManagerOptions, AuthState, DisconnectOptions, HeadlessConnectOptions, IdentityInfo, ImportFromPhraseOptions, ImportFromPortableOptions, LocalConnectOptions, RestoreSessionOptions, ShutdownOptions, WalletConnectOptions } from './types.js';
|
|
13
12
|
/**
|
|
14
13
|
* The primary entry point for authentication and identity management.
|
|
15
14
|
*
|
|
@@ -47,7 +46,6 @@ export declare class AuthManager {
|
|
|
47
46
|
private _userAgent;
|
|
48
47
|
private _emitter;
|
|
49
48
|
private _storage;
|
|
50
|
-
private _vault;
|
|
51
49
|
private _session;
|
|
52
50
|
private _state;
|
|
53
51
|
private _isConnecting;
|
|
@@ -57,6 +55,7 @@ export declare class AuthManager {
|
|
|
57
55
|
private _defaultSync?;
|
|
58
56
|
private _defaultDwnEndpoints?;
|
|
59
57
|
private _registration?;
|
|
58
|
+
private _connectHandler?;
|
|
60
59
|
/**
|
|
61
60
|
* The local DWN server endpoint discovered during `create()`, if any.
|
|
62
61
|
* `undefined` means no local server was found. This is set before any
|
|
@@ -77,16 +76,60 @@ export declare class AuthManager {
|
|
|
77
76
|
*/
|
|
78
77
|
static create(options?: AuthManagerOptions): Promise<AuthManager>;
|
|
79
78
|
/**
|
|
80
|
-
*
|
|
79
|
+
* Connect to a wallet or create a local session.
|
|
81
80
|
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
81
|
+
* This is the primary entry point for dapps. It routes to the
|
|
82
|
+
* appropriate flow based on the options:
|
|
84
83
|
*
|
|
85
|
-
*
|
|
84
|
+
* **Handler-based connect** (dapps): Delegates credential acquisition
|
|
85
|
+
* to a {@link ConnectHandler}. Triggered when `protocols` or
|
|
86
|
+
* `connectHandler` is provided.
|
|
87
|
+
*
|
|
88
|
+
* **Local connect** (wallets / CLI): Creates or unlocks a local vault.
|
|
89
|
+
* Triggered when `password`, `createIdentity`, or `recoveryPhrase`
|
|
90
|
+
* is provided.
|
|
91
|
+
*
|
|
92
|
+
* In both cases, `connect()` first attempts to restore a previous
|
|
93
|
+
* session. If a valid session exists, it is returned immediately
|
|
94
|
+
* without any user interaction.
|
|
95
|
+
*
|
|
96
|
+
* @example Dapp (browser)
|
|
97
|
+
* ```ts
|
|
98
|
+
* import { BrowserConnectHandler } from '@enbox/browser';
|
|
99
|
+
*
|
|
100
|
+
* const auth = await AuthManager.create({
|
|
101
|
+
* connectHandler: BrowserConnectHandler(),
|
|
102
|
+
* });
|
|
103
|
+
* const session = await auth.connect({
|
|
104
|
+
* protocols: [NotesProtocol],
|
|
105
|
+
* });
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* @example Wallet / CLI
|
|
109
|
+
* ```ts
|
|
110
|
+
* const session = await auth.connect({
|
|
111
|
+
* password: userPin,
|
|
112
|
+
* createIdentity: true,
|
|
113
|
+
* });
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
* @param options - Connection options. The shape determines the flow.
|
|
117
|
+
* @returns An active AuthSession.
|
|
118
|
+
* @throws If a connection attempt is already in progress.
|
|
119
|
+
* @throws If handler-based connect is attempted without a handler.
|
|
120
|
+
*/
|
|
121
|
+
connect(options?: ConnectOptions): Promise<AuthSession>;
|
|
122
|
+
/**
|
|
123
|
+
* Create or reconnect a local identity (explicit local connect).
|
|
124
|
+
*
|
|
125
|
+
* Use this when you explicitly want the local vault flow, bypassing
|
|
126
|
+
* auto-detection. This is the preferred method for wallet apps.
|
|
127
|
+
*
|
|
128
|
+
* @param options - Local connect options.
|
|
86
129
|
* @returns An active AuthSession.
|
|
87
130
|
* @throws If a connection attempt is already in progress.
|
|
88
131
|
*/
|
|
89
|
-
|
|
132
|
+
connectLocal(options?: LocalConnectOptions): Promise<AuthSession>;
|
|
90
133
|
/**
|
|
91
134
|
* Connect to an external wallet via the Enbox Connect relay protocol.
|
|
92
135
|
*
|
|
@@ -223,8 +266,8 @@ export declare class AuthManager {
|
|
|
223
266
|
* to another device.
|
|
224
267
|
*/
|
|
225
268
|
exportIdentity(didUri: string): Promise<PortableIdentity>;
|
|
226
|
-
/** Access the vault
|
|
227
|
-
get vault():
|
|
269
|
+
/** Access the underlying identity vault for lock/unlock/backup operations. */
|
|
270
|
+
get vault(): HdIdentityVault;
|
|
228
271
|
/**
|
|
229
272
|
* Subscribe to an auth lifecycle event.
|
|
230
273
|
*
|
|
@@ -251,6 +294,41 @@ export declare class AuthManager {
|
|
|
251
294
|
* before any event listeners are attached.
|
|
252
295
|
*/
|
|
253
296
|
get localDwnEndpoint(): string | undefined;
|
|
297
|
+
/**
|
|
298
|
+
* Determine whether the given options indicate a local connect flow.
|
|
299
|
+
*
|
|
300
|
+
* Local connect is indicated by the presence of `password`,
|
|
301
|
+
* `createIdentity`, or `recoveryPhrase` — signals that the caller
|
|
302
|
+
* is managing its own vault/identity lifecycle. In non-browser
|
|
303
|
+
* environments, local connect is the fallback.
|
|
304
|
+
*/
|
|
305
|
+
private _isLocalConnect;
|
|
306
|
+
/**
|
|
307
|
+
* Run a handler-based (delegated) connect flow.
|
|
308
|
+
*
|
|
309
|
+
* 1. Initialize the vault (agent-only, no identity).
|
|
310
|
+
* 2. Normalize protocol permission requests.
|
|
311
|
+
* 3. Delegate to the connect handler for credential acquisition.
|
|
312
|
+
* 4. Import the delegate DID, process grants, set up sync.
|
|
313
|
+
* 5. Finalize and return the AuthSession.
|
|
314
|
+
*/
|
|
315
|
+
private _handlerConnect;
|
|
316
|
+
/**
|
|
317
|
+
* Build a `FlowContext` from the manager's current state.
|
|
318
|
+
*
|
|
319
|
+
* Replaces the 5 manual inline context constructions that were
|
|
320
|
+
* previously duplicated across `connect()`, `walletConnect()`,
|
|
321
|
+
* `importFromPhrase()`, `importFromPortable()`, and `restoreSession()`.
|
|
322
|
+
*/
|
|
323
|
+
private _flowContext;
|
|
324
|
+
/**
|
|
325
|
+
* Template for connection flows that follow the guard → try/finally → setState pattern.
|
|
326
|
+
*
|
|
327
|
+
* Consolidates the duplicated concurrency guard, `_isConnecting` flag management,
|
|
328
|
+
* session assignment, and state transition across `connect()`, `walletConnect()`,
|
|
329
|
+
* `importFromPhrase()`, and `importFromPortable()`.
|
|
330
|
+
*/
|
|
331
|
+
private _withConnect;
|
|
254
332
|
private _setState;
|
|
255
333
|
private _guardConcurrency;
|
|
256
334
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-manager.d.ts","sourceRoot":"","sources":["../../src/auth-manager.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"auth-manager.d.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AAO,CAAC,IAAI,CAAC;IA4BnD;;;;;OAKG;IACG,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAM/D,8EAA8E;IAC9E,IAAI,KAAK,IAAI,eAAe,CAE3B;IAID;;;;;;OAMG;IACH,EAAE,CAAC,CAAC,SAAS,SAAS,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI;IAM3E,8BAA8B;IAC9B,IAAI,KAAK,IAAI,SAAS,CAErB;IAED,wCAAwC;IACxC,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED,6CAA6C;IAC7C,IAAI,QAAQ,IAAI,OAAO,CAEtB;IAED,mDAAmD;IACnD,IAAI,YAAY,IAAI,OAAO,CAE1B;IAED,0DAA0D;IAC1D,IAAI,KAAK,IAAI,cAAc,CAE1B;IAED;;;;;;OAMG;IACH,IAAI,gBAAgB,IAAI,MAAM,GAAG,SAAS,CAEzC;IAID;;;;;;;OAOG;IACH,OAAO,CAAC,eAAe;IA0BvB;;;;;;;;OAQG;YACW,eAAe;IAoD7B;;;;;;OAMG;IACH,OAAO,CAAC,YAAY;IAapB;;;;;;OAMG;YACW,YAAY;IAc1B,OAAO,CAAC,SAAS;IAOjB,OAAO,CAAC,iBAAiB;CAQ1B"}
|
|
@@ -0,0 +1,25 @@
|
|
|
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 { AuthSession } from '../identity-session.js';
|
|
9
|
+
import type { FlowContext } from './lifecycle.js';
|
|
10
|
+
import type { ImportFromPhraseOptions, ImportFromPortableOptions } from '../types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Import (or recover) an identity from a BIP-39 recovery phrase.
|
|
13
|
+
*
|
|
14
|
+
* This re-initializes the vault with the given phrase and password,
|
|
15
|
+
* recovering the agent DID and all derived keys.
|
|
16
|
+
*/
|
|
17
|
+
export declare function importFromPhrase(ctx: FlowContext, options: ImportFromPhraseOptions): Promise<AuthSession>;
|
|
18
|
+
/**
|
|
19
|
+
* Import an identity from a PortableIdentity JSON object.
|
|
20
|
+
*
|
|
21
|
+
* The portable identity contains the DID's private keys and metadata,
|
|
22
|
+
* allowing it to be used on this device.
|
|
23
|
+
*/
|
|
24
|
+
export declare function importFromPortable(ctx: FlowContext, options: ImportFromPortableOptions): Promise<AuthSession>;
|
|
25
|
+
//# sourceMappingURL=import.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"import.d.ts","sourceRoot":"","sources":["../../../src/connect/import.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,KAAK,EAAE,uBAAuB,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AAMtF;;;;;GAKG;AACH,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,WAAW,EAChB,OAAO,EAAE,uBAAuB,GAC/B,OAAO,CAAC,WAAW,CAAC,CAiEtB;AAED;;;;;GAKG;AACH,wBAAsB,kBAAkB,CACtC,GAAG,EAAE,WAAW,EAChB,OAAO,EAAE,yBAAyB,GACjC,OAAO,CAAC,WAAW,CAAC,CA8CtB"}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared helpers for connect flows.
|
|
3
|
+
*
|
|
4
|
+
* Consolidates duplicated logic across `local-connect`, `session-restore`,
|
|
5
|
+
* `wallet-connect`, and `import-identity` flows:
|
|
6
|
+
*
|
|
7
|
+
* - Password resolution chain
|
|
8
|
+
* - Vault init/start lifecycle
|
|
9
|
+
* - Sync mode/interval calculation and startup
|
|
10
|
+
* - `connectedDid` / `delegateDid` derivation from identity metadata
|
|
11
|
+
* - Session finalization (storage persistence + AuthSession construction + events)
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
import type { PortableDid } from '@enbox/dids';
|
|
17
|
+
import type { BearerIdentity, DwnDataEncodedRecordsWriteMessage, EnboxUserAgent } from '@enbox/agent';
|
|
18
|
+
import type { AuthEventEmitter } from '../events.js';
|
|
19
|
+
import type { PasswordProvider } from '../password-provider.js';
|
|
20
|
+
import type { RegistrationOptions, StorageAdapter, SyncOption } from '../types.js';
|
|
21
|
+
import { AuthSession } from '../identity-session.js';
|
|
22
|
+
/**
|
|
23
|
+
* Unified context passed from `AuthManager` to every connect flow.
|
|
24
|
+
*
|
|
25
|
+
* Replaces the per-flow `LocalConnectContext`, `SessionRestoreContext`,
|
|
26
|
+
* `WalletConnectContext`, and `ImportContext` interfaces. All fields are
|
|
27
|
+
* optional beyond the core triple (`userAgent`, `emitter`, `storage`) so
|
|
28
|
+
* flows only consume what they need.
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export interface FlowContext {
|
|
33
|
+
userAgent: EnboxUserAgent;
|
|
34
|
+
emitter: AuthEventEmitter;
|
|
35
|
+
storage: StorageAdapter;
|
|
36
|
+
defaultPassword?: string;
|
|
37
|
+
passwordProvider?: PasswordProvider;
|
|
38
|
+
defaultSync?: SyncOption;
|
|
39
|
+
defaultDwnEndpoints?: string[];
|
|
40
|
+
registration?: RegistrationOptions;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Resolve a password through the standard chain:
|
|
44
|
+
* explicit option → manager default → provider → insecure fallback.
|
|
45
|
+
*
|
|
46
|
+
* Emits a console warning when the insecure default is used.
|
|
47
|
+
*
|
|
48
|
+
* @param ctx - The flow context (provides `defaultPassword` and `passwordProvider`).
|
|
49
|
+
* @param explicit - An explicit password from the caller (highest priority).
|
|
50
|
+
* @param isFirstLaunch - Whether the vault has never been initialized.
|
|
51
|
+
* @returns The resolved password string.
|
|
52
|
+
*
|
|
53
|
+
* @internal
|
|
54
|
+
*/
|
|
55
|
+
export declare function resolvePassword(ctx: Pick<FlowContext, 'defaultPassword' | 'passwordProvider'>, explicit: string | undefined, isFirstLaunch: boolean): Promise<string>;
|
|
56
|
+
/**
|
|
57
|
+
* Initialize (on first launch) and start the agent, then emit `vault-unlocked`.
|
|
58
|
+
*
|
|
59
|
+
* This consolidates the 5 copies of:
|
|
60
|
+
* ```ts
|
|
61
|
+
* if (isFirstLaunch) { await userAgent.initialize({ password, ... }); }
|
|
62
|
+
* await userAgent.start({ password });
|
|
63
|
+
* emitter.emit('vault-unlocked', {});
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @returns The recovery phrase if the vault was just initialized, otherwise `undefined`.
|
|
67
|
+
*
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
export declare function ensureVaultReady(params: {
|
|
71
|
+
userAgent: EnboxUserAgent;
|
|
72
|
+
emitter: AuthEventEmitter;
|
|
73
|
+
password: string;
|
|
74
|
+
isFirstLaunch: boolean;
|
|
75
|
+
recoveryPhrase?: string;
|
|
76
|
+
dwnEndpoints?: string[];
|
|
77
|
+
}): Promise<string | undefined>;
|
|
78
|
+
/**
|
|
79
|
+
* Start DWN synchronisation if `sync` is not `'off'`.
|
|
80
|
+
*
|
|
81
|
+
* Consolidates 6 copies of:
|
|
82
|
+
* ```ts
|
|
83
|
+
* const syncMode = sync === undefined ? 'live' : 'poll';
|
|
84
|
+
* const syncInterval = sync ?? (syncMode === 'live' ? '5m' : '2m');
|
|
85
|
+
* userAgent.sync.startSync({ mode: syncMode, interval: syncInterval })
|
|
86
|
+
* .catch((err) => console.error('[@enbox/auth] Sync failed:', err));
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @internal
|
|
90
|
+
*/
|
|
91
|
+
export declare function startSyncIfEnabled(userAgent: EnboxUserAgent, sync: SyncOption | undefined): void;
|
|
92
|
+
/**
|
|
93
|
+
* Create a new `did:dht` identity with Ed25519 signing and X25519
|
|
94
|
+
* encryption keys, and a DWN service endpoint.
|
|
95
|
+
*
|
|
96
|
+
* This consolidates the identical identity creation block that was
|
|
97
|
+
* duplicated in `localConnect` and `importFromPhrase`.
|
|
98
|
+
*
|
|
99
|
+
* @internal
|
|
100
|
+
*/
|
|
101
|
+
export declare function createDefaultIdentity(userAgent: EnboxUserAgent, dwnEndpoints?: string[], name?: string): Promise<BearerIdentity>;
|
|
102
|
+
/**
|
|
103
|
+
* Derive `connectedDid` and `delegateDid` from identity metadata.
|
|
104
|
+
*
|
|
105
|
+
* For a **local** identity: `connectedDid` is the identity's own DID URI
|
|
106
|
+
* and `delegateDid` is `undefined`.
|
|
107
|
+
*
|
|
108
|
+
* For a **wallet-connected** identity: `connectedDid` is the external wallet
|
|
109
|
+
* DID, and `delegateDid` is the local identity's DID URI.
|
|
110
|
+
*
|
|
111
|
+
* @param identity - The bearer identity to extract DIDs from.
|
|
112
|
+
* @param storedDelegateDid - Optional fallback delegate DID from storage,
|
|
113
|
+
* used by session-restore when the identity metadata doesn't include a
|
|
114
|
+
* `connectedDid` but a delegate DID was persisted in a prior session.
|
|
115
|
+
*
|
|
116
|
+
* @internal
|
|
117
|
+
*/
|
|
118
|
+
export declare function resolveIdentityDids(identity: BearerIdentity, storedDelegateDid?: string): {
|
|
119
|
+
connectedDid: string;
|
|
120
|
+
delegateDid: string | undefined;
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Process connected grants by storing them in the local DWN as the owner.
|
|
124
|
+
*
|
|
125
|
+
* This is the agent-level equivalent of `Enbox.processConnectedGrants()`.
|
|
126
|
+
* It stores each grant, signed as owner, and returns the deduplicated
|
|
127
|
+
* list of protocol URIs represented by the grants.
|
|
128
|
+
*
|
|
129
|
+
* @internal
|
|
130
|
+
*/
|
|
131
|
+
export declare function processConnectedGrants(params: {
|
|
132
|
+
agent: EnboxUserAgent;
|
|
133
|
+
delegateDid: string;
|
|
134
|
+
grants: DwnDataEncodedRecordsWriteMessage[];
|
|
135
|
+
}): Promise<string[]>;
|
|
136
|
+
/**
|
|
137
|
+
* Import a delegated DID, process its grants, register sync, and pull.
|
|
138
|
+
*
|
|
139
|
+
* This is the shared post-connect lifecycle used by both the DWeb Connect
|
|
140
|
+
* and relay WalletConnect flows. On failure, the imported identity is
|
|
141
|
+
* cleaned up before re-throwing.
|
|
142
|
+
*
|
|
143
|
+
* @internal
|
|
144
|
+
*/
|
|
145
|
+
export declare function importDelegateAndSetupSync(params: {
|
|
146
|
+
userAgent: EnboxUserAgent;
|
|
147
|
+
delegatePortableDid: PortableDid;
|
|
148
|
+
connectedDid: string;
|
|
149
|
+
delegateGrants: DwnDataEncodedRecordsWriteMessage[];
|
|
150
|
+
flowName: string;
|
|
151
|
+
}): Promise<BearerIdentity>;
|
|
152
|
+
/**
|
|
153
|
+
* Build an `AuthSession` for a delegated connect flow (DWeb Connect or
|
|
154
|
+
* relay WalletConnect). Starts sync and persists delegate/connected DID
|
|
155
|
+
* markers.
|
|
156
|
+
*
|
|
157
|
+
* @internal
|
|
158
|
+
*/
|
|
159
|
+
export declare function finalizeDelegateSession(params: {
|
|
160
|
+
userAgent: EnboxUserAgent;
|
|
161
|
+
emitter: AuthEventEmitter;
|
|
162
|
+
storage: StorageAdapter;
|
|
163
|
+
identity: BearerIdentity;
|
|
164
|
+
connectedDid: string;
|
|
165
|
+
delegateDid: string;
|
|
166
|
+
sync: SyncOption | undefined;
|
|
167
|
+
}): Promise<AuthSession>;
|
|
168
|
+
/**
|
|
169
|
+
* Persist session markers, build an `AuthSession`, and emit lifecycle events.
|
|
170
|
+
*
|
|
171
|
+
* Consolidates 5 copies of:
|
|
172
|
+
* ```ts
|
|
173
|
+
* await storage.set(STORAGE_KEYS.PREVIOUSLY_CONNECTED, 'true');
|
|
174
|
+
* await storage.set(STORAGE_KEYS.ACTIVE_IDENTITY, connectedDid);
|
|
175
|
+
* const session = new AuthSession({ ... });
|
|
176
|
+
* emitter.emit('identity-added', { identity: identityInfo });
|
|
177
|
+
* emitter.emit('session-start', { session: { ... } });
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* @param params.emitIdentityAdded - Whether to emit `identity-added`. Defaults to `true`.
|
|
181
|
+
* Set to `false` for session-restore (identity was already added in the original flow).
|
|
182
|
+
* @param params.extraStorageKeys - Additional key-value pairs to persist (e.g. delegate/connected DIDs
|
|
183
|
+
* for wallet-connect flows).
|
|
184
|
+
*
|
|
185
|
+
* @internal
|
|
186
|
+
*/
|
|
187
|
+
export declare function finalizeSession(params: {
|
|
188
|
+
userAgent: EnboxUserAgent;
|
|
189
|
+
emitter: AuthEventEmitter;
|
|
190
|
+
storage: StorageAdapter;
|
|
191
|
+
connectedDid: string;
|
|
192
|
+
delegateDid?: string;
|
|
193
|
+
recoveryPhrase?: string;
|
|
194
|
+
identityName?: string;
|
|
195
|
+
identityConnectedDid?: string;
|
|
196
|
+
emitIdentityAdded?: boolean;
|
|
197
|
+
extraStorageKeys?: Record<string, string>;
|
|
198
|
+
}): Promise<AuthSession>;
|
|
199
|
+
//# sourceMappingURL=lifecycle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lifecycle.d.ts","sourceRoot":"","sources":["../../../src/connect/lifecycle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,EAAE,cAAc,EAAE,iCAAiC,EAAyD,cAAc,EAAE,MAAM,cAAc,CAAC;AAE7J,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAChE,OAAO,KAAK,EAAgB,mBAAmB,EAAE,cAAc,EAAE,UAAU,EAA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AM,CAAC;IACrB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC3C,GAAG,OAAO,CAAC,WAAW,CAAC,CAiDvB"}
|
|
@@ -0,0 +1,23 @@
|
|
|
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 { AuthSession } from '../identity-session.js';
|
|
9
|
+
import type { FlowContext } from './lifecycle.js';
|
|
10
|
+
import type { LocalConnectOptions } from '../types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Execute the local connect flow.
|
|
13
|
+
*
|
|
14
|
+
* - On first launch: initializes the vault. Identity creation is opt-in via
|
|
15
|
+
* `options.createIdentity: true`.
|
|
16
|
+
* - On subsequent launches: unlocks the vault and reconnects to the existing identity.
|
|
17
|
+
*
|
|
18
|
+
* When no identities exist and `createIdentity` is not `true`, the session
|
|
19
|
+
* is returned with the **agent DID** as the connected DID. This allows apps to
|
|
20
|
+
* manage identity creation separately from vault setup.
|
|
21
|
+
*/
|
|
22
|
+
export declare function localConnect(ctx: FlowContext, options?: LocalConnectOptions): Promise<AuthSession>;
|
|
23
|
+
//# sourceMappingURL=local.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local.d.ts","sourceRoot":"","sources":["../../../src/connect/local.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAOvD;;;;;;;;;;GAUG;AACH,wBAAsB,YAAY,CAChC,GAAG,EAAE,WAAW,EAChB,OAAO,GAAE,mBAAwB,GAChC,OAAO,CAAC,WAAW,CAAC,CAoFtB"}
|
|
@@ -0,0 +1,18 @@
|
|
|
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 { AuthSession } from '../identity-session.js';
|
|
9
|
+
import type { FlowContext } from './lifecycle.js';
|
|
10
|
+
import type { RestoreSessionOptions } from '../types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Attempt to restore a previous session.
|
|
13
|
+
*
|
|
14
|
+
* Returns `undefined` if no previous session exists.
|
|
15
|
+
* Returns an `AuthSession` if the session was successfully restored.
|
|
16
|
+
*/
|
|
17
|
+
export declare function restoreSession(ctx: FlowContext, options?: RestoreSessionOptions): Promise<AuthSession | undefined>;
|
|
18
|
+
//# sourceMappingURL=restore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"restore.d.ts","sourceRoot":"","sources":["../../../src/connect/restore.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAMzD;;;;;GAKG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,WAAW,EAChB,OAAO,GAAE,qBAA0B,GAClC,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CA2GlC"}
|