@enbox/agent 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/dist/browser.mjs +11 -11
  2. package/dist/browser.mjs.map +4 -4
  3. package/dist/esm/anonymous-dwn-api.js +184 -0
  4. package/dist/esm/anonymous-dwn-api.js.map +1 -0
  5. package/dist/esm/dwn-api.js +86 -777
  6. package/dist/esm/dwn-api.js.map +1 -1
  7. package/dist/esm/dwn-encryption.js +342 -0
  8. package/dist/esm/dwn-encryption.js.map +1 -0
  9. package/dist/esm/dwn-key-delivery.js +256 -0
  10. package/dist/esm/dwn-key-delivery.js.map +1 -0
  11. package/dist/esm/dwn-record-upgrade.js +119 -0
  12. package/dist/esm/dwn-record-upgrade.js.map +1 -0
  13. package/dist/esm/dwn-type-guards.js +23 -0
  14. package/dist/esm/dwn-type-guards.js.map +1 -0
  15. package/dist/esm/index.js +6 -0
  16. package/dist/esm/index.js.map +1 -1
  17. package/dist/esm/protocol-utils.js +158 -0
  18. package/dist/esm/protocol-utils.js.map +1 -0
  19. package/dist/esm/store-data-protocols.js +1 -1
  20. package/dist/esm/store-data-protocols.js.map +1 -1
  21. package/dist/esm/sync-engine-level.js +22 -353
  22. package/dist/esm/sync-engine-level.js.map +1 -1
  23. package/dist/esm/sync-messages.js +234 -0
  24. package/dist/esm/sync-messages.js.map +1 -0
  25. package/dist/esm/sync-topological-sort.js +143 -0
  26. package/dist/esm/sync-topological-sort.js.map +1 -0
  27. package/dist/esm/test-harness.js +20 -0
  28. package/dist/esm/test-harness.js.map +1 -1
  29. package/dist/types/anonymous-dwn-api.d.ts +140 -0
  30. package/dist/types/anonymous-dwn-api.d.ts.map +1 -0
  31. package/dist/types/dwn-api.d.ts +36 -179
  32. package/dist/types/dwn-api.d.ts.map +1 -1
  33. package/dist/types/dwn-encryption.d.ts +144 -0
  34. package/dist/types/dwn-encryption.d.ts.map +1 -0
  35. package/dist/types/dwn-key-delivery.d.ts +112 -0
  36. package/dist/types/dwn-key-delivery.d.ts.map +1 -0
  37. package/dist/types/dwn-record-upgrade.d.ts +33 -0
  38. package/dist/types/dwn-record-upgrade.d.ts.map +1 -0
  39. package/dist/types/dwn-type-guards.d.ts +9 -0
  40. package/dist/types/dwn-type-guards.d.ts.map +1 -0
  41. package/dist/types/index.d.ts +6 -0
  42. package/dist/types/index.d.ts.map +1 -1
  43. package/dist/types/protocol-utils.d.ts +70 -0
  44. package/dist/types/protocol-utils.d.ts.map +1 -0
  45. package/dist/types/sync-engine-level.d.ts +5 -42
  46. package/dist/types/sync-engine-level.d.ts.map +1 -1
  47. package/dist/types/sync-messages.d.ts +76 -0
  48. package/dist/types/sync-messages.d.ts.map +1 -0
  49. package/dist/types/sync-topological-sort.d.ts +15 -0
  50. package/dist/types/sync-topological-sort.d.ts.map +1 -0
  51. package/dist/types/test-harness.d.ts +10 -0
  52. package/dist/types/test-harness.d.ts.map +1 -1
  53. package/package.json +5 -5
  54. package/src/anonymous-dwn-api.ts +263 -0
  55. package/src/dwn-api.ts +160 -1015
  56. package/src/dwn-encryption.ts +481 -0
  57. package/src/dwn-key-delivery.ts +370 -0
  58. package/src/dwn-record-upgrade.ts +166 -0
  59. package/src/dwn-type-guards.ts +43 -0
  60. package/src/index.ts +6 -0
  61. package/src/protocol-utils.ts +185 -0
  62. package/src/store-data-protocols.ts +1 -1
  63. package/src/sync-engine-level.ts +24 -413
  64. package/src/sync-messages.ts +277 -0
  65. package/src/sync-topological-sort.ts +167 -0
  66. package/src/test-harness.ts +19 -0
@@ -1,9 +1,10 @@
1
- import type { DerivedPrivateJwk, DwnConfig, EncryptionKeyDeriver, GenericMessage, ProtocolDefinition } from '@enbox/dwn-sdk-js';
1
+ import type { DerivedPrivateJwk, DwnConfig, EncryptionKeyDeriver, ProtocolDefinition } from '@enbox/dwn-sdk-js';
2
2
  import type { PublicKeyJwk } from '@enbox/crypto';
3
3
  import { Dwn } from '@enbox/dwn-sdk-js';
4
4
  import type { Web5PlatformAgent } from './types/agent.js';
5
- import type { DwnMessage, DwnMessagesPermissionScope, DwnPermissionScope, DwnRecordsInterfaces, DwnRecordsPermissionScope, DwnResponse, ProcessDwnRequest, SendDwnRequest } from './types/dwn.js';
5
+ import type { DwnResponse, ProcessDwnRequest, SendDwnRequest } from './types/dwn.js';
6
6
  import { DwnInterface } from './types/dwn.js';
7
+ export { isDwnMessage, isDwnRequest, isMessagesPermissionScope, isRecordPermissionScope, isRecordsType } from './dwn-type-guards.js';
7
8
  type DwnApiParams = {
8
9
  agent?: Web5PlatformAgent;
9
10
  dwn: Dwn;
@@ -11,11 +12,6 @@ type DwnApiParams = {
11
12
  interface DwnApiCreateDwnParams extends Partial<DwnConfig> {
12
13
  dataPath?: string;
13
14
  }
14
- export declare function isDwnRequest<T extends DwnInterface>(dwnRequest: ProcessDwnRequest<DwnInterface>, messageType: T): dwnRequest is ProcessDwnRequest<T>;
15
- export declare function isDwnMessage<T extends DwnInterface>(messageType: T, message: GenericMessage): message is DwnMessage[T];
16
- export declare function isRecordsType(messageType: DwnInterface): messageType is DwnRecordsInterfaces;
17
- export declare function isRecordPermissionScope(scope: DwnPermissionScope): scope is DwnRecordsPermissionScope;
18
- export declare function isMessagesPermissionScope(scope: DwnPermissionScope): scope is DwnMessagesPermissionScope;
19
15
  export declare class AgentDwnApi {
20
16
  /**
21
17
  * Holds the instance of a `Web5PlatformAgent` that represents the current execution context for
@@ -72,131 +68,65 @@ export declare class AgentDwnApi {
72
68
  private constructDwnMessage;
73
69
  private hasGrantParams;
74
70
  private getSigner;
75
- /**
76
- * Resolves the encryption key info for a given DID.
77
- * Looks up the keyAgreement verification method in the DID document,
78
- * then resolves the corresponding KMS key URI.
79
- *
80
- * @param didUri - The DID URI to resolve encryption key info for
81
- * @returns keyId (fully qualified verification method ID), keyUri (KMS reference),
82
- * and publicKeyJwk. No private key material is returned.
83
- * @throws If the DID has no keyAgreement verification method or it's not X25519.
84
- */
85
- private getEncryptionKeyInfo;
86
- /**
87
- * Builds a partial EncryptionInput object for a single key-encryption entry.
88
- * The `authenticationTag` is NOT set here — the caller must set it after
89
- * AEAD encryption produces the tag.
90
- */
91
- private buildEncryptionInput;
92
- /**
93
- * Encrypts plaintext bytes with AEAD (AES-256-GCM by default) and computes
94
- * the CID of the resulting ciphertext. Returns everything needed to attach
95
- * the encrypted data to a DWN message, including the authentication tag.
96
- */
97
- private encryptAndComputeCid;
98
- /**
99
- * Derives a ProtocolContext public key for a given DID and context ID,
100
- * then returns a fully-formed EncryptionInput. Consolidates the repeated
101
- * getEncryptionKeyInfo → constructKeyDerivationPath → derivePublicKey
102
- * → build EncryptionInput sequence.
103
- */
104
- private deriveContextEncryptionInput;
105
- /**
106
- * Builds a KMS-backed JWE key unwrap callback. Used for both ProtocolPath
107
- * and ProtocolContext decryption where the KMS holds the root private key.
108
- */
109
- private buildKmsDecryptCallback;
110
71
  /**
111
72
  * Constructs an EncryptionKeyDeriver callback for the SDK.
112
- * The SDK calls derivePublicKey(path), the KMS performs HKDF + public key
113
- * computation internally. The private key never leaves the KMS.
114
- *
115
- * Analogous to getSigner() for signing operations.
73
+ * Delegates to the standalone function in `dwn-encryption.ts`.
116
74
  *
117
75
  * @param didUri - The DID URI to create the key deriver for
118
76
  * @returns An EncryptionKeyDeriver callback object
119
77
  */
120
78
  getEncryptionKeyDeriver(didUri: string): Promise<EncryptionKeyDeriver>;
121
79
  /**
122
- * Constructs a KeyDecrypter callback for the SDK.
123
- * The SDK calls decrypt(path, eciesParams), the KMS performs HKDF + ECIES
124
- * decryption internally. The private key never leaves the KMS.
125
- *
126
- * Analogous to getSigner() for signing operations.
80
+ * Resolves the keyAgreement verification method for the given DID and returns
81
+ * the key ID, key URI, and public key JWK.
127
82
  *
128
- * @param didUri - The DID URI to create the key decrypter for
129
- * @returns A KeyDecrypter callback object
83
+ * @param didUri - The DID URI to look up
130
84
  */
131
- private getKeyDecrypter;
85
+ private getEncryptionKeyInfo;
132
86
  /**
133
- * Fetches a protocol definition from the local DWN, with caching.
134
- * Returns undefined if the protocol is not installed.
87
+ * Constructs a ProtocolPath KeyDecrypter for the given DID.
135
88
  *
136
- * @param tenantDid - The tenant DID to query
137
- * @param protocolUri - The protocol URI to fetch
138
- * @returns The protocol definition, or undefined if not found
89
+ * @param didUri - The DID URI to build a decrypter for
139
90
  */
140
- private getProtocolDefinition;
91
+ private getKeyDecrypter;
141
92
  /**
142
- * Checks if a protocol path represents a multi-party context. Returns true
143
- * if the root path's subtree contains:
144
- * (a) any `$role: true` descendants, OR
145
- * (b) any relational `who`/`of` `$actions` rules that grant `read` access
146
- * (indicating external authors or recipients need context keys).
93
+ * Checks if a protocol path represents a multi-party context.
147
94
  *
148
- * This generalises the earlier `protocolPathHasRoles()` to cover protocols
149
- * that use relational access without explicit role definitions.
95
+ * @param protocolDefinition - The full protocol definition
96
+ * @param rootProtocolPath - The root protocol path to check
150
97
  */
151
98
  private isMultiPartyContext;
152
99
  /**
153
- * Checks whether any relational `who`/`of` rule in the protocol grants
154
- * `read` access for a given actor type and ancestor path.
155
- *
156
- * Walks the *entire* protocol structure looking for any `$actions` rule that:
157
- * - Has `who` equal to `actorType` ('recipient' or 'author'), or any actor
158
- * type if `actorType` is `undefined`
159
- * - Has `of` equal to `ofPath`
160
- * - Has `can` including 'read'
100
+ * Checks if any `$actions` rule in the protocol grants read access
101
+ * via `who: '<actorType>'` and `of: '<path>'`.
161
102
  *
162
- * The search covers all record types in the protocol, since a relational
163
- * rule can appear at any level (e.g. `{ who: 'recipient', of: 'thread',
164
- * can: ['read'] }` might be defined on `thread/message`).
165
- *
166
- * @param actorType - 'author' | 'recipient', or undefined for any
167
- * @param ofPath - The protocol path to check (e.g. 'thread', 'email')
168
- * @param protocolDefinition - The full protocol definition
169
- * @returns true if a matching relational read rule exists
103
+ * @param actorType - The actor type to check ('author', 'recipient', or undefined for any)
104
+ * @param ofPath - The protocol path to check
105
+ * @param protocolDefinition - The protocol definition
170
106
  */
171
107
  private hasRelationalReadAccess;
172
108
  /**
173
109
  * Analyses a record write to determine which DIDs need context key delivery.
174
110
  *
175
- * Returns a set of participant DIDs that should receive `contextKey` records.
176
- * The DWN owner (tenantDid) is always excluded — they have ProtocolPath access.
177
- *
178
- * Cases handled:
179
- * 1. `$role` record with a recipient → recipient is a participant
180
- * 2. Record has a recipient and a relational read rule grants access
181
- * via `{ who: 'recipient', of: '<path>', can: ['read'] }`
182
- * 3. Record is authored by an external party → if `{ who: 'author', of:
183
- * '<path>', can: ['read'] }` rules grant read access, the author needs
184
- * a context key.
185
- *
186
- * @param params.protocolDefinition - The installed protocol definition
187
- * @param params.protocolPath - The written record's protocol path
188
- * @param params.recipient - Recipient DID from the record, if any
189
- * @param params.tenantDid - The DWN owner's DID (excluded from results)
190
- * @param params.authorDid - Author DID if externally authored, undefined otherwise
111
+ * @param params - Parameters for participant detection
191
112
  * @returns Set of DIDs that need context key delivery
192
113
  */
193
- detectNewParticipants({ protocolDefinition, protocolPath, recipient, tenantDid, authorDid }: {
114
+ detectNewParticipants(params: {
194
115
  protocolDefinition: ProtocolDefinition;
195
116
  protocolPath: string;
196
117
  recipient?: string;
197
118
  tenantDid: string;
198
119
  authorDid?: string;
199
120
  }): Set<string>;
121
+ /**
122
+ * Fetches a protocol definition from the local DWN, with caching.
123
+ * Returns undefined if the protocol is not installed.
124
+ *
125
+ * @param tenantDid - The tenant DID to query
126
+ * @param protocolUri - The protocol URI to fetch
127
+ * @returns The protocol definition, or undefined if not found
128
+ */
129
+ private getProtocolDefinition;
200
130
  /**
201
131
  * Fetches a protocol definition from a remote DWN.
202
132
  * Uses an unsigned ProtocolsQuery (public protocols can be queried anonymously).
@@ -215,59 +145,11 @@ export declare class AgentDwnApi {
215
145
  * record exists yet
216
146
  */
217
147
  private extractDerivedPublicKey;
218
- /**
219
- * Reactively upgrades an externally-authored root record that has only
220
- * ProtocolPath encryption by appending a ProtocolContext recipient entry.
221
- *
222
- * After the upgrade, both the owner (ProtocolPath) and context key holders —
223
- * including the external author (ProtocolContext) — can decrypt the record.
224
- *
225
- * Steps:
226
- * 1. Decrypt the DEK using the owner's ProtocolPath-derived private key
227
- * 2. Derive the context public key from the owner's #enc key
228
- * 3. ECIES-encrypt the same DEK to the context public key
229
- * 4. Append the ProtocolContext recipient entry (using PR 0b append mode)
230
- * 5. Re-sign the record as owner
231
- *
232
- * The author's signature payload includes an `encryptionCid` that becomes
233
- * stale after step 4. The SDK's `validateIntegrity()` skips the encryptionCid
234
- * check on the author's signature when an ownerSignature is present (step 5),
235
- * since the owner vouches for the updated encryption property.
236
- *
237
- * NOTE: An alternative design would deliver the DEK out-of-band via the
238
- * key-delivery protocol (as a field on the contextKey record) instead of
239
- * mutating the record's encryption property. That avoids the stale
240
- * encryptionCid concern entirely but adds complexity to the read path and
241
- * the contextKey schema. We chose the in-record approach because it keeps
242
- * records self-contained and the read/decrypt path unchanged.
243
- *
244
- * @param tenantDid - The DWN owner's DID
245
- * @param recordsWrite - The RecordsWrite message to upgrade
246
- */
247
- private upgradeExternalRootRecord;
248
- /**
249
- * Resolves the appropriate KeyDecrypter for a record's encryption scheme.
250
- * Handles both single-party (ProtocolPath) and multi-party (ProtocolContext).
251
- *
252
- * For ProtocolContext records:
253
- * - Context creator: derives key directly from KMS
254
- * - Participant: fetches contextKey via key-delivery protocol, caches it
255
- */
256
- private resolveKeyDecrypter;
257
- /**
258
- * Builds a KeyDecrypter from a context-derived private key.
259
- * Uses the raw key directly (since it was shared with us via the key-delivery protocol).
260
- */
261
- private buildContextKeyDecrypter;
262
148
  /**
263
149
  * Post-processes a DWN reply, auto-decrypting data if encryption is enabled.
264
- * Delegates to the SDK's Records.decrypt() with the appropriate KeyDecrypter —
265
- * resolveKeyDecrypter() selects between ProtocolPath and ProtocolContext schemes.
150
+ * Delegates to the standalone function in `dwn-encryption.ts`.
266
151
  */
267
152
  private maybeDecryptReply;
268
- /**
269
- * FURTHER REFACTORING NEEDED BELOW THIS LINE
270
- */
271
153
  private getDwnMessage;
272
154
  /**
273
155
  * Cache for key delivery protocol installation status per tenant.
@@ -276,8 +158,7 @@ export declare class AgentDwnApi {
276
158
  private _keyDeliveryProtocolInstalledCache;
277
159
  /**
278
160
  * Ensures the key delivery protocol is installed on the given tenant's DWN,
279
- * with `$encryption` keys injected. Uses the same lazy initialization pattern
280
- * as `DwnDataStore.initialize()`.
161
+ * with `$encryption` keys injected.
281
162
  *
282
163
  * @param tenantDid - The DID of the DWN owner
283
164
  */
@@ -286,27 +167,10 @@ export declare class AgentDwnApi {
286
167
  * Writes a `contextKey` record to the owner's DWN, delivering an encrypted
287
168
  * context key to a participant.
288
169
  *
289
- * The payload is encrypted to the **recipient's** ProtocolPath-derived public
290
- * key on the key-delivery protocol, so only the recipient can decrypt it.
291
- * The recipient's key is supplied via `recipientKeyDeliveryPublicKey` (which
292
- * the external author attached as `authorKeyDeliveryPublicKey` on the
293
- * original cross-DWN record).
294
- *
295
- * When `recipientKeyDeliveryPublicKey` is not provided (e.g. the owner is
296
- * writing a contextKey for themselves), the record is encrypted to the
297
- * owner's own ProtocolPath key using the generic `processRequest` path.
298
- *
299
- * @param params.tenantDid - The DWN owner's DID (who is delivering the key)
300
- * @param params.recipientDid - The participant's DID (who will receive the key)
301
- * @param params.contextKeyData - The `DerivedPrivateJwk` to deliver
302
- * @param params.sourceProtocol - The URI of the source protocol (tag)
303
- * @param params.sourceContextId - The root context ID (tag)
304
- * @param params.recipientKeyDeliveryPublicKey - The recipient's ProtocolPath-
305
- * derived public key for `key-delivery/contextKey`. When provided,
306
- * the contextKey record is encrypted directly to this key.
170
+ * @param params - The write parameters
307
171
  * @returns The recordId of the written contextKey record
308
172
  */
309
- writeContextKeyRecord({ tenantDid, recipientDid, contextKeyData, sourceProtocol, sourceContextId, recipientKeyDeliveryPublicKey }: {
173
+ writeContextKeyRecord(params: {
310
174
  tenantDid: string;
311
175
  recipientDid: string;
312
176
  contextKeyData: DerivedPrivateJwk;
@@ -326,21 +190,14 @@ export declare class AgentDwnApi {
326
190
  * Fetches and decrypts a `contextKey` record from a DWN, returning the
327
191
  * `DerivedPrivateJwk` payload.
328
192
  *
329
- * Supports both local reads (tenant queries own DWN) and remote reads
330
- * (participant queries the context owner's DWN).
331
- *
332
- * @param params.ownerDid - The DWN owner's DID (where contextKey records live)
333
- * @param params.requesterDid - The DID of the requester (used for signing and decryption)
334
- * @param params.sourceProtocol - The URI of the source protocol (tag filter)
335
- * @param params.sourceContextId - The root context ID (tag filter)
193
+ * @param params - The fetch parameters
336
194
  * @returns The decrypted `DerivedPrivateJwk`, or `undefined` if no matching record found
337
195
  */
338
- fetchContextKeyRecord({ ownerDid, requesterDid, sourceProtocol, sourceContextId }: {
196
+ fetchContextKeyRecord(params: {
339
197
  ownerDid: string;
340
198
  requesterDid: string;
341
199
  sourceProtocol: string;
342
200
  sourceContextId: string;
343
201
  }): Promise<DerivedPrivateJwk | undefined>;
344
202
  }
345
- export {};
346
203
  //# sourceMappingURL=dwn-api.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"dwn-api.d.ts","sourceRoot":"","sources":["../../src/dwn-api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,SAAS,EAET,oBAAoB,EACpB,cAAc,EAEd,kBAAkB,EAOG,MAAM,mBAAmB,CAAC;AACjD,OAAO,KAAK,EAAgC,YAAY,EAAE,MAAM,eAAe,CAAC;AAGhF,OAAO,EAKL,GAAG,EAcJ,MAAM,mBAAmB,CAAC;AAI3B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,KAAK,EACV,UAAU,EAIV,0BAA0B,EAE1B,kBAAkB,EAClB,oBAAoB,EACpB,yBAAyB,EACzB,WAAW,EAGX,iBAAiB,EACjB,cAAc,EACf,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EAAE,YAAY,EAA0B,MAAM,gBAAgB,CAAC;AAQtE,KAAK,YAAY,GAAG;IAClB,KAAK,CAAC,EAAE,iBAAiB,CAAC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AC,MAAM,EAAE;QACzC,SAAS,EAAE,MAAM,CAAC;QAClB,YAAY,EAAE,MAAM,CAAC;QACrB,cAAc,EAAE,iBAAiB,CAAC;QAClC,cAAc,EAAE,MAAM,CAAC;QACvB,eAAe,EAAE,MAAM,CAAC;QACxB,6BAA6B,CAAC,EAAE;YAAE,SAAS,EAAE,MAAM,CAAC;YAAC,YAAY,EAAE,YAAY,CAAA;SAAE,CAAC;KACnF,GAAG,OAAO,CAAC,MAAM,CAAC;IASnB;;;OAGG;YACW,yBAAyB;IAWvC;;;;;;OAMG;IACU,qBAAqB,CAAC,MAAM,EAAE;QACzC,QAAQ,EAAE,MAAM,CAAC;QACjB,YAAY,EAAE,MAAM,CAAC;QACrB,cAAc,EAAE,MAAM,CAAC;QACvB,eAAe,EAAE,MAAM,CAAC;KACzB,GAAG,OAAO,CAAC,iBAAiB,GAAG,SAAS,CAAC;CAQ3C"}
@@ -0,0 +1,144 @@
1
+ import type { DerivedPrivateJwk, EncryptionInput, EncryptionKeyDeriver, KeyDecrypter, RecordsWriteMessage } from '@enbox/dwn-sdk-js';
2
+ import type { KeyIdentifier, PublicKeyJwk } from '@enbox/crypto';
3
+ import type { Web5PlatformAgent } from './types/agent.js';
4
+ import type { DwnMessageReply, ProcessDwnRequest, SendDwnRequest } from './types/dwn.js';
5
+ import { ContentEncryptionAlgorithm, KeyDerivationScheme } from '@enbox/dwn-sdk-js';
6
+ import { DwnInterface } from './types/dwn.js';
7
+ /**
8
+ * Returns the correct nonce/IV byte length for the given content encryption algorithm.
9
+ * A256GCM uses 96-bit (12-byte) nonces; XC20P uses 192-bit (24-byte) nonces.
10
+ */
11
+ export declare function ivLength(algorithm: ContentEncryptionAlgorithm): number;
12
+ /**
13
+ * Builds a partial EncryptionInput object for a single key-encryption entry.
14
+ * The `authenticationTag` is NOT set here — the caller must set it after
15
+ * AEAD encryption produces the tag.
16
+ */
17
+ export declare function buildEncryptionInput(dek: Uint8Array, iv: Uint8Array, publicKeyId: string, publicKey: PublicKeyJwk, derivationScheme: typeof KeyDerivationScheme.ProtocolPath | typeof KeyDerivationScheme.ProtocolContext): Omit<EncryptionInput, 'authenticationTag'>;
18
+ /**
19
+ * Encrypts plaintext bytes with AEAD and computes the CID of the resulting ciphertext.
20
+ * Returns everything needed to attach the encrypted data to a DWN message, including
21
+ * the authentication tag.
22
+ */
23
+ export declare function encryptAndComputeCid(plaintextBytes: Uint8Array, dek: Uint8Array, iv: Uint8Array, algorithm?: ContentEncryptionAlgorithm): Promise<{
24
+ encryptedBytes: Uint8Array;
25
+ dataCid: string;
26
+ dataSize: number;
27
+ authenticationTag: Uint8Array;
28
+ }>;
29
+ /**
30
+ * Resolves the encryption key info for a given DID.
31
+ * Looks up the keyAgreement verification method in the DID document,
32
+ * then resolves the corresponding KMS key URI.
33
+ *
34
+ * @param agent - The platform agent to use for DID resolution and key management
35
+ * @param didUri - The DID URI to resolve encryption key info for
36
+ * @returns keyId (fully qualified verification method ID), keyUri (KMS reference),
37
+ * and publicKeyJwk. No private key material is returned.
38
+ * @throws If the DID has no keyAgreement verification method or it's not X25519.
39
+ */
40
+ export declare function getEncryptionKeyInfo(agent: Web5PlatformAgent, didUri: string): Promise<{
41
+ keyId: string;
42
+ keyUri: KeyIdentifier;
43
+ publicKeyJwk: PublicKeyJwk;
44
+ }>;
45
+ /**
46
+ * Derives a ProtocolContext public key for a given DID and context ID,
47
+ * then returns a fully-formed EncryptionInput. Consolidates the repeated
48
+ * getEncryptionKeyInfo -> constructKeyDerivationPath -> derivePublicKey
49
+ * -> build EncryptionInput sequence.
50
+ *
51
+ * @param agent - The platform agent
52
+ * @param didUri - The DID URI to derive encryption key for
53
+ * @param contextId - The context ID
54
+ * @param dek - Data encryption key
55
+ * @param iv - Initialization vector
56
+ */
57
+ export declare function deriveContextEncryptionInput(agent: Web5PlatformAgent, didUri: string, contextId: string, dek: Uint8Array, iv: Uint8Array): Promise<{
58
+ encryptionInput: Omit<EncryptionInput, 'authenticationTag'>;
59
+ keyId: string;
60
+ keyUri: KeyIdentifier;
61
+ contextDerivationPath: string[];
62
+ }>;
63
+ /**
64
+ * Builds a KMS-backed JWE key unwrap callback. Used for both ProtocolPath
65
+ * and ProtocolContext decryption where the KMS holds the root private key.
66
+ *
67
+ * @param agent - The platform agent with access to the key manager
68
+ * @param keyId - The root key ID
69
+ * @param keyUri - The KMS key URI
70
+ * @param derivationScheme - The key derivation scheme
71
+ */
72
+ export declare function buildKmsDecryptCallback(agent: Web5PlatformAgent, keyId: string, keyUri: KeyIdentifier, derivationScheme: typeof KeyDerivationScheme.ProtocolPath | typeof KeyDerivationScheme.ProtocolContext): KeyDecrypter;
73
+ /**
74
+ * Constructs an EncryptionKeyDeriver callback for the SDK.
75
+ * The SDK calls derivePublicKey(path), the KMS performs HKDF + public key
76
+ * computation internally. The private key never leaves the KMS.
77
+ *
78
+ * Analogous to getSigner() for signing operations.
79
+ *
80
+ * @param agent - The platform agent
81
+ * @param didUri - The DID URI to create the key deriver for
82
+ * @returns An EncryptionKeyDeriver callback object
83
+ */
84
+ export declare function getEncryptionKeyDeriver(agent: Web5PlatformAgent, didUri: string): Promise<EncryptionKeyDeriver>;
85
+ /**
86
+ * Constructs a ProtocolPath KeyDecrypter.
87
+ *
88
+ * @param agent - The platform agent
89
+ * @param didUri - The DID URI to create the key decrypter for
90
+ * @returns A KeyDecrypter callback object
91
+ */
92
+ export declare function getKeyDecrypter(agent: Web5PlatformAgent, didUri: string): Promise<KeyDecrypter>;
93
+ /**
94
+ * Builds a KeyDecrypter from a context-derived private key.
95
+ * Uses the raw key directly (since it was shared with us via the key-delivery protocol).
96
+ *
97
+ * @param contextKey - The derived private key for the context
98
+ */
99
+ export declare function buildContextKeyDecrypter(contextKey: DerivedPrivateJwk): KeyDecrypter;
100
+ /**
101
+ * Resolves the appropriate KeyDecrypter for a record's encryption scheme.
102
+ * Handles both single-party (ProtocolPath) and multi-party (ProtocolContext).
103
+ *
104
+ * For ProtocolContext records:
105
+ * - Context creator: derives key directly from KMS
106
+ * - Participant: fetches contextKey via key-delivery protocol, caches it
107
+ *
108
+ * @param agent - The platform agent
109
+ * @param authorDid - The DID of the author attempting to decrypt
110
+ * @param recordsWrite - The records write message containing encryption info
111
+ * @param targetDid - The target DID (DWN owner), if known
112
+ * @param contextDerivedKeyCache - Cache for context-derived private keys
113
+ * @param fetchContextKeyRecordFn - Function to fetch context key records from key-delivery protocol
114
+ */
115
+ export declare function resolveKeyDecrypter(agent: Web5PlatformAgent, authorDid: string, recordsWrite: RecordsWriteMessage, targetDid: string | undefined, contextDerivedKeyCache: {
116
+ get(key: string): DerivedPrivateJwk | undefined;
117
+ set(key: string, value: DerivedPrivateJwk): void;
118
+ }, fetchContextKeyRecordFn: (params: {
119
+ ownerDid: string;
120
+ requesterDid: string;
121
+ sourceProtocol: string;
122
+ sourceContextId: string;
123
+ }) => Promise<DerivedPrivateJwk | undefined>): Promise<KeyDecrypter>;
124
+ /**
125
+ * Post-processes a DWN reply, auto-decrypting data if encryption is enabled.
126
+ * Delegates to the SDK's Records.decrypt() with the appropriate KeyDecrypter —
127
+ * resolveKeyDecrypter() selects between ProtocolPath and ProtocolContext schemes.
128
+ *
129
+ * @param request - The original DWN request
130
+ * @param reply - The DWN reply to process
131
+ * @param agent - The platform agent
132
+ * @param contextDerivedKeyCache - Cache for context-derived private keys
133
+ * @param fetchContextKeyRecordFn - Function to fetch context key records
134
+ */
135
+ export declare function maybeDecryptReply<T extends DwnInterface>(request: ProcessDwnRequest<T> | SendDwnRequest<T>, reply: DwnMessageReply[T], agent: Web5PlatformAgent, contextDerivedKeyCache: {
136
+ get(key: string): DerivedPrivateJwk | undefined;
137
+ set(key: string, value: DerivedPrivateJwk): void;
138
+ }, fetchContextKeyRecordFn: (params: {
139
+ ownerDid: string;
140
+ requesterDid: string;
141
+ sourceProtocol: string;
142
+ sourceContextId: string;
143
+ }) => Promise<DerivedPrivateJwk | undefined>): Promise<void>;
144
+ //# sourceMappingURL=dwn-encryption.d.ts.map
@@ -0,0 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B,sBAAsB,EAAE;IAAE,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,iBAAiB,GAAG,SAAS,CAAC;IAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,iBAAiB,GAAG,IAAI,CAAA;CAAE,EAC7H,uBAAuB,EAAE,CAAC,MAAM,EAAE;IAChC,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,MAAM,CAAC;CACzB,KAAK,OAAO,CAAC,iBAAiB,GAAG,SAAS,CAAC,GAC3C,OAAO,CAAC,YAAY,CAAC,CAuEvB;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,iBAAiB,CAAC,CAAC,SAAS,YAAY,EAC5D,OAAO,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,EACjD,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,EACzB,KAAK,EAAE,iBAAiB,EACxB,sBAAsB,EAAE;IAAE,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,iBAAiB,GAAG,SAAS,CAAC;IAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,iBAAiB,GAAG,IAAI,CAAA;CAAE,EAC7H,uBAAuB,EAAE,CAAC,MAAM,EAAE;IAChC,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,MAAM,CAAC;CACzB,KAAK,OAAO,CAAC,iBAAiB,GAAG,SAAS,CAAC,GAC3C,OAAO,CAAC,IAAI,CAAC,CA6Df"}
@@ -0,0 +1,112 @@
1
+ import type { PublicKeyJwk } from '@enbox/crypto';
2
+ import type { DerivedPrivateJwk } from '@enbox/dwn-sdk-js';
3
+ import type { Web5PlatformAgent } from './types/agent.js';
4
+ import type { DwnMessage, DwnMessageReply, ProcessDwnRequest } from './types/dwn.js';
5
+ import { DwnInterface } from './types/dwn.js';
6
+ /**
7
+ * Parameters for writeContextKeyRecord.
8
+ */
9
+ export type WriteContextKeyParams = {
10
+ tenantDid: string;
11
+ recipientDid: string;
12
+ contextKeyData: DerivedPrivateJwk;
13
+ sourceProtocol: string;
14
+ sourceContextId: string;
15
+ recipientKeyDeliveryPublicKey?: {
16
+ rootKeyId: string;
17
+ publicKeyJwk: PublicKeyJwk;
18
+ };
19
+ };
20
+ /**
21
+ * Parameters for fetchContextKeyRecord.
22
+ */
23
+ export type FetchContextKeyParams = {
24
+ ownerDid: string;
25
+ requesterDid: string;
26
+ sourceProtocol: string;
27
+ sourceContextId: string;
28
+ };
29
+ /** Callback type for processRequest, used by key-delivery functions. */
30
+ type ProcessRequestFn = <T extends DwnInterface>(request: ProcessDwnRequest<T>) => Promise<{
31
+ reply: DwnMessageReply[T];
32
+ message?: DwnMessage[T];
33
+ messageCid: string;
34
+ }>;
35
+ /**
36
+ * Ensures the key delivery protocol is installed on the given tenant's DWN,
37
+ * with `$encryption` keys injected. Uses the same lazy initialization pattern
38
+ * as `DwnDataStore.initialize()`.
39
+ *
40
+ * @param agent - The platform agent
41
+ * @param tenantDid - The DID of the DWN owner
42
+ * @param processRequest - The agent's processRequest method (bound)
43
+ * @param getProtocolDefinition - Function to get a protocol definition
44
+ * @param installedCache - Cache for installation status
45
+ */
46
+ export declare function ensureKeyDeliveryProtocol(agent: Web5PlatformAgent, tenantDid: string, processRequest: ProcessRequestFn, getProtocolDefinition: (tenantDid: string, protocolUri: string) => Promise<any>, installedCache: {
47
+ get(key: string): boolean | undefined;
48
+ set(key: string, value: boolean): void;
49
+ delete(key: string): void;
50
+ }, protocolDefinitionCache: {
51
+ delete(key: string): void;
52
+ }): Promise<void>;
53
+ /**
54
+ * Writes a `contextKey` record to the owner's DWN, delivering an encrypted
55
+ * context key to a participant.
56
+ *
57
+ * The payload is encrypted to the **recipient's** ProtocolPath-derived public
58
+ * key on the key-delivery protocol, so only the recipient can decrypt it.
59
+ *
60
+ * @param agent - The platform agent
61
+ * @param params - The write parameters
62
+ * @param processRequest - The agent's processRequest method (bound)
63
+ * @param ensureProtocol - Function to ensure key delivery protocol is installed
64
+ * @param eagerSend - Function to eagerly send the record to the remote DWN
65
+ * @returns The recordId of the written contextKey record
66
+ */
67
+ export declare function writeContextKeyRecord(agent: Web5PlatformAgent, params: WriteContextKeyParams, processRequest: ProcessRequestFn, ensureProtocol: (tenantDid: string) => Promise<void>, eagerSend: (tenantDid: string, message: DwnMessage[DwnInterface.RecordsWrite]) => Promise<void>): Promise<string>;
68
+ /**
69
+ * Eagerly sends a contextKey record to the tenant's remote DWN.
70
+ * This is best-effort — sync guarantees eventual consistency regardless.
71
+ *
72
+ * @param agent - The platform agent
73
+ * @param tenantDid - The DWN owner's DID
74
+ * @param contextKeyMessage - The context key message to send
75
+ * @param getDwnMessage - Function to read a full message from local DWN
76
+ * @param sendDwnRpcRequest - Function to send a DWN RPC request
77
+ */
78
+ export declare function eagerSendContextKeyRecord(agent: Web5PlatformAgent, tenantDid: string, contextKeyMessage: DwnMessage[DwnInterface.RecordsWrite], getDwnMessage: (params: {
79
+ author: string;
80
+ messageType: DwnInterface;
81
+ messageCid: string;
82
+ }) => Promise<{
83
+ message: any;
84
+ data?: Blob;
85
+ }>, sendDwnRpcRequest: (params: {
86
+ targetDid: string;
87
+ dwnEndpointUrls: string[];
88
+ message: any;
89
+ data?: Blob;
90
+ }) => Promise<any>): Promise<void>;
91
+ /**
92
+ * Fetches and decrypts a `contextKey` record from a DWN, returning the
93
+ * `DerivedPrivateJwk` payload.
94
+ *
95
+ * Supports both local reads (tenant queries own DWN) and remote reads
96
+ * (participant queries the context owner's DWN).
97
+ *
98
+ * @param agent - The platform agent
99
+ * @param params - The fetch parameters
100
+ * @param processRequest - The agent's processRequest method (bound)
101
+ * @param getSigner - Function to get a signer for a DID
102
+ * @param sendDwnRpcRequest - Function to send a DWN RPC request
103
+ * @returns The decrypted `DerivedPrivateJwk`, or `undefined` if no matching record found
104
+ */
105
+ export declare function fetchContextKeyRecord(agent: Web5PlatformAgent, params: FetchContextKeyParams, processRequest: ProcessRequestFn, getSigner: (author: string) => Promise<any>, sendDwnRpcRequest: (params: {
106
+ targetDid: string;
107
+ dwnEndpointUrls: string[];
108
+ message: any;
109
+ data?: Blob;
110
+ }) => Promise<any>): Promise<DerivedPrivateJwk | undefined>;
111
+ export {};
112
+ //# sourceMappingURL=dwn-key-delivery.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dwn-key-delivery.d.ts","sourceRoot":"","sources":["../../src/dwn-key-delivery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,KAAK,EACV,iBAAiB,EAIlB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,KAAK,EACV,UAAU,EACV,eAAe,EACf,iBAAiB,EAClB,MAAM,gBAAgB,CAAC;AAcxB,OAAO,EAAE,YAAY,EAA0B,MAAM,gBAAgB,CAAC;AAEtE;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,iBAAiB,CAAC;IAClC,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,MAAM,CAAC;IACxB,6BAA6B,CAAC,EAAE;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,YAAY,CAAA;KAAE,CAAC;CACnF,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF,wEAAwE;AACxE,KAAK,gBA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AK,EAAE,iBAAiB,EACxB,SAAS,EAAE,MAAM,EACjB,iBAAiB,EAAE,UAAU,CAAC,YAAY,CAAC,YAAY,CAAC,EACxD,aAAa,EAAE,CAAC,MAAM,EAAE;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,YAAY,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,KAAK,OAAO,CAAC;IAAE,OAAO,EAAE,GAAG,CAAC;IAAC,IAAI,CAAC,EAAE,IAAI,CAAA;CAAE,CAAC,EACpI,iBAAiB,EAAE,CAAC,MAAM,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,eAAe,EAAE,MAAM,EAAE,CAAC;IAAC,OAAO,EAAE,GAAG,CAAC;IAAC,IAAI,CAAC,EAAE,IAAI,CAAA;CAAE,KAAK,OAAO,CAAC,GAAG,CAAC,GACvH,OAAO,CAAC,IAAI,CAAC,CA0Bf;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,qBAAqB,CACzC,KAAK,EAAE,iBAAiB,EACxB,MAAM,EAAE,qBAAqB,EAC7B,cAAc,EAAE,gBAAgB,EAChC,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,CAAC,EAC3C,iBAAiB,EAAE,CAAC,MAAM,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,eAAe,EAAE,MAAM,EAAE,CAAC;IAAC,OAAO,EAAE,GAAG,CAAC;IAAC,IAAI,CAAC,EAAE,IAAI,CAAA;CAAE,KAAK,OAAO,CAAC,GAAG,CAAC,GACvH,OAAO,CAAC,iBAAiB,GAAG,SAAS,CAAC,CA6FxC"}
@@ -0,0 +1,33 @@
1
+ import type { KeyIdentifier } from '@enbox/crypto';
2
+ import type { Dwn, RecordsWriteMessage } from '@enbox/dwn-sdk-js';
3
+ import type { DwnSigner } from './types/dwn.js';
4
+ import type { Web5PlatformAgent } from './types/agent.js';
5
+ /**
6
+ * Reactively upgrades an externally-authored root record that has only
7
+ * ProtocolPath encryption by appending a ProtocolContext recipient entry.
8
+ *
9
+ * After the upgrade, both the owner (ProtocolPath) and context key holders —
10
+ * including the external author (ProtocolContext) — can decrypt the record.
11
+ *
12
+ * Steps:
13
+ * 1. Decrypt the DEK using the owner's ProtocolPath-derived private key
14
+ * 2. Derive the context public key from the owner's #enc key
15
+ * 3. ECIES-encrypt the same DEK to the context public key
16
+ * 4. Append the ProtocolContext recipient entry (using PR 0b append mode)
17
+ * 5. Re-sign the record as owner
18
+ *
19
+ * @param agent - The platform agent
20
+ * @param tenantDid - The DWN owner's DID
21
+ * @param recordsWrite - The RecordsWrite message to upgrade
22
+ * @param dwn - The DWN instance
23
+ * @param getSigner - Function to get a DWN signer
24
+ * @param contextKeyCache - Cache for context key info
25
+ */
26
+ export declare function upgradeExternalRootRecord(agent: Web5PlatformAgent, tenantDid: string, recordsWrite: RecordsWriteMessage, dwn: Dwn, getSigner: (author: string) => Promise<DwnSigner>, contextKeyCache: {
27
+ set(key: string, value: {
28
+ keyId: string;
29
+ keyUri: KeyIdentifier;
30
+ contextDerivationPath: string[];
31
+ }): void;
32
+ }): Promise<void>;
33
+ //# sourceMappingURL=dwn-record-upgrade.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dwn-record-upgrade.d.ts","sourceRoot":"","sources":["../../src/dwn-record-upgrade.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,KAAK,EACV,GAAG,EAIH,mBAAmB,EACpB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAY1D;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,yBAAyB,CAC7C,KAAK,EAAE,iBAAiB,EACxB,SAAS,EAAE,MAAM,EACjB,YAAY,EAAE,mBAAmB,EACjC,GAAG,EAAE,GAAG,EACR,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,SAAS,CAAC,EACjD,eAAe,EAAE;IAAE,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,aAAa,CAAC;QAAC,qBAAqB,EAAE,MAAM,EAAE,CAAA;KAAE,GAAG,IAAI,CAAA;CAAE,GAC5H,OAAO,CAAC,IAAI,CAAC,CAmHf"}
@@ -0,0 +1,9 @@
1
+ import type { GenericMessage } from '@enbox/dwn-sdk-js';
2
+ import type { DwnMessage, DwnMessagesPermissionScope, DwnPermissionScope, DwnRecordsInterfaces, DwnRecordsPermissionScope, ProcessDwnRequest } from './types/dwn.js';
3
+ import { DwnInterface } from './types/dwn.js';
4
+ export declare function isDwnRequest<T extends DwnInterface>(dwnRequest: ProcessDwnRequest<DwnInterface>, messageType: T): dwnRequest is ProcessDwnRequest<T>;
5
+ export declare function isDwnMessage<T extends DwnInterface>(messageType: T, message: GenericMessage): message is DwnMessage[T];
6
+ export declare function isRecordsType(messageType: DwnInterface): messageType is DwnRecordsInterfaces;
7
+ export declare function isRecordPermissionScope(scope: DwnPermissionScope): scope is DwnRecordsPermissionScope;
8
+ export declare function isMessagesPermissionScope(scope: DwnPermissionScope): scope is DwnMessagesPermissionScope;
9
+ //# sourceMappingURL=dwn-type-guards.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dwn-type-guards.d.ts","sourceRoot":"","sources":["../../src/dwn-type-guards.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,KAAK,EACV,UAAU,EACV,0BAA0B,EAC1B,kBAAkB,EAClB,oBAAoB,EACpB,yBAAyB,EACzB,iBAAiB,EAClB,MAAM,gBAAgB,CAAC;AAIxB,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,wBAAgB,YAAY,CAAC,CAAC,SAAS,YAAY,EACjD,UAAU,EAAE,iBAAiB,CAAC,YAAY,CAAC,EAAE,WAAW,EAAE,CAAC,GAC1D,UAAU,IAAI,iBAAiB,CAAC,CAAC,CAAC,CAEpC;AAED,wBAAgB,YAAY,CAAC,CAAC,SAAS,YAAY,EACjD,WAAW,EAAE,CAAC,EAAE,OAAO,EAAE,cAAc,GACtC,OAAO,IAAI,UAAU,CAAC,CAAC,CAAC,CAG1B;AAED,wBAAgB,aAAa,CAAC,WAAW,EAAE,YAAY,GAAG,WAAW,IAAI,oBAAoB,CAM5F;AAED,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,kBAAkB,GAAG,KAAK,IAAI,yBAAyB,CAErG;AAED,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,kBAAkB,GAAG,KAAK,IAAI,0BAA0B,CAExG"}