pub fn from_jwk(jwk: &Jwk) -> Result<Self, JoseError> {
        (|| -> anyhow::Result<Self> {
            match jwk.key_type() {
                val if val == "EC" => {}
                val => bail!("A parameter kty must be EC: {}", val),
            }
            let curve = match jwk.parameter("crv") {
                Some(Value::String(val)) => match val.as_str() {
                    "P-256" => EcCurve::P256,
                    "P-384" => EcCurve::P384,
                    "P-521" => EcCurve::P521,
                    "secp256k1" => EcCurve::Secp256k1,
                    _ => bail!("A Unknown curve: {}", val),
                },
                Some(_) => bail!("A parameter crv must be a string."),
                None => bail!("A parameter crv is required."),
            };
            let d = match jwk.parameter("d") {
                Some(Value::String(val)) => base64::decode_config(val, base64::URL_SAFE_NO_PAD)?,
                Some(_) => bail!("A parameter d must be a string."),
                None => bail!("A parameter d is required."),
            };
            let x = match jwk.parameter("x") {
                Some(Value::String(val)) => {
                    let x = base64::decode_config(val, base64::URL_SAFE_NO_PAD)?;
                    Some(x)
                }
                Some(_) => bail!("A parameter x must be a string."),
                None => None,
            };
            let y = match jwk.parameter("y") {
                Some(Value::String(val)) => {
                    let y = base64::decode_config(val, base64::URL_SAFE_NO_PAD)?;
                    Some(y)
                }
                Some(_) => bail!("A parameter y must be a string."),
                None => None,
            };

            let public_key = if let (Some(x), Some(y)) = (x, y) {
                let mut public_key = Vec::with_capacity(1 + x.len() + y.len());
                public_key.push(0x04);
                public_key.extend_from_slice(&x);
                public_key.extend_from_slice(&y);
                Some(public_key)
            } else {
                None
            };

            let mut builder = DerBuilder::new();
            builder.begin(DerType::Sequence);
            {
                builder.append_integer_from_u8(1);
                builder.append_octed_string_from_bytes(&d);
                builder.begin(DerType::Other(DerClass::ContextSpecific, 0));
                {
                    builder.append_object_identifier(curve.oid());
                }
                builder.end();

                if let Some(public_key) = public_key {
                    builder.begin(DerType::Other(DerClass::ContextSpecific, 1));
                    {
                        builder.append_bit_string_from_bytes(&public_key, 0);
                    }
                    builder.end();
                }
            }
            builder.end();

            let pkcs8 = EcKeyPair::to_pkcs8(&builder.build(), false, curve);
            let private_key = PKey::private_key_from_der(&pkcs8)?;
            let algorithm = jwk.algorithm().map(|val| val.to_string());
            let key_id = jwk.key_id().map(|val| val.to_string());

            Ok(EcKeyPair {
                private_key,
                curve,
                algorithm,
                key_id,
            })
        })()
        .map_err(|err| JoseError::InvalidKeyFormat(err))
    }
/// Return a signer from a private key that is formatted by a JWK of EC type.
///
/// # Arguments
///
/// * `jwk` - A private key that is formatted by a JWK of EC type.
Return a signer from a private key that is formatted by a JWK of EC type.
Return a signer from a private key that is formatted by a JWK of EC type .
pub fn from_jwk(jwk: &Jwk) -> Result<Self, JoseError> {
        (|| -> anyhow::Result<Self> {
            match jwk.key_type() {
                val if val == "EC" => {}
                val => bail!("A parameter kty must be EC: {}", val),
            }
            let curve = match jwk.parameter("crv") {
                Some(Value::String(val)) => match val.as_str() {
                    "P-256" => EcCurve::P256,
                    "P-384" => EcCurve::P384,
                    "P-521" => EcCurve::P521,
                    "secp256k1" => EcCurve::Secp256k1,
                    _ => bail!("A Unknown curve: {}", val),
                },
                Some(_) => bail!("A parameter crv must be a string."),
                None => bail!("A parameter crv is required."),
            };
            let d = match jwk.parameter("d") {
                Some(Value::String(val)) => base64::decode_config(val, base64::URL_SAFE_NO_PAD)?,
                Some(_) => bail!("A parameter d must be a string."),
                None => bail!("A parameter d is required."),
            };
            let x = match jwk.parameter("x") {
                Some(Value::String(val)) => {
                    let x = base64::decode_config(val, base64::URL_SAFE_NO_PAD)?;
                    Some(x)
                }
                Some(_) => bail!("A parameter x must be a string."),
                None => None,
            };
            let y = match jwk.parameter("y") {
                Some(Value::String(val)) => {
                    let y = base64::decode_config(val, base64::URL_SAFE_NO_PAD)?;
                    Some(y)
                }
                Some(_) => bail!("A parameter y must be a string."),
                None => None,
            };
            let public_key = if let (Some(x), Some(y)) = (x, y) {
                let mut public_key = Vec::with_capacity(1 + x.len() + y.len());
                public_key.push(0x04);
                public_key.extend_from_slice(&x);
                public_key.extend_from_slice(&y);
                Some(public_key)
            } else {
                None
            };
            let mut builder = DerBuilder::new();
            builder.begin(DerType::Sequence);
            {
                builder.append_integer_from_u8(1);
                builder.append_octed_string_from_bytes(&d);
                builder.begin(DerType::Other(DerClass::ContextSpecific, 0));
                {
                    builder.append_object_identifier(curve.oid());
                }
                builder.end();
                if let Some(public_key) = public_key {
                    builder.begin(DerType::Other(DerClass::ContextSpecific, 1));
                    {
                        builder.append_bit_string_from_bytes(&public_key, 0);
                    }
                    builder.end();
                }
            }
            builder.end();
            let pkcs8 = EcKeyPair::to_pkcs8(&builder.build(), false, curve);
            let private_key = PKey::private_key_from_der(&pkcs8)?;
            let algorithm = jwk.algorithm().map(|val| val.to_string());
            let key_id = jwk.key_id().map(|val| val.to_string());
            Ok(EcKeyPair {
                private_key,
                curve,
                algorithm,
                key_id,
            })
        })()
        .map_err(|err| JoseError::InvalidKeyFormat(err))
    }
pub fn from_jwk ( jwk : & Jwk ) -> Result < Self , JoseError > { ( | | -> anyhow :: Result < Self > { match jwk . key_type ( ) { val if val == " " => { } val => bail ! ( " " val ) , } let curve = match jwk . parameter ( " " ) { Some ( Value :: String ( val ) ) => match val . as_str ( ) { " " => EcCurve :: P256 , " " => EcCurve :: P384 , " " => EcCurve :: P521 , " " => EcCurve :: Secp256k1 , _ => bail ! ( " " val ) , } , Some ( _ ) => bail ! ( " " ) , None => bail ! ( " " ) , } ; let d = match jwk . parameter ( " " ) { Some ( Value :: String ( val ) ) => base64 :: decode_config ( val , base64 :: URL_SAFE_NO_PAD ) ? , Some ( _ ) => bail ! ( " " ) , None => bail ! ( " " ) , } ; let x = match jwk . parameter ( " " ) { Some ( Value :: String ( val ) ) => { let x = base64 :: decode_config ( val , base64 :: URL_SAFE_NO_PAD ) ? ; Some ( x ) } Some ( _ ) => bail ! ( " " ) , None => None , } ; let y = match jwk . parameter ( " " ) { Some ( Value :: String ( val ) ) => { let y = base64 :: decode_config ( val , base64 :: URL_SAFE_NO_PAD ) ? ; Some ( y ) } Some ( _ ) => bail ! ( " " ) , None => None , } ; let public_key = if let ( Some ( x ) , Some ( y ) ) = ( x , y ) { let mut public_key = Vec :: with_capacity ( 1 + x . len ( ) + y . len ( ) ) ; public_key . push ( 0x04 ) ; public_key . extend_from_slice ( & x ) ; public_key . extend_from_slice ( & y ) ; Some ( public_key ) } else { None } ; let mut builder = DerBuilder :: new ( ) ; builder . begin ( DerType :: Sequence ) ; { builder . append_integer_from_u8 ( 1 ) ; builder . append_octed_string_from_bytes ( & d ) ; builder . begin ( DerType :: Other ( DerClass :: ContextSpecific , 0 ) ) ; { builder . append_object_identifier ( curve . oid ( ) ) ; } builder . end ( ) ; if let Some ( public_key ) = public_key { builder . begin ( DerType :: Other ( DerClass :: ContextSpecific , 1 ) ) ; { builder . append_bit_string_from_bytes ( & public_key , 0 ) ; } builder . end ( ) ; } } builder . end ( ) ; let pkcs8 = EcKeyPair :: to_pkcs8 ( & builder . build ( ) , false , curve ) ; let private_key = PKey :: private_key_from_der ( & pkcs8 ) ? ; let algorithm = jwk . algorithm ( ) . map ( | val | val . to_string ( ) ) ; let key_id = jwk . key_id ( ) . map ( | val | val . to_string ( ) ) ; Ok ( EcKeyPair { private_key , curve , algorithm , key_id , } ) } ) ( ) . map_err ( | err | JoseError :: InvalidKeyFormat ( err ) ) }
Return a signer from a private key that is formatted by a JWK of EC type.
Return a signer from a private key that is formatted by a JWK of EC type .
jwk
&Jwk
jwk
&Jwk