Skip to content

Commit

Permalink
support in VM processing to public key
Browse files Browse the repository at this point in the history
Signed-off-by: gmulhearn <gmulhearn@proton.me>
  • Loading branch information
gmulhearn committed Oct 2, 2024
1 parent ce45a85 commit 8089e48
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
3 changes: 3 additions & 0 deletions did_core/did_doc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
52 changes: 52 additions & 0 deletions did_core/did_doc/src/schema/verification_method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ impl TryFrom<VerificationMethodType> 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,
)),
Expand Down
8 changes: 7 additions & 1 deletion did_core/public_key/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 "*"

0 comments on commit 8089e48

Please sign in to comment.