Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cli] Display the transaction info when sign tx #2619

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion crates/rooch/src/commands/transaction/commands/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use super::{FileOutput, FileOutputData};
use crate::cli_types::{CommandAction, FileOrHexInput, WalletContextOptions};
use crate::utils::prompt_yes_no;
use async_trait::async_trait;
use moveos_types::module_binding::MoveFunctionCaller;
use rooch_key::keystore::account_keystore::AccountKeystore;
Expand Down Expand Up @@ -50,7 +51,6 @@ impl SignInput {
}
}
}

pub enum SignOutput {
SignedRoochTransaction(RoochTransaction),
PartiallySignedRoochTransaction(PartiallySignedRoochTransaction),
Expand Down Expand Up @@ -91,6 +91,10 @@ pub struct SignCommand {
#[clap(long, short = 'o')]
output: Option<String>,

/// Automatically answer 'yes' to all prompts
#[clap(long = "yes", short = 'y')]
answer_yes: bool,

/// Return command outputs in json format
#[clap(long, default_value = "false")]
json: bool,
Expand Down Expand Up @@ -175,11 +179,47 @@ impl SignCommand {
};
Ok(output)
}

fn print_tx_details(input: &SignInput) {
let tx_data = |tx_data: &RoochTransactionData| -> String {
format!(
" Sender: {}\n Sequence number: {}\n Chain id: {}\n Max gas amount: {}\n Action: {}\n Transaction hash: {}\n",
tx_data.sender,
tx_data.sequence_number,
tx_data.chain_id,
tx_data.max_gas_amount,
tx_data.action,
tx_data.tx_hash()
)
};

match input {
SignInput::RoochTransactionData(tx) => {
println!("Transaction data:\n{}", tx_data(tx));
}
SignInput::PartiallySignedRoochTransaction(pstx) => {
println!(
"Partially signed transaction data:\n{}",
tx_data(&pstx.data)
);
println!(
" Collected signatures: {}/{}",
pstx.authenticators.len(),
pstx.threshold
);
}
}
}
}

#[async_trait]
impl CommandAction<Option<FileOutput>> for SignCommand {
async fn execute(self) -> RoochResult<Option<FileOutput>> {
let sign_input = SignInput::try_from(self.input.clone())?;
SignCommand::print_tx_details(&sign_input);
if !self.answer_yes && !prompt_yes_no("Do you want to sign this transaction?") {
return Ok(None);
}
let json = self.json;
let output = self.output.clone();
let sign_output = self.sign().await?;
Expand Down
17 changes: 17 additions & 0 deletions crates/rooch/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,20 @@ pub fn read_line() -> Result<String, anyhow::Error> {
io::stdin().read_line(&mut s)?;
Ok(s.trim_end().to_string())
}

pub fn prompt_yes_no(question: &str) -> bool {
loop {
println!("{} [yes/no] > ", question);

let Ok(input) = read_line() else {
println!("Please answer yes or no.");
continue;
};

match input.trim_start().to_lowercase().as_str() {
"yes" | "y" => return true,
"no" | "n" => return false,
_ => println!("Please answer yes or no."),
}
}
}
2 changes: 1 addition & 1 deletion crates/testsuite/features/cmd.feature
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Feature: Rooch CLI integration tests
Then cmd: "transaction get-transactions-by-hash --hashes {{$.transaction[-1].data[0].execution_info.tx_hash}}"
Then cmd: "transaction build --function rooch_framework::empty::empty --json"
Then assert: "'{{$.transaction[-1]}}' not_contains error"
Then cmd: "transaction sign {{$.transaction[-1].path}} --json"
Then cmd: "transaction sign {{$.transaction[-1].path}} --json -y"
Then assert: "'{{$.transaction[-1]}}' not_contains error"
Then cmd: "transaction submit {{$.transaction[-1].path}}"
Then assert: "{{$.transaction[-1].execution_info.status.type}} == executed"
Expand Down
4 changes: 2 additions & 2 deletions crates/testsuite/features/multisign.feature
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ Feature: Rooch CLI multisign integration tests
# l2 transaction
Then cmd: "tx build --sender {{$.account[-3].multisign_address}} --function rooch_framework::empty::empty --json"
Then assert: "'{{$.tx[-1]}}' not_contains error"
Then cmd: "tx sign {{$.tx[-1].path}} -s {{$.account[-3].participants[0].participant_address}} --json"
Then cmd: "tx sign {{$.tx[-1].path}} -s {{$.account[-3].participants[0].participant_address}} --json -y"
Then assert: "'{{$.tx[-1]}}' not_contains error"
Then cmd: "tx sign {{$.tx[-1].path}} -s {{$.account[-3].participants[1].participant_address}} --json"
Then cmd: "tx sign {{$.tx[-1].path}} -s {{$.account[-3].participants[1].participant_address}} --json -y"
Then assert: "'{{$.tx[-1]}}' not_contains error"
Then cmd: "tx submit {{$.tx[-1].path}} --json"
Then assert: "{{$.tx[-1].execution_info.status.type}} == executed"
Expand Down
2 changes: 1 addition & 1 deletion moveos/moveos-types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl Display for MoveAction {
}
write!(
f,
"MoveAction::FunctionCall( function_id: 0x{:?}, type_args: {:?}, args: {:?})",
"MoveAction::FunctionCall( function_id: {}, type_args: {:?}, args: {:?})",
function.function_id, function.ty_args, arg_list
)
}
Expand Down
Loading