@enbox/agent 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/dist/browser.mjs +9 -9
  2. package/dist/browser.mjs.map +4 -4
  3. package/dist/esm/agent-did-resolver-cache.js.map +1 -1
  4. package/dist/esm/anonymous-dwn-api.js +1 -1
  5. package/dist/esm/bearer-identity.js +1 -1
  6. package/dist/esm/connect.js +3 -3
  7. package/dist/esm/connect.js.map +1 -1
  8. package/dist/esm/did-api.js +3 -3
  9. package/dist/esm/did-api.js.map +1 -1
  10. package/dist/esm/dwn-api.js +150 -10
  11. package/dist/esm/dwn-api.js.map +1 -1
  12. package/dist/esm/dwn-discovery-file.js +244 -0
  13. package/dist/esm/dwn-discovery-file.js.map +1 -0
  14. package/dist/esm/dwn-discovery-payload.js +253 -0
  15. package/dist/esm/dwn-discovery-payload.js.map +1 -0
  16. package/dist/esm/dwn-encryption.js.map +1 -1
  17. package/dist/esm/dwn-key-delivery.js +6 -5
  18. package/dist/esm/dwn-key-delivery.js.map +1 -1
  19. package/dist/esm/dwn-protocol-cache.js +6 -7
  20. package/dist/esm/dwn-protocol-cache.js.map +1 -1
  21. package/dist/esm/dwn-record-upgrade.js.map +1 -1
  22. package/dist/esm/{web5-user-agent.js → enbox-user-agent.js} +18 -9
  23. package/dist/esm/enbox-user-agent.js.map +1 -0
  24. package/dist/esm/identity-api.js +4 -5
  25. package/dist/esm/identity-api.js.map +1 -1
  26. package/dist/esm/index.js +4 -1
  27. package/dist/esm/index.js.map +1 -1
  28. package/dist/esm/local-dwn.js +197 -0
  29. package/dist/esm/local-dwn.js.map +1 -0
  30. package/dist/esm/local-key-manager.js +2 -2
  31. package/dist/esm/local-key-manager.js.map +1 -1
  32. package/dist/esm/oidc.js +11 -11
  33. package/dist/esm/oidc.js.map +1 -1
  34. package/dist/esm/permissions-api.js.map +1 -1
  35. package/dist/esm/store-data.js.map +1 -1
  36. package/dist/esm/sync-api.js +2 -2
  37. package/dist/esm/sync-api.js.map +1 -1
  38. package/dist/esm/sync-engine-level.js +3 -4
  39. package/dist/esm/sync-engine-level.js.map +1 -1
  40. package/dist/esm/test-harness.js +5 -4
  41. package/dist/esm/test-harness.js.map +1 -1
  42. package/dist/esm/utils-internal.js +2 -2
  43. package/dist/types/agent-did-resolver-cache.d.ts +7 -7
  44. package/dist/types/agent-did-resolver-cache.d.ts.map +1 -1
  45. package/dist/types/anonymous-dwn-api.d.ts +3 -3
  46. package/dist/types/anonymous-dwn-api.d.ts.map +1 -1
  47. package/dist/types/bearer-identity.d.ts +1 -1
  48. package/dist/types/connect.d.ts +8 -8
  49. package/dist/types/connect.d.ts.map +1 -1
  50. package/dist/types/did-api.d.ts +12 -11
  51. package/dist/types/did-api.d.ts.map +1 -1
  52. package/dist/types/dwn-api.d.ts +58 -11
  53. package/dist/types/dwn-api.d.ts.map +1 -1
  54. package/dist/types/dwn-discovery-file.d.ts +122 -0
  55. package/dist/types/dwn-discovery-file.d.ts.map +1 -0
  56. package/dist/types/dwn-discovery-payload.d.ts +105 -0
  57. package/dist/types/dwn-discovery-payload.d.ts.map +1 -0
  58. package/dist/types/dwn-encryption.d.ts +8 -8
  59. package/dist/types/dwn-encryption.d.ts.map +1 -1
  60. package/dist/types/dwn-key-delivery.d.ts +9 -7
  61. package/dist/types/dwn-key-delivery.d.ts.map +1 -1
  62. package/dist/types/dwn-protocol-cache.d.ts +6 -5
  63. package/dist/types/dwn-protocol-cache.d.ts.map +1 -1
  64. package/dist/types/dwn-record-upgrade.d.ts +2 -2
  65. package/dist/types/dwn-record-upgrade.d.ts.map +1 -1
  66. package/dist/types/{web5-user-agent.d.ts → enbox-user-agent.d.ts} +21 -13
  67. package/dist/types/enbox-user-agent.d.ts.map +1 -0
  68. package/dist/types/identity-api.d.ts +10 -10
  69. package/dist/types/identity-api.d.ts.map +1 -1
  70. package/dist/types/index.d.ts +4 -1
  71. package/dist/types/index.d.ts.map +1 -1
  72. package/dist/types/local-dwn.d.ts +121 -0
  73. package/dist/types/local-dwn.d.ts.map +1 -0
  74. package/dist/types/local-key-manager.d.ts +9 -9
  75. package/dist/types/local-key-manager.d.ts.map +1 -1
  76. package/dist/types/oidc.d.ts +23 -19
  77. package/dist/types/oidc.d.ts.map +1 -1
  78. package/dist/types/permissions-api.d.ts +4 -4
  79. package/dist/types/permissions-api.d.ts.map +1 -1
  80. package/dist/types/store-data.d.ts +3 -3
  81. package/dist/types/store-data.d.ts.map +1 -1
  82. package/dist/types/store-did.d.ts +2 -2
  83. package/dist/types/store-did.d.ts.map +1 -1
  84. package/dist/types/store-identity.d.ts +2 -2
  85. package/dist/types/store-identity.d.ts.map +1 -1
  86. package/dist/types/store-key.d.ts +2 -2
  87. package/dist/types/store-key.d.ts.map +1 -1
  88. package/dist/types/sync-api.d.ts +9 -9
  89. package/dist/types/sync-api.d.ts.map +1 -1
  90. package/dist/types/sync-engine-level.d.ts +9 -9
  91. package/dist/types/sync-engine-level.d.ts.map +1 -1
  92. package/dist/types/sync-messages.d.ts +5 -5
  93. package/dist/types/sync-messages.d.ts.map +1 -1
  94. package/dist/types/test-harness.d.ts +4 -4
  95. package/dist/types/test-harness.d.ts.map +1 -1
  96. package/dist/types/types/agent.d.ts +24 -19
  97. package/dist/types/types/agent.d.ts.map +1 -1
  98. package/dist/types/types/identity.d.ts +1 -1
  99. package/dist/types/types/key-manager.d.ts +2 -2
  100. package/dist/types/types/key-manager.d.ts.map +1 -1
  101. package/dist/types/types/sync.d.ts +2 -2
  102. package/dist/types/types/sync.d.ts.map +1 -1
  103. package/dist/types/utils-internal.d.ts +4 -4
  104. package/dist/types/utils-internal.d.ts.map +1 -1
  105. package/package.json +6 -6
  106. package/src/agent-did-resolver-cache.ts +8 -8
  107. package/src/anonymous-dwn-api.ts +4 -4
  108. package/src/bearer-identity.ts +1 -1
  109. package/src/connect.ts +12 -12
  110. package/src/did-api.ts +13 -11
  111. package/src/dwn-api.ts +196 -16
  112. package/src/dwn-discovery-file.ts +305 -0
  113. package/src/dwn-discovery-payload.ts +308 -0
  114. package/src/dwn-encryption.ts +8 -8
  115. package/src/dwn-key-delivery.ts +11 -8
  116. package/src/dwn-protocol-cache.ts +9 -8
  117. package/src/dwn-record-upgrade.ts +2 -2
  118. package/src/{web5-user-agent.ts → enbox-user-agent.ts} +39 -19
  119. package/src/identity-api.ts +12 -13
  120. package/src/index.ts +4 -1
  121. package/src/local-dwn.ts +207 -0
  122. package/src/local-key-manager.ts +10 -10
  123. package/src/oidc.ts +40 -30
  124. package/src/permissions-api.ts +5 -5
  125. package/src/store-data.ts +7 -7
  126. package/src/store-did.ts +2 -2
  127. package/src/store-identity.ts +2 -2
  128. package/src/store-key.ts +2 -2
  129. package/src/sync-api.ts +10 -10
  130. package/src/sync-engine-level.ts +13 -14
  131. package/src/sync-messages.ts +5 -5
  132. package/src/test-harness.ts +11 -10
  133. package/src/types/agent.ts +31 -20
  134. package/src/types/identity.ts +1 -1
  135. package/src/types/key-manager.ts +2 -2
  136. package/src/types/sync.ts +2 -2
  137. package/src/utils-internal.ts +4 -4
  138. package/dist/esm/web5-user-agent.js.map +0 -1
  139. package/dist/types/web5-user-agent.d.ts.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"utils-internal.d.ts","sourceRoot":"","sources":["../../src/utils-internal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,aAAa,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAElH,OAAO,EAAiC,eAAe,EAAE,MAAM,eAAe,CAAC;AAE/E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAE1D;;;GAGG;AAEH;;;GAGG;AACH,eAAO,MAAM,gBAAgB,MAAM,CAAC;AAEpC,qBAAa,yBAA0B,SAAQ,eAAe;IAC5D,OAAO,CAAC,eAAe,CAA0B;IACjD,OAAO,CAAC,aAAa,CAAkC;;IAQ1C,iBAAiB,CAAC,EAAE,WAAW,EAAE,EAAE;QAAE,WAAW,EAAE,GAAG,EAAE,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAqBzE,SAAS,CAAC,EAAE,MAAM,EAAE,EAC/B,kBAAkB,GACjB,OAAO,CAAC,GAAG,CAAC;IAYF,WAAW,CAAC,OAAO,EAAE;QAChC,SAAS,EAAE,SAAS,GAAG,WAAW,GAAG,WAAW,GAAG,QAAQ,CAAA;KAC5D,GAAG,OAAO,CAAC,aAAa,CAAC;IAYb,YAAY,CAAC,EAAE,MAAM,EAAE,EAChC,qBAAqB,GACtB,OAAO,CAAC,GAAG,CAAC;IAeF,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,EAChC,aAAa,GACZ,OAAO,CAAC,UAAU,CAAC;CAcvB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,kBAAkB,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE;IAClE,KAAK,EAAE,iBAAiB,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,GAAG,OAAO,CAAC,MAAM,CAAC,CAuBlB"}
1
+ {"version":3,"file":"utils-internal.d.ts","sourceRoot":"","sources":["../../src/utils-internal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,aAAa,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAElH,OAAO,EAAiC,eAAe,EAAE,MAAM,eAAe,CAAC;AAE/E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAE3D;;;GAGG;AAEH;;;GAGG;AACH,eAAO,MAAM,gBAAgB,MAAM,CAAC;AAEpC,qBAAa,yBAA0B,SAAQ,eAAe;IAC5D,OAAO,CAAC,eAAe,CAA0B;IACjD,OAAO,CAAC,aAAa,CAAkC;;IAQ1C,iBAAiB,CAAC,EAAE,WAAW,EAAE,EAAE;QAAE,WAAW,EAAE,GAAG,EAAE,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAqBzE,SAAS,CAAC,EAAE,MAAM,EAAE,EAC/B,kBAAkB,GACjB,OAAO,CAAC,GAAG,CAAC;IAYF,WAAW,CAAC,OAAO,EAAE;QAChC,SAAS,EAAE,SAAS,GAAG,WAAW,GAAG,WAAW,GAAG,QAAQ,CAAA;KAC5D,GAAG,OAAO,CAAC,aAAa,CAAC;IAYb,YAAY,CAAC,EAAE,MAAM,EAAE,EAChC,qBAAqB,GACtB,OAAO,CAAC,GAAG,CAAC;IAeF,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,EAChC,aAAa,GACZ,OAAO,CAAC,UAAU,CAAC;CAcvB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,kBAAkB,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE;IAClE,KAAK,EAAE,kBAAkB,CAAC;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,GAAG,OAAO,CAAC,MAAM,CAAC,CAuBlB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enbox/agent",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "main": "./dist/esm/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -71,11 +71,11 @@
71
71
  },
72
72
  "dependencies": {
73
73
  "@scure/bip39": "1.2.2",
74
- "@enbox/dwn-clients": "0.0.8",
75
- "@enbox/dwn-sdk-js": "0.1.1",
76
- "@enbox/common": "0.0.6",
77
- "@enbox/crypto": "0.0.7",
78
- "@enbox/dids": "0.0.8",
74
+ "@enbox/dwn-clients": "0.1.0",
75
+ "@enbox/dwn-sdk-js": "0.1.2",
76
+ "@enbox/common": "0.0.7",
77
+ "@enbox/crypto": "0.0.8",
78
+ "@enbox/dids": "0.0.9",
79
79
  "abstract-level": "1.0.4",
80
80
  "ed25519-keygen": "0.4.11",
81
81
  "level": "8.0.0",
@@ -1,6 +1,6 @@
1
1
  import type { DidResolutionResult, DidResolverCache, DidResolverCacheLevelParams } from '@enbox/dids';
2
2
 
3
- import type { Web5PlatformAgent } from './types/agent.js';
3
+ import type { EnboxPlatformAgent } from './types/agent.js';
4
4
 
5
5
  import { DidResolverCacheLevel } from '@enbox/dids';
6
6
  import { logger } from '@enbox/common';
@@ -13,29 +13,29 @@ import { logger } from '@enbox/common';
13
13
  export class AgentDidResolverCache extends DidResolverCacheLevel implements DidResolverCache {
14
14
 
15
15
  /**
16
- * Holds the instance of a `Web5PlatformAgent` that represents the current execution context for
17
- * the `AgentDidApi`. This agent is used to interact with other Web5 agent components. It's vital
18
- * to ensure this instance is set to correctly contextualize operations within the broader Web5
16
+ * Holds the instance of a `EnboxPlatformAgent` that represents the current execution context for
17
+ * the `AgentDidApi`. This agent is used to interact with other Enbox agent components. It's vital
18
+ * to ensure this instance is set to correctly contextualize operations within the broader Enbox
19
19
  * Agent framework.
20
20
  */
21
- private _agent?: Web5PlatformAgent;
21
+ private _agent?: EnboxPlatformAgent;
22
22
 
23
23
  /** A map of DIDs that are currently in-flight. This helps avoid going into an infinite loop */
24
24
  private _resolving: Map<string, boolean> = new Map();
25
25
 
26
- constructor({ agent, db, location, ttl }: DidResolverCacheLevelParams & { agent?: Web5PlatformAgent }) {
26
+ constructor({ agent, db, location, ttl }: DidResolverCacheLevelParams & { agent?: EnboxPlatformAgent }) {
27
27
  super ({ db, location, ttl });
28
28
  this._agent = agent;
29
29
  }
30
30
 
31
- get agent(): Web5PlatformAgent {
31
+ get agent(): EnboxPlatformAgent {
32
32
  if (!this._agent) {
33
33
  throw new Error('Agent not initialized');
34
34
  }
35
35
  return this._agent;
36
36
  }
37
37
 
38
- set agent(agent: Web5PlatformAgent) {
38
+ set agent(agent: EnboxPlatformAgent) {
39
39
  this._agent = agent;
40
40
  }
41
41
 
@@ -13,7 +13,7 @@ import type {
13
13
  RecordsSubscribeReply,
14
14
  SubscriptionListener,
15
15
  } from '@enbox/dwn-sdk-js';
16
- import type { DwnRpcRequest, Web5Rpc } from '@enbox/dwn-clients';
16
+ import type { DwnRpcRequest, EnboxRpc } from '@enbox/dwn-clients';
17
17
 
18
18
  import { ProtocolsQuery, RecordsCount, RecordsQuery, RecordsRead, RecordsSubscribe } from '@enbox/dwn-sdk-js';
19
19
 
@@ -26,7 +26,7 @@ export type AnonymousDwnApiParams = {
26
26
  /** A DID URL dereferencer for resolving target DID service endpoints. */
27
27
  didResolver: DidUrlDereferencer;
28
28
  /** An RPC client for sending messages to remote DWNs. */
29
- rpcClient: Web5Rpc;
29
+ rpcClient: EnboxRpc;
30
30
  };
31
31
 
32
32
  /**
@@ -89,7 +89,7 @@ export type AnonymousProtocolsQueryParams = {
89
89
  * @example
90
90
  * ```ts
91
91
  * const resolver = new UniversalResolver({ didResolvers: [DidDht, DidJwk] });
92
- * const rpcClient = new Web5RpcClient();
92
+ * const rpcClient = new EnboxRpcClient();
93
93
  * const anonymousDwn = new AnonymousDwnApi({ didResolver: resolver, rpcClient });
94
94
  *
95
95
  * const reply = await anonymousDwn.recordsQuery('did:dht:alice...', {
@@ -99,7 +99,7 @@ export type AnonymousProtocolsQueryParams = {
99
99
  */
100
100
  export class AnonymousDwnApi {
101
101
  private _didResolver: DidUrlDereferencer;
102
- private _rpcClient: Web5Rpc;
102
+ private _rpcClient: EnboxRpc;
103
103
 
104
104
  constructor({ didResolver, rpcClient }: AnonymousDwnApiParams) {
105
105
  this._didResolver = didResolver;
@@ -2,7 +2,7 @@ import type { BearerDid } from '@enbox/dids';
2
2
  import type { IdentityMetadata, PortableIdentity } from './types/identity.js';
3
3
 
4
4
  /**
5
- * Represents a Web5 Identity with its DID and metadata.
5
+ * Represents an Enbox Identity with its DID and metadata.
6
6
  */
7
7
  export class BearerIdentity {
8
8
  /** {@inheritDoc BearerDid} */
package/src/connect.ts CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  import type { PushedAuthResponse } from './oidc.js';
3
- import type { DwnPermissionScope, DwnProtocolDefinition, Web5ConnectAuthResponse } from './index.js';
3
+ import type { DwnPermissionScope, DwnProtocolDefinition, EnboxConnectAuthResponse } from './index.js';
4
4
 
5
5
  import { CryptoUtils } from '@enbox/crypto';
6
6
  import { DidJwk } from '@enbox/dids';
@@ -21,8 +21,8 @@ async function initClient({
21
21
  onWalletUriReady,
22
22
  validatePin,
23
23
  }: WalletConnectOptions): Promise<{
24
- delegateGrants: Web5ConnectAuthResponse['delegateGrants'];
25
- delegatePortableDid: Web5ConnectAuthResponse['delegatePortableDid'];
24
+ delegateGrants: EnboxConnectAuthResponse['delegateGrants'];
25
+ delegatePortableDid: EnboxConnectAuthResponse['delegatePortableDid'];
26
26
  connectedDid: string;
27
27
  } | undefined> {
28
28
  // ephemeral client did for ECDH, signing, verification
@@ -93,8 +93,8 @@ async function initClient({
93
93
 
94
94
  const parData: PushedAuthResponse = await parResponse.json();
95
95
 
96
- // a deeplink to a web5 compatible wallet. if the wallet scans this link it should receive
97
- // a route to its web5 connect provider flow and the params of where to fetch the auth request.
96
+ // a deeplink to a compatible wallet. if the wallet scans this link it should receive
97
+ // a route to its Connect provider flow and the params of where to fetch the auth request.
98
98
  logger.log(`Wallet URI: ${walletUri}`);
99
99
  const generatedWalletUri = new URL(walletUri);
100
100
  generatedWalletUri.searchParams.set('request_uri', parData.request_uri);
@@ -112,7 +112,7 @@ async function initClient({
112
112
  tokenParam : request.state,
113
113
  });
114
114
 
115
- // subscribe to receiving a response from the wallet with default TTL. receive ciphertext of {@link Web5ConnectAuthResponse}
115
+ // subscribe to receiving a response from the wallet with default TTL. receive ciphertext of {@link EnboxConnectAuthResponse}
116
116
  const authResponse = await pollWithTtl(() => fetch(tokenUrl, { signal: AbortSignal.timeout(30_000) }));
117
117
 
118
118
  if (authResponse) {
@@ -123,7 +123,7 @@ async function initClient({
123
123
  const jwt = await Oidc.decryptAuthResponse(clientDid, jwe, pin);
124
124
  const verifiedAuthResponse = (await Oidc.verifyJwt({
125
125
  jwt,
126
- })) as Web5ConnectAuthResponse;
126
+ })) as EnboxConnectAuthResponse;
127
127
 
128
128
  return {
129
129
  delegateGrants : verifiedAuthResponse.delegateGrants,
@@ -159,20 +159,20 @@ export type WalletConnectOptions = {
159
159
  permissionRequests: ConnectPermissionRequest[];
160
160
 
161
161
  /**
162
- * The Web5 API provides a URI to the wallet based on the `walletUri` plus a query params payload valid for 5 minutes.
162
+ * The Connect API provides a URI to the wallet based on the `walletUri` plus a query params payload valid for 5 minutes.
163
163
  * The link can either be used as a deep link on the same device or a QR code for cross device or both.
164
164
  * The query params are `{ request_uri: string; encryption_key: string; }`
165
165
  * The wallet will use the `request_uri to contact the intermediary server's `authorize` endpoint
166
- * and pull down the {@link Web5ConnectAuthRequest} and use the `encryption_key` to decrypt it.
166
+ * and pull down the {@link EnboxConnectAuthRequest} and use the `encryption_key` to decrypt it.
167
167
  *
168
- * @param uri - The URI returned by the web5 connect API to be passed to a provider.
168
+ * @param uri - The URI returned by the Connect API to be passed to a provider.
169
169
  */
170
170
  onWalletUriReady: (uri: string) => void;
171
171
 
172
172
  /**
173
173
  * Function that must be provided to submit the pin entered by the user on the client.
174
- * The pin is used to decrypt the {@link Web5ConnectAuthResponse} that was retrieved from the
175
- * token endpoint by the client inside of web5 connect.
174
+ * The pin is used to decrypt the {@link EnboxConnectAuthResponse} that was retrieved from the
175
+ * token endpoint by the client inside of Connect.
176
176
  *
177
177
  * @returns A promise that resolves to the PIN as a string.
178
178
  */
package/src/did-api.ts CHANGED
@@ -8,6 +8,7 @@ import type {
8
8
  DidResolutionResult,
9
9
  DidResolverCache,
10
10
  DidVerificationMethod,
11
+ DidWebCreateOptions,
11
12
  PortableDid,
12
13
  } from '@enbox/dids';
13
14
 
@@ -15,7 +16,7 @@ import { BearerDid, Did, DidDht, UniversalResolver } from '@enbox/dids';
15
16
 
16
17
  import type { AgentDataStore } from './store-data.js';
17
18
  import type { AgentKeyManager } from './types/key-manager.js';
18
- import type { ResponseStatus, Web5PlatformAgent } from './types/agent.js';
19
+ import type { EnboxPlatformAgent, ResponseStatus } from './types/agent.js';
19
20
 
20
21
  import { AgentDidResolverCache } from './agent-did-resolver-cache.js';
21
22
  import { canonicalize } from '@enbox/crypto';
@@ -77,12 +78,13 @@ export interface DidCreateParams<
77
78
  export interface DidMethodCreateOptions<TKeyManager> {
78
79
  dht: DidDhtCreateOptions<TKeyManager>;
79
80
  jwk: DidJwkCreateOptions<TKeyManager>;
81
+ web: DidWebCreateOptions<TKeyManager>;
80
82
  }
81
83
 
82
84
  export interface DidApiParams {
83
85
  didMethods: DidMethodApi[];
84
86
 
85
- agent?: Web5PlatformAgent;
87
+ agent?: EnboxPlatformAgent;
86
88
 
87
89
  /**
88
90
  * An optional `DidResolverCache` instance used for caching resolved DID documents.
@@ -105,19 +107,19 @@ export function isDidRequest<T extends DidInterface>(
105
107
  }
106
108
 
107
109
  /**
108
- * This API is used to manage and interact with DIDs within the Web5 Agent framework.
110
+ * This API is used to manage and interact with DIDs within the Enbox Agent framework.
109
111
  *
110
112
  * If a DWN Data Store is used, the DID information is stored under DID's own tenant by default.
111
113
  * If a tenant property is passed, that tenant will be used to store the DID information.
112
114
  */
113
115
  export class AgentDidApi<TKeyManager extends AgentKeyManager = AgentKeyManager> extends UniversalResolver {
114
116
  /**
115
- * Holds the instance of a `Web5PlatformAgent` that represents the current execution context for
116
- * the `AgentDidApi`. This agent is used to interact with other Web5 agent components. It's vital
117
- * to ensure this instance is set to correctly contextualize operations within the broader Web5
117
+ * Holds the instance of a `EnboxPlatformAgent` that represents the current execution context for
118
+ * the `AgentDidApi`. This agent is used to interact with other Enbox agent components. It's vital
119
+ * to ensure this instance is set to correctly contextualize operations within the broader Enbox
118
120
  * Agent framework.
119
121
  */
120
- private _agent?: Web5PlatformAgent;
122
+ private _agent?: EnboxPlatformAgent;
121
123
 
122
124
  private _didMethods: Map<string, DidMethodApi> = new Map();
123
125
 
@@ -146,12 +148,12 @@ export class AgentDidApi<TKeyManager extends AgentKeyManager = AgentKeyManager>
146
148
  }
147
149
 
148
150
  /**
149
- * Retrieves the `Web5PlatformAgent` execution context.
151
+ * Retrieves the `EnboxPlatformAgent` execution context.
150
152
  *
151
- * @returns The `Web5PlatformAgent` instance that represents the current execution context.
153
+ * @returns The `EnboxPlatformAgent` instance that represents the current execution context.
152
154
  * @throws Will throw an error if the `agent` instance property is undefined.
153
155
  */
154
- get agent(): Web5PlatformAgent {
156
+ get agent(): EnboxPlatformAgent {
155
157
  if (this._agent === undefined) {
156
158
  throw new Error('AgentDidApi: Unable to determine agent execution context.');
157
159
  }
@@ -159,7 +161,7 @@ export class AgentDidApi<TKeyManager extends AgentKeyManager = AgentKeyManager>
159
161
  return this._agent;
160
162
  }
161
163
 
162
- set agent(agent: Web5PlatformAgent) {
164
+ set agent(agent: EnboxPlatformAgent) {
163
165
  this._agent = agent;
164
166
 
165
167
  // AgentDidResolverCache should set the agent if it is the type of cache being used
package/src/dwn-api.ts CHANGED
@@ -33,7 +33,8 @@ import {
33
33
  import { CryptoUtils, X25519 } from '@enbox/crypto';
34
34
  import { DidDht, DidJwk, DidResolverCacheLevel, UniversalResolver } from '@enbox/dids';
35
35
 
36
- import type { Web5PlatformAgent } from './types/agent.js';
36
+ import type { EnboxPlatformAgent } from './types/agent.js';
37
+ import type { LocalDwnStrategy } from './local-dwn.js';
37
38
  import type {
38
39
  DwnMessage,
39
40
  DwnMessageInstance,
@@ -47,7 +48,9 @@ import type {
47
48
  SendDwnRequest,
48
49
  } from './types/dwn.js';
49
50
 
51
+ import { DwnDiscoveryFile } from './dwn-discovery-file.js';
50
52
  import { KeyDeliveryProtocolDefinition } from './store-data-protocols.js';
53
+ import { LocalDwnDiscovery } from './local-dwn.js';
51
54
  import { DwnInterface, dwnMessageConstructors } from './types/dwn.js';
52
55
  import { getDwnServiceEndpointUrls, isRecordsWrite } from './utils.js';
53
56
 
@@ -99,8 +102,9 @@ type DwnMessageWithBlob<T extends DwnInterface> = {
99
102
  };
100
103
 
101
104
  type DwnApiParams = {
102
- agent?: Web5PlatformAgent;
105
+ agent?: EnboxPlatformAgent;
103
106
  dwn: Dwn;
107
+ localDwnStrategy?: LocalDwnStrategy;
104
108
  };
105
109
 
106
110
  interface DwnApiCreateDwnParams extends Partial<DwnConfig> {
@@ -109,12 +113,12 @@ interface DwnApiCreateDwnParams extends Partial<DwnConfig> {
109
113
 
110
114
  export class AgentDwnApi {
111
115
  /**
112
- * Holds the instance of a `Web5PlatformAgent` that represents the current execution context for
113
- * the `AgentDwnApi`. This agent is used to interact with other Web5 agent components. It's vital
114
- * to ensure this instance is set to correctly contextualize operations within the broader Web5
116
+ * Holds the instance of a `EnboxPlatformAgent` that represents the current execution context for
117
+ * the `AgentDwnApi`. This agent is used to interact with other Enbox agent components. It's vital
118
+ * to ensure this instance is set to correctly contextualize operations within the broader Enbox
115
119
  * Agent framework.
116
120
  */
117
- private _agent?: Web5PlatformAgent;
121
+ private _agent?: EnboxPlatformAgent;
118
122
 
119
123
  /**
120
124
  * The DWN instance to use for this API.
@@ -148,21 +152,47 @@ export class AgentDwnApi {
148
152
  ttl: 30 * 60 * 1000
149
153
  });
150
154
 
151
- constructor({ agent, dwn }: DwnApiParams) {
155
+ /**
156
+ * Cache of locally-managed DIDs (agent DID + identities). Used to decide
157
+ * whether a target DID should be routed through the local DWN server.
158
+ */
159
+ private _localManagedDidCache = new TtlCache<string, boolean>({
160
+ ttl: 30 * 60 * 1000
161
+ });
162
+
163
+ /** Controls local DWN discovery behavior ('prefer' | 'only' | 'off'). */
164
+ private _localDwnStrategy: LocalDwnStrategy;
165
+
166
+ /** Lazy-initialized local DWN discovery instance. */
167
+ private _localDwnDiscovery?: LocalDwnDiscovery;
168
+
169
+ constructor({ agent, dwn, localDwnStrategy = 'prefer' }: DwnApiParams) {
152
170
  // If an agent is provided, set it as the execution context for this API.
153
171
  this._agent = agent;
154
172
 
155
173
  // Set the DWN instance for this API.
156
174
  this._dwn = dwn;
175
+
176
+ // Set the local DWN discovery strategy.
177
+ this._localDwnStrategy = localDwnStrategy;
178
+
179
+ // If agent is already available, eagerly initialize the discovery instance.
180
+ if (agent) {
181
+ this._localDwnDiscovery = new LocalDwnDiscovery(
182
+ agent.rpc,
183
+ 10_000,
184
+ AgentDwnApi._tryCreateDiscoveryFile(),
185
+ );
186
+ }
157
187
  }
158
188
 
159
189
  /**
160
- * Retrieves the `Web5PlatformAgent` execution context.
190
+ * Retrieves the `EnboxPlatformAgent` execution context.
161
191
  *
162
- * @returns The `Web5PlatformAgent` instance that represents the current execution context.
192
+ * @returns The `EnboxPlatformAgent` instance that represents the current execution context.
163
193
  * @throws Will throw an error if the `agent` instance property is undefined.
164
194
  */
165
- get agent(): Web5PlatformAgent {
195
+ get agent(): EnboxPlatformAgent {
166
196
  if (this._agent === undefined) {
167
197
  throw new Error('AgentDwnApi: Unable to determine agent execution context.');
168
198
  }
@@ -170,8 +200,156 @@ export class AgentDwnApi {
170
200
  return this._agent;
171
201
  }
172
202
 
173
- set agent(agent: Web5PlatformAgent) {
203
+ set agent(agent: EnboxPlatformAgent) {
174
204
  this._agent = agent;
205
+ // Re-initialize local DWN discovery with the new agent's RPC client.
206
+ this._localDwnDiscovery = new LocalDwnDiscovery(
207
+ agent.rpc,
208
+ 10_000,
209
+ AgentDwnApi._tryCreateDiscoveryFile(),
210
+ );
211
+ this._localManagedDidCache.clear();
212
+ }
213
+
214
+ get localDwnStrategy(): LocalDwnStrategy {
215
+ return this._localDwnStrategy;
216
+ }
217
+
218
+ public setLocalDwnStrategy(strategy: LocalDwnStrategy): void {
219
+ this._localDwnStrategy = strategy;
220
+ }
221
+
222
+ /**
223
+ * Inject a cached local DWN endpoint (e.g. from a `dwn://register`
224
+ * browser redirect or from persisted storage). The endpoint is validated
225
+ * via `GET /info` before being accepted.
226
+ *
227
+ * @param endpoint - The local DWN server base URL.
228
+ * @returns `true` if the endpoint was validated and cached, `false` otherwise.
229
+ * @see https://github.com/enboxorg/enbox/issues/589
230
+ */
231
+ public async setCachedLocalDwnEndpoint(endpoint: string): Promise<boolean> {
232
+ this._localDwnDiscovery ??= new LocalDwnDiscovery(
233
+ this.agent.rpc,
234
+ 10_000,
235
+ AgentDwnApi._tryCreateDiscoveryFile(),
236
+ );
237
+ return this._localDwnDiscovery.setCachedEndpoint(endpoint);
238
+ }
239
+
240
+ /**
241
+ * Resolves the DWN service endpoint URLs for the given target DID, optionally
242
+ * prepending a local DWN server endpoint when local discovery is enabled and
243
+ * the target is a locally-managed DID.
244
+ *
245
+ * @param targetDid - The DID whose DWN endpoints should be resolved.
246
+ * @returns An array of endpoint URLs.
247
+ * @throws When strategy is `'only'` and no local server is available.
248
+ */
249
+ public async getDwnEndpointUrlsForTarget(targetDid: string): Promise<string[]> {
250
+ const shouldUseLocalDwn = await this.shouldUseLocalDwnForTarget(targetDid);
251
+
252
+ if (!shouldUseLocalDwn) {
253
+ return getDwnServiceEndpointUrls(targetDid, this.agent.did);
254
+ }
255
+
256
+ const localDwnEndpoint = await this.getLocalDwnEndpoint();
257
+ if (this._localDwnStrategy === 'only') {
258
+ if (!localDwnEndpoint) {
259
+ throw new Error(
260
+ `AgentDwnApi: Local DWN strategy is 'only' but no local server is available ` +
261
+ `on 127.0.0.1:{3000,55500-55509}`
262
+ );
263
+ }
264
+
265
+ return [localDwnEndpoint];
266
+ }
267
+
268
+ let dwnEndpointUrls: string[] = [];
269
+ try {
270
+ dwnEndpointUrls = await getDwnServiceEndpointUrls(targetDid, this.agent.did);
271
+ } catch (error) {
272
+ if (!localDwnEndpoint) {
273
+ throw error;
274
+ }
275
+ }
276
+
277
+ if (!localDwnEndpoint) {
278
+ return dwnEndpointUrls;
279
+ }
280
+
281
+ const uniqueEndpoints = new Set<string>([
282
+ localDwnEndpoint,
283
+ ...dwnEndpointUrls,
284
+ ]);
285
+
286
+ return [...uniqueEndpoints];
287
+ }
288
+
289
+ /** Lazily retrieves the local DWN server endpoint via discovery. */
290
+ private async getLocalDwnEndpoint(): Promise<string | undefined> {
291
+ this._localDwnDiscovery ??= new LocalDwnDiscovery(
292
+ this.agent.rpc,
293
+ 10_000,
294
+ AgentDwnApi._tryCreateDiscoveryFile(),
295
+ );
296
+ return this._localDwnDiscovery.getEndpoint();
297
+ }
298
+
299
+ /**
300
+ * Attempt to create a {@link DwnDiscoveryFile} for file-based local DWN
301
+ * discovery. Returns `undefined` in environments where the filesystem
302
+ * is not available (e.g. browsers).
303
+ */
304
+ private static _tryCreateDiscoveryFile(): DwnDiscoveryFile | undefined {
305
+ try {
306
+ return new DwnDiscoveryFile();
307
+ } catch {
308
+ // Browser environment — node:fs/promises not available.
309
+ return undefined;
310
+ }
311
+ }
312
+
313
+ /**
314
+ * Determines whether the given target DID should be routed through the
315
+ * local DWN server. Returns `true` if the DID is the agent DID or one
316
+ * of the locally-managed identity DIDs.
317
+ */
318
+ private async shouldUseLocalDwnForTarget(targetDid: string): Promise<boolean> {
319
+ if (this._localDwnStrategy === 'off') {
320
+ return false;
321
+ }
322
+
323
+ const cached = this._localManagedDidCache.get(targetDid);
324
+ if (cached !== undefined) {
325
+ return cached;
326
+ }
327
+
328
+ if (targetDid === this.agent.agentDid.uri) {
329
+ this._localManagedDidCache.set(targetDid, true);
330
+ return true;
331
+ }
332
+
333
+ const identities = await this.agent.identity.list();
334
+ const localManagedDids = new Set<string>();
335
+
336
+ for (const identity of identities) {
337
+ localManagedDids.add(identity.did.uri);
338
+ if (identity.metadata.connectedDid) {
339
+ localManagedDids.add(identity.metadata.connectedDid);
340
+ }
341
+ }
342
+
343
+ for (const localDid of localManagedDids) {
344
+ this._localManagedDidCache.set(localDid, true);
345
+ }
346
+
347
+ const isLocalManaged = localManagedDids.has(targetDid);
348
+ if (!isLocalManaged) {
349
+ this._localManagedDidCache.set(targetDid, false);
350
+ }
351
+
352
+ return isLocalManaged;
175
353
  }
176
354
 
177
355
  /**
@@ -182,7 +360,7 @@ export class AgentDwnApi {
182
360
  * However, it is recommended to use the `processRequest` method to interact with the DWN
183
361
  * instance to ensure that the DWN message is constructed correctly.
184
362
  * - The getter is named `node` to avoid confusion with the `dwn` property of the
185
- * `Web5PlatformAgent`. In other words, so that a developer can call `agent.dwn.node` to access
363
+ * `EnboxPlatformAgent`. In other words, so that a developer can call `agent.dwn.node` to access
186
364
  * the DWN instance and not `agent.dwn.dwn`.
187
365
  */
188
366
  get node(): Dwn {
@@ -252,8 +430,8 @@ export class AgentDwnApi {
252
430
  public async sendRequest<T extends DwnInterface>(
253
431
  request: SendDwnRequest<T>
254
432
  ): Promise<DwnResponse<T>> {
255
- // First, confirm the target DID can be dereferenced and extract the DWN service endpoint URLs.
256
- const dwnEndpointUrls = await getDwnServiceEndpointUrls(request.target, this.agent.did);
433
+ // Resolve DWN service endpoint URLs, with local DWN discovery if enabled.
434
+ const dwnEndpointUrls = await this.getDwnEndpointUrlsForTarget(request.target);
257
435
  if (dwnEndpointUrls.length === 0) {
258
436
  throw new Error(`AgentDwnApi: DID Service is missing or malformed: ${request.target}#dwn`);
259
437
  }
@@ -997,7 +1175,7 @@ export class AgentDwnApi {
997
1175
  protocolUri: string,
998
1176
  ): Promise<ProtocolDefinition> {
999
1177
  return fetchRemoteProtocolDefinitionFn(
1000
- targetDid, protocolUri, this.agent.did,
1178
+ targetDid, protocolUri, this.getDwnEndpointUrlsForTarget.bind(this),
1001
1179
  this.sendDwnRpcRequest.bind(this), this._protocolDefinitionCache,
1002
1180
  );
1003
1181
  }
@@ -1022,7 +1200,7 @@ export class AgentDwnApi {
1022
1200
  ): Promise<{ rootKeyId: string; derivedPublicKey: PublicKeyJwk } | undefined> {
1023
1201
  return extractDerivedPublicKeyFn(
1024
1202
  targetDid, protocolUri, rootContextId, requesterDid,
1025
- this.agent.did, this.getSigner.bind(this),
1203
+ this.getDwnEndpointUrlsForTarget.bind(this), this.getSigner.bind(this),
1026
1204
  this.sendDwnRpcRequest.bind(this),
1027
1205
  );
1028
1206
  }
@@ -1139,6 +1317,7 @@ export class AgentDwnApi {
1139
1317
  this.agent, tenantDid, contextKeyMessage,
1140
1318
  this.getDwnMessage.bind(this),
1141
1319
  this.sendDwnRpcRequest.bind(this),
1320
+ this.getDwnEndpointUrlsForTarget.bind(this),
1142
1321
  );
1143
1322
  }
1144
1323
 
@@ -1160,6 +1339,7 @@ export class AgentDwnApi {
1160
1339
  this.processRequest.bind(this),
1161
1340
  this.getSigner.bind(this),
1162
1341
  this.sendDwnRpcRequest.bind(this),
1342
+ this.getDwnEndpointUrlsForTarget.bind(this),
1163
1343
  );
1164
1344
  }
1165
1345
  }