diff --git a/Cargo.toml b/Cargo.toml index 327143b78c..f0fe515e3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -78,8 +78,8 @@ let_underscore_drop = "allow" indy-vdr = { git = "https://github.com/hyperledger/indy-vdr.git", tag = "v0.4.3", default-features = false, features = [ "log", ] } -indy-vdr-proxy-client = { git = "https://github.com/hyperledger/indy-vdr.git", tag = "v0.4.3" } +indy-vdr-proxy-client = { git = "https://github.com/hyperledger/indy-vdr.git", tag = "v0.4.3" } indy-credx = { git = "https://github.com/hyperledger/indy-shared-rs", tag = "v1.1.0" } anoncreds = { git = "https://github.com/hyperledger/anoncreds-rs.git", tag = "v0.2.0" } aries-askar = { version = "0.3.1" } -askar-crypto = { version = "0.3.1" } +askar-crypto = { version = "0.3.1", default-features = false } diff --git a/did_core/did_doc/Cargo.toml b/did_core/did_doc/Cargo.toml index 4bad05ee3d..061d4fc36f 100644 --- a/did_core/did_doc/Cargo.toml +++ b/did_core/did_doc/Cargo.toml @@ -3,6 +3,9 @@ name = "did_doc" version = "0.1.0" edition = "2021" +[features] +jwk = ["public_key/jwk"] + [dependencies] base64 = "0.22.1" bs58 = "0.5.0" diff --git a/did_core/did_doc/src/schema/verification_method/mod.rs b/did_core/did_doc/src/schema/verification_method/mod.rs index 0198c2acef..894b28a311 100644 --- a/did_core/did_doc/src/schema/verification_method/mod.rs +++ b/did_core/did_doc/src/schema/verification_method/mod.rs @@ -46,6 +46,8 @@ impl VerificationMethod { PublicKeyField::Multibase { public_key_multibase, } => Key::from_fingerprint(public_key_multibase)?, + #[cfg(feature = "jwk")] + PublicKeyField::Jwk { public_key_jwk } => Key::from_jwk(&public_key_jwk.to_string())?, // TODO - FUTURE - other key types could do with some special handling, i.e. // those where the key_type is encoded within the key field (multibase, jwk, etc) _ => Key::new( @@ -152,3 +154,53 @@ mod tests { assert!(vm.is_err()); } } + +#[cfg(feature = "jwk")] +#[cfg(test)] +mod jwk_tests { + use ::public_key::KeyType; + use serde_json::json; + + use super::*; + + #[test] + fn test_public_key_from_ed25519_jwk_vm() { + let vm: VerificationMethod = serde_json::from_value(json!({ + "id": "did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH#z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH", + "type": "Ed25519VerificationKey2018", + "controller": "did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH", + "publicKeyJwk": { + "kty": "OKP", + "crv": "Ed25519", + "x": "lJZrfAjkBXdfjebMHEUI9usidAPhAlssitLXR3OYxbI" + } + })).unwrap(); + let pk = vm.public_key().unwrap(); + assert!(matches!(pk.key_type(), KeyType::Ed25519)); + assert_eq!( + pk.fingerprint(), + "z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH" + ) + } + + #[test] + fn test_public_key_from_p256_jwk_vm() { + let vm: VerificationMethod = serde_json::from_value(json!({ + "id": "did:key:zDnaerDaTF5BXEavCrfRZEk316dpbLsfPDZ3WJ5hRTPFU2169#zDnaerDaTF5BXEavCrfRZEk316dpbLsfPDZ3WJ5hRTPFU2169", + "type": "JsonWebKey2020", + "controller": "did:key:zDnaerDaTF5BXEavCrfRZEk316dpbLsfPDZ3WJ5hRTPFU2169", + "publicKeyJwk": { + "kty": "EC", + "crv": "P-256", + "x": "fyNYMN0976ci7xqiSdag3buk-ZCwgXU4kz9XNkBlNUI", + "y": "hW2ojTNfH7Jbi8--CJUo3OCbH3y5n91g-IMA9MLMbTU" + } + })).unwrap(); + let pk = vm.public_key().unwrap(); + assert!(matches!(pk.key_type(), KeyType::P256)); + assert_eq!( + pk.fingerprint(), + "zDnaerDaTF5BXEavCrfRZEk316dpbLsfPDZ3WJ5hRTPFU2169" + ) + } +} diff --git a/did_core/did_doc/src/schema/verification_method/verification_method_type.rs b/did_core/did_doc/src/schema/verification_method/verification_method_type.rs index 9d41289141..f8e715f5f0 100644 --- a/did_core/did_doc/src/schema/verification_method/verification_method_type.rs +++ b/did_core/did_doc/src/schema/verification_method/verification_method_type.rs @@ -66,6 +66,8 @@ impl TryFrom for KeyType { VerificationMethodType::Bls12381G2Key2020 => Ok(KeyType::Bls12381g2), VerificationMethodType::X25519KeyAgreementKey2019 | VerificationMethodType::X25519KeyAgreementKey2020 => Ok(KeyType::X25519), + // The verification method type does not map directly to a key type. + // This may occur when the VM type is a multikey (JsonWebKey, Multikey, etc) _ => Err(DidDocumentBuilderError::UnsupportedVerificationMethodType( value, )), diff --git a/did_core/public_key/Cargo.toml b/did_core/public_key/Cargo.toml index 63b676c637..c9baf690e7 100644 --- a/did_core/public_key/Cargo.toml +++ b/did_core/public_key/Cargo.toml @@ -14,4 +14,10 @@ base64 = "0.22.1" bs58 = "0.5.0" multibase = "0.9.1" unsigned-varint = "0.8.0" -askar-crypto = { workspace = true, features = ["std"], optional = true } +# askar-crypto used for jwk conversion. maintain minimal feature set +askar-crypto = { workspace = true, features = [ + "std", + "any_key", + "ec_curves", + "ed25519", +], optional = true } diff --git a/justfile b/justfile index cae10c52d4..911326b130 100644 --- a/justfile +++ b/justfile @@ -44,4 +44,4 @@ test-integration-aries-vcx-vdrproxy test_name="": cargo test --manifest-path="aries/aries_vcx/Cargo.toml" -F vdr_proxy_ledger,credx -- --ignored {{test_name}} test-integration-did-crate test_name="": - cargo test --examples -p did_doc -p did_parser_nom -p did_resolver -p did_resolver_registry -p did_resolver_sov -p did_resolver_web -p did_key -p did_peer --test "*" + cargo test --examples -p did_doc -p did_parser_nom -p did_resolver -p did_resolver_registry -p did_resolver_sov -p did_resolver_web -p did_key -p did_peer -F did_doc/jwk --test "*"