Skip to content

Commit

Permalink
Add recover-public-key to forc-crypto
Browse files Browse the repository at this point in the history
This function is ported from FuelLabs/forc-wallet#152
  • Loading branch information
crodas committed Jan 10, 2024
1 parent fb06a8f commit 84bb5a3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 20 deletions.
19 changes: 11 additions & 8 deletions forc-plugins/forc-crypto/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,19 @@ fn read_as_binary(content: &Option<String>) -> Vec<u8> {
/// fully valid hex strings are accepted.
/// 7. Any other string, or any malformed hex string will be treated as a
/// vector of bytes
pub fn read_content_or_filepath(arg: Option<String>) -> Vec<u8> {
match checked_read_file(&arg) {
Some(bytes) => bytes,
None => match checked_read_stdin(&arg, io::stdin().lock()) {
Some(bytes) => bytes,
None => read_as_binary(&arg),
},
}
}

impl From<HashArgs> for Vec<u8> {
fn from(value: HashArgs) -> Self {
let arg = value.content_or_filepath;
match checked_read_file(&arg) {
Some(bytes) => bytes,
None => match checked_read_stdin(&arg, io::stdin().lock()) {
Some(bytes) => bytes,
None => read_as_binary(&arg),
},
}
read_content_or_filepath(value.content_or_filepath)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::ValueEnum;

pub mod new_key;
pub mod parse_secret;
pub mod recover;

#[derive(Clone, Debug, Default, ValueEnum)]
pub enum KeyType {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//! This file will be hosted here until
//! https://github.com/FuelLabs/sway/issues/5170 is fixed
use super::KeyType;
use anyhow::Result;
use fuel_core_types::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//! This file will be hosted here until
//! https://github.com/FuelLabs/sway/issues/5170 is fixed
use super::KeyType;
use anyhow::Result;
use fuel_core_types::{fuel_crypto::SecretKey, fuel_tx::Input};
Expand Down
42 changes: 42 additions & 0 deletions forc-plugins/forc-crypto/src/keys/recover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::args::read_content_or_filepath;
use anyhow::Result;
use fuel_crypto::{fuel_types::Address, Message, Signature};
use fuels_core::types::bech32::Bech32Address;
use serde_json::json;

forc_util::cli_examples! {
[ Recovers a public key from a message and its signature => crypto r#"recover-public-key \
0xb0b2f29b52d95c1cba47ea7c7edeec6c84a0bd196df489e219f6f388b69d760479b994f4bae2d5f2abef7d5faf7d9f5ee3ea47ada4d15b7a7ee2777dcd7b36bb \
"Blah blah blah""#]
}

/// Parse a secret key to view the associated public key
#[derive(Debug, clap::Args)]
#[clap(
author,
version,
about = "Recovers a public key from a message and its signature",
after_long_help = help(),
)]
pub struct Arg {
/// A private key in hex format
signature: Signature,
/// A message
message: Option<String>,
}

pub fn handler(arg: Arg) -> Result<serde_json::Value> {
let message = Message::new(read_content_or_filepath(arg.message));
let public_key = Signature::recover(&arg.signature, &message)?;

let bytes = *public_key.hash();

let bech32 = Bech32Address::from(Address::from(bytes));
let addr = Address::from(bytes);

Ok(json!({
"PublicKey": public_key.to_string(),
"Bench32": bech32.to_string(),
"Address": addr.to_string(),
}))
}
19 changes: 11 additions & 8 deletions forc-plugins/forc-crypto/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ use termion::screen::IntoAlternateScreen;
mod address;
mod args;
mod keccak256;
mod keygen;
mod keys;
mod sha256;

const ABOUT: &str = "Forc plugin for hashing arbitrary data.";

fn help() -> &'static str {
Box::leak(
format!(
"EXAMPLES:\n{}{}{}{}",
"EXAMPLES:\n{}{}{}{}{}",
args::examples(),
address::examples(),
keygen::new_key::examples(),
keygen::parse_secret::examples(),
keys::new_key::examples(),
keys::parse_secret::examples(),
keys::recover::examples(),
)
.into_boxed_str(),
)
Expand All @@ -42,8 +43,9 @@ pub enum Command {
Keccak256(args::HashArgs),
Sha256(args::HashArgs),
Address(address::Args),
NewKey(keygen::new_key::Arg),
ParseSecret(keygen::parse_secret::Arg),
RecoverPublicKey(keys::recover::Arg),
NewKey(keys::new_key::Arg),
ParseSecret(keys::parse_secret::Arg),
}

fn main() {
Expand All @@ -58,10 +60,11 @@ fn run() -> Result<()> {
let app = Command::parse();
let content = match app {
Command::Keccak256(arg) => keccak256::hash(arg)?,
Command::RecoverPublicKey(arg) => keys::recover::handler(arg)?,
Command::Sha256(arg) => sha256::hash(arg)?,
Command::Address(arg) => address::dump_address(arg.address)?,
Command::NewKey(arg) => keygen::new_key::handler(arg)?,
Command::ParseSecret(arg) => keygen::parse_secret::handler(arg)?,
Command::NewKey(arg) => keys::new_key::handler(arg)?,
Command::ParseSecret(arg) => keys::parse_secret::handler(arg)?,
};

display_output(content)
Expand Down

0 comments on commit 84bb5a3

Please sign in to comment.