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

WIP: expose a JS api through WASM. #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 22 additions & 18 deletions ucan-key-support/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,61 +1,65 @@
[package]
name = "ucan-key-support"
description = "Ready to use SigningKey implementations for the ucan crate"
edition = "2021"
keywords = ["ucan", "authz", "jwt", "pki"]
categories = [
"authorization",
"cryptography",
"encoding",
"web-programming"
"web-programming",
]
description = "Ready to use SigningKey implementations for the ucan crate"
documentation = "https://docs.rs/ucan"
repository = "https://github.com/cdata/rs-ucan/"
edition = "2021"
homepage = "https://github.com/cdata/rs-ucan"
keywords = ["ucan", "authz", "jwt", "pki"]
license = "Apache-2.0"
name = "ucan-key-support"
readme = "README.md"
repository = "https://github.com/cdata/rs-ucan/"
version = "0.4.0-alpha.1"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = []
web = ["wasm-bindgen", "wasm-bindgen-futures", "js-sys", "web-sys", "ucan/web", "getrandom/js"]

[dependencies]
ucan = {path = "../ucan", version = "0.6.0-alpha.1" }
anyhow = "1.0.52"
async-trait = "0.1.52"
bs58 = "0.4"
ed25519-zebra = "^3"
log = "0.4"
rsa = "0.6"
sha2 = "0.10"
bs58 = "0.4"
log = "0.4"
ucan = {path = "../ucan", version = "0.6.0-alpha.1"}

[build-dependencies]
npm_rs = "0.2.1"

[dev-dependencies]
rand = "0.8"
# NOTE: This is needed so that rand can be included in WASM builds
getrandom = { version = "0.2.5", features = ["js"] }
getrandom = {version = "0.2.5", features = ["js"]}
tokio = {version = "^1", features = ["macros", "rt"]}
wasm-bindgen-test = "0.3"
tokio = { version = "^1", features = ["macros", "rt"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2", optional = true }
wasm-bindgen-futures = { version = "0.4", optional = true }
js-sys = { version = "0.3", optional = true }
js-sys = {version = "0.3", optional = true}
wasm-bindgen = {version = "0.2", features = ["serde-serialize"], optional = true}
wasm-bindgen-futures = {version = "0.4", optional = true}
wee_alloc = {version = "0.4"}

[target.'cfg(target_arch="wasm32")'.dependencies.web-sys]
version = "0.3"
optional = true
features = [
'Window',
'SubtleCrypto',
'Crypto',
'CryptoKey',
'CryptoKeyPair',
'DedicatedWorkerGlobalScope'
'DedicatedWorkerGlobalScope',
]
optional = true
version = "0.3"

[target.'cfg(target_arch="wasm32")'.dev-dependencies]
pollster = "0.2.5"
pollster = "0.2.5"
1 change: 1 addition & 0 deletions ucan-key-support/demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
static/
8 changes: 8 additions & 0 deletions ucan-key-support/demo/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

cargo build --release --target="wasm32-unknown-unknown" --features=web

wasm-bindgen \
--target web \
--out-dir static \
../../target/wasm32-unknown-unknown/release/ucan_key_support.wasm
79 changes: 79 additions & 0 deletions ucan-key-support/demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<!DOCTYPE html>
<html>
<head>
<title>UCANs on the Web - Powered by rs-ucan</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<script type="module">
import {
WebCryptoRsaKeyMaterial as KeyMaterial,
WasmUcanBuilder as UcanBuilder,
WasmUcan as Ucan,
default as ucanInit,
} from "../static/ucan_key_support.js";

function log_(msg, kind) {
let container = document.getElementById("log");
let node = document.createElement("div");
node.classList.add(kind);
node.textContent = msg;
container.append(node);
}

function log(msg) {
log_(msg, "log");
}

function error(msg) {
log_(msg, "error");
}

async function runTest() {
document.getElementById("log").innerHTML = "";
let aliceKey = await KeyMaterial.generate(2048);
let did = await aliceKey.getDid();
log(`Alice's DID url: ${did}`);
log(`JWT algorithm: ${aliceKey.jwtAlgorithm()}`);

let data = new TextEncoder().encode("Hello World!");
let signature = await aliceKey.sign(data);
try {
await aliceKey.verify(data, signature);
log(`Signature successfully verified`);
} catch (e) {
error(`Signature verification failed: ${e}`);
}

let bobKey = await KeyMaterial.generate(2048);

let builder = new UcanBuilder()
.issuedBy(aliceKey)
.forAudience(bobKey)
.withLifetime(60n);
let signable = builder.build();
let ucan = await signable.sign();
let encoded = ucan.encode();

log(`Encoded ucan: ${encoded}`);

let decodedUcan = Ucan.fromToken(encoded);
log(`Decoded ucan validity: ${await decodedUcan.validate()}`);
log(`Too early? ${decodedUcan.isTooEarly()}`);
}

document.addEventListener("DOMContentLoaded", async () => {
document.getElementById("run-test").addEventListener("click", runTest);

await ucanInit();
});
</script>
<style>
div.error {
color: red;
}
</style>
</head>
<body>
<button id="run-test">Run Test</button>
<pre id="log"></pre>
</body>
</html>
4 changes: 4 additions & 0 deletions ucan-key-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ extern crate log;
#[cfg(all(target_arch = "wasm32", feature = "web"))]
pub mod web_crypto;

#[cfg(all(target_arch = "wasm32", feature = "web"))]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

pub mod ed25519;
pub mod rsa;
6 changes: 2 additions & 4 deletions ucan-key-support/src/rsa.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use anyhow::{anyhow, Result};
use async_trait::async_trait;

use rsa::{
Hash, PaddingScheme, PublicKey, RsaPrivateKey, RsaPublicKey,
};
use rsa::pkcs1::{DecodeRsaPublicKey, EncodeRsaPublicKey};
use rsa::pkcs1::der::{Document, Encodable};
use rsa::pkcs1::{DecodeRsaPublicKey, EncodeRsaPublicKey};
use rsa::{Hash, PaddingScheme, PublicKey, RsaPrivateKey, RsaPublicKey};

use sha2::{Digest, Sha256};
use ucan::crypto::KeyMaterial;
Expand Down
Loading