Skip to content

Commit

Permalink
Merge pull request #29 from stevenroose/negate-pubkey
Browse files Browse the repository at this point in the history
key: Add negate-pubkey command to negate pubkeys
  • Loading branch information
stevenroose committed Mar 23, 2023
2 parents 6d1fd49 + f1ca015 commit 07027d4
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/bin/hal/cmd/key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::process;
use std::io::Write;
use std::str::FromStr;

use bitcoin::secp256k1;
Expand All @@ -15,6 +16,7 @@ pub fn subcommand<'a>() -> clap::App<'a, 'a> {
.subcommand(cmd_inspect())
.subcommand(cmd_sign())
.subcommand(cmd_verify())
.subcommand(cmd_negate_pubkey())
}

pub fn execute<'a>(matches: &clap::ArgMatches<'a>) {
Expand All @@ -23,6 +25,7 @@ pub fn execute<'a>(matches: &clap::ArgMatches<'a>) {
("inspect", Some(ref m)) => exec_inspect(&m),
("sign", Some(ref m)) => exec_sign(&m),
("verify", Some(ref m)) => exec_verify(&m),
("negate-pubkey", Some(ref m)) => exec_negate_pubkey(&m),
(_, _) => unreachable!("clap prints help"),
};
}
Expand Down Expand Up @@ -201,3 +204,22 @@ fn exec_verify<'a>(matches: &clap::ArgMatches<'a>) {
process::exit(1);
}
}

fn cmd_negate_pubkey<'a>() -> clap::App<'a, 'a> {
cmd::subcommand("negate-pubkey", "negate the public key")
.args(&[cmd::opt_yaml(), cmd::arg("pubkey", "the public key").required(true)])
}

fn exec_negate_pubkey<'a>(matches: &clap::ArgMatches<'a>) {
let s = matches.value_of("pubkey").expect("no public key provided");
let key = PublicKey::from_str(&s).expect("invalid public key");

let secp = secp256k1::Secp256k1::new();
let negated = {
let mut key = key.key.clone();
key.negate_assign(&secp);
key
};

write!(::std::io::stdout(), "{}", negated).expect("failed to write stdout");
}

0 comments on commit 07027d4

Please sign in to comment.