From b91a83597f3678bc8d282add68c708bfeb4cde15 Mon Sep 17 00:00:00 2001 From: Kaya Gokalp Date: Tue, 16 Jan 2024 16:27:41 -0800 Subject: [PATCH 1/7] fix: depend on std via path for forc-doc, forc-fmt and forc-tx cli tests --- forc-plugins/forc-doc/tests/Forc.lock | 13 +++++++++++++ forc-plugins/forc-doc/tests/Forc.toml | 1 + forc-plugins/forc-doc/tests_project2/Forc.lock | 13 +++++++++++++ forc-plugins/forc-doc/tests_project2/Forc.toml | 1 + forc-plugins/forc-fmt/tests/Forc.lock | 13 +++++++++++++ forc-plugins/forc-fmt/tests/Forc.toml | 1 + forc-plugins/forc-tx/tests/Forc.lock | 4 ++-- forc-plugins/forc-tx/tests/Forc.toml | 1 + forc-plugins/forc-tx/tests/out/debug/tests.bin | Bin 68 -> 84 bytes 9 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 forc-plugins/forc-doc/tests/Forc.lock create mode 100644 forc-plugins/forc-doc/tests_project2/Forc.lock create mode 100644 forc-plugins/forc-fmt/tests/Forc.lock diff --git a/forc-plugins/forc-doc/tests/Forc.lock b/forc-plugins/forc-doc/tests/Forc.lock new file mode 100644 index 00000000000..69b439832fa --- /dev/null +++ b/forc-plugins/forc-doc/tests/Forc.lock @@ -0,0 +1,13 @@ +[[package]] +name = "core" +source = "path+from-root-F252333F9C4A5D78" + +[[package]] +name = "std" +source = "path+from-root-F252333F9C4A5D78" +dependencies = ["core"] + +[[package]] +name = "tests" +source = "member" +dependencies = ["std"] diff --git a/forc-plugins/forc-doc/tests/Forc.toml b/forc-plugins/forc-doc/tests/Forc.toml index ba9dcc1577f..b7d241e5420 100644 --- a/forc-plugins/forc-doc/tests/Forc.toml +++ b/forc-plugins/forc-doc/tests/Forc.toml @@ -5,3 +5,4 @@ license = "Apache-2.0" name = "tests" [dependencies] +std = { path = "../../../sway-lib-std/" } diff --git a/forc-plugins/forc-doc/tests_project2/Forc.lock b/forc-plugins/forc-doc/tests_project2/Forc.lock new file mode 100644 index 00000000000..080998fb7b5 --- /dev/null +++ b/forc-plugins/forc-doc/tests_project2/Forc.lock @@ -0,0 +1,13 @@ +[[package]] +name = "core" +source = "path+from-root-A1AE67D36B4D77A0" + +[[package]] +name = "inner" +source = "member" +dependencies = ["std"] + +[[package]] +name = "std" +source = "path+from-root-A1AE67D36B4D77A0" +dependencies = ["core"] diff --git a/forc-plugins/forc-doc/tests_project2/Forc.toml b/forc-plugins/forc-doc/tests_project2/Forc.toml index 2d0e09e18ef..941145a3aaa 100644 --- a/forc-plugins/forc-doc/tests_project2/Forc.toml +++ b/forc-plugins/forc-doc/tests_project2/Forc.toml @@ -5,3 +5,4 @@ license = "Apache-2.0" name = "inner" [dependencies] +std = { path = "../../../sway-lib-std/" } diff --git a/forc-plugins/forc-fmt/tests/Forc.lock b/forc-plugins/forc-fmt/tests/Forc.lock new file mode 100644 index 00000000000..69b439832fa --- /dev/null +++ b/forc-plugins/forc-fmt/tests/Forc.lock @@ -0,0 +1,13 @@ +[[package]] +name = "core" +source = "path+from-root-F252333F9C4A5D78" + +[[package]] +name = "std" +source = "path+from-root-F252333F9C4A5D78" +dependencies = ["core"] + +[[package]] +name = "tests" +source = "member" +dependencies = ["std"] diff --git a/forc-plugins/forc-fmt/tests/Forc.toml b/forc-plugins/forc-fmt/tests/Forc.toml index ba9dcc1577f..b7d241e5420 100644 --- a/forc-plugins/forc-fmt/tests/Forc.toml +++ b/forc-plugins/forc-fmt/tests/Forc.toml @@ -5,3 +5,4 @@ license = "Apache-2.0" name = "tests" [dependencies] +std = { path = "../../../sway-lib-std/" } diff --git a/forc-plugins/forc-tx/tests/Forc.lock b/forc-plugins/forc-tx/tests/Forc.lock index 6f3c4b77dd7..69b439832fa 100644 --- a/forc-plugins/forc-tx/tests/Forc.lock +++ b/forc-plugins/forc-tx/tests/Forc.lock @@ -1,10 +1,10 @@ [[package]] name = "core" -source = "path+from-root-AC247AEA3D39B916" +source = "path+from-root-F252333F9C4A5D78" [[package]] name = "std" -source = "git+https://github.com/fuellabs/sway?tag=v0.47.0#34265301c6037d51444899a99df1cfc563df6016" +source = "path+from-root-F252333F9C4A5D78" dependencies = ["core"] [[package]] diff --git a/forc-plugins/forc-tx/tests/Forc.toml b/forc-plugins/forc-tx/tests/Forc.toml index ba9dcc1577f..26b2d43474b 100644 --- a/forc-plugins/forc-tx/tests/Forc.toml +++ b/forc-plugins/forc-tx/tests/Forc.toml @@ -5,3 +5,4 @@ license = "Apache-2.0" name = "tests" [dependencies] +std = { path = "../../../sway-lib-std" } diff --git a/forc-plugins/forc-tx/tests/out/debug/tests.bin b/forc-plugins/forc-tx/tests/out/debug/tests.bin index 02725352ba7d6b4b808ad40089621278a9b4c2d7..8461c37f1de4f96729e4eee8af94299d99d2a5d5 100644 GIT binary patch delta 67 zcmZ<>Q7BQ7B Date: Thu, 21 Dec 2023 17:01:20 -0300 Subject: [PATCH 2/7] Add recover-public-key to forc-crypto This function is ported from https://github.com/FuelLabs/forc-wallet/pull/152 --- forc-plugins/forc-crypto/src/args.rs | 19 +++++---- .../forc-crypto/src/{keygen => keys}/mod.rs | 1 + .../src/{keygen => keys}/new_key.rs | 2 - .../src/{keygen => keys}/parse_secret.rs | 2 - forc-plugins/forc-crypto/src/keys/recover.rs | 42 +++++++++++++++++++ forc-plugins/forc-crypto/src/main.rs | 19 +++++---- 6 files changed, 65 insertions(+), 20 deletions(-) rename forc-plugins/forc-crypto/src/{keygen => keys}/mod.rs (95%) rename forc-plugins/forc-crypto/src/{keygen => keys}/new_key.rs (95%) rename forc-plugins/forc-crypto/src/{keygen => keys}/parse_secret.rs (95%) create mode 100644 forc-plugins/forc-crypto/src/keys/recover.rs diff --git a/forc-plugins/forc-crypto/src/args.rs b/forc-plugins/forc-crypto/src/args.rs index 233e86f7c62..7fed8bb391b 100644 --- a/forc-plugins/forc-crypto/src/args.rs +++ b/forc-plugins/forc-crypto/src/args.rs @@ -86,16 +86,19 @@ fn read_as_binary(content: &Option) -> Vec { /// 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) -> Vec { + 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 for Vec { 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) } } diff --git a/forc-plugins/forc-crypto/src/keygen/mod.rs b/forc-plugins/forc-crypto/src/keys/mod.rs similarity index 95% rename from forc-plugins/forc-crypto/src/keygen/mod.rs rename to forc-plugins/forc-crypto/src/keys/mod.rs index 6958367dc3d..90b4de3cc5a 100644 --- a/forc-plugins/forc-crypto/src/keygen/mod.rs +++ b/forc-plugins/forc-crypto/src/keys/mod.rs @@ -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 { diff --git a/forc-plugins/forc-crypto/src/keygen/new_key.rs b/forc-plugins/forc-crypto/src/keys/new_key.rs similarity index 95% rename from forc-plugins/forc-crypto/src/keygen/new_key.rs rename to forc-plugins/forc-crypto/src/keys/new_key.rs index d84ff450eb2..449ee8884f5 100644 --- a/forc-plugins/forc-crypto/src/keygen/new_key.rs +++ b/forc-plugins/forc-crypto/src/keys/new_key.rs @@ -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::{ diff --git a/forc-plugins/forc-crypto/src/keygen/parse_secret.rs b/forc-plugins/forc-crypto/src/keys/parse_secret.rs similarity index 95% rename from forc-plugins/forc-crypto/src/keygen/parse_secret.rs rename to forc-plugins/forc-crypto/src/keys/parse_secret.rs index 309af4fae2a..82f2cbf8c1b 100644 --- a/forc-plugins/forc-crypto/src/keygen/parse_secret.rs +++ b/forc-plugins/forc-crypto/src/keys/parse_secret.rs @@ -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}; diff --git a/forc-plugins/forc-crypto/src/keys/recover.rs b/forc-plugins/forc-crypto/src/keys/recover.rs new file mode 100644 index 00000000000..65e8d653505 --- /dev/null +++ b/forc-plugins/forc-crypto/src/keys/recover.rs @@ -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, +} + +pub fn handler(arg: Arg) -> Result { + 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(), + })) +} diff --git a/forc-plugins/forc-crypto/src/main.rs b/forc-plugins/forc-crypto/src/main.rs index 9a3dd4f3858..79adb613ad5 100644 --- a/forc-plugins/forc-crypto/src/main.rs +++ b/forc-plugins/forc-crypto/src/main.rs @@ -13,7 +13,7 @@ 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."; @@ -21,11 +21,12 @@ 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(), ) @@ -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() { @@ -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) From dc127de93ce0116375be59aeef8d66fccce89ef3 Mon Sep 17 00:00:00 2001 From: Cesar Rodas Date: Tue, 16 Jan 2024 01:07:14 -0300 Subject: [PATCH 3/7] Add better organization to files --- .../forc-crypto/src/keys/{recover.rs => get_public_key.rs} | 4 ++-- forc-plugins/forc-crypto/src/keys/mod.rs | 2 +- forc-plugins/forc-crypto/src/main.rs | 6 +++--- forc-plugins/forc-debug/README.md | 0 forc-plugins/forc-debug/docs/walkthrough.md | 0 forc-plugins/forc-debug/examples/client_usage.rs | 0 forc-plugins/forc-debug/examples/example_tx.json | 0 forc-plugins/forc-debug/src/lib.rs | 0 forc-plugins/forc-debug/src/names.rs | 0 forc-plugins/forc-debug/tests/cli_integration.rs | 0 10 files changed, 6 insertions(+), 6 deletions(-) rename forc-plugins/forc-crypto/src/keys/{recover.rs => get_public_key.rs} (87%) mode change 100755 => 100644 forc-plugins/forc-debug/README.md mode change 100755 => 100644 forc-plugins/forc-debug/docs/walkthrough.md mode change 100755 => 100644 forc-plugins/forc-debug/examples/client_usage.rs mode change 100755 => 100644 forc-plugins/forc-debug/examples/example_tx.json mode change 100755 => 100644 forc-plugins/forc-debug/src/lib.rs mode change 100755 => 100644 forc-plugins/forc-debug/src/names.rs mode change 100755 => 100644 forc-plugins/forc-debug/tests/cli_integration.rs diff --git a/forc-plugins/forc-crypto/src/keys/recover.rs b/forc-plugins/forc-crypto/src/keys/get_public_key.rs similarity index 87% rename from forc-plugins/forc-crypto/src/keys/recover.rs rename to forc-plugins/forc-crypto/src/keys/get_public_key.rs index 65e8d653505..569150e5fee 100644 --- a/forc-plugins/forc-crypto/src/keys/recover.rs +++ b/forc-plugins/forc-crypto/src/keys/get_public_key.rs @@ -5,7 +5,7 @@ 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 \ + [ Get the public key from a message and its signature => crypto r#"get-public-key \ 0xb0b2f29b52d95c1cba47ea7c7edeec6c84a0bd196df489e219f6f388b69d760479b994f4bae2d5f2abef7d5faf7d9f5ee3ea47ada4d15b7a7ee2777dcd7b36bb \ "Blah blah blah""#] } @@ -15,7 +15,7 @@ forc_util::cli_examples! { #[clap( author, version, - about = "Recovers a public key from a message and its signature", + about = "Get the public key from a message and its signature", after_long_help = help(), )] pub struct Arg { diff --git a/forc-plugins/forc-crypto/src/keys/mod.rs b/forc-plugins/forc-crypto/src/keys/mod.rs index 90b4de3cc5a..9741e32623c 100644 --- a/forc-plugins/forc-crypto/src/keys/mod.rs +++ b/forc-plugins/forc-crypto/src/keys/mod.rs @@ -1,8 +1,8 @@ use clap::ValueEnum; +pub mod get_public_key; pub mod new_key; pub mod parse_secret; -pub mod recover; #[derive(Clone, Debug, Default, ValueEnum)] pub enum KeyType { diff --git a/forc-plugins/forc-crypto/src/main.rs b/forc-plugins/forc-crypto/src/main.rs index 79adb613ad5..b872264d4f6 100644 --- a/forc-plugins/forc-crypto/src/main.rs +++ b/forc-plugins/forc-crypto/src/main.rs @@ -26,7 +26,7 @@ fn help() -> &'static str { address::examples(), keys::new_key::examples(), keys::parse_secret::examples(), - keys::recover::examples(), + keys::get_public_key::examples(), ) .into_boxed_str(), ) @@ -43,7 +43,7 @@ pub enum Command { Keccak256(args::HashArgs), Sha256(args::HashArgs), Address(address::Args), - RecoverPublicKey(keys::recover::Arg), + GetPublicKey(keys::get_public_key::Arg), NewKey(keys::new_key::Arg), ParseSecret(keys::parse_secret::Arg), } @@ -60,7 +60,7 @@ 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::GetPublicKey(arg) => keys::get_public_key::handler(arg)?, Command::Sha256(arg) => sha256::hash(arg)?, Command::Address(arg) => address::dump_address(arg.address)?, Command::NewKey(arg) => keys::new_key::handler(arg)?, diff --git a/forc-plugins/forc-debug/README.md b/forc-plugins/forc-debug/README.md old mode 100755 new mode 100644 diff --git a/forc-plugins/forc-debug/docs/walkthrough.md b/forc-plugins/forc-debug/docs/walkthrough.md old mode 100755 new mode 100644 diff --git a/forc-plugins/forc-debug/examples/client_usage.rs b/forc-plugins/forc-debug/examples/client_usage.rs old mode 100755 new mode 100644 diff --git a/forc-plugins/forc-debug/examples/example_tx.json b/forc-plugins/forc-debug/examples/example_tx.json old mode 100755 new mode 100644 diff --git a/forc-plugins/forc-debug/src/lib.rs b/forc-plugins/forc-debug/src/lib.rs old mode 100755 new mode 100644 diff --git a/forc-plugins/forc-debug/src/names.rs b/forc-plugins/forc-debug/src/names.rs old mode 100755 new mode 100644 diff --git a/forc-plugins/forc-debug/tests/cli_integration.rs b/forc-plugins/forc-debug/tests/cli_integration.rs old mode 100755 new mode 100644 From 36db222a4976e00204190264bb8db7fcdcf10552 Mon Sep 17 00:00:00 2001 From: Cesar Rodas Date: Tue, 16 Jan 2024 11:27:58 -0300 Subject: [PATCH 4/7] Update example --- forc-plugins/forc-crypto/src/keys/get_public_key.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/forc-plugins/forc-crypto/src/keys/get_public_key.rs b/forc-plugins/forc-crypto/src/keys/get_public_key.rs index 569150e5fee..74f2bd4b54e 100644 --- a/forc-plugins/forc-crypto/src/keys/get_public_key.rs +++ b/forc-plugins/forc-crypto/src/keys/get_public_key.rs @@ -6,8 +6,8 @@ use serde_json::json; forc_util::cli_examples! { [ Get the public key from a message and its signature => crypto r#"get-public-key \ - 0xb0b2f29b52d95c1cba47ea7c7edeec6c84a0bd196df489e219f6f388b69d760479b994f4bae2d5f2abef7d5faf7d9f5ee3ea47ada4d15b7a7ee2777dcd7b36bb \ - "Blah blah blah""#] + 0x1eff08081394b72239a0cf7ff6b499213dcb7a338bedbd75d072d504588ef27a1f74d5ceb2f111ec02ede097fb09ed00aa9867922ed39299dae0b1afc0fa8661 \ + "This is a message that is signed""# ] } /// Parse a secret key to view the associated public key From ebe7defb08e4ad2f08e3bbd93528e7b91ed598c5 Mon Sep 17 00:00:00 2001 From: Cesar Rodas Date: Thu, 18 Jan 2024 15:31:39 -0300 Subject: [PATCH 5/7] Update doc comments and renamed function --- forc-plugins/forc-crypto/src/args.rs | 29 +++++++++---------- .../forc-crypto/src/keys/get_public_key.rs | 4 +-- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/forc-plugins/forc-crypto/src/args.rs b/forc-plugins/forc-crypto/src/args.rs index 7fed8bb391b..a278b8f453e 100644 --- a/forc-plugins/forc-crypto/src/args.rs +++ b/forc-plugins/forc-crypto/src/args.rs @@ -71,22 +71,16 @@ fn read_as_binary(content: &Option) -> Vec { .unwrap_or_default() } -/// The HashArgs takes no or a single argument, it can be either a string or a -/// path to a file. It can be consumed and converted to a Vec using the From -/// trait. +/// Reads the arg and returns a vector of bytes /// -/// The usage is as follows: -/// 1. Zero or one argument is accepted -/// 2. If no argument is passed, `stdin` is being read -/// 3. The argument will be checked to be a file path, if it is the content -/// will be ded from the file -/// 4. Otherwise, the content is treated as a string -/// 5. If the string is "-", `stdin` is being read -/// 6. If the string starts with "0x", it will be treated as a hex string. Only +/// These are the rules +/// 1. If None, stdin is read. +/// 2. If it is an String and it happens to be a file path, its content will be returned +/// 3. If it is an String and it is "-", stdin is read +/// 4. If the string starts with "0x", it will be treated as a hex string. Only /// 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) -> Vec { +/// 5. Otherwise the String will be converted to a vector of bytes +pub fn read_content_filepath_or_stdin(arg: Option) -> Vec { match checked_read_file(&arg) { Some(bytes) => bytes, None => match checked_read_stdin(&arg, io::stdin().lock()) { @@ -96,9 +90,14 @@ pub fn read_content_or_filepath(arg: Option) -> Vec { } } +/// The HashArgs takes no or a single argument, it can be either a string or a +/// path to a file. It can be consumed and converted to a Vec using the From +/// trait. +/// +/// This is a wrapper around `read_content_filepath_or_stdin` impl From for Vec { fn from(value: HashArgs) -> Self { - read_content_or_filepath(value.content_or_filepath) + read_content_filepath_or_stdin(value.content_or_filepath) } } diff --git a/forc-plugins/forc-crypto/src/keys/get_public_key.rs b/forc-plugins/forc-crypto/src/keys/get_public_key.rs index 74f2bd4b54e..cbf0c6738b6 100644 --- a/forc-plugins/forc-crypto/src/keys/get_public_key.rs +++ b/forc-plugins/forc-crypto/src/keys/get_public_key.rs @@ -1,4 +1,4 @@ -use crate::args::read_content_or_filepath; +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; @@ -26,7 +26,7 @@ pub struct Arg { } pub fn handler(arg: Arg) -> Result { - let message = Message::new(read_content_or_filepath(arg.message)); + let message = Message::new(read_content_filepath_or_stdin(arg.message)); let public_key = Signature::recover(&arg.signature, &message)?; let bytes = *public_key.hash(); From 3dba53b9005a1f0cadf8893142a7ad1dc1e54ab3 Mon Sep 17 00:00:00 2001 From: Cesar Rodas Date: Thu, 18 Jan 2024 15:45:35 -0300 Subject: [PATCH 6/7] Add unit test for expected function --- .../forc-crypto/src/keys/get_public_key.rs | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/forc-plugins/forc-crypto/src/keys/get_public_key.rs b/forc-plugins/forc-crypto/src/keys/get_public_key.rs index cbf0c6738b6..c02dda04a46 100644 --- a/forc-plugins/forc-crypto/src/keys/get_public_key.rs +++ b/forc-plugins/forc-crypto/src/keys/get_public_key.rs @@ -36,7 +36,32 @@ pub fn handler(arg: Arg) -> Result { Ok(json!({ "PublicKey": public_key.to_string(), - "Bench32": bech32.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(), + ) + } +} From dbf7894ff6929f91c97bc445c11db3f34b7899fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20D=2E=20Rodas?= Date: Mon, 22 Jan 2024 11:51:09 -0300 Subject: [PATCH 7/7] Apply suggestions from code review Co-authored-by: Joshua Batty --- forc-plugins/forc-crypto/src/args.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/forc-plugins/forc-crypto/src/args.rs b/forc-plugins/forc-crypto/src/args.rs index a278b8f453e..71872bca9ac 100644 --- a/forc-plugins/forc-crypto/src/args.rs +++ b/forc-plugins/forc-crypto/src/args.rs @@ -75,8 +75,8 @@ fn read_as_binary(content: &Option) -> Vec { /// /// These are the rules /// 1. If None, stdin is read. -/// 2. If it is an String and it happens to be a file path, its content will be returned -/// 3. If it is an String and it is "-", stdin is read +/// 2. If it's a String and it happens to be a file path, its content will be returned +/// 3. If it's a String and it is "-", stdin is read /// 4. If the string starts with "0x", it will be treated as a hex string. Only /// fully valid hex strings are accepted. /// 5. Otherwise the String will be converted to a vector of bytes