Skip to content

Commit 2095b16

Browse files
authored
docs: CRP-2757 add code docs in Motoko (#136)
1 parent db21f4e commit 2095b16

6 files changed

Lines changed: 168 additions & 84 deletions

File tree

backend/mo/ic_vetkeys/README.md

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
# ic-vetkeys
1+
# Internet Computer (IC) vetKeys
22

3-
# TODO
3+
This crate contains a set of tools designed to help canister developers integrate **vetKeys** into their Internet Computer (ICP) applications.
44

5-
## [Encrypted Maps](./src/encrypted_maps/README.md)
5+
## [Key Manager](https://mops.one/ic_vetkeys/docs/key_manager/KeyManager.html)
6+
A canister library for derivation of encrypted vetkeys from arbitrary strings. It can be used in combination with the [frontend key manager library](https://dfinity.github.io/vetkeys/classes/_dfinity_vetkeys_key_manager.KeyManager.html).
67

7-
## [Key Manager](./src/key_manager/README.md)
8+
## [Encrypted Maps](https://mops.one/ic_vetkeys/docs/encrypted_maps/EncryptedMaps.html)
9+
An efficient canister library facilitating access control and encrypted storage for a collection of maps contatining key-value pairs. It can be used in combination with the [frontend encrypted maps library](https://dfinity.github.io/vetkeys/classes/_dfinity_vetkeys_encrypted_maps.EncryptedMaps.html).
810

9-
# Install
10-
```
11-
mops add ic-vetkeys
12-
```
13-
14-
# Usage
15-
```motoko
16-
import IcVetkeys "mo:ic-vetkeys";
17-
18-
// example...
19-
```
11+
## Cross-language library
12+
If Rust better suits your needs, take a look at the [Rust equivalent of this library](https://docs.rs/ic_vetkeys).

backend/mo/ic_vetkeys/src/encrypted_maps/EncryptedMaps.mo

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1+
/// The **EncryptedMaps** backend is a support library built on top of `KeyManager`.
2+
///
3+
/// **EncryptedMaps** is designed to facilitate secure, encrypted data sharing between users on the Internet Computer (ICP) using the **vetKeys** feature. It allows developers to store encrypted key-value pairs (**maps**) securely and to manage fine-grained user access.
4+
///
5+
/// For an introduction to **vetKeys**, refer to the [vetKeys Overview](https://internetcomputer.org/docs/building-apps/network-features/encryption/vetkeys).
6+
///
7+
/// ## Core Features
8+
///
9+
/// The **EncryptedMaps** library provides the following key functionalities:
10+
///
11+
/// - **Encrypted Key-Value Storage:** Securely store and manage encrypted key-value pairs within named maps.
12+
/// - **User-Specific Map Access:** Control precisely which users can read or modify entries in an encrypted map.
13+
/// - **Integrated Access Control:** Leverages the **KeyManager** library to manage and enforce user permissions.
14+
/// - **Stable Storage:** Utilizes **OrderedMap** for reliable, persistent storage across canister upgrades.
15+
///
16+
/// ## EncryptedMaps Architecture
17+
///
18+
/// The **EncryptedMaps** library contains:
19+
///
20+
/// - **Encrypted Values Storage:** Maps `(KeyId, MapKey)` to `EncryptedMapValue`, securely storing encrypted data.
21+
/// - **KeyManager Integration:** Uses **KeyManager** to handle user permissions, ensuring authorized access to maps.
22+
///
23+
/// ## Example Use Case
24+
///
25+
/// 1. **User A** initializes an encrypted map and adds values.
26+
/// 2. **User A** shares access to this map with **User B**.
27+
/// 3. **User B** retrieves encrypted values securely.
28+
/// 4. **User A** revokes **User B**'s access as necessary.
29+
///
30+
/// ## Security Considerations
31+
///
32+
/// - Encrypted values are stored securely with fine-grained access control.
33+
/// - Access rights and permissions are strictly enforced.
34+
/// - Data persists securely across canister upgrades through stable storage.
35+
///
36+
/// ## Summary
37+
/// **EncryptedMaps** simplifies secure storage, retrieval, and controlled sharing of encrypted data on the Internet Computer, complementing the robust security and permissions management provided by **KeyManager**.
38+
139
import Principal "mo:base/Principal";
240
import Blob "mo:base/Blob";
341
import Buffer "mo:base/Buffer";
@@ -12,12 +50,22 @@ import Text "mo:base/Text";
1250
import KeyManager "../key_manager/KeyManager";
1351

1452
module {
53+
/// The caller requesting access to encrypted maps, represented as a Principal.
1554
public type Caller = Principal;
55+
56+
/// The name of an encrypted map, used as part of the map identifier.
1657
public type MapName = KeyManager.KeyName;
58+
59+
/// A unique identifier for an encrypted map, consisting of the owner and map name.
1760
public type MapId = KeyManager.KeyId;
61+
62+
/// A key within an encrypted map, used to identify specific values.
1863
public type MapKey = Blob;
64+
65+
/// An encrypted value stored within an encrypted map.
1966
public type EncryptedMapValue = Blob;
2067

68+
/// Represents the complete data for an encrypted map, including ownership, contents, and access control.
2169
public type EncryptedMapData<T> = {
2270
map_owner : Principal;
2371
map_name : MapName;
@@ -54,22 +102,27 @@ module {
54102
return OrderedMap.Make<MapId>(compareMapIds);
55103
};
56104

105+
/// See the module documentation for more information.
57106
public class EncryptedMaps<T>(key_id : { curve : { #bls12_381_g2 }; name : Text }, domainSeparator : Text, accessRightsOperations : Types.AccessControlOperations<T>) {
58107
public var keyManager = KeyManager.KeyManager<T>(key_id, domainSeparator, accessRightsOperations);
59108
public var mapKeyVals : OrderedMap.Map<(MapId, MapKey), EncryptedMapValue> = mapKeyValsMapOps().empty();
60109
public var mapKeys : OrderedMap.Map<MapId, [MapKey]> = mapKeysMapOps().empty();
61110

62-
// Get accessible shared map names for a caller
111+
/// Lists all map names shared with the caller.
112+
/// Returns a vector of map IDs that the caller has access to.
63113
public func getAccessibleSharedMapNames(caller : Caller) : [MapId] {
64114
keyManager.getAccessibleSharedKeyIds(caller);
65115
};
66116

67-
// Get shared user access for a map
117+
/// Retrieves all users and their access rights for a specific map.
118+
/// The caller must have appropriate permissions to view this information.
68119
public func getSharedUserAccessForMap(caller : Caller, mapId : MapId) : Result.Result<[(Caller, T)], Text> {
69120
keyManager.getSharedUserAccessForKey(caller, mapId);
70121
};
71122

72-
// Remove all values from a map
123+
/// Removes all values from a map if the caller has sufficient rights.
124+
/// Returns the removed keys.
125+
/// The caller must have write permissions to perform this operation.
73126
public func removeMapValues(caller : Caller, mapId : MapId) : Result.Result<[MapKey], Text> {
74127
switch (keyManager.getUserRights(caller, mapId, caller)) {
75128
case (#err(msg)) { #err(msg) };
@@ -96,7 +149,8 @@ module {
96149
};
97150
};
98151

99-
// Get encrypted values for a map
152+
/// Retrieves all encrypted key-value pairs from a map.
153+
/// The caller must have read permissions to access the map values.
100154
public func getEncryptedValuesForMap(caller : Caller, mapId : MapId) : Result.Result<[(MapKey, EncryptedMapValue)], Text> {
101155
switch (keyManager.getUserRights(caller, mapId, caller)) {
102156
case (#err(msg)) { #err(msg) };
@@ -119,7 +173,8 @@ module {
119173
};
120174
};
121175

122-
// Get encrypted value
176+
/// Retrieves a specific encrypted value from a map.
177+
/// The caller must have read permissions to access the value.
123178
public func getEncryptedValue(caller : Caller, mapId : MapId, key : MapKey) : Result.Result<?EncryptedMapValue, Text> {
124179
switch (keyManager.ensureUserCanRead(caller, mapId)) {
125180
case (#err(msg)) { #err(msg) };
@@ -129,7 +184,7 @@ module {
129184
};
130185
};
131186

132-
// Get all accessible encrypted values
187+
/// Retrieves the non-empty map names owned by the caller.
133188
public func getAllAccessibleEncryptedValues(caller : Caller) : [(MapId, [(MapKey, EncryptedMapValue)])] {
134189
let result = Buffer.Buffer<(MapId, [(MapKey, EncryptedMapValue)])>(0);
135190
for (mapId in getAccessibleMapIdsIter(caller)) {
@@ -145,7 +200,7 @@ module {
145200
Buffer.toArray(result);
146201
};
147202

148-
// Get all accessible encrypted maps
203+
/// Retrieves all accessible encrypted maps and their data for the caller.
149204
public func getAllAccessibleEncryptedMaps(caller : Caller) : [EncryptedMapData<T>] {
150205
let result = Buffer.Buffer<EncryptedMapData<T>>(0);
151206
for (mapId in getAccessibleMapIdsIter(caller)) {
@@ -174,7 +229,8 @@ module {
174229
Buffer.toArray(result);
175230
};
176231

177-
// Get owned non-empty map names
232+
/// Retrieves the non-empty map names owned by the caller.
233+
/// Returns a list of map names that contain at least one key-value pair.
178234
public func getOwnedNonEmptyMapNames(caller : Caller) : [MapName] {
179235
let mapNames = Buffer.Buffer<MapName>(0);
180236
for ((mapId, _) in mapKeysMapOps().entries(mapKeys)) {
@@ -185,7 +241,8 @@ module {
185241
Buffer.toArray(mapNames);
186242
};
187243

188-
// Insert encrypted value
244+
/// Inserts or updates an encrypted value in a map.
245+
/// The caller must have write permissions to modify the map.
189246
public func insertEncryptedValue(
190247
caller : Caller,
191248
mapId : MapId,
@@ -212,7 +269,8 @@ module {
212269
};
213270
};
214271

215-
// Remove encrypted value
272+
/// Removes an encrypted value from a map.
273+
/// The caller must have write permissions to modify the map.
216274
public func removeEncryptedValue(
217275
caller : Caller,
218276
mapId : MapId,
@@ -241,12 +299,14 @@ module {
241299
};
242300
};
243301

244-
// Get vetkey verification key
302+
/// Retrieves the public verification key from KeyManager.
303+
/// This key is used to verify the authenticity of derived keys.
245304
public func getVetkeyVerificationKey() : async KeyManager.VetKeyVerificationKey {
246305
await keyManager.getVetkeyVerificationKey();
247306
};
248307

249-
// Get encrypted vetkey
308+
/// Retrieves an encrypted vetkey for caller and key id.
309+
/// The key is secured using the provided transport key and can only be accessed by authorized users.
250310
public func getEncryptedVetkey(
251311
caller : Caller,
252312
mapId : MapId,
@@ -255,12 +315,14 @@ module {
255315
await keyManager.getEncryptedVetkey(caller, mapId, transportKey);
256316
};
257317

258-
// Get user rights
318+
/// Retrieves access rights for a user to a map.
319+
/// The caller must have appropriate permissions to view this information.
259320
public func getUserRights(caller : Caller, mapId : MapId, user : Principal) : Result.Result<?T, Text> {
260321
keyManager.getUserRights(caller, mapId, user);
261322
};
262323

263-
// Set user rights
324+
/// Sets or updates access rights for a user to a map.
325+
/// Only the map owner or a user with management rights can perform this action.
264326
public func setUserRights(
265327
caller : Caller,
266328
mapId : MapId,
@@ -270,7 +332,8 @@ module {
270332
keyManager.setUserRights(caller, mapId, user, accessRights);
271333
};
272334

273-
// Remove user
335+
/// Removes access rights for a user from a map.
336+
/// Only the map owner or a user with management rights can perform this action.
274337
public func removeUser(caller : Caller, mapId : MapId, user : Principal) : Result.Result<?T, Text> {
275338
keyManager.removeUserRights(caller, mapId, user);
276339
};

backend/mo/ic_vetkeys/src/encrypted_maps/README.md

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)