@enbox/api 0.3.2 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/README.md +63 -0
  2. package/dist/browser.mjs +11 -28
  3. package/dist/browser.mjs.map +4 -4
  4. package/dist/esm/advanced.js +1 -1
  5. package/dist/esm/define-protocol.js +3 -3
  6. package/dist/esm/did-api.js +1 -1
  7. package/dist/esm/did-api.js.map +1 -1
  8. package/dist/esm/dwn-api.js +6 -6
  9. package/dist/esm/dwn-api.js.map +1 -1
  10. package/dist/esm/dwn-reader-api.js +2 -2
  11. package/dist/esm/enbox.js +205 -0
  12. package/dist/esm/enbox.js.map +1 -0
  13. package/dist/esm/index.js +16 -15
  14. package/dist/esm/index.js.map +1 -1
  15. package/dist/esm/protocol.js +2 -2
  16. package/dist/esm/protocol.js.map +1 -1
  17. package/dist/esm/record-data.js +79 -5
  18. package/dist/esm/record-data.js.map +1 -1
  19. package/dist/esm/record.js +49 -10
  20. package/dist/esm/record.js.map +1 -1
  21. package/dist/esm/repository.js +7 -7
  22. package/dist/esm/repository.js.map +1 -1
  23. package/dist/esm/typed-enbox.js +583 -0
  24. package/dist/esm/typed-enbox.js.map +1 -0
  25. package/dist/esm/typed-live-query.js +1 -1
  26. package/dist/esm/typed-record.js +370 -46
  27. package/dist/esm/typed-record.js.map +1 -1
  28. package/dist/esm/utils.js +25 -0
  29. package/dist/esm/utils.js.map +1 -1
  30. package/dist/esm/vc-api.js.map +1 -1
  31. package/dist/types/advanced.d.ts +1 -1
  32. package/dist/types/define-protocol.d.ts +3 -3
  33. package/dist/types/did-api.d.ts +4 -4
  34. package/dist/types/did-api.d.ts.map +1 -1
  35. package/dist/types/dwn-api.d.ts +12 -7
  36. package/dist/types/dwn-api.d.ts.map +1 -1
  37. package/dist/types/dwn-reader-api.d.ts +2 -2
  38. package/dist/types/enbox.d.ts +202 -0
  39. package/dist/types/enbox.d.ts.map +1 -0
  40. package/dist/types/grant-revocation.d.ts +2 -2
  41. package/dist/types/grant-revocation.d.ts.map +1 -1
  42. package/dist/types/index.d.ts +16 -15
  43. package/dist/types/index.d.ts.map +1 -1
  44. package/dist/types/live-query.d.ts +2 -2
  45. package/dist/types/live-query.d.ts.map +1 -1
  46. package/dist/types/permission-grant.d.ts +2 -2
  47. package/dist/types/permission-grant.d.ts.map +1 -1
  48. package/dist/types/permission-request.d.ts +2 -2
  49. package/dist/types/permission-request.d.ts.map +1 -1
  50. package/dist/types/protocol-types.d.ts +2 -2
  51. package/dist/types/protocol.d.ts +7 -7
  52. package/dist/types/protocol.d.ts.map +1 -1
  53. package/dist/types/record-data.d.ts +17 -0
  54. package/dist/types/record-data.d.ts.map +1 -1
  55. package/dist/types/record.d.ts +24 -10
  56. package/dist/types/record.d.ts.map +1 -1
  57. package/dist/types/repository-types.d.ts +19 -11
  58. package/dist/types/repository-types.d.ts.map +1 -1
  59. package/dist/types/repository.d.ts +7 -7
  60. package/dist/types/repository.d.ts.map +1 -1
  61. package/dist/types/typed-enbox.d.ts +613 -0
  62. package/dist/types/typed-enbox.d.ts.map +1 -0
  63. package/dist/types/typed-live-query.d.ts +1 -1
  64. package/dist/types/typed-record.d.ts +427 -53
  65. package/dist/types/typed-record.d.ts.map +1 -1
  66. package/dist/types/utils.d.ts +23 -0
  67. package/dist/types/utils.d.ts.map +1 -1
  68. package/dist/types/vc-api.d.ts +3 -3
  69. package/dist/types/vc-api.d.ts.map +1 -1
  70. package/package.json +12 -11
  71. package/src/advanced.ts +1 -1
  72. package/src/define-protocol.ts +3 -3
  73. package/src/did-api.ts +5 -5
  74. package/src/dwn-api.ts +22 -17
  75. package/src/dwn-reader-api.ts +2 -2
  76. package/src/enbox.ts +281 -0
  77. package/src/grant-revocation.ts +3 -3
  78. package/src/index.ts +17 -16
  79. package/src/live-query.ts +2 -2
  80. package/src/permission-grant.ts +4 -4
  81. package/src/permission-request.ts +3 -3
  82. package/src/protocol-types.ts +2 -2
  83. package/src/protocol.ts +8 -8
  84. package/src/record-data.ts +86 -5
  85. package/src/record.ts +54 -13
  86. package/src/repository-types.ts +19 -7
  87. package/src/repository.ts +15 -15
  88. package/src/typed-enbox.ts +1169 -0
  89. package/src/typed-live-query.ts +1 -1
  90. package/src/typed-record.ts +431 -53
  91. package/src/utils.ts +27 -0
  92. package/src/vc-api.ts +4 -4
  93. package/dist/esm/typed-web5.js +0 -339
  94. package/dist/esm/typed-web5.js.map +0 -1
  95. package/dist/esm/web5.js +0 -410
  96. package/dist/esm/web5.js.map +0 -1
  97. package/dist/types/typed-web5.d.ts +0 -221
  98. package/dist/types/typed-web5.d.ts.map +0 -1
  99. package/dist/types/web5.d.ts +0 -351
  100. package/dist/types/web5.d.ts.map +0 -1
  101. package/src/typed-web5.ts +0 -598
  102. package/src/web5.ts +0 -762
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@enbox/api",
3
- "version": "0.3.2",
4
- "description": "SDK for accessing the features and capabilities of Web5",
3
+ "version": "0.4.1",
4
+ "description": "Enbox SDK — high-level API for Decentralized Web Nodes, DIDs, and Verifiable Credentials",
5
5
  "type": "module",
6
6
  "main": "./dist/esm/index.js",
7
7
  "module": "./dist/esm/index.js",
@@ -71,8 +71,8 @@
71
71
  "DID",
72
72
  "sdk",
73
73
  "verifiable-credentials",
74
- "web5",
75
- "web5-sdk"
74
+ "enbox",
75
+ "enbox-sdk"
76
76
  ],
77
77
  "publishConfig": {
78
78
  "access": "public"
@@ -81,15 +81,16 @@
81
81
  "bun": ">=1.0.0"
82
82
  },
83
83
  "dependencies": {
84
- "@enbox/agent": "0.2.2",
85
- "@enbox/common": "0.0.6",
86
- "@enbox/dwn-clients": "0.0.9"
84
+ "@enbox/agent": "0.3.1",
85
+ "@enbox/auth": "0.2.0",
86
+ "@enbox/common": "0.0.7",
87
+ "@enbox/dwn-clients": "0.1.0"
87
88
  },
88
89
  "devDependencies": {
89
- "@enbox/crypto": "0.0.7",
90
- "@enbox/dids": "0.0.8",
91
- "@enbox/dwn-sdk-js": "0.1.1",
92
- "@enbox/protocols": "0.2.9",
90
+ "@enbox/crypto": "0.0.8",
91
+ "@enbox/dids": "0.0.9",
92
+ "@enbox/dwn-sdk-js": "0.1.2",
93
+ "@enbox/protocols": "0.2.11",
93
94
  "@types/node": "20.14.8",
94
95
  "@types/sinon": "17.0.3",
95
96
  "@typescript-eslint/eslint-plugin": "8.32.1",
package/src/advanced.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * Advanced API surface for power users who need direct access to
3
3
  * the underlying {@link DwnApi}.
4
4
  *
5
- * Most applications should use `web5.using(protocol)` instead.
5
+ * Most applications should use `enbox.using(protocol)` instead.
6
6
  * Import from `@enbox/api/advanced` to access these exports.
7
7
  *
8
8
  * @packageDocumentation
@@ -2,7 +2,7 @@
2
2
  * Factory function for creating typed protocol definitions.
3
3
  *
4
4
  * `defineProtocol()` wraps a standard {@link ProtocolDefinition} with
5
- * compile-time type metadata so that {@link TypedWeb5} can provide
5
+ * compile-time type metadata so that {@link TypedEnbox} can provide
6
6
  * path autocompletion, data-shape inference, and tag type safety.
7
7
  */
8
8
 
@@ -17,7 +17,7 @@ import type { SchemaMap, TypedProtocol } from './protocol-types.js';
17
17
  *
18
18
  * The `definition` argument is returned as-is (no cloning). The schema map
19
19
  * is a phantom type parameter — it exists only at compile time to thread
20
- * type information through to `TypedWeb5`.
20
+ * type information through to `TypedEnbox`.
21
21
  *
22
22
  * @param definition - A standard `ProtocolDefinition` object.
23
23
  * @param _schemaMap - A phantom value (e.g. `{} as MySchemaMap`) that carries
@@ -32,7 +32,7 @@ import type { SchemaMap, TypedProtocol } from './protocol-types.js';
32
32
  * });
33
33
  *
34
34
  * // socialGraph.definition is the raw ProtocolDefinition
35
- * // TypedWeb5 infers paths like 'friend' | 'friend/message' and
35
+ * // TypedEnbox infers paths like 'friend' | 'friend/message' and
36
36
  * // data types from the schema map.
37
37
  * ```
38
38
  */
package/src/did-api.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { DidCreateParams, DidMessageResult, DidResolveParams, ResponseStatus, Web5Agent } from '@enbox/agent';
1
+ import type { DidCreateParams, DidMessageResult, DidResolveParams, EnboxAgent, ResponseStatus } from '@enbox/agent';
2
2
 
3
3
  import { DidInterface } from '@enbox/agent';
4
4
 
@@ -37,15 +37,15 @@ export type DidResolveResponse = DidMessageResult[DidInterface.Resolve];
37
37
  */
38
38
  export class DidApi {
39
39
  /**
40
- * Holds the instance of a {@link Web5Agent} that represents the current execution context for
40
+ * Holds the instance of a {@link EnboxAgent} that represents the current execution context for
41
41
  * the `DidApi`. This agent is used to process DID requests.
42
42
  */
43
- private agent: Web5Agent;
43
+ private agent: EnboxAgent;
44
44
 
45
45
  /** The DID of the tenant under which DID operations are being performed. */
46
46
  private connectedDid: string;
47
47
 
48
- constructor(options: { agent: Web5Agent, connectedDid: string }) {
48
+ constructor(options: { agent: EnboxAgent, connectedDid: string }) {
49
49
  this.agent = options.agent;
50
50
  this.connectedDid = options.connectedDid;
51
51
  }
@@ -54,7 +54,7 @@ export class DidApi {
54
54
  * Initiates the creation of a Decentralized Identifier (DID) using the specified method, options,
55
55
  * and storage preference.
56
56
  *
57
- * This method sends a request to the Web5 Agent to create a new DID based on the provided method,
57
+ * This method sends a request to the Enbox Agent to create a new DID based on the provided method,
58
58
  * with method-specific options. It also specifies whether the newly created DID should be stored.
59
59
  *
60
60
  * @param request - The request parameters for creating a DID, including the method, options, and
package/src/dwn-api.ts CHANGED
@@ -12,10 +12,10 @@ import type {
12
12
  DwnPaginationCursor,
13
13
  DwnResponse,
14
14
  DwnResponseStatus,
15
+ EnboxAgent,
15
16
  FetchPermissionRequestParams,
16
17
  FetchPermissionsParams,
17
- ProcessDwnRequest,
18
- Web5Agent } from '@enbox/agent';
18
+ ProcessDwnRequest } from '@enbox/agent';
19
19
 
20
20
  import type { DwnSubscriptionMessage } from '@enbox/dwn-clients';
21
21
 
@@ -161,10 +161,13 @@ export type RecordsReadRequest = Omit<DwnMessageParams[DwnInterface.RecordsRead]
161
161
  /**
162
162
  * Encapsulates the response from a record read operation, combining the general operation status
163
163
  * with the specific record that was retrieved.
164
+ *
165
+ * When the status code is not in the 2xx range (e.g. 401, 404), `record` will be `undefined`.
166
+ * Always check `status.code` before accessing the record.
164
167
  */
165
168
  export type RecordsReadResponse = DwnResponseStatus & {
166
- /** The record retrieved by the read operation. */
167
- record: Record;
169
+ /** The record retrieved by the read operation, or `undefined` if the request failed. */
170
+ record?: Record;
168
171
  };
169
172
 
170
173
  /**
@@ -223,9 +226,11 @@ export type RecordsWriteRequest = Omit<Partial<DwnMessageParams[DwnInterface.Rec
223
226
  export type RecordsWriteResponse = DwnResponseStatus & {
224
227
  /**
225
228
  * The `Record` instance representing the record that was successfully written to the
226
- * DWN as a result of the write operation.
229
+ * DWN as a result of the write operation, or `undefined` if the write failed.
230
+ *
231
+ * Always check `status.code` before accessing the record.
227
232
  */
228
- record: Record;
233
+ record?: Record;
229
234
  };
230
235
 
231
236
  /**
@@ -233,10 +238,10 @@ export type RecordsWriteResponse = DwnResponseStatus & {
233
238
  */
234
239
  export class DwnApi {
235
240
  /**
236
- * Holds the instance of a {@link Web5Agent} that represents the current execution context for
241
+ * Holds the instance of a {@link EnboxAgent} that represents the current execution context for
237
242
  * the `DwnApi`. This agent is used to process DWN requests.
238
243
  */
239
- private agent: Web5Agent;
244
+ private agent: EnboxAgent;
240
245
 
241
246
  /** The DID of the DWN tenant under which operations are being performed. */
242
247
  private connectedDid: string;
@@ -247,7 +252,7 @@ export class DwnApi {
247
252
  /** Holds the instance of {@link AgentPermissionsApi} that helps when dealing with permissions protocol records */
248
253
  private permissionsApi: AgentPermissionsApi;
249
254
 
250
- constructor(options: { agent: Web5Agent, connectedDid: string, delegateDid?: string }) {
255
+ constructor(options: { agent: EnboxAgent, connectedDid: string, delegateDid?: string }) {
251
256
  this.agent = options.agent;
252
257
  this.connectedDid = options.connectedDid;
253
258
  this.delegateDid = options.delegateDid;
@@ -491,7 +496,7 @@ export class DwnApi {
491
496
 
492
497
  const agentRequest: ProcessDwnRequest<DwnInterface.RecordsDelete> = {
493
498
  /**
494
- * The `author` is the DID that will sign the message and must be the DID the Web5 app is
499
+ * The `author` is the DID that will sign the message and must be the DID the Enbox app is
495
500
  * connected with and is authorized to access the signing private key of.
496
501
  */
497
502
  author : this.connectedDid,
@@ -543,7 +548,7 @@ export class DwnApi {
543
548
 
544
549
  const agentRequest: ProcessDwnRequest<DwnInterface.RecordsQuery> = {
545
550
  /**
546
- * The `author` is the DID that will sign the message and must be the DID the Web5 app is
551
+ * The `author` is the DID that will sign the message and must be the DID the Enbox app is
547
552
  * connected with and is authorized to access the signing private key of.
548
553
  */
549
554
  author : this.connectedDid,
@@ -562,7 +567,7 @@ export class DwnApi {
562
567
  // if we don't find a delegated grant, we will attempt to query signing as the delegated DID
563
568
  // This is to allow the API caller to query public records without needing to impersonate the delegate.
564
569
  //
565
- // NOTE: For anonymous/public queries without explicit permissions, callers can use `DwnReaderApi` via `Web5.anonymous()`.
570
+ // NOTE: For anonymous/public queries without explicit permissions, callers can use `DwnReaderApi` via `Enbox.anonymous()`.
566
571
  // See: https://github.com/enboxorg/enbox/issues/898
567
572
  try {
568
573
  const { message: delegatedGrant } = await this.permissionsApi.getPermissionForRequest({
@@ -635,7 +640,7 @@ export class DwnApi {
635
640
 
636
641
  const agentRequest: ProcessDwnRequest<DwnInterface.RecordsRead> = {
637
642
  /**
638
- * The `author` is the DID that will sign the message and must be the DID the Web5 app is
643
+ * The `author` is the DID that will sign the message and must be the DID the Enbox app is
639
644
  * connected with and is authorized to access the signing private key of.
640
645
  */
641
646
  author : this.connectedDid,
@@ -654,7 +659,7 @@ export class DwnApi {
654
659
  // if we don't find a delegated grant, we will attempt to read signing as the delegated DID
655
660
  // This is to allow the API caller to read public records without needing to impersonate the delegate.
656
661
  //
657
- // NOTE: For anonymous/public reads without explicit permissions, callers can use `DwnReaderApi` via `Web5.anonymous()`.
662
+ // NOTE: For anonymous/public reads without explicit permissions, callers can use `DwnReaderApi` via `Enbox.anonymous()`.
658
663
  // See: https://github.com/enboxorg/enbox/issues/898
659
664
 
660
665
  try {
@@ -688,7 +693,7 @@ export class DwnApi {
688
693
 
689
694
  const { reply: { entry, status } } = agentResponse;
690
695
 
691
- let record: Record;
696
+ let record: Record | undefined;
692
697
  if (200 <= status.code && status.code <= 299) {
693
698
  const recordOptions = {
694
699
  /**
@@ -785,7 +790,7 @@ export class DwnApi {
785
790
  // if we don't find a delegated grant, we will attempt to subscribe signing as the delegated DID
786
791
  // This is to allow the API caller to subscribe to public records without needing to impersonate the delegate.
787
792
  //
788
- // NOTE: For anonymous/public subscriptions without explicit permissions, callers can use `DwnReaderApi` via `Web5.anonymous()`.
793
+ // NOTE: For anonymous/public subscriptions without explicit permissions, callers can use `DwnReaderApi` via `Enbox.anonymous()`.
789
794
  // See: https://github.com/enboxorg/enbox/issues/898
790
795
  try {
791
796
  const { message: delegatedGrant } = await this.permissionsApi.getPermissionForRequest({
@@ -883,7 +888,7 @@ export class DwnApi {
883
888
 
884
889
  const { message: responseMessage, reply: { status } } = agentResponse;
885
890
 
886
- let record: Record;
891
+ let record: Record | undefined;
887
892
  if (200 <= status.code && status.code <= 299) {
888
893
  const recordOptions = {
889
894
  /**
@@ -129,11 +129,11 @@ export type ReaderProtocolsQueryResponse = DwnResponseStatus & {
129
129
  * on every call (remote-only). All messages are unsigned, so only published
130
130
  * records and protocols are accessible.
131
131
  *
132
- * Obtain an instance via {@link Web5.anonymous | `Web5.anonymous()`}.
132
+ * Obtain an instance via {@link Enbox.anonymous | `Enbox.anonymous()`}.
133
133
  *
134
134
  * @example
135
135
  * ```ts
136
- * const { dwn } = Web5.anonymous();
136
+ * const { dwn } = Enbox.anonymous();
137
137
  *
138
138
  * const { records } = await dwn.records.query({
139
139
  * from: 'did:dht:alice...',
package/src/enbox.ts ADDED
@@ -0,0 +1,281 @@
1
+ /**
2
+ * NOTE: Added reference types here to avoid a `pnpm` bug during build.
3
+ * https://github.com/enboxorg/enbox/pull/507
4
+ */
5
+ /// <reference types="@enbox/dwn-sdk-js" />
6
+
7
+ import type { AuthSession } from '@enbox/auth';
8
+ import type { DidMethodResolver } from '@enbox/dids';
9
+ import type { EnboxAgent } from '@enbox/agent';
10
+ import type { ProtocolDefinition } from '@enbox/dwn-sdk-js';
11
+
12
+ import type { SchemaMap, TypedProtocol } from './protocol-types.js';
13
+
14
+ import { AnonymousDwnApi } from '@enbox/agent';
15
+ import { EnboxRpcClient } from '@enbox/dwn-clients';
16
+ import { DidDht, DidJwk, DidKey, DidResolverCacheMemory, DidWeb, UniversalResolver } from '@enbox/dids';
17
+
18
+ import { DidApi } from './did-api.js';
19
+ import { DwnApi } from './dwn-api.js';
20
+ import { DwnReaderApi } from './dwn-reader-api.js';
21
+ import { TypedEnbox } from './typed-enbox.js';
22
+ import { VcApi } from './vc-api.js';
23
+
24
+ /**
25
+ * Options for creating an anonymous (read-only) Enbox instance via {@link Enbox.anonymous}.
26
+ *
27
+ * @beta
28
+ */
29
+ export type EnboxAnonymousOptions = {
30
+ /** Override the default DID method resolvers. Defaults to `[DidDht, DidJwk, DidKey, DidWeb]`. */
31
+ didResolvers?: DidMethodResolver[];
32
+ };
33
+
34
+ /**
35
+ * The result of calling {@link Enbox.anonymous}.
36
+ *
37
+ * Contains only a read-only `dwn` property — no `did`, `vc`, or `agent`.
38
+ *
39
+ * @beta
40
+ */
41
+ export type EnboxAnonymousApi = {
42
+ /** A read-only DWN API for querying public data on remote DWNs. */
43
+ dwn: DwnReaderApi;
44
+ };
45
+
46
+ /**
47
+ * Parameters for constructing an {@link Enbox} instance.
48
+ *
49
+ * These are the minimal primitives needed to interact with the DWN network.
50
+ * Typically obtained from an {@link AuthSession} via `@enbox/auth`.
51
+ */
52
+ export type EnboxParams = {
53
+ /**
54
+ * A {@link EnboxAgent} instance that handles DIDs, DWNs and VCs requests. The agent manages the
55
+ * user keys and identities, and is responsible to sign and verify messages.
56
+ */
57
+ agent: EnboxAgent;
58
+
59
+ /** The DID of the tenant under which all DID, DWN, and VC requests are being performed. */
60
+ connectedDid: string;
61
+
62
+ /** The DID that will be signing messages using grants from the connectedDid. */
63
+ delegateDid?: string;
64
+ };
65
+
66
+ /**
67
+ * The main Enbox API interface. It provides protocol-scoped access to
68
+ * Decentralized Web Nodes (DWNs), Decentralized Identifiers (DIDs),
69
+ * and Verifiable Credentials (VCs).
70
+ *
71
+ * Authentication and identity management are handled externally by
72
+ * `@enbox/auth`. Use {@link Enbox.connect} to create an instance from
73
+ * an {@link AuthSession} or raw parameters.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * import { AuthManager } from '@enbox/auth';
78
+ * import { Enbox } from '@enbox/api';
79
+ *
80
+ * const auth = await AuthManager.create({ sync: '15s' });
81
+ * const session = await auth.connect();
82
+ *
83
+ * const enbox = Enbox.connect({ session });
84
+ * const social = enbox.using(SocialProtocol);
85
+ * ```
86
+ */
87
+ export class Enbox {
88
+ /**
89
+ * A {@link EnboxAgent} instance that handles DIDs, DWNs and VCs requests. The agent manages the
90
+ * user keys and identities, and is responsible to sign and verify messages.
91
+ */
92
+ public agent: EnboxAgent;
93
+
94
+ /** Exposed instance to the DID APIs, allow users to create and resolve DIDs. */
95
+ public did: DidApi;
96
+
97
+ /** Internal DWN API instance. Use {@link Enbox.using} for protocol-scoped access. */
98
+ private _dwn: DwnApi;
99
+
100
+ /**
101
+ * Cache of {@link TypedEnbox} instances keyed by protocol URI.
102
+ *
103
+ * Ensures that `enbox.using(Protocol)` returns the **same** `TypedEnbox`
104
+ * instance for a given protocol across multiple call sites, avoiding
105
+ * redundant protocol installations and duplicated internal state.
106
+ */
107
+ private _typedInstances = new Map<string, TypedEnbox<ProtocolDefinition, SchemaMap>>();
108
+
109
+ /** Exposed instance to the VC APIs, allow users to issue, present and verify VCs. */
110
+ public vc: VcApi;
111
+
112
+ constructor({ agent, connectedDid, delegateDid }: EnboxParams) {
113
+ this.agent = agent;
114
+ this.did = new DidApi({ agent, connectedDid });
115
+ this._dwn = new DwnApi({ agent, connectedDid, delegateDid });
116
+ this.vc = new VcApi({ agent, connectedDid });
117
+ }
118
+
119
+ /**
120
+ * Returns a {@link TypedEnbox} instance scoped to the given protocol.
121
+ *
122
+ * This is the **primary developer interface** for interacting with
123
+ * protocol-backed records. It auto-injects the protocol URI, protocolPath,
124
+ * and schema into every operation, and provides compile-time path
125
+ * autocompletion plus typed data payloads via the schema map.
126
+ *
127
+ * Instances are **cached by protocol URI** — calling `using()` multiple
128
+ * times with the same protocol returns the same `TypedEnbox` instance,
129
+ * so auto-configure only runs once and all call sites share state.
130
+ *
131
+ * @param protocol - A typed protocol created via `defineProtocol()`.
132
+ * @returns A `TypedEnbox` instance bound to the given protocol.
133
+ *
134
+ * @example
135
+ * ```ts
136
+ * const social = enbox.using(SocialProtocol);
137
+ *
138
+ * await social.configure();
139
+ *
140
+ * const { record } = await social.records.write('friend', {
141
+ * data: { did: 'did:example:alice', alias: 'Alice' },
142
+ * });
143
+ *
144
+ * const { records } = await social.records.query('friend');
145
+ * ```
146
+ */
147
+ public using<D extends ProtocolDefinition, M extends SchemaMap>(
148
+ protocol: TypedProtocol<D, M>,
149
+ ): TypedEnbox<D, M> {
150
+ const uri = protocol.definition.protocol;
151
+ const cached = this._typedInstances.get(uri);
152
+
153
+ if (cached) {
154
+ // The map stores a type-erased instance; restore the caller's generics.
155
+ return cached as unknown as TypedEnbox<D, M>;
156
+ }
157
+
158
+ const instance = new TypedEnbox<D, M>(this._dwn, protocol);
159
+ // Store with erased generics so the map value type stays uniform.
160
+ this._typedInstances.set(uri, instance as unknown as TypedEnbox<ProtocolDefinition, SchemaMap>);
161
+ return instance;
162
+ }
163
+
164
+ /**
165
+ * Stops DWN sync and clears the cached {@link TypedEnbox} instances.
166
+ *
167
+ * Call this when the application is shutting down or the user is
168
+ * disconnecting to cleanly release background resources. After calling
169
+ * `disconnect()`, the `Enbox` instance should not be reused.
170
+ *
171
+ * @param timeout - Maximum milliseconds to wait for an in-progress sync
172
+ * cycle to finish before force-stopping. Defaults to `2000`.
173
+ *
174
+ * @example
175
+ * ```ts
176
+ * await enbox.disconnect();
177
+ * ```
178
+ *
179
+ * @beta
180
+ */
181
+ public async disconnect(timeout?: number): Promise<void> {
182
+ // Stop any active sync.
183
+ if ('sync' in this.agent && typeof (this.agent as any).sync?.stopSync === 'function') {
184
+ await (this.agent as any).sync.stopSync(timeout);
185
+ }
186
+
187
+ // Clear cached TypedEnbox instances so they are not accidentally reused.
188
+ this._typedInstances.clear();
189
+ }
190
+
191
+ /**
192
+ * Creates a lightweight, read-only Enbox instance for querying public DWN data.
193
+ *
194
+ * No identity, vault, password, or signing keys are required. The returned
195
+ * API supports querying and reading published records and protocols from any
196
+ * remote DWN, using **unsigned** (anonymous) DWN messages.
197
+ *
198
+ * @param options - Optional configuration overrides.
199
+ * @returns An {@link EnboxAnonymousApi} with a read-only `dwn` property.
200
+ *
201
+ * @example
202
+ * ```ts
203
+ * const { dwn } = Enbox.anonymous();
204
+ *
205
+ * const { records } = await dwn.records.query({
206
+ * from: 'did:dht:alice...',
207
+ * filter: { protocol: 'https://social.example/posts', protocolPath: 'post' },
208
+ * });
209
+ *
210
+ * for (const record of records) {
211
+ * console.log(record.id, await record.data.text());
212
+ * }
213
+ * ```
214
+ *
215
+ * @beta
216
+ */
217
+ public static anonymous(options?: EnboxAnonymousOptions): EnboxAnonymousApi {
218
+ const didResolver = new UniversalResolver({
219
+ didResolvers : options?.didResolvers ?? [DidDht, DidJwk, DidKey, DidWeb],
220
+ cache : new DidResolverCacheMemory(),
221
+ });
222
+
223
+ const rpcClient = new EnboxRpcClient();
224
+ const anonymousDwn = new AnonymousDwnApi({ didResolver, rpcClient });
225
+
226
+ return {
227
+ dwn: new DwnReaderApi(anonymousDwn),
228
+ };
229
+ }
230
+
231
+ /**
232
+ * Creates an {@link Enbox} instance from an {@link AuthSession} or raw parameters.
233
+ *
234
+ * This is a thin factory — all authentication, identity management, vault
235
+ * initialization, DWN registration, and sync setup are handled externally
236
+ * by `@enbox/auth` via {@link AuthSession}.
237
+ *
238
+ * @param params - Either `{ session }` with an {@link AuthSession} from
239
+ * `@enbox/auth`, or raw `{ agent, connectedDid, delegateDid? }` parameters.
240
+ * @returns A new {@link Enbox} instance ready for use.
241
+ *
242
+ * @example
243
+ * ```ts
244
+ * // Using an AuthSession from @enbox/auth
245
+ * import { AuthManager } from '@enbox/auth';
246
+ * const auth = await AuthManager.create({ sync: '15s' });
247
+ * const session = await auth.connect();
248
+ * const enbox = Enbox.connect({ session });
249
+ *
250
+ * // Using raw parameters
251
+ * const enbox = Enbox.connect({ agent, connectedDid: did });
252
+ * ```
253
+ */
254
+ public static connect(params: { session: AuthSession } | EnboxParams): Enbox {
255
+ if ('session' in params) {
256
+ const { session } = params;
257
+ return new Enbox({
258
+ agent : session.agent,
259
+ connectedDid : session.did,
260
+ delegateDid : session.delegateDid,
261
+ });
262
+ }
263
+ return new Enbox(params);
264
+ }
265
+ }
266
+
267
+ // ---------------------------------------------------------------------------
268
+ // Deprecated aliases — migration aid
269
+ // ---------------------------------------------------------------------------
270
+
271
+ /** @deprecated Use {@link Enbox} instead. Will be removed in a future version. */
272
+ export const Web5 = Enbox;
273
+
274
+ /** @deprecated Use {@link EnboxParams} instead. */
275
+ export type Web5Params = EnboxParams;
276
+
277
+ /** @deprecated Use {@link EnboxAnonymousOptions} instead. */
278
+ export type Web5AnonymousOptions = EnboxAnonymousOptions;
279
+
280
+ /** @deprecated Use {@link EnboxAnonymousApi} instead. */
281
+ export type Web5AnonymousApi = EnboxAnonymousApi;
@@ -1,4 +1,4 @@
1
- import type { DwnDataEncodedRecordsWriteMessage, DwnResponseStatus, SendDwnRequest, Web5Agent } from '@enbox/agent';
1
+ import type { DwnDataEncodedRecordsWriteMessage, DwnResponseStatus, EnboxAgent, SendDwnRequest } from '@enbox/agent';
2
2
 
3
3
  import { Convert } from '@enbox/common';
4
4
  import { AgentPermissionsApi, DwnInterface, getRecordAuthor } from '@enbox/agent';
@@ -53,7 +53,7 @@ export class PermissionGrantRevocation implements GrantRevocationModel {
53
53
  /** parses the grant revocation given am agent, connectedDid and data encoded records write message */
54
54
  static async parse({ connectedDid, agent, message }:{
55
55
  connectedDid: string;
56
- agent: Web5Agent;
56
+ agent: EnboxAgent;
57
57
  message: DwnDataEncodedRecordsWriteMessage;
58
58
  }): Promise<PermissionGrantRevocation> {
59
59
  const permissions = new AgentPermissionsApi({ agent });
@@ -61,7 +61,7 @@ export class PermissionGrantRevocation implements GrantRevocationModel {
61
61
  }
62
62
 
63
63
  /** The agent to use for this instantiation of the grant revocation */
64
- private get agent(): Web5Agent {
64
+ private get agent(): EnboxAgent {
65
65
  return this._permissions.agent;
66
66
  }
67
67
 
package/src/index.ts CHANGED
@@ -1,20 +1,20 @@
1
1
  /**
2
- * Making developing with Web5 components at least 5 times easier to work with.
2
+ * Enbox SDK high-level API for Decentralized Web Nodes, DIDs, and VCs.
3
3
  *
4
- * Web5 consists of the following components:
5
- * - Decentralized Identifiers
6
- * - Verifiable Credentials
7
- * - DWeb Node personal datastores
4
+ * The SDK provides protocol-scoped access to DWN records with compile-time
5
+ * type safety, DID management, and Verifiable Credential operations.
8
6
  *
9
- * The SDK sets out to gather the most oft used functionality from all three of
10
- * these pillar technologies to provide a simple library that is as close to
11
- * effortless as possible.
7
+ * Authentication and identity management are handled by `@enbox/auth`.
12
8
  *
13
- * The SDK is currently still under active development, but having entered the
14
- * Tech Preview phase there is now a drive to avoid unnecessary changes unless
15
- * backwards compatibility is provided. Additional functionality will be added
16
- * in the lead up to 1.0 final, and modifications will be made to address
17
- * issues and community feedback.
9
+ * @example
10
+ * ```ts
11
+ * import { AuthManager } from '@enbox/auth';
12
+ * import { Enbox } from '@enbox/api';
13
+ *
14
+ * const auth = await AuthManager.create({ sync: '15s' });
15
+ * const session = await auth.connect();
16
+ * const enbox = Enbox.connect({ session });
17
+ * ```
18
18
  *
19
19
  * [Link to GitHub Repo](https://github.com/enboxorg/enbox)
20
20
  *
@@ -24,6 +24,7 @@
24
24
  export * from './define-protocol.js';
25
25
  export * from './did-api.js';
26
26
  export * from './dwn-reader-api.js';
27
+ export * from './enbox.js';
27
28
  export * from './grant-revocation.js';
28
29
  export * from './live-query.js';
29
30
  export * from './permission-grant.js';
@@ -36,11 +37,11 @@ export * from './record-data.js';
36
37
  export * from './record-types.js';
37
38
  export * from './repository.js';
38
39
  export * from './repository-types.js';
40
+ export * from './typed-enbox.js';
39
41
  export * from './typed-live-query.js';
40
42
  export * from './typed-record.js';
41
- export * from './typed-web5.js';
42
43
  export * from './vc-api.js';
43
- export * from './web5.js';
44
44
 
45
45
  import * as utils from './utils.js';
46
- export { utils };
46
+ export { utils };
47
+ export { isOk } from './utils.js';
package/src/live-query.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { DwnMessageSubscription, PermissionsApi, Web5Agent } from '@enbox/agent';
1
+ import type { DwnMessageSubscription, EnboxAgent, PermissionsApi } from '@enbox/agent';
2
2
  import type { PaginationCursor, RecordsQueryReplyEntry } from '@enbox/dwn-sdk-js';
3
3
 
4
4
  import { getRecordAuthor } from '@enbox/agent';
@@ -40,7 +40,7 @@ export class RecordChangeEvent extends CustomEvent<RecordChange> {
40
40
  */
41
41
  export type LiveQueryOptions = {
42
42
  /** The agent instance used to construct Record objects. */
43
- agent: Web5Agent;
43
+ agent: EnboxAgent;
44
44
 
45
45
  /** The DID of the connected user. */
46
46
  connectedDid: string;
@@ -3,8 +3,8 @@ import type {
3
3
  DwnPermissionConditions,
4
4
  DwnPermissionScope,
5
5
  DwnResponseStatus,
6
- SendDwnRequest,
7
- Web5Agent
6
+ EnboxAgent,
7
+ SendDwnRequest
8
8
  } from '@enbox/agent';
9
9
 
10
10
  import { Convert } from '@enbox/common';
@@ -80,7 +80,7 @@ export interface PermissionGrantOptions {
80
80
  /** The underlying DWN `RecordsWrite` message along with encoded data that represent the grant */
81
81
  message: DwnDataEncodedRecordsWriteMessage;
82
82
  /** The agent to use when interacting with the underlying DWN record representing the grant */
83
- agent: Web5Agent;
83
+ agent: EnboxAgent;
84
84
  }
85
85
 
86
86
  /**
@@ -127,7 +127,7 @@ export class PermissionGrant implements PermissionGrantModel {
127
127
  }
128
128
 
129
129
  /** The agent to use for this instantiation of the grant */
130
- private get agent(): Web5Agent {
130
+ private get agent(): EnboxAgent {
131
131
  return this._permissions.agent;
132
132
  }
133
133