Skip to content

Commit 2d25c87

Browse files
committed
fix: preserve output metadata in try_finalize_psbt
1 parent a407e32 commit 2d25c87

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

src/wallet/mod.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,14 +1922,18 @@ impl Wallet {
19221922
.unwrap_or_else(|| self.chain.tip().height());
19231923

19241924
Ok(self
1925-
.try_finalize_psbt_with(psbt, |_, input| {
1926-
Some((
1927-
current_height,
1928-
confirmation_heights
1929-
.get(&input.previous_output.txid)
1930-
.copied(),
1931-
))
1932-
})?
1925+
.try_finalize_psbt_with(
1926+
psbt,
1927+
|_, input| {
1928+
Some((
1929+
current_height,
1930+
confirmation_heights
1931+
.get(&input.previous_output.txid)
1932+
.copied(),
1933+
))
1934+
},
1935+
true,
1936+
)?
19331937
.is_finalized())
19341938
}
19351939

@@ -1942,13 +1946,14 @@ impl Wallet {
19421946
&self,
19431947
psbt: &mut Psbt,
19441948
) -> Result<FinalizePsbtOutcome, IndexOutOfBoundsError> {
1945-
self.try_finalize_psbt_with(psbt, |_, _| None)
1949+
self.try_finalize_psbt_with(psbt, |_, _| None, false)
19461950
}
19471951

19481952
fn try_finalize_psbt_with<F>(
19491953
&self,
19501954
psbt: &mut Psbt,
19511955
mut wallet_timelocks: F,
1956+
clear_output_derivations: bool,
19521957
) -> Result<FinalizePsbtOutcome, IndexOutOfBoundsError>
19531958
where
19541959
F: FnMut(usize, &bitcoin::TxIn) -> Option<(u32, Option<u32>)>,
@@ -2037,9 +2042,8 @@ impl Wallet {
20372042
}
20382043
}
20392044

2040-
// Clear derivation paths from outputs.
20412045
let finalized = FinalizePsbtOutcome::new(outcomes);
2042-
if finalized.is_finalized() {
2046+
if clear_output_derivations && finalized.is_finalized() {
20432047
for output in &mut psbt.outputs {
20442048
output.bip32_derivation.clear();
20452049
output.tap_key_origins.clear();

tests/wallet.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,10 +1628,15 @@ fn test_try_finalize_psbt_outcomes() {
16281628
)
16291629
.unwrap();
16301630
assert!(!is_final);
1631+
let output_bip32_derivations = psbt
1632+
.outputs
1633+
.iter()
1634+
.map(|output| output.bip32_derivation.clone())
1635+
.collect::<Vec<_>>();
16311636
assert!(
1632-
psbt.outputs
1637+
output_bip32_derivations
16331638
.iter()
1634-
.any(|output| !output.bip32_derivation.is_empty()),
1639+
.any(|derivation| !derivation.is_empty()),
16351640
"expected wallet-owned outputs to retain derivation data before finalization"
16361641
);
16371642

@@ -1646,10 +1651,13 @@ fn test_try_finalize_psbt_outcomes() {
16461651
psbt.inputs[0].final_script_sig.is_some()
16471652
|| psbt.inputs[0].final_script_witness.is_some()
16481653
);
1649-
assert!(psbt
1650-
.outputs
1651-
.iter()
1652-
.all(|output| output.bip32_derivation.is_empty()));
1654+
assert_eq!(
1655+
psbt.outputs
1656+
.iter()
1657+
.map(|output| output.bip32_derivation.clone())
1658+
.collect::<Vec<_>>(),
1659+
output_bip32_derivations
1660+
);
16531661

16541662
let finalized = wallet.try_finalize_psbt(&mut psbt).unwrap();
16551663

0 commit comments

Comments
 (0)