Skip to content

Commit 07027d4

Browse files
authored
Merge pull request #29 from stevenroose/negate-pubkey
key: Add negate-pubkey command to negate pubkeys
2 parents 6d1fd49 + f1ca015 commit 07027d4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/bin/hal/cmd/key.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::process;
2+
use std::io::Write;
23
use std::str::FromStr;
34

45
use bitcoin::secp256k1;
@@ -15,6 +16,7 @@ pub fn subcommand<'a>() -> clap::App<'a, 'a> {
1516
.subcommand(cmd_inspect())
1617
.subcommand(cmd_sign())
1718
.subcommand(cmd_verify())
19+
.subcommand(cmd_negate_pubkey())
1820
}
1921

2022
pub fn execute<'a>(matches: &clap::ArgMatches<'a>) {
@@ -23,6 +25,7 @@ pub fn execute<'a>(matches: &clap::ArgMatches<'a>) {
2325
("inspect", Some(ref m)) => exec_inspect(&m),
2426
("sign", Some(ref m)) => exec_sign(&m),
2527
("verify", Some(ref m)) => exec_verify(&m),
28+
("negate-pubkey", Some(ref m)) => exec_negate_pubkey(&m),
2629
(_, _) => unreachable!("clap prints help"),
2730
};
2831
}
@@ -201,3 +204,22 @@ fn exec_verify<'a>(matches: &clap::ArgMatches<'a>) {
201204
process::exit(1);
202205
}
203206
}
207+
208+
fn cmd_negate_pubkey<'a>() -> clap::App<'a, 'a> {
209+
cmd::subcommand("negate-pubkey", "negate the public key")
210+
.args(&[cmd::opt_yaml(), cmd::arg("pubkey", "the public key").required(true)])
211+
}
212+
213+
fn exec_negate_pubkey<'a>(matches: &clap::ArgMatches<'a>) {
214+
let s = matches.value_of("pubkey").expect("no public key provided");
215+
let key = PublicKey::from_str(&s).expect("invalid public key");
216+
217+
let secp = secp256k1::Secp256k1::new();
218+
let negated = {
219+
let mut key = key.key.clone();
220+
key.negate_assign(&secp);
221+
key
222+
};
223+
224+
write!(::std::io::stdout(), "{}", negated).expect("failed to write stdout");
225+
}

0 commit comments

Comments
 (0)