Skip to content

Commit ee0657c

Browse files
committed
fix(rust/signed-doc): add verify command to cli tool
1 parent 0f85057 commit ee0657c

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

rust/signed_doc/examples/mk_signed_doc.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ use std::{
1010

1111
use catalyst_signed_doc::{Builder, CatalystSignedDocument, KidUri, Metadata};
1212
use clap::Parser;
13-
use coset::CborSerializable;
14-
use ed25519_dalek::{ed25519::signature::Signer, pkcs8::DecodePrivateKey};
13+
use ed25519_dalek::pkcs8::{DecodePrivateKey, DecodePublicKey};
1514

1615
fn main() {
1716
if let Err(err) = Cli::parse().exec() {
@@ -52,6 +51,16 @@ enum Cli {
5251
/// Hex-formatted COSE SIGN Bytes
5352
cose_sign_hex: String,
5453
},
54+
/// Validates a signature by Key ID and verifiying key
55+
Verify {
56+
/// Path to the formed (could be empty, without any signatures) COSE document
57+
/// This exact file would be modified and new signature would be added
58+
path: PathBuf,
59+
/// Path to the verifying key in PEM format
60+
pk: PathBuf,
61+
/// Signer kid
62+
kid: KidUri,
63+
},
5564
}
5665

5766
impl Cli {
@@ -89,6 +98,22 @@ impl Cli {
8998
let cose_bytes = hex::decode(&cose_sign_hex)?;
9099
inspect_signed_doc(&cose_bytes)?;
91100
},
101+
Self::Verify { path, pk, kid } => {
102+
let pk = load_public_key_from_file(&pk)
103+
.map_err(|e| anyhow::anyhow!("Failed to load PK FILE {pk:?}: {e}"))?;
104+
let cose_bytes = read_bytes_from_file(&path)?;
105+
let signed_doc = signed_doc_from_bytes(cose_bytes.as_slice())?;
106+
signed_doc
107+
.verify(|k| {
108+
if k.to_string() == kid.to_string() {
109+
pk
110+
} else {
111+
k.role0_pk()
112+
}
113+
})
114+
.map_err(|e| anyhow::anyhow!("Catalyst Document Verification failed: {e}"))?;
115+
println!("Catalyst Signed Document is Verified.");
116+
},
92117
}
93118
println!("Done");
94119
Ok(())

0 commit comments

Comments
 (0)