From 51cb75fb96b1b1f85a2afc2efe029ed4685adef9 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Mon, 7 Oct 2024 15:00:15 -0400 Subject: [PATCH] Add InMemoryKeyManager, JSON stringify web5 parts over wasm ffi, remove generate-mappings project (#135) --- .github/workflows/ci.yml | 1 - Cargo.toml | 6 +- Justfile | 1 - bindings/tbdex_wasm/src/web5/bearer_did.rs | 46 +--- bindings/tbdex_wasm/src/web5/did.rs | 99 +------ bindings/tbdex_wasm/src/web5/document.rs | 237 ---------------- bindings/tbdex_wasm/src/web5/jwk.rs | 79 ------ bindings/tbdex_wasm/src/web5/key_managers.rs | 57 ++-- bindings/tbdex_wasm/src/web5/mod.rs | 3 - bindings/tbdex_wasm/src/web5/portable_did.rs | 70 ----- bindings/tbdex_wasm/src/web5/signers.rs | 3 +- .../generate-mappings/package-lock.json | 253 ------------------ .../typescript/generate-mappings/package.json | 19 -- .../generate-mappings/src/generate.ts | 144 ---------- .../typescript/generate-mappings/src/main.ts | 20 -- .../typescript/generate-mappings/src/wasm.ts | 110 -------- .../generate-mappings/tsconfig.json | 13 - bound/typescript/package.json | 2 +- .../presentation-definition.ts | 0 .../src/crypto/in-memory-key-manager.ts | 26 ++ bound/typescript/src/crypto/jwk.ts | 8 + bound/typescript/src/crypto/key-manager.ts | 27 ++ .../src/{signers.ts => crypto/signer.ts} | 2 +- bound/typescript/src/{ => dids}/bearer-did.ts | 37 ++- bound/typescript/src/dids/did.ts | 20 ++ bound/typescript/src/dids/document.ts | 28 ++ bound/typescript/src/dids/portable-did.ts | 8 + bound/typescript/src/http-client/balances.ts | 2 +- bound/typescript/src/http-client/exchanges.ts | 3 +- bound/typescript/src/key-managers.ts | 41 --- bound/typescript/src/messages/cancel.ts | 2 +- bound/typescript/src/messages/close.ts | 2 +- .../src/messages/order-instructions.ts | 2 +- bound/typescript/src/messages/order-status.ts | 2 +- bound/typescript/src/messages/order.ts | 2 +- bound/typescript/src/messages/quote.ts | 2 +- bound/typescript/src/messages/rfq.ts | 2 +- bound/typescript/src/portable-did.ts | 38 --- bound/typescript/src/resources/balance.ts | 2 +- bound/typescript/src/resources/offering.ts | 4 +- bound/typescript/src/wasm/foreign-fetch.ts | 15 +- .../typescript/src/wasm/generated-mappings.ts | 253 ------------------ bound/typescript/tests/test-vectors.test.ts | 11 +- 43 files changed, 213 insertions(+), 1489 deletions(-) delete mode 100644 bindings/tbdex_wasm/src/web5/document.rs delete mode 100644 bindings/tbdex_wasm/src/web5/jwk.rs delete mode 100644 bindings/tbdex_wasm/src/web5/portable_did.rs delete mode 100644 bound/typescript/generate-mappings/package-lock.json delete mode 100644 bound/typescript/generate-mappings/package.json delete mode 100644 bound/typescript/generate-mappings/src/generate.ts delete mode 100644 bound/typescript/generate-mappings/src/main.ts delete mode 100644 bound/typescript/generate-mappings/src/wasm.ts delete mode 100644 bound/typescript/generate-mappings/tsconfig.json rename bound/typescript/src/{ => credentials}/presentation-definition.ts (100%) create mode 100644 bound/typescript/src/crypto/in-memory-key-manager.ts create mode 100644 bound/typescript/src/crypto/jwk.ts create mode 100644 bound/typescript/src/crypto/key-manager.ts rename bound/typescript/src/{signers.ts => crypto/signer.ts} (95%) rename bound/typescript/src/{ => dids}/bearer-did.ts (55%) create mode 100644 bound/typescript/src/dids/did.ts create mode 100644 bound/typescript/src/dids/document.ts create mode 100644 bound/typescript/src/dids/portable-did.ts delete mode 100644 bound/typescript/src/key-managers.ts delete mode 100644 bound/typescript/src/portable-did.ts delete mode 100644 bound/typescript/src/wasm/generated-mappings.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 019aabca..fcbd4888 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 49bb4e74..10324665 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/Justfile b/Justfile index ce42272f..0210cf24 100644 --- a/Justfile +++ b/Justfile @@ -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 diff --git a/bindings/tbdex_wasm/src/web5/bearer_did.rs b/bindings/tbdex_wasm/src/web5/bearer_did.rs index 248c3795..6dc32aa5 100644 --- a/bindings/tbdex_wasm/src/web5/bearer_did.rs +++ b/bindings/tbdex_wasm/src/web5/bearer_did.rs @@ -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 { @@ -20,25 +20,20 @@ impl From 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 { + 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 { - 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 { Ok(self @@ -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() - } } diff --git a/bindings/tbdex_wasm/src/web5/did.rs b/bindings/tbdex_wasm/src/web5/did.rs index 3a88b947..46bef04e 100644 --- a/bindings/tbdex_wasm/src/web5/did.rs +++ b/bindings/tbdex_wasm/src/web5/did.rs @@ -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 for Did { - fn from(value: WasmDid) -> Self { - value.inner - } -} - -impl From 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, - query: Option, - fragment: Option, - ) -> 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 { - self.inner.path.clone() - } - - #[wasm_bindgen(getter)] - pub fn query(&self) -> Option { - self.inner.query.clone() - } - - #[wasm_bindgen(getter)] - pub fn fragment(&self) -> Option { - self.inner.fragment.clone() - } +pub fn parse_did(uri: &str) -> Result { + 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) } diff --git a/bindings/tbdex_wasm/src/web5/document.rs b/bindings/tbdex_wasm/src/web5/document.rs deleted file mode 100644 index 28b7b78a..00000000 --- a/bindings/tbdex_wasm/src/web5/document.rs +++ /dev/null @@ -1,237 +0,0 @@ -use super::jwk::WasmJwk; -use crate::errors::{map_web5_err, Result}; -use wasm_bindgen::prelude::wasm_bindgen; -use web5::{ - dids::data_model::{ - document::Document, service::Service, verification_method::VerificationMethod, - }, - json::{FromJson, ToJson}, -}; - -#[wasm_bindgen] -pub struct WasmDocument { - inner: Document, -} - -impl From for Document { - fn from(value: WasmDocument) -> Self { - value.inner - } -} - -impl From for WasmDocument { - fn from(value: Document) -> Self { - WasmDocument { inner: value } - } -} - -#[wasm_bindgen] -impl WasmDocument { - #[allow(clippy::too_many_arguments)] - #[wasm_bindgen(constructor)] - pub fn new( - id: String, - context: Option>, - controller: Option>, - also_known_as: Option>, - verification_method: Vec, - authentication: Option>, - assertion_method: Option>, - key_agreement: Option>, - capability_invocation: Option>, - capability_delegation: Option>, - service: Option>, - ) -> Self { - Self { - inner: Document { - id, - context, - controller, - also_known_as, - verification_method: verification_method - .into_iter() - .map(|wvm| wvm.into()) - .collect(), - authentication, - assertion_method, - key_agreement, - capability_invocation, - capability_delegation, - service: service.map(|wss| wss.into_iter().map(|ws| ws.into()).collect()), - }, - } - } - - #[wasm_bindgen] - pub fn from_json_string(json: &str) -> Result { - Ok(Self { - inner: Document::from_json_string(json).map_err(map_web5_err)?, - }) - } - - #[wasm_bindgen] - pub fn to_json_string(&self) -> Result { - self.inner.to_json_string().map_err(map_web5_err) - } - - #[wasm_bindgen(getter)] - pub fn id(&self) -> String { - self.inner.id.clone() - } - - #[wasm_bindgen(getter)] - pub fn context(&self) -> Option> { - self.inner.context.clone() - } - - #[wasm_bindgen(getter)] - pub fn controller(&self) -> Option> { - self.inner.controller.clone() - } - - #[wasm_bindgen(getter)] - pub fn also_known_as(&self) -> Option> { - self.inner.also_known_as.clone() - } - - #[wasm_bindgen(getter)] - pub fn verification_method(&self) -> Vec { - self.inner - .verification_method - .clone() - .into_iter() - .map(|vm| vm.into()) - .collect() - } - - #[wasm_bindgen(getter)] - pub fn authentication(&self) -> Option> { - self.inner.authentication.clone() - } - - #[wasm_bindgen(getter)] - pub fn assertion_method(&self) -> Option> { - self.inner.assertion_method.clone() - } - - #[wasm_bindgen(getter)] - pub fn key_agreement(&self) -> Option> { - self.inner.key_agreement.clone() - } - - #[wasm_bindgen(getter)] - pub fn capability_invocation(&self) -> Option> { - self.inner.capability_invocation.clone() - } - - #[wasm_bindgen(getter)] - pub fn capability_delegation(&self) -> Option> { - self.inner.capability_delegation.clone() - } - - #[wasm_bindgen(getter)] - pub fn service(&self) -> Option> { - self.inner - .service - .clone() - .map(|services| services.into_iter().map(|s| s.into()).collect()) - } -} - -#[wasm_bindgen] -pub struct WasmVerificationMethod { - inner: VerificationMethod, -} - -impl From for VerificationMethod { - fn from(value: WasmVerificationMethod) -> Self { - value.inner - } -} - -impl From for WasmVerificationMethod { - fn from(value: VerificationMethod) -> Self { - Self { inner: value } - } -} - -#[wasm_bindgen] -impl WasmVerificationMethod { - #[wasm_bindgen(constructor)] - pub fn new(id: String, r#type: String, controller: String, public_key_jwk: WasmJwk) -> Self { - Self { - inner: VerificationMethod { - id, - r#type, - controller, - public_key_jwk: public_key_jwk.into(), - }, - } - } - - #[wasm_bindgen(getter)] - pub fn id(&self) -> String { - self.inner.id.clone() - } - - #[wasm_bindgen(getter)] - pub fn r#type(&self) -> String { - self.inner.r#type.clone() - } - - #[wasm_bindgen(getter)] - pub fn controller(&self) -> String { - self.inner.controller.clone() - } - - #[wasm_bindgen(getter)] - pub fn public_key_jwk(&self) -> WasmJwk { - self.inner.public_key_jwk.clone().into() - } -} - -#[wasm_bindgen] -pub struct WasmService { - inner: Service, -} - -impl From for Service { - fn from(value: WasmService) -> Self { - value.inner - } -} - -impl From for WasmService { - fn from(value: Service) -> Self { - Self { inner: value } - } -} - -#[wasm_bindgen] -impl WasmService { - #[wasm_bindgen(constructor)] - pub fn new(id: String, r#type: String, service_endpoint: Vec) -> Self { - Self { - inner: Service { - id, - r#type, - service_endpoint, - }, - } - } - - #[wasm_bindgen(getter)] - pub fn id(&self) -> String { - self.inner.id.clone() - } - - #[wasm_bindgen(getter)] - pub fn r#type(&self) -> String { - self.inner.r#type.clone() - } - - #[wasm_bindgen(getter)] - pub fn service_endpoint(&self) -> Vec { - self.inner.service_endpoint.clone() - } -} diff --git a/bindings/tbdex_wasm/src/web5/jwk.rs b/bindings/tbdex_wasm/src/web5/jwk.rs deleted file mode 100644 index 18ebd339..00000000 --- a/bindings/tbdex_wasm/src/web5/jwk.rs +++ /dev/null @@ -1,79 +0,0 @@ -use crate::errors::{map_web5_err, Result}; -use wasm_bindgen::prelude::wasm_bindgen; -use web5::crypto::jwk::Jwk; - -#[wasm_bindgen] -pub struct WasmJwk { - inner: Jwk, -} - -impl From for WasmJwk { - fn from(value: Jwk) -> Self { - Self { inner: value } - } -} - -impl From for Jwk { - fn from(value: WasmJwk) -> Self { - value.inner - } -} - -#[wasm_bindgen] -impl WasmJwk { - #[wasm_bindgen(constructor)] - pub fn new( - alg: Option, - kty: String, - crv: String, - d: Option, - x: String, - y: Option, - ) -> WasmJwk { - WasmJwk { - inner: Jwk { - alg, - kty, - crv, - d, - x, - y, - }, - } - } - - #[wasm_bindgen] - pub fn compute_thumbprint(&self) -> Result { - self.inner.compute_thumbprint().map_err(map_web5_err) - } - - #[wasm_bindgen(getter)] - pub fn alg(&self) -> Option { - self.inner.alg.clone() - } - - #[wasm_bindgen(getter)] - pub fn kty(&self) -> String { - self.inner.kty.clone() - } - - #[wasm_bindgen(getter)] - pub fn crv(&self) -> String { - self.inner.crv.clone() - } - - #[wasm_bindgen(getter)] - pub fn d(&self) -> Option { - self.inner.d.clone() - } - - #[wasm_bindgen(getter)] - pub fn x(&self) -> String { - self.inner.x.clone() - } - - #[wasm_bindgen(getter)] - pub fn y(&self) -> Option { - self.inner.y.clone() - } -} diff --git a/bindings/tbdex_wasm/src/web5/key_managers.rs b/bindings/tbdex_wasm/src/web5/key_managers.rs index c0fbd19b..fddc422a 100644 --- a/bindings/tbdex_wasm/src/web5/key_managers.rs +++ b/bindings/tbdex_wasm/src/web5/key_managers.rs @@ -1,24 +1,26 @@ -use super::jwk::WasmJwk; use crate::{ - errors::{map_web5_err, Result}, + errors::{map_err, map_web5_err, Result}, web5::signers::WasmSigner, }; use std::sync::Arc; use wasm_bindgen::prelude::wasm_bindgen; -use web5::crypto::key_managers::KeyManager; +use web5::crypto::{ + jwk::Jwk, + key_managers::{in_memory_key_manager::InMemoryKeyManager, KeyManager}, +}; #[wasm_bindgen] extern "C" { #[wasm_bindgen( - typescript_type = "{ import_private_jwk: (private_jwk: WasmJwk) => WasmJwk, get_signer: (public_jwk: WasmJwk) => WasmSigner }" + typescript_type = "{ import_private_jwk: (private_jwk_json: string) => string, get_signer: (public_jwk_json: string) => WasmSigner }" )] pub type ForeignKeyManager; #[wasm_bindgen(method)] - fn import_private_jwk(this: &ForeignKeyManager, private_jwk: WasmJwk) -> WasmJwk; + fn import_private_jwk(this: &ForeignKeyManager, private_jwk_json: &str) -> String; #[wasm_bindgen(method)] - fn get_signer(this: &ForeignKeyManager, public_jwk: WasmJwk) -> WasmSigner; + fn get_signer(this: &ForeignKeyManager, public_jwk_json: &str) -> WasmSigner; } pub struct ConcreteForeignKeyManager(ForeignKeyManager); @@ -33,18 +35,19 @@ unsafe impl Send for ConcreteForeignKeyManager {} unsafe impl Sync for ConcreteForeignKeyManager {} impl KeyManager for ConcreteForeignKeyManager { - fn import_private_jwk( - &self, - private_jwk: web5::crypto::jwk::Jwk, - ) -> web5::errors::Result { - Ok(self.0.import_private_jwk(private_jwk.into()).into()) + fn import_private_jwk(&self, private_jwk: Jwk) -> web5::errors::Result { + let private_jwk_json = serde_json::to_string(&private_jwk)?; + let public_jwk_json = self.0.import_private_jwk(&private_jwk_json); + let public_jwk = serde_json::from_str::(&public_jwk_json)?; + Ok(public_jwk) } fn get_signer( &self, - public_jwk: web5::crypto::jwk::Jwk, + public_jwk: Jwk, ) -> web5::errors::Result> { - Ok(self.0.get_signer(public_jwk.into()).into()) + let public_jwk_json = serde_json::to_string(&public_jwk)?; + Ok(self.0.get_signer(&public_jwk_json).into()) } } @@ -75,20 +78,34 @@ impl WasmKeyManager { } #[wasm_bindgen] - pub fn import_private_jwk(&self, private_jwk: WasmJwk) -> Result { - Ok(self + pub fn import_private_jwk(&self, private_jwk_json: &str) -> Result { + let private_jwk = + serde_json::from_str::(private_jwk_json).map_err(|e| map_err(e.into()))?; + let public_jwk = self .inner - .import_private_jwk(private_jwk.into()) - .map_err(map_web5_err)? - .into()) + .import_private_jwk(private_jwk) + .map_err(map_web5_err)?; + let public_jwk_json = serde_json::to_string(&public_jwk).map_err(|e| map_err(e.into()))?; + + Ok(public_jwk_json) } #[wasm_bindgen] - pub fn get_signer(&self, public_jwk: WasmJwk) -> Result { + pub fn get_signer(&self, public_jwk_json: &str) -> Result { + let public_jwk = + serde_json::from_str::(public_jwk_json).map_err(|e| map_err(e.into()))?; Ok(self .inner - .get_signer(public_jwk.into()) + .get_signer(public_jwk) .map_err(map_web5_err)? .into()) } } + +#[wasm_bindgen] +pub fn new_in_memory_key_manager() -> WasmKeyManager { + let in_memory_key_manager = InMemoryKeyManager::new(); + WasmKeyManager { + inner: Arc::new(in_memory_key_manager), + } +} diff --git a/bindings/tbdex_wasm/src/web5/mod.rs b/bindings/tbdex_wasm/src/web5/mod.rs index df6e6713..314c131e 100644 --- a/bindings/tbdex_wasm/src/web5/mod.rs +++ b/bindings/tbdex_wasm/src/web5/mod.rs @@ -1,7 +1,4 @@ pub mod bearer_did; pub mod did; -pub mod document; -pub mod jwk; pub mod key_managers; -pub mod portable_did; pub mod signers; diff --git a/bindings/tbdex_wasm/src/web5/portable_did.rs b/bindings/tbdex_wasm/src/web5/portable_did.rs deleted file mode 100644 index 0b1b14ae..00000000 --- a/bindings/tbdex_wasm/src/web5/portable_did.rs +++ /dev/null @@ -1,70 +0,0 @@ -use super::{document::WasmDocument, jwk::WasmJwk}; -use crate::errors::{map_web5_err, Result}; -use wasm_bindgen::prelude::wasm_bindgen; -use web5::{ - dids::portable_did::PortableDid, - json::{FromJson, ToJson}, -}; - -#[wasm_bindgen] -pub struct WasmPortableDid { - inner: PortableDid, -} - -impl From for PortableDid { - fn from(value: WasmPortableDid) -> Self { - value.inner - } -} - -impl From for WasmPortableDid { - fn from(value: PortableDid) -> Self { - Self { inner: value } - } -} - -#[wasm_bindgen] -impl WasmPortableDid { - #[wasm_bindgen(constructor)] - pub fn new(did_uri: String, document: WasmDocument, private_keys: Vec) -> Self { - Self { - inner: PortableDid { - did_uri, - document: document.into(), - private_jwks: private_keys.into_iter().map(|pj| pj.into()).collect(), - }, - } - } - - #[wasm_bindgen] - pub fn from_json_string(json: &str) -> Result { - Ok(Self { - inner: PortableDid::from_json_string(json).map_err(map_web5_err)?, - }) - } - - #[wasm_bindgen] - pub fn to_json_string(&self) -> Result { - self.inner.to_json_string().map_err(map_web5_err) - } - - #[wasm_bindgen(getter)] - pub fn did_uri(&self) -> String { - self.inner.did_uri.clone() - } - - #[wasm_bindgen(getter)] - pub fn document(&self) -> WasmDocument { - self.inner.document.clone().into() - } - - #[wasm_bindgen(getter)] - pub fn private_keys(&self) -> Vec { - self.inner - .private_jwks - .clone() - .into_iter() - .map(|j| j.into()) - .collect() - } -} diff --git a/bindings/tbdex_wasm/src/web5/signers.rs b/bindings/tbdex_wasm/src/web5/signers.rs index ac788afb..56ff8e98 100644 --- a/bindings/tbdex_wasm/src/web5/signers.rs +++ b/bindings/tbdex_wasm/src/web5/signers.rs @@ -1,9 +1,8 @@ +use crate::errors::{map_web5_err, Result}; use std::sync::Arc; use wasm_bindgen::prelude::wasm_bindgen; use web5::crypto::dsa::Signer; -use crate::errors::{map_web5_err, Result}; - #[wasm_bindgen] extern "C" { #[wasm_bindgen(typescript_type = "{ sign: (payload: Uint8Array) => Uint8Array }")] diff --git a/bound/typescript/generate-mappings/package-lock.json b/bound/typescript/generate-mappings/package-lock.json deleted file mode 100644 index efa095a9..00000000 --- a/bound/typescript/generate-mappings/package-lock.json +++ /dev/null @@ -1,253 +0,0 @@ -{ - "name": "generate-mappings", - "version": "1.0.0", - "lockfileVersion": 3, - "requires": true, - "packages": { - "": { - "name": "generate-mappings", - "version": "1.0.0", - "license": "ISC", - "devDependencies": { - "@types/node": "^22.6.1", - "prettier": "^3.3.3", - "ts-node": "^10.9.2", - "typescript": "^5.6.2" - } - }, - "node_modules/@cspotcode/source-map-support": { - "version": "0.8.1", - "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", - "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", - "dev": true, - "license": "MIT", - "dependencies": { - "@jridgewell/trace-mapping": "0.3.9" - }, - "engines": { - "node": ">=12" - } - }, - "node_modules/@jridgewell/resolve-uri": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", - "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=6.0.0" - } - }, - "node_modules/@jridgewell/sourcemap-codec": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.5.0.tgz", - "integrity": "sha512-gv3ZRaISU3fjPAgNsriBRqGWQL6quFx04YMPW/zD8XMLsU32mhCCbfbO6KZFLjvYpCZ8zyDEgqsgf+PwPaM7GQ==", - "dev": true, - "license": "MIT" - }, - "node_modules/@jridgewell/trace-mapping": { - "version": "0.3.9", - "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", - "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "@jridgewell/resolve-uri": "^3.0.3", - "@jridgewell/sourcemap-codec": "^1.4.10" - } - }, - "node_modules/@tsconfig/node10": { - "version": "1.0.11", - "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.11.tgz", - "integrity": "sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==", - "dev": true, - "license": "MIT" - }, - "node_modules/@tsconfig/node12": { - "version": "1.0.11", - "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz", - "integrity": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==", - "dev": true, - "license": "MIT" - }, - "node_modules/@tsconfig/node14": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz", - "integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==", - "dev": true, - "license": "MIT" - }, - "node_modules/@tsconfig/node16": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz", - "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==", - "dev": true, - "license": "MIT" - }, - "node_modules/@types/node": { - "version": "22.6.1", - "resolved": "https://registry.npmjs.org/@types/node/-/node-22.6.1.tgz", - "integrity": "sha512-V48tCfcKb/e6cVUigLAaJDAILdMP0fUW6BidkPK4GpGjXcfbnoHasCZDwz3N3yVt5we2RHm4XTQCpv0KJz9zqw==", - "dev": true, - "license": "MIT", - "dependencies": { - "undici-types": "~6.19.2" - } - }, - "node_modules/acorn": { - "version": "8.12.1", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.12.1.tgz", - "integrity": "sha512-tcpGyI9zbizT9JbV6oYE477V6mTlXvvi0T0G3SNIYE2apm/G5huBa1+K89VGeovbg+jycCrfhl3ADxErOuO6Jg==", - "dev": true, - "license": "MIT", - "bin": { - "acorn": "bin/acorn" - }, - "engines": { - "node": ">=0.4.0" - } - }, - "node_modules/acorn-walk": { - "version": "8.3.4", - "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.3.4.tgz", - "integrity": "sha512-ueEepnujpqee2o5aIYnvHU6C0A42MNdsIDeqy5BydrkuC5R1ZuUFnm27EeFJGoEHJQgn3uleRvmTXaJgfXbt4g==", - "dev": true, - "license": "MIT", - "dependencies": { - "acorn": "^8.11.0" - }, - "engines": { - "node": ">=0.4.0" - } - }, - "node_modules/arg": { - "version": "4.1.3", - "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", - "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==", - "dev": true, - "license": "MIT" - }, - "node_modules/create-require": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", - "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==", - "dev": true, - "license": "MIT" - }, - "node_modules/diff": { - "version": "4.0.2", - "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", - "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", - "dev": true, - "license": "BSD-3-Clause", - "engines": { - "node": ">=0.3.1" - } - }, - "node_modules/make-error": { - "version": "1.3.6", - "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", - "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==", - "dev": true, - "license": "ISC" - }, - "node_modules/prettier": { - "version": "3.3.3", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.3.3.tgz", - "integrity": "sha512-i2tDNA0O5IrMO757lfrdQZCc2jPNDVntV0m/+4whiDfWaTKfMNgR7Qz0NAeGz/nRqF4m5/6CLzbP4/liHt12Ew==", - "dev": true, - "license": "MIT", - "bin": { - "prettier": "bin/prettier.cjs" - }, - "engines": { - "node": ">=14" - }, - "funding": { - "url": "https://github.com/prettier/prettier?sponsor=1" - } - }, - "node_modules/ts-node": { - "version": "10.9.2", - "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz", - "integrity": "sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "@cspotcode/source-map-support": "^0.8.0", - "@tsconfig/node10": "^1.0.7", - "@tsconfig/node12": "^1.0.7", - "@tsconfig/node14": "^1.0.0", - "@tsconfig/node16": "^1.0.2", - "acorn": "^8.4.1", - "acorn-walk": "^8.1.1", - "arg": "^4.1.0", - "create-require": "^1.1.0", - "diff": "^4.0.1", - "make-error": "^1.1.1", - "v8-compile-cache-lib": "^3.0.1", - "yn": "3.1.1" - }, - "bin": { - "ts-node": "dist/bin.js", - "ts-node-cwd": "dist/bin-cwd.js", - "ts-node-esm": "dist/bin-esm.js", - "ts-node-script": "dist/bin-script.js", - "ts-node-transpile-only": "dist/bin-transpile.js", - "ts-script": "dist/bin-script-deprecated.js" - }, - "peerDependencies": { - "@swc/core": ">=1.2.50", - "@swc/wasm": ">=1.2.50", - "@types/node": "*", - "typescript": ">=2.7" - }, - "peerDependenciesMeta": { - "@swc/core": { - "optional": true - }, - "@swc/wasm": { - "optional": true - } - } - }, - "node_modules/typescript": { - "version": "5.6.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.2.tgz", - "integrity": "sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw==", - "dev": true, - "license": "Apache-2.0", - "bin": { - "tsc": "bin/tsc", - "tsserver": "bin/tsserver" - }, - "engines": { - "node": ">=14.17" - } - }, - "node_modules/undici-types": { - "version": "6.19.8", - "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz", - "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==", - "dev": true, - "license": "MIT" - }, - "node_modules/v8-compile-cache-lib": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", - "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==", - "dev": true, - "license": "MIT" - }, - "node_modules/yn": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", - "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=6" - } - } - } -} diff --git a/bound/typescript/generate-mappings/package.json b/bound/typescript/generate-mappings/package.json deleted file mode 100644 index 00a460d2..00000000 --- a/bound/typescript/generate-mappings/package.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "type": "module", - "name": "generate-mappings", - "version": "1.0.0", - "main": "index.js", - "scripts": { - "generate": "node --loader ts-node/esm src/main.ts && prettier --write generated-mappings.ts && mv generated-mappings.ts ../src/wasm" - }, - "keywords": [], - "author": "", - "license": "ISC", - "description": "", - "devDependencies": { - "@types/node": "^22.6.1", - "prettier": "^3.3.3", - "ts-node": "^10.9.2", - "typescript": "^5.6.2" - } -} diff --git a/bound/typescript/generate-mappings/src/generate.ts b/bound/typescript/generate-mappings/src/generate.ts deleted file mode 100644 index 3fbf39d2..00000000 --- a/bound/typescript/generate-mappings/src/generate.ts +++ /dev/null @@ -1,144 +0,0 @@ -import { WasmClass } from "./wasm.js"; - -const EXCLUDE = [ - "KeyManager", - "BearerDid", - "Signer", - "PortableDid", - - // resources - "Offering", - "Balance", - - // messages - "Rfq", - "Quote", - "Order", - "OrderInstructions", - "Cancel", - "OrderStatus", - "Close", - - // http - "GetExchangeResponseBody", - "GetExchangesResponseBody", - "CreateExchangeRequestBody", - "UpdateExchangeRequestBody", - "ReplyToRequestBody", - "GetOfferingsResponseBody", - "GetBalancesResponseBody", - "Exchange" -]; - -export const generateToWASM = (wasmClass: WasmClass): string => ` - export const toWASM = ( - obj: ${wasmClass.className} - ): wasm.Wasm${wasmClass.className} => { - return new wasm.Wasm${wasmClass.className}( - ${wasmClass.constructorParams - .map((p) => { - const member = wasmClass.members.find((x) => x.tsName === p); - if (member?.isWasmClass) { - if (member.type.endsWith("[]")) { - // if array, then map over the property and call the toWASM() function for each - return `obj.${p}?.map(${member.type.slice(0, -2)}.toWASM)`; - } else if (member.isNullable) { - // if nullable, then only call the toWASM() function if it's value is set - return `obj.${p} ? ${member.type}.toWASM(obj.${p}) : undefined`; - } else { - // call the toWASM() function - return `${member.type}.toWASM(obj.${p})`; - } - } else if (member?.type === "bigint") { - return `BigInt(obj.${p})`; - } else { - return `obj.${p}`; - } - }) - .join(",")} - ) - } -`; - -export const generateFromWASM = (wasmClass: WasmClass): string => ` - export const fromWASM = ( - obj: wasm.Wasm${wasmClass.className} - ): ${wasmClass.className} => { - const result: ${wasmClass.className} = { - ${wasmClass.members - .filter((x) => !x.isNullable) - .map((member) => { - if (member.isWasmClass) { - if (member.type.endsWith("[]")) { - // if Wasm* array, then map over each and fromWASM() - return `${member.tsName}: obj.${ - member.wasmName - }?.map(${member.type.slice(0, -2)}.fromWASM)`; - } else { - // else if Wasm*, then call fromWASM() - return `${member.tsName}: ${member.type}.fromWASM(obj.${member.wasmName}),`; - } - } else if (member.type === "bigint") { - // special case for bigint - return `${member.tsName}: Number(obj.${member.wasmName}),`; - } else { - // else just simple assignment - return `${member.tsName}: obj.${member.wasmName},`; - } - }) - .join("")} - }; - - ${wasmClass.members - .filter((x) => x.isNullable) - .map((member) => { - let code = ` - if (obj.${member.wasmName} !== undefined) - result.${member.tsName} = `; - - if (member.isWasmClass) { - if (member.type.endsWith("[]")) - code += `obj.${member.wasmName}?.map(${member.type.slice( - 0, - -2 - )}.fromWASM)`; - else code += `${member.type}.fromWASM(obj.${member.wasmName})`; - } else { - code += `obj.${member.wasmName}`; - } - - return code; - }) - .join("")} - - return result - }; -`; - -export const generateMappingsCode = (wasmClasses: WasmClass[]): string => ` - import wasm from "./" - - ${wasmClasses - .map((wasmClass) => { - // todo - if (EXCLUDE.includes(wasmClass.className)) return; - - return ` - export type ${wasmClass.className} = { - ${wasmClass.members - .map((m) => { - if (m.type === "bigint") - return `${m.tsName}${m.isNullable ? "?" : ""}: number`; - else return `${m.tsName}${m.isNullable ? "?" : ""}: ${m.type}`; - }) - .join("\n")} - } - - export namespace ${wasmClass.className} { - ${generateToWASM(wasmClass)} - ${generateFromWASM(wasmClass)} - } - `; - }) - .join("\n")} -`; diff --git a/bound/typescript/generate-mappings/src/main.ts b/bound/typescript/generate-mappings/src/main.ts deleted file mode 100644 index 29728e51..00000000 --- a/bound/typescript/generate-mappings/src/main.ts +++ /dev/null @@ -1,20 +0,0 @@ -import * as fs from "fs"; -import * as path from "path"; -import { fileURLToPath } from "url"; -import { readWasmClasses } from "./wasm.js"; -import { generateMappingsCode } from "./generate.js"; - -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); -const tsCode = fs.readFileSync( - path.resolve(__dirname, "../../src/wasm/generated.d.ts"), - "utf8" -); - -const inputCode = tsCode; -const wasmClasses = readWasmClasses(inputCode); -const outputCode = generateMappingsCode(wasmClasses); -fs.writeFileSync( - path.resolve(__dirname, "../generated-mappings.ts"), - outputCode -); diff --git a/bound/typescript/generate-mappings/src/wasm.ts b/bound/typescript/generate-mappings/src/wasm.ts deleted file mode 100644 index 8e0e8ad2..00000000 --- a/bound/typescript/generate-mappings/src/wasm.ts +++ /dev/null @@ -1,110 +0,0 @@ -import * as ts from "typescript"; - -export type WasmClass = { - className: string; - members: WasmClassMember[]; - constructorParams: string[]; -}; - -export type WasmClassMember = { - wasmName: string; // always snake case - tsName: string; // camel case except for PEX types - type: string; - isWasmClass: boolean; - isNullable: boolean; -}; - -const toCamelCase = (snakeCase: string): string => { - return snakeCase.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()); -}; - -const isNullable = (typeNode: ts.TypeNode): boolean => { - const type = typeNode.getText(); - return type === "any" || type.includes("undefined") || type.includes("null"); -}; - -const PEX_CONST_VALUE = "const_value"; -const PEX_NAMES = [ - "input_descriptors", - "submission_requirements", - "from_nested", - PEX_CONST_VALUE, -]; - -export const readWasmClasses = (code: string): WasmClass[] => { - const classes: WasmClass[] = []; - - const sourceFile = ts.createSourceFile( - "tmp.ts", - code, - ts.ScriptTarget.Latest, - true, - ts.ScriptKind.TS - ); - - ts.forEachChild(sourceFile, (node) => { - if (ts.isClassDeclaration(node) && node.name) { - if (!node.name.text.startsWith("Wasm")) { - return; - } - - const c: WasmClass = { - className: node.name.text.slice(4), - members: [], - constructorParams: [], - }; - - node.members.forEach((member) => { - if (ts.isPropertyDeclaration(member) && member.name && member.type) { - const wasmName = member.name.getText(); - - let tsName = toCamelCase(wasmName); - // PEX names are the exception to the rule b/c they're snake case - if (PEX_NAMES.includes(wasmName)) { - if (wasmName === PEX_CONST_VALUE) tsName = "const"; - else tsName = wasmName; - } - - let type = member.type.getText(); - if (type.includes(" | undefined")) - type = type.replace(" | undefined", ""); // we use `?` syntax instead - if (type.endsWith("[]") && type.startsWith("(")) - type = type.slice(1, -3) + "[]"; // ex. `(string)[]` slice out the `()` - if (type.startsWith("Wasm")) type = type.slice(4); - - const classMember: WasmClassMember = { - wasmName, - tsName, - type, - isNullable: isNullable(member.type), - isWasmClass: - member.type.getText().startsWith("Wasm") || - member.type.getText().startsWith("(Wasm"), - }; - - c.members.push(classMember); - } - - if (ts.isConstructorDeclaration(member)) { - member.parameters.forEach((param) => { - if (ts.isParameter(param) && param.name) { - const wasmName = param.name.getText(); - let tsName = toCamelCase(wasmName); - // PEX names are the exception to the rule b/c they're snake case - if (PEX_NAMES.includes(wasmName)) { - if (wasmName === PEX_CONST_VALUE) tsName = "const"; - else tsName = wasmName; - } - - c.constructorParams.push(tsName); - } - }); - } - }); - - classes.push(c); - } - }); - - return classes; -}; diff --git a/bound/typescript/generate-mappings/tsconfig.json b/bound/typescript/generate-mappings/tsconfig.json deleted file mode 100644 index 5bf6d70b..00000000 --- a/bound/typescript/generate-mappings/tsconfig.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "compilerOptions": { - "module": "NodeNext", - "target": "ESNext", - "outDir": "./dist", - "rootDir": "./src", - "strict": true, - "moduleResolution": "nodenext" - }, - "include": [ - "src/**/*.ts" - ] -} diff --git a/bound/typescript/package.json b/bound/typescript/package.json index 6e3e5309..9abb6c1c 100644 --- a/bound/typescript/package.json +++ b/bound/typescript/package.json @@ -33,7 +33,7 @@ }, "scripts": { "clean": "rimraf pkg tests/compiled dist", - "build:wasm": "./scripts/build.sh && node ./scripts/bundle.js && cp dist/bundle.js src/wasm/generated.js && cp dist/tbdex_wasm.d.ts src/wasm/generated.d.ts && (cd ./generate-mappings && npm run generate)", + "build:wasm": "./scripts/build.sh && node ./scripts/bundle.js && cp dist/bundle.js src/wasm/generated.js && cp dist/tbdex_wasm.d.ts src/wasm/generated.d.ts", "build": "tsc && cp -r src/wasm dist/bound/typescript/src", "test:node": "npm run build && node tests/bundle-node.js && mocha", "test:browser": "npm run build && node tests/bundle-browser.js && web-test-runner", diff --git a/bound/typescript/src/presentation-definition.ts b/bound/typescript/src/credentials/presentation-definition.ts similarity index 100% rename from bound/typescript/src/presentation-definition.ts rename to bound/typescript/src/credentials/presentation-definition.ts diff --git a/bound/typescript/src/crypto/in-memory-key-manager.ts b/bound/typescript/src/crypto/in-memory-key-manager.ts new file mode 100644 index 00000000..14666237 --- /dev/null +++ b/bound/typescript/src/crypto/in-memory-key-manager.ts @@ -0,0 +1,26 @@ +import wasm from "../wasm"; +import { Jwk } from "./jwk"; +import { KeyManager } from "./key-manager"; +import { Signer } from "./signer"; + +export class InMemoryKeyManager implements KeyManager { + private readonly wasmKeyManager: wasm.WasmKeyManager; + + constructor() { + this.wasmKeyManager = wasm.new_in_memory_key_manager(); + } + + importPrivateJwk(privateJwk: Jwk): Jwk { + const privateJwkJson = JSON.stringify(privateJwk); + const publicJwkJson = + this.wasmKeyManager.import_private_jwk(privateJwkJson); + const publicJwk: Jwk = JSON.parse(publicJwkJson); + return publicJwk; + } + + getSigner(publicJwk: Jwk): Signer { + const publicJwkJson = JSON.stringify(publicJwk); + const wasmSigner = this.wasmKeyManager.get_signer(publicJwkJson); + return Signer.fromWASM(wasmSigner); + } +} diff --git a/bound/typescript/src/crypto/jwk.ts b/bound/typescript/src/crypto/jwk.ts new file mode 100644 index 00000000..7750689d --- /dev/null +++ b/bound/typescript/src/crypto/jwk.ts @@ -0,0 +1,8 @@ +export type Jwk = { + alg?: string; + crv: string; + d?: string; + kty: string; + x: string; + y?: string; +}; diff --git a/bound/typescript/src/crypto/key-manager.ts b/bound/typescript/src/crypto/key-manager.ts new file mode 100644 index 00000000..ce2a047c --- /dev/null +++ b/bound/typescript/src/crypto/key-manager.ts @@ -0,0 +1,27 @@ +import { Jwk } from "./jwk"; +import { Signer } from "./signer"; +import wasm from "../wasm"; + +export type KeyManager = { + importPrivateJwk(privateJwk: Jwk): Jwk; + getSigner(publicJwk: Jwk): Signer; +}; + +export namespace KeyManager { + export const toWASM = (keyManager: KeyManager): wasm.WasmKeyManager => { + const foreignKeyManager = { + import_private_jwk: (privateJwkJson: string): string => { + const privateJwk: Jwk = JSON.parse(privateJwkJson); + const publicJwk = keyManager.importPrivateJwk(privateJwk); + const publicJwkJson = JSON.stringify(publicJwk); + return publicJwkJson; + }, + get_signer: (publicJwkJson: string): wasm.WasmSigner => { + const publicJwk: Jwk = JSON.parse(publicJwkJson); + return Signer.toWASM(keyManager.getSigner(publicJwk)); + }, + }; + + return new wasm.WasmKeyManager(foreignKeyManager); + }; +} diff --git a/bound/typescript/src/signers.ts b/bound/typescript/src/crypto/signer.ts similarity index 95% rename from bound/typescript/src/signers.ts rename to bound/typescript/src/crypto/signer.ts index bb347db5..a6be59c1 100644 --- a/bound/typescript/src/signers.ts +++ b/bound/typescript/src/crypto/signer.ts @@ -1,4 +1,4 @@ -import wasm from "./wasm"; +import wasm from "../wasm"; export type Signer = { sign: (payload: Uint8Array) => Uint8Array; diff --git a/bound/typescript/src/bearer-did.ts b/bound/typescript/src/dids/bearer-did.ts similarity index 55% rename from bound/typescript/src/bearer-did.ts rename to bound/typescript/src/dids/bearer-did.ts index fe19cb67..49bcaeeb 100644 --- a/bound/typescript/src/bearer-did.ts +++ b/bound/typescript/src/dids/bearer-did.ts @@ -1,9 +1,11 @@ -import { tbdexError } from "./errors"; -import { KeyManager } from "./key-managers"; import { PortableDid } from "./portable-did"; -import { Signer } from "./signers"; -import wasm from "./wasm"; -import { Did, Document } from "./wasm/generated-mappings"; +import wasm from "../wasm"; +import { Document } from "./document"; +import { KeyManager } from "../crypto/key-manager"; +import { tbdexError } from "../errors"; +import { InMemoryKeyManager } from "../crypto/in-memory-key-manager"; +import { Signer } from "../crypto/signer"; +import { Did } from "./did"; export class BearerDid { readonly did: Did; @@ -16,23 +18,11 @@ export class BearerDid { this.keyManager = keyManager; } - static fromWASM = (wasmBearerDid: wasm.WasmBearerDid): BearerDid => { - try { - return new BearerDid( - Did.fromWASM(wasmBearerDid.did), - Document.fromWASM(wasmBearerDid.document), - KeyManager.fromWASM(wasmBearerDid.key_manager) - ); - } catch (error) { - throw tbdexError(error); - } - }; - toWASM = (): wasm.WasmBearerDid => { try { return new wasm.WasmBearerDid( - Did.toWASM(this.did), - Document.toWASM(this.document), + this.did.uri, + JSON.stringify(this.document), KeyManager.toWASM(this.keyManager) ); } catch (error) { @@ -42,9 +32,14 @@ export class BearerDid { static fromPortableDID = (portableDID: PortableDid): BearerDid => { try { - return BearerDid.fromWASM( - wasm.WasmBearerDid.from_portable_did(portableDID.toWASM()) + const did = Did.parse(portableDID.uri); + + const keyManager = new InMemoryKeyManager(); + portableDID.privateKeys.forEach((privateJwk) => + keyManager.importPrivateJwk(privateJwk) ); + + return new BearerDid(did, portableDID.document, keyManager); } catch (error) { throw tbdexError(error); } diff --git a/bound/typescript/src/dids/did.ts b/bound/typescript/src/dids/did.ts new file mode 100644 index 00000000..b09e372a --- /dev/null +++ b/bound/typescript/src/dids/did.ts @@ -0,0 +1,20 @@ +import wasm from "../wasm"; + +export type Did = { + fragment?: string; + id: string; + method: string; + params?: any; + path?: string; + query?: string; + uri: string; + url: string; +}; + +export namespace Did { + export const parse = (uri: string): Did => { + const did_json = wasm.parse_did(uri); + const did: Did = JSON.parse(did_json); + return did; + }; +} diff --git a/bound/typescript/src/dids/document.ts b/bound/typescript/src/dids/document.ts new file mode 100644 index 00000000..5855efbb --- /dev/null +++ b/bound/typescript/src/dids/document.ts @@ -0,0 +1,28 @@ +import { Jwk } from "../crypto/jwk"; + +export type Document = { + alsoKnownAs?: string[]; + assertionMethod?: string[]; + authentication?: string[]; + capabilityDelegation?: string[]; + capabilityInvocation?: string[]; + context?: string[]; + controller?: string[]; + id: string; + keyAgreement?: string[]; + service?: Service[]; + verificationMethod: VerificationMethod[]; +}; + +export type Service = { + id: string; + serviceEndpoint: string[]; + type: string; +}; + +export type VerificationMethod = { + controller: string; + id: string; + publicKeyJwk: Jwk; + type: string; +}; diff --git a/bound/typescript/src/dids/portable-did.ts b/bound/typescript/src/dids/portable-did.ts new file mode 100644 index 00000000..bbeed6a6 --- /dev/null +++ b/bound/typescript/src/dids/portable-did.ts @@ -0,0 +1,8 @@ +import { Jwk } from "../crypto/jwk"; +import { Document } from "./document"; + +export type PortableDid = { + uri: string; + document: Document; + privateKeys: Jwk[]; +}; diff --git a/bound/typescript/src/http-client/balances.ts b/bound/typescript/src/http-client/balances.ts index a9d3a24e..e158a3f6 100644 --- a/bound/typescript/src/http-client/balances.ts +++ b/bound/typescript/src/http-client/balances.ts @@ -1,4 +1,4 @@ -import { BearerDid } from "../bearer-did"; +import { BearerDid } from "../dids/bearer-did"; import { Balance } from "../resources/balance"; import wasm from "../wasm"; diff --git a/bound/typescript/src/http-client/exchanges.ts b/bound/typescript/src/http-client/exchanges.ts index 3cefe341..4679c65c 100644 --- a/bound/typescript/src/http-client/exchanges.ts +++ b/bound/typescript/src/http-client/exchanges.ts @@ -1,5 +1,4 @@ -import { BearerDid } from "../bearer-did"; -import { tbdexError } from "../errors"; +import { BearerDid } from "../dids/bearer-did"; import { Cancel } from "../messages/cancel"; import { Close } from "../messages/close"; import { Order } from "../messages/order"; diff --git a/bound/typescript/src/key-managers.ts b/bound/typescript/src/key-managers.ts deleted file mode 100644 index b198cb88..00000000 --- a/bound/typescript/src/key-managers.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { Signer } from "./signers"; -import wasm from "./wasm"; -import { Jwk } from "./wasm/generated-mappings"; - -export type KeyManager = { - importPrivateJwk(privateJwk: Jwk): Jwk; - getSigner(publicJwk: Jwk): Signer; -}; - -export namespace KeyManager { - export const toWASM = (keyManager: KeyManager): wasm.WasmKeyManager => { - const foreignKeyManager = { - import_private_jwk: (privateJwk: wasm.WasmJwk): wasm.WasmJwk => { - const publicJwk = keyManager.importPrivateJwk(Jwk.fromWASM(privateJwk)); - return Jwk.toWASM(publicJwk); - }, - get_signer: (publicJwk: wasm.WasmJwk): wasm.WasmSigner => { - return Signer.toWASM(keyManager.getSigner(Jwk.fromWASM(publicJwk))); - }, - }; - - return new wasm.WasmKeyManager(foreignKeyManager); - }; - - export const fromWASM = (wasmKeyManager: wasm.WasmKeyManager): KeyManager => { - const keyManager: KeyManager = { - importPrivateJwk: (privateJwk: Jwk): Jwk => { - const wasmPublicJwk = wasmKeyManager.import_private_jwk( - Jwk.toWASM(privateJwk) - ); - return Jwk.fromWASM(wasmPublicJwk); - }, - getSigner: (publicJwk: Jwk): Signer => { - const wasmSigner = wasmKeyManager.get_signer(Jwk.toWASM(publicJwk)); - return Signer.fromWASM(wasmSigner); - }, - }; - - return keyManager; - }; -} diff --git a/bound/typescript/src/messages/cancel.ts b/bound/typescript/src/messages/cancel.ts index 5aea40eb..7bad2744 100644 --- a/bound/typescript/src/messages/cancel.ts +++ b/bound/typescript/src/messages/cancel.ts @@ -1,5 +1,5 @@ import { MessageMetadata } from "."; -import { BearerDid } from "../bearer-did"; +import { BearerDid } from "../dids/bearer-did"; import { tbdexError } from "../errors"; import wasm from "../wasm"; diff --git a/bound/typescript/src/messages/close.ts b/bound/typescript/src/messages/close.ts index 38aa3ad5..17fd1b00 100644 --- a/bound/typescript/src/messages/close.ts +++ b/bound/typescript/src/messages/close.ts @@ -1,5 +1,5 @@ import { MessageMetadata } from "."; -import { BearerDid } from "../bearer-did"; +import { BearerDid } from "../dids/bearer-did"; import { tbdexError } from "../errors"; import wasm from "../wasm"; diff --git a/bound/typescript/src/messages/order-instructions.ts b/bound/typescript/src/messages/order-instructions.ts index de9b3bed..4f2799e7 100644 --- a/bound/typescript/src/messages/order-instructions.ts +++ b/bound/typescript/src/messages/order-instructions.ts @@ -1,5 +1,5 @@ import { MessageMetadata } from "."; -import { BearerDid } from "../bearer-did"; +import { BearerDid } from "../dids/bearer-did"; import { tbdexError } from "../errors"; import wasm from "../wasm"; diff --git a/bound/typescript/src/messages/order-status.ts b/bound/typescript/src/messages/order-status.ts index b36ee730..983059ed 100644 --- a/bound/typescript/src/messages/order-status.ts +++ b/bound/typescript/src/messages/order-status.ts @@ -1,5 +1,5 @@ import { MessageMetadata } from "."; -import { BearerDid } from "../bearer-did"; +import { BearerDid } from "../dids/bearer-did"; import { tbdexError } from "../errors"; import wasm from "../wasm"; diff --git a/bound/typescript/src/messages/order.ts b/bound/typescript/src/messages/order.ts index c74948ec..79152ca5 100644 --- a/bound/typescript/src/messages/order.ts +++ b/bound/typescript/src/messages/order.ts @@ -1,5 +1,5 @@ import { MessageMetadata } from "."; -import { BearerDid } from "../bearer-did"; +import { BearerDid } from "../dids/bearer-did"; import { tbdexError } from "../errors"; import wasm from "../wasm"; diff --git a/bound/typescript/src/messages/quote.ts b/bound/typescript/src/messages/quote.ts index 54ff2281..d2717316 100644 --- a/bound/typescript/src/messages/quote.ts +++ b/bound/typescript/src/messages/quote.ts @@ -1,5 +1,5 @@ import { MessageMetadata } from "."; -import { BearerDid } from "../bearer-did"; +import { BearerDid } from "../dids/bearer-did"; import { tbdexError } from "../errors"; import wasm from "../wasm"; diff --git a/bound/typescript/src/messages/rfq.ts b/bound/typescript/src/messages/rfq.ts index 02067077..ce367bbf 100644 --- a/bound/typescript/src/messages/rfq.ts +++ b/bound/typescript/src/messages/rfq.ts @@ -1,5 +1,5 @@ import { MessageMetadata } from "."; -import { BearerDid } from "../bearer-did"; +import { BearerDid } from "../dids/bearer-did"; import { tbdexError } from "../errors"; import { Offering } from "../resources/offering"; import wasm from "../wasm"; diff --git a/bound/typescript/src/portable-did.ts b/bound/typescript/src/portable-did.ts deleted file mode 100644 index 2159fc46..00000000 --- a/bound/typescript/src/portable-did.ts +++ /dev/null @@ -1,38 +0,0 @@ -import wasm from "./wasm"; -import { Document, Jwk } from "./wasm/generated-mappings"; - -export class PortableDid { - readonly uri: string; - readonly document: Document; - readonly privateKeys: Jwk[]; - - constructor(uri: string, document: Document, privateKeys: Jwk[]) { - this.uri = uri; - this.document = document; - this.privateKeys = privateKeys; - } - - static fromWASM = (wasmPortableDid: wasm.WasmPortableDid): PortableDid => { - return new PortableDid( - wasmPortableDid.did_uri, - Document.fromWASM(wasmPortableDid.document), - wasmPortableDid.private_keys.map((x) => Jwk.fromWASM(x)) - ); - }; - - toWASM = (): wasm.WasmPortableDid => { - return new wasm.WasmPortableDid( - this.uri, - Document.toWASM(this.document), - this.privateKeys.map((x) => Jwk.toWASM(x)) - ); - }; - - static fromJSONString = (json: string): PortableDid => { - return PortableDid.fromWASM(wasm.WasmPortableDid.from_json_string(json)); - }; - - toJSONString = (): string => { - return this.toWASM().to_json_string(); - }; -} diff --git a/bound/typescript/src/resources/balance.ts b/bound/typescript/src/resources/balance.ts index 3143dd7d..cb8b1203 100644 --- a/bound/typescript/src/resources/balance.ts +++ b/bound/typescript/src/resources/balance.ts @@ -1,5 +1,5 @@ import { ResourceMetadata } from "."; -import { BearerDid } from "../bearer-did"; +import { BearerDid } from "../dids/bearer-did"; import { tbdexError } from "../errors"; import wasm from "../wasm"; diff --git a/bound/typescript/src/resources/offering.ts b/bound/typescript/src/resources/offering.ts index 953b4914..f0214dfd 100644 --- a/bound/typescript/src/resources/offering.ts +++ b/bound/typescript/src/resources/offering.ts @@ -1,8 +1,8 @@ import { ResourceMetadata } from "."; -import { BearerDid } from "../bearer-did"; import { tbdexError } from "../errors"; -import { PresentationDefinition } from "../presentation-definition"; +import { PresentationDefinition } from "../credentials/presentation-definition"; import wasm from "../wasm"; +import { BearerDid } from "../dids/bearer-did"; export class Offering { readonly metadata: ResourceMetadata; diff --git a/bound/typescript/src/wasm/foreign-fetch.ts b/bound/typescript/src/wasm/foreign-fetch.ts index efbcf29e..aead0137 100644 --- a/bound/typescript/src/wasm/foreign-fetch.ts +++ b/bound/typescript/src/wasm/foreign-fetch.ts @@ -1,15 +1,10 @@ import wasm from "."; -import { FetchOptions, Response } from "./generated-mappings"; export const ForeignFetch = { fetch: async ( url: string, - wasmFetchOptions?: wasm.WasmFetchOptions + options?: wasm.WasmFetchOptions ): Promise => { - const options = wasmFetchOptions - ? FetchOptions.fromWASM(wasmFetchOptions) - : undefined; - const fetchResponse = await fetch(url, { method: options?.method || "GET", headers: options?.headers, @@ -22,12 +17,6 @@ export const ForeignFetch = { headers[key] = value; }); - const response: Response = { - statusCode: fetchResponse.status, - body: body, - headers: headers, - }; - - return Response.toWASM(response); + return new wasm.WasmResponse(fetchResponse.status, headers, body); }, }; diff --git a/bound/typescript/src/wasm/generated-mappings.ts b/bound/typescript/src/wasm/generated-mappings.ts deleted file mode 100644 index 1a905ddf..00000000 --- a/bound/typescript/src/wasm/generated-mappings.ts +++ /dev/null @@ -1,253 +0,0 @@ -import wasm from "./"; - -export type Did = { - fragment?: string; - id: string; - method: string; - params?: any; - path?: string; - query?: string; - uri: string; - url: string; -}; - -export namespace Did { - export const toWASM = (obj: Did): wasm.WasmDid => { - return new wasm.WasmDid( - obj.uri, - obj.url, - obj.method, - obj.id, - obj.params, - obj.path, - obj.query, - obj.fragment, - ); - }; - - export const fromWASM = (obj: wasm.WasmDid): Did => { - const result: Did = { - id: obj.id, - method: obj.method, - uri: obj.uri, - url: obj.url, - }; - - if (obj.fragment !== undefined) result.fragment = obj.fragment; - if (obj.params !== undefined) result.params = obj.params; - if (obj.path !== undefined) result.path = obj.path; - if (obj.query !== undefined) result.query = obj.query; - - return result; - }; -} - -export type Document = { - alsoKnownAs?: string[]; - assertionMethod?: string[]; - authentication?: string[]; - capabilityDelegation?: string[]; - capabilityInvocation?: string[]; - context?: string[]; - controller?: string[]; - id: string; - keyAgreement?: string[]; - service?: Service[]; - verificationMethod: VerificationMethod[]; -}; - -export namespace Document { - export const toWASM = (obj: Document): wasm.WasmDocument => { - return new wasm.WasmDocument( - obj.id, - obj.context, - obj.controller, - obj.alsoKnownAs, - obj.verificationMethod?.map(VerificationMethod.toWASM), - obj.authentication, - obj.assertionMethod, - obj.keyAgreement, - obj.capabilityInvocation, - obj.capabilityDelegation, - obj.service?.map(Service.toWASM), - ); - }; - - export const fromWASM = (obj: wasm.WasmDocument): Document => { - const result: Document = { - id: obj.id, - verificationMethod: obj.verification_method?.map( - VerificationMethod.fromWASM, - ), - }; - - if (obj.also_known_as !== undefined) result.alsoKnownAs = obj.also_known_as; - if (obj.assertion_method !== undefined) - result.assertionMethod = obj.assertion_method; - if (obj.authentication !== undefined) - result.authentication = obj.authentication; - if (obj.capability_delegation !== undefined) - result.capabilityDelegation = obj.capability_delegation; - if (obj.capability_invocation !== undefined) - result.capabilityInvocation = obj.capability_invocation; - if (obj.context !== undefined) result.context = obj.context; - if (obj.controller !== undefined) result.controller = obj.controller; - if (obj.key_agreement !== undefined) - result.keyAgreement = obj.key_agreement; - if (obj.service !== undefined) - result.service = obj.service?.map(Service.fromWASM); - - return result; - }; -} - -export type FetchOptions = { - body?: Uint8Array; - headers?: any; - method?: string; -}; - -export namespace FetchOptions { - export const toWASM = (obj: FetchOptions): wasm.WasmFetchOptions => { - return new wasm.WasmFetchOptions(obj.method, obj.headers, obj.body); - }; - - export const fromWASM = (obj: wasm.WasmFetchOptions): FetchOptions => { - const result: FetchOptions = {}; - - if (obj.body !== undefined) result.body = obj.body; - if (obj.headers !== undefined) result.headers = obj.headers; - if (obj.method !== undefined) result.method = obj.method; - - return result; - }; -} - -export type Jwk = { - alg?: string; - crv: string; - d?: string; - kty: string; - x: string; - y?: string; -}; - -export namespace Jwk { - export const toWASM = (obj: Jwk): wasm.WasmJwk => { - return new wasm.WasmJwk(obj.alg, obj.kty, obj.crv, obj.d, obj.x, obj.y); - }; - - export const fromWASM = (obj: wasm.WasmJwk): Jwk => { - const result: Jwk = { - crv: obj.crv, - kty: obj.kty, - x: obj.x, - }; - - if (obj.alg !== undefined) result.alg = obj.alg; - if (obj.d !== undefined) result.d = obj.d; - if (obj.y !== undefined) result.y = obj.y; - - return result; - }; -} - -export type Response = { - body: Uint8Array; - headers?: any; - statusCode: number; -}; - -export namespace Response { - export const toWASM = (obj: Response): wasm.WasmResponse => { - return new wasm.WasmResponse(obj.statusCode, obj.headers, obj.body); - }; - - export const fromWASM = (obj: wasm.WasmResponse): Response => { - const result: Response = { - body: obj.body, - statusCode: obj.status_code, - }; - - if (obj.headers !== undefined) result.headers = obj.headers; - - return result; - }; -} - -export type Service = { - id: string; - serviceEndpoint: string[]; - type: string; -}; - -export namespace Service { - export const toWASM = (obj: Service): wasm.WasmService => { - return new wasm.WasmService(obj.id, obj.type, obj.serviceEndpoint); - }; - - export const fromWASM = (obj: wasm.WasmService): Service => { - const result: Service = { - id: obj.id, - serviceEndpoint: obj.service_endpoint, - type: obj.type, - }; - - return result; - }; -} - -export type TbdexError = { - isWeb5Error: boolean; - message: string; - variant: string; -}; - -export namespace TbdexError { - export const toWASM = (obj: TbdexError): wasm.WasmTbdexError => { - return new wasm.WasmTbdexError(); - }; - - export const fromWASM = (obj: wasm.WasmTbdexError): TbdexError => { - const result: TbdexError = { - isWeb5Error: obj.is_web5_error, - message: obj.message, - variant: obj.variant, - }; - - return result; - }; -} - -export type VerificationMethod = { - controller: string; - id: string; - publicKeyJwk: Jwk; - type: string; -}; - -export namespace VerificationMethod { - export const toWASM = ( - obj: VerificationMethod, - ): wasm.WasmVerificationMethod => { - return new wasm.WasmVerificationMethod( - obj.id, - obj.type, - obj.controller, - Jwk.toWASM(obj.publicKeyJwk), - ); - }; - - export const fromWASM = ( - obj: wasm.WasmVerificationMethod, - ): VerificationMethod => { - const result: VerificationMethod = { - controller: obj.controller, - id: obj.id, - publicKeyJwk: Jwk.fromWASM(obj.public_key_jwk), - type: obj.type, - }; - - return result; - }; -} diff --git a/bound/typescript/tests/test-vectors.test.ts b/bound/typescript/tests/test-vectors.test.ts index 11b367a9..eff29886 100644 --- a/bound/typescript/tests/test-vectors.test.ts +++ b/bound/typescript/tests/test-vectors.test.ts @@ -20,8 +20,6 @@ import ReplyToRequestBodyOrderStatusVector from "./vectors/http/exchanges/reply- import ReplyToRequestBodyCloseVector from "./vectors/http/exchanges/reply-to-close.json" assert { type: "json" }; import GetOfferingsResponseBodyVector from "./vectors/http/get-offerings.json" assert { type: "json" }; import { Offering } from "../src/resources/offering"; -import { PortableDid } from "../src/portable-did"; -import { BearerDid } from "../src/bearer-did"; import { Balance } from "../src/resources/balance"; import { CreateRfqData, Rfq } from "../src/messages/rfq"; import { Quote } from "../src/messages/quote"; @@ -40,16 +38,17 @@ import { UpdateExchangeRequestBody, } from "../src/http/exchanges"; import { GetOfferingsResponseBody } from "../src/http/offerings"; +import { BearerDid } from "../src/dids/bearer-did"; +import { PortableDid } from "../src/dids/portable-did"; describe("test vectors", () => { let bearerDID: BearerDid; before(() => { - bearerDID = BearerDid.fromPortableDID( - PortableDid.fromJSONString( - '{"uri":"did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0","document":{"id":"did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0","@context":["https://www.w3.org/ns/did/v1"],"verificationMethod":[{"id":"did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0#0","type":"JsonWebKey","controller":"did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0","publicKeyJwk":{"alg":"Ed25519","kty":"OKP","crv":"Ed25519","x":"iAuxCoarRaizG1ZH0zajTkrb_Pk-7zM-zFW4DA8AK5M"}}],"authentication":["did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0#0"],"assertionMethod":["did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0#0"],"capabilityInvocation":["did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0#0"],"capabilityDelegation":["did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0#0"]},"privateKeys":[{"alg":"Ed25519","kty":"OKP","crv":"Ed25519","d":"GSd8aUVcNX9O8ipqOV2gXJToHyzUZ_8mJrQ7G5UsmHs","x":"iAuxCoarRaizG1ZH0zajTkrb_Pk-7zM-zFW4DA8AK5M"}]}' - ) + const portableDID: PortableDid = JSON.parse( + '{"uri":"did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0","document":{"id":"did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0","@context":["https://www.w3.org/ns/did/v1"],"verificationMethod":[{"id":"did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0#0","type":"JsonWebKey","controller":"did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0","publicKeyJwk":{"alg":"Ed25519","kty":"OKP","crv":"Ed25519","x":"iAuxCoarRaizG1ZH0zajTkrb_Pk-7zM-zFW4DA8AK5M"}}],"authentication":["did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0#0"],"assertionMethod":["did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0#0"],"capabilityInvocation":["did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0#0"],"capabilityDelegation":["did:jwk:eyJhbGciOiJFZDI1NTE5Iiwia3R5IjoiT0tQIiwiY3J2IjoiRWQyNTUxOSIsIngiOiJpQXV4Q29hclJhaXpHMVpIMHphalRrcmJfUGstN3pNLXpGVzREQThBSzVNIn0#0"]},"privateKeys":[{"alg":"Ed25519","kty":"OKP","crv":"Ed25519","d":"GSd8aUVcNX9O8ipqOV2gXJToHyzUZ_8mJrQ7G5UsmHs","x":"iAuxCoarRaizG1ZH0zajTkrb_Pk-7zM-zFW4DA8AK5M"}]}' ); + bearerDID = BearerDid.fromPortableDID(portableDID); }); describe("resources", () => {