Skip to content

Commit

Permalink
feat: expose AccessKey encode/decode api (#296)
Browse files Browse the repository at this point in the history
* feat: expose AccessKey encode/decode api

* Format using newer formatter

---------

Co-authored-by: Philipp Krüger <philipp.krueger1@gmail.com>
  • Loading branch information
Gozala and matheus23 committed Jul 5, 2023
1 parent 25c5215 commit 982feff
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 12 deletions.
10 changes: 10 additions & 0 deletions wnfs-wasm/src/fs/private/access_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,14 @@ impl AccessKey {
pub fn get_content_cid(&self) -> Vec<u8> {
self.0.get_content_cid().to_bytes()
}

#[wasm_bindgen(js_name = "toBytes")]
pub fn into_bytes(&self) -> Vec<u8> {
Vec::<u8>::from(&self.0)
}

#[wasm_bindgen(js_name = "fromBytes")]
pub fn from_bytes(bytes: &[u8]) -> Self {
Self(WnfsAccessKey::from(bytes).into())
}
}
2 changes: 2 additions & 0 deletions wnfs-wasm/tests/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Namefilter,
PrivateDirectory,
PrivateForest,
AccessKey,
PublicDirectory,
} from "../pkg";

Expand Down Expand Up @@ -190,4 +191,5 @@ export {
createRecipientExchangeRoot,
PrivateKey,
ExchangeKey,
AccessKey,
};
33 changes: 33 additions & 0 deletions wnfs-wasm/tests/private.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,36 @@ test.describe("PrivateForest", () => {
expect(result).toBeDefined();
});
});

test.describe("AccessKey", () => {
test("can encode / decode an access key", async ({ page }) => {
const [metadataBefore, metadataAfter] = await page.evaluate(async () => {
const {
wnfs: { AccessKey, Namefilter, PrivateFile, PrivateNode, PrivateForest },
mock: { MemoryBlockStore, Rng },
} = await window.setup();


const rng = new Rng();
const store = new MemoryBlockStore();
const time = new Date();
const file = new PrivateFile(new Namefilter(), time, rng);
const node = file.asNode();
const forest = new PrivateForest();
const [accessKey, newForest] = await node.store(forest, store, rng);

const encodedAccessKey = accessKey.toBytes();
const decodedAccessKey = AccessKey.fromBytes(encodedAccessKey);

const fetched = await PrivateNode.load(decodedAccessKey, newForest, store);
const metadataBefore = node.asFile().metadata();
const metadataAfter = fetched.asFile().metadata();
return [metadataBefore, metadataAfter];
});

expect(metadataBefore).toBeDefined();
expect(metadataAfter).toBeDefined();
expect(metadataBefore.created).toEqual(metadataAfter.created);
expect(metadataBefore.modified).toEqual(metadataAfter.modified);
});
});
15 changes: 12 additions & 3 deletions wnfs/src/private/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,10 @@ impl PrivateDirectory {
return Ok(None);
};

let SearchResult::Found(dir) = self.get_leaf_dir(path, search_latest, forest, store).await? else {
let SearchResult::Found(dir) = self
.get_leaf_dir(path, search_latest, forest, store)
.await?
else {
return Ok(None);
};

Expand Down Expand Up @@ -1069,7 +1072,10 @@ impl PrivateDirectory {
store: &impl BlockStore,
) -> Result<PrivateNode> {
let (path, node_name) = crate::utils::split_last(path_segments)?;
let SearchResult::Found(dir) = self.get_leaf_dir_mut(path, search_latest, forest, store).await? else {
let SearchResult::Found(dir) = self
.get_leaf_dir_mut(path, search_latest, forest, store)
.await?
else {
bail!(FsError::NotFound)
};

Expand All @@ -1096,7 +1102,10 @@ impl PrivateDirectory {
rng: &mut impl RngCore,
) -> Result<()> {
let (path, node_name) = crate::utils::split_last(path_segments)?;
let SearchResult::Found(dir) = self.get_leaf_dir_mut(path, search_latest, forest, store).await? else {
let SearchResult::Found(dir) = self
.get_leaf_dir_mut(path, search_latest, forest, store)
.await?
else {
bail!(FsError::NotFound);
};

Expand Down
12 changes: 12 additions & 0 deletions wnfs/src/private/keys/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,15 @@ impl From<&PrivateRef> for SnapshotAccessKey {
}
}
}

impl From<&[u8]> for AccessKey {
fn from(bytes: &[u8]) -> Self {
serde_ipld_dagcbor::from_slice(bytes).unwrap()
}
}

impl From<&AccessKey> for Vec<u8> {
fn from(key: &AccessKey) -> Self {
serde_ipld_dagcbor::to_vec(key).unwrap()
}
}
15 changes: 7 additions & 8 deletions wnfs/src/private/previous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ impl PrivateNodeHistory {
&mut self,
store: &impl BlockStore,
) -> Result<Option<PrivateNode>> {
let Some(previous_ratchet) = self.ratchets.next()
else {
let Some(previous_ratchet) = self.ratchets.next() else {
return Ok(None);
};

Expand Down Expand Up @@ -144,7 +143,7 @@ impl PrivateNodeHistory {
.iter()
.find(|(revisions_back, _)| *revisions_back == 1)
else {
return Ok(None)
return Ok(None);
};
Ok(Some(*first_backpointer.resolve_value(&temporal_key)?))
}
Expand Down Expand Up @@ -289,7 +288,9 @@ impl PrivateNodeOnPathHistory {
PathNodesResult::NotADirectory(_, _) => bail!(FsError::NotADirectory),
};

let Some(target) = (*path_nodes.tail).lookup_node(target_path_segment, false, &forest, store).await?
let Some(target) = (*path_nodes.tail)
.lookup_node(target_path_segment, false, &forest, store)
.await?
else {
bail!(FsError::NotFound);
};
Expand Down Expand Up @@ -355,8 +356,7 @@ impl PrivateNodeOnPathHistory {
return Ok(Some(node));
}

let Some(working_stack) = self.find_and_step_segment_history(store).await?
else {
let Some(working_stack) = self.find_and_step_segment_history(store).await? else {
return Ok(None);
};

Expand Down Expand Up @@ -481,8 +481,7 @@ impl PrivateNodeOnPathHistory {
};

// We need to find the in-between history entry! See the test case `previous_with_multiple_child_changes`.
let Some(directory_prev) = directory_history.get_previous_dir(store).await?
else {
let Some(directory_prev) = directory_history.get_previous_dir(store).await? else {
return Ok(false);
};

Expand Down
2 changes: 1 addition & 1 deletion wnfs/src/public/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl PublicDirectory {
};

let SearchResult::Found(dir) = self.get_leaf_dir(path, store).await? else {
return Ok(None)
return Ok(None);
};

dir.lookup_node(tail, store).await
Expand Down

0 comments on commit 982feff

Please sign in to comment.