soil_cli/commands/
verify.rs1use crate::{error, params::MessageParams, utils, with_crypto_scheme, CryptoSchemeFlag};
10use clap::Parser;
11use std::io::BufRead;
12use subsoil::core::crypto::{ByteArray, Ss58Codec};
13
14#[derive(Debug, Clone, Parser)]
16#[command(
17 name = "verify",
18 about = "Verify a signature for a message, provided on STDIN, with a given (public or secret) key"
19)]
20pub struct VerifyCmd {
21 sig: String,
23
24 uri: Option<String>,
28
29 #[allow(missing_docs)]
30 #[clap(flatten)]
31 pub message_params: MessageParams,
32
33 #[allow(missing_docs)]
34 #[clap(flatten)]
35 pub crypto_scheme: CryptoSchemeFlag,
36}
37
38impl VerifyCmd {
39 pub fn run(&self) -> error::Result<()> {
41 self.verify(|| std::io::stdin().lock())
42 }
43
44 pub(crate) fn verify<F, R>(&self, create_reader: F) -> error::Result<()>
50 where
51 R: BufRead,
52 F: FnOnce() -> R,
53 {
54 let message = self.message_params.message_from(create_reader)?;
55 let sig_data = array_bytes::hex2bytes(&self.sig)?;
56 let uri = utils::read_uri(self.uri.as_ref())?;
57 let uri = if let Some(uri) = uri.strip_prefix("0x") { uri } else { &uri };
58
59 with_crypto_scheme!(self.crypto_scheme.scheme, verify(sig_data, message, uri))
60 }
61}
62
63fn verify<Pair>(sig_data: Vec<u8>, message: Vec<u8>, uri: &str) -> error::Result<()>
64where
65 Pair: subsoil::core::Pair,
66 Pair::Signature: for<'a> TryFrom<&'a [u8]>,
67{
68 let signature =
69 Pair::Signature::try_from(&sig_data).map_err(|_| error::Error::SignatureFormatInvalid)?;
70
71 let pubkey = if let Ok(pubkey_vec) = array_bytes::hex2bytes(uri) {
72 Pair::Public::from_slice(pubkey_vec.as_slice())
73 .map_err(|_| error::Error::KeyFormatInvalid)?
74 } else {
75 Pair::Public::from_string(uri)?
76 };
77
78 if Pair::verify(&signature, &message, &pubkey) {
79 println!("Signature verifies correctly.");
80 } else {
81 return Err(error::Error::SignatureInvalid);
82 }
83
84 Ok(())
85}
86
87#[cfg(test)]
88mod test {
89 use super::*;
90
91 const ALICE: &str = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";
92 const SIG1: &str = "0x4eb25a2285a82374888880af0024eb30c3a21ce086eae3862888d345af607f0ad6fb081312f11730932564f24a9f8ebcee2d46861413ae61307eca58db2c3e81";
93 const SIG2: &str = "0x026342225155056ea797118c1c8c8b3cc002aa2020c36f4217fa3c302783a572ad3dcd38c231cbaf86cadb93984d329c963ceac0685cc1ee4c1ed50fa443a68f";
94
95 #[test]
97 fn verify_immediate() {
98 let cmd = VerifyCmd::parse_from(&["verify", SIG1, ALICE, "--message", "test message"]);
99 assert!(cmd.run().is_ok(), "Alice' signature should verify");
100 }
101
102 #[test]
104 fn verify_stdin() {
105 let cmd = VerifyCmd::parse_from(&["verify", SIG1, ALICE]);
106 let message = "test message";
107 assert!(cmd.verify(|| message.as_bytes()).is_ok(), "Alice' signature should verify");
108 }
109
110 #[test]
112 fn verify_immediate_hex() {
113 let cmd = VerifyCmd::parse_from(&["verify", SIG2, ALICE, "--message", "0xaabbcc", "--hex"]);
114 assert!(cmd.run().is_ok(), "Alice' signature should verify");
115 }
116
117 #[test]
119 fn verify_stdin_hex() {
120 let cmd = VerifyCmd::parse_from(&["verify", SIG2, ALICE, "--hex"]);
121 assert!(cmd.verify(|| "0xaabbcc".as_bytes()).is_ok());
122 assert!(cmd.verify(|| "aabbcc".as_bytes()).is_ok());
123 assert!(cmd.verify(|| "0xaABBcC".as_bytes()).is_ok());
124 }
125}