-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description Add recover-public-key to forc-crypto This function is ported from FuelLabs/forc-wallet#152 This PR depends on #5388 ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
- Loading branch information
Showing
13 changed files
with
100 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use crate::args::read_content_filepath_or_stdin; | ||
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! { | ||
[ Get the public key from a message and its signature => crypto r#"get-public-key \ | ||
0x1eff08081394b72239a0cf7ff6b499213dcb7a338bedbd75d072d504588ef27a1f74d5ceb2f111ec02ede097fb09ed00aa9867922ed39299dae0b1afc0fa8661 \ | ||
"This is a message that is signed""# ] | ||
} | ||
|
||
/// Parse a secret key to view the associated public key | ||
#[derive(Debug, clap::Args)] | ||
#[clap( | ||
author, | ||
version, | ||
about = "Get the 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_filepath_or_stdin(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(), | ||
"Bech32": bech32.to_string(), | ||
"Address": addr.to_string(), | ||
})) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use std::str::FromStr; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn expect_output() { | ||
let arg = Arg { | ||
signature: Signature::from_str("0x1eff08081394b72239a0cf7ff6b499213dcb7a338bedbd75d072d504588ef27a1f74d5ceb2f111ec02ede097fb09ed00aa9867922ed39299dae0b1afc0fa8661").unwrap(), | ||
message: Some("This is a message that is signed".to_string()), | ||
}; | ||
let json = handler(arg).unwrap(); | ||
assert_eq!( | ||
"fuel1fmmfhjapeak3knq96arrvttwrtmzghe0w9gx79gkcl2jhaweakdqfqhzdr", | ||
json.as_object() | ||
.unwrap() | ||
.get("Bech32") | ||
.unwrap() | ||
.as_str() | ||
.unwrap(), | ||
) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
forc-plugins/forc-crypto/src/keygen/mod.rs → forc-plugins/forc-crypto/src/keys/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use clap::ValueEnum; | ||
|
||
pub mod get_public_key; | ||
pub mod new_key; | ||
pub mod parse_secret; | ||
|
||
|
2 changes: 0 additions & 2 deletions
2
...plugins/forc-crypto/src/keygen/new_key.rs → forc-plugins/forc-crypto/src/keys/new_key.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
...ns/forc-crypto/src/keygen/parse_secret.rs → ...gins/forc-crypto/src/keys/parse_secret.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.