@enbox/api 0.0.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 (92) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +585 -0
  3. package/dist/browser.js +2226 -0
  4. package/dist/browser.js.map +7 -0
  5. package/dist/browser.mjs +2226 -0
  6. package/dist/browser.mjs.map +7 -0
  7. package/dist/cjs/did-api.js +126 -0
  8. package/dist/cjs/did-api.js.map +1 -0
  9. package/dist/cjs/dwn-api.js +804 -0
  10. package/dist/cjs/dwn-api.js.map +1 -0
  11. package/dist/cjs/grant-revocation.js +183 -0
  12. package/dist/cjs/grant-revocation.js.map +1 -0
  13. package/dist/cjs/index.js +63 -0
  14. package/dist/cjs/index.js.map +1 -0
  15. package/dist/cjs/package.json +1 -0
  16. package/dist/cjs/permission-grant.js +365 -0
  17. package/dist/cjs/permission-grant.js.map +1 -0
  18. package/dist/cjs/permission-request.js +272 -0
  19. package/dist/cjs/permission-request.js.map +1 -0
  20. package/dist/cjs/protocol.js +110 -0
  21. package/dist/cjs/protocol.js.map +1 -0
  22. package/dist/cjs/record.js +1127 -0
  23. package/dist/cjs/record.js.map +1 -0
  24. package/dist/cjs/subscription-util.js +86 -0
  25. package/dist/cjs/subscription-util.js.map +1 -0
  26. package/dist/cjs/utils.js +127 -0
  27. package/dist/cjs/utils.js.map +1 -0
  28. package/dist/cjs/vc-api.js +64 -0
  29. package/dist/cjs/vc-api.js.map +1 -0
  30. package/dist/cjs/web5.js +471 -0
  31. package/dist/cjs/web5.js.map +1 -0
  32. package/dist/esm/did-api.js +69 -0
  33. package/dist/esm/did-api.js.map +1 -0
  34. package/dist/esm/dwn-api.js +573 -0
  35. package/dist/esm/dwn-api.js.map +1 -0
  36. package/dist/esm/grant-revocation.js +109 -0
  37. package/dist/esm/grant-revocation.js.map +1 -0
  38. package/dist/esm/index.js +34 -0
  39. package/dist/esm/index.js.map +1 -0
  40. package/dist/esm/permission-grant.js +233 -0
  41. package/dist/esm/permission-grant.js.map +1 -0
  42. package/dist/esm/permission-request.js +166 -0
  43. package/dist/esm/permission-request.js.map +1 -0
  44. package/dist/esm/protocol.js +67 -0
  45. package/dist/esm/protocol.js.map +1 -0
  46. package/dist/esm/record.js +814 -0
  47. package/dist/esm/record.js.map +1 -0
  48. package/dist/esm/subscription-util.js +35 -0
  49. package/dist/esm/subscription-util.js.map +1 -0
  50. package/dist/esm/utils.js +120 -0
  51. package/dist/esm/utils.js.map +1 -0
  52. package/dist/esm/vc-api.js +30 -0
  53. package/dist/esm/vc-api.js.map +1 -0
  54. package/dist/esm/web5.js +281 -0
  55. package/dist/esm/web5.js.map +1 -0
  56. package/dist/types/did-api.d.ts +66 -0
  57. package/dist/types/did-api.d.ts.map +1 -0
  58. package/dist/types/dwn-api.d.ts +336 -0
  59. package/dist/types/dwn-api.d.ts.map +1 -0
  60. package/dist/types/grant-revocation.d.ts +66 -0
  61. package/dist/types/grant-revocation.d.ts.map +1 -0
  62. package/dist/types/index.d.ts +34 -0
  63. package/dist/types/index.d.ts.map +1 -0
  64. package/dist/types/permission-grant.d.ts +157 -0
  65. package/dist/types/permission-grant.d.ts.map +1 -0
  66. package/dist/types/permission-request.d.ts +108 -0
  67. package/dist/types/permission-request.d.ts.map +1 -0
  68. package/dist/types/protocol.d.ts +59 -0
  69. package/dist/types/protocol.d.ts.map +1 -0
  70. package/dist/types/record.d.ts +441 -0
  71. package/dist/types/record.d.ts.map +1 -0
  72. package/dist/types/subscription-util.d.ts +19 -0
  73. package/dist/types/subscription-util.d.ts.map +1 -0
  74. package/dist/types/utils.d.ts +85 -0
  75. package/dist/types/utils.d.ts.map +1 -0
  76. package/dist/types/vc-api.d.ts +24 -0
  77. package/dist/types/vc-api.d.ts.map +1 -0
  78. package/dist/types/web5.d.ts +219 -0
  79. package/dist/types/web5.d.ts.map +1 -0
  80. package/package.json +111 -0
  81. package/src/did-api.ts +90 -0
  82. package/src/dwn-api.ts +952 -0
  83. package/src/grant-revocation.ts +124 -0
  84. package/src/index.ts +35 -0
  85. package/src/permission-grant.ts +327 -0
  86. package/src/permission-request.ts +214 -0
  87. package/src/protocol.ts +87 -0
  88. package/src/record.ts +1125 -0
  89. package/src/subscription-util.ts +42 -0
  90. package/src/utils.ts +128 -0
  91. package/src/vc-api.ts +30 -0
  92. package/src/web5.ts +516 -0
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Converts various data types to a `Blob` object, automatically detecting the data type or using
3
+ * the specified `dataFormat` to set the Blob's MIME type.
4
+ *
5
+ * This function supports plain text, JSON objects, binary data (Uint8Array, ArrayBuffer), and Blob
6
+ * inputs and will attempt to automatically detect the type of the data if `dataFormat` is not
7
+ * explicitly provided.
8
+ *
9
+ * @beta
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * // Convert a JSON object to a Blob
14
+ * const { dataBlob, dataFormat } = dataToBlob({ key: 'value' }, 'application/json');
15
+ *
16
+ * // Convert a plain text string to a Blob without specifying dataFormat
17
+ * const { dataBlob: textBlob } = dataToBlob('Hello, world!');
18
+ *
19
+ * // Convert binary data to a Blob
20
+ * const binaryData = new Uint8Array([0, 1, 2, 3]);
21
+ * const { dataBlob: binaryBlob } = dataToBlob(binaryData);
22
+ * ```
23
+ *
24
+ * @param data - The data to be converted into a `Blob`. This can be a string, an object, binary
25
+ * data (Uint8Array or ArrayBuffer), or a Blob.
26
+ * @param dataFormat - An optional MIME type string that specifies the format of the data. Common
27
+ * types include 'text/plain' for string data, 'application/json' for JSON
28
+ * objects, and 'application/octet-stream' for binary data. If not provided, the
29
+ * function will attempt to detect the format based on the data type or default
30
+ * to 'application/octet-stream'.
31
+ * @returns An object containing the `dataBlob`, a Blob representation of the input data, and
32
+ * `dataFormat`, the MIME type of the data as determined by the function or specified by the caller.
33
+ * @throws An error if the data type is not supported or cannot be converted to a Blob.
34
+ */
35
+ export declare function dataToBlob(data: any, dataFormat?: string): {
36
+ /** A Blob representation of the input data. */
37
+ dataBlob: Blob;
38
+ /** The MIME type of the data. */
39
+ dataFormat: string;
40
+ };
41
+ /**
42
+ * The `SendCache` class provides a static caching mechanism to optimize the process of sending
43
+ * records to remote DWN targets by minimizing redundant sends.
44
+ *
45
+ * It maintains a cache of record IDs and their associated target DIDs to which they have been sent.
46
+ * This helps in avoiding unnecessary network requests and ensures efficient data synchronization
47
+ * across Decentralized Web Nodes (DWNs).
48
+ *
49
+ * The cache employs a simple eviction policy to maintain a manageable size, ensuring that the cache
50
+ * does not grow indefinitely and consume excessive memory resources.
51
+ *
52
+ * @beta
53
+ */
54
+ export declare class SendCache {
55
+ /**
56
+ * A private static map that serves as the core storage mechanism for the cache. It maps record
57
+ * IDs to a set of target DIDs, indicating which records have been sent to which targets.
58
+ */
59
+ private static cache;
60
+ /**
61
+ * The maximum number of entries allowed in the cache. Once this limit is exceeded, the oldest
62
+ * entries are evicted to make room for new ones. This limit applies both to the number of records
63
+ * and the number of targets per record.
64
+ */
65
+ private static sendCacheLimit;
66
+ /**
67
+ * Checks if a given record ID has been sent to a specified target DID. This method is used to
68
+ * determine whether a send operation is necessary or if it can be skipped to avoid redundancy.
69
+ *
70
+ * @param id - The unique identifier of the record.
71
+ * @param target - The DID of the target to check against.
72
+ * @returns A boolean indicating whether the record has been sent to the target.
73
+ */
74
+ static check(id: string, target: string): boolean;
75
+ /**
76
+ * Adds or updates an entry in the cache for a given record ID and target DID. If the cache
77
+ * exceeds its size limit, the oldest entry is removed. This method ensures that the cache
78
+ * reflects the most recent sends.
79
+ *
80
+ * @param id - The unique identifier of the record.
81
+ * @param target - The DID of the target to which the record has been sent.
82
+ */
83
+ static set(id: string, target: string): void;
84
+ }
85
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,GAAG,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG;IAC1D,+CAA+C;IAC/C,QAAQ,EAAE,IAAI,CAAC;IACf,iCAAiC;IACjC,UAAU,EAAE,MAAM,CAAC;CACpB,CAqBA;AAED;;;;;;;;;;;;GAYG;AACH,qBAAa,SAAS;IACpB;;;MAGE;IACF,OAAO,CAAC,MAAM,CAAC,KAAK,CAAkC;IAEtD;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,cAAc,CAAO;IAEpC;;;;;;;OAOG;WACW,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO;IAKxD;;;;;;;OAOG;WACW,GAAG,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;CAepD"}
@@ -0,0 +1,24 @@
1
+ import type { Web5Agent } from '@enbox/agent';
2
+ /**
3
+ * The VC API is used to issue, present and verify VCs
4
+ *
5
+ * @beta
6
+ */
7
+ export declare class VcApi {
8
+ /**
9
+ * Holds the instance of a {@link Web5Agent} that represents the current execution context for
10
+ * the `VcApi`. This agent is used to process VC requests.
11
+ */
12
+ private agent;
13
+ /** The DID of the tenant under which DID operations are being performed. */
14
+ private connectedDid;
15
+ constructor(options: {
16
+ agent: Web5Agent;
17
+ connectedDid: string;
18
+ });
19
+ /**
20
+ * Issues a VC (Not implemented yet)
21
+ */
22
+ create(): Promise<void>;
23
+ }
24
+ //# sourceMappingURL=vc-api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vc-api.d.ts","sourceRoot":"","sources":["../../src/vc-api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C;;;;GAIG;AACH,qBAAa,KAAK;IAChB;;;OAGG;IACH,OAAO,CAAC,KAAK,CAAY;IAEzB,4EAA4E;IAC5E,OAAO,CAAC,YAAY,CAAS;gBAEjB,OAAO,EAAE;QAAE,KAAK,EAAE,SAAS,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE;IAK/D;;OAEG;IACG,MAAM;CAIb"}
@@ -0,0 +1,219 @@
1
+ /**
2
+ * NOTE: Added reference types here to avoid a `pnpm` bug during build.
3
+ * https://github.com/TBD54566975/web5-js/pull/507
4
+ */
5
+ import type { DwnDataEncodedRecordsWriteMessage, DwnProtocolDefinition, HdIdentityVault, Permission, WalletConnectOptions, Web5Agent } from '@enbox/agent';
6
+ import { DidApi } from './did-api.js';
7
+ import { DwnApi } from './dwn-api.js';
8
+ import { VcApi } from './vc-api.js';
9
+ /** Override defaults configured during the technical preview phase. */
10
+ export type TechPreviewOptions = {
11
+ /** Override default dwnEndpoints provided for technical preview. */
12
+ dwnEndpoints?: string[];
13
+ };
14
+ /** Override defaults for DID creation. */
15
+ export type DidCreateOptions = {
16
+ /** Override default dwnEndpoints provided during DID creation. */
17
+ dwnEndpoints?: string[];
18
+ };
19
+ /**
20
+ * Represents a permission request for a protocol definition.
21
+ */
22
+ export type ConnectPermissionRequest = {
23
+ /**
24
+ * The protocol definition for the protocol being requested.
25
+ */
26
+ protocolDefinition: DwnProtocolDefinition;
27
+ /**
28
+ * The permissions being requested for the protocol. If none are provided, the default is to request all permissions.
29
+ */
30
+ permissions?: Permission[];
31
+ };
32
+ /**
33
+ * Options for connecting to a Web5 agent. This includes the ability to connect to an external wallet.
34
+ *
35
+ * NOTE: the returned `ConnectPermissionRequest` type is different to the `ConnectPermissionRequest` type in the `@enbox/agent` package.
36
+ */
37
+ export type ConnectOptions = Omit<WalletConnectOptions, 'permissionRequests'> & {
38
+ /** The user friendly name of the client/app to be displayed when prompting end-user with permission requests. */
39
+ displayName: string;
40
+ /**
41
+ * The permissions that are being requested for the connected DID.
42
+ * This is used to create the {@link ConnectPermissionRequest} for the wallet connect flow.
43
+ */
44
+ permissionRequests: ConnectPermissionRequest[];
45
+ };
46
+ /** Optional overrides that can be provided when calling {@link Web5.connect}. */
47
+ export type Web5ConnectOptions = {
48
+ /**
49
+ * When specified, external wallet connect flow is triggered.
50
+ * This param currently will not work in apps that are currently connected.
51
+ * It must only be invoked at registration with a reset and empty DWN and agent.
52
+ */
53
+ walletConnectOptions?: ConnectOptions;
54
+ /**
55
+ * Provide a {@link Web5Agent} implementation. Defaults to creating a local
56
+ * {@link Web5UserAgent} if one isn't provided
57
+ **/
58
+ agent?: Web5Agent;
59
+ /**
60
+ * Provide an instance of a {@link HdIdentityVault} implementation. Defaults to
61
+ * a LevelDB-backed store with an insecure, static unlock password if one
62
+ * isn't provided. To allow the app user to enter a secure password of
63
+ * their choosing, provide an initialized {@link HdIdentityVault} instance.
64
+ **/
65
+ agentVault?: HdIdentityVault;
66
+ /** Specify an existing DID to connect to. */
67
+ connectedDid?: string;
68
+ /**
69
+ * The Web5 app `password` is used to protect data on the device the application is running on.
70
+ *
71
+ * Only the end user should know this password: it should not be stored on the device or
72
+ * transmitted over the network.
73
+ *
74
+ * This password is crucial for the security of an identity vault that stores the local Agent's
75
+ * cryptographic keys and decentralized identifier (DID). The vault's content is encrypted using
76
+ * the password, making it accessible only to those who know the password.
77
+ *
78
+ * App users should be advised to use a strong, unique passphrase that is not shared across
79
+ * different services or applications. The password should be kept confidential and not be
80
+ * exposed to unauthorized entities. Losing the password may result in irreversible loss of
81
+ * access to the vault's contents.
82
+ */
83
+ password?: string;
84
+ /**
85
+ * The `recoveryPhrase` is a unique, secure key for recovering the identity vault.
86
+ *
87
+ * This phrase is a series of 12 words generated securely and known only to the user. It plays a
88
+ * critical role in the security of the identity vault by enabling the recovery of the vault's
89
+ * contents, including cryptographic keys and the Agent's decentralized identifier (DID), across
90
+ * different devices or if the original device is compromised or lost.
91
+ *
92
+ * The recovery phrase is akin to a master key, as anyone with access to this phrase can restore
93
+ * and access the vault's contents. It’s combined with the app `password` to encrypt the vault's
94
+ * content.
95
+ *
96
+ * Unlike a password, the recovery phrase is not intended for regular use but as a secure backup
97
+ * method for vault recovery. Losing this phrase can result in permanent loss of access to the
98
+ * vault's contents, as it cannot be reset or retrieved if forgotten.
99
+ *
100
+ * Users should treat the recovery phrase with the highest level of security, ensuring it is
101
+ * never shared, stored online, or exposed to potential threats. It is the user's responsibility
102
+ * to keep this phrase safe to maintain the integrity and accessibility of their secured data. It
103
+ * is recommended to write it down and store it in a secure location, separate from the device and
104
+ * digital backups.
105
+ */
106
+ recoveryPhrase?: string;
107
+ /**
108
+ * Enable synchronization of DWN records between local and remote DWNs.
109
+ * Sync defaults to running every 2 minutes and can be set to any value accepted by `ms()`.
110
+ * To disable sync set to 'off'.
111
+ */
112
+ sync?: string;
113
+ /**
114
+ * Override defaults configured during the technical preview phase.
115
+ * See {@link TechPreviewOptions} for available options.
116
+ */
117
+ techPreview?: TechPreviewOptions;
118
+ /**
119
+ * Override defaults configured options for creating a DID during connect.
120
+ * See {@link DidCreateOptions} for available options.
121
+ */
122
+ didCreateOptions?: DidCreateOptions;
123
+ /**
124
+ * If the `registration` option is provided, the agent DID and the connected DID will be registered with the DWN endpoints provided by `techPreview` or `didCreateOptions`.
125
+ *
126
+ * If registration fails, the `onFailure` callback will be called with the error.
127
+ * If registration is successful, the `onSuccess` callback will be called.
128
+ */
129
+ registration?: {
130
+ /** Called when all of the DWN registrations are successful */
131
+ onSuccess: () => void;
132
+ /** Called when any of the DWN registrations fail */
133
+ onFailure: (error: any) => void;
134
+ };
135
+ };
136
+ /**
137
+ * Represents the result of the Web5 connection process, including the Web5 instance,
138
+ * the connected decentralized identifier (DID), and optionally the recovery phrase used
139
+ * during the agent's initialization.
140
+ */
141
+ export type Web5ConnectResult = {
142
+ /** The Web5 instance, providing access to the agent, DID, DWN, and VC APIs. */
143
+ web5: Web5;
144
+ /** The DID that has been connected or created during the connection process. */
145
+ did: string;
146
+ /**
147
+ * The first time a Web5 agent is initialized, the recovery phrase that was used to generate the
148
+ * agent's DID and keys is returned. This phrase can be used to recover the agent's vault contents
149
+ * and should be stored securely by the user.
150
+ */
151
+ recoveryPhrase?: string;
152
+ /**
153
+ * The resulting did of a successful wallet connect. Only returned on success if
154
+ * {@link WalletConnectOptions} was provided.
155
+ */
156
+ delegateDid?: string;
157
+ };
158
+ /**
159
+ * Parameters that are passed to Web5 constructor.
160
+ *
161
+ * @see {@link Web5ConnectOptions}
162
+ */
163
+ export type Web5Params = {
164
+ /**
165
+ * A {@link Web5Agent} instance that handles DIDs, DWNs and VCs requests. The agent manages the
166
+ * user keys and identities, and is responsible to sign and verify messages.
167
+ */
168
+ agent: Web5Agent;
169
+ /** The DID of the tenant under which all DID, DWN, and VC requests are being performed. */
170
+ connectedDid: string;
171
+ /** The DID that will be signing Web5 messages using grants from the connectedDid */
172
+ delegateDid?: string;
173
+ };
174
+ /**
175
+ * The main Web5 API interface. It manages the creation of a DID if needed, the connection to the
176
+ * local DWN and all the web5 main foundational APIs such as VC, syncing, etc.
177
+ */
178
+ export declare class Web5 {
179
+ /**
180
+ * A {@link Web5Agent} instance that handles DIDs, DWNs and VCs requests. The agent manages the
181
+ * user keys and identities, and is responsible to sign and verify messages.
182
+ */
183
+ agent: Web5Agent;
184
+ /** Exposed instance to the DID APIs, allow users to create and resolve DIDs */
185
+ did: DidApi;
186
+ /** Exposed instance to the DWN APIs, allow users to read/write records */
187
+ dwn: DwnApi;
188
+ /** Exposed instance to the VC APIs, allow users to issue, present and verify VCs */
189
+ vc: VcApi;
190
+ constructor({ agent, connectedDid, delegateDid }: Web5Params);
191
+ /**
192
+ * Connects to a {@link Web5Agent}. Defaults to creating a local {@link Web5UserAgent} if one
193
+ * isn't provided.
194
+ *
195
+ * If `walletConnectOptions` are provided, a WalletConnect flow will be initiated to import a delegated DID from an external wallet.
196
+ * If there is a failure at any point during connecting and processing grants, all created DIDs and Identities as well as the provided grants
197
+ * will be cleaned up and an error thrown. This allows for subsequent Connect attempts to be made without any errors.
198
+ *
199
+ * @param options - Optional overrides that can be provided when calling {@link Web5.connect}.
200
+ * @returns A promise that resolves to a {@link Web5} instance and the connected DID.
201
+ */
202
+ static connect({ agent, agentVault, connectedDid, password, recoveryPhrase, sync, techPreview, didCreateOptions, registration, walletConnectOptions, }?: Web5ConnectOptions): Promise<Web5ConnectResult>;
203
+ /**
204
+ * Cleans up the DID, Keys and Identity. Primarily used by a failed WalletConnect import.
205
+ * Does not throw on error, but logs to console.
206
+ */
207
+ private static cleanUpIdentity;
208
+ /**
209
+ * A static method to process connected grants for a delegate DID.
210
+ *
211
+ * This will store the grants as the DWN owner to be used later when impersonating the connected DID.
212
+ */
213
+ static processConnectedGrants({ grants, agent, delegateDid }: {
214
+ grants: DwnDataEncodedRecordsWriteMessage[];
215
+ agent: Web5Agent;
216
+ delegateDid: string;
217
+ }): Promise<string[]>;
218
+ }
219
+ //# sourceMappingURL=web5.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web5.d.ts","sourceRoot":"","sources":["../../src/web5.ts"],"names":[],"mappings":"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"}
package/package.json ADDED
@@ -0,0 +1,111 @@
1
+ {
2
+ "name": "@enbox/api",
3
+ "version": "0.0.1",
4
+ "description": "SDK for accessing the features and capabilities of Web5",
5
+ "type": "module",
6
+ "main": "./dist/cjs/index.js",
7
+ "module": "./dist/esm/index.js",
8
+ "types": "./dist/types/index.d.ts",
9
+ "scripts": {
10
+ "clean": "rimraf dist tests/compiled",
11
+ "build:esm": "rimraf dist/esm dist/types && pnpm tsc -p tsconfig.json",
12
+ "build:cjs": "rimraf dist/cjs && pnpm tsc -p tsconfig.cjs.json && echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json",
13
+ "build:browser": "rimraf dist/browser.mjs dist/browser.js && node build/bundles.js",
14
+ "build:tests:node": "rimraf tests/compiled && pnpm tsc -p tests/tsconfig.json",
15
+ "build:tests:browser": "rimraf tests/compiled && node build/esbuild-tests.cjs",
16
+ "build": "pnpm clean && pnpm build:esm && pnpm build:cjs && pnpm build:browser",
17
+ "lint": "eslint . --max-warnings 0",
18
+ "lint:fix": "eslint . --fix",
19
+ "test:node": "pnpm build:tests:node && pnpm c8 mocha",
20
+ "test:browser": "pnpm build:tests:browser && web-test-runner"
21
+ },
22
+ "homepage": "https://github.com/enboxorg/enbox/tree/main/packages/api#readme",
23
+ "bugs": "https://github.com/enboxorg/enbox/issues",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/enboxorg/enbox.git",
27
+ "directory": "packages/api"
28
+ },
29
+ "license": "Apache-2.0",
30
+ "contributors": [
31
+ {
32
+ "name": "Daniel Buchner",
33
+ "url": "https://github.com/csuwildcat"
34
+ },
35
+ {
36
+ "name": "Frank Hinek",
37
+ "url": "https://github.com/frankhinek"
38
+ },
39
+ {
40
+ "name": "Moe Jangda",
41
+ "url": "https://github.com/mistermoe"
42
+ }
43
+ ],
44
+ "files": [
45
+ "dist",
46
+ "src"
47
+ ],
48
+ "exports": {
49
+ ".": {
50
+ "types": "./dist/types/index.d.ts",
51
+ "import": "./dist/esm/index.js",
52
+ "require": "./dist/cjs/index.js"
53
+ },
54
+ "./browser": {
55
+ "types": "./dist/types/index.d.ts",
56
+ "import": "./dist/browser.mjs",
57
+ "require": "./dist/browser.js"
58
+ }
59
+ },
60
+ "react-native": "./dist/esm/index.js",
61
+ "keywords": [
62
+ "decentralized",
63
+ "decentralized-applications",
64
+ "decentralized-identity",
65
+ "decentralized-web",
66
+ "DID",
67
+ "sdk",
68
+ "verifiable-credentials",
69
+ "web5",
70
+ "web5-sdk"
71
+ ],
72
+ "publishConfig": {
73
+ "access": "public"
74
+ },
75
+ "engines": {
76
+ "node": ">=18.0.0"
77
+ },
78
+ "dependencies": {
79
+ "@enbox/agent": "workspace:*",
80
+ "@enbox/common": "workspace:*",
81
+ "@enbox/crypto": "workspace:*",
82
+ "@enbox/dids": "workspace:*",
83
+ "@enbox/user-agent": "workspace:*"
84
+ },
85
+ "devDependencies": {
86
+ "@playwright/test": "1.45.3",
87
+ "@enbox/dwn-sdk-js": "workspace:*",
88
+ "@types/chai": "4.3.6",
89
+ "@types/eslint": "8.56.10",
90
+ "@types/mocha": "10.0.1",
91
+ "@types/node": "20.14.8",
92
+ "@types/sinon": "17.0.3",
93
+ "@typescript-eslint/eslint-plugin": "7.9.0",
94
+ "@typescript-eslint/parser": "7.14.1",
95
+ "@web/test-runner": "0.18.2",
96
+ "@web/test-runner-playwright": "0.11.0",
97
+ "c8": "9.1.0",
98
+ "chai": "4.3.10",
99
+ "esbuild": "0.19.8",
100
+ "eslint": "9.3.0",
101
+ "eslint-plugin-mocha": "10.4.3",
102
+ "mocha": "10.2.0",
103
+ "mocha-junit-reporter": "2.2.1",
104
+ "node-stdlib-browser": "1.2.0",
105
+ "playwright": "1.45.3",
106
+ "rimraf": "4.4.0",
107
+ "sinon": "18.0.0",
108
+ "source-map-loader": "4.0.2",
109
+ "typescript": "5.1.6"
110
+ }
111
+ }
package/src/did-api.ts ADDED
@@ -0,0 +1,90 @@
1
+ import type { DidCreateParams, DidMessageResult, DidResolveParams, ResponseStatus, Web5Agent } from '@enbox/agent';
2
+
3
+ import { DidInterface } from '@enbox/agent';
4
+
5
+ /**
6
+ * Parameters for creating a DID, specifying the method, options for the DID method, and whether to
7
+ * store the DID.
8
+ *
9
+ * @typeParam method - The DID method to use for creating the DID.
10
+ * @typeParam options - Method-specific options for creating the DID.
11
+ * @typeParam store - Indicates whether the newly created DID should be stored.
12
+ */
13
+ export type DidCreateRequest = Pick<DidCreateParams, 'method' | 'options' | 'store'>;
14
+
15
+ /**
16
+ * The response from a DID creation request, including the operation's status and, if successful,
17
+ * the created DID.
18
+ */
19
+ export type DidCreateResponse = ResponseStatus & {
20
+ /** The result of the DID creation operation, containing the newly created DID, if successful */
21
+ did?: DidMessageResult[DidInterface.Create];
22
+ };
23
+
24
+ /**
25
+ * The response from resolving a DID, containing the DID Resolution Result.
26
+ *
27
+ * This type directly maps to the result of a DID resolution operation, providing detailed
28
+ * information about the DID document, including any DID document metadata and DID resolution
29
+ * metadata,
30
+ */
31
+ export type DidResolveResponse = DidMessageResult[DidInterface.Resolve];
32
+
33
+ /**
34
+ * The DID API is used to resolve DIDs.
35
+ *
36
+ * @beta
37
+ */
38
+ export class DidApi {
39
+ /**
40
+ * Holds the instance of a {@link Web5Agent} that represents the current execution context for
41
+ * the `DidApi`. This agent is used to process DID requests.
42
+ */
43
+ private agent: Web5Agent;
44
+
45
+ /** The DID of the tenant under which DID operations are being performed. */
46
+ private connectedDid: string;
47
+
48
+ constructor(options: { agent: Web5Agent, connectedDid: string }) {
49
+ this.agent = options.agent;
50
+ this.connectedDid = options.connectedDid;
51
+ }
52
+
53
+ /**
54
+ * Initiates the creation of a Decentralized Identifier (DID) using the specified method, options,
55
+ * and storage preference.
56
+ *
57
+ * This method sends a request to the Web5 Agent to create a new DID based on the provided method,
58
+ * with method-specific options. It also specifies whether the newly created DID should be stored.
59
+ *
60
+ * @param request - The request parameters for creating a DID, including the method, options, and
61
+ * storage flag.
62
+ * @returns A promise that resolves to a `DidCreateResponse`, which includes the operation's
63
+ * status and, if successful, the newly created DID.
64
+ */
65
+ public async create(request: DidCreateRequest): Promise<DidCreateResponse> {
66
+ const { result, ...status } = await this.agent.processDidRequest({
67
+ messageType : DidInterface.Create,
68
+ messageParams : { ...request }
69
+ });
70
+
71
+ return { did: result, ...status };
72
+ }
73
+
74
+ /**
75
+ * Resolves a DID to a DID Resolution Result.
76
+ *
77
+ * @param didUri - The DID or DID URL to resolve.
78
+ * @returns A promise that resolves to the DID Resolution Result.
79
+ */
80
+ public async resolve(
81
+ didUri: DidResolveParams['didUri'], options?: DidResolveParams['options']
82
+ ): Promise<DidResolveResponse> {
83
+ const { result: didResolutionResult } = await this.agent.processDidRequest({
84
+ messageParams : { didUri, options },
85
+ messageType : DidInterface.Resolve
86
+ });
87
+
88
+ return didResolutionResult;
89
+ }
90
+ }