feat: v1.10.18, add parse-ecdsa-signature

This commit is contained in:
2025-03-22 10:04:39 +08:00
parent 04247bb846
commit 7d2cf85f89
5 changed files with 76 additions and 3 deletions

View File

@@ -0,0 +1,64 @@
use std::collections::BTreeMap;
use clap::{App, Arg, ArgMatches, SubCommand};
use rust_util::util_clap::{Command, CommandError};
use rust_util::util_msg;
use crate::ecdsautil::parse_ecdsa_r_and_s;
use crate::util::try_decode;
const SEPARATOR: &str = ".";
pub struct CommandImpl;
impl Command for CommandImpl {
fn name(&self) -> &str {
"parse-ecdsa-signature"
}
fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name())
.about("Parse ECDSA signature")
.arg(
Arg::with_name("signature")
.long("signature")
.required(true)
.takes_value(true)
.help("ECDSA signature"),
)
.arg(Arg::with_name("json").long("json").help("JSON output"))
}
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError {
let json_output = sub_arg_matches.is_present("json");
if json_output {
util_msg::set_logger_std_out(false);
}
let mut json = BTreeMap::<&'_ str, String>::new();
let signature = sub_arg_matches.value_of("signature").unwrap();
let signature_der = try_decode(signature)?;
let (r, s) = parse_ecdsa_r_and_s(&signature_der)?;
let mut r_and_s = r.clone();
r_and_s.extend_from_slice(&s);
if json_output {
json.insert("r", hex::encode(&r));
json.insert("s", hex::encode(&s));
json.insert("rs", hex::encode(&r_and_s));
} else {
information!("R: {}", hex::encode(&r));
information!("S: {}", hex::encode(&s));
information!("RS: {}", hex::encode(&r_and_s));
}
if json_output {
println!("{}", serde_json::to_string_pretty(&json).unwrap());
}
Ok(None)
}
}