Skip to content

Commit 16d43cd

Browse files
committed
Handle LdkEvent::FundingTransactionReadyForSigning
When the interactive-tx construction protocol completes in LDK during splicing (and in the future dual-funding), LDK Node must provide signatures for any non-shared inputs belonging to its on-chain wallet. This commit implements this when handling the corresponding FundingTransactionReadyForSigning event.
1 parent c585275 commit 16d43cd

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/event.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,8 +1676,35 @@ where
16761676
}
16771677
}
16781678
},
1679-
LdkEvent::FundingTransactionReadyForSigning { .. } => {
1680-
debug_assert!(false, "We currently don't support interactive-tx, so this event should never be emitted.");
1679+
// TODO(splicing): Revisit error handling once splicing API is settled in LDK 0.3
1680+
LdkEvent::FundingTransactionReadyForSigning {
1681+
channel_id,
1682+
counterparty_node_id,
1683+
unsigned_transaction,
1684+
..
1685+
} => match self.wallet.sign_owned_inputs(unsigned_transaction) {
1686+
Ok(partially_signed_tx) => {
1687+
match self.channel_manager.funding_transaction_signed(
1688+
&channel_id,
1689+
&counterparty_node_id,
1690+
partially_signed_tx,
1691+
) {
1692+
Ok(()) => {
1693+
log_info!(
1694+
self.logger,
1695+
"Signed funding transaction for channel {} with counterparty {}",
1696+
channel_id,
1697+
counterparty_node_id
1698+
);
1699+
},
1700+
Err(e) => {
1701+
// TODO(splicing): Abort splice once supported in LDK 0.3
1702+
debug_assert!(false, "Failed signing funding transaction: {:?}", e);
1703+
log_error!(self.logger, "Failed signing funding transaction: {:?}", e);
1704+
},
1705+
}
1706+
},
1707+
Err(()) => log_error!(self.logger, "Failed signing funding transaction"),
16811708
},
16821709
LdkEvent::SplicePending {
16831710
channel_id,

src/wallet/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,43 @@ impl Wallet {
664664
Ok(address_info.address.script_pubkey())
665665
}
666666

667+
#[allow(deprecated)]
668+
pub(crate) fn sign_owned_inputs(&self, unsigned_tx: Transaction) -> Result<Transaction, ()> {
669+
let locked_wallet = self.inner.lock().unwrap();
670+
671+
let mut psbt = Psbt::from_unsigned_tx(unsigned_tx).map_err(|e| {
672+
log_error!(self.logger, "Failed to construct PSBT: {}", e);
673+
})?;
674+
for (i, txin) in psbt.unsigned_tx.input.iter().enumerate() {
675+
if let Some(utxo) = locked_wallet.get_utxo(txin.previous_output) {
676+
debug_assert!(!utxo.is_spent);
677+
psbt.inputs[i] = locked_wallet.get_psbt_input(utxo, None, true).map_err(|e| {
678+
log_error!(self.logger, "Failed to construct PSBT input: {}", e);
679+
})?;
680+
}
681+
}
682+
683+
let mut sign_options = SignOptions::default();
684+
sign_options.trust_witness_utxo = true;
685+
686+
match locked_wallet.sign(&mut psbt, sign_options) {
687+
Ok(finalized) => debug_assert!(!finalized),
688+
Err(e) => {
689+
log_error!(self.logger, "Failed to sign owned inputs: {}", e);
690+
return Err(());
691+
},
692+
}
693+
694+
match psbt.extract_tx() {
695+
Ok(tx) => Ok(tx),
696+
Err(bitcoin::psbt::ExtractTxError::MissingInputValue { tx }) => Ok(tx),
697+
Err(e) => {
698+
log_error!(self.logger, "Failed to extract transaction: {}", e);
699+
Err(())
700+
},
701+
}
702+
}
703+
667704
#[allow(deprecated)]
668705
fn sign_psbt_inner(&self, mut psbt: Psbt) -> Result<Transaction, ()> {
669706
let locked_wallet = self.inner.lock().unwrap();

0 commit comments

Comments
 (0)