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

feat(did-key): Add Support To Resolve JWK_JCS-PUB encoded did:key DIDs #600

Merged
merged 14 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
68 changes: 42 additions & 26 deletions 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> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this a constructor of JWK? And also add a TryFrom<&[u8]> implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timothee-haudebourg - ok. I moved it down to line 313, within the JWK implementation. I hope that's what you mean by make it a constructor.
And I added the TryFrom implementation too.

serde_json::from_slice(bytes)
}

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

Expand Down Expand Up @@ -418,40 +422,40 @@ impl JWK {
match (&self.params, &other.params) {
(
Params::RSA(RSAParams {
modulus: Some(n1),
exponent: Some(e1),
..
}),
modulus: Some(n1),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a formatting issue here. Did you run cargo fmt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timothee-haudebourg Yikes! I did. Should be good now.

exponent: Some(e1),
..
}),
Params::RSA(RSAParams {
modulus: Some(n2),
exponent: Some(e2),
..
}),
modulus: Some(n2),
exponent: Some(e2),
..
}),
) => n1 == n2 && e1 == e2,
(Params::OKP(okp1), Params::OKP(okp2)) => {
okp1.curve == okp2.curve && okp1.public_key == okp2.public_key
}
(
Params::EC(ECParams {
curve: Some(crv1),
x_coordinate: Some(x1),
y_coordinate: Some(y1),
..
}),
curve: Some(crv1),
x_coordinate: Some(x1),
y_coordinate: Some(y1),
..
}),
Params::EC(ECParams {
curve: Some(crv2),
x_coordinate: Some(x2),
y_coordinate: Some(y2),
..
}),
curve: Some(crv2),
x_coordinate: Some(x2),
y_coordinate: Some(y2),
..
}),
) => crv1 == crv2 && x1 == x2 && y1 == y2,
(
Params::Symmetric(SymmetricParams {
key_value: Some(kv1),
}),
key_value: Some(kv1),
}),
Params::Symmetric(SymmetricParams {
key_value: Some(kv2),
}),
key_value: Some(kv2),
}),
) => kv1 == kv2,
_ => false,
}
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 All @@ -1379,7 +1395,7 @@ mod tests {
"kty": "OKP",
"x":"11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo"
}))
.unwrap();
.unwrap();
let thumbprint = key.thumbprint().unwrap();
assert_eq!(thumbprint, "kPrK_qmxVWaYVA9wwBF6Iuo3vVzz7TxHCTwXBygrS4k");

Expand All @@ -1391,7 +1407,7 @@ mod tests {
"x": "weNJy2HscCSM6AEDTDg04biOvhFhyyWvOHQfeF_PxMQ",
"y": "e8lnCO-AlStT-NJVX-crhB7QRYhiix03illJOVAOyck",
}))
.unwrap();
.unwrap();
let thumbprint = key.thumbprint().unwrap();
assert_eq!(thumbprint, "Vy57XrArUrW0NbpI12tEzDHABxMwrTh6HHXRenSpnCo");

Expand All @@ -1400,7 +1416,7 @@ mod tests {
"kty": "oct",
"k": "11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo"
}))
.unwrap();
.unwrap();
let thumbprint = key.thumbprint().unwrap();
assert_eq!(thumbprint, "kcfv_I8tB4KY_ljAlRa1ip-y7jzbPdH0sUlCGb-1Jx8");
}
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 => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test checking that its able to decode a JWK encoded with the JWK_JCS_PUB multicodec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

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"
}