Skip to content

Commit

Permalink
wip: demo TxBuilder::build_psbt
Browse files Browse the repository at this point in the history
  • Loading branch information
ValuedMammal committed Mar 2, 2025
1 parent a4978af commit 127d3f0
Show file tree
Hide file tree
Showing 7 changed files with 848 additions and 102 deletions.
2 changes: 2 additions & 0 deletions crates/wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ bitcoin = { version = "0.32.0", features = [ "serde", "base64" ], default-featur
serde = { version = "^1.0", features = ["derive"] }
serde_json = { version = "^1.0" }
bdk_chain = { path = "../chain", version = "0.21.1", features = [ "miniscript", "serde" ], default-features = false }
bdk_coin_select = "0.4"
bdk_tx = { git = "https://github.com/bitcoindevkit/bdk-tx", branch = "feat/add-psbt-input" }
bdk_file_store = { path = "../file_store", version = "0.18.1", optional = true }

# Optional dependencies
Expand Down
14 changes: 13 additions & 1 deletion crates/wallet/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
// licenses.

use alloc::boxed::Box;
use chain::{ChainPosition, ConfirmationBlockTime};
use core::convert::AsRef;

use bdk_coin_select::Candidate;
use bitcoin::transaction::{OutPoint, Sequence, TxOut};
use bitcoin::{psbt, Weight};
use chain::{ChainPosition, ConfirmationBlockTime};

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -77,6 +78,17 @@ pub struct WeightedUtxo {
pub utxo: Utxo,
}

impl From<WeightedUtxo> for Candidate {
fn from(u: WeightedUtxo) -> Self {
Self {
input_count: 1,
weight: u.satisfaction_weight.to_wu(),
value: u.utxo.txout().value.to_sat(),
is_segwit: u.utxo.txout().script_pubkey.witness_version().is_some(),
}
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// An unspent transaction output (UTXO).
pub enum Utxo {
Expand Down
26 changes: 26 additions & 0 deletions crates/wallet/src/wallet/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{descriptor, KeychainKind};
use alloc::string::String;
use bitcoin::{absolute, psbt, Amount, OutPoint, Sequence, Txid};
use core::fmt;
use miniscript::{descriptor::DefiniteDescriptorKey, Descriptor};

/// Errors returned by miniscript when updating inconsistent PSBTs
#[derive(Debug, Clone)]
Expand All @@ -43,6 +44,19 @@ impl fmt::Display for MiniscriptPsbtError {
#[cfg(feature = "std")]
impl std::error::Error for MiniscriptPsbtError {}

/// Error when trying to create a spending [`Plan`](miniscript::plan::Plan).
#[derive(Debug)]
pub struct PlanError(pub(crate) Descriptor<DefiniteDescriptorKey>);

impl fmt::Display for PlanError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "failed to create plan for descriptor {}", self.0)
}
}

#[cfg(feature = "std")]
impl std::error::Error for PlanError {}

#[derive(Debug)]
/// Error returned from [`TxBuilder::finish`]
///
Expand Down Expand Up @@ -88,6 +102,8 @@ pub enum CreateTxError {
OutputBelowDustLimit(usize),
/// There was an error with coin selection
CoinSelection(coin_selection::InsufficientFunds),
/// bdk_coin_select insufficient funds
Selection(bdk_coin_select::InsufficientFunds),
/// Cannot build a tx without recipients
NoRecipients,
/// Partially signed bitcoin transaction error
Expand All @@ -104,6 +120,12 @@ pub enum CreateTxError {
MissingNonWitnessUtxo(OutPoint),
/// Miniscript PSBT error
MiniscriptPsbt(MiniscriptPsbtError),
/// Error when building a transaction
BuildPsbt(bdk_tx::Error),
/// Error updating a PSBT
UpdatePsbt(bdk_tx::UpdatePsbtError),
/// Error creating a spending plan
Plan(PlanError),
}

impl fmt::Display for CreateTxError {
Expand Down Expand Up @@ -155,6 +177,7 @@ impl fmt::Display for CreateTxError {
write!(f, "Output below the dust limit: {}", limit)
}
CreateTxError::CoinSelection(e) => e.fmt(f),
CreateTxError::Selection(e) => e.fmt(f),
CreateTxError::NoRecipients => {
write!(f, "Cannot build tx without recipients")
}
Expand All @@ -171,6 +194,9 @@ impl fmt::Display for CreateTxError {
CreateTxError::MiniscriptPsbt(err) => {
write!(f, "Miniscript PSBT error: {}", err)
}
CreateTxError::BuildPsbt(e) => e.fmt(f),
CreateTxError::UpdatePsbt(e) => e.fmt(f),
CreateTxError::Plan(e) => e.fmt(f),
}
}
}
Expand Down
Loading

0 comments on commit 127d3f0

Please sign in to comment.