diff --git a/src/components/finutils/src/bins/fn.rs b/src/components/finutils/src/bins/fn.rs index 83c45275f..633952307 100644 --- a/src/components/finutils/src/bins/fn.rs +++ b/src/components/finutils/src/bins/fn.rs @@ -330,6 +330,10 @@ fn run() -> Result<()> { } else { common::setup(sa, om, tp).c(d!())?; } + } else if let Some(m) = matches.subcommand_matches("sign") { + let sk = m.value_of("sk"); + let msg = m.value_of("message"); + common::sign(sk, msg).c(d!())?; } else if let Some(m) = matches.subcommand_matches("transfer") { let f = read_file_path(m.value_of("from-seckey")).c(d!())?; let asset = m.value_of("asset").unwrap_or("FRA"); diff --git a/src/components/finutils/src/bins/fn.yml b/src/components/finutils/src/bins/fn.yml index 02043a839..a5e6e9d75 100644 --- a/src/components/finutils/src/bins/fn.yml +++ b/src/components/finutils/src/bins/fn.yml @@ -46,6 +46,23 @@ subcommands: long: validator-key takes_value: true value_name: Path + - sign: + about: Sign message + args: + - sk: + help: secret key in hex + short: s + long: secret-key + takes_value: true + value_name: SecretKey + required: true + - message: + help: the message string + short: m + long: message + takes_value: true + value_name: Message + required: true - stake: about: Stake tokens (i.e. bond tokens) from a Findora account to a validator args: diff --git a/src/components/finutils/src/common/mod.rs b/src/components/finutils/src/common/mod.rs index 55e57f6b8..86fa6bb64 100644 --- a/src/components/finutils/src/common/mod.rs +++ b/src/components/finutils/src/common/mod.rs @@ -6,8 +6,10 @@ //! This module is the library part of FN. //! +use sha2::{Digest, Sha256}; use std::str::FromStr; - +use zei::noah_algebra::serialization::NoahFromToBytes; +use zei::noah_api::keys::SecretKey; #[cfg(not(target_arch = "wasm32"))] pub mod dev; @@ -384,6 +386,27 @@ pub fn show(basic: bool, is_address_eth: bool) -> Result<()> { Ok(()) } +/// Sign message +/// Secret Key +/// Message +pub fn sign(secret_key: Option<&str>, message: Option<&str>) -> Result<()> { + let msg_hash = Sha256::digest(message.unwrap().as_bytes()); + + let binding = hex::decode(secret_key.unwrap()).unwrap(); + let sk_bytes = binding.as_slice(); + let sk = XfrSecretKey::from_noah(&SecretKey::noah_from_bytes(sk_bytes).unwrap()); + let pair = sk.into_keypair(); + let sig = pair.get_sk_ref().sign(msg_hash.as_slice()).unwrap(); + println!( + "data: 0x{}{}{}", + hex::encode(pair.get_pk_ref().noah_to_bytes()), + hex::encode(msg_hash.as_slice()), + hex::encode(sig.noah_to_bytes()) + ); + + Ok(()) +} + /// Setup for a cli tool /// Server URL /// Owner mnemonic path