@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.
- package/LICENSE +201 -0
- package/README.md +585 -0
- package/dist/browser.js +2226 -0
- package/dist/browser.js.map +7 -0
- package/dist/browser.mjs +2226 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/cjs/did-api.js +126 -0
- package/dist/cjs/did-api.js.map +1 -0
- package/dist/cjs/dwn-api.js +804 -0
- package/dist/cjs/dwn-api.js.map +1 -0
- package/dist/cjs/grant-revocation.js +183 -0
- package/dist/cjs/grant-revocation.js.map +1 -0
- package/dist/cjs/index.js +63 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/permission-grant.js +365 -0
- package/dist/cjs/permission-grant.js.map +1 -0
- package/dist/cjs/permission-request.js +272 -0
- package/dist/cjs/permission-request.js.map +1 -0
- package/dist/cjs/protocol.js +110 -0
- package/dist/cjs/protocol.js.map +1 -0
- package/dist/cjs/record.js +1127 -0
- package/dist/cjs/record.js.map +1 -0
- package/dist/cjs/subscription-util.js +86 -0
- package/dist/cjs/subscription-util.js.map +1 -0
- package/dist/cjs/utils.js +127 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/cjs/vc-api.js +64 -0
- package/dist/cjs/vc-api.js.map +1 -0
- package/dist/cjs/web5.js +471 -0
- package/dist/cjs/web5.js.map +1 -0
- package/dist/esm/did-api.js +69 -0
- package/dist/esm/did-api.js.map +1 -0
- package/dist/esm/dwn-api.js +573 -0
- package/dist/esm/dwn-api.js.map +1 -0
- package/dist/esm/grant-revocation.js +109 -0
- package/dist/esm/grant-revocation.js.map +1 -0
- package/dist/esm/index.js +34 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/permission-grant.js +233 -0
- package/dist/esm/permission-grant.js.map +1 -0
- package/dist/esm/permission-request.js +166 -0
- package/dist/esm/permission-request.js.map +1 -0
- package/dist/esm/protocol.js +67 -0
- package/dist/esm/protocol.js.map +1 -0
- package/dist/esm/record.js +814 -0
- package/dist/esm/record.js.map +1 -0
- package/dist/esm/subscription-util.js +35 -0
- package/dist/esm/subscription-util.js.map +1 -0
- package/dist/esm/utils.js +120 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/esm/vc-api.js +30 -0
- package/dist/esm/vc-api.js.map +1 -0
- package/dist/esm/web5.js +281 -0
- package/dist/esm/web5.js.map +1 -0
- package/dist/types/did-api.d.ts +66 -0
- package/dist/types/did-api.d.ts.map +1 -0
- package/dist/types/dwn-api.d.ts +336 -0
- package/dist/types/dwn-api.d.ts.map +1 -0
- package/dist/types/grant-revocation.d.ts +66 -0
- package/dist/types/grant-revocation.d.ts.map +1 -0
- package/dist/types/index.d.ts +34 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/permission-grant.d.ts +157 -0
- package/dist/types/permission-grant.d.ts.map +1 -0
- package/dist/types/permission-request.d.ts +108 -0
- package/dist/types/permission-request.d.ts.map +1 -0
- package/dist/types/protocol.d.ts +59 -0
- package/dist/types/protocol.d.ts.map +1 -0
- package/dist/types/record.d.ts +441 -0
- package/dist/types/record.d.ts.map +1 -0
- package/dist/types/subscription-util.d.ts +19 -0
- package/dist/types/subscription-util.d.ts.map +1 -0
- package/dist/types/utils.d.ts +85 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/types/vc-api.d.ts +24 -0
- package/dist/types/vc-api.d.ts.map +1 -0
- package/dist/types/web5.d.ts +219 -0
- package/dist/types/web5.d.ts.map +1 -0
- package/package.json +111 -0
- package/src/did-api.ts +90 -0
- package/src/dwn-api.ts +952 -0
- package/src/grant-revocation.ts +124 -0
- package/src/index.ts +35 -0
- package/src/permission-grant.ts +327 -0
- package/src/permission-request.ts +214 -0
- package/src/protocol.ts +87 -0
- package/src/record.ts +1125 -0
- package/src/subscription-util.ts +42 -0
- package/src/utils.ts +128 -0
- package/src/vc-api.ts +30 -0
- package/src/web5.ts +516 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { DwnRecordSubscriptionHandler, getRecordAuthor, PermissionsApi, Web5Agent } from '@enbox/agent';
|
|
2
|
+
import { RecordsSubscribeRequest } from './dwn-api.js';
|
|
3
|
+
import { Record } from './record.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Utility class for dealing with subscriptions.
|
|
7
|
+
*/
|
|
8
|
+
export class SubscriptionUtil {
|
|
9
|
+
/**
|
|
10
|
+
* Creates a record subscription handler that can be used to process incoming {Record} messages.
|
|
11
|
+
*/
|
|
12
|
+
static recordSubscriptionHandler({ agent, connectedDid, request, delegateDid, protocolRole, permissionsApi }:{
|
|
13
|
+
agent: Web5Agent;
|
|
14
|
+
connectedDid: string;
|
|
15
|
+
delegateDid?: string;
|
|
16
|
+
protocolRole?: string;
|
|
17
|
+
permissionsApi?: PermissionsApi;
|
|
18
|
+
request: RecordsSubscribeRequest;
|
|
19
|
+
}): DwnRecordSubscriptionHandler {
|
|
20
|
+
const { subscriptionHandler, from: remoteOrigin } = request;
|
|
21
|
+
|
|
22
|
+
return async (event) => {
|
|
23
|
+
const { message, initialWrite } = event;
|
|
24
|
+
const author = getRecordAuthor(message);
|
|
25
|
+
const recordOptions = {
|
|
26
|
+
author,
|
|
27
|
+
connectedDid,
|
|
28
|
+
remoteOrigin,
|
|
29
|
+
initialWrite
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const record = new Record(agent, {
|
|
33
|
+
...message,
|
|
34
|
+
...recordOptions,
|
|
35
|
+
protocolRole,
|
|
36
|
+
delegateDid: delegateDid,
|
|
37
|
+
}, permissionsApi);
|
|
38
|
+
|
|
39
|
+
subscriptionHandler(record);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { Convert, universalTypeOf } from '@enbox/common';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts various data types to a `Blob` object, automatically detecting the data type or using
|
|
5
|
+
* the specified `dataFormat` to set the Blob's MIME type.
|
|
6
|
+
*
|
|
7
|
+
* This function supports plain text, JSON objects, binary data (Uint8Array, ArrayBuffer), and Blob
|
|
8
|
+
* inputs and will attempt to automatically detect the type of the data if `dataFormat` is not
|
|
9
|
+
* explicitly provided.
|
|
10
|
+
*
|
|
11
|
+
* @beta
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* // Convert a JSON object to a Blob
|
|
16
|
+
* const { dataBlob, dataFormat } = dataToBlob({ key: 'value' }, 'application/json');
|
|
17
|
+
*
|
|
18
|
+
* // Convert a plain text string to a Blob without specifying dataFormat
|
|
19
|
+
* const { dataBlob: textBlob } = dataToBlob('Hello, world!');
|
|
20
|
+
*
|
|
21
|
+
* // Convert binary data to a Blob
|
|
22
|
+
* const binaryData = new Uint8Array([0, 1, 2, 3]);
|
|
23
|
+
* const { dataBlob: binaryBlob } = dataToBlob(binaryData);
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @param data - The data to be converted into a `Blob`. This can be a string, an object, binary
|
|
27
|
+
* data (Uint8Array or ArrayBuffer), or a Blob.
|
|
28
|
+
* @param dataFormat - An optional MIME type string that specifies the format of the data. Common
|
|
29
|
+
* types include 'text/plain' for string data, 'application/json' for JSON
|
|
30
|
+
* objects, and 'application/octet-stream' for binary data. If not provided, the
|
|
31
|
+
* function will attempt to detect the format based on the data type or default
|
|
32
|
+
* to 'application/octet-stream'.
|
|
33
|
+
* @returns An object containing the `dataBlob`, a Blob representation of the input data, and
|
|
34
|
+
* `dataFormat`, the MIME type of the data as determined by the function or specified by the caller.
|
|
35
|
+
* @throws An error if the data type is not supported or cannot be converted to a Blob.
|
|
36
|
+
*/
|
|
37
|
+
export function dataToBlob(data: any, dataFormat?: string): {
|
|
38
|
+
/** A Blob representation of the input data. */
|
|
39
|
+
dataBlob: Blob;
|
|
40
|
+
/** The MIME type of the data. */
|
|
41
|
+
dataFormat: string;
|
|
42
|
+
} {
|
|
43
|
+
let dataBlob: Blob;
|
|
44
|
+
|
|
45
|
+
// Check for Object or String, and if neither, assume bytes.
|
|
46
|
+
const detectedType = universalTypeOf(data);
|
|
47
|
+
if (dataFormat === 'text/plain' || detectedType === 'String') {
|
|
48
|
+
dataBlob = new Blob([data], { type: 'text/plain' });
|
|
49
|
+
} else if (dataFormat === 'application/json' || detectedType === 'Object') {
|
|
50
|
+
const dataBytes = Convert.object(data).toUint8Array();
|
|
51
|
+
dataBlob = new Blob([dataBytes], { type: 'application/json' });
|
|
52
|
+
} else if (detectedType === 'Uint8Array' || detectedType === 'ArrayBuffer') {
|
|
53
|
+
dataBlob = new Blob([data], { type: 'application/octet-stream' });
|
|
54
|
+
} else if (detectedType === 'Blob') {
|
|
55
|
+
dataBlob = data;
|
|
56
|
+
} else {
|
|
57
|
+
throw new Error('data type not supported.');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
dataFormat = dataFormat || dataBlob.type || 'application/octet-stream';
|
|
61
|
+
|
|
62
|
+
return { dataBlob, dataFormat };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The `SendCache` class provides a static caching mechanism to optimize the process of sending
|
|
67
|
+
* records to remote DWN targets by minimizing redundant sends.
|
|
68
|
+
*
|
|
69
|
+
* It maintains a cache of record IDs and their associated target DIDs to which they have been sent.
|
|
70
|
+
* This helps in avoiding unnecessary network requests and ensures efficient data synchronization
|
|
71
|
+
* across Decentralized Web Nodes (DWNs).
|
|
72
|
+
*
|
|
73
|
+
* The cache employs a simple eviction policy to maintain a manageable size, ensuring that the cache
|
|
74
|
+
* does not grow indefinitely and consume excessive memory resources.
|
|
75
|
+
*
|
|
76
|
+
* @beta
|
|
77
|
+
*/
|
|
78
|
+
export class SendCache {
|
|
79
|
+
/**
|
|
80
|
+
* A private static map that serves as the core storage mechanism for the cache. It maps record
|
|
81
|
+
* IDs to a set of target DIDs, indicating which records have been sent to which targets.
|
|
82
|
+
*/
|
|
83
|
+
private static cache = new Map<string, Set<string>>();
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The maximum number of entries allowed in the cache. Once this limit is exceeded, the oldest
|
|
87
|
+
* entries are evicted to make room for new ones. This limit applies both to the number of records
|
|
88
|
+
* and the number of targets per record.
|
|
89
|
+
*/
|
|
90
|
+
private static sendCacheLimit = 100;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Checks if a given record ID has been sent to a specified target DID. This method is used to
|
|
94
|
+
* determine whether a send operation is necessary or if it can be skipped to avoid redundancy.
|
|
95
|
+
*
|
|
96
|
+
* @param id - The unique identifier of the record.
|
|
97
|
+
* @param target - The DID of the target to check against.
|
|
98
|
+
* @returns A boolean indicating whether the record has been sent to the target.
|
|
99
|
+
*/
|
|
100
|
+
public static check(id: string, target: string): boolean {
|
|
101
|
+
let targetCache = SendCache.cache.get(id);
|
|
102
|
+
return targetCache ? targetCache.has(target) : false;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Adds or updates an entry in the cache for a given record ID and target DID. If the cache
|
|
107
|
+
* exceeds its size limit, the oldest entry is removed. This method ensures that the cache
|
|
108
|
+
* reflects the most recent sends.
|
|
109
|
+
*
|
|
110
|
+
* @param id - The unique identifier of the record.
|
|
111
|
+
* @param target - The DID of the target to which the record has been sent.
|
|
112
|
+
*/
|
|
113
|
+
public static set(id: string, target: string): void {
|
|
114
|
+
let targetCache = SendCache.cache.get(id) || new Set();
|
|
115
|
+
SendCache.cache.delete(id);
|
|
116
|
+
SendCache.cache.set(id, targetCache);
|
|
117
|
+
if (this.cache.size > SendCache.sendCacheLimit) {
|
|
118
|
+
const firstRecord = SendCache.cache.keys().next().value;
|
|
119
|
+
SendCache.cache.delete(firstRecord);
|
|
120
|
+
}
|
|
121
|
+
targetCache.delete(target);
|
|
122
|
+
targetCache.add(target);
|
|
123
|
+
if (targetCache.size > SendCache.sendCacheLimit) {
|
|
124
|
+
const firstTarget = targetCache.keys().next().value;
|
|
125
|
+
targetCache.delete(firstTarget);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
package/src/vc-api.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Web5Agent } from '@enbox/agent';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The VC API is used to issue, present and verify VCs
|
|
5
|
+
*
|
|
6
|
+
* @beta
|
|
7
|
+
*/
|
|
8
|
+
export class VcApi {
|
|
9
|
+
/**
|
|
10
|
+
* Holds the instance of a {@link Web5Agent} that represents the current execution context for
|
|
11
|
+
* the `VcApi`. This agent is used to process VC requests.
|
|
12
|
+
*/
|
|
13
|
+
private agent: Web5Agent;
|
|
14
|
+
|
|
15
|
+
/** The DID of the tenant under which DID operations are being performed. */
|
|
16
|
+
private connectedDid: string;
|
|
17
|
+
|
|
18
|
+
constructor(options: { agent: Web5Agent, connectedDid: string }) {
|
|
19
|
+
this.agent = options.agent;
|
|
20
|
+
this.connectedDid = options.connectedDid;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Issues a VC (Not implemented yet)
|
|
25
|
+
*/
|
|
26
|
+
async create() {
|
|
27
|
+
// TODO: implement
|
|
28
|
+
throw new Error('Not implemented.');
|
|
29
|
+
}
|
|
30
|
+
}
|