Skip to content

Commit

Permalink
feat: make AccessKey the key entrypoint for public apis (#285)
Browse files Browse the repository at this point in the history
* Make AccessKey the key entrypoint for public apis

* Remove todos

* Update wnfs/src/private/share.rs

Co-authored-by: Philipp Krüger <philipp.krueger1@gmail.com>
Signed-off-by: Stephen Akinyemi <appcypher@outlook.com>

* Refactor get_multivalue

---------

Signed-off-by: Stephen Akinyemi <appcypher@outlook.com>
Co-authored-by: Philipp Krüger <philipp.krueger1@gmail.com>
  • Loading branch information
appcypher and matheus23 committed Jun 26, 2023
1 parent 085242d commit b749e74
Show file tree
Hide file tree
Showing 25 changed files with 448 additions and 846 deletions.
31 changes: 31 additions & 0 deletions wnfs-wasm/src/fs/private/access_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use wasm_bindgen::prelude::wasm_bindgen;
use wnfs::private::AccessKey as WnfsAccessKey;

//--------------------------------------------------------------------------------------------------
// Type Definitions
//--------------------------------------------------------------------------------------------------

#[wasm_bindgen]
pub struct AccessKey(pub(crate) WnfsAccessKey);

//--------------------------------------------------------------------------------------------------
// Implementations
//--------------------------------------------------------------------------------------------------

#[wasm_bindgen]
impl AccessKey {
#[wasm_bindgen(js_name = "getLabel")]
pub fn get_label(&self) -> Vec<u8> {
self.0.get_label().to_vec()
}

#[wasm_bindgen(js_name = "getTemporalKey")]
pub fn get_temporal_key(&self) -> Vec<u8> {
self.0.get_temporal_key().unwrap().0.as_bytes().to_vec()
}

#[wasm_bindgen(js_name = "getContentCid")]
pub fn get_content_cid(&self) -> Vec<u8> {
self.0.get_content_cid().to_bytes()
}
}
4 changes: 2 additions & 2 deletions wnfs-wasm/src/fs/private/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
mod access_key;
mod directory;
mod exchange_key;
mod file;
mod forest;
mod namefilter;
mod node;
mod privateref;
mod rng;
mod share;

pub use access_key::*;
pub use directory::*;
pub use exchange_key::*;
pub use file::*;
pub use forest::*;
pub use namefilter::*;
pub use node::*;
pub use privateref::*;
pub use rng::*;
pub use share::*;
18 changes: 9 additions & 9 deletions wnfs-wasm/src/fs/private/node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::AccessKey;
use crate::{
fs::{
private::{PrivateDirectory, PrivateFile, PrivateForest, PrivateRef},
private::{PrivateDirectory, PrivateFile, PrivateForest},
utils::{self, error},
BlockStore, ForeignBlockStore, JsResult, Namefilter, Rng,
},
Expand Down Expand Up @@ -44,34 +45,33 @@ impl PrivateNode {
mut rng: Rng,
) -> JsResult<Promise> {
let node = self.0.clone(); // cheap clone
let mut store = ForeignBlockStore(store);
let store = ForeignBlockStore(store);
let mut forest = Rc::clone(&forest.0);

Ok(future_to_promise(async move {
let private_ref = node
.store(&mut forest, &mut store, &mut rng)
let access_key = node
.store(&mut forest, &store, &mut rng)
.await
.map_err(error("Cannot store node"))?;

Ok(utils::create_private_forest_result(
value!(PrivateRef::from(private_ref)),
value!(AccessKey(access_key)),
forest,
)?)
}))
}

/// Loads a node from the PrivateForest using the PrivateRef.
/// Loads a node from the PrivateForest using the AccessKey.
pub fn load(
private_ref: PrivateRef,
access_key: AccessKey,
forest: &PrivateForest,
store: BlockStore,
) -> JsResult<Promise> {
let store = ForeignBlockStore(store);
let forest = Rc::clone(&forest.0);
let private_ref = private_ref.try_into()?;

Ok(future_to_promise(async move {
let node = WnfsPrivateNode::load(&private_ref, &forest, &store)
let node = WnfsPrivateNode::load(&access_key.0, &forest, &store)
.await
.map_err(error("Cannot load node"))?;

Expand Down
87 changes: 0 additions & 87 deletions wnfs-wasm/src/fs/private/privateref.rs

This file was deleted.

90 changes: 20 additions & 70 deletions wnfs-wasm/src/fs/private/share.rs
Original file line number Diff line number Diff line change
@@ -1,97 +1,48 @@
use super::{ForeignExchangeKey, ForeignPrivateKey, Namefilter, PrivateForest, PrivateNode};
use super::{
AccessKey, ForeignExchangeKey, ForeignPrivateKey, Namefilter, PrivateForest, PrivateNode,
};
use crate::{
fs::{utils::error, BlockStore, ForeignBlockStore, JsResult, PrivateKey, Rng},
fs::{utils::error, BlockStore, ForeignBlockStore, JsResult, PrivateKey},
value,
};
use js_sys::{Array, Promise, Reflect};
use js_sys::Promise;
use libipld_core::cid::Cid;
use std::rc::Rc;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen_futures::future_to_promise;
use wnfs::{
private::share::{recipient, sharer, SharePayload as WnfsSharePayload},
private::share::{recipient, sharer},
public::PublicLink,
};

//--------------------------------------------------------------------------------------------------
// Type Definitions
//--------------------------------------------------------------------------------------------------

#[wasm_bindgen]
pub struct SharePayload(pub(crate) WnfsSharePayload);

//--------------------------------------------------------------------------------------------------
// Implementations
//--------------------------------------------------------------------------------------------------

#[wasm_bindgen]
impl SharePayload {
#[wasm_bindgen(js_name = "fromNode")]
pub fn from_node(
node: PrivateNode,
temporal: bool,
forest: &PrivateForest,
store: BlockStore,
mut rng: Rng,
) -> JsResult<Promise> {
let mut store = ForeignBlockStore(store);
let mut forest = Rc::clone(&forest.0);

Ok(future_to_promise(async move {
let payload =
WnfsSharePayload::from_node(&node.0, temporal, &mut forest, &mut store, &mut rng)
.await
.map_err(error("Cannot create share payload"))?;

let result = Array::new();

Reflect::set(&result, &value!(0), &SharePayload(payload).into())
.map_err(error("Failed to set file"))?;

Reflect::set(&result, &value!(1), &PrivateForest(forest).into())
.map_err(error("Failed to set forest"))?;

Ok(value!(result))
}))
}

#[wasm_bindgen(js_name = "getLabel")]
pub fn get_label(&self) -> Vec<u8> {
self.0.get_label().to_vec()
}
}

//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------

#[wasm_bindgen]
pub fn share(
share_payload: SharePayload,
access_key: AccessKey,
share_count: u32,
sharer_root_did: String,
sharer_forest: &PrivateForest,
sharer_store: BlockStore,
recipient_exchange_root: Vec<u8>,
recipient_store: BlockStore,
store: BlockStore,
) -> JsResult<Promise> {
let mut sharer_store = ForeignBlockStore(sharer_store);
let mut sharer_forest = Rc::clone(&sharer_forest.0);
let recipient_store = ForeignBlockStore(recipient_store);
let cid = Cid::try_from(&recipient_exchange_root[..]).map_err(error("Invalid CID"))?;
let store = ForeignBlockStore(store);

Ok(future_to_promise(async move {
sharer::share::<ForeignExchangeKey>(
&share_payload.0,
&access_key.0,
share_count.into(),
&sharer_root_did,
&mut sharer_forest,
&mut sharer_store,
PublicLink::from_cid(cid),
&recipient_store,
&store,
)
.await
.map_err(error("Cannot share payload"))?;
.map_err(error("Cannot share item"))?;

Ok(value!(PrivateForest(sharer_forest)))
}))
Expand All @@ -117,9 +68,9 @@ pub fn find_latest_share_counter(
recipient_exchange_key: Vec<u8>,
sharer_root_did: String,
sharer_forest: &PrivateForest,
sharer_store: BlockStore,
store: BlockStore,
) -> JsResult<Promise> {
let sharer_store = ForeignBlockStore(sharer_store);
let store = ForeignBlockStore(store);
let sharer_forest = Rc::clone(&sharer_forest.0);

Ok(future_to_promise(async move {
Expand All @@ -129,7 +80,7 @@ pub fn find_latest_share_counter(
&recipient_exchange_key,
&sharer_root_did,
&sharer_forest,
&sharer_store,
&store,
)
.await
.map_err(error("Cannot find share"))?;
Expand All @@ -143,17 +94,16 @@ pub fn receive_share(
share_label: Namefilter,
recipient_key: PrivateKey,
sharer_forest: &PrivateForest,
sharer_store: BlockStore,
store: BlockStore,
) -> JsResult<Promise> {
let sharer_store = ForeignBlockStore(sharer_store);
let store = ForeignBlockStore(store);
let recipient_key = ForeignPrivateKey(recipient_key);
let sharer_forest = Rc::clone(&sharer_forest.0);

Ok(future_to_promise(async move {
let node =
recipient::receive_share(share_label.0, &recipient_key, &sharer_forest, &sharer_store)
.await
.map_err(error("Cannot receive share"))?;
let node = recipient::receive_share(share_label.0, &recipient_key, &sharer_forest, &store)
.await
.map_err(error("Cannot receive share"))?;

Ok(value!(PrivateNode(node)))
}))
Expand Down
3 changes: 1 addition & 2 deletions wnfs-wasm/tests/server/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ declare global {
PrivateForest: typeof import("../../pkg/index").PrivateForest;
PrivateFile: typeof import("../../pkg/index").PrivateFile;
PrivateNode: typeof import("../../pkg/index").PrivateNode;
PrivateRef: typeof import("../../pkg/index").PrivateRef;
AccessKey: typeof import("../../pkg/index").AccessKey;
Namefilter: typeof import("../../pkg/index").Namefilter;
SharePayload: typeof import("../../pkg/index").SharePayload;
share: typeof import("../../pkg/index").share;
findLatestShareCounter: typeof import("../../pkg/index").findLatestShareCounter;
receiveShare: typeof import("../../pkg/index").receiveShare;
Expand Down
6 changes: 2 additions & 4 deletions wnfs-wasm/tests/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ const setup = async () => {
PrivateForest,
PrivateFile,
PrivateNode,
PrivateRef,
AccessKey,
Namefilter,
setPanicHook,
SharePayload,
share,
createShareLabel,
receiveShare,
Expand All @@ -49,9 +48,8 @@ const setup = async () => {
PrivateForest,
PrivateFile,
PrivateNode,
PrivateRef,
AccessKey,
Namefilter,
SharePayload,
share,
createShareLabel,
receiveShare,
Expand Down
Loading

0 comments on commit b749e74

Please sign in to comment.