Skip to content

Commit 71ca931

Browse files
committed
docs: Improve docs for create_psbt and similar
Rename `*_with_aux_rand` methods to `*_with_rng` instead.
1 parent 703f3db commit 71ca931

File tree

3 files changed

+89
-28
lines changed

3 files changed

+89
-28
lines changed

src/psbt/params.rs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Parameters for PSBT building.
1+
//! Parameters for creating a PSBT.
22
33
use alloc::sync::Arc;
44
use alloc::vec::Vec;
@@ -78,8 +78,17 @@ impl PsbtParams {
7878
/// Add UTXOs by outpoint to fund the transaction.
7979
///
8080
/// A single outpoint may appear at most once in the list of UTXOs to spend. The caller is
81-
/// responsible for ensuring that elements of `outpoints` correspond to outputs of previous
81+
/// responsible for ensuring that items of `outpoints` correspond to outputs of previous
8282
/// transactions and are currently unspent.
83+
///
84+
/// If an outpoint doesn't correspond to an indexed script pubkey, a [`UnknownUtxo`]
85+
/// will occur. See [`Wallet::create_psbt`] for more.
86+
///
87+
/// To add a UTXO that did not originate from this wallet (i.e. a "foreign" UTXO), see
88+
/// [`PsbtParams::add_planned_input`].
89+
///
90+
/// [`UnknownUtxo`]: crate::wallet::error::CreatePsbtError::UnknownUtxo
91+
/// [`Wallet::create_psbt`]: crate::Wallet::create_psbt
8392
pub fn add_utxos(&mut self, outpoints: &[OutPoint]) -> &mut Self {
8493
self.utxos
8594
.extend(outpoints.iter().copied().filter(|&op| self.set.insert(op)));
@@ -134,10 +143,10 @@ impl PsbtParams {
134143
self
135144
}
136145

137-
/// Add recipients.
146+
/// Add outgoing recipients to the transaction.
138147
///
139-
/// - `recipients`: An iterator of `(S, Amount)` tuples where `S` can be a bitcoin [`Address`],
140-
/// a scriptPubKey, or anything that can be converted straight into a [`ScriptBuf`].
148+
/// - `recipients`: An iterator of `(S, Amount)` tuples where `S` can be a [`bitcoin::Address`],
149+
/// a script pubkey, or anything that can be converted straight into a [`ScriptBuf`].
141150
pub fn add_recipients<I, S>(&mut self, recipients: I) -> &mut Self
142151
where
143152
I: IntoIterator<Item = (S, Amount)>,
@@ -201,7 +210,7 @@ impl PsbtParams {
201210
///
202211
/// This merges all of the spends into a single transaction while retaining the parameters
203212
/// of `self`. Note however that any previously added UTXOs are removed. Call
204-
/// [`replace_by_fee_with_aux_rand`](crate::Wallet::replace_by_fee_with_aux_rand) to finish
213+
/// [`replace_by_fee_with_rng`](crate::Wallet::replace_by_fee_with_rng) to finish
205214
/// building the PSBT.
206215
///
207216
/// ## Note
@@ -247,6 +256,35 @@ impl PsbtParams {
247256
/// Add a planned input.
248257
///
249258
/// This can be used to add inputs that come with a [`Plan`] or [`psbt::Input`] provided.
259+
/// See [`Input`] for more on how to create inputs manually. Be aware that creating inputs
260+
/// in this manner relies on certain assumptions, like the UTXO validity, the satisfaction
261+
/// weight, and so on. As such you should only use this method to add inputs you definitely
262+
/// trust the values for.
263+
///
264+
/// # Example
265+
///
266+
/// ```rust,no_run
267+
/// use bdk_tx::Input;
268+
/// # use bdk_wallet::psbt::PsbtParams;
269+
/// # use bitcoin::{psbt, OutPoint, Sequence, TxOut};
270+
/// # let outpoint = OutPoint::null();
271+
/// # let sequence = Sequence::ENABLE_LOCKTIME_NO_RBF;
272+
/// # let psbt_input = psbt::Input::default();
273+
/// # let satisfaction_weight = 0;
274+
/// # let tx_status = None;
275+
/// # let is_coinbase = false;
276+
/// let mut params = PsbtParams::default();
277+
/// let input = Input::from_psbt_input(
278+
/// outpoint,
279+
/// sequence,
280+
/// psbt_input,
281+
/// satisfaction_weight,
282+
/// tx_status,
283+
/// is_coinbase,
284+
/// )?;
285+
/// params.add_planned_input(input);
286+
/// # Ok::<_, anyhow::Error>(())
287+
/// ```
250288
///
251289
/// [`Plan`]: miniscript::plan::Plan
252290
/// [`psbt::Input`]: bitcoin::psbt::Input
@@ -346,8 +384,8 @@ impl ReplaceParams {
346384
params
347385
}
348386

349-
/// Replace spends of the provided `txs`. This will internally set the internal list
350-
/// of UTXOs to be spent.
387+
/// Replace spends of the provided `txs`. This will internally set the list of UTXOs
388+
/// to be spent.
351389
pub fn replace(&mut self, txs: &[Arc<Transaction>]) {
352390
self.inner.utxos.clear();
353391
let mut utxos = vec![];

src/wallet/error.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,20 +263,20 @@ impl std::error::Error for BuildFeeBumpError {}
263263
pub enum CreatePsbtError {
264264
/// No Bnb solution.
265265
Bnb(bdk_coin_select::NoBnbSolution),
266-
/// Non-sufficient funds
266+
/// Non-sufficient funds.
267267
InsufficientFunds(bdk_coin_select::InsufficientFunds),
268268
/// In order to use the [`add_global_xpubs`] option, every extended key in the descriptor must
269269
/// either be a master key itself, having a depth of 0, or have an explicit origin provided.
270270
///
271271
/// [`add_global_xpubs`]: crate::psbt::PsbtParams::add_global_xpubs
272272
MissingKeyOrigin(bitcoin::bip32::Xpub),
273-
/// Failed to create a spend [`Plan`] for a manually selected output
273+
/// Failed to create a spending plan for a manually selected output.
274274
Plan(OutPoint),
275-
/// Failed to create PSBT
275+
/// Failed to create PSBT.
276276
Psbt(bdk_tx::CreatePsbtError),
277-
/// Selector error
277+
/// Selector error.
278278
Selector(bdk_tx::SelectorError),
279-
/// The UTXO of outpoint could not be found
279+
/// The UTXO of outpoint could not be found.
280280
UnknownUtxo(OutPoint),
281281
}
282282

src/wallet/mod.rs

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,6 +2837,9 @@ impl Wallet {
28372837
/// Creates a PSBT with the given `params` and returns the updated [`Psbt`] and
28382838
/// [`Finalizer`].
28392839
///
2840+
/// This function uses the thread-local random number generator (RNG) to generate
2841+
/// randomness. To supply your own source of entropy see [`Wallet::create_psbt_with_rng`].
2842+
///
28402843
/// # Example
28412844
///
28422845
/// ```rust,no_run
@@ -2857,21 +2860,31 @@ impl Wallet {
28572860
/// let (psbt, finalizer) = wallet.create_psbt(params)?;
28582861
/// # Ok::<_, anyhow::Error>(())
28592862
/// ```
2863+
///
2864+
/// # Errors
2865+
///
2866+
/// A [`CreatePsbtError`] will be thrown if any of the following occurs
2867+
///
2868+
/// - A manually selected input is missing from the wallet, or could not be planned
2869+
/// - The input value is insufficient to fund the outputs
2870+
/// - Failure to complete coin selection
2871+
/// - Failure to create or update the PSBT.
28602872
#[cfg(feature = "std")]
28612873
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
28622874
pub fn create_psbt(&self, params: PsbtParams) -> Result<(Psbt, Finalizer), CreatePsbtError> {
2863-
self.create_psbt_with_aux_rand(params, &mut rand::thread_rng())
2875+
self.create_psbt_with_rng(params, &mut rand::thread_rng())
28642876
}
28652877

2866-
/// Creates a PSBT with the given `params` and auxiliary randomness.
2878+
/// Creates a PSBT with the given `params` and random number generator (RNG).
28672879
///
2868-
/// ### Parameters:
2880+
/// Return the updated [`Psbt`] and [`Finalizer`].
28692881
///
2870-
/// - `params`: [`PsbtParams`]
2871-
/// - `rng`: Source of entropy, may be used during coin selection.
2882+
/// ## Parameters:
28722883
///
2873-
/// Returns the updated [`Psbt`] and [`Finalizer`].
2874-
pub fn create_psbt_with_aux_rand(
2884+
/// - `params`: [`PsbtParams`]
2885+
/// - `rng`: Source of entropy, may be used during coin selection and to sort inputs and outputs
2886+
/// by the [`TxOrdering`](crate::wallet::tx_builder::TxOrdering).
2887+
pub fn create_psbt_with_rng(
28752888
&self,
28762889
params: PsbtParams,
28772890
rng: &mut impl RngCore,
@@ -3020,7 +3033,7 @@ impl Wallet {
30203033
/// [`Finalizer`].
30213034
///
30223035
/// This is a convenience for getting a new [`ReplaceParams`], and updating the recipients
3023-
/// and feerate before calling [`replace_by_fee_with_aux_rand`]. If further configuration is
3036+
/// and feerate before calling [`Wallet::replace_by_fee_with_rng`]. If further configuration is
30243037
/// desired, consider using [`PsbtParams::replace`] instead.
30253038
///
30263039
/// # Example
@@ -3053,7 +3066,7 @@ impl Wallet {
30533066
feerate: FeeRate,
30543067
recipients: Vec<(ScriptBuf, Amount)>,
30553068
) -> Result<(Psbt, Finalizer), ReplaceByFeeError> {
3056-
self.replace_by_fee_with_aux_rand(
3069+
self.replace_by_fee_with_rng(
30573070
ReplaceParams::new(
30583071
txs,
30593072
PsbtParams {
@@ -3069,25 +3082,35 @@ impl Wallet {
30693082
/// Creates a Replace-By-Fee transaction (RBF) and returns the updated [`Psbt`] and
30703083
/// [`Finalizer`].
30713084
///
3072-
/// ### Parameters:
3085+
/// This function uses the thread-local random number generator (RNG) to generate
3086+
/// randomness. To supply your own source of entropy see [`Wallet::replace_by_fee_with_rng`].
30733087
///
3074-
/// - `params`: [`ReplaceParams`]
3088+
/// # Errors
3089+
///
3090+
/// A [`ReplaceByFeeError`] will be thrown if any of the following occurs
3091+
///
3092+
/// - An original transaction is missing from the wallet
3093+
/// - Failure to calculate the [fee](Wallet::calculate_fee) of an original transaction
3094+
/// - Failure to complete coin selection
3095+
/// - Failure to create or update the PSBT.
30753096
#[cfg(feature = "std")]
3097+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
30763098
pub fn replace_by_fee(
30773099
&self,
30783100
params: ReplaceParams,
30793101
) -> Result<(Psbt, Finalizer), ReplaceByFeeError> {
3080-
self.replace_by_fee_with_aux_rand(params, &mut rand::thread_rng())
3102+
self.replace_by_fee_with_rng(params, &mut rand::thread_rng())
30813103
}
30823104

30833105
/// Creates a Replace-By-Fee transaction (RBF) and returns the updated [`Psbt`] and
30843106
/// [`Finalizer`].
30853107
///
3086-
/// ### Parameters:
3108+
/// ## Parameters:
30873109
///
30883110
/// - `params`: [`ReplaceParams`]
3089-
/// - `rng`: Source of entropy, may be used during coin selection.
3090-
pub fn replace_by_fee_with_aux_rand(
3111+
/// - `rng`: Source of entropy, may be used during coin selection and to sort inputs and outputs
3112+
/// by the [`TxOrdering`](crate::wallet::tx_builder::TxOrdering).
3113+
pub fn replace_by_fee_with_rng(
30913114
&self,
30923115
params: ReplaceParams,
30933116
rng: &mut impl RngCore,

0 commit comments

Comments
 (0)