Skip to content

Commit

Permalink
feat: Add eaglesong/blake2b hash tool
Browse files Browse the repository at this point in the history
  • Loading branch information
TheWaWaR committed Dec 12, 2019
1 parent 19b74af commit 143a2d1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 52 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ colored = "^1.6.0"
atty = "^0.2.10"
rustyline = "4.0.0"
shell-words = "0.1.0"
eaglesong = "0.1"
regex = "1.1.6"
dirs = "1.0.5"
url = "1.7.2"
Expand Down
130 changes: 78 additions & 52 deletions src/subcommands/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ use ckb_types::{
H160, H256, U256,
};
use clap::{App, Arg, ArgMatches, SubCommand};
use eaglesong::EagleSongBuilder;
use faster_hex::hex_string;

use super::CliSubCommand;
use crate::utils::{
arg_parser::{
AddressParser, AddressPayloadOption, ArgParser, FixedHashParser, FromStrParser,
AddressParser, AddressPayloadOption, ArgParser, FixedHashParser, FromStrParser, HexParser,
PrivkeyPathParser, PrivkeyWrapper, PubkeyHexParser,
},
other::get_address,
Expand Down Expand Up @@ -64,6 +65,11 @@ impl<'a> UtilSubCommand<'a> {
.validator(|input| FixedHashParser::<H160>::default().validate(input))
.help("Lock argument (account identifier, blake2b(pubkey)[0..20])");

let binary_hex_arg = Arg::with_name("binary-hex")
.long("binary-hex")
.takes_value(true)
.required(true)
.validator(|input| HexParser.validate(input));
let arg_sighash_address = Arg::with_name("sighash-address")
.long("sighash-address")
.required(true)
Expand All @@ -78,57 +84,61 @@ impl<'a> UtilSubCommand<'a> {
SubCommand::with_name(name)
.about("Utilities")
.subcommands(vec![
SubCommand::with_name("key-info")
.about(
"Show public information of a secp256k1 private key (from file) or public key",
)
.arg(arg_privkey.clone().conflicts_with("pubkey"))
.arg(arg_pubkey.clone().required(false))
.arg(arg_address.clone().required(false))
.arg(arg_lock_arg.clone()),
SubCommand::with_name("compact-to-difficulty")
.about("Convert compact target value to difficulty value")
.arg(
Arg::with_name("compact-target")
.long("compact-target")
.takes_value(true)
.validator(|input| {
FromStrParser::<u32>::default()
.validate(input.clone())
.or_else(|_| {
let input =
if input.starts_with("0x") || input.starts_with("0X") {
&input[2..]
} else {
&input[..]
};
u32::from_str_radix(input, 16)
.map(|_| ())
.map_err(|err| err.to_string())
})
})
.required(true)
.help("The compact target value"),
),
SubCommand::with_name("difficulty-to-compact")
.about("Convert difficulty value to compact target value")
.arg(
Arg::with_name("difficulty")
.long("difficulty")
.takes_value(true)
.validator(|input| {
let input = if input.starts_with("0x") || input.starts_with("0X") {
&input[2..]
} else {
&input[..]
};
U256::from_hex_str(input)
.map(|_| ())
.map_err(|err| err.to_string())
})
.required(true)
.help("The difficulty value"),
),
SubCommand::with_name("key-info")
.about(
"Show public information of a secp256k1 private key (from file) or public key",
)
.arg(arg_privkey.clone().conflicts_with("pubkey"))
.arg(arg_pubkey.clone().required(false))
.arg(arg_address.clone().required(false))
.arg(arg_lock_arg.clone()),
SubCommand::with_name("eaglesong")
.about("Hash binary use eaglesong algorithm")
.arg(binary_hex_arg.clone()),
SubCommand::with_name("blake2b")
.about("Hash binary use blake2b algorithm (personalization: 'ckb-default-hash')")
.arg(binary_hex_arg.clone())
.arg(
Arg::with_name("prefix-160")
.long("prefix-160")
.help("Only show prefix 160 bits (Example: calculate lock_arg from pubkey)")
),
SubCommand::with_name("compact-to-difficulty")
.about("Convert compact target value to difficulty value")
.arg(Arg::with_name("compact-target")
.long("compact-target")
.takes_value(true)
.validator(|input| {
FromStrParser::<u32>::default()
.validate(input.clone())
.or_else(|_| {
let input = if input.starts_with("0x") || input.starts_with("0X") {
&input[2..]
} else {
&input[..]
};
u32::from_str_radix(input, 16).map(|_| ()).map_err(|err| err.to_string())
})
})
.required(true)
.help("The compact target value")
),
SubCommand::with_name("difficulty-to-compact")
.about("Convert difficulty value to compact target value")
.arg(Arg::with_name("difficulty")
.long("difficulty")
.takes_value(true)
.validator(|input| {
let input = if input.starts_with("0x") || input.starts_with("0X") {
&input[2..]
} else {
&input[..]
};
U256::from_hex_str(input).map(|_| ()).map_err(|err| err.to_string())
})
.required(true)
.help("The difficulty value")
),
SubCommand::with_name("to-genesis-multisig-addr")
.about("Convert address in single signature format to multisig format (only for mainnet genesis cells)")
.arg(
Expand Down Expand Up @@ -218,6 +228,22 @@ message = "0x"
});
Ok(resp.render(format, color))
}
("eaglesong", Some(m)) => {
let binary: Vec<u8> = HexParser.from_matches(m, "binary-hex")?;
let mut builder = EagleSongBuilder::new();
builder.update(&binary);
Ok(format!("{:#x}", H256::from(builder.finalize())))
}
("blake2b", Some(m)) => {
let binary: Vec<u8> = HexParser.from_matches(m, "binary-hex")?;
let hash_data = blake2b_256(&binary);
let slice = if m.is_present("prefix-160") {
&hash_data[0..20]
} else {
&hash_data[..]
};
Ok(format!("0x{}", hex_string(slice).unwrap()))
}
("compact-to-difficulty", Some(m)) => {
let compact_target: u32 = FromStrParser::<u32>::default()
.from_matches(m, "compact-target")
Expand Down

0 comments on commit 143a2d1

Please sign in to comment.