@enbox/agent 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/dist/browser.mjs +11 -11
  2. package/dist/browser.mjs.map +4 -4
  3. package/dist/esm/anonymous-dwn-api.js +184 -0
  4. package/dist/esm/anonymous-dwn-api.js.map +1 -0
  5. package/dist/esm/dwn-api.js +86 -777
  6. package/dist/esm/dwn-api.js.map +1 -1
  7. package/dist/esm/dwn-encryption.js +342 -0
  8. package/dist/esm/dwn-encryption.js.map +1 -0
  9. package/dist/esm/dwn-key-delivery.js +256 -0
  10. package/dist/esm/dwn-key-delivery.js.map +1 -0
  11. package/dist/esm/dwn-record-upgrade.js +119 -0
  12. package/dist/esm/dwn-record-upgrade.js.map +1 -0
  13. package/dist/esm/dwn-type-guards.js +23 -0
  14. package/dist/esm/dwn-type-guards.js.map +1 -0
  15. package/dist/esm/index.js +6 -0
  16. package/dist/esm/index.js.map +1 -1
  17. package/dist/esm/protocol-utils.js +158 -0
  18. package/dist/esm/protocol-utils.js.map +1 -0
  19. package/dist/esm/store-data-protocols.js +1 -1
  20. package/dist/esm/store-data-protocols.js.map +1 -1
  21. package/dist/esm/sync-engine-level.js +22 -353
  22. package/dist/esm/sync-engine-level.js.map +1 -1
  23. package/dist/esm/sync-messages.js +234 -0
  24. package/dist/esm/sync-messages.js.map +1 -0
  25. package/dist/esm/sync-topological-sort.js +143 -0
  26. package/dist/esm/sync-topological-sort.js.map +1 -0
  27. package/dist/esm/test-harness.js +20 -0
  28. package/dist/esm/test-harness.js.map +1 -1
  29. package/dist/types/anonymous-dwn-api.d.ts +140 -0
  30. package/dist/types/anonymous-dwn-api.d.ts.map +1 -0
  31. package/dist/types/dwn-api.d.ts +36 -179
  32. package/dist/types/dwn-api.d.ts.map +1 -1
  33. package/dist/types/dwn-encryption.d.ts +144 -0
  34. package/dist/types/dwn-encryption.d.ts.map +1 -0
  35. package/dist/types/dwn-key-delivery.d.ts +112 -0
  36. package/dist/types/dwn-key-delivery.d.ts.map +1 -0
  37. package/dist/types/dwn-record-upgrade.d.ts +33 -0
  38. package/dist/types/dwn-record-upgrade.d.ts.map +1 -0
  39. package/dist/types/dwn-type-guards.d.ts +9 -0
  40. package/dist/types/dwn-type-guards.d.ts.map +1 -0
  41. package/dist/types/index.d.ts +6 -0
  42. package/dist/types/index.d.ts.map +1 -1
  43. package/dist/types/protocol-utils.d.ts +70 -0
  44. package/dist/types/protocol-utils.d.ts.map +1 -0
  45. package/dist/types/sync-engine-level.d.ts +5 -42
  46. package/dist/types/sync-engine-level.d.ts.map +1 -1
  47. package/dist/types/sync-messages.d.ts +76 -0
  48. package/dist/types/sync-messages.d.ts.map +1 -0
  49. package/dist/types/sync-topological-sort.d.ts +15 -0
  50. package/dist/types/sync-topological-sort.d.ts.map +1 -0
  51. package/dist/types/test-harness.d.ts +10 -0
  52. package/dist/types/test-harness.d.ts.map +1 -1
  53. package/package.json +5 -5
  54. package/src/anonymous-dwn-api.ts +263 -0
  55. package/src/dwn-api.ts +160 -1015
  56. package/src/dwn-encryption.ts +481 -0
  57. package/src/dwn-key-delivery.ts +370 -0
  58. package/src/dwn-record-upgrade.ts +166 -0
  59. package/src/dwn-type-guards.ts +43 -0
  60. package/src/index.ts +6 -0
  61. package/src/protocol-utils.ts +185 -0
  62. package/src/store-data-protocols.ts +1 -1
  63. package/src/sync-engine-level.ts +24 -413
  64. package/src/sync-messages.ts +277 -0
  65. package/src/sync-topological-sort.ts +167 -0
  66. package/src/test-harness.ts +19 -0
@@ -0,0 +1,184 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { ProtocolsQuery, RecordsCount, RecordsQuery, RecordsRead, RecordsSubscribe } from '@enbox/dwn-sdk-js';
11
+ import { getDwnServiceEndpointUrls } from './utils.js';
12
+ /**
13
+ * A lightweight DWN API that creates **unsigned** (anonymous) DWN messages and
14
+ * sends them to remote DWNs via RPC.
15
+ *
16
+ * This class does not require a vault, agent DID, signing keys, or any identity
17
+ * infrastructure. It leverages the DWN SDK's native support for optional
18
+ * `signer` on read-path operations (RecordsQuery, RecordsRead, RecordsSubscribe,
19
+ * RecordsCount, ProtocolsQuery).
20
+ *
21
+ * Anonymous queries return only published records. Anonymous reads succeed for
22
+ * published records and for protocol records with `{ who: 'anyone', can: ['read'] }`.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const resolver = new UniversalResolver({ didResolvers: [DidDht, DidJwk] });
27
+ * const rpcClient = new Web5RpcClient();
28
+ * const anonymousDwn = new AnonymousDwnApi({ didResolver: resolver, rpcClient });
29
+ *
30
+ * const reply = await anonymousDwn.recordsQuery('did:dht:alice...', {
31
+ * filter: { protocol: 'https://social.example/posts', protocolPath: 'post' },
32
+ * });
33
+ * ```
34
+ */
35
+ export class AnonymousDwnApi {
36
+ constructor({ didResolver, rpcClient }) {
37
+ this._didResolver = didResolver;
38
+ this._rpcClient = rpcClient;
39
+ }
40
+ /**
41
+ * Send an anonymous (unsigned) `RecordsQuery` to a remote DWN.
42
+ *
43
+ * Only published records are returned by the remote DWN.
44
+ *
45
+ * @param target - The DID whose DWN will be queried.
46
+ * @param params - Query parameters (filter, sort, pagination).
47
+ * @returns The raw `RecordsQueryReply` from the remote DWN.
48
+ */
49
+ recordsQuery(target, params) {
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ const recordsQuery = yield RecordsQuery.create({
52
+ filter: params.filter,
53
+ dateSort: params.dateSort,
54
+ pagination: params.pagination,
55
+ messageTimestamp: params.messageTimestamp,
56
+ // No signer — creates an unsigned message.
57
+ });
58
+ return yield this.sendRequest(target, recordsQuery.message);
59
+ });
60
+ }
61
+ /**
62
+ * Send an anonymous (unsigned) `RecordsRead` to a remote DWN.
63
+ *
64
+ * Succeeds for published records and for protocol records with
65
+ * `{ who: 'anyone', can: ['read'] }` rules.
66
+ *
67
+ * @param target - The DID whose DWN will be read from.
68
+ * @param params - Read parameters (filter).
69
+ * @returns The raw `RecordsReadReply` from the remote DWN.
70
+ */
71
+ recordsRead(target, params) {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ const recordsRead = yield RecordsRead.create({
74
+ filter: params.filter,
75
+ messageTimestamp: params.messageTimestamp,
76
+ // No signer — creates an unsigned message.
77
+ });
78
+ return yield this.sendRequest(target, recordsRead.message);
79
+ });
80
+ }
81
+ /**
82
+ * Send an anonymous (unsigned) `RecordsSubscribe` to a remote DWN.
83
+ *
84
+ * Only published record events are received.
85
+ *
86
+ * @param target - The DID whose DWN to subscribe to.
87
+ * @param params - Subscribe parameters (filter).
88
+ * @param handler - Callback for incoming record events.
89
+ * @returns The raw `RecordsSubscribeReply` from the remote DWN.
90
+ */
91
+ recordsSubscribe(target, params, handler) {
92
+ return __awaiter(this, void 0, void 0, function* () {
93
+ const recordsSubscribe = yield RecordsSubscribe.create({
94
+ filter: params.filter,
95
+ dateSort: params.dateSort,
96
+ pagination: params.pagination,
97
+ messageTimestamp: params.messageTimestamp,
98
+ // No signer — creates an unsigned message.
99
+ });
100
+ return yield this.sendRequest(target, recordsSubscribe.message, undefined, handler);
101
+ });
102
+ }
103
+ /**
104
+ * Send an anonymous (unsigned) `RecordsCount` to a remote DWN.
105
+ *
106
+ * Only published records are counted.
107
+ *
108
+ * @param target - The DID whose DWN to count records in.
109
+ * @param params - Count parameters (filter).
110
+ * @returns The raw `RecordsCountReply` from the remote DWN.
111
+ */
112
+ recordsCount(target, params) {
113
+ return __awaiter(this, void 0, void 0, function* () {
114
+ const recordsCount = yield RecordsCount.create({
115
+ filter: params.filter,
116
+ messageTimestamp: params.messageTimestamp,
117
+ // No signer — creates an unsigned message.
118
+ });
119
+ return yield this.sendRequest(target, recordsCount.message);
120
+ });
121
+ }
122
+ /**
123
+ * Send an anonymous (unsigned) `ProtocolsQuery` to a remote DWN.
124
+ *
125
+ * Only published protocol definitions are returned.
126
+ *
127
+ * @param target - The DID whose DWN to query protocols from.
128
+ * @param params - Optional query parameters (protocol filter).
129
+ * @returns The raw `ProtocolsQueryReply` from the remote DWN.
130
+ */
131
+ protocolsQuery(target, params) {
132
+ return __awaiter(this, void 0, void 0, function* () {
133
+ const protocolsQuery = yield ProtocolsQuery.create({
134
+ filter: params === null || params === void 0 ? void 0 : params.filter,
135
+ messageTimestamp: params === null || params === void 0 ? void 0 : params.messageTimestamp,
136
+ // No signer — creates an unsigned message.
137
+ });
138
+ return yield this.sendRequest(target, protocolsQuery.message);
139
+ });
140
+ }
141
+ /**
142
+ * Resolve the target DID's DWN service endpoints and send an unsigned
143
+ * message to the first one that responds.
144
+ *
145
+ * Follows the same retry-over-endpoints pattern as `AgentDwnApi.sendDwnRpcRequest()`.
146
+ */
147
+ sendRequest(target, message, data, subscriptionHandler) {
148
+ return __awaiter(this, void 0, void 0, function* () {
149
+ const dwnEndpointUrls = yield getDwnServiceEndpointUrls(target, this._didResolver);
150
+ const errorMessages = [];
151
+ for (let dwnUrl of dwnEndpointUrls) {
152
+ try {
153
+ // For subscriptions, upgrade to WebSocket transport if available.
154
+ if (subscriptionHandler !== undefined) {
155
+ const serverInfo = yield this._rpcClient.getServerInfo(dwnUrl);
156
+ if (!serverInfo.webSocketSupport) {
157
+ errorMessages.push({ url: dwnUrl, message: 'WebSocket support is not enabled on the server.' });
158
+ continue;
159
+ }
160
+ const parsedUrl = new URL(dwnUrl);
161
+ parsedUrl.protocol = parsedUrl.protocol === 'http:' ? 'ws:' : 'wss:';
162
+ dwnUrl = parsedUrl.toString();
163
+ }
164
+ const reply = yield this._rpcClient.sendDwnRequest({
165
+ dwnUrl,
166
+ targetDid: target,
167
+ message,
168
+ data,
169
+ subscriptionHandler: subscriptionHandler,
170
+ });
171
+ return reply;
172
+ }
173
+ catch (error) {
174
+ errorMessages.push({
175
+ url: dwnUrl,
176
+ message: (error instanceof Error) ? error.message : 'Unknown error',
177
+ });
178
+ }
179
+ }
180
+ throw new Error(`AnonymousDwnApi: Failed to send request to '${target}': ${JSON.stringify(errorMessages)}`);
181
+ });
182
+ }
183
+ }
184
+ //# sourceMappingURL=anonymous-dwn-api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"anonymous-dwn-api.js","sourceRoot":"","sources":["../../src/anonymous-dwn-api.ts"],"names":[],"mappings":";;;;;;;;;AAiBA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAE9G,OAAO,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAyDvD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,OAAO,eAAe;IAI1B,YAAY,EAAE,WAAW,EAAE,SAAS,EAAyB;QAC3D,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC;QAChC,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;IAC9B,CAAC;IAED;;;;;;;;OAQG;IACU,YAAY,CAAC,MAAc,EAAE,MAAmC;;YAC3E,MAAM,YAAY,GAAG,MAAM,YAAY,CAAC,MAAM,CAAC;gBAC7C,MAAM,EAAa,MAAM,CAAC,MAAM;gBAChC,QAAQ,EAAW,MAAM,CAAC,QAAQ;gBAClC,UAAU,EAAS,MAAM,CAAC,UAAU;gBACpC,gBAAgB,EAAG,MAAM,CAAC,gBAAgB;gBAC1C,2CAA2C;aAC5C,CAAC,CAAC;YAEH,OAAO,MAAM,IAAI,CAAC,WAAW,CAAoB,MAAM,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC;QACjF,CAAC;KAAA;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