-
Notifications
You must be signed in to change notification settings - Fork 62
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
Changes from 5 commits
43c9843
eaeb8a8
5faedc3
5a03909
6c12ad2
79c8969
9f9d5e8
cf6984e
05f0283
0ed793a
78a6adb
c9ec5bc
b3b301b
9953e2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -418,40 +422,40 @@ impl JWK { | |
match (&self.params, &other.params) { | ||
( | ||
Params::RSA(RSAParams { | ||
modulus: Some(n1), | ||
exponent: Some(e1), | ||
.. | ||
}), | ||
modulus: Some(n1), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is a formatting issue here. Did you run There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
} | ||
|
@@ -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() { | ||
|
@@ -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(); | ||
|
@@ -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"); | ||
|
||
|
@@ -1379,7 +1395,7 @@ mod tests { | |
"kty": "OKP", | ||
"x":"11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo" | ||
})) | ||
.unwrap(); | ||
.unwrap(); | ||
let thumbprint = key.thumbprint().unwrap(); | ||
assert_eq!(thumbprint, "kPrK_qmxVWaYVA9wwBF6Iuo3vVzz7TxHCTwXBygrS4k"); | ||
|
||
|
@@ -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"); | ||
|
||
|
@@ -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"); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)), | ||
} | ||
} | ||
|
@@ -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), | ||
|
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" | ||
} |
There was a problem hiding this comment.
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 aTryFrom<&[u8]>
implementation?There was a problem hiding this comment.
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.