Skip to content

Commit

Permalink
[rooch-networkgh-2381] revise account verify command and add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Feliciss committed Sep 5, 2024
1 parent 4802983 commit 88f4a28
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
33 changes: 17 additions & 16 deletions crates/rooch/src/commands/account/commands/verify.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::cli_types::{CommandAction, WalletContextOptions};
use crate::cli_types::{CommandAction, TransactionOptions, WalletContextOptions};
use async_trait::async_trait;
use clap::Parser;
use rooch_key::keystore::account_keystore::AccountKeystore;
use rooch_types::{
error::{RoochError, RoochResult},
framework::auth_payload::AuthPayload, transaction::RoochTransactionData,
framework::auth_payload::AuthPayload,
transaction::{Authenticator, RoochTransactionData},
};

/// Verify a tx with a auth payload
#[derive(Debug, Parser)]
pub struct VerifyCommand {
/// tx data hex
/// input for tx data hex
#[clap(long, required = true)]
tx_data: String,
input: String,

/// auth payload hex
#[clap(long, required = true)]
auth_payload: String,
#[clap(flatten)]
pub tx_options: TransactionOptions,

#[clap(flatten)]
pub context_options: WalletContextOptions,
Expand All @@ -31,21 +32,21 @@ pub struct VerifyCommand {
#[async_trait]
impl CommandAction<Option<String>> for VerifyCommand {
async fn execute(self) -> RoochResult<Option<String>> {
let tx_data_bytes = hex::decode(&self.tx_data).map_err(|e| {
let context = self.context_options.build_require_password()?;
let password = context.get_password();
let sender = context.resolve_address(self.tx_options.sender)?.into();
let kp = context.keystore.get_key_pair(&sender, password)?;

let tx_data_bytes = hex::decode(&self.input).map_err(|e| {
RoochError::CommandArgumentError(format!(
"Failed to decode tx hex: {}, err:{:?}",
self.tx_data, e
))
})?;
let auth_payload_bytes = hex::decode(&self.auth_payload).map_err(|e| {
RoochError::CommandArgumentError(format!(
"Failed to decode auth payload hex: {}, err:{:?}",
self.auth_payload, e
self.input, e
))
})?;

let tx_data = RoochTransactionData::decode(&tx_data_bytes)?;
let auth_payload = bcs::from_bytes::<AuthPayload>(&auth_payload_bytes)?;
let auth = Authenticator::bitcoin(&kp, &tx_data);
let auth_payload = bcs::from_bytes::<AuthPayload>(&auth.payload).unwrap();
let _ = auth_payload.verify(&tx_data);

if self.json {
Expand Down
6 changes: 6 additions & 0 deletions crates/testsuite/features/cmd.feature
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ Feature: Rooch CLI integration tests
# alias tx for transaction
Then cmd: "tx get-transactions-by-order --cursor 1 --limit 2 --descending-order true"

# account sign
Then cmd: "account sign --function 0x3::empty::empty --json"

# account verify
Then cmd: "account verify --input {{$.account[-1].tx_data}}"

# account balance
Then cmd: "account balance"
Then cmd: "account balance --coin-type rooch_framework::gas_coin::RGas"
Expand Down

0 comments on commit 88f4a28

Please sign in to comment.