Skip to content

Commit 1170e1b

Browse files
committed
Support splice shared input signing
This commit tracks all data related to the shared input of a splice, such that a valid witness can be formed upon the splice transaction finalization.
1 parent 712b385 commit 1170e1b

File tree

3 files changed

+218
-78
lines changed

3 files changed

+218
-78
lines changed

lightning/src/ln/channel.rs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,6 +2192,11 @@ impl FundingScope {
21922192
self.channel_transaction_parameters.make_funding_redeemscript()
21932193
}
21942194

2195+
#[cfg(splicing)]
2196+
fn holder_funding_pubkey(&self) -> &PublicKey {
2197+
&self.get_holder_pubkeys().funding_pubkey
2198+
}
2199+
21952200
fn counterparty_funding_pubkey(&self) -> &PublicKey {
21962201
&self.get_counterparty_pubkeys().funding_pubkey
21972202
}
@@ -2335,8 +2340,16 @@ impl FundingScope {
23352340
};
23362341

23372342
let local_owned = self.value_to_self_msat / 1000;
2338-
2339-
SharedOwnedInput::new(input, prev_output, local_owned)
2343+
let holder_sig_first = self.holder_funding_pubkey().serialize()[..]
2344+
< self.counterparty_funding_pubkey().serialize()[..];
2345+
2346+
SharedOwnedInput::new(
2347+
input,
2348+
prev_output,
2349+
local_owned,
2350+
holder_sig_first,
2351+
self.get_funding_redeemscript(),
2352+
)
23402353
}
23412354
}
23422355

@@ -7969,9 +7982,9 @@ where
79697982
}
79707983

79717984
pub fn funding_transaction_signed(
7972-
&mut self, witnesses: Vec<Witness>,
7985+
&mut self, funding_txid_signed: Txid, witnesses: Vec<Witness>,
79737986
) -> Result<(Option<msgs::TxSignatures>, Option<Transaction>), APIError> {
7974-
let (funding_tx_opt, tx_signatures_opt) = self
7987+
let (tx_signatures_opt, funding_tx_opt) = self
79757988
.interactive_tx_signing_session
79767989
.as_mut()
79777990
.ok_or_else(|| APIError::APIMisuseError {
@@ -7981,12 +7994,21 @@ where
79817994
),
79827995
})
79837996
.and_then(|signing_session| {
7997+
let tx = signing_session.unsigned_tx().build_unsigned_tx();
7998+
if funding_txid_signed != tx.compute_txid() {
7999+
return Err(APIError::APIMisuseError {
8000+
err: "Transaction was malleated prior to signing".to_owned(),
8001+
});
8002+
}
8003+
8004+
let tx_signatures = msgs::TxSignatures {
8005+
channel_id: self.context.channel_id,
8006+
tx_hash: funding_txid_signed,
8007+
witnesses,
8008+
shared_input_signature: None,
8009+
};
79848010
signing_session
7985-
.provide_holder_witnesses(
7986-
&self.context.secp_ctx,
7987-
self.context.channel_id,
7988-
witnesses,
7989-
)
8011+
.provide_holder_witnesses(tx_signatures, &self.context.secp_ctx)
79908012
.map_err(|err| APIError::APIMisuseError { err })
79918013
})?;
79928014

@@ -8004,7 +8026,7 @@ where
80048026
}
80058027

80068028
#[rustfmt::skip]
8007-
pub fn tx_signatures(&mut self, msg: &msgs::TxSignatures) -> Result<(Option<Transaction>, Option<msgs::TxSignatures>), ChannelError> {
8029+
pub fn tx_signatures(&mut self, msg: &msgs::TxSignatures) -> Result<(Option<msgs::TxSignatures>, Option<Transaction>), ChannelError> {
80088030
if !self.context.channel_state.is_interactive_signing()
80098031
|| self.context.channel_state.is_their_tx_signatures_sent()
80108032
{
@@ -8035,7 +8057,7 @@ where
80358057
}
80368058
}
80378059

8038-
let (holder_tx_signatures_opt, funding_tx_opt) = signing_session.received_tx_signatures(msg.clone())
8060+
let (holder_tx_signatures_opt, funding_tx_opt) = signing_session.received_tx_signatures(msg)
80398061
.map_err(|msg| ChannelError::Warn(msg))?;
80408062

80418063
// Set `THEIR_TX_SIGNATURES_SENT` flag after all potential errors.
@@ -8050,7 +8072,7 @@ where
80508072
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
80518073
}
80528074

8053-
Ok((funding_tx_opt, holder_tx_signatures_opt))
8075+
Ok((holder_tx_signatures_opt, funding_tx_opt))
80548076
} else {
80558077
let msg = "Unexpected tx_signatures. No funding transaction awaiting signatures";
80568078
let reason = ClosureReason::ProcessingError { err: msg.to_owned() };

lightning/src/ln/channelmanager.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5969,13 +5969,14 @@ where
59695969
match peer_state.channel_by_id.get_mut(channel_id) {
59705970
Some(channel) => match channel.as_funded_mut() {
59715971
Some(chan) => {
5972+
let txid = transaction.compute_txid();
59725973
let witnesses: Vec<_> = transaction
59735974
.input
59745975
.into_iter()
59755976
.map(|input| input.witness)
59765977
.filter(|witness| !witness.is_empty())
59775978
.collect();
5978-
match chan.funding_transaction_signed(witnesses) {
5979+
match chan.funding_transaction_signed(txid, witnesses) {
59795980
Ok((Some(tx_signatures), funding_tx_opt)) => {
59805981
if let Some(funding_tx) = funding_tx_opt {
59815982
self.broadcast_interactive_funding(chan, &funding_tx);
@@ -9030,7 +9031,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
90309031
}
90319032
} else {
90329033
let txid = signing_session.unsigned_tx().compute_txid();
9033-
match channel.funding_transaction_signed(vec![]) {
9034+
match channel.funding_transaction_signed(txid, vec![]) {
90349035
Ok((Some(tx_signatures), funding_tx_opt)) => {
90359036
if let Some(funding_tx) = funding_tx_opt {
90369037
self.broadcast_interactive_funding(channel, &funding_tx);
@@ -10030,7 +10031,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1003010031
hash_map::Entry::Occupied(mut chan_entry) => {
1003110032
match chan_entry.get_mut().as_funded_mut() {
1003210033
Some(chan) => {
10033-
let (funding_tx_opt, tx_signatures_opt) = try_channel_entry!(self, peer_state, chan.tx_signatures(msg), chan_entry);
10034+
let (tx_signatures_opt, funding_tx_opt) = try_channel_entry!(self, peer_state, chan.tx_signatures(msg), chan_entry);
1003410035
if let Some(tx_signatures) = tx_signatures_opt {
1003510036
peer_state.pending_msg_events.push(MessageSendEvent::SendTxSignatures {
1003610037
node_id: *counterparty_node_id,

0 commit comments

Comments
 (0)