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 bitcoin tx detail #2629

Merged
merged 2 commits into from
Sep 14, 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
86 changes: 82 additions & 4 deletions crates/rooch/src/commands/bitcoin/sign_tx.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::utils::prompt_yes_no;
use crate::{
cli_types::{CommandAction, FileOrHexInput, WalletContextOptions},
commands::bitcoin::{FileOutput, FileOutputData},
Expand All @@ -11,7 +12,7 @@ use async_trait::async_trait;
use bitcoin::{
key::{Keypair, Secp256k1, TapTweak},
sighash::{Prevouts, SighashCache},
Psbt, TapLeafHash, TapSighashType, Witness,
Address, Network, Psbt, TapLeafHash, TapSighashType, Witness,
};
use clap::Parser;
use moveos_types::module_binding::MoveFunctionCaller;
Expand Down Expand Up @@ -39,6 +40,14 @@ pub struct SignTx {
#[clap(long)]
output_file: Option<String>,

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

/// The Bitcoin network to use (mainnet, testnet, regtest, signet)
#[clap(long, short = 'n', value_parser = parse_network)]
network: Option<Network>,

#[clap(flatten)]
pub(crate) context_options: WalletContextOptions,
}
Expand All @@ -50,12 +59,16 @@ pub enum SignOutput {
}

#[async_trait]
impl CommandAction<FileOutput> for SignTx {
async fn execute(self) -> RoochResult<FileOutput> {
impl CommandAction<Option<FileOutput>> for SignTx {
async fn execute(self) -> RoochResult<Option<FileOutput>> {
let context = self.context_options.build_require_password()?;
let client = context.get_client().await?;

let psbt = Psbt::deserialize(&self.input.data)?;
print_transaction_details(&psbt, self.network);
if !self.answer_yes && !prompt_yes_no("Do you want to sign this transaction?") {
return Ok(None);
}
debug!("psbt before sign: {:?}", psbt);
let output = sign_psbt(psbt, self.signer, &context, &client).await?;
debug!("sign output: {:?}", output);
Expand All @@ -65,7 +78,7 @@ impl CommandAction<FileOutput> for SignTx {
SignOutput::Tx(tx) => FileOutputData::Tx(tx),
};
let output = FileOutput::write_to_file(file_output_data, self.output_file)?;
Ok(output)
Ok(Some(output))
}
}

Expand Down Expand Up @@ -237,3 +250,68 @@ fn is_psbt_finalized(psbt: &Psbt) -> bool {
.iter()
.all(|input| input.final_script_sig.is_some() || input.final_script_witness.is_some())
}

fn print_transaction_details(psbt: &Psbt, network: Option<Network>) {
println!("Transaction details before signing:");
println!(" Version: {}", psbt.unsigned_tx.version);
println!(" Lock time: {}", psbt.unsigned_tx.lock_time);
if let Some(net) = network {
println!(" Network: {}", net);
}
println!(" Inputs:");
for (i, (unsigned_input, psbt_input)) in psbt
.unsigned_tx
.input
.iter()
.zip(psbt.inputs.iter())
.enumerate()
{
println!(" Input {}:", i);
println!(
" Previous output: {}:{}",
unsigned_input.previous_output.txid, unsigned_input.previous_output.vout
);
println!(" Sequence: {}", unsigned_input.sequence);

if let Some(witness_utxo) = &psbt_input.witness_utxo {
print_script_or_address(&witness_utxo.script_pubkey, network);
} else if let Some(non_witness_utxo) = &psbt_input.non_witness_utxo {
let vout = unsigned_input.previous_output.vout as usize;
if vout < non_witness_utxo.output.len() {
print_script_or_address(&non_witness_utxo.output[vout].script_pubkey, network);
}
} else {
println!(" Script pubkey: Unable to determine (no UTXO information)");
}
}
println!(" Outputs:");
for (i, output) in psbt.unsigned_tx.output.iter().enumerate() {
println!(" Output {}:", i);
println!(" Value: {}", output.value);
print_script_or_address(&output.script_pubkey, network);
}
println!();
}

fn print_script_or_address(script_pubkey: &bitcoin::ScriptBuf, network: Option<Network>) {
if let Some(net) = network {
if let Ok(address) = Address::from_script(script_pubkey, net) {
println!(" Address: {}", address);
return;
}
}
println!(" Script pubkey: {}", script_pubkey);
}

fn parse_network(s: &str) -> Result<Network, String> {
match s.to_lowercase().as_str() {
"mainnet" => Ok(Network::Bitcoin),
"testnet" => Ok(Network::Testnet),
"regtest" => Ok(Network::Regtest),
"signet" => Ok(Network::Signet),
_ => Err(format!(
"Invalid network: {}. Valid options are: mainnet, testnet, regtest, signet",
s
)),
}
}
4 changes: 2 additions & 2 deletions crates/testsuite/features/multisign.feature
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ Feature: Rooch CLI multisign integration tests
# l1 transaction
Then cmd: "bitcoin build-tx --sender {{$.account[-1].multisign_bitcoin_address}} -o {{$.account[-2].account0.bitcoin_address}}:100000000"
Then assert: "'{{$.bitcoin[-1]}}' not_contains error"
Then cmd: "bitcoin sign-tx -s {{$.account[-1].participants[0].participant_address}} {{$.bitcoin[-1].path}}"
Then cmd: "bitcoin sign-tx -s {{$.account[-1].participants[0].participant_address}} {{$.bitcoin[-1].path}} -y"
Then assert: "'{{$.bitcoin[-1]}}' not_contains error"
Then cmd: "bitcoin sign-tx -s {{$.account[-1].participants[2].participant_address}} {{$.bitcoin[-1].path}}"
Then cmd: "bitcoin sign-tx -s {{$.account[-1].participants[2].participant_address}} {{$.bitcoin[-1].path}} -y"
Then assert: "'{{$.bitcoin[-1]}}' not_contains error"
Then cmd: "bitcoin broadcast-tx {{$.bitcoin[-1].path}}"
Then assert: "'{{$.bitcoin[-1]}}' not_contains error"
Expand Down
Loading