Skip to content

Commit

Permalink
Bind Dids (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
nitro-neal authored Oct 7, 2024
1 parent ff0dbca commit 4ce21ad
Show file tree
Hide file tree
Showing 17 changed files with 774 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ bound/LICENSE
bound/typescript/dist/*
!bound/typescript/dist/index.js
bound/typescript/tests/compiled
bound/typescript/src/wasm/generated.js
bound/typescript/src/wasm/generated.js

# macOS
.DS_Store
5 changes: 3 additions & 2 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ setup:
if [ ! -d ".git/modules/web5-spec" ]; then
git submodule update --init --recursive
fi
if [[ "$(cargo 2>&1)" == *"rustup could not choose a version of cargo to run"* ]]; then
rustup default 1.76.0 # TODO undo this
if [[ "$(cargo --version)" != "cargo 1.76.0"* ]]; then
rustup install 1.76.0
rustup default 1.76.0
rustup target add aarch64-apple-darwin
fi
if ! command -v wasm-pack >/dev/null || [[ "$(wasm-pack --version)" != "wasm-pack 0.13.0" ]]; then
Expand Down
1 change: 0 additions & 1 deletion bindings/web5_wasm/src/crypto/dsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use web5::crypto::dsa::{
secp256k1::{Secp256k1Generator, Secp256k1Signer},
Signer,
};

#[wasm_bindgen]
pub struct WasmSigner {
inner: Arc<dyn Signer>,
Expand Down
12 changes: 12 additions & 0 deletions bindings/web5_wasm/src/crypto/key_managers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ impl From<InMemoryKeyManager> for WasmKeyManager {
}
}

impl From<Arc<dyn KeyManager>> for WasmKeyManager {
fn from(value: Arc<dyn KeyManager>) -> Self {
Self { inner: value }
}
}

impl From<WasmKeyManager> for Arc<dyn KeyManager> {
fn from(wasm_key_manager: WasmKeyManager) -> Self {
wasm_key_manager.inner
}
}

#[wasm_bindgen]
impl WasmKeyManager {
pub fn import_private_jwk(&self, private_jwk: WasmJwk) -> Result<WasmJwk> {
Expand Down
66 changes: 66 additions & 0 deletions bindings/web5_wasm/src/dids/bearer_did.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::errors::{map_err, Result};
use wasm_bindgen::prelude::wasm_bindgen;
use web5::dids::bearer_did::BearerDid;
use crate::crypto::dsa::WasmSigner;
use crate::crypto::key_managers::WasmKeyManager;
use crate::dids::did::WasmDid;
use crate::dids::document::WasmDocument;
use crate::dids::portable_did::WasmPortableDid;

#[wasm_bindgen]
pub struct WasmBearerDid {
inner: BearerDid,
}

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

#[wasm_bindgen]
impl WasmBearerDid {
#[wasm_bindgen(constructor)]
pub fn new(did: WasmDid, document: WasmDocument, key_manager: WasmKeyManager) -> Self {
Self {
inner: BearerDid {
did: did.into(),
document: document.into(),
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_err)?,
})
}

// todo key exporter for to_portable_did

#[wasm_bindgen]
pub fn get_signer(&self, verification_method_id: &str) -> Result<WasmSigner> {
Ok(self
.inner
.get_signer(verification_method_id)
.map_err(map_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: 99 additions & 0 deletions bindings/web5_wasm/src/dids/did.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use std::collections::HashMap;
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()
}
}
Loading

0 comments on commit 4ce21ad

Please sign in to comment.