-
Notifications
You must be signed in to change notification settings - Fork 25
/
private.rs
73 lines (58 loc) · 2.38 KB
/
private.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! This example shows how to add a directory to a private forest (a HAMT) where encrypted ciphertexts are stored.
//! It also shows how to retrieve encrypted nodes from the forest using `AccessKey`s.
use anyhow::Result;
use chrono::Utc;
use libipld_core::cid::Cid;
use rand_chacha::ChaCha12Rng;
use rand_core::{CryptoRngCore, SeedableRng};
use wnfs::{
common::{BlockStore, MemoryBlockStore},
nameaccumulator::AccumulatorSetup,
private::{
forest::{hamt::HamtForest, traits::PrivateForest},
AccessKey, PrivateDirectory, PrivateNode,
},
};
use wnfs_common::{utils::CondSend, Storable};
#[async_std::main]
async fn main() -> Result<()> {
// Create an in-memory block store.
let store = &MemoryBlockStore::default();
// Create a random number generator the private filesystem can use.
let rng = &mut ChaCha12Rng::from_entropy();
// Create a new private forest and get the cid to it.
let (forest_cid, access_key) = create_forest_and_add_directory(store, rng).await?;
// Deserialize private forest from the blockstore.
let forest = HamtForest::load(&forest_cid, store).await?;
// Fetch and decrypt a directory from the private forest using provided private ref.
let dir = PrivateNode::load(&access_key, &forest, store, None).await?;
// Print the directory.
println!("{:#?}", dir);
Ok(())
}
async fn create_forest_and_add_directory(
store: &impl BlockStore,
rng: &mut (impl CryptoRngCore + CondSend),
) -> Result<(Cid, AccessKey)> {
// Do a trusted setup for WNFS' name accumulators
let setup = AccumulatorSetup::trusted(rng);
// Create the private forest (a HAMT), a map-like structure where file and directory ciphertexts are stored.
let forest = &mut HamtForest::new_rc(setup);
// Create a new directory.
let dir = &mut PrivateDirectory::new_rc(&forest.empty_name(), Utc::now(), rng);
// Add a /pictures/cats subdirectory.
dir.mkdir(
&["pictures".into(), "cats".into()],
true,
Utc::now(),
forest,
store,
rng,
)
.await?;
// Access key contains the materials for fetching and decrypting the directory node in the private forest.
let access_key = dir.as_node().store(forest, store, rng).await?;
// Persist encoded private forest to the block store.
let forest_cid = forest.store(store).await?;
Ok((forest_cid, access_key))
}