Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Add InMemoryKeyManager, JSON stringify web5 parts over wasm ffi, remo…
Browse files Browse the repository at this point in the history
…ve generate-mappings project (#135)
  • Loading branch information
KendallWeihe authored Oct 7, 2024
1 parent 15d44c4 commit 51cb75f
Show file tree
Hide file tree
Showing 43 changed files with 213 additions and 1,489 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ jobs:
# NOTE: only currently testing nodejs in the CI, not the browser
run: |
cd bound/typescript
(cd generate-mappings/; npm install)
npm install
npm run clean
npm run build:wasm
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ lazy_static = "1.5.0"
serde = { version = "1.0.193", features = ["derive", "rc"] }
serde_json = "1.0.108"
thiserror = "1.0.50"
http-std = { git = "https://github.com/TBD54566975/web5-rs", rev = "ef1a31970411686ed3a2fd502f5952048cc39876" }
web5 = { git = "https://github.com/TBD54566975/web5-rs", rev = "ef1a31970411686ed3a2fd502f5952048cc39876" }
web5_uniffi_wrapper = { git = "https://github.com/TBD54566975/web5-rs", rev = "ef1a31970411686ed3a2fd502f5952048cc39876" }
http-std = { git = "https://github.com/TBD54566975/web5-rs", rev = "52bf9ca268a0fd17c68a8fc0c75d8c68d1cdb41f" }
web5 = { git = "https://github.com/TBD54566975/web5-rs", rev = "52bf9ca268a0fd17c68a8fc0c75d8c68d1cdb41f" }
web5_uniffi_wrapper = { git = "https://github.com/TBD54566975/web5-rs", rev = "52bf9ca268a0fd17c68a8fc0c75d8c68d1cdb41f" }

1 change: 0 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ wasm: setup
test-typescript: setup
#!/bin/bash
cd bound/typescript
(cd generate-mappings/; npm install)
npm install
npm run clean
npm run build:wasm
Expand Down
46 changes: 13 additions & 33 deletions bindings/tbdex_wasm/src/web5/bearer_did.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::{
did::WasmDid, document::WasmDocument, key_managers::WasmKeyManager,
portable_did::WasmPortableDid, signers::WasmSigner,
};
use super::{key_managers::WasmKeyManager, signers::WasmSigner};
use crate::errors::{map_web5_err, Result};
use wasm_bindgen::prelude::wasm_bindgen;
use web5::dids::bearer_did::BearerDid;
use web5::{
dids::{bearer_did::BearerDid, data_model::document::Document, did::Did},
json::FromJson,
};

#[wasm_bindgen]
pub struct WasmBearerDid {
Expand All @@ -20,25 +20,20 @@ impl From<WasmBearerDid> for BearerDid {
#[wasm_bindgen]
impl WasmBearerDid {
#[wasm_bindgen(constructor)]
pub fn new(did: WasmDid, document: WasmDocument, key_manager: WasmKeyManager) -> Self {
Self {
pub fn new(
did_uri: &str,
document_json: &str,
key_manager: WasmKeyManager,
) -> Result<WasmBearerDid> {
Ok(Self {
inner: BearerDid {
did: did.into(),
document: document.into(),
did: Did::parse(did_uri).map_err(map_web5_err)?,
document: Document::from_json_string(document_json).map_err(map_web5_err)?,
key_manager: key_manager.into(),
},
}
}

#[wasm_bindgen]
pub fn from_portable_did(portable_did: WasmPortableDid) -> Result<WasmBearerDid> {
Ok(Self {
inner: BearerDid::from_portable_did(portable_did.into()).map_err(map_web5_err)?,
})
}

// todo key exporter for to_portable_did

#[wasm_bindgen]
pub fn get_signer(&self, verification_method_id: &str) -> Result<WasmSigner> {
Ok(self
Expand All @@ -47,19 +42,4 @@ impl WasmBearerDid {
.map_err(map_web5_err)?
.into())
}

#[wasm_bindgen(getter)]
pub fn did(&self) -> WasmDid {
self.inner.did.clone().into()
}

#[wasm_bindgen(getter)]
pub fn document(&self) -> WasmDocument {
self.inner.document.clone().into()
}

#[wasm_bindgen(getter)]
pub fn key_manager(&self) -> WasmKeyManager {
self.inner.key_manager.clone().into()
}
}
99 changes: 5 additions & 94 deletions bindings/tbdex_wasm/src/web5/did.rs
Original file line number Diff line number Diff line change
@@ -1,99 +1,10 @@
use std::collections::HashMap;
use crate::errors::{map_err, map_web5_err, Result};
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use web5::dids::did::Did;

#[wasm_bindgen]
pub struct WasmDid {
inner: Did,
}

impl From<WasmDid> for Did {
fn from(value: WasmDid) -> Self {
value.inner
}
}

impl From<Did> for WasmDid {
fn from(value: Did) -> Self {
WasmDid { inner: value }
}
}

#[wasm_bindgen]
impl WasmDid {
#[allow(clippy::too_many_arguments)]
#[wasm_bindgen(constructor)]
pub fn new(
uri: String,
url: String,
method: String,
id: String,
params: JsValue,
path: Option<String>,
query: Option<String>,
fragment: Option<String>,
) -> Self {
let params = if params.is_undefined() {
None
} else {
serde_wasm_bindgen::from_value(params).unwrap_or(Some(HashMap::new()))
};

Self {
inner: Did {
uri,
url,
method,
id,
params,
path,
query,
fragment,
},
}
}

#[wasm_bindgen(getter)]
pub fn uri(&self) -> String {
self.inner.uri.clone()
}

#[wasm_bindgen(getter)]
pub fn url(&self) -> String {
self.inner.url.clone()
}

#[wasm_bindgen(getter)]
pub fn method(&self) -> String {
self.inner.method.clone()
}

#[wasm_bindgen(getter)]
pub fn id(&self) -> String {
self.inner.id.clone()
}

#[wasm_bindgen(getter)]
pub fn params(&self) -> JsValue {
match &self.inner.params {
Some(map) => serde_wasm_bindgen::to_value(map).unwrap_or(JsValue::UNDEFINED),
None => JsValue::UNDEFINED,
}
}

#[wasm_bindgen(getter)]
pub fn path(&self) -> Option<String> {
self.inner.path.clone()
}

#[wasm_bindgen(getter)]
pub fn query(&self) -> Option<String> {
self.inner.query.clone()
}

#[wasm_bindgen(getter)]
pub fn fragment(&self) -> Option<String> {
self.inner.fragment.clone()
}
pub fn parse_did(uri: &str) -> Result<String> {
let did = Did::parse(uri).map_err(map_web5_err)?;
let did_json = serde_json::to_string(&did).map_err(|e| map_err(e.into()))?;
Ok(did_json)
}
Loading

0 comments on commit 51cb75f

Please sign in to comment.