Skip to content

Commit

Permalink
[rooch-networkgh-2381] revise the signature hex and verify command.
Browse files Browse the repository at this point in the history
  • Loading branch information
Feliciss committed Sep 8, 2024
1 parent 3a2ebdd commit 47c5a51
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
5 changes: 4 additions & 1 deletion crates/rooch-types/src/rooch_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ impl ParsedSignature {
}

pub fn parse(s: &str) -> anyhow::Result<Self, anyhow::Error> {
Ok(Self::from_signature(Signature::from_bytes(s.as_bytes())?))
let signature_bytes = hex::decode(s)?;
Ok(Self::from_signature(Signature::from_bytes(
&signature_bytes,
)?))
}
}
15 changes: 10 additions & 5 deletions crates/rooch/src/commands/account/commands/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use moveos_types::state::MoveState;
use rooch_key::keystore::account_keystore::AccountKeystore;
use rooch_types::{
address::ParsedAddress,
crypto::Signature,
error::RoochResult,
framework::auth_payload::{SignData, MESSAGE_INFO_PREFIX},
};
Expand All @@ -33,8 +32,8 @@ pub struct SignCommand {
}

#[async_trait]
impl CommandAction<Option<Signature>> for SignCommand {
async fn execute(self) -> RoochResult<Option<Signature>> {
impl CommandAction<Option<String>> for SignCommand {
async fn execute(self) -> RoochResult<Option<String>> {
let context = self.context_options.build_require_password()?;
let password = context.get_password();
let mapping = context.address_mapping();
Expand All @@ -49,10 +48,16 @@ impl CommandAction<Option<Signature>> for SignCommand {
.keystore
.sign_hashed(&rooch_address, &encoded_sign_data, password)?;

let signature_bytes = signature.as_ref();
let signature_hex = hex::encode(signature_bytes);

if self.json {
Ok(Some(signature))
Ok(Some(signature_hex))
} else {
println!("Sign message succeeded with the signatue {:?}", signature);
println!(
"Sign message succeeded with the signatue {:?}",
signature_hex
);
Ok(None)
}
}
Expand Down
23 changes: 3 additions & 20 deletions crates/rooch/src/commands/account/commands/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
use crate::cli_types::{CommandAction, WalletContextOptions};
use async_trait::async_trait;
use clap::Parser;
use fastcrypto::{
hash::Sha256,
secp256k1::{Secp256k1PublicKey, Secp256k1Signature},
traits::ToFromBytes,
};
use moveos_types::{h256::sha2_256_of, state::MoveState};
use rooch_types::{
crypto::{RoochSignature, Signature},
error::{RoochError, RoochResult},
crypto::RoochSignature,
error::RoochResult,
framework::auth_payload::{SignData, MESSAGE_INFO_PREFIX},
rooch_signature::ParsedSignature,
};
Expand All @@ -39,23 +34,11 @@ pub struct VerifyCommand {
#[async_trait]
impl CommandAction<Option<bool>> for VerifyCommand {
async fn execute(self) -> RoochResult<Option<bool>> {
let signatrue =
Signature::from_bytes(self.signature.into_inner().as_ref()).map_err(|e| {
RoochError::CommandArgumentError(format!("Invalid signature argument: {}", e))
})?;
let pk = Secp256k1PublicKey::from_bytes(signatrue.public_key_bytes())
.map_err(|e| RoochError::CommandArgumentError(format!("Invalid public key: {}", e)))?;
let sig = Secp256k1Signature::from_bytes(signatrue.signature_bytes()).map_err(|e| {
RoochError::CommandArgumentError(format!("Invalid signature argument: {}", e))
})?;

let sign_data =
SignData::new_without_tx_hash(MESSAGE_INFO_PREFIX.to_vec(), self.message.to_bytes());
let encoded_sign_data = sign_data.encode();
let message_hash = sha2_256_of(&encoded_sign_data).0.to_vec();

pk.verify_with_hash::<Sha256>(&message_hash, &sig)
.map_err(|e| RoochError::CommandArgumentError(format!("Failed verification: {}", e)))?;
let _ = self.signature.into_inner().verify(&message_hash);

if self.json {
Ok(Some(true))
Expand Down
2 changes: 1 addition & 1 deletion crates/testsuite/features/cmd.feature
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Feature: Rooch CLI integration tests
Then cmd: "account list --json"
# account sign and verify
Then cmd: "account sign -a {{$.account[-1].account0.address}} -m 'empty' --json"
Then cmd: "account verify -s {{$.account[-1].signature}} -m {{$.account[-1].message}} --json"
Then cmd: "account verify -s {{$.account[-1]}} -m 'empty' --json"
Then assert: "{{$.account[-1]}} == true"
Then cmd: "account list --json"
Then cmd: "account export"
Expand Down

0 comments on commit 47c5a51

Please sign in to comment.