-
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 11 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
use ssi_multicodec::{MultiEncoded, MultiEncodedBuf}; | ||
use ssi_multicodec::{Codec, MultiEncoded, MultiEncodedBuf}; | ||
use std::borrow::Cow; | ||
|
||
use crate::{Error, Params, JWK}; | ||
|
||
|
@@ -47,6 +48,9 @@ impl JWK { | |
ssi_multicodec::BLS12_381_G2_PUB => { | ||
crate::bls12381g2_parse(k).map_err(FromMulticodecError::Bls12381G2Pub) | ||
} | ||
ssi_multicodec::JWK_JCS_PUB => { | ||
JWK::from_bytes(k).map_err(FromMulticodecError::JwkJcsPub) | ||
} | ||
_ => Err(FromMulticodecError::UnsupportedCodec(codec)), | ||
} | ||
} | ||
|
@@ -123,6 +127,18 @@ impl JWK { | |
} | ||
} | ||
|
||
impl Codec for JWK { | ||
const CODEC: u64 = ssi_multicodec::JWK_JCS_PUB; | ||
|
||
fn to_bytes(&self) -> Cow<[u8]> { | ||
Cow::Owned(serde_jcs::to_vec(self).unwrap()) | ||
} | ||
|
||
fn from_bytes(bytes: &[u8]) -> Result<Self, ssi_multicodec::Error> { | ||
Self::try_from(bytes).map_err(|_| ssi_multicodec::Error::InvalidData) | ||
} | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum FromMulticodecError { | ||
#[cfg(feature = "rsa")] | ||
|
@@ -165,6 +181,9 @@ pub enum FromMulticodecError { | |
#[error(transparent)] | ||
Bls12381G2Pub(ssi_bbs::Error), | ||
|
||
#[error(transparent)] | ||
JwkJcsPub(ssi_multicodec::Error), | ||
|
||
/// Unexpected multibase (multicodec) key prefix multicodec | ||
#[error("Unsupported multicodec key type 0x{0:x}")] | ||
UnsupportedCodec(u64), | ||
|
@@ -184,3 +203,24 @@ pub enum ToMulticodecError { | |
#[error("invalid input key: {0}")] | ||
InvalidInputKey(Error), | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_multicodec_jwk_jcs_pub() { | ||
let jwk = JWK::generate_p256(); | ||
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. This won't work without the 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 - sorry about that. This is my first foray into Rust, can you tell? |
||
let jwk_str = serde_jcs::to_string(&jwk).unwrap(); | ||
// Note: can't use JWK::to_multicodec() because it's based on the particular key within the JWK | ||
// it will see the P256 key and assign a multicodec of 0x1200. | ||
// For jwk_jcs_pub multicodecs, we can only decode them | ||
let jwk_buf = | ||
MultiEncodedBuf::encode_bytes(ssi_multicodec::JWK_JCS_PUB, jwk_str.as_bytes()); | ||
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. Since you implemented 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 Done! |
||
let (codec, data) = jwk_buf.parts(); | ||
assert_eq!(codec, ssi_multicodec::JWK_JCS_PUB); | ||
assert_eq!(*data, jwk.to_bytes().into_owned()); | ||
let jwk2 = JWK::from_multicodec(&jwk_buf).unwrap(); | ||
assert_eq!(jwk, jwk2); | ||
} | ||
} |
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 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Done!