Skip to content
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 42 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 Sep 19, 2024
bde8074
Consolidate http calls
KendallWeihe Sep 19, 2024
5a34fa3
Fix jsonschema depedency
KendallWeihe Sep 19, 2024
cbf783a
Compile away reqwest for wasm
KendallWeihe Sep 19, 2024
a19a572
Add global http client var
KendallWeihe Sep 19, 2024
8bbce34
Add Jwk wasm binding
KendallWeihe Sep 19, 2024
74d18e9
Stub in starter typescript project
KendallWeihe Sep 19, 2024
cb52e21
Redo typescript project following girlmath
KendallWeihe Sep 19, 2024
a577e69
Remove unnecessary comments
KendallWeihe Sep 19, 2024
b214745
Add assertion to test
KendallWeihe Sep 19, 2024
08ba611
Add typescript, add wasm build, call from test
KendallWeihe Sep 19, 2024
4199934
Bind Jwk and Web5Error
KendallWeihe Sep 20, 2024
ea38ad5
Add discerning property to Web5Error
KendallWeihe Sep 20, 2024
d862b15
Prototype foreign function calls embedded within a type
KendallWeihe Sep 20, 2024
43a4ec9
Prototype foreign signer
KendallWeihe Sep 20, 2024
1cf32fa
Bind Ed25519 generator and signer
KendallWeihe Sep 20, 2024
89b4ad9
Bind InMemoryKeyManager
KendallWeihe Sep 20, 2024
20223f6
Make jose public
KendallWeihe Sep 20, 2024
e640319
Add Jws
KendallWeihe Sep 20, 2024
852e43a
Fix detached typo
KendallWeihe Sep 20, 2024
4897174
Add aud to JwtClaims, add Default to JwtClaims
KendallWeihe Sep 20, 2024
6c512e6
Fix aud to array of strings
KendallWeihe Sep 20, 2024
d8504f0
Add eddsa to Dsa from_str
KendallWeihe Sep 21, 2024
1b42d19
Match off crv not alg, fix kt tests
KendallWeihe Sep 21, 2024
80f2161
Fix presentation definition casing bug
KendallWeihe Sep 21, 2024
78d865f
Fix kt PD json casing
KendallWeihe Sep 21, 2024
1366524
Fix PD null serialization
KendallWeihe Sep 23, 2024
544a3b0
Restore LICENSE
KendallWeihe Sep 23, 2024
bd45c64
Uninstall pnpm
KendallWeihe Sep 23, 2024
103dbef
Restore pom.xml
KendallWeihe Sep 23, 2024
51070e6
Remove poc code
KendallWeihe Sep 23, 2024
e85bd45
Restore commented test assertion
KendallWeihe Sep 23, 2024
720212a
Fix gitignore for LICENSE
KendallWeihe Sep 23, 2024
ed7f439
Merge branch 'main' into kendall/bind-wasm-starter
KendallWeihe Sep 24, 2024
a3ba3ea
Add http-std
KendallWeihe Sep 24, 2024
ef63168
Remove unnecessary comment
KendallWeihe Sep 24, 2024
4a5787f
Fix linting, fix Dockerfile builds
KendallWeihe Sep 24, 2024
c08932a
Add stream flush and 60 sec timeout
KendallWeihe Sep 24, 2024
4d58a72
Add ReqwestClient, fix kt tests
KendallWeihe Sep 24, 2024
bcec3d2
Fix docker builds
KendallWeihe Sep 24, 2024
68c2005
Fix musl builds
KendallWeihe Sep 24, 2024
8c21361
Fix wasm build
KendallWeihe Sep 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ Cargo.lock

# Do not put native binaries in source control
bound/kt/src/main/resources/*.dylib
bound/kt/src/main/resources/*.so
bound/kt/src/main/resources/*.so

bound/LICENSE
.vscode/
bound/typescript/dist/*
!bound/typescript/dist/index.js
bound/typescript/tests/compiled
bound/typescript/src/wasm/generated.js
15 changes: 8 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[workspace]
members = [
"crates/web5",
"crates/web5_cli",
"bindings/web5_uniffi",
"bindings/web5_uniffi_wrapper",
]
default-members = [
members = [
"bindings/web5_uniffi",
"bindings/web5_uniffi_wrapper",
"bindings/web5_wasm",
"crates/http-std",
"crates/web5",
"crates/web5_cli",
]
default-members = ["crates/web5"]
resolver = "2"

[workspace.package]
Expand All @@ -18,6 +18,7 @@ license-file = "LICENSE"
[workspace.dependencies]
base64 = "0.22.0"
chrono = { version = "0.4.37", features = ["std"] }
lazy_static = "1.5.0"
thiserror = "1.0.50"
rand = "0.8.5"
serde = { version = "1.0.193", features = ["derive"] }
Expand Down
8 changes: 7 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ setup:
git submodule update --init --recursive
fi
if [[ "$(cargo 2>&1)" == *"rustup could not choose a version of cargo to run"* ]]; then
rustup default 1.74.0
rustup default 1.76.0 # TODO undo this
Copy link
Contributor Author

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 lower wasm-pack I'm not sure what the consequences would be. Lmk your thoughts.

Copy link
Contributor

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 get rust-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.

rustup target add aarch64-apple-darwin
fi
if ! command -v wasm-pack >/dev/null || [[ "$(wasm-pack --version)" != "wasm-pack 0.13.0" ]]; then
cargo install wasm-pack --version 0.13.0
fi

docs: setup
cargo doc --open --no-deps
Expand Down Expand Up @@ -46,3 +49,6 @@ test-bound: setup

test-kotlin: setup
cd bound/kt && mvn clean test

wasm: setup
(cd bindings/web5_wasm; wasm-pack build --target nodejs --out-dir ../../bound/typescript/pkg)
File renamed without changes.
2 changes: 1 addition & 1 deletion bin/corepack
2 changes: 1 addition & 1 deletion bin/node
2 changes: 1 addition & 1 deletion bin/npm
2 changes: 1 addition & 1 deletion bin/npx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ WORKDIR /usr/src/myapp
COPY Cargo.toml ./
COPY bindings/web5_uniffi_wrapper ./bindings/web5_uniffi_wrapper
COPY bindings/web5_uniffi ./bindings/web5_uniffi
COPY bindings/web5_wasm ./bindings/web5_wasm
COPY crates/http-std ./crates/http-std
COPY crates/web5 ./crates/web5
COPY crates/web5_cli ./crates/web5_cli

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,30 @@ RUN apk add --no-cache \
git \
perl \
make \
bash
bash \
openssl-dev \
openssl-libs-static # TODO remove above two once we remove reqwest

# Install rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | bash -s -- -y --no-modify-path --default-toolchain 1.74.0
ENV PATH="/root/.cargo/bin:${PATH}"

# TODO remove once we remove reqwest
# Set environment variables to ensure vendored OpenSSL is used
ENV OPENSSL_STATIC=1
ENV OPENSSL_LIB_DIR=/usr/lib
ENV OPENSSL_INCLUDE_DIR=/usr/include
ENV PKG_CONFIG_ALLOW_CROSS=1
ENV PKG_CONFIG_PATH=/usr/lib/pkgconfig
ENV LIBRARY_PATH="/usr/lib:/usr/local/lib"

# Copy the source code to the container
WORKDIR /usr/src/myapp
COPY Cargo.toml ./
COPY bindings/web5_uniffi_wrapper ./bindings/web5_uniffi_wrapper
COPY bindings/web5_uniffi ./bindings/web5_uniffi
COPY bindings/web5_wasm ./bindings/web5_wasm
COPY crates/http-std ./crates/http-std
COPY crates/web5 ./crates/web5
COPY crates/web5_cli ./crates/web5_cli

Expand All @@ -34,7 +47,8 @@ RUN cargo build --release --package web5_uniffi
# Compile as a dynamic lib (.so) from our static lib (.a) while keeping dependencies self-contained
RUN gcc -shared -o target/release/libweb5_uniffi.so -Wl,--whole-archive \
target/release/libweb5_uniffi.a -Wl,--no-whole-archive -static-libgcc \
-Wl,-Bdynamic -fPIC
-L/usr/lib -lssl -lcrypto -Wl,-Bdynamic -fPIC
# -Wl,-Bdynamic -fPIC # TODO replace above line with this line one we remove reqwest

# Set the entrypoint, so that we can `docker cp` the build output
CMD tail -f /dev/null
16 changes: 16 additions & 0 deletions bindings/web5_wasm/Cargo.toml
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"]
63 changes: 63 additions & 0 deletions bindings/web5_wasm/src/crypto/dsa.rs
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())
}
79 changes: 79 additions & 0 deletions bindings/web5_wasm/src/crypto/jwk.rs
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()
}
}
74 changes: 74 additions & 0 deletions bindings/web5_wasm/src/crypto/key_managers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 fetch implementation

#[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());
key_manager.get_signer(public_jwk)
}
3 changes: 3 additions & 0 deletions bindings/web5_wasm/src/crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod dsa;
pub mod jwk;
pub mod key_managers;
Loading
Loading