Skip to content

Commit

Permalink
feat(did-key): Add EBSI Natural Person Format
Browse files Browse the repository at this point in the history
Enhance did:key functionality to resolve [EBSI-style did:keys](https://hub.ebsi.eu/vc-framework/did/natural-person).

They use the JWK_JCS-PUB multicodec.

Relevant issues that are related to this PR:
[w3c-ccg/did-method-key#63](w3c-ccg/did-method-key#63)
[#536](#536)

Re-do of former PR #545
  • Loading branch information
ianhamilton87 committed Aug 15, 2024
1 parent 94d5ff9 commit 43c9843
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
18 changes: 17 additions & 1 deletion crates/jwk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ impl FromStr for JWK {
}
}

pub fn from_bytes(bytes: &[u8]) -> Result<JWK, serde_json::Error> {
serde_json::from_slice(bytes)
}

impl TryFrom<serde_json::Value> for JWK {
type Error = serde_json::Error;

Expand Down Expand Up @@ -1314,6 +1318,7 @@ mod tests {
const RSA_DER: &[u8] = include_bytes!("../../../tests/rsa2048-2020-08-25.der");
const RSA_PK_DER: &[u8] = include_bytes!("../../../tests/rsa2048-2020-08-25-pk.der");
const ED25519_JSON: &str = include_str!("../../../tests/ed25519-2020-10-18.json");
const JWK_JCS_JSON: &[u8] = include_bytes!("../../../tests/jwk_jcs-pub.json");

#[test]
fn jwk_to_from_der_rsa() {
Expand All @@ -1325,6 +1330,17 @@ mod tests {
assert_eq!(key.to_public().params, Params::RSA(rsa_params));
}

#[test]
fn jwk_from_bytes() {
let actual_jwk: JWK = from_bytes(JWK_JCS_JSON).unwrap();
let actual_params: Params = actual_jwk.params;
if let Params::EC(ref ec_params) = actual_params {
assert_eq!(ec_params.curve.as_deref(), Some("P-256"));
} else {
panic!("actual_params is not of type Params::EC");
}
}

#[test]
fn rsa_from_str() {
let _key: JWK = serde_json::from_str(RSA_JSON).unwrap();
Expand Down Expand Up @@ -1369,7 +1385,7 @@ mod tests {
"alg": "RS256",
"kid": "2011-04-29"
}))
.unwrap();
.unwrap();
let thumbprint = key.thumbprint().unwrap();
assert_eq!(thumbprint, "NzbLsXh8uDCcd-6MNwXF4W_7noWXFZAfHkxZsRGC9Xs");

Expand Down
6 changes: 6 additions & 0 deletions crates/jwk/src/multicodec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ impl JWK {
ssi_multicodec::BLS12_381_G2_PUB => {
crate::bls12381g2_parse(k).map_err(FromMulticodecError::Bls12381G2Pub)
}
ssi_multicodec::JWK_JCS_PUB => {
crate::from_bytes(k).map_err(FromMulticodecError::JwkJcsPub)
}
_ => Err(FromMulticodecError::UnsupportedCodec(codec)),
}
}
Expand Down Expand Up @@ -165,6 +168,9 @@ pub enum FromMulticodecError {
#[error(transparent)]
Bls12381G2Pub(ssi_bbs::Error),

#[error(transparent)]
JwkJcsPub(serde_json::Error),

/// Unexpected multibase (multicodec) key prefix multicodec
#[error("Unsupported multicodec key type 0x{0:x}")]
UnsupportedCodec(u64),
Expand Down
6 changes: 6 additions & 0 deletions tests/jwk_jcs-pub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"crv": "P-256",
"kty": "EC",
"x": "g3fsv1xpWPH099LIUn_zJoOF5Ur8xobyzZwX9m_dJ4E",
"y": "9304UAFl55xQMfrnB-zKEjjXEC4OFWSuYnr7W6hdkVA"
}

0 comments on commit 43c9843

Please sign in to comment.