Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for creation of forest from key #8

Merged
merged 1 commit into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dep/wnfsutils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ serde = "1.0.149"
serde_json = "1.0.89"
anyhow = "1.0.66"
async-trait = "0.1.58"
log = "0.4.14"
log = "0.4.14"
sha3 = "0.10"
62 changes: 48 additions & 14 deletions dep/wnfsutils/src/private_forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ use std::{
io::{Read, Write}
};
use wnfs::{
dagcbor,
private::{PrivateForest, PrivateRef},
dagcbor, Hasher, utils,
private::{PrivateForest, PrivateRef, PrivateNode, Key},
BlockStore, Namefilter, PrivateDirectory, PrivateOpResult, Metadata,
};
use anyhow::Result;
use log::{trace, Level};
use sha3::Sha3_256;


use crate::blockstore::FFIFriendlyBlockStore;
Expand All @@ -42,12 +43,8 @@ impl<'a> PrivateDirectoryHelper<'a> {
pub async fn create_private_forest(&mut self) -> Result<Cid> {
// Create the private forest (also HAMT), a map-like structure where files and directories are stored.
let forest = Rc::new(PrivateForest::new());

// Serialize the private forest to DAG CBOR.
let cbor_bytes = dagcbor::async_encode(&forest, &mut self.store).await.unwrap();

// Persist encoded private forest to the block store.
self.store.put_serializable(&cbor_bytes).await
self.update_forest(forest).await
}

pub async fn load_forest(&mut self, forest_cid: Cid) -> Result<Rc<PrivateForest>> {
Expand All @@ -61,9 +58,9 @@ impl<'a> PrivateDirectoryHelper<'a> {
Ok(Rc::new(dagcbor::decode::<PrivateForest>(cbor_bytes.as_ref()).unwrap()))
}

pub async fn update_forest(&mut self, hamt: Rc<PrivateForest>) -> Result<Cid> {
pub async fn update_forest(&mut self, forest: Rc<PrivateForest>) -> Result<Cid> {
// Serialize the private forest to DAG CBOR.
let cbor_bytes = dagcbor::async_encode(&hamt, &mut self.store).await.unwrap();
let cbor_bytes = dagcbor::async_encode(&forest, &mut self.store).await.unwrap();

// Persist encoded private forest to the block store.
self.store.put_serializable(&cbor_bytes).await
Expand All @@ -77,20 +74,57 @@ impl<'a> PrivateDirectoryHelper<'a> {
.unwrap().unwrap().as_dir()
}

pub async fn init(&mut self, forest: Rc<PrivateForest>) -> (Cid, PrivateRef) {
pub async fn init(&mut self, forest: Rc<PrivateForest>, wnfs_key: Vec<u8>) -> (Cid, PrivateRef) {
let ratchet_seed: [u8; 32];
if wnfs_key.is_empty() {
let wnfs_random_key = Key::new(utils::get_random_bytes::<32>(&mut self.rng));
ratchet_seed = Sha3_256::hash(&wnfs_random_key.as_bytes());
}else {
ratchet_seed = Sha3_256::hash(&wnfs_key);
}

let inumber: [u8; 32] = utils::get_random_bytes::<32>(&mut self.rng); // Needs to be random


//START TO RETRIEVE CURRENT FOREST
let private_ref = PrivateRef::with_seed(Default::default(), ratchet_seed, inumber);
let dir = forest
.get(
&private_ref,
PrivateForest::resolve_lowest,
&mut self.store
)
.await
.unwrap()
.unwrap()
.as_dir()
.unwrap();
//END OF LOAD CURRENT FOREST

/*
// Create a new directory.
let dir = Rc::new(PrivateDirectory::new(
let dir = Rc::new(PrivateDirectory::with_seed(
Namefilter::default(),
Utc::now(),
&mut self.rng,
ratchet_seed,
inumber
));
*/
//TODO: CHECK IF root exists in ls thenskip mkdir

let PrivateOpResult { root_dir, forest, .. } = dir
.mkdir(&["root".into()], true, Utc::now(), forest, &mut self.store,&mut self.rng)
.mkdir(
&["root".into()],
true,
Utc::now(),
forest,
&mut self.store,
&mut self.rng
)
.await
.unwrap();

(self.update_forest(forest).await.unwrap(), root_dir.header.get_private_ref())
(self.update_forest(forest).await.unwrap(), private_ref)
}

fn get_file_as_byte_vec(&mut self, filename: &String) -> Vec<u8> {
Expand Down