-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement storing externally encrypted content in
Metadata
(#340
) * feat: Implement storing externally encrypted content in `Metadata` * Add `file_variants` to example README.md * Remove debug println * feat: Also implement `get_metadata_mut`(-`_rc`) and `open_file_mut` on the public side * refactor: Implement and use `as_metadata_value`
- Loading branch information
Showing
10 changed files
with
510 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,7 @@ | |
```bash | ||
cargo run --example mnemonic_based --release | ||
``` | ||
|
||
```bash | ||
cargo run --example file_variants | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//! This example shows how to store multiple byte arrays per file, by storing | ||
//! additional data in a file's metadata, which links out to externally encrypted data. | ||
use anyhow::Result; | ||
use chrono::Utc; | ||
use rand::thread_rng; | ||
use std::rc::Rc; | ||
use wnfs::private::{ | ||
forest::{hamt::HamtForest, traits::PrivateForest}, | ||
PrivateFile, PrivateForestContent, | ||
}; | ||
use wnfs_common::MemoryBlockStore; | ||
|
||
#[async_std::main] | ||
async fn main() -> Result<()> { | ||
// The usual in-memory testing setup for WNFS | ||
let store = &MemoryBlockStore::default(); | ||
let rng = &mut thread_rng(); | ||
let forest = &mut HamtForest::new_rsa_2048(rng); | ||
|
||
// Create a new file (detached from any directory) | ||
let mut file = Rc::new( | ||
PrivateFile::with_content( | ||
&forest.empty_name(), | ||
Utc::now(), | ||
b"main content".to_vec(), | ||
forest, | ||
store, | ||
rng, | ||
) | ||
.await?, | ||
); | ||
|
||
// Create some content that's stored encrypted in the private forest. | ||
// The PrivateForestContent struct holds the keys and pointers to look it back up. | ||
// We use the file's name as the "path" for this content. This means anyone | ||
// who had write access to the file will have write access to the external content. | ||
let content = PrivateForestContent::new( | ||
file.header.get_name(), | ||
b"secondary content".to_vec(), | ||
forest, | ||
store, | ||
rng, | ||
) | ||
.await?; | ||
|
||
// We store the content in the file metadata. | ||
// This will update the `file: Rc<PrivateFile>` for us with a new reference. | ||
file.get_metadata_mut_rc()? | ||
.put("thumbnail", content.as_metadata_value()?); | ||
|
||
// We store the new reference in the forest. | ||
file.as_node().store(forest, store, rng).await?; | ||
|
||
// When can look up the private forest content again. | ||
let content_ipld = file.get_metadata().get("thumbnail").unwrap(); | ||
let content = PrivateForestContent::from_metadata_value(content_ipld)?; | ||
|
||
assert_eq!( | ||
content.get_content(forest, store).await?, | ||
b"secondary content".to_vec() | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.