Skip to content
Open
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
10 changes: 7 additions & 3 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1507,10 +1507,14 @@ impl Psbt {
}

/// Returns the spending utxo for this PSBT's input at `input_index`.
pub fn spend_utxo(&self, input_index: u64) -> String {
pub fn spend_utxo(&self, input_index: u64) -> Result<String, PsbtError> {
let psbt = self.0.lock().unwrap();
let utxo = psbt.spend_utxo(input_index as usize).unwrap();
serde_json::to_string(&utxo).unwrap()
let utxo = psbt
.spend_utxo(input_index as usize)
.map_err(PsbtError::from)?;
serde_json::to_string(&utxo).map_err(|error| PsbtError::Serialization {
error_message: error.to_string(),
})
}

/// The corresponding key-value map for each input in the unsigned transaction.
Expand Down
25 changes: 25 additions & 0 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use bdk_wallet::bitcoin::hex::DisplayHex;
use bdk_wallet::bitcoin::psbt::Error as BdkPsbtError;
use bdk_wallet::bitcoin::psbt::ExtractTxError as BdkExtractTxError;
use bdk_wallet::bitcoin::psbt::PsbtParseError as BdkPsbtParseError;
use bdk_wallet::bitcoin::psbt::SignError as BdkPsbtSignError;
use bdk_wallet::bitcoin::script::PushBytesError;
use bdk_wallet::chain::local_chain::CannotConnectError as BdkCannotConnectError;
use bdk_wallet::chain::rusqlite::Error as BdkSqliteError;
Expand Down Expand Up @@ -618,6 +619,18 @@ pub enum PsbtError {
#[error("output index is out of bounds of non witness script output array")]
PsbtUtxoOutOfBounds,

#[error("input index is out of bounds")]
InputIndexOutOfBounds,

#[error("missing spend UTXO in PSBT")]
MissingSpendUtxo,

#[error("PSBT serialization error: {error_message}")]
Serialization { error_message: String },

#[error("PSBT spend UTXO error: {error_message}")]
SpendUtxo { error_message: String },

#[error("invalid key: {key}")]
InvalidKey { key: String },

Expand Down Expand Up @@ -1542,6 +1555,18 @@ impl From<BdkPsbtError> for PsbtError {
}
}

impl From<BdkPsbtSignError> for PsbtError {
fn from(error: BdkPsbtSignError) -> Self {
match error {
BdkPsbtSignError::IndexOutOfBounds(_) => PsbtError::InputIndexOutOfBounds,
BdkPsbtSignError::MissingSpendUtxo => PsbtError::MissingSpendUtxo,
_ => PsbtError::SpendUtxo {
error_message: error.to_string(),
},
}
}
}

impl From<BdkPsbtParseError> for PsbtParseError {
fn from(error: BdkPsbtParseError) -> Self {
match error {
Expand Down
11 changes: 10 additions & 1 deletion bdk-ffi/src/tests/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::bitcoin::{Address, AddressData, Key, Network, ProprietaryKey, Psbt};
use crate::error::PsbtError;
use bdk_electrum::bdk_core::bitcoin::hex::DisplayHex;

#[test]
Expand Down Expand Up @@ -360,7 +361,7 @@ fn test_to_address_data() {
#[test]
fn test_psbt_spend_utxo() {
let psbt = sample_psbt();
let psbt_utxo = psbt.spend_utxo(0);
let psbt_utxo = psbt.spend_utxo(0).unwrap();

assert_eq!(
psbt_utxo,
Expand All @@ -369,6 +370,14 @@ fn test_psbt_spend_utxo() {
);
}

#[test]
fn test_psbt_spend_utxo_invalid_index_returns_error() {
let psbt = sample_psbt();
let error = psbt.spend_utxo(1).unwrap_err();

assert!(matches!(error, PsbtError::InputIndexOutOfBounds));
}

#[test]
fn test_psbt_input_length() {
let psbt = sample_psbt2();
Expand Down
17 changes: 17 additions & 0 deletions bdk-ffi/src/tests/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,23 @@ fn test_error_psbt() {
PsbtError::PsbtUtxoOutOfBounds,
"output index is out of bounds of non witness script output array",
),
(
PsbtError::InputIndexOutOfBounds,
"input index is out of bounds",
),
(PsbtError::MissingSpendUtxo, "missing spend UTXO in PSBT"),
(
PsbtError::Serialization {
error_message: "serialization error".to_string(),
},
"PSBT serialization error: serialization error",
),
(
PsbtError::SpendUtxo {
error_message: "spend error".to_string(),
},
"PSBT spend UTXO error: spend error",
),
(
PsbtError::InvalidKey {
key: "key".to_string(),
Expand Down
Loading