@didcid/keymaster 0.1.3

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 (70) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +109 -0
  3. package/dist/cjs/db/abstract-base.cjs +25 -0
  4. package/dist/cjs/db/cache.cjs +27 -0
  5. package/dist/cjs/db/chrome.cjs +32 -0
  6. package/dist/cjs/db/json-memory.cjs +24 -0
  7. package/dist/cjs/db/json.cjs +35 -0
  8. package/dist/cjs/db/mongo.cjs +57 -0
  9. package/dist/cjs/db/redis.cjs +55 -0
  10. package/dist/cjs/db/sqlite.cjs +69 -0
  11. package/dist/cjs/db/typeGuards.cjs +11 -0
  12. package/dist/cjs/db/web.cjs +29 -0
  13. package/dist/cjs/encryption.cjs +59 -0
  14. package/dist/cjs/index.cjs +32 -0
  15. package/dist/cjs/keymaster-client.cjs +1139 -0
  16. package/dist/cjs/keymaster.cjs +3787 -0
  17. package/dist/cjs/node.cjs +45 -0
  18. package/dist/cjs/search-client.cjs +87 -0
  19. package/dist/esm/db/abstract-base.js +22 -0
  20. package/dist/esm/db/abstract-base.js.map +1 -0
  21. package/dist/esm/db/cache.js +21 -0
  22. package/dist/esm/db/cache.js.map +1 -0
  23. package/dist/esm/db/chrome.js +26 -0
  24. package/dist/esm/db/chrome.js.map +1 -0
  25. package/dist/esm/db/json-memory.js +18 -0
  26. package/dist/esm/db/json-memory.js.map +1 -0
  27. package/dist/esm/db/json.js +29 -0
  28. package/dist/esm/db/json.js.map +1 -0
  29. package/dist/esm/db/mongo.js +51 -0
  30. package/dist/esm/db/mongo.js.map +1 -0
  31. package/dist/esm/db/redis.js +49 -0
  32. package/dist/esm/db/redis.js.map +1 -0
  33. package/dist/esm/db/sqlite.js +63 -0
  34. package/dist/esm/db/sqlite.js.map +1 -0
  35. package/dist/esm/db/typeGuards.js +7 -0
  36. package/dist/esm/db/typeGuards.js.map +1 -0
  37. package/dist/esm/db/web.js +23 -0
  38. package/dist/esm/db/web.js.map +1 -0
  39. package/dist/esm/encryption.js +55 -0
  40. package/dist/esm/encryption.js.map +1 -0
  41. package/dist/esm/index.js +11 -0
  42. package/dist/esm/index.js.map +1 -0
  43. package/dist/esm/keymaster-client.js +1133 -0
  44. package/dist/esm/keymaster-client.js.map +1 -0
  45. package/dist/esm/keymaster.js +2733 -0
  46. package/dist/esm/keymaster.js.map +1 -0
  47. package/dist/esm/node.js +7 -0
  48. package/dist/esm/node.js.map +1 -0
  49. package/dist/esm/search-client.js +81 -0
  50. package/dist/esm/search-client.js.map +1 -0
  51. package/dist/esm/types.js +2 -0
  52. package/dist/esm/types.js.map +1 -0
  53. package/dist/types/db/abstract-base.d.ts +7 -0
  54. package/dist/types/db/cache.d.ts +9 -0
  55. package/dist/types/db/chrome.d.ts +8 -0
  56. package/dist/types/db/json-memory.d.ts +7 -0
  57. package/dist/types/db/json.d.ts +9 -0
  58. package/dist/types/db/mongo.d.ts +15 -0
  59. package/dist/types/db/redis.d.ts +13 -0
  60. package/dist/types/db/sqlite.d.ts +12 -0
  61. package/dist/types/db/typeGuards.d.ts +3 -0
  62. package/dist/types/db/web.d.ts +8 -0
  63. package/dist/types/encryption.d.ts +10 -0
  64. package/dist/types/index.d.ts +10 -0
  65. package/dist/types/keymaster-client.d.ts +134 -0
  66. package/dist/types/keymaster.d.ts +211 -0
  67. package/dist/types/node.d.ts +6 -0
  68. package/dist/types/search-client.d.ts +9 -0
  69. package/dist/types/types.d.ts +373 -0
  70. package/package.json +171 -0
@@ -0,0 +1,1139 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var buffer = require('buffer');
6
+ var axiosModule = require('axios');
7
+
8
+ const axios = axiosModule?.default ??
9
+ axiosModule;
10
+ const VERSION = '/api/v1';
11
+ function throwError(error) {
12
+ if (error.response) {
13
+ throw error.response.data;
14
+ }
15
+ throw error;
16
+ }
17
+ class KeymasterClient {
18
+ API = "/api/v1";
19
+ // Factory method
20
+ static async create(options) {
21
+ const keymaster = new KeymasterClient();
22
+ await keymaster.connect(options);
23
+ return keymaster;
24
+ }
25
+ async connect(options = {}) {
26
+ if (options.url) {
27
+ this.API = `${options.url}${VERSION}`;
28
+ }
29
+ // Only used for unit testing
30
+ // TBD replace console with a real logging package
31
+ if (options.console) {
32
+ // eslint-disable-next-line
33
+ console = options.console;
34
+ }
35
+ if (options.waitUntilReady) {
36
+ await this.waitUntilReady(options);
37
+ }
38
+ }
39
+ async waitUntilReady(options = {}) {
40
+ let { intervalSeconds = 5, chatty = false, becomeChattyAfter = 0, maxRetries = 0 } = options;
41
+ let ready = false;
42
+ let retries = 0;
43
+ if (chatty) {
44
+ console.log(`Connecting to Keymaster at ${this.API}`);
45
+ }
46
+ while (!ready) {
47
+ ready = await this.isReady();
48
+ if (!ready) {
49
+ if (chatty) {
50
+ console.log('Waiting for Keymaster to be ready...');
51
+ }
52
+ // wait for 1 second before checking again
53
+ await new Promise(resolve => setTimeout(resolve, intervalSeconds * 1000));
54
+ }
55
+ retries += 1;
56
+ if (maxRetries > 0 && retries > maxRetries) {
57
+ return;
58
+ }
59
+ if (!chatty && becomeChattyAfter > 0 && retries > becomeChattyAfter) {
60
+ console.log(`Connecting to Keymaster at ${this.API}`);
61
+ chatty = true;
62
+ }
63
+ }
64
+ if (chatty) {
65
+ console.log('Keymaster service is ready!');
66
+ }
67
+ }
68
+ async isReady() {
69
+ try {
70
+ const response = await axios.get(`${this.API}/ready`);
71
+ return response.data.ready;
72
+ }
73
+ catch (error) {
74
+ return false;
75
+ }
76
+ }
77
+ async loadWallet() {
78
+ try {
79
+ const response = await axios.get(`${this.API}/wallet`);
80
+ return response.data.wallet;
81
+ }
82
+ catch (error) {
83
+ throwError(error);
84
+ }
85
+ }
86
+ async saveWallet(wallet) {
87
+ try {
88
+ const response = await axios.put(`${this.API}/wallet`, { wallet });
89
+ return response.data.ok;
90
+ }
91
+ catch (error) {
92
+ throwError(error);
93
+ }
94
+ }
95
+ async newWallet(mnemonic, overwrite = false) {
96
+ try {
97
+ const response = await axios.post(`${this.API}/wallet/new`, { mnemonic, overwrite });
98
+ return response.data.wallet;
99
+ }
100
+ catch (error) {
101
+ throwError(error);
102
+ }
103
+ }
104
+ async backupWallet() {
105
+ try {
106
+ const response = await axios.post(`${this.API}/wallet/backup`);
107
+ return response.data.ok;
108
+ }
109
+ catch (error) {
110
+ throwError(error);
111
+ }
112
+ }
113
+ async recoverWallet() {
114
+ try {
115
+ const response = await axios.post(`${this.API}/wallet/recover`);
116
+ return response.data.wallet;
117
+ }
118
+ catch (error) {
119
+ throwError(error);
120
+ }
121
+ }
122
+ async checkWallet() {
123
+ try {
124
+ const response = await axios.post(`${this.API}/wallet/check`);
125
+ return response.data.check;
126
+ }
127
+ catch (error) {
128
+ throwError(error);
129
+ }
130
+ }
131
+ async fixWallet() {
132
+ try {
133
+ const response = await axios.post(`${this.API}/wallet/fix`);
134
+ return response.data.fix;
135
+ }
136
+ catch (error) {
137
+ throwError(error);
138
+ }
139
+ }
140
+ async decryptMnemonic() {
141
+ try {
142
+ const response = await axios.get(`${this.API}/wallet/mnemonic`);
143
+ return response.data.mnemonic;
144
+ }
145
+ catch (error) {
146
+ throwError(error);
147
+ }
148
+ }
149
+ async listRegistries() {
150
+ try {
151
+ const response = await axios.get(`${this.API}/registries`);
152
+ return response.data.registries;
153
+ }
154
+ catch (error) {
155
+ throwError(error);
156
+ }
157
+ }
158
+ async getCurrentId() {
159
+ try {
160
+ const response = await axios.get(`${this.API}/ids/current`);
161
+ return response.data.current;
162
+ }
163
+ catch (error) {
164
+ throwError(error);
165
+ }
166
+ }
167
+ async setCurrentId(name) {
168
+ try {
169
+ const response = await axios.put(`${this.API}/ids/current`, { name });
170
+ return response.data.ok;
171
+ }
172
+ catch (error) {
173
+ throwError(error);
174
+ }
175
+ }
176
+ async listIds() {
177
+ try {
178
+ const response = await axios.get(`${this.API}/ids`);
179
+ return response.data.ids;
180
+ }
181
+ catch (error) {
182
+ throwError(error);
183
+ }
184
+ }
185
+ async rotateKeys() {
186
+ try {
187
+ const response = await axios.post(`${this.API}/keys/rotate`);
188
+ return response.data.ok;
189
+ }
190
+ catch (error) {
191
+ throwError(error);
192
+ }
193
+ }
194
+ async encryptMessage(msg, receiver, options = {}) {
195
+ try {
196
+ const response = await axios.post(`${this.API}/keys/encrypt/message`, { msg, receiver, options });
197
+ return response.data.did;
198
+ }
199
+ catch (error) {
200
+ throwError(error);
201
+ }
202
+ }
203
+ async decryptMessage(did) {
204
+ try {
205
+ const response = await axios.post(`${this.API}/keys/decrypt/message`, { did });
206
+ return response.data.message;
207
+ }
208
+ catch (error) {
209
+ throwError(error);
210
+ }
211
+ }
212
+ async encryptJSON(json, receiver, options) {
213
+ try {
214
+ const response = await axios.post(`${this.API}/keys/encrypt/json`, { json, receiver, options });
215
+ return response.data.did;
216
+ }
217
+ catch (error) {
218
+ throwError(error);
219
+ }
220
+ }
221
+ async decryptJSON(did) {
222
+ try {
223
+ const response = await axios.post(`${this.API}/keys/decrypt/json`, { did });
224
+ return response.data.json;
225
+ }
226
+ catch (error) {
227
+ throwError(error);
228
+ }
229
+ }
230
+ async createId(name, options) {
231
+ try {
232
+ const response = await axios.post(`${this.API}/ids`, { name, options });
233
+ return response.data.did;
234
+ }
235
+ catch (error) {
236
+ throwError(error);
237
+ }
238
+ }
239
+ async removeId(id) {
240
+ try {
241
+ const response = await axios.delete(`${this.API}/ids/${id}`);
242
+ return response.data.ok;
243
+ }
244
+ catch (error) {
245
+ throwError(error);
246
+ }
247
+ }
248
+ async renameId(id, name) {
249
+ try {
250
+ const response = await axios.post(`${this.API}/ids/${id}/rename`, { name });
251
+ return response.data.ok;
252
+ }
253
+ catch (error) {
254
+ throwError(error);
255
+ }
256
+ }
257
+ async backupId(id) {
258
+ try {
259
+ if (!id) {
260
+ id = await this.getCurrentId();
261
+ }
262
+ const response = await axios.post(`${this.API}/ids/${id}/backup`);
263
+ return response.data.ok;
264
+ }
265
+ catch (error) {
266
+ throwError(error);
267
+ }
268
+ }
269
+ async recoverId(did) {
270
+ try {
271
+ const response = await axios.post(`${this.API}/ids/${did}/recover`);
272
+ return response.data.recovered;
273
+ }
274
+ catch (error) {
275
+ throwError(error);
276
+ }
277
+ }
278
+ async listNames() {
279
+ try {
280
+ const response = await axios.get(`${this.API}/names`);
281
+ return response.data.names;
282
+ }
283
+ catch (error) {
284
+ throwError(error);
285
+ }
286
+ }
287
+ async addName(name, did) {
288
+ try {
289
+ const response = await axios.post(`${this.API}/names`, { name, did });
290
+ return response.data.ok;
291
+ }
292
+ catch (error) {
293
+ throwError(error);
294
+ }
295
+ }
296
+ async getName(name) {
297
+ try {
298
+ const response = await axios.get(`${this.API}/names/${name}`);
299
+ return response.data.did;
300
+ }
301
+ catch (error) {
302
+ throwError(error);
303
+ }
304
+ }
305
+ async removeName(name) {
306
+ try {
307
+ const response = await axios.delete(`${this.API}/names/${name}`);
308
+ return response.data.ok;
309
+ }
310
+ catch (error) {
311
+ throwError(error);
312
+ }
313
+ }
314
+ async resolveDID(id, options) {
315
+ try {
316
+ if (options) {
317
+ const queryParams = new URLSearchParams(options);
318
+ const response = await axios.get(`${this.API}/did/${id}?${queryParams.toString()}`);
319
+ return response.data.docs;
320
+ }
321
+ else {
322
+ const response = await axios.get(`${this.API}/did/${id}`);
323
+ return response.data.docs;
324
+ }
325
+ }
326
+ catch (error) {
327
+ throwError(error);
328
+ }
329
+ }
330
+ async revokeDID(id) {
331
+ try {
332
+ const response = await axios.delete(`${this.API}/did/${id}`);
333
+ return response.data.ok;
334
+ }
335
+ catch (error) {
336
+ throwError(error);
337
+ }
338
+ }
339
+ async createAsset(data, options) {
340
+ try {
341
+ const response = await axios.post(`${this.API}/assets`, { data, options });
342
+ return response.data.did;
343
+ }
344
+ catch (error) {
345
+ throwError(error);
346
+ }
347
+ }
348
+ async cloneAsset(id, options) {
349
+ try {
350
+ const response = await axios.post(`${this.API}/assets/${id}/clone`, { options });
351
+ return response.data.did;
352
+ }
353
+ catch (error) {
354
+ throwError(error);
355
+ }
356
+ }
357
+ async listAssets() {
358
+ try {
359
+ const response = await axios.get(`${this.API}/assets`);
360
+ return response.data.assets;
361
+ }
362
+ catch (error) {
363
+ throwError(error);
364
+ }
365
+ }
366
+ async resolveAsset(id, options) {
367
+ try {
368
+ if (options) {
369
+ const queryParams = new URLSearchParams(options);
370
+ const response = await axios.get(`${this.API}/assets/${id}?${queryParams.toString()}`);
371
+ return response.data.asset;
372
+ }
373
+ else {
374
+ const response = await axios.get(`${this.API}/assets/${id}`);
375
+ return response.data.asset;
376
+ }
377
+ }
378
+ catch (error) {
379
+ throwError(error);
380
+ }
381
+ }
382
+ async updateAsset(id, data) {
383
+ try {
384
+ const response = await axios.put(`${this.API}/assets/${id}`, { data });
385
+ return response.data.ok;
386
+ }
387
+ catch (error) {
388
+ throwError(error);
389
+ }
390
+ }
391
+ async transferAsset(id, controller) {
392
+ try {
393
+ const response = await axios.post(`${this.API}/assets/${id}/transfer`, { controller });
394
+ return response.data.ok;
395
+ }
396
+ catch (error) {
397
+ throwError(error);
398
+ }
399
+ }
400
+ async createChallenge(challenge = {}, options = {}) {
401
+ try {
402
+ const response = await axios.post(`${this.API}/challenge`, { challenge, options });
403
+ return response.data.did;
404
+ }
405
+ catch (error) {
406
+ throwError(error);
407
+ }
408
+ }
409
+ async createResponse(challenge, options) {
410
+ try {
411
+ const response = await axios.post(`${this.API}/response`, { challenge, options });
412
+ return response.data.did;
413
+ }
414
+ catch (error) {
415
+ throwError(error);
416
+ }
417
+ }
418
+ async verifyResponse(responseDID, options) {
419
+ try {
420
+ const response = await axios.post(`${this.API}/response/verify`, { response: responseDID, options });
421
+ return response.data.verify;
422
+ }
423
+ catch (error) {
424
+ throwError(error);
425
+ }
426
+ }
427
+ async createGroup(name, options) {
428
+ try {
429
+ const response = await axios.post(`${this.API}/groups`, { name, options });
430
+ return response.data.did;
431
+ }
432
+ catch (error) {
433
+ throwError(error);
434
+ }
435
+ }
436
+ async getGroup(group) {
437
+ try {
438
+ const response = await axios.get(`${this.API}/groups/${group}`);
439
+ return response.data.group;
440
+ }
441
+ catch (error) {
442
+ throwError(error);
443
+ }
444
+ }
445
+ async addGroupMember(group, member) {
446
+ try {
447
+ const response = await axios.post(`${this.API}/groups/${group}/add`, { member });
448
+ return response.data.ok;
449
+ }
450
+ catch (error) {
451
+ throwError(error);
452
+ }
453
+ }
454
+ async removeGroupMember(group, member) {
455
+ try {
456
+ const response = await axios.post(`${this.API}/groups/${group}/remove`, { member });
457
+ return response.data.ok;
458
+ }
459
+ catch (error) {
460
+ throwError(error);
461
+ }
462
+ }
463
+ async testGroup(group, member) {
464
+ try {
465
+ const response = await axios.post(`${this.API}/groups/${group}/test`, { member });
466
+ return response.data.test;
467
+ }
468
+ catch (error) {
469
+ throwError(error);
470
+ }
471
+ }
472
+ async listGroups(owner) {
473
+ try {
474
+ if (owner) {
475
+ const response = await axios.get(`${this.API}/groups?owner=${owner}`);
476
+ return response.data.groups;
477
+ }
478
+ else {
479
+ const response = await axios.get(`${this.API}/groups`);
480
+ return response.data.groups;
481
+ }
482
+ }
483
+ catch (error) {
484
+ throwError(error);
485
+ }
486
+ }
487
+ async createSchema(schema, options) {
488
+ try {
489
+ const response = await axios.post(`${this.API}/schemas`, { schema, options });
490
+ return response.data.did;
491
+ }
492
+ catch (error) {
493
+ throwError(error);
494
+ }
495
+ }
496
+ async getSchema(id) {
497
+ try {
498
+ const response = await axios.get(`${this.API}/schemas/${id}`);
499
+ return response.data.schema;
500
+ }
501
+ catch (error) {
502
+ throwError(error);
503
+ }
504
+ }
505
+ async setSchema(id, schema) {
506
+ try {
507
+ const response = await axios.put(`${this.API}/schemas/${id}`, { schema });
508
+ return response.data.ok;
509
+ }
510
+ catch (error) {
511
+ throwError(error);
512
+ }
513
+ }
514
+ async testSchema(id) {
515
+ try {
516
+ const response = await axios.post(`${this.API}/schemas/${id}/test`);
517
+ return response.data.test;
518
+ }
519
+ catch (error) {
520
+ throwError(error);
521
+ }
522
+ }
523
+ async listSchemas(owner) {
524
+ try {
525
+ if (owner) {
526
+ const response = await axios.get(`${this.API}/schemas?owner=${owner}`);
527
+ return response.data.schemas;
528
+ }
529
+ else {
530
+ const response = await axios.get(`${this.API}/schemas`);
531
+ return response.data.schemas;
532
+ }
533
+ }
534
+ catch (error) {
535
+ throwError(error);
536
+ }
537
+ }
538
+ async createTemplate(schemaId) {
539
+ try {
540
+ const response = await axios.post(`${this.API}/schemas/${schemaId}/template`);
541
+ return response.data.template;
542
+ }
543
+ catch (error) {
544
+ throwError(error);
545
+ }
546
+ }
547
+ async testAgent(id) {
548
+ try {
549
+ const response = await axios.post(`${this.API}/agents/${id}/test`);
550
+ return response.data.test;
551
+ }
552
+ catch (error) {
553
+ throwError(error);
554
+ }
555
+ }
556
+ async bindCredential(schema, subject, options) {
557
+ try {
558
+ const response = await axios.post(`${this.API}/credentials/bind`, { schema, subject, options });
559
+ return response.data.credential;
560
+ }
561
+ catch (error) {
562
+ throwError(error);
563
+ }
564
+ }
565
+ async issueCredential(credential, options) {
566
+ try {
567
+ const response = await axios.post(`${this.API}/credentials/issued`, { credential, options });
568
+ return response.data.did;
569
+ }
570
+ catch (error) {
571
+ throwError(error);
572
+ }
573
+ }
574
+ async sendCredential(did, options) {
575
+ try {
576
+ const response = await axios.post(`${this.API}/credentials/issued/${did}/send`, { options });
577
+ return response.data.did;
578
+ }
579
+ catch (error) {
580
+ throwError(error);
581
+ }
582
+ }
583
+ async updateCredential(did, credential) {
584
+ try {
585
+ const response = await axios.post(`${this.API}/credentials/issued/${did}`, { credential });
586
+ return response.data.ok;
587
+ }
588
+ catch (error) {
589
+ throwError(error);
590
+ }
591
+ }
592
+ async listCredentials() {
593
+ try {
594
+ const response = await axios.get(`${this.API}/credentials/held`);
595
+ return response.data.held;
596
+ }
597
+ catch (error) {
598
+ throwError(error);
599
+ }
600
+ }
601
+ async acceptCredential(did) {
602
+ try {
603
+ const response = await axios.post(`${this.API}/credentials/held`, { did });
604
+ return response.data.ok;
605
+ }
606
+ catch (error) {
607
+ throwError(error);
608
+ }
609
+ }
610
+ async getCredential(did) {
611
+ try {
612
+ const response = await axios.get(`${this.API}/credentials/held/${did}`);
613
+ return response.data.credential;
614
+ }
615
+ catch (error) {
616
+ throwError(error);
617
+ }
618
+ }
619
+ async removeCredential(did) {
620
+ try {
621
+ const response = await axios.delete(`${this.API}/credentials/held/${did}`);
622
+ return response.data.ok;
623
+ }
624
+ catch (error) {
625
+ throwError(error);
626
+ }
627
+ }
628
+ async publishCredential(did, options) {
629
+ try {
630
+ const response = await axios.post(`${this.API}/credentials/held/${did}/publish`, { options });
631
+ return response.data.ok;
632
+ }
633
+ catch (error) {
634
+ throwError(error);
635
+ }
636
+ }
637
+ async unpublishCredential(did) {
638
+ try {
639
+ const response = await axios.post(`${this.API}/credentials/held/${did}/unpublish`);
640
+ return response.data.ok;
641
+ }
642
+ catch (error) {
643
+ throwError(error);
644
+ }
645
+ }
646
+ async listIssued() {
647
+ try {
648
+ const response = await axios.get(`${this.API}/credentials/issued`);
649
+ return response.data.issued;
650
+ }
651
+ catch (error) {
652
+ throwError(error);
653
+ }
654
+ }
655
+ async revokeCredential(did) {
656
+ try {
657
+ const response = await axios.delete(`${this.API}/credentials/issued/${did}`);
658
+ return response.data.ok;
659
+ }
660
+ catch (error) {
661
+ throwError(error);
662
+ }
663
+ }
664
+ async pollTemplate() {
665
+ try {
666
+ const response = await axios.get(`${this.API}/templates/poll`);
667
+ return response.data.template;
668
+ }
669
+ catch (error) {
670
+ throwError(error);
671
+ }
672
+ }
673
+ async createPoll(poll, options) {
674
+ try {
675
+ const response = await axios.post(`${this.API}/polls`, { poll, options });
676
+ return response.data.did;
677
+ }
678
+ catch (error) {
679
+ throwError(error);
680
+ }
681
+ }
682
+ async getPoll(pollId) {
683
+ try {
684
+ const response = await axios.get(`${this.API}/polls/${pollId}`);
685
+ return response.data.poll;
686
+ }
687
+ catch (error) {
688
+ throwError(error);
689
+ }
690
+ }
691
+ async viewPoll(pollId) {
692
+ try {
693
+ const response = await axios.get(`${this.API}/polls/${pollId}/view`);
694
+ return response.data.poll;
695
+ }
696
+ catch (error) {
697
+ throwError(error);
698
+ }
699
+ }
700
+ async votePoll(pollId, vote, options) {
701
+ try {
702
+ const response = await axios.post(`${this.API}/polls/${pollId}/vote`, { vote, options });
703
+ return response.data.did;
704
+ }
705
+ catch (error) {
706
+ throwError(error);
707
+ }
708
+ }
709
+ async updatePoll(ballot) {
710
+ try {
711
+ const response = await axios.put(`${this.API}/polls/update`, { ballot });
712
+ return response.data.ok;
713
+ }
714
+ catch (error) {
715
+ throwError(error);
716
+ }
717
+ }
718
+ async publishPoll(pollId, options) {
719
+ try {
720
+ const response = await axios.post(`${this.API}/polls/${pollId}/publish`, { options });
721
+ return response.data.ok;
722
+ }
723
+ catch (error) {
724
+ throwError(error);
725
+ }
726
+ }
727
+ async unpublishPoll(pollId) {
728
+ try {
729
+ const response = await axios.post(`${this.API}/polls/${pollId}/unpublish`);
730
+ return response.data.ok;
731
+ }
732
+ catch (error) {
733
+ throwError(error);
734
+ }
735
+ }
736
+ async createImage(data, options = {}) {
737
+ try {
738
+ const response = await axios.post(`${this.API}/images`, data, {
739
+ headers: {
740
+ // eslint-disable-next-line
741
+ 'Content-Type': 'application/octet-stream',
742
+ 'X-Options': JSON.stringify(options), // Pass options as a custom header
743
+ }
744
+ });
745
+ return response.data.did;
746
+ }
747
+ catch (error) {
748
+ throwError(error);
749
+ }
750
+ }
751
+ async updateImage(id, data) {
752
+ try {
753
+ const response = await axios.put(`${this.API}/images/${id}`, data, {
754
+ headers: {
755
+ 'Content-Type': 'application/octet-stream'
756
+ }
757
+ });
758
+ return response.data.ok;
759
+ }
760
+ catch (error) {
761
+ throwError(error);
762
+ }
763
+ }
764
+ async getImage(id) {
765
+ try {
766
+ const response = await axios.get(`${this.API}/images/${id}`);
767
+ return response.data.image;
768
+ }
769
+ catch (error) {
770
+ throwError(error);
771
+ }
772
+ }
773
+ async testImage(id) {
774
+ try {
775
+ const response = await axios.post(`${this.API}/images/${id}/test`);
776
+ return response.data.test;
777
+ }
778
+ catch (error) {
779
+ throwError(error);
780
+ }
781
+ }
782
+ async createDocument(data, options = {}) {
783
+ try {
784
+ const response = await axios.post(`${this.API}/documents`, data, {
785
+ headers: {
786
+ 'Content-Type': 'application/octet-stream',
787
+ 'X-Options': JSON.stringify(options), // Pass options as a custom header
788
+ }
789
+ });
790
+ return response.data.did;
791
+ }
792
+ catch (error) {
793
+ throwError(error);
794
+ }
795
+ }
796
+ async updateDocument(id, data, options = {}) {
797
+ try {
798
+ const response = await axios.put(`${this.API}/documents/${id}`, data, {
799
+ headers: {
800
+ 'Content-Type': 'application/octet-stream',
801
+ 'X-Options': JSON.stringify(options), // Pass options as a custom header
802
+ }
803
+ });
804
+ return response.data.ok;
805
+ }
806
+ catch (error) {
807
+ throwError(error);
808
+ }
809
+ }
810
+ async getDocument(id) {
811
+ try {
812
+ const response = await axios.get(`${this.API}/documents/${id}`);
813
+ return response.data.document;
814
+ }
815
+ catch (error) {
816
+ throwError(error);
817
+ }
818
+ }
819
+ async testDocument(id) {
820
+ try {
821
+ const response = await axios.post(`${this.API}/documents/${id}/test`);
822
+ return response.data.test;
823
+ }
824
+ catch (error) {
825
+ throwError(error);
826
+ }
827
+ }
828
+ async createGroupVault(options = {}) {
829
+ try {
830
+ const response = await axios.post(`${this.API}/groupVaults`, { options });
831
+ return response.data.did;
832
+ }
833
+ catch (error) {
834
+ throwError(error);
835
+ }
836
+ }
837
+ async getGroupVault(id, options) {
838
+ try {
839
+ if (options) {
840
+ const queryParams = new URLSearchParams(options);
841
+ const response = await axios.get(`${this.API}/groupVaults/${id}?${queryParams.toString()}`);
842
+ return response.data.groupVault;
843
+ }
844
+ else {
845
+ const response = await axios.get(`${this.API}/groupVaults/${id}`);
846
+ return response.data.groupVault;
847
+ }
848
+ }
849
+ catch (error) {
850
+ throwError(error);
851
+ }
852
+ }
853
+ async testGroupVault(id, options) {
854
+ try {
855
+ const response = await axios.post(`${this.API}/groupVaults/${id}/test`, { options });
856
+ return response.data.test;
857
+ }
858
+ catch (error) {
859
+ throwError(error);
860
+ }
861
+ }
862
+ async addGroupVaultMember(vaultId, memberId) {
863
+ try {
864
+ const response = await axios.post(`${this.API}/groupVaults/${vaultId}/members`, { memberId });
865
+ return response.data.ok;
866
+ }
867
+ catch (error) {
868
+ throwError(error);
869
+ }
870
+ }
871
+ async removeGroupVaultMember(vaultId, memberId) {
872
+ try {
873
+ const response = await axios.delete(`${this.API}/groupVaults/${vaultId}/members/${memberId}`);
874
+ return response.data.ok;
875
+ }
876
+ catch (error) {
877
+ throwError(error);
878
+ }
879
+ }
880
+ async listGroupVaultMembers(vaultId) {
881
+ try {
882
+ const response = await axios.get(`${this.API}/groupVaults/${vaultId}/members`);
883
+ return response.data.members;
884
+ }
885
+ catch (error) {
886
+ throwError(error);
887
+ }
888
+ }
889
+ async addGroupVaultItem(vaultId, name, buffer) {
890
+ try {
891
+ const response = await axios.post(`${this.API}/groupVaults/${vaultId}/items`, buffer, {
892
+ headers: {
893
+ // eslint-disable-next-line
894
+ 'Content-Type': 'application/octet-stream',
895
+ 'X-Options': JSON.stringify({ name }), // Pass name as a custom header
896
+ }
897
+ });
898
+ return response.data.ok;
899
+ }
900
+ catch (error) {
901
+ throwError(error);
902
+ }
903
+ }
904
+ async removeGroupVaultItem(vaultId, name) {
905
+ try {
906
+ const response = await axios.delete(`${this.API}/groupVaults/${vaultId}/items/${name}`);
907
+ return response.data.ok;
908
+ }
909
+ catch (error) {
910
+ throwError(error);
911
+ }
912
+ }
913
+ async listGroupVaultItems(vaultId, options) {
914
+ try {
915
+ if (options) {
916
+ const queryParams = new URLSearchParams(options);
917
+ const response = await axios.get(`${this.API}/groupVaults/${vaultId}/items?${queryParams.toString()}`);
918
+ return response.data.items;
919
+ }
920
+ else {
921
+ const response = await axios.get(`${this.API}/groupVaults/${vaultId}/items`);
922
+ return response.data.items;
923
+ }
924
+ }
925
+ catch (error) {
926
+ throwError(error);
927
+ }
928
+ }
929
+ async getGroupVaultItem(vaultId, name, options) {
930
+ try {
931
+ let url = `${this.API}/groupVaults/${vaultId}/items/${name}`;
932
+ if (options) {
933
+ const queryParams = new URLSearchParams(options);
934
+ url += `?${queryParams.toString()}`;
935
+ }
936
+ const response = await axios.get(url, {
937
+ responseType: 'arraybuffer'
938
+ });
939
+ if (!response.data || (buffer.Buffer.isBuffer(response.data) && response.data.length === 0)) {
940
+ return null;
941
+ }
942
+ return buffer.Buffer.from(response.data);
943
+ }
944
+ catch (error) {
945
+ const axiosError = error;
946
+ // Return null for 404 Not Found
947
+ if (axiosError.response && axiosError.response.status === 404) {
948
+ return null;
949
+ }
950
+ if (axiosError.response && axiosError.response.data instanceof Uint8Array) {
951
+ const textDecoder = new TextDecoder();
952
+ const errorMessage = textDecoder.decode(axiosError.response.data);
953
+ axiosError.response.data = JSON.parse(errorMessage);
954
+ }
955
+ throwError(axiosError);
956
+ }
957
+ }
958
+ async listDmail() {
959
+ try {
960
+ const response = await axios.get(`${this.API}/dmail`);
961
+ return response.data.dmail;
962
+ }
963
+ catch (error) {
964
+ throwError(error);
965
+ }
966
+ }
967
+ async createDmail(message, options = {}) {
968
+ try {
969
+ const response = await axios.post(`${this.API}/dmail`, { message, options });
970
+ return response.data.did;
971
+ }
972
+ catch (error) {
973
+ throwError(error);
974
+ }
975
+ }
976
+ async updateDmail(did, message) {
977
+ try {
978
+ const response = await axios.put(`${this.API}/dmail/${did}`, { message });
979
+ return response.data.ok;
980
+ }
981
+ catch (error) {
982
+ throwError(error);
983
+ }
984
+ }
985
+ async sendDmail(did) {
986
+ try {
987
+ const response = await axios.post(`${this.API}/dmail/${did}/send`);
988
+ return response.data.did;
989
+ }
990
+ catch (error) {
991
+ throwError(error);
992
+ }
993
+ }
994
+ async fileDmail(did, tags) {
995
+ try {
996
+ const response = await axios.post(`${this.API}/dmail/${did}/file`, { tags });
997
+ return response.data.ok;
998
+ }
999
+ catch (error) {
1000
+ throwError(error);
1001
+ }
1002
+ }
1003
+ async removeDmail(did) {
1004
+ try {
1005
+ const response = await axios.delete(`${this.API}/dmail/${did}`);
1006
+ return response.data.ok;
1007
+ }
1008
+ catch (error) {
1009
+ throwError(error);
1010
+ }
1011
+ }
1012
+ async getDmailMessage(did, options) {
1013
+ try {
1014
+ if (options) {
1015
+ const queryParams = new URLSearchParams(options);
1016
+ const response = await axios.get(`${this.API}/dmail/${did}?${queryParams.toString()}`);
1017
+ return response.data.message;
1018
+ }
1019
+ else {
1020
+ const response = await axios.get(`${this.API}/dmail/${did}`);
1021
+ return response.data.message;
1022
+ }
1023
+ }
1024
+ catch (error) {
1025
+ throwError(error);
1026
+ }
1027
+ }
1028
+ async listDmailAttachments(did, options) {
1029
+ try {
1030
+ if (options) {
1031
+ const queryParams = new URLSearchParams(options);
1032
+ const response = await axios.get(`${this.API}/dmail/${did}/attachments?${queryParams.toString()}`);
1033
+ return response.data.attachments;
1034
+ }
1035
+ else {
1036
+ const response = await axios.get(`${this.API}/dmail/${did}/attachments`);
1037
+ return response.data.attachments;
1038
+ }
1039
+ }
1040
+ catch (error) {
1041
+ throwError(error);
1042
+ }
1043
+ }
1044
+ async addDmailAttachment(did, name, buffer) {
1045
+ try {
1046
+ const response = await axios.post(`${this.API}/dmail/${did}/attachments`, buffer, {
1047
+ headers: {
1048
+ // eslint-disable-next-line
1049
+ 'Content-Type': 'application/octet-stream',
1050
+ 'X-Options': JSON.stringify({ name }), // Pass name as a custom header
1051
+ }
1052
+ });
1053
+ return response.data.ok;
1054
+ }
1055
+ catch (error) {
1056
+ throwError(error);
1057
+ }
1058
+ }
1059
+ async removeDmailAttachment(did, name) {
1060
+ try {
1061
+ const response = await axios.delete(`${this.API}/dmail/${did}/attachments/${name}`);
1062
+ return response.data.ok;
1063
+ }
1064
+ catch (error) {
1065
+ throwError(error);
1066
+ }
1067
+ }
1068
+ async getDmailAttachment(did, name) {
1069
+ try {
1070
+ const response = await axios.get(`${this.API}/dmail/${did}/attachments/${name}`, {
1071
+ responseType: 'arraybuffer'
1072
+ });
1073
+ if (!response.data || (buffer.Buffer.isBuffer(response.data) && response.data.length === 0)) {
1074
+ return null;
1075
+ }
1076
+ return buffer.Buffer.from(response.data);
1077
+ }
1078
+ catch (error) {
1079
+ const axiosError = error;
1080
+ // Return null for 404 Not Found
1081
+ if (axiosError.response && axiosError.response.status === 404) {
1082
+ return null;
1083
+ }
1084
+ if (axiosError.response && axiosError.response.data instanceof Uint8Array) {
1085
+ const textDecoder = new TextDecoder();
1086
+ const errorMessage = textDecoder.decode(axiosError.response.data);
1087
+ axiosError.response.data = JSON.parse(errorMessage);
1088
+ }
1089
+ throwError(axiosError);
1090
+ }
1091
+ }
1092
+ async importDmail(did) {
1093
+ try {
1094
+ const response = await axios.post(`${this.API}/dmail/import`, { did });
1095
+ return response.data.ok;
1096
+ }
1097
+ catch (error) {
1098
+ throwError(error);
1099
+ }
1100
+ }
1101
+ async createNotice(message, options = {}) {
1102
+ try {
1103
+ const response = await axios.post(`${this.API}/notices`, { message, options });
1104
+ return response.data.did;
1105
+ }
1106
+ catch (error) {
1107
+ throwError(error);
1108
+ }
1109
+ }
1110
+ async updateNotice(did, message) {
1111
+ try {
1112
+ const response = await axios.put(`${this.API}/notices/${did}`, { message });
1113
+ return response.data.ok;
1114
+ }
1115
+ catch (error) {
1116
+ throwError(error);
1117
+ }
1118
+ }
1119
+ async refreshNotices() {
1120
+ try {
1121
+ const response = await axios.post(`${this.API}/notices/refresh`);
1122
+ return response.data.ok;
1123
+ }
1124
+ catch (error) {
1125
+ throwError(error);
1126
+ }
1127
+ }
1128
+ async exportEncryptedWallet() {
1129
+ try {
1130
+ const response = await axios.get(`${this.API}/export/wallet/encrypted`);
1131
+ return response.data.wallet;
1132
+ }
1133
+ catch (error) {
1134
+ throwError(error);
1135
+ }
1136
+ }
1137
+ }
1138
+
1139
+ exports.default = KeymasterClient;