@coasys/ad4m 0.8.0-fix → 0.8.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 (161) hide show
  1. package/README.md +229 -229
  2. package/jest.config.js +5 -5
  3. package/jsdoc.json +27 -27
  4. package/lib/index.cjs +4522 -4571
  5. package/lib/index.js +4522 -4571
  6. package/lib/shims/type-graphql.d.ts +32 -32
  7. package/lib/shims/type-graphql.js +70 -70
  8. package/lib/src/Ad4mClient.d.ts +26 -26
  9. package/lib/src/Ad4mClient.js +66 -66
  10. package/lib/src/Address.d.ts +1 -1
  11. package/lib/src/Address.js +1 -1
  12. package/lib/src/DID.d.ts +1 -1
  13. package/lib/src/DID.js +1 -1
  14. package/lib/src/Exception.d.ts +6 -6
  15. package/lib/src/Exception.js +7 -7
  16. package/lib/src/Literal.d.ts +7 -7
  17. package/lib/src/Literal.js +76 -76
  18. package/lib/src/PubSub.d.ts +14 -14
  19. package/lib/src/PubSub.js +14 -14
  20. package/lib/src/SmartLiteral.d.ts +12 -12
  21. package/lib/src/SmartLiteral.js +74 -74
  22. package/lib/src/agent/Agent.d.ts +104 -104
  23. package/lib/src/agent/Agent.js +337 -337
  24. package/lib/src/agent/AgentClient.d.ts +57 -57
  25. package/lib/src/agent/AgentClient.js +431 -431
  26. package/lib/src/agent/AgentResolver.d.ts +30 -30
  27. package/lib/src/agent/AgentResolver.js +325 -325
  28. package/lib/src/agent/AgentStatus.d.ts +8 -8
  29. package/lib/src/agent/AgentStatus.js +61 -61
  30. package/lib/src/buildSchema.d.ts +1 -1
  31. package/lib/src/buildSchema.js +36 -36
  32. package/lib/src/expression/Expression.d.ts +28 -28
  33. package/lib/src/expression/Expression.js +143 -143
  34. package/lib/src/expression/ExpressionClient.d.ts +13 -13
  35. package/lib/src/expression/ExpressionClient.js +102 -102
  36. package/lib/src/expression/ExpressionRef.d.ts +9 -9
  37. package/lib/src/expression/ExpressionRef.js +67 -67
  38. package/lib/src/expression/ExpressionResolver.d.ts +10 -10
  39. package/lib/src/expression/ExpressionResolver.js +106 -106
  40. package/lib/src/index.d.ts +30 -30
  41. package/lib/src/index.js +30 -30
  42. package/lib/src/jestSetup.d.ts +1 -1
  43. package/lib/src/jestSetup.js +1 -1
  44. package/lib/src/language/Icon.d.ts +4 -4
  45. package/lib/src/language/Icon.js +24 -24
  46. package/lib/src/language/Language.d.ts +195 -195
  47. package/lib/src/language/Language.js +79 -79
  48. package/lib/src/language/LanguageClient.d.ts +17 -17
  49. package/lib/src/language/LanguageClient.js +137 -137
  50. package/lib/src/language/LanguageContext.d.ts +37 -37
  51. package/lib/src/language/LanguageContext.js +2 -2
  52. package/lib/src/language/LanguageHandle.d.ts +9 -9
  53. package/lib/src/language/LanguageHandle.js +41 -41
  54. package/lib/src/language/LanguageMeta.d.ts +35 -35
  55. package/lib/src/language/LanguageMeta.js +89 -89
  56. package/lib/src/language/LanguageRef.d.ts +6 -6
  57. package/lib/src/language/LanguageRef.js +30 -30
  58. package/lib/src/language/LanguageResolver.d.ts +19 -19
  59. package/lib/src/language/LanguageResolver.js +143 -143
  60. package/lib/src/links/Links.d.ts +39 -39
  61. package/lib/src/links/Links.js +148 -148
  62. package/lib/src/links/Links.test.d.ts +1 -1
  63. package/lib/src/links/Links.test.js +33 -33
  64. package/lib/src/neighbourhood/Neighbourhood.d.ts +10 -10
  65. package/lib/src/neighbourhood/Neighbourhood.js +38 -38
  66. package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +25 -25
  67. package/lib/src/neighbourhood/NeighbourhoodClient.js +256 -256
  68. package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +19 -19
  69. package/lib/src/neighbourhood/NeighbourhoodProxy.js +54 -54
  70. package/lib/src/neighbourhood/NeighbourhoodResolver.d.ts +24 -24
  71. package/lib/src/neighbourhood/NeighbourhoodResolver.js +180 -180
  72. package/lib/src/perspectives/LinkQuery.d.ts +11 -11
  73. package/lib/src/perspectives/LinkQuery.js +80 -80
  74. package/lib/src/perspectives/Perspective.d.ts +38 -38
  75. package/lib/src/perspectives/Perspective.js +144 -144
  76. package/lib/src/perspectives/Perspective.test.d.ts +1 -1
  77. package/lib/src/perspectives/Perspective.test.js +20 -20
  78. package/lib/src/perspectives/PerspectiveClient.d.ts +55 -54
  79. package/lib/src/perspectives/PerspectiveClient.js +418 -410
  80. package/lib/src/perspectives/PerspectiveDiff.d.ts +9 -9
  81. package/lib/src/perspectives/PerspectiveDiff.js +33 -33
  82. package/lib/src/perspectives/PerspectiveHandle.d.ts +16 -16
  83. package/lib/src/perspectives/PerspectiveHandle.js +59 -59
  84. package/lib/src/perspectives/PerspectiveProxy.d.ts +178 -178
  85. package/lib/src/perspectives/PerspectiveProxy.js +501 -540
  86. package/lib/src/perspectives/PerspectiveResolver.d.ts +38 -37
  87. package/lib/src/perspectives/PerspectiveResolver.js +376 -362
  88. package/lib/src/runtime/RuntimeClient.d.ts +33 -33
  89. package/lib/src/runtime/RuntimeClient.js +263 -263
  90. package/lib/src/runtime/RuntimeResolver.d.ts +47 -47
  91. package/lib/src/runtime/RuntimeResolver.js +328 -328
  92. package/lib/src/schema.gql +1 -0
  93. package/lib/src/subject/SDNADecorators.d.ts +45 -45
  94. package/lib/src/subject/SDNADecorators.js +250 -250
  95. package/lib/src/subject/Subject.d.ts +7 -7
  96. package/lib/src/subject/Subject.js +160 -160
  97. package/lib/src/subject/SubjectEntity.d.ts +31 -31
  98. package/lib/src/subject/SubjectEntity.js +250 -250
  99. package/lib/src/subject/util.d.ts +12 -12
  100. package/lib/src/subject/util.js +73 -73
  101. package/lib/src/typeDefs.d.ts +1 -1
  102. package/lib/src/typeDefs.js +1 -0
  103. package/lib/src/unwrapApolloResult.d.ts +2 -2
  104. package/lib/src/unwrapApolloResult.js +14 -12
  105. package/lib/src/utils.d.ts +2 -2
  106. package/lib/src/utils.js +21 -21
  107. package/package.json +61 -61
  108. package/rollup.config.js +26 -26
  109. package/shims/type-graphql.ts +75 -75
  110. package/src/Ad4mClient.test.ts +1089 -1084
  111. package/src/Ad4mClient.ts +62 -62
  112. package/src/Address.ts +1 -1
  113. package/src/Exception.ts +6 -6
  114. package/src/Literal.test.ts +30 -30
  115. package/src/Literal.ts +80 -80
  116. package/src/PubSub.ts +13 -13
  117. package/src/SmartLiteral.ts +74 -74
  118. package/src/agent/Agent.ts +293 -293
  119. package/src/agent/AgentClient.ts +531 -531
  120. package/src/agent/AgentResolver.ts +263 -263
  121. package/src/agent/AgentStatus.ts +43 -43
  122. package/src/buildSchema.ts +41 -41
  123. package/src/expression/Expression.ts +99 -99
  124. package/src/expression/ExpressionClient.ts +98 -98
  125. package/src/expression/ExpressionRef.ts +62 -62
  126. package/src/expression/ExpressionResolver.ts +63 -63
  127. package/src/index.ts +30 -30
  128. package/src/language/Icon.ts +10 -10
  129. package/src/language/Language.ts +266 -266
  130. package/src/language/LanguageClient.ts +159 -159
  131. package/src/language/LanguageContext.ts +37 -37
  132. package/src/language/LanguageHandle.ts +23 -23
  133. package/src/language/LanguageMeta.ts +69 -69
  134. package/src/language/LanguageRef.ts +18 -18
  135. package/src/language/LanguageResolver.ts +98 -98
  136. package/src/links/Links.test.ts +39 -39
  137. package/src/links/Links.ts +107 -107
  138. package/src/neighbourhood/Neighbourhood.ts +22 -22
  139. package/src/neighbourhood/NeighbourhoodClient.ts +275 -275
  140. package/src/neighbourhood/NeighbourhoodProxy.ts +58 -58
  141. package/src/neighbourhood/NeighbourhoodResolver.ts +99 -99
  142. package/src/perspectives/LinkQuery.ts +65 -65
  143. package/src/perspectives/Perspective.test.ts +22 -22
  144. package/src/perspectives/Perspective.ts +118 -118
  145. package/src/perspectives/PerspectiveClient.ts +452 -443
  146. package/src/perspectives/PerspectiveDiff.ts +15 -15
  147. package/src/perspectives/PerspectiveHandle.ts +39 -39
  148. package/src/perspectives/PerspectiveProxy.ts +576 -610
  149. package/src/perspectives/PerspectiveResolver.ts +224 -219
  150. package/src/runtime/RuntimeClient.ts +282 -282
  151. package/src/runtime/RuntimeResolver.ts +192 -192
  152. package/src/subject/SDNADecorators.ts +333 -333
  153. package/src/subject/Subject.ts +152 -152
  154. package/src/subject/SubjectEntity.ts +291 -291
  155. package/src/subject/util.ts +84 -84
  156. package/src/typeDefs.ts +5 -5
  157. package/src/unwrapApolloResult.ts +15 -13
  158. package/src/utils.ts +25 -25
  159. package/tsconfig-browser.json +82 -82
  160. package/tsconfig.json +80 -80
  161. package/yarn-error.log +0 -22814
@@ -1,531 +1,531 @@
1
- import { ApolloClient, gql } from "@apollo/client/core";
2
- import { PerspectiveInput } from "../perspectives/Perspective";
3
- import unwrapApolloResult from "../unwrapApolloResult";
4
- import {
5
- Agent,
6
- Apps,
7
- AuthInfo,
8
- AuthInfoInput,
9
- EntanglementProof,
10
- EntanglementProofInput,
11
- } from "./Agent";
12
- import { AgentStatus } from "./AgentStatus";
13
- import { LinkMutations } from "../links/Links";
14
- import { PerspectiveClient } from "../perspectives/PerspectiveClient";
15
-
16
- const AGENT_SUBITEMS = `
17
- did
18
- directMessageLanguage
19
- perspective {
20
- links {
21
- author, timestamp,
22
- proof {
23
- signature, key, valid, invalid
24
- }
25
- data {
26
- source, predicate, target
27
- }
28
- }
29
- }
30
- `;
31
-
32
- const Apps_FIELDS = `
33
- requestId
34
- revoked
35
- auth {
36
- appName
37
- appDesc
38
- appUrl
39
- appIconPath
40
- capabilities {
41
- with {
42
- domain
43
- pointers
44
- }
45
- can
46
- }
47
- }
48
- `;
49
-
50
- const AGENT_STATUS_FIELDS = `
51
- isInitialized
52
- isUnlocked
53
- did
54
- didDocument
55
- error
56
- `;
57
-
58
- const ENTANGLEMENT_PROOF_FIELDS = `
59
- did
60
- didSigningKeyId
61
- deviceKeyType
62
- deviceKey
63
- deviceKeySignedByDid
64
- didSignedByDeviceKey
65
- `;
66
-
67
- const AGENT_SIGNATURE_FIELDS = `
68
- signature
69
- publicKey
70
- `;
71
-
72
- export interface InitializeArgs {
73
- did: string;
74
- didDocument: string;
75
- keystore: string;
76
- passphrase: string;
77
- }
78
-
79
- export type AgentUpdatedCallback = (agent: Agent) => null;
80
- export type AgentStatusChangedCallback = (agent: Agent) => null;
81
- export type AgentAppsUpdatedCallback = () => null;
82
- /**
83
- * Provides access to all functions regarding the local agent,
84
- * such as generating, locking, unlocking, importing the DID keystore,
85
- * as well as updating the publicly shared Agent expression.
86
- */
87
- export class AgentClient {
88
- #apolloClient: ApolloClient<any>;
89
- #appsChangedCallback: AgentAppsUpdatedCallback[];
90
- #updatedCallbacks: AgentUpdatedCallback[];
91
- #agentStatusChangedCallbacks: AgentStatusChangedCallback[];
92
-
93
- constructor(client: ApolloClient<any>, subscribe: boolean = true) {
94
- this.#apolloClient = client;
95
- this.#updatedCallbacks = [];
96
- this.#agentStatusChangedCallbacks = [];
97
- this.#appsChangedCallback = [];
98
-
99
- if (subscribe) {
100
- this.subscribeAgentUpdated();
101
- this.subscribeAgentStatusChanged();
102
- this.subscribeAppsChanged();
103
- }
104
- }
105
-
106
- /**
107
- * Returns the Agent expression of the local agent as it is shared
108
- * publicly via the AgentLanguage.
109
- *
110
- * I.e. this is the users profile.
111
- */
112
- async me(): Promise<Agent> {
113
- const { agent } = unwrapApolloResult(
114
- await this.#apolloClient.query({
115
- query: gql`query agent { agent { ${AGENT_SUBITEMS} } }`,
116
- })
117
- );
118
- let agentObject = new Agent(agent.did, agent.perspective);
119
- agentObject.directMessageLanguage = agent.directMessageLanguage;
120
- return agentObject;
121
- }
122
-
123
- async status(): Promise<AgentStatus> {
124
- const { agentStatus } = unwrapApolloResult(
125
- await this.#apolloClient.query({
126
- query: gql`query agentStatus {
127
- agentStatus {
128
- ${AGENT_STATUS_FIELDS}
129
- }
130
- }`,
131
- })
132
- );
133
- return new AgentStatus(agentStatus);
134
- }
135
-
136
- async generate(passphrase: string): Promise<AgentStatus> {
137
- const { agentGenerate } = unwrapApolloResult(
138
- await this.#apolloClient.mutate({
139
- mutation: gql`mutation agentGenerate(
140
- $passphrase: String!
141
- ) {
142
- agentGenerate(passphrase: $passphrase) {
143
- ${AGENT_STATUS_FIELDS}
144
- }
145
- }`,
146
- variables: { passphrase },
147
- })
148
- );
149
- return new AgentStatus(agentGenerate);
150
- }
151
-
152
- async import(args: InitializeArgs): Promise<AgentStatus> {
153
- let { did, didDocument, keystore, passphrase } = args;
154
- const { agentImport } = unwrapApolloResult(
155
- await this.#apolloClient.mutate({
156
- mutation: gql`mutation agentImport(
157
- $did: String!,
158
- $didDocument: String!,
159
- $keystore: String!,
160
- $passphrase: String!
161
- ) {
162
- agentImport(did: $did, didDocument: $didDocument, keystore: $keystore, passphrase: $passphrase) {
163
- ${AGENT_STATUS_FIELDS}
164
- }
165
- }`,
166
- variables: { did, didDocument, keystore, passphrase },
167
- })
168
- );
169
- return new AgentStatus(agentImport);
170
- }
171
-
172
- async lock(passphrase: string): Promise<AgentStatus> {
173
- const { agentLock } = unwrapApolloResult(
174
- await this.#apolloClient.mutate({
175
- mutation: gql`mutation agentLock($passphrase: String!) {
176
- agentLock(passphrase: $passphrase) {
177
- ${AGENT_STATUS_FIELDS}
178
- }
179
- }`,
180
- variables: { passphrase },
181
- })
182
- );
183
- return new AgentStatus(agentLock);
184
- }
185
-
186
- async unlock(passphrase: string, holochain = true): Promise<AgentStatus> {
187
- const { agentUnlock } = unwrapApolloResult(
188
- await this.#apolloClient.mutate({
189
- mutation: gql`mutation agentUnlock($passphrase: String!, $holochain: Boolean!) {
190
- agentUnlock(passphrase: $passphrase, holochain: $holochain) {
191
- ${AGENT_STATUS_FIELDS}
192
- }
193
- }`,
194
- variables: { passphrase, holochain },
195
- })
196
- );
197
- return new AgentStatus(agentUnlock);
198
- }
199
-
200
- async byDID(did: string): Promise<Agent> {
201
- const { agentByDID } = unwrapApolloResult(
202
- await this.#apolloClient.query({
203
- query: gql`query agentByDID($did: String!) {
204
- agentByDID(did: $did) {
205
- ${AGENT_SUBITEMS}
206
- }
207
- }`,
208
- variables: { did },
209
- })
210
- );
211
- return agentByDID as Agent;
212
- }
213
-
214
- async updatePublicPerspective(perspective: PerspectiveInput): Promise<Agent> {
215
- const cleanedPerspective = JSON.parse(JSON.stringify(perspective));
216
- delete cleanedPerspective.__typename;
217
- cleanedPerspective.links.forEach((link) => {
218
- delete link.__typename;
219
- delete link.data.__typename;
220
- delete link.proof.__typename;
221
- delete link.status;
222
- });
223
-
224
- const { agentUpdatePublicPerspective } = unwrapApolloResult(
225
- await this.#apolloClient.mutate({
226
- mutation: gql`mutation agentUpdatePublicPerspective($perspective: PerspectiveInput!) {
227
- agentUpdatePublicPerspective(perspective: $perspective) {
228
- ${AGENT_SUBITEMS}
229
- }
230
- }`,
231
- variables: { perspective: cleanedPerspective },
232
- })
233
- );
234
- const a = agentUpdatePublicPerspective;
235
- const agent = new Agent(a.did, a.perspective);
236
- agent.directMessageLanguage = a.directMessageLanguage;
237
- return agent;
238
- }
239
-
240
- async mutatePublicPerspective(mutations: LinkMutations): Promise<Agent> {
241
- const perspectiveClient = new PerspectiveClient(this.#apolloClient);
242
- const agentClient = new AgentClient(this.#apolloClient);
243
-
244
- //Create the proxy perspective and load existing links
245
- const proxyPerspective = await perspectiveClient.add(
246
- "Agent Perspective Proxy"
247
- );
248
- const agentMe = await agentClient.me();
249
-
250
- if (agentMe.perspective) {
251
- await proxyPerspective.loadSnapshot(agentMe.perspective);
252
- }
253
-
254
- //Make the mutations on the proxy perspective
255
- for (const addition of mutations.additions) {
256
- await proxyPerspective.add(addition);
257
- }
258
- for (const removal of mutations.removals) {
259
- await proxyPerspective.remove(removal);
260
- }
261
-
262
- //Get the snapshot of the proxy perspective
263
- const snapshot = await proxyPerspective.snapshot();
264
- //Update the users public perspective
265
- const agent = await this.updatePublicPerspective(snapshot);
266
- //Cleanup and return
267
- await perspectiveClient.remove(proxyPerspective.uuid);
268
- return agent;
269
- }
270
-
271
- async updateDirectMessageLanguage(
272
- directMessageLanguage: string
273
- ): Promise<Agent> {
274
- const { agentUpdateDirectMessageLanguage } = unwrapApolloResult(
275
- await this.#apolloClient.mutate({
276
- mutation: gql`mutation agentUpdateDirectMessageLanguage($directMessageLanguage: String!) {
277
- agentUpdateDirectMessageLanguage(directMessageLanguage: $directMessageLanguage) {
278
- ${AGENT_SUBITEMS}
279
- }
280
- }`,
281
- variables: { directMessageLanguage },
282
- })
283
- );
284
- const a = agentUpdateDirectMessageLanguage;
285
- const agent = new Agent(a.did, a.perspective);
286
- agent.directMessageLanguage = a.directMessageLanguage;
287
- return agent;
288
- }
289
-
290
- async addEntanglementProofs(
291
- proofs: EntanglementProofInput[]
292
- ): Promise<EntanglementProof[]> {
293
- const { agentAddEntanglementProofs } = unwrapApolloResult(
294
- await this.#apolloClient.mutate({
295
- mutation: gql`mutation agentAddEntanglementProofs($proofs: [EntanglementProofInput!]!) {
296
- agentAddEntanglementProofs(proofs: $proofs) {
297
- ${ENTANGLEMENT_PROOF_FIELDS}
298
- }
299
- }`,
300
- variables: { proofs },
301
- })
302
- );
303
- return agentAddEntanglementProofs;
304
- }
305
-
306
- async deleteEntanglementProofs(
307
- proofs: EntanglementProofInput[]
308
- ): Promise<EntanglementProof[]> {
309
- const { agentDeleteEntanglementProofs } = unwrapApolloResult(
310
- await this.#apolloClient.mutate({
311
- mutation: gql`mutation agentDeleteEntanglementProofs($proofs: [EntanglementProofInput!]!) {
312
- agentDeleteEntanglementProofs(proofs: $proofs) {
313
- ${ENTANGLEMENT_PROOF_FIELDS}
314
- }
315
- }`,
316
- variables: { proofs },
317
- })
318
- );
319
- return agentDeleteEntanglementProofs;
320
- }
321
-
322
- async getEntanglementProofs(): Promise<string[]> {
323
- const { agentGetEntanglementProofs } = unwrapApolloResult(
324
- await this.#apolloClient.query({
325
- query: gql`query agentGetEntanglementProofs {
326
- agentGetEntanglementProofs {
327
- ${ENTANGLEMENT_PROOF_FIELDS}
328
- }
329
- }`,
330
- })
331
- );
332
- return agentGetEntanglementProofs;
333
- }
334
-
335
- async entanglementProofPreFlight(
336
- deviceKey: string,
337
- deviceKeyType: string
338
- ): Promise<EntanglementProof> {
339
- const { agentEntanglementProofPreFlight } = unwrapApolloResult(
340
- await this.#apolloClient.mutate({
341
- mutation: gql`mutation agentEntanglementProofPreFlight($deviceKey: String!, $deviceKeyType: String!) {
342
- agentEntanglementProofPreFlight(deviceKey: $deviceKey, deviceKeyType: $deviceKeyType) {
343
- ${ENTANGLEMENT_PROOF_FIELDS}
344
- }
345
- }`,
346
- variables: { deviceKey, deviceKeyType },
347
- })
348
- );
349
- return agentEntanglementProofPreFlight;
350
- }
351
-
352
- addUpdatedListener(listener) {
353
- this.#updatedCallbacks.push(listener);
354
- }
355
-
356
- addAppChangedListener(listener) {
357
- this.#appsChangedCallback.push(listener);
358
- }
359
-
360
- subscribeAgentUpdated() {
361
- this.#apolloClient
362
- .subscribe({
363
- query: gql` subscription {
364
- agentUpdated { ${AGENT_SUBITEMS} }
365
- }
366
- `,
367
- })
368
- .subscribe({
369
- next: (result) => {
370
- const agent = result.data.agentUpdated;
371
- this.#updatedCallbacks.forEach((cb) => {
372
- cb(agent);
373
- });
374
- },
375
- error: (e) => console.error(e),
376
- });
377
- }
378
-
379
- subscribeAppsChanged() {
380
- this.#apolloClient
381
- .subscribe({
382
- query: gql` subscription {
383
- agentAppsChanged {
384
- ${Apps_FIELDS}
385
- }
386
- }
387
- `,
388
- })
389
- .subscribe({
390
- next: (result) => {
391
- this.#appsChangedCallback.forEach((cb) => {
392
- cb();
393
- });
394
- },
395
- error: (e) => console.error(e),
396
- });
397
- }
398
-
399
- addAgentStatusChangedListener(listener) {
400
- this.#agentStatusChangedCallbacks.push(listener);
401
- }
402
-
403
- subscribeAgentStatusChanged() {
404
- this.#apolloClient
405
- .subscribe({
406
- query: gql` subscription {
407
- agentStatusChanged { ${AGENT_STATUS_FIELDS} }
408
- }
409
- `,
410
- })
411
- .subscribe({
412
- next: (result) => {
413
- const agent = result.data.agentStatusChanged;
414
- this.#agentStatusChangedCallbacks.forEach((cb) => {
415
- cb(agent);
416
- });
417
- },
418
- error: (e) => console.error(e),
419
- });
420
- }
421
-
422
- async requestCapability(authInfo: AuthInfoInput): Promise<string> {
423
- const { agentRequestCapability } = unwrapApolloResult(
424
- await this.#apolloClient.mutate({
425
- mutation: gql`
426
- mutation agentRequestCapability($authInfo: AuthInfoInput!) {
427
- agentRequestCapability(authInfo: $authInfo)
428
- }
429
- `,
430
- variables: { authInfo },
431
- })
432
- );
433
- return agentRequestCapability;
434
- }
435
-
436
- async permitCapability(auth: string): Promise<string> {
437
- const { agentPermitCapability } = unwrapApolloResult(
438
- await this.#apolloClient.mutate({
439
- mutation: gql`
440
- mutation agentPermitCapability($auth: String!) {
441
- agentPermitCapability(auth: $auth)
442
- }
443
- `,
444
- variables: { auth },
445
- })
446
- );
447
- return agentPermitCapability;
448
- }
449
-
450
- async generateJwt(requestId: string, rand: string): Promise<string> {
451
- const { agentGenerateJwt } = unwrapApolloResult(
452
- await this.#apolloClient.mutate({
453
- mutation: gql`
454
- mutation agentGenerateJwt($requestId: String!, $rand: String!) {
455
- agentGenerateJwt(requestId: $requestId, rand: $rand)
456
- }
457
- `,
458
- variables: { requestId, rand },
459
- })
460
- );
461
- return agentGenerateJwt;
462
- }
463
-
464
- async getApps(): Promise<Apps[]> {
465
- const { agentGetApps } = unwrapApolloResult(
466
- await this.#apolloClient.mutate({
467
- mutation: gql`query agentGetApps {
468
- agentGetApps {
469
- ${Apps_FIELDS}
470
- }
471
- }`,
472
- })
473
- );
474
- return agentGetApps;
475
- }
476
-
477
- async removeApp(requestId: string): Promise<Apps[]> {
478
- const { agentRemoveApp } = unwrapApolloResult(
479
- await this.#apolloClient.mutate({
480
- mutation: gql`mutation agentRemoveApp($requestId: String!) {
481
- agentRemoveApp(requestId: $requestId) {
482
- ${Apps_FIELDS}
483
- }
484
- }`,
485
- variables: { requestId },
486
- })
487
- );
488
- return agentRemoveApp;
489
- }
490
-
491
- async revokeToken(requestId: string): Promise<Apps[]> {
492
- const { agentRevokeToken } = unwrapApolloResult(
493
- await this.#apolloClient.mutate({
494
- mutation: gql`mutation agentRevokeToken($requestId: String!) {
495
- agentRevokeToken(requestId: $requestId) {
496
- ${Apps_FIELDS}
497
- }
498
- }`,
499
- variables: { requestId },
500
- })
501
- );
502
- return agentRevokeToken;
503
- }
504
-
505
- async isLocked(): Promise<boolean> {
506
- const { agentIsLocked } = unwrapApolloResult(
507
- await this.#apolloClient.mutate({
508
- mutation: gql`
509
- query agentIsLocked {
510
- agentIsLocked
511
- }
512
- `,
513
- })
514
- );
515
- return agentIsLocked;
516
- }
517
-
518
- async signMessage(message: string): Promise<string> {
519
- const { agentSignMessage } = unwrapApolloResult(
520
- await this.#apolloClient.mutate({
521
- mutation: gql`mutation agentSignMessage($message: String!) {
522
- agentSignMessage(message: $message) {
523
- ${AGENT_SIGNATURE_FIELDS}
524
- }
525
- }`,
526
- variables: { message },
527
- })
528
- );
529
- return agentSignMessage;
530
- }
531
- }
1
+ import { ApolloClient, gql } from "@apollo/client/core";
2
+ import { PerspectiveInput } from "../perspectives/Perspective";
3
+ import unwrapApolloResult from "../unwrapApolloResult";
4
+ import {
5
+ Agent,
6
+ Apps,
7
+ AuthInfo,
8
+ AuthInfoInput,
9
+ EntanglementProof,
10
+ EntanglementProofInput,
11
+ } from "./Agent";
12
+ import { AgentStatus } from "./AgentStatus";
13
+ import { LinkMutations } from "../links/Links";
14
+ import { PerspectiveClient } from "../perspectives/PerspectiveClient";
15
+
16
+ const AGENT_SUBITEMS = `
17
+ did
18
+ directMessageLanguage
19
+ perspective {
20
+ links {
21
+ author, timestamp,
22
+ proof {
23
+ signature, key, valid, invalid
24
+ }
25
+ data {
26
+ source, predicate, target
27
+ }
28
+ }
29
+ }
30
+ `;
31
+
32
+ const Apps_FIELDS = `
33
+ requestId
34
+ revoked
35
+ auth {
36
+ appName
37
+ appDesc
38
+ appUrl
39
+ appIconPath
40
+ capabilities {
41
+ with {
42
+ domain
43
+ pointers
44
+ }
45
+ can
46
+ }
47
+ }
48
+ `;
49
+
50
+ const AGENT_STATUS_FIELDS = `
51
+ isInitialized
52
+ isUnlocked
53
+ did
54
+ didDocument
55
+ error
56
+ `;
57
+
58
+ const ENTANGLEMENT_PROOF_FIELDS = `
59
+ did
60
+ didSigningKeyId
61
+ deviceKeyType
62
+ deviceKey
63
+ deviceKeySignedByDid
64
+ didSignedByDeviceKey
65
+ `;
66
+
67
+ const AGENT_SIGNATURE_FIELDS = `
68
+ signature
69
+ publicKey
70
+ `;
71
+
72
+ export interface InitializeArgs {
73
+ did: string;
74
+ didDocument: string;
75
+ keystore: string;
76
+ passphrase: string;
77
+ }
78
+
79
+ export type AgentUpdatedCallback = (agent: Agent) => null;
80
+ export type AgentStatusChangedCallback = (agent: Agent) => null;
81
+ export type AgentAppsUpdatedCallback = () => null;
82
+ /**
83
+ * Provides access to all functions regarding the local agent,
84
+ * such as generating, locking, unlocking, importing the DID keystore,
85
+ * as well as updating the publicly shared Agent expression.
86
+ */
87
+ export class AgentClient {
88
+ #apolloClient: ApolloClient<any>;
89
+ #appsChangedCallback: AgentAppsUpdatedCallback[];
90
+ #updatedCallbacks: AgentUpdatedCallback[];
91
+ #agentStatusChangedCallbacks: AgentStatusChangedCallback[];
92
+
93
+ constructor(client: ApolloClient<any>, subscribe: boolean = true) {
94
+ this.#apolloClient = client;
95
+ this.#updatedCallbacks = [];
96
+ this.#agentStatusChangedCallbacks = [];
97
+ this.#appsChangedCallback = [];
98
+
99
+ if (subscribe) {
100
+ this.subscribeAgentUpdated();
101
+ this.subscribeAgentStatusChanged();
102
+ this.subscribeAppsChanged();
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Returns the Agent expression of the local agent as it is shared
108
+ * publicly via the AgentLanguage.
109
+ *
110
+ * I.e. this is the users profile.
111
+ */
112
+ async me(): Promise<Agent> {
113
+ const { agent } = unwrapApolloResult(
114
+ await this.#apolloClient.query({
115
+ query: gql`query agent { agent { ${AGENT_SUBITEMS} } }`,
116
+ })
117
+ );
118
+ let agentObject = new Agent(agent.did, agent.perspective);
119
+ agentObject.directMessageLanguage = agent.directMessageLanguage;
120
+ return agentObject;
121
+ }
122
+
123
+ async status(): Promise<AgentStatus> {
124
+ const { agentStatus } = unwrapApolloResult(
125
+ await this.#apolloClient.query({
126
+ query: gql`query agentStatus {
127
+ agentStatus {
128
+ ${AGENT_STATUS_FIELDS}
129
+ }
130
+ }`,
131
+ })
132
+ );
133
+ return new AgentStatus(agentStatus);
134
+ }
135
+
136
+ async generate(passphrase: string): Promise<AgentStatus> {
137
+ const { agentGenerate } = unwrapApolloResult(
138
+ await this.#apolloClient.mutate({
139
+ mutation: gql`mutation agentGenerate(
140
+ $passphrase: String!
141
+ ) {
142
+ agentGenerate(passphrase: $passphrase) {
143
+ ${AGENT_STATUS_FIELDS}
144
+ }
145
+ }`,
146
+ variables: { passphrase },
147
+ })
148
+ );
149
+ return new AgentStatus(agentGenerate);
150
+ }
151
+
152
+ async import(args: InitializeArgs): Promise<AgentStatus> {
153
+ let { did, didDocument, keystore, passphrase } = args;
154
+ const { agentImport } = unwrapApolloResult(
155
+ await this.#apolloClient.mutate({
156
+ mutation: gql`mutation agentImport(
157
+ $did: String!,
158
+ $didDocument: String!,
159
+ $keystore: String!,
160
+ $passphrase: String!
161
+ ) {
162
+ agentImport(did: $did, didDocument: $didDocument, keystore: $keystore, passphrase: $passphrase) {
163
+ ${AGENT_STATUS_FIELDS}
164
+ }
165
+ }`,
166
+ variables: { did, didDocument, keystore, passphrase },
167
+ })
168
+ );
169
+ return new AgentStatus(agentImport);
170
+ }
171
+
172
+ async lock(passphrase: string): Promise<AgentStatus> {
173
+ const { agentLock } = unwrapApolloResult(
174
+ await this.#apolloClient.mutate({
175
+ mutation: gql`mutation agentLock($passphrase: String!) {
176
+ agentLock(passphrase: $passphrase) {
177
+ ${AGENT_STATUS_FIELDS}
178
+ }
179
+ }`,
180
+ variables: { passphrase },
181
+ })
182
+ );
183
+ return new AgentStatus(agentLock);
184
+ }
185
+
186
+ async unlock(passphrase: string, holochain = true): Promise<AgentStatus> {
187
+ const { agentUnlock } = unwrapApolloResult(
188
+ await this.#apolloClient.mutate({
189
+ mutation: gql`mutation agentUnlock($passphrase: String!, $holochain: Boolean!) {
190
+ agentUnlock(passphrase: $passphrase, holochain: $holochain) {
191
+ ${AGENT_STATUS_FIELDS}
192
+ }
193
+ }`,
194
+ variables: { passphrase, holochain },
195
+ })
196
+ );
197
+ return new AgentStatus(agentUnlock);
198
+ }
199
+
200
+ async byDID(did: string): Promise<Agent> {
201
+ const { agentByDID } = unwrapApolloResult(
202
+ await this.#apolloClient.query({
203
+ query: gql`query agentByDID($did: String!) {
204
+ agentByDID(did: $did) {
205
+ ${AGENT_SUBITEMS}
206
+ }
207
+ }`,
208
+ variables: { did },
209
+ })
210
+ );
211
+ return agentByDID as Agent;
212
+ }
213
+
214
+ async updatePublicPerspective(perspective: PerspectiveInput): Promise<Agent> {
215
+ const cleanedPerspective = JSON.parse(JSON.stringify(perspective));
216
+ delete cleanedPerspective.__typename;
217
+ cleanedPerspective.links.forEach((link) => {
218
+ delete link.__typename;
219
+ delete link.data.__typename;
220
+ delete link.proof.__typename;
221
+ delete link.status;
222
+ });
223
+
224
+ const { agentUpdatePublicPerspective } = unwrapApolloResult(
225
+ await this.#apolloClient.mutate({
226
+ mutation: gql`mutation agentUpdatePublicPerspective($perspective: PerspectiveInput!) {
227
+ agentUpdatePublicPerspective(perspective: $perspective) {
228
+ ${AGENT_SUBITEMS}
229
+ }
230
+ }`,
231
+ variables: { perspective: cleanedPerspective },
232
+ })
233
+ );
234
+ const a = agentUpdatePublicPerspective;
235
+ const agent = new Agent(a.did, a.perspective);
236
+ agent.directMessageLanguage = a.directMessageLanguage;
237
+ return agent;
238
+ }
239
+
240
+ async mutatePublicPerspective(mutations: LinkMutations): Promise<Agent> {
241
+ const perspectiveClient = new PerspectiveClient(this.#apolloClient);
242
+ const agentClient = new AgentClient(this.#apolloClient);
243
+
244
+ //Create the proxy perspective and load existing links
245
+ const proxyPerspective = await perspectiveClient.add(
246
+ "Agent Perspective Proxy"
247
+ );
248
+ const agentMe = await agentClient.me();
249
+
250
+ if (agentMe.perspective) {
251
+ await proxyPerspective.loadSnapshot(agentMe.perspective);
252
+ }
253
+
254
+ //Make the mutations on the proxy perspective
255
+ for (const addition of mutations.additions) {
256
+ await proxyPerspective.add(addition);
257
+ }
258
+ for (const removal of mutations.removals) {
259
+ await proxyPerspective.remove(removal);
260
+ }
261
+
262
+ //Get the snapshot of the proxy perspective
263
+ const snapshot = await proxyPerspective.snapshot();
264
+ //Update the users public perspective
265
+ const agent = await this.updatePublicPerspective(snapshot);
266
+ //Cleanup and return
267
+ await perspectiveClient.remove(proxyPerspective.uuid);
268
+ return agent;
269
+ }
270
+
271
+ async updateDirectMessageLanguage(
272
+ directMessageLanguage: string
273
+ ): Promise<Agent> {
274
+ const { agentUpdateDirectMessageLanguage } = unwrapApolloResult(
275
+ await this.#apolloClient.mutate({
276
+ mutation: gql`mutation agentUpdateDirectMessageLanguage($directMessageLanguage: String!) {
277
+ agentUpdateDirectMessageLanguage(directMessageLanguage: $directMessageLanguage) {
278
+ ${AGENT_SUBITEMS}
279
+ }
280
+ }`,
281
+ variables: { directMessageLanguage },
282
+ })
283
+ );
284
+ const a = agentUpdateDirectMessageLanguage;
285
+ const agent = new Agent(a.did, a.perspective);
286
+ agent.directMessageLanguage = a.directMessageLanguage;
287
+ return agent;
288
+ }
289
+
290
+ async addEntanglementProofs(
291
+ proofs: EntanglementProofInput[]
292
+ ): Promise<EntanglementProof[]> {
293
+ const { agentAddEntanglementProofs } = unwrapApolloResult(
294
+ await this.#apolloClient.mutate({
295
+ mutation: gql`mutation agentAddEntanglementProofs($proofs: [EntanglementProofInput!]!) {
296
+ agentAddEntanglementProofs(proofs: $proofs) {
297
+ ${ENTANGLEMENT_PROOF_FIELDS}
298
+ }
299
+ }`,
300
+ variables: { proofs },
301
+ })
302
+ );
303
+ return agentAddEntanglementProofs;
304
+ }
305
+
306
+ async deleteEntanglementProofs(
307
+ proofs: EntanglementProofInput[]
308
+ ): Promise<EntanglementProof[]> {
309
+ const { agentDeleteEntanglementProofs } = unwrapApolloResult(
310
+ await this.#apolloClient.mutate({
311
+ mutation: gql`mutation agentDeleteEntanglementProofs($proofs: [EntanglementProofInput!]!) {
312
+ agentDeleteEntanglementProofs(proofs: $proofs) {
313
+ ${ENTANGLEMENT_PROOF_FIELDS}
314
+ }
315
+ }`,
316
+ variables: { proofs },
317
+ })
318
+ );
319
+ return agentDeleteEntanglementProofs;
320
+ }
321
+
322
+ async getEntanglementProofs(): Promise<string[]> {
323
+ const { agentGetEntanglementProofs } = unwrapApolloResult(
324
+ await this.#apolloClient.query({
325
+ query: gql`query agentGetEntanglementProofs {
326
+ agentGetEntanglementProofs {
327
+ ${ENTANGLEMENT_PROOF_FIELDS}
328
+ }
329
+ }`,
330
+ })
331
+ );
332
+ return agentGetEntanglementProofs;
333
+ }
334
+
335
+ async entanglementProofPreFlight(
336
+ deviceKey: string,
337
+ deviceKeyType: string
338
+ ): Promise<EntanglementProof> {
339
+ const { agentEntanglementProofPreFlight } = unwrapApolloResult(
340
+ await this.#apolloClient.mutate({
341
+ mutation: gql`mutation agentEntanglementProofPreFlight($deviceKey: String!, $deviceKeyType: String!) {
342
+ agentEntanglementProofPreFlight(deviceKey: $deviceKey, deviceKeyType: $deviceKeyType) {
343
+ ${ENTANGLEMENT_PROOF_FIELDS}
344
+ }
345
+ }`,
346
+ variables: { deviceKey, deviceKeyType },
347
+ })
348
+ );
349
+ return agentEntanglementProofPreFlight;
350
+ }
351
+
352
+ addUpdatedListener(listener) {
353
+ this.#updatedCallbacks.push(listener);
354
+ }
355
+
356
+ addAppChangedListener(listener) {
357
+ this.#appsChangedCallback.push(listener);
358
+ }
359
+
360
+ subscribeAgentUpdated() {
361
+ this.#apolloClient
362
+ .subscribe({
363
+ query: gql` subscription {
364
+ agentUpdated { ${AGENT_SUBITEMS} }
365
+ }
366
+ `,
367
+ })
368
+ .subscribe({
369
+ next: (result) => {
370
+ const agent = result.data.agentUpdated;
371
+ this.#updatedCallbacks.forEach((cb) => {
372
+ cb(agent);
373
+ });
374
+ },
375
+ error: (e) => console.error(e),
376
+ });
377
+ }
378
+
379
+ subscribeAppsChanged() {
380
+ this.#apolloClient
381
+ .subscribe({
382
+ query: gql` subscription {
383
+ agentAppsChanged {
384
+ ${Apps_FIELDS}
385
+ }
386
+ }
387
+ `,
388
+ })
389
+ .subscribe({
390
+ next: (result) => {
391
+ this.#appsChangedCallback.forEach((cb) => {
392
+ cb();
393
+ });
394
+ },
395
+ error: (e) => console.error(e),
396
+ });
397
+ }
398
+
399
+ addAgentStatusChangedListener(listener) {
400
+ this.#agentStatusChangedCallbacks.push(listener);
401
+ }
402
+
403
+ subscribeAgentStatusChanged() {
404
+ this.#apolloClient
405
+ .subscribe({
406
+ query: gql` subscription {
407
+ agentStatusChanged { ${AGENT_STATUS_FIELDS} }
408
+ }
409
+ `,
410
+ })
411
+ .subscribe({
412
+ next: (result) => {
413
+ const agent = result.data.agentStatusChanged;
414
+ this.#agentStatusChangedCallbacks.forEach((cb) => {
415
+ cb(agent);
416
+ });
417
+ },
418
+ error: (e) => console.error(e),
419
+ });
420
+ }
421
+
422
+ async requestCapability(authInfo: AuthInfoInput): Promise<string> {
423
+ const { agentRequestCapability } = unwrapApolloResult(
424
+ await this.#apolloClient.mutate({
425
+ mutation: gql`
426
+ mutation agentRequestCapability($authInfo: AuthInfoInput!) {
427
+ agentRequestCapability(authInfo: $authInfo)
428
+ }
429
+ `,
430
+ variables: { authInfo },
431
+ })
432
+ );
433
+ return agentRequestCapability;
434
+ }
435
+
436
+ async permitCapability(auth: string): Promise<string> {
437
+ const { agentPermitCapability } = unwrapApolloResult(
438
+ await this.#apolloClient.mutate({
439
+ mutation: gql`
440
+ mutation agentPermitCapability($auth: String!) {
441
+ agentPermitCapability(auth: $auth)
442
+ }
443
+ `,
444
+ variables: { auth },
445
+ })
446
+ );
447
+ return agentPermitCapability;
448
+ }
449
+
450
+ async generateJwt(requestId: string, rand: string): Promise<string> {
451
+ const { agentGenerateJwt } = unwrapApolloResult(
452
+ await this.#apolloClient.mutate({
453
+ mutation: gql`
454
+ mutation agentGenerateJwt($requestId: String!, $rand: String!) {
455
+ agentGenerateJwt(requestId: $requestId, rand: $rand)
456
+ }
457
+ `,
458
+ variables: { requestId, rand },
459
+ })
460
+ );
461
+ return agentGenerateJwt;
462
+ }
463
+
464
+ async getApps(): Promise<Apps[]> {
465
+ const { agentGetApps } = unwrapApolloResult(
466
+ await this.#apolloClient.mutate({
467
+ mutation: gql`query agentGetApps {
468
+ agentGetApps {
469
+ ${Apps_FIELDS}
470
+ }
471
+ }`,
472
+ })
473
+ );
474
+ return agentGetApps;
475
+ }
476
+
477
+ async removeApp(requestId: string): Promise<Apps[]> {
478
+ const { agentRemoveApp } = unwrapApolloResult(
479
+ await this.#apolloClient.mutate({
480
+ mutation: gql`mutation agentRemoveApp($requestId: String!) {
481
+ agentRemoveApp(requestId: $requestId) {
482
+ ${Apps_FIELDS}
483
+ }
484
+ }`,
485
+ variables: { requestId },
486
+ })
487
+ );
488
+ return agentRemoveApp;
489
+ }
490
+
491
+ async revokeToken(requestId: string): Promise<Apps[]> {
492
+ const { agentRevokeToken } = unwrapApolloResult(
493
+ await this.#apolloClient.mutate({
494
+ mutation: gql`mutation agentRevokeToken($requestId: String!) {
495
+ agentRevokeToken(requestId: $requestId) {
496
+ ${Apps_FIELDS}
497
+ }
498
+ }`,
499
+ variables: { requestId },
500
+ })
501
+ );
502
+ return agentRevokeToken;
503
+ }
504
+
505
+ async isLocked(): Promise<boolean> {
506
+ const { agentIsLocked } = unwrapApolloResult(
507
+ await this.#apolloClient.mutate({
508
+ mutation: gql`
509
+ query agentIsLocked {
510
+ agentIsLocked
511
+ }
512
+ `,
513
+ })
514
+ );
515
+ return agentIsLocked;
516
+ }
517
+
518
+ async signMessage(message: string): Promise<string> {
519
+ const { agentSignMessage } = unwrapApolloResult(
520
+ await this.#apolloClient.mutate({
521
+ mutation: gql`mutation agentSignMessage($message: String!) {
522
+ agentSignMessage(message: $message) {
523
+ ${AGENT_SIGNATURE_FIELDS}
524
+ }
525
+ }`,
526
+ variables: { message },
527
+ })
528
+ );
529
+ return agentSignMessage;
530
+ }
531
+ }