Skip to content

Commit 7a4d3e7

Browse files
committed
feat(wallet): Add Wallet::create_psbt
We use the new `PsbtParams` to add methods on `Wallet` for creating PSBTs, including RBF transactions. `Wallet::create_psbt` and `Wallet::replace_by_fee` each have no-std counterparts that take an additional `impl RngCore` parameter. Also adds a high level convenience method `Wallet::replace_by_fee_and_recipients` that exposes the minimum information needed to create an RBF. This commit re-introduces the `Wallet::insert_tx` API for adding transaction data to the wallet that may or may not be canonical from the point of view of the TxGraph. Added `Wallet::transactions_with_params` that allows customizing the internal canonicalization logic. Added new errors to `wallet::errors` - `CreatePsbtError` - `ReplaceByFeeError`
1 parent 2a40d6e commit 7a4d3e7

File tree

3 files changed

+717
-14
lines changed

3 files changed

+717
-14
lines changed

src/psbt/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use bitcoin::FeeRate;
1717
use bitcoin::Psbt;
1818
use bitcoin::TxOut;
1919

20-
#[allow(unused)]
2120
mod params;
2221

2322
pub use params::*;

src/wallet/error.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,74 @@ impl fmt::Display for BuildFeeBumpError {
256256

257257
#[cfg(feature = "std")]
258258
impl std::error::Error for BuildFeeBumpError {}
259+
260+
/// Error when creating a PSBT.
261+
#[derive(Debug)]
262+
#[non_exhaustive]
263+
pub enum CreatePsbtError {
264+
/// No Bnb solution.
265+
Bnb(bdk_coin_select::NoBnbSolution),
266+
/// Non-sufficient funds.
267+
InsufficientFunds(bdk_coin_select::InsufficientFunds),
268+
/// In order to use the [`add_global_xpubs`] option, every extended key in the descriptor must
269+
/// either be a master key itself, having a depth of 0, or have an explicit origin provided.
270+
///
271+
/// [`add_global_xpubs`]: crate::psbt::PsbtParams::add_global_xpubs
272+
MissingKeyOrigin(bitcoin::bip32::Xpub),
273+
/// Failed to create a spending plan for a manually selected output.
274+
Plan(OutPoint),
275+
/// Failed to create PSBT.
276+
Psbt(bdk_tx::CreatePsbtError),
277+
/// Selector error.
278+
Selector(bdk_tx::SelectorError),
279+
/// The UTXO of outpoint could not be found.
280+
UnknownUtxo(OutPoint),
281+
}
282+
283+
impl fmt::Display for CreatePsbtError {
284+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
285+
match self {
286+
Self::Bnb(e) => write!(f, "{e}"),
287+
Self::InsufficientFunds(e) => write!(f, "{e}"),
288+
Self::MissingKeyOrigin(e) => write!(f, "missing key origin: {e}"),
289+
Self::Plan(op) => write!(f, "failed to create a plan for txout with outpoint {op}"),
290+
Self::Psbt(e) => write!(f, "{e}"),
291+
Self::Selector(e) => write!(f, "{e}"),
292+
Self::UnknownUtxo(op) => write!(f, "unknown UTXO: {op}"),
293+
}
294+
}
295+
}
296+
297+
#[cfg(feature = "std")]
298+
impl std::error::Error for CreatePsbtError {}
299+
300+
/// Error when creating a Replace-By-Fee transaction.
301+
#[derive(Debug)]
302+
#[non_exhaustive]
303+
pub enum ReplaceByFeeError {
304+
/// There was a problem creating the PSBT
305+
CreatePsbt(CreatePsbtError),
306+
/// Failed to compute the fee of an original transaction
307+
PreviousFee(bdk_chain::tx_graph::CalculateFeeError),
308+
/// Original transaction could not be found
309+
MissingTransaction(Txid),
310+
}
311+
312+
impl fmt::Display for ReplaceByFeeError {
313+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
314+
match self {
315+
Self::CreatePsbt(e) => write!(f, "{e}"),
316+
Self::PreviousFee(e) => write!(f, "{e}"),
317+
Self::MissingTransaction(txid) => write!(f, "missing transaction: {txid}"),
318+
}
319+
}
320+
}
321+
322+
#[cfg(feature = "std")]
323+
impl std::error::Error for ReplaceByFeeError {}
324+
325+
impl From<CreatePsbtError> for ReplaceByFeeError {
326+
fn from(e: CreatePsbtError) -> Self {
327+
Self::CreatePsbt(e)
328+
}
329+
}

0 commit comments

Comments
 (0)