From 7fee038f5fd833ddfcb1e02b85977fc2cef26e46 Mon Sep 17 00:00:00 2001 From: Taylor Beeston Date: Mon, 16 Nov 2026 15:54:39 -0800 Subject: [PATCH] fix(wasm): enable secp256k1 did:key generation and resolution (LC-2431) didkit-wasm built didkit without the secp256k1 feature, so did-method-key had no secp256k1 support: keyToDID('key', ) failed with 'Unable to generate DID' and did:key:zQ3s... DIDs didn't resolve. Adds a round-trip test (seed bytes -> JWK -> did:key -> resolved key), runs every didkit-wasm lib test in CI instead of only the JWE ones, and documents which DID methods work in the WASM build (did:ion and did:onion don't). --- .github/workflows/build.yml | 4 ++-- lib/web/Cargo.toml | 2 +- lib/web/README.md | 14 ++++++++++++++ lib/web/src/lib.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4ebae029..6b6f12e4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -55,8 +55,8 @@ jobs: - name: Test run: cargo test --locked - - name: Test WASM JWE encryption - run: cargo test --locked -p didkit-wasm --lib jwe::tests + - name: Test WASM library (JWE, did:key) + run: cargo test --locked -p didkit-wasm --lib - name: Test CLI run: cli/tests/example.sh diff --git a/lib/web/Cargo.toml b/lib/web/Cargo.toml index a2d7ae30..7b2354e1 100644 --- a/lib/web/Cargo.toml +++ b/lib/web/Cargo.toml @@ -35,7 +35,7 @@ unsigned-varint = "0.7.0" [dependencies.didkit] path = "../" default-features = false -features = ["ed25519", "rsa", "ripemd-160", "eip", "tezos", "secp256r1"] +features = ["ed25519", "rsa", "ripemd-160", "eip", "tezos", "secp256r1", "secp256k1"] [dependencies.did-ethr] path = "../../../ssi/did-ethr" diff --git a/lib/web/README.md b/lib/web/README.md index 906b838e..638894c8 100644 --- a/lib/web/README.md +++ b/lib/web/README.md @@ -80,6 +80,20 @@ features explicitly. Avoid `--all-features`: it can introduce proposals that the target browser does not support. Keep the generated JavaScript glue paired with its WASM binary. +## DID methods + +In this WASM build: + +- `keyToDID` and `keyToVerificationMethod` can generate `did:key` (Ed25519, + secp256k1 and P-256 keys), `did:tz`, `did:ethr`, `did:pkh` and `did:jwk`. +- `resolveDID` can resolve `did:key`, `did:web`, `did:tz`, `did:ethr`, + `did:pkh` and `did:jwk`. +- `did:ion` and `did:onion` don't resolve. Both read their endpoint from an + environment variable (`DID_ION_API_URL`, `DID_ONION_PROXY_URL`), and a + browser has none: `did:ion` fails with `Missing Sidetree API endpoint`, and + `did:onion` tries a plain HTTP request to the `.onion` host. Use the native + library or CLI for those methods. + ## Tests The `test/` directory contains manual tests to run in the browser. Instructions diff --git a/lib/web/src/lib.rs b/lib/web/src/lib.rs index d70435dd..f53a9539 100644 --- a/lib/web/src/lib.rs +++ b/lib/web/src/lib.rs @@ -1091,3 +1091,30 @@ async fn context_loader(url: String) -> Result { pub fn contextLoader(url: String) -> Promise { map_async_jsvalue(context_loader(url)) } + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::Value; + + // LearnCard derives its secp256k1 key from seed bytes, then asks for a did:key. + #[cfg(feature = "generate")] + #[tokio::test] + async fn secp256k1_key_to_did_key_round_trips() { + let jwk = generate_secp256k1_key_from_bytes(&[7u8; 32]).unwrap(); + + let did = key_to_did("key".to_string(), jwk.clone()).unwrap(); + assert!(did.starts_with("did:key:zQ3s"), "unexpected did: {}", did); + + let doc: Value = + serde_json::from_str(&resolve_did(did.clone(), "{}".to_string()).await.unwrap()) + .unwrap(); + assert_eq!(doc["id"], did.as_str()); + + let key: Value = serde_json::from_str(&jwk).unwrap(); + let resolved_key = &doc["verificationMethod"][0]["publicKeyJwk"]; + assert_eq!(resolved_key["crv"], "secp256k1"); + assert_eq!(resolved_key["x"], key["x"]); + assert_eq!(resolved_key["y"], key["y"]); + } +}