@@ -71,50 +71,60 @@ impl Cli {
7171 . with_content ( payload)
7272 . with_metadata ( metadata)
7373 . build ( ) ?;
74- let mut bytes: Vec < u8 > = Vec :: new ( ) ;
75- minicbor:: encode ( signed_doc, & mut bytes)
76- . map_err ( |e| anyhow:: anyhow!( "Failed to encode document: {e}" ) ) ?;
77-
78- write_bytes_to_file ( & bytes, & output) ?;
74+ save_signed_doc ( signed_doc, & output) ?;
7975 } ,
8076 Self :: Sign { sk, doc, kid } => {
8177 let sk = load_secret_key_from_file ( & sk)
8278 . map_err ( |e| anyhow:: anyhow!( "Failed to load SK FILE: {e}" ) ) ?;
83- let mut cose = load_cose_from_file ( & doc)
84- . map_err ( |e| anyhow :: anyhow! ( "Failed to load COSE FROM FILE: {e}" ) ) ?;
85- add_signature_to_cose ( & mut cose , & sk , kid. to_string ( ) ) ;
86- store_cose_file ( cose , & doc) ?;
79+ let cose_bytes = read_bytes_from_file ( & doc) ? ;
80+ let signed_doc = signed_doc_from_bytes ( cose_bytes . as_slice ( ) ) ?;
81+ let new_signed_doc = signed_doc . sign ( sk . to_bytes ( ) , kid) ? ;
82+ save_signed_doc ( new_signed_doc , & doc) ?;
8783 } ,
8884 Self :: Inspect { path } => {
89- let mut cose_file = File :: open ( path) ?;
90- let mut cose_bytes = Vec :: new ( ) ;
91- cose_file. read_to_end ( & mut cose_bytes) ?;
92- decode_signed_doc ( & cose_bytes) ;
85+ let cose_bytes = read_bytes_from_file ( & path) ?;
86+ inspect_signed_doc ( & cose_bytes) ?;
9387 } ,
9488 Self :: InspectBytes { cose_sign_hex } => {
9589 let cose_bytes = hex:: decode ( & cose_sign_hex) ?;
96- decode_signed_doc ( & cose_bytes) ;
90+ inspect_signed_doc ( & cose_bytes) ? ;
9791 } ,
9892 }
9993 println ! ( "Done" ) ;
10094 Ok ( ( ) )
10195 }
10296}
10397
104- fn decode_signed_doc ( cose_bytes : & [ u8 ] ) {
98+ fn read_bytes_from_file ( path : & PathBuf ) -> anyhow:: Result < Vec < u8 > > {
99+ let mut cose_file = File :: open ( path) ?;
100+ let mut cose_bytes = Vec :: new ( ) ;
101+ cose_file. read_to_end ( & mut cose_bytes) ?;
102+ Ok ( cose_bytes)
103+ }
104+
105+ fn inspect_signed_doc ( cose_bytes : & [ u8 ] ) -> anyhow:: Result < ( ) > {
105106 println ! (
106- "Decoding {} bytes: {}" ,
107+ "Decoding {} bytes:\n {}" ,
107108 cose_bytes. len( ) ,
108109 hex:: encode( cose_bytes)
109110 ) ;
111+ let cat_signed_doc = signed_doc_from_bytes ( cose_bytes) ?;
112+ println ! ( "This is a valid Catalyst Document." ) ;
113+ println ! ( "{cat_signed_doc}" ) ;
114+ Ok ( ( ) )
115+ }
110116
111- match CatalystSignedDocument :: try_from ( cose_bytes) {
112- Ok ( cat_signed_doc) => {
113- println ! ( "This is a valid Catalyst Document." ) ;
114- println ! ( "{cat_signed_doc}" ) ;
115- } ,
116- Err ( e) => eprintln ! ( "Invalid Catalyst Document, err: {e}" ) ,
117- }
117+ fn save_signed_doc ( signed_doc : CatalystSignedDocument , path : & PathBuf ) -> anyhow:: Result < ( ) > {
118+ let mut bytes: Vec < u8 > = Vec :: new ( ) ;
119+ minicbor:: encode ( signed_doc, & mut bytes)
120+ . map_err ( |e| anyhow:: anyhow!( "Failed to encode document: {e}" ) ) ?;
121+
122+ write_bytes_to_file ( & bytes, path)
123+ }
124+
125+ fn signed_doc_from_bytes ( cose_bytes : & [ u8 ] ) -> anyhow:: Result < CatalystSignedDocument > {
126+ CatalystSignedDocument :: try_from ( cose_bytes)
127+ . map_err ( |e| anyhow:: anyhow!( "Invalid Catalyst Document: {e}" ) )
118128}
119129
120130fn load_json_from_file < T > ( path : & PathBuf ) -> anyhow:: Result < T >
@@ -124,45 +134,20 @@ where T: for<'de> serde::Deserialize<'de> {
124134 Ok ( json)
125135}
126136
127- fn load_cose_from_file ( cose_path : & PathBuf ) -> anyhow:: Result < coset:: CoseSign > {
128- let cose_file_bytes = read_bytes_from_file ( cose_path) ?;
129- let cose = coset:: CoseSign :: from_slice ( & cose_file_bytes) . map_err ( |e| anyhow:: anyhow!( "{e}" ) ) ?;
130- Ok ( cose)
131- }
132-
133- fn read_bytes_from_file ( path : & PathBuf ) -> anyhow:: Result < Vec < u8 > > {
134- let mut file_bytes = Vec :: new ( ) ;
135- File :: open ( path) ?. read_to_end ( & mut file_bytes) ?;
136- Ok ( file_bytes)
137- }
138-
139137fn write_bytes_to_file ( bytes : & [ u8 ] , output : & PathBuf ) -> anyhow:: Result < ( ) > {
140138 File :: create ( output) ?
141139 . write_all ( bytes)
142140 . map_err ( |e| anyhow:: anyhow!( "Failed to write to file {output:?}: {e}" ) )
143141}
144142
145- fn store_cose_file ( cose : coset:: CoseSign , output : & PathBuf ) -> anyhow:: Result < ( ) > {
146- let cose_bytes = cose
147- . to_vec ( )
148- . map_err ( |e| anyhow:: anyhow!( "Failed to Store COSE SIGN: {e}" ) ) ?;
149- write_bytes_to_file ( & cose_bytes, output)
150- }
151-
152143fn load_secret_key_from_file ( sk_path : & PathBuf ) -> anyhow:: Result < ed25519_dalek:: SigningKey > {
153144 let sk_str = read_to_string ( sk_path) ?;
154145 let sk = ed25519_dalek:: SigningKey :: from_pkcs8_pem ( & sk_str) ?;
155146 Ok ( sk)
156147}
157148
158- fn add_signature_to_cose ( cose : & mut coset:: CoseSign , sk : & ed25519_dalek:: SigningKey , kid : String ) {
159- let protected_header = coset:: HeaderBuilder :: new ( )
160- . key_id ( kid. into_bytes ( ) )
161- . algorithm ( coset:: iana:: Algorithm :: EdDSA ) ;
162- let mut signature = coset:: CoseSignatureBuilder :: new ( )
163- . protected ( protected_header. build ( ) )
164- . build ( ) ;
165- let data_to_sign = cose. tbs_data ( & [ ] , & signature) ;
166- signature. signature = sk. sign ( & data_to_sign) . to_vec ( ) ;
167- cose. signatures . push ( signature) ;
149+ fn load_public_key_from_file ( pk_path : & PathBuf ) -> anyhow:: Result < ed25519_dalek:: VerifyingKey > {
150+ let pk_str = read_to_string ( pk_path) ?;
151+ let pk = ed25519_dalek:: VerifyingKey :: from_public_key_pem ( & pk_str) ?;
152+ Ok ( pk)
168153}
0 commit comments