Repository navigation
Reject recipient addresses from the wrong network #370
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
Open
emma31-dev
wants to merge
1
commit into
bitcoindevkit:master
Choose a base branch
from
emma31-dev:fix/360-create-tx-network-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+425
−47
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,30 +3,34 @@ use crate::error::BDKCliError as Error; | |
| use crate::handlers::{AppCommand, AppContext, OfflineOperations}; | ||
| use crate::utils::output::FormatOutput; | ||
| use crate::utils::{ | ||
| check_standard_weight, max_signed_weight, parse_address, parse_fee_rate, | ||
| parse_op_return_base64, parse_op_return_string, parse_outpoint, parse_recipient, | ||
| check_standard_weight, max_signed_weight, parse_fee_rate, parse_op_return_base64, | ||
| parse_op_return_string, parse_outpoint, parse_recipient, recipient_script, | ||
| }; | ||
| use bdk_wallet::bitcoin::address::NetworkUnchecked; | ||
| use bdk_wallet::bitcoin::base64::{Engine, prelude::BASE64_STANDARD}; | ||
| use bdk_wallet::bitcoin::{Address, Amount, FeeRate, OutPoint, Psbt, ScriptBuf, Sequence, Txid}; | ||
| use bdk_wallet::bitcoin::{consensus::encode::serialize_hex, script::PushBytesBuf}; | ||
| use bdk_wallet::bitcoin::consensus::encode::serialize_hex; | ||
| use bdk_wallet::bitcoin::script::PushBytesBuf; | ||
| use bdk_wallet::bitcoin::{Address, Amount, FeeRate, OutPoint, Psbt, Sequence, Txid}; | ||
| use bdk_wallet::{KeychainKind, SignOptions}; | ||
| use clap::Parser; | ||
| use serde_json::{Value, json}; | ||
| use std::collections::BTreeMap; | ||
| #[cfg(feature = "silent-payments")] | ||
| use { | ||
| crate::utils::common::parse_sp_code_value_pairs, | ||
| crate::utils::common::{parse_sp_code_value_pairs, sp_code_valid_for_network}, | ||
| bdk_sp::{ | ||
| bitcoin::{PrivateKey, PublicKey}, | ||
| encoding::SilentPaymentCode, | ||
| send::psbt::derive_sp, | ||
| }, | ||
| bdk_wallet::bitcoin::ScriptBuf, | ||
| bdk_wallet::bitcoin::key::Secp256k1, | ||
| bdk_wallet::keys::{DescriptorPublicKey, DescriptorSecretKey, SinglePubKey}, | ||
| std::collections::HashMap, | ||
| }; | ||
| #[cfg(feature = "message_signer")] | ||
| use { | ||
| crate::utils::parse_address, | ||
| crate::utils::parse_signature_format, | ||
| bdk_message_signer::{MessageProof, MessageSigner}, | ||
| }; | ||
|
|
@@ -188,7 +192,7 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for BalanceCommand { | |
| pub struct CreateTxCommand { | ||
| /// Adds a recipient to the transaction. | ||
| #[arg(env = "ADDRESS_SAT", long = "to", required = true, value_parser = parse_recipient)] | ||
| pub recipients: Vec<(ScriptBuf, u64)>, | ||
| pub recipients: Vec<(Address<NetworkUnchecked>, u64)>, | ||
|
|
||
| /// Sends all the funds (or all the selected utxos). Requires only one recipient with value 0. | ||
| #[arg(long = "send_all", short = 'a')] | ||
|
|
@@ -245,13 +249,14 @@ pub struct CreateTxCommand { | |
|
|
||
| impl AppCommand<AppContext<OfflineOperations<'_>>> for CreateTxCommand { | ||
| fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Value, Error> { | ||
| let network = ctx.state.wallet.network(); | ||
| let mut tx_builder = ctx.state.wallet.build_tx(); | ||
|
|
||
| if self.send_all { | ||
| if self.recipients.len() == 1 { | ||
| tx_builder | ||
| .drain_wallet() | ||
| .drain_to(self.recipients[0].0.clone()); | ||
| .drain_to(recipient_script(&self.recipients[0].0, network)?); | ||
| } else { | ||
| return Err(Error::Generic( | ||
| "Wallet can only be drained to a single output".to_string(), | ||
|
|
@@ -260,10 +265,14 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for CreateTxCommand { | |
| } else { | ||
| let recipients = self | ||
| .recipients | ||
| .clone() | ||
| .into_iter() | ||
| .map(|(script, amount)| (script, Amount::from_sat(amount))) | ||
| .collect(); | ||
| .iter() | ||
| .map(|(address, amount)| { | ||
| Ok(( | ||
| recipient_script(address, network)?, | ||
| Amount::from_sat(*amount), | ||
| )) | ||
| }) | ||
| .collect::<Result<_, Error>>()?; | ||
| tx_builder.set_recipients(recipients); | ||
| } | ||
|
|
||
|
|
@@ -320,7 +329,7 @@ pub struct CreateSpTxCommand { | |
| // Clap Doesn't support complex vector parsing https://github.com/clap-rs/clap/issues/1704. | ||
| // Address and amount parsing is done at run time in handler function. | ||
| #[arg(env = "ADDRESS_SAT", long = "to", required = false, value_parser = parse_recipient)] | ||
| pub recipients: Option<Vec<(ScriptBuf, u64)>>, | ||
| pub recipients: Option<Vec<(Address<NetworkUnchecked>, u64)>>, | ||
| /// Parse silent payment recipients | ||
| #[arg(long = "to-sp", required = true, value_parser = parse_sp_code_value_pairs)] | ||
| pub silent_payment_recipients: Vec<(SilentPaymentCode, u64)>, | ||
|
|
@@ -368,13 +377,21 @@ pub struct CreateSpTxCommand { | |
| #[cfg(feature = "silent-payments")] | ||
| impl AppCommand<AppContext<OfflineOperations<'_>>> for CreateSpTxCommand { | ||
| fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Value, Error> { | ||
| let network = ctx.state.wallet.network(); | ||
| let mut tx_builder = ctx.state.wallet.build_tx(); | ||
|
|
||
| let sp_recipients: Vec<SilentPaymentCode> = self | ||
| .silent_payment_recipients | ||
| .iter() | ||
| .map(|(sp_code, _)| sp_code.clone()) | ||
| .collect(); | ||
| .map(|(sp_code, _)| { | ||
| if !sp_code_valid_for_network(sp_code, network) { | ||
| return Err(Error::Generic(format!( | ||
| "Silent payment code {sp_code} is not valid for {network}" | ||
| ))); | ||
| } | ||
| Ok(sp_code.clone()) | ||
| }) | ||
| .collect::<Result<_, Error>>()?; | ||
|
|
||
| if self.send_all { | ||
| if sp_recipients.len() == 1 && self.recipients.is_none() { | ||
|
|
@@ -385,7 +402,9 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for CreateSpTxCommand { | |
| && sp_recipients.is_empty() | ||
| { | ||
| if recipients.len() == 1 { | ||
| tx_builder.drain_wallet().drain_to(recipients[0].0.clone()); | ||
| tx_builder | ||
| .drain_wallet() | ||
| .drain_to(recipient_script(&recipients[0].0, network)?); | ||
| } else { | ||
| return Err(Error::Generic( | ||
| "Wallet can only be drain to a single output".to_string(), | ||
|
|
@@ -409,7 +428,13 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for CreateSpTxCommand { | |
| if let Some(recipients) = &self.recipients { | ||
| let recipients = recipients | ||
| .iter() | ||
| .map(|(script, amount)| (script.clone(), Amount::from_sat(*amount))); | ||
| .map(|(address, amount)| { | ||
| Ok(( | ||
| recipient_script(address, network)?, | ||
| Amount::from_sat(*amount), | ||
| )) | ||
| }) | ||
| .collect::<Result<Vec<_>, Error>>()?; | ||
|
|
||
| outputs.extend(recipients); | ||
| } | ||
|
|
@@ -538,8 +563,8 @@ pub struct BumpFeeCommand { | |
| pub txid: Txid, | ||
|
|
||
| /// Allows the wallet to reduce the amount to the specified address in order to increase fees. | ||
| #[arg(env = "SHRINK_ADDRESS", long = "shrink", value_parser = parse_address)] | ||
| pub shrink_address: Option<Address>, | ||
| #[arg(env = "SHRINK_ADDRESS", long = "shrink")] | ||
| pub shrink_address: Option<Address<NetworkUnchecked>>, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also add a test case for bump_fee too. I think there is a test gap
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will include too. thanks! |
||
|
|
||
| /// Make a PSBT that can be signed by offline signers and hardware wallets. Forces the addition of `non_witness_utxo` and more details to let the signer identify the change output. | ||
| #[arg(long = "offline_signer")] | ||
|
|
@@ -566,14 +591,14 @@ pub struct BumpFeeCommand { | |
|
|
||
| impl AppCommand<AppContext<OfflineOperations<'_>>> for BumpFeeCommand { | ||
| fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Value, Error> { | ||
| let network = ctx.state.wallet.network(); | ||
| let wallet = &mut ctx.state.wallet; | ||
|
|
||
| let mut tx_builder = wallet.build_fee_bump(self.txid)?; | ||
| tx_builder.fee_rate(self.fee_rate); | ||
|
|
||
| if let Some(address) = &self.shrink_address { | ||
| let script_pubkey = address.script_pubkey(); | ||
| tx_builder.drain_to(script_pubkey); | ||
| tx_builder.drain_to(recipient_script(address, network)?); | ||
| } | ||
|
|
||
| if self.offline_signer { | ||
|
|
@@ -760,7 +785,7 @@ pub struct SignMessageCommand { | |
| impl AppCommand<AppContext<OfflineOperations<'_>>> for SignMessageCommand { | ||
| fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Value, Error> { | ||
| let wallet = &mut ctx.state.wallet; | ||
| let address: Address = parse_address(&self.address)?; | ||
| let address: Address = parse_address(&self.address, wallet.network())?; | ||
| let signature_format = parse_signature_format(&self.signature_type)?; | ||
|
|
||
| if !wallet.is_mine(address.script_pubkey()) { | ||
|
|
@@ -802,7 +827,7 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for VerifyMessageCommand { | |
| fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Value, Error> { | ||
| let wallet = &ctx.state.wallet; | ||
|
|
||
| let address: Address = parse_address(&self.address)?; | ||
| let address: Address = parse_address(&self.address, wallet.network())?; | ||
|
|
||
| let parsed_proof = MessageProof::from_base64(&self.proof) | ||
| .map_err(|e| Error::Generic(format!("Invalid proof format: {e}")))?; | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.