11//! COSE Signature Protected Header `kid` URI Role0 Public Key.
22
33use std:: {
4- fmt:: { Display , Formatter } ,
5- str:: FromStr ,
4+ fmt:: { Display , Formatter } ,
5+ str:: FromStr ,
66} ;
77
88/// Role0 Public Key.
99#[ derive( Debug , Clone ) ]
1010pub struct Role0PublicKey ( [ u8 ; 32 ] ) ;
1111
1212impl FromStr for Role0PublicKey {
13- type Err = anyhow:: Error ;
13+ type Err = anyhow:: Error ;
1414
15- fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
16- let Some ( role0_hex) = s. strip_prefix ( "0x" ) else {
17- anyhow:: bail!( "Role0 Public Key hex string must start with '0x': {}" , s) ;
18- } ;
19- let role0_key = hex:: decode ( role0_hex)
20- . map_err ( |e| anyhow:: anyhow!( "Role0 Public Key is not a valid hex string: {}" , e) ) ?;
21- if role0_key. len ( ) != 32 {
22- anyhow:: bail!(
23- "Role0 Public Key must have 32 bytes: {role0_hex}, len: {}" ,
24- role0_key. len( )
25- ) ;
26- }
27- let role0 = role0_key. try_into ( ) . map_err ( |e| {
28- anyhow:: anyhow!(
29- "Unable to read Role0 Public Key, this should never happen. Eror: {e:?}"
30- )
31- } ) ?;
32- Ok ( Role0PublicKey ( role0) )
33- }
15+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
16+ let Some ( role0_hex) = s. strip_prefix ( "0x" ) else {
17+ anyhow:: bail!( "Role0 Public Key hex string must start with '0x': {}" , s) ;
18+ } ;
19+ let role0_key = hex:: decode ( role0_hex)
20+ . map_err ( |e| anyhow:: anyhow!( "Role0 Public Key is not a valid hex string: {}" , e) ) ?;
21+ if role0_key. len ( ) != 32 {
22+ anyhow:: bail!(
23+ "Role0 Public Key must have 32 bytes: {role0_hex}, len: {}" ,
24+ role0_key. len( )
25+ ) ;
26+ }
27+ let role0 = role0_key. try_into ( ) . map_err ( |e| {
28+ anyhow:: anyhow!(
29+ "Unable to read Role0 Public Key, this should never happen. Eror: {e:?}"
30+ )
31+ } ) ?;
32+ Ok ( Role0PublicKey ( role0) )
33+ }
3434}
3535
3636impl Display for Role0PublicKey {
37- fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result < ( ) , std:: fmt:: Error > {
38- write ! ( f, "0x{}" , hex:: encode( self . 0 ) )
39- }
40- }
37+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result < ( ) , std:: fmt:: Error > {
38+ write ! ( f, "0x{}" , hex:: encode( self . 0 ) )
39+ }
40+ }
0 commit comments