Skip to content

Commit

Permalink
feat: Expose wasm bindings for Name ser/de
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Dec 15, 2023
1 parent 0964ac4 commit 9bca7a1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
34 changes: 34 additions & 0 deletions wnfs-wasm/src/fs/private/name.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::fs::{utils::error, JsResult, PrivateForest};
use std::rc::Rc;
use wasm_bindgen::prelude::wasm_bindgen;
use wnfs::private::forest::traits::PrivateForest as WnfsPrivateForest;
use wnfs_nameaccumulator::{Name as WnfsName, NameAccumulator as WnfsNameAccumulator};

//--------------------------------------------------------------------------------------------------
Expand All @@ -14,3 +17,34 @@ pub struct NameAccumulator(pub(crate) WnfsNameAccumulator);
//--------------------------------------------------------------------------------------------------
// Implementations
//--------------------------------------------------------------------------------------------------

#[wasm_bindgen]
impl Name {
#[wasm_bindgen(constructor)]
pub fn new(accumulator: &NameAccumulator) -> Name {
Self(WnfsName::new(accumulator.0.clone(), []))
}

#[wasm_bindgen(js_name = "toNameAccumulator")]
pub fn to_name_accumulator(&self, forest: &PrivateForest) -> NameAccumulator {
let name = &self.0;
let forest = Rc::clone(&forest.0);
let accumulator = name.into_accumulator(&forest.get_accumulator_setup());
return NameAccumulator(accumulator);
}
}

#[wasm_bindgen]
impl NameAccumulator {
#[wasm_bindgen(js_name = "fromBytes")]
pub fn from_bytes(bytes: Vec<u8>) -> JsResult<NameAccumulator> {
let accumulator = WnfsNameAccumulator::parse_bytes(bytes)
.map_err(error("Couldn't parse name accumulator"))?;
Ok(Self(accumulator))
}

#[wasm_bindgen(js_name = "toBytes")]
pub fn to_bytes(&self) -> Vec<u8> {
self.0.as_bytes().to_vec()
}
}
9 changes: 6 additions & 3 deletions wnfs-wasm/tests/share.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test.describe("Share", () => {
test("share and recieve share", async ({ page }) => {
const result = await page.evaluate(async () => {
const {
wnfs: { PrivateForest, share, createShareName, receiveShare },
wnfs: { PrivateForest, Name, NameAccumulator, share, createShareName, receiveShare },
mock: {
MemoryBlockStore,
Rng,
Expand All @@ -31,7 +31,7 @@ test.describe("Share", () => {
const sharerRootDid = "did:key:z6MkqZjY";
const store = new MemoryBlockStore();

var { rootDir: sharerDir, forest: forest } = await createSharerDir(
var { rootDir: sharerDir, forest } = await createSharerDir(
forest,
store,
rng
Expand All @@ -58,8 +58,11 @@ test.describe("Share", () => {
const modulus = await recipientKey.getPublicKey().getPublicKeyModulus();
const shareLabel = createShareName(0, sharerRootDid, modulus, forest2);

const shareLabelSerialized = shareLabel.toNameAccumulator(forest2).toBytes();
const shareLabelDeserialized = new Name(NameAccumulator.fromBytes(shareLabelSerialized));

const sharedNode = await receiveShare(
shareLabel,
shareLabelDeserialized,
recipientKey,
forest2,
store
Expand Down

0 comments on commit 9bca7a1

Please sign in to comment.