generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add wasm foundation #363
Merged
Merged
Add wasm foundation #363
Changes from 30 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
653c245
Add new web5_wasm crate, add Justfile recipes
KendallWeihe bde8074
Consolidate http calls
KendallWeihe 5a34fa3
Fix jsonschema depedency
KendallWeihe cbf783a
Compile away reqwest for wasm
KendallWeihe a19a572
Add global http client var
KendallWeihe 8bbce34
Add Jwk wasm binding
KendallWeihe 74d18e9
Stub in starter typescript project
KendallWeihe cb52e21
Redo typescript project following girlmath
KendallWeihe a577e69
Remove unnecessary comments
KendallWeihe b214745
Add assertion to test
KendallWeihe 08ba611
Add typescript, add wasm build, call from test
KendallWeihe 4199934
Bind Jwk and Web5Error
KendallWeihe ea38ad5
Add discerning property to Web5Error
KendallWeihe d862b15
Prototype foreign function calls embedded within a type
KendallWeihe 43a4ec9
Prototype foreign signer
KendallWeihe 1cf32fa
Bind Ed25519 generator and signer
KendallWeihe 89b4ad9
Bind InMemoryKeyManager
KendallWeihe 20223f6
Make jose public
KendallWeihe e640319
Add Jws
KendallWeihe 852e43a
Fix detached typo
KendallWeihe 4897174
Add aud to JwtClaims, add Default to JwtClaims
KendallWeihe 6c512e6
Fix aud to array of strings
KendallWeihe d8504f0
Add eddsa to Dsa from_str
KendallWeihe 1b42d19
Match off crv not alg, fix kt tests
KendallWeihe 80f2161
Fix presentation definition casing bug
KendallWeihe 78d865f
Fix kt PD json casing
KendallWeihe 1366524
Fix PD null serialization
KendallWeihe 544a3b0
Restore LICENSE
KendallWeihe bd45c64
Uninstall pnpm
KendallWeihe 103dbef
Restore pom.xml
KendallWeihe 51070e6
Remove poc code
KendallWeihe e85bd45
Restore commented test assertion
KendallWeihe 720212a
Fix gitignore for LICENSE
KendallWeihe ed7f439
Merge branch 'main' into kendall/bind-wasm-starter
KendallWeihe a3ba3ea
Add http-std
KendallWeihe ef63168
Remove unnecessary comment
KendallWeihe 4a5787f
Fix linting, fix Dockerfile builds
KendallWeihe c08932a
Add stream flush and 60 sec timeout
KendallWeihe 4d58a72
Add ReqwestClient, fix kt tests
KendallWeihe bcec3d2
Fix docker builds
KendallWeihe 68c2005
Fix musl builds
KendallWeihe 8c21361
Fix wasm build
KendallWeihe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
.node-20.9.0.pkg | ||
.node-20.15.1.pkg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
.node-20.9.0.pkg | ||
.node-20.15.1.pkg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
.node-20.9.0.pkg | ||
.node-20.15.1.pkg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
.node-20.9.0.pkg | ||
.node-20.15.1.pkg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "web5_wasm" | ||
version = "0.1.0" | ||
edition = "2021" | ||
homepage.workspace = true | ||
repository.workspace = true | ||
license-file.workspace = true | ||
|
||
[dependencies] | ||
serde = { workspace = true } | ||
serde-wasm-bindgen = "0.6.5" | ||
wasm-bindgen = "0.2.93" | ||
web5 = { path = "../../crates/web5" } | ||
|
||
[lib] | ||
crate-type = ["cdylib"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use super::jwk::WasmJwk; | ||
use crate::errors::{map_err, Result}; | ||
use std::sync::Arc; | ||
use wasm_bindgen::prelude::wasm_bindgen; | ||
use web5::crypto::dsa::{ | ||
ed25519::{Ed25519Generator, Ed25519Signer}, | ||
secp256k1::{Secp256k1Generator, Secp256k1Signer}, | ||
Signer, | ||
}; | ||
|
||
#[wasm_bindgen] | ||
pub struct WasmSigner { | ||
inner: Arc<dyn Signer>, | ||
} | ||
|
||
impl From<Ed25519Signer> for WasmSigner { | ||
fn from(value: Ed25519Signer) -> Self { | ||
Self { | ||
inner: Arc::new(value), | ||
} | ||
} | ||
} | ||
|
||
impl From<Secp256k1Signer> for WasmSigner { | ||
fn from(value: Secp256k1Signer) -> Self { | ||
Self { | ||
inner: Arc::new(value), | ||
} | ||
} | ||
} | ||
|
||
impl From<Arc<dyn Signer>> for WasmSigner { | ||
fn from(value: Arc<dyn Signer>) -> Self { | ||
Self { inner: value } | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl WasmSigner { | ||
pub fn sign(&self, payload: &[u8]) -> Result<Vec<u8>> { | ||
self.inner.sign(payload).map_err(map_err) | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn generate_ed25519_key() -> Result<WasmJwk> { | ||
Ok(Ed25519Generator::generate().into()) | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn generate_secp256k1_key() -> Result<WasmJwk> { | ||
Ok(Secp256k1Generator::generate().into()) | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn new_ed25519_signer(jwk: WasmJwk) -> Result<WasmSigner> { | ||
Ok(Ed25519Signer::new(jwk.into()).into()) | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn new_secp256k1_signer(jwk: WasmJwk) -> Result<WasmSigner> { | ||
Ok(Secp256k1Signer::new(jwk.into()).into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use crate::errors::{map_err, Result}; | ||
use wasm_bindgen::prelude::wasm_bindgen; | ||
use web5::crypto::jwk::Jwk; | ||
|
||
#[wasm_bindgen] | ||
pub struct WasmJwk { | ||
inner: Jwk, | ||
} | ||
|
||
impl From<Jwk> for WasmJwk { | ||
fn from(value: Jwk) -> Self { | ||
Self { inner: value } | ||
} | ||
} | ||
|
||
impl From<WasmJwk> for Jwk { | ||
fn from(value: WasmJwk) -> Self { | ||
value.inner | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl WasmJwk { | ||
#[wasm_bindgen(constructor)] | ||
pub fn new( | ||
alg: Option<String>, | ||
kty: String, | ||
crv: String, | ||
d: Option<String>, | ||
x: String, | ||
y: Option<String>, | ||
) -> WasmJwk { | ||
WasmJwk { | ||
inner: Jwk { | ||
alg, | ||
kty, | ||
crv, | ||
d, | ||
x, | ||
y, | ||
}, | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn compute_thumbprint(&self) -> Result<String> { | ||
self.inner.compute_thumbprint().map_err(map_err) | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn alg(&self) -> Option<String> { | ||
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<String> { | ||
self.inner.d.clone() | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn x(&self) -> String { | ||
self.inner.x.clone() | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn y(&self) -> Option<String> { | ||
self.inner.y.clone() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use super::{dsa::WasmSigner, jwk::WasmJwk}; | ||
use crate::errors::{map_err, Result}; | ||
use std::sync::Arc; | ||
use wasm_bindgen::prelude::wasm_bindgen; | ||
use web5::crypto::{ | ||
jwk::Jwk, | ||
key_managers::{in_memory_key_manager::InMemoryKeyManager, KeyManager}, | ||
}; | ||
|
||
#[wasm_bindgen] | ||
pub struct WasmKeyManager { | ||
inner: Arc<dyn KeyManager>, | ||
} | ||
|
||
impl From<InMemoryKeyManager> for WasmKeyManager { | ||
fn from(value: InMemoryKeyManager) -> Self { | ||
Self { | ||
inner: Arc::new(value), | ||
} | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl WasmKeyManager { | ||
pub fn import_private_jwk(&self, private_jwk: WasmJwk) -> Result<WasmJwk> { | ||
Ok(self | ||
.inner | ||
.import_private_jwk(private_jwk.into()) | ||
.map_err(map_err)? | ||
.into()) | ||
} | ||
|
||
pub fn get_signer(&self, public_jwk: WasmJwk) -> Result<WasmSigner> { | ||
Ok(self | ||
.inner | ||
.get_signer(public_jwk.into()) | ||
.map_err(map_err)? | ||
.into()) | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn new_in_memory_key_manager() -> Result<WasmKeyManager> { | ||
Ok(InMemoryKeyManager::new().into()) | ||
} | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here's an example of a foreign implementation, for which we'll use this same pattern for a foreign |
||
#[wasm_bindgen( | ||
typescript_type = "{ importPrivateJwk: (privateJwk: WasmJwk) => WasmJwk, getSigner: (publicJwk: WasmJwk) => WasmSigner }" | ||
)] | ||
pub type ForeignKeyManager; | ||
|
||
#[wasm_bindgen(method)] | ||
fn import_private_jwk(this: &ForeignKeyManager, private_jwk: WasmJwk) -> WasmJwk; | ||
|
||
#[wasm_bindgen(method)] | ||
fn get_signer(this: &ForeignKeyManager, public_jwk: WasmJwk) -> WasmSigner; | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn poc_key_manager_from_foreign(key_manager: &ForeignKeyManager) -> WasmSigner { | ||
let private_jwk = Jwk { | ||
alg: Some("Ed25519".to_string()), | ||
kty: "OKP".to_string(), | ||
crv: "Ed25519".to_string(), | ||
d: Some("UMxzGsW84I6kS3JkenqYI1gH0GmvxYG2ovI69Vlno8g".to_string()), | ||
x: "EzbXpICojY4ZI2i775GwkkTIbe5nuLL13JbdzUfsO6Q".to_string(), | ||
y: None, | ||
}; | ||
|
||
let public_jwk = key_manager.import_private_jwk(private_jwk.into()); | ||
let signer = key_manager.get_signer(public_jwk); | ||
signer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod dsa; | ||
pub mod jwk; | ||
pub mod key_managers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use serde::Serialize; | ||
use serde_wasm_bindgen::to_value; | ||
use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; | ||
use web5::errors::Web5Error; | ||
|
||
pub type Result<T> = std::result::Result<T, JsValue>; | ||
|
||
#[wasm_bindgen] | ||
#[derive(Serialize)] | ||
pub struct WasmWeb5Error { | ||
variant: String, | ||
message: String, | ||
is_web5_error: bool, | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl WasmWeb5Error { | ||
#[wasm_bindgen(getter)] | ||
pub fn variant(&self) -> String { | ||
self.variant.clone() | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn message(&self) -> String { | ||
self.message.clone() | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn is_web5_error(&self) -> bool { | ||
self.is_web5_error.clone() | ||
} | ||
} | ||
|
||
pub fn map_err(err: Web5Error) -> JsValue { | ||
let msg = format!("{:?}", err); | ||
let variant = msg.split('(').next().unwrap_or("Unknown").to_string(); | ||
|
||
let js_error = WasmWeb5Error { | ||
variant, | ||
message: err.to_string(), | ||
is_web5_error: true, | ||
}; | ||
|
||
to_value(&js_error).unwrap_or_else(|_| JsValue::from_str("failed to serialize error")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
pub mod crypto; | ||
|
||
pub mod errors; | ||
|
||
use wasm_bindgen::prelude::*; | ||
|
||
// #[wasm_bindgen] | ||
// extern "C" { | ||
// pub type JsFunction; | ||
|
||
// #[wasm_bindgen(method)] | ||
// fn call(this: &JsFunction); | ||
// } | ||
|
||
// #[wasm_bindgen] | ||
// pub fn call_js_function(f: &JsFunction) { | ||
// f.call(); | ||
// } | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
#[wasm_bindgen(typescript_type = "{hello1: Function, hello2: Function}")] | ||
pub type JsObject1; | ||
|
||
#[wasm_bindgen(method)] | ||
fn hello1(this: &JsObject1); | ||
|
||
#[wasm_bindgen(method)] | ||
fn hello2(this: &JsObject1); | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn call_js_functions(obj: &JsObject1) { | ||
obj.hello1(); | ||
obj.hello2(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@diehuxx I know this is no bueno but thinking of leaving it for now. I had to do it because of the
wasm-pack
version we're using... though we could look into moving to a lowerwasm-pack
I'm not sure what the consequences would be. Lmk your thoughts.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine. We CI job that checks if the web5 crate has MSRV 1.74. The CI job checks
crates/web5/Cargo.toml
to getrust-version = 1.74
, so as long as we don't change that, we're fine.The only drawback of using a higher rust version during development is that we may accidentally use features in
crates/web5
that aren't supported in version 1.74. If that happens, we'll just notice them in the CI job failure and fix afterward.