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
65 changes: 44 additions & 21 deletions psbt/src/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
use std::str::FromStr;

use derive::{
Address, AddressParseError, Keychain, LockTime, Network, NormalIndex, Outpoint, Sats,
ScriptPubkey, SeqNo, Terminal, Vout,
Address, AddressNetwork, AddressParseError, Keychain, LockTime, Network, NormalIndex, Outpoint,
Sats, ScriptPubkey, SeqNo, Terminal, Vout,
};
use descriptors::Descriptor;

Expand Down Expand Up @@ -175,10 +175,19 @@
}
}

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct ChangeInfo {
pub vout: Vout,
pub terminal: Terminal,
}

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct PsbtMeta {
pub change_vout: Option<Vout>,
pub change_terminal: Option<Terminal>,
pub network: AddressNetwork,
pub fee: Sats,
pub weight: u32,
pub size: u32,
pub change: Option<ChangeInfo>,
}

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
Expand Down Expand Up @@ -276,22 +285,36 @@
}

// 3. Add change - only if exceeded the dust limit
let (change_vout, change_terminal) =
if remaining_value > self.descriptor().class().dust_limit() {
let change_index =
self.next_derivation_index(params.change_keychain, params.change_shift);
let change_terminal = Terminal::new(params.change_keychain, change_index);
let change_vout = psbt
.append_change_expect(self.descriptor(), change_terminal, remaining_value)
.index();
(Some(Vout::from_u32(change_vout as u32)), Some(change_terminal))
} else {
(None, None)
};

Ok((psbt, PsbtMeta {
change_vout,
change_terminal,
}))
let change = if remaining_value > self.descriptor().class().dust_limit() {
let change_index =
self.next_derivation_index(params.change_keychain, params.change_shift);
let change_terminal = Terminal::new(params.change_keychain, change_index);
let change_vout = psbt
.append_change_expect(self.descriptor(), change_terminal, remaining_value)
.index();
Some(ChangeInfo {
vout: Vout::from_u32(change_vout as u32),
terminal: change_terminal,
})

Check warning on line 298 in psbt/src/constructor.rs

View check run for this annotation

Codecov / codecov/patch

psbt/src/constructor.rs#L288-L298

Added lines #L288 - L298 were not covered by tests
} else {
None

Check warning on line 300 in psbt/src/constructor.rs

View check run for this annotation

Codecov / codecov/patch

psbt/src/constructor.rs#L300

Added line #L300 was not covered by tests
};

let meta = PsbtMeta {
network: self.network().into(),
fee: params.fee,
weight: 0, // TODO: Implement weight/size computation
size: 0,
change,
};
self.after_construct_psbt(&psbt, &meta);

Check warning on line 310 in psbt/src/constructor.rs

View check run for this annotation

Codecov / codecov/patch

psbt/src/constructor.rs#L303-L310

Added lines #L303 - L310 were not covered by tests

Ok((psbt, meta))
}

Check warning on line 313 in psbt/src/constructor.rs

View check run for this annotation

Codecov / codecov/patch

psbt/src/constructor.rs#L312-L313

Added lines #L312 - L313 were not covered by tests

/// A hook which is called by the default `Self::construct_psbt` before returning the newly
/// constructed PSBT to the caller.
fn after_construct_psbt(&mut self, _psbt: &Psbt, _meta: &PsbtMeta) {

Check warning on line 317 in psbt/src/constructor.rs

View check run for this annotation

Codecov / codecov/patch

psbt/src/constructor.rs#L317

Added line #L317 was not covered by tests
// By default, we do not use the hook
}
}
4 changes: 2 additions & 2 deletions psbt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ mod sign;

pub use coders::{Decode, DecodeError, Encode, PsbtError};
pub use constructor::{
Beneficiary, BeneficiaryParseError, ConstructionError, Payment, PsbtConstructor, PsbtMeta,
TxParams, Utxo,
Beneficiary, BeneficiaryParseError, ChangeInfo, ConstructionError, Payment, PsbtConstructor,
PsbtMeta, TxParams, Utxo,
};
#[cfg(feature = "client-side-validation")]
pub use csval::*;
Expand Down