@icp-sdk/vetkeys 0.5.0-beta.0

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.
@@ -0,0 +1,650 @@
1
+ import { T as h, E as f, D as g, a as v } from "./index-BUXhtQhx.mjs";
2
+ import { A as m } from "./actor-4fotMNaR.mjs";
3
+ function y(r) {
4
+ return new Promise((e, t) => {
5
+ r.oncomplete = r.onsuccess = () => e(r.result), r.onabort = r.onerror = () => t(r.error);
6
+ });
7
+ }
8
+ function w(r, e) {
9
+ let t;
10
+ const n = () => {
11
+ if (t)
12
+ return t;
13
+ const a = indexedDB.open(r);
14
+ return a.onupgradeneeded = () => a.result.createObjectStore(e), t = y(a), t.then((i) => {
15
+ i.onclose = () => t = void 0;
16
+ }, () => {
17
+ }), t;
18
+ };
19
+ return (a, i) => n().then((s) => i(s.transaction(e, a).objectStore(e)));
20
+ }
21
+ let u;
22
+ function d() {
23
+ return u || (u = w("keyval-store", "keyval")), u;
24
+ }
25
+ function k(r, e = d()) {
26
+ return e("readonly", (t) => y(t.get(r)));
27
+ }
28
+ function E(r, e, t = d()) {
29
+ return t("readwrite", (n) => (n.put(e, r), y(n.transaction)));
30
+ }
31
+ const V = ({ IDL: r }) => {
32
+ const e = r.Record({ inner: r.Vec(r.Nat8) }), t = r.Variant({
33
+ Read: r.Null,
34
+ ReadWrite: r.Null,
35
+ ReadWriteManage: r.Null
36
+ }), n = r.Record({
37
+ access_control: r.Vec(r.Tuple(r.Principal, t)),
38
+ keyvals: r.Vec(r.Tuple(e, e)),
39
+ map_name: e,
40
+ map_owner: r.Principal
41
+ }), a = r.Variant({ Ok: r.Opt(e), Err: r.Text }), i = r.Variant({
42
+ Ok: r.Vec(r.Tuple(e, e)),
43
+ Err: r.Text
44
+ }), s = r.Variant({ Ok: e, Err: r.Text }), l = r.Variant({
45
+ Ok: r.Vec(r.Tuple(r.Principal, t)),
46
+ Err: r.Text
47
+ }), o = r.Variant({
48
+ Ok: r.Opt(t),
49
+ Err: r.Text
50
+ }), _ = r.Variant({ Ok: r.Vec(e), Err: r.Text });
51
+ return r.Service({
52
+ get_accessible_shared_map_names: r.Func(
53
+ [],
54
+ [r.Vec(r.Tuple(r.Principal, e))],
55
+ ["query"]
56
+ ),
57
+ get_all_accessible_encrypted_maps: r.Func(
58
+ [],
59
+ [r.Vec(n)],
60
+ ["query"]
61
+ ),
62
+ get_all_accessible_encrypted_values: r.Func(
63
+ [],
64
+ [
65
+ r.Vec(
66
+ r.Tuple(
67
+ r.Tuple(r.Principal, e),
68
+ r.Vec(r.Tuple(e, e))
69
+ )
70
+ )
71
+ ],
72
+ ["query"]
73
+ ),
74
+ get_encrypted_value: r.Func(
75
+ [r.Principal, e, e],
76
+ [a],
77
+ ["query"]
78
+ ),
79
+ get_encrypted_values_for_map: r.Func(
80
+ [r.Principal, e],
81
+ [i],
82
+ ["query"]
83
+ ),
84
+ get_encrypted_vetkey: r.Func(
85
+ [r.Principal, e, e],
86
+ [s],
87
+ []
88
+ ),
89
+ get_owned_non_empty_map_names: r.Func(
90
+ [],
91
+ [r.Vec(e)],
92
+ ["query"]
93
+ ),
94
+ get_shared_user_access_for_map: r.Func(
95
+ [r.Principal, e],
96
+ [l],
97
+ ["query"]
98
+ ),
99
+ get_user_rights: r.Func(
100
+ [r.Principal, e, r.Principal],
101
+ [o],
102
+ ["query"]
103
+ ),
104
+ get_vetkey_verification_key: r.Func([], [e], []),
105
+ insert_encrypted_value: r.Func(
106
+ [r.Principal, e, e, e],
107
+ [a],
108
+ []
109
+ ),
110
+ remove_encrypted_value: r.Func(
111
+ [r.Principal, e, e],
112
+ [a],
113
+ []
114
+ ),
115
+ remove_map_values: r.Func([r.Principal, e], [_], []),
116
+ remove_user: r.Func(
117
+ [r.Principal, e, r.Principal],
118
+ [o],
119
+ []
120
+ ),
121
+ set_user_rights: r.Func(
122
+ [r.Principal, e, r.Principal, t],
123
+ [o],
124
+ []
125
+ )
126
+ });
127
+ };
128
+ class U {
129
+ actor;
130
+ constructor(e, t) {
131
+ this.actor = m.createActor(V, {
132
+ agent: e,
133
+ canisterId: t
134
+ });
135
+ }
136
+ get_accessible_shared_map_names() {
137
+ return this.actor.get_accessible_shared_map_names();
138
+ }
139
+ get_shared_user_access_for_map(e, t) {
140
+ return this.actor.get_shared_user_access_for_map(e, t);
141
+ }
142
+ get_owned_non_empty_map_names() {
143
+ return this.actor.get_owned_non_empty_map_names();
144
+ }
145
+ get_all_accessible_encrypted_values() {
146
+ return this.actor.get_all_accessible_encrypted_values();
147
+ }
148
+ get_all_accessible_encrypted_maps() {
149
+ return this.actor.get_all_accessible_encrypted_maps();
150
+ }
151
+ get_encrypted_value(e, t, n) {
152
+ return this.actor.get_encrypted_value(e, t, n);
153
+ }
154
+ get_encrypted_values_for_map(e, t) {
155
+ return this.actor.get_encrypted_values_for_map(e, t);
156
+ }
157
+ get_encrypted_vetkey(e, t, n) {
158
+ return this.actor.get_encrypted_vetkey(e, t, n);
159
+ }
160
+ insert_encrypted_value(e, t, n, a) {
161
+ return this.actor.insert_encrypted_value(
162
+ e,
163
+ t,
164
+ n,
165
+ a
166
+ );
167
+ }
168
+ remove_encrypted_value(e, t, n) {
169
+ return this.actor.remove_encrypted_value(e, t, n);
170
+ }
171
+ remove_map_values(e, t) {
172
+ return this.actor.remove_map_values(e, t);
173
+ }
174
+ get_vetkey_verification_key() {
175
+ return this.actor.get_vetkey_verification_key();
176
+ }
177
+ set_user_rights(e, t, n, a) {
178
+ return this.actor.set_user_rights(e, t, n, a);
179
+ }
180
+ get_user_rights(e, t, n) {
181
+ return this.actor.get_user_rights(e, t, n);
182
+ }
183
+ remove_user(e, t, n) {
184
+ return this.actor.remove_user(e, t, n);
185
+ }
186
+ }
187
+ class F {
188
+ /**
189
+ * The client instance for interacting with the EncryptedMaps canister.
190
+ */
191
+ canisterClient;
192
+ /**
193
+ * The cached verification key for validating encrypted VetKeys.
194
+ */
195
+ verificationKey = void 0;
196
+ /**
197
+ * Creates a new instance of the EncryptedMaps client.
198
+ *
199
+ * @example
200
+ * ```ts
201
+ * import { EncryptedMaps } from "@icp-sdk/vetkeys/encrypted_maps";
202
+ *
203
+ * const encryptedMaps = new EncryptedMaps(encryptedMapsClientInstance);
204
+ * ```
205
+ */
206
+ constructor(e) {
207
+ this.canisterClient = e;
208
+ }
209
+ /**
210
+ * Retrieves a list of maps that were shared with the user and the user still has access to.
211
+ *
212
+ * @example
213
+ * ```ts
214
+ * const sharedMaps = await encryptedMaps.getAccessibleSharedMapNames();
215
+ * console.log("Shared Maps:", sharedMaps);
216
+ * ```
217
+ *
218
+ * @returns Promise resolving to an array of `[Principal, Uint8Array]` pairs representing accessible map identifiers.
219
+ */
220
+ async getAccessibleSharedMapNames() {
221
+ return (await this.canisterClient.get_accessible_shared_map_names()).map(([e, t]) => [e, Uint8Array.from(t.inner)]);
222
+ }
223
+ /**
224
+ * Retrieves a list of non-empty maps owned by the caller.
225
+ *
226
+ * @returns Promise resolving to an array of map names
227
+ */
228
+ async getOwnedNonEmptyMapNames() {
229
+ return (await this.canisterClient.get_owned_non_empty_map_names()).map(
230
+ (e) => Uint8Array.from(e.inner)
231
+ );
232
+ }
233
+ /**
234
+ * Retrieves all accessible values across all maps the user has access to.
235
+ *
236
+ * @returns Promise resolving to an array of map data with decrypted values
237
+ */
238
+ async getAllAccessibleValues() {
239
+ const e = await this.canisterClient.get_all_accessible_encrypted_values(), t = [];
240
+ for (const [n, a] of e) {
241
+ const i = Uint8Array.from(n[1].inner), s = [];
242
+ for (const [l, o] of a) {
243
+ const _ = Uint8Array.from(l.inner), p = await this.decryptFor(
244
+ n[0],
245
+ i,
246
+ _,
247
+ Uint8Array.from(o.inner)
248
+ );
249
+ s.push([_, p]);
250
+ }
251
+ t.push([
252
+ [n[0], Uint8Array.from(n[1].inner)],
253
+ s
254
+ ]);
255
+ }
256
+ return t;
257
+ }
258
+ /**
259
+ * Retrieves all accessible maps with their decrypted values.
260
+ *
261
+ * @returns Promise resolving to an array of map data
262
+ */
263
+ async getAllAccessibleMaps() {
264
+ const e = await this.canisterClient.get_all_accessible_encrypted_maps(), t = [];
265
+ for (const n of e) {
266
+ const a = Uint8Array.from(n.map_name.inner), i = [];
267
+ for (const [
268
+ s,
269
+ l
270
+ ] of n.keyvals) {
271
+ const o = Uint8Array.from(s.inner), _ = await this.decryptFor(
272
+ n.map_owner,
273
+ a,
274
+ o,
275
+ Uint8Array.from(l.inner)
276
+ );
277
+ i.push([o, _]);
278
+ }
279
+ t.push({
280
+ accessControl: n.access_control,
281
+ keyvals: i,
282
+ mapName: a,
283
+ mapOwner: n.map_owner
284
+ });
285
+ }
286
+ return t;
287
+ }
288
+ /**
289
+ * Retrieves and decrypts a stored value from a map.
290
+ *
291
+ * @example
292
+ * ```ts
293
+ * const mapOwner = Principal.fromText("aaaaa-aa");
294
+ * const mapName = "passwords";
295
+ * const mapKey = "email_account";
296
+ *
297
+ * const storedValue = await encryptedMaps.getValue(mapOwner, mapName, mapKey);
298
+ * console.log("Decrypted Value:", new TextDecoder().decode(storedValue));
299
+ * ```
300
+ *
301
+ * @param mapOwner - The principal of the map owner
302
+ * @param mapName - The name/identifier of the map
303
+ * @param mapKey - The key to retrieve
304
+ * @returns Promise resolving to the decrypted value
305
+ * @throws Error if the operation fails
306
+ */
307
+ async getValue(e, t, n) {
308
+ const a = await this.canisterClient.get_encrypted_value(
309
+ e,
310
+ c(t),
311
+ c(n)
312
+ );
313
+ if ("Err" in a)
314
+ throw Error(a.Err);
315
+ return a.Ok.length === 0 ? new Uint8Array(0) : await this.decryptFor(
316
+ e,
317
+ t,
318
+ n,
319
+ Uint8Array.from(a.Ok[0].inner)
320
+ );
321
+ }
322
+ /**
323
+ * Retrieves all values from a specific map.
324
+ *
325
+ * @param mapOwner - The principal of the map owner
326
+ * @param mapName - The name/identifier of the map
327
+ * @returns Promise resolving to an array of key-value pairs
328
+ * @throws Error if the operation fails
329
+ */
330
+ async getValuesForMap(e, t) {
331
+ const n = await this.canisterClient.get_encrypted_values_for_map(
332
+ e,
333
+ c(t)
334
+ );
335
+ if ("Err" in n)
336
+ throw Error(n.Err);
337
+ const a = new Array();
338
+ for (const [s, l] of n.Ok)
339
+ a.push([
340
+ Uint8Array.from(s.inner),
341
+ Uint8Array.from(l.inner)
342
+ ]);
343
+ const i = new Array();
344
+ for (const [s, l] of n.Ok) {
345
+ const o = Uint8Array.from(s.inner), _ = await this.decryptFor(
346
+ e,
347
+ t,
348
+ o,
349
+ Uint8Array.from(l.inner)
350
+ );
351
+ i.push([o, _]);
352
+ }
353
+ return i;
354
+ }
355
+ /**
356
+ * Stores an encrypted value in a map.
357
+ *
358
+ * @example
359
+ * ```ts
360
+ * const value = new TextEncoder().encode("my_secure_password");
361
+ * const result = await encryptedMaps.setValue(mapOwner, mapName, mapKey, value);
362
+ * console.log("Replaced Value:", result);
363
+ * ```
364
+ *
365
+ * @param mapOwner - The principal of the map owner
366
+ * @param mapName - The name/identifier of the map
367
+ * @param mapKey - The key to store
368
+ * @param data - The value to store
369
+ * @returns Promise resolving to the previous value if it existed
370
+ * @throws Error if the operation fails
371
+ */
372
+ async setValue(e, t, n, a) {
373
+ const i = await this.encryptFor(
374
+ e,
375
+ t,
376
+ n,
377
+ a
378
+ ), s = await this.canisterClient.insert_encrypted_value(
379
+ e,
380
+ c(t),
381
+ c(n),
382
+ { inner: i }
383
+ );
384
+ if ("Err" in s)
385
+ throw Error(s.Err);
386
+ return s.Ok.length === 0 ? void 0 : await this.decryptFor(
387
+ e,
388
+ t,
389
+ n,
390
+ Uint8Array.from(s.Ok[0].inner)
391
+ );
392
+ }
393
+ /**
394
+ * Removes a value from a map.
395
+ *
396
+ * @param mapOwner - The principal of the map owner
397
+ * @param mapName - The name/identifier of the map
398
+ * @param mapKey - The key to remove
399
+ * @returns Promise resolving to the removed value if it existed
400
+ * @throws Error if the operation fails
401
+ */
402
+ async removeEncryptedValue(e, t, n) {
403
+ const a = await this.canisterClient.remove_encrypted_value(
404
+ e,
405
+ c(t),
406
+ c(n)
407
+ );
408
+ if ("Err" in a)
409
+ throw Error(a.Err);
410
+ return a.Ok.length === 0 ? void 0 : await this.decryptFor(
411
+ e,
412
+ t,
413
+ n,
414
+ Uint8Array.from(a.Ok[0].inner)
415
+ );
416
+ }
417
+ /**
418
+ * Removes all values from a map.
419
+ *
420
+ * @param mapOwner - The principal of the map owner
421
+ * @param mapName - The name/identifier of the map
422
+ * @returns Promise resolving to an array of removed keys
423
+ * @throws Error if the operation fails
424
+ */
425
+ async removeMapValues(e, t) {
426
+ const n = await this.canisterClient.remove_map_values(
427
+ e,
428
+ c(t)
429
+ );
430
+ if ("Err" in n)
431
+ throw Error(n.Err);
432
+ return n.Ok.map(
433
+ (a) => Uint8Array.from(a.inner)
434
+ );
435
+ }
436
+ /**
437
+ * Retrieves the public verification key for validating encrypted VetKeys.
438
+ * The vetkeys obtained via `getVetkey` are verified using this key,
439
+ * and, therefore, this method is not needed for using `getVetkey`.
440
+ *
441
+ * @example
442
+ * ```ts
443
+ * const verificationKey = await encryptedMaps.getVetkeyVerificationKey();
444
+ * console.log("Verification Key:", verificationKey);
445
+ * ```
446
+ *
447
+ * @returns Promise resolving to the verification key bytes
448
+ */
449
+ async getVetkeyVerificationKey() {
450
+ if (!this.verificationKey) {
451
+ const e = await this.canisterClient.get_vetkey_verification_key();
452
+ this.verificationKey = Uint8Array.from(e.inner);
453
+ }
454
+ return this.verificationKey;
455
+ }
456
+ /**
457
+ * Grants or modifies access rights for a user.
458
+ *
459
+ * @example
460
+ * ```ts
461
+ * const owner = Principal.fromText("aaaaa-aa");
462
+ * const user = Principal.fromText("bbbbbb-bb");
463
+ * const accessRights = { ReadWrite: null };
464
+ *
465
+ * const result = await encryptedMaps.setUserRights(
466
+ * owner,
467
+ * mapName,
468
+ * user,
469
+ * accessRights,
470
+ * );
471
+ * console.log("Access Rights Updated:", result);
472
+ * ```
473
+ *
474
+ * @param owner - The principal of the map owner
475
+ * @param mapName - The name/identifier of the map
476
+ * @param user - The principal of the user to grant/modify rights for
477
+ * @param userRights - The access rights to grant
478
+ * @returns Promise resolving to the previous access rights if they existed
479
+ * @throws Error if the operation fails
480
+ */
481
+ async setUserRights(e, t, n, a) {
482
+ const i = await this.canisterClient.set_user_rights(
483
+ e,
484
+ c(t),
485
+ n,
486
+ a
487
+ );
488
+ if ("Err" in i) throw Error(i.Err);
489
+ if (i.Ok.length > 1)
490
+ throw Error("Unexpected result from set_user_rights");
491
+ return i.Ok.length === 0 ? void 0 : i.Ok[0];
492
+ }
493
+ /**
494
+ * Checks a user's access rights.
495
+ *
496
+ * @example
497
+ * ```ts
498
+ * const userRights = await encryptedMaps.getUserRights(owner, mapName, user);
499
+ * console.log("User Access Rights:", userRights);
500
+ * ```
501
+ *
502
+ * @param owner - The principal of the map owner
503
+ * @param mapName - The name/identifier of the map
504
+ * @param user - The principal of the user to check rights for
505
+ * @returns Promise resolving to the user's access rights if they exist
506
+ * @throws Error if the operation fails
507
+ */
508
+ async getUserRights(e, t, n) {
509
+ const a = await this.canisterClient.get_user_rights(
510
+ e,
511
+ c(t),
512
+ n
513
+ );
514
+ if ("Err" in a) throw Error(a.Err);
515
+ if (a.Ok.length > 1)
516
+ throw Error("Unexpected result from set_user_rights");
517
+ return a.Ok.length === 0 ? void 0 : a.Ok[0];
518
+ }
519
+ /**
520
+ * Gets all users that have access to a map and their access rights.
521
+ *
522
+ * @param owner - The principal of the map owner
523
+ * @param mapName - The name/identifier of the map
524
+ * @returns Promise resolving to an array of user-access rights pairs
525
+ * @throws Error if the operation fails
526
+ */
527
+ async getSharedUserAccessForMap(e, t) {
528
+ const n = await this.canisterClient.get_shared_user_access_for_map(
529
+ e,
530
+ c(t)
531
+ );
532
+ if ("Err" in n)
533
+ throw Error(n.Err);
534
+ return n.Ok;
535
+ }
536
+ /**
537
+ * Revokes a user's access.
538
+ *
539
+ * @example
540
+ * ```ts
541
+ * const removalResult = await encryptedMaps.removeUser(owner, mapName, user);
542
+ * console.log("User Removed:", removalResult);
543
+ * ```
544
+ *
545
+ * @param owner - The principal of the map owner
546
+ * @param mapName - The name/identifier of the map
547
+ * @param user - The principal of the user to remove
548
+ * @returns Promise resolving to the previous access rights if they existed
549
+ * @throws Error if the operation fails
550
+ */
551
+ async removeUser(e, t, n) {
552
+ const a = await this.canisterClient.remove_user(
553
+ e,
554
+ c(t),
555
+ n
556
+ );
557
+ if ("Err" in a)
558
+ throw Error(a.Err);
559
+ return a.Ok.length === 0 ? void 0 : a.Ok[0];
560
+ }
561
+ /**
562
+ * Derives a key material for a specific map.
563
+ *
564
+ * @param mapOwner - The principal of the map owner
565
+ * @param mapName - The name/identifier of the map
566
+ * @returns Promise resolving to the derived key material
567
+ * @throws Error if the operation fails
568
+ */
569
+ async getDerivedKeyMaterial(e, t) {
570
+ const n = h.random(), a = await this.canisterClient.get_encrypted_vetkey(
571
+ e,
572
+ c(t),
573
+ c(n.publicKeyBytes())
574
+ );
575
+ if ("Err" in a)
576
+ throw Error(a.Err);
577
+ {
578
+ const i = Uint8Array.from(a.Ok.inner), s = await this.getVetkeyVerificationKey(), l = new Uint8Array([
579
+ e.toUint8Array().length,
580
+ ...e.toUint8Array(),
581
+ ...t
582
+ ]), o = f.deserialize(i), _ = g.deserialize(s);
583
+ return await o.decryptAndVerify(
584
+ n,
585
+ _,
586
+ l
587
+ ).asDerivedKeyMaterial();
588
+ }
589
+ }
590
+ /**
591
+ * Encrypts a value for a specific map and key.
592
+ *
593
+ * @param mapOwner - The principal of the map owner
594
+ * @param mapName - The name/identifier of the map
595
+ * @param mapKey - The key to encrypt for
596
+ * @param cleartext - The value to encrypt
597
+ * @returns Promise resolving to the encrypted value
598
+ */
599
+ async encryptFor(e, t, n, a) {
600
+ return await (await this.getDerivedKeyMaterialOrFetchIfNeeded(e, t)).encryptMessage(a, n, "");
601
+ }
602
+ /**
603
+ * Decrypts a value for a specific map and key.
604
+ *
605
+ * @param mapOwner - The principal of the map owner
606
+ * @param mapName - The name/identifier of the map
607
+ * @param mapKey - The key to decrypt for
608
+ * @param encryptedValue - The value to decrypt
609
+ * @returns Promise resolving to the decrypted value
610
+ */
611
+ async decryptFor(e, t, n, a) {
612
+ return await (await this.getDerivedKeyMaterialOrFetchIfNeeded(e, t)).decryptMessage(
613
+ a,
614
+ n,
615
+ ""
616
+ );
617
+ }
618
+ /**
619
+ * Gets or fetches the derived key material for a map.
620
+ *
621
+ * @param mapOwner - The principal of the map owner
622
+ * @param mapName - The name/identifier of the map
623
+ * @returns Promise resolving to the derived key material
624
+ */
625
+ async getDerivedKeyMaterialOrFetchIfNeeded(e, t) {
626
+ const n = new Uint8Array(t), a = await k([
627
+ e.toString(),
628
+ n
629
+ ]);
630
+ if (a)
631
+ return await v.fromCryptoKey(
632
+ a
633
+ );
634
+ const i = await this.getDerivedKeyMaterial(
635
+ e,
636
+ t
637
+ );
638
+ return await E(
639
+ [e.toString(), n],
640
+ i.getCryptoKey()
641
+ ), i;
642
+ }
643
+ }
644
+ function c(r) {
645
+ return { inner: r };
646
+ }
647
+ export {
648
+ U as DefaultEncryptedMapsClient,
649
+ F as EncryptedMaps
650
+ };