Skip to content
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
32 changes: 25 additions & 7 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,30 @@ impl TryFrom<Input> for BdkInput {
})?;

let output_key_parity = k.output_key_parity;
if !matches!(output_key_parity, 0 | 1) {
return Err(AddForeignUtxoError::InputConversionError {
error_message: format!(
"invalid output key parity: {}",
output_key_parity
),
});
}

let leaf_version_u8 = k.leaf_version;
if v.leaf_version != leaf_version_u8 {
return Err(AddForeignUtxoError::InputConversionError {
error_message: format!(
"tap script leaf version {} does not match control block leaf version {}",
v.leaf_version, leaf_version_u8
),
});
}

let leaf_version = LeafVersion::from_consensus(leaf_version_u8).map_err(|_| {
AddForeignUtxoError::InputConversionError {
error_message: format!("invalid leaf version: {}", leaf_version_u8),
}
})?;

let merkle_branch: Vec<TapNodeHash> = k
.merkle_branch
Expand All @@ -1043,7 +1066,8 @@ impl TryFrom<Input> for BdkInput {
})
.collect::<Result<_, AddForeignUtxoError>>()?;

let mut control_block_bytes = vec![output_key_parity | leaf_version_u8];
let mut control_block_bytes =
vec![output_key_parity | leaf_version.to_consensus()];
control_block_bytes.extend_from_slice(&internal_key.serialize());
for hash in &merkle_branch {
control_block_bytes.extend_from_slice(&hash.to_byte_array());
Expand All @@ -1055,12 +1079,6 @@ impl TryFrom<Input> for BdkInput {
}
})?;

let leaf_version = LeafVersion::from_consensus(leaf_version_u8).map_err(|_| {
AddForeignUtxoError::InputConversionError {
error_message: format!("invalid leaf version: {}", leaf_version_u8),
}
})?;

Ok((control_block, (v.script.0.clone(), leaf_version)))
})
.collect::<Result<_, AddForeignUtxoError>>()?;
Expand Down
107 changes: 105 additions & 2 deletions bdk-ffi/src/tests/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use crate::bitcoin::{
Address, AddressData, FeeRate, Key, Network, ProprietaryKey, Psbt, Transaction,
Address, AddressData, ControlBlock, FeeRate, Input, Key, Network, ProprietaryKey, Psbt, Script,
TapScriptEntry, Transaction,
};
use crate::error::TransactionError;
use crate::error::{AddForeignUtxoError, TransactionError};
use bdk_electrum::bdk_core::bitcoin::hex::DisplayHex;
use bdk_wallet::bitcoin::psbt::Input as BdkInput;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::sync::Arc;

#[test]
fn maximum_fee_rate_converts_and_displays_without_overflow() {
Expand Down Expand Up @@ -393,6 +398,104 @@ fn test_transaction_new_rejects_trailing_bytes() {
));
}

#[test]
fn test_input_conversion_rejects_invalid_taproot_output_key_parity() {
let control_block = ControlBlock {
internal_key: vec![
0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87,
0x0b, 0x07, 0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2, 0x81, 0x5b,
0x16, 0xf8, 0x17, 0x98,
],
merkle_branch: Vec::new(),
output_key_parity: 2,
leaf_version: 0xc0,
};
let tap_script = TapScriptEntry {
script: Arc::new(Script::new(vec![0x51])),
leaf_version: 0xc0,
};
let input = Input {
non_witness_utxo: None,
witness_utxo: None,
partial_sigs: HashMap::new(),
sighash_type: None,
redeem_script: None,
witness_script: None,
bip32_derivation: HashMap::new(),
final_script_sig: None,
final_script_witness: None,
ripemd160_preimages: HashMap::new(),
sha256_preimages: HashMap::new(),
hash160_preimages: HashMap::new(),
hash256_preimages: HashMap::new(),
tap_key_sig: None,
tap_script_sigs: HashMap::new(),
tap_scripts: HashMap::from([(control_block, tap_script)]),
tap_key_origins: HashMap::new(),
tap_internal_key: None,
tap_merkle_root: None,
proprietary: HashMap::new(),
unknown: HashMap::new(),
};

let result = BdkInput::try_from(input);

assert!(matches!(
result,
Err(AddForeignUtxoError::InputConversionError { error_message })
if error_message.contains("invalid output key parity")
));
}

#[test]
fn test_input_conversion_rejects_mismatched_taproot_leaf_versions() {
let control_block = ControlBlock {
internal_key: vec![
0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87,
0x0b, 0x07, 0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2, 0x81, 0x5b,
0x16, 0xf8, 0x17, 0x98,
],
merkle_branch: Vec::new(),
output_key_parity: 0,
leaf_version: 0xc0,
};
let tap_script = TapScriptEntry {
script: Arc::new(Script::new(vec![0x51])),
leaf_version: 0xc2,
};
let input = Input {
non_witness_utxo: None,
witness_utxo: None,
partial_sigs: HashMap::new(),
sighash_type: None,
redeem_script: None,
witness_script: None,
bip32_derivation: HashMap::new(),
final_script_sig: None,
final_script_witness: None,
ripemd160_preimages: HashMap::new(),
sha256_preimages: HashMap::new(),
hash160_preimages: HashMap::new(),
hash256_preimages: HashMap::new(),
tap_key_sig: None,
tap_script_sigs: HashMap::new(),
tap_scripts: HashMap::from([(control_block, tap_script)]),
tap_key_origins: HashMap::new(),
tap_internal_key: None,
tap_merkle_root: None,
proprietary: HashMap::new(),
unknown: HashMap::new(),
};

let result = BdkInput::try_from(input);

assert!(matches!(
result,
Err(AddForeignUtxoError::InputConversionError { error_message })
if error_message.contains("does not match control block leaf version")
));
}

#[test]
fn test_psbt_spend_utxo() {
let psbt = sample_psbt();
Expand Down
Loading