From 4e104b4db197ae9014065c4ee0b03021fc29f848 Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Wed, 1 Nov 2023 20:52:10 +1100 Subject: [PATCH] Remain backwards-compatible with SubChannels without channel_keys_id --- dlc-manager/src/sub_channel_manager.rs | 35 +++++++++++++++++++++----- dlc-manager/src/subchannel/mod.rs | 5 +++- dlc-manager/src/subchannel/ser.rs | 2 +- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/dlc-manager/src/sub_channel_manager.rs b/dlc-manager/src/sub_channel_manager.rs index 5f0f386b..e1710c3e 100644 --- a/dlc-manager/src/sub_channel_manager.rs +++ b/dlc-manager/src/sub_channel_manager.rs @@ -316,6 +316,11 @@ where SubChannelState::OffChainClosed => { s.is_offer = true; s.update_idx -= 1; + + // We do this to give older channels a chance to upgrade to the new version which + // expects this field to be set to `Some`. + s.channel_keys_id = Some(channel_details.channel_keys_id); + Some(s) } _ => return Err(Error::InvalidState( @@ -411,7 +416,7 @@ where counter_fund_pk: channel_details.counter_funding_pubkey.ok_or_else(|| { Error::InvalidState("Counter funding PK is missing".to_string()) })?, - channel_keys_id: channel_details.channel_keys_id, + channel_keys_id: Some(channel_details.channel_keys_id), } } }; @@ -1581,6 +1586,11 @@ where let sub_channel = match sub_channel { Some(mut s) => { s.state = SubChannelState::Offered(offered_sub_channel); + + // We do this to give older channels a chance to upgrade to the new version which + // expects this field to be set to `Some`. + s.channel_keys_id = Some(channel_details.channel_keys_id); + s } None => SubChannel { @@ -1607,7 +1617,7 @@ where counter_fund_pk: channel_details.counter_funding_pubkey.ok_or_else(|| { Error::InvalidState("Counter funding PK is missing".to_string()) })?, - channel_keys_id: channel_details.channel_keys_id, + channel_keys_id: Some(channel_details.channel_keys_id), }, }; @@ -3691,11 +3701,24 @@ where split_tx: &Transaction, ) -> Result { let mut signers = self.ln_channel_signers.lock().unwrap(); + + let channel_keys_id = match sub_channel.channel_keys_id { + Some(channel_keys_id) => channel_keys_id, + None => { + let channel_id = sub_channel.channel_id; + let channel_details = self + .ln_channel_manager + .get_channel_details(&channel_id) + .ok_or_else(|| { + Error::InvalidParameters(format!("Unknown LN channel {channel_id:02x?}")) + })?; + channel_details.channel_keys_id + } + }; + let signer = signers.entry(sub_channel.channel_id).or_insert( - self.signer_provider.derive_ln_dlc_channel_signer( - sub_channel.fund_value_satoshis, - sub_channel.channel_keys_id, - ), + self.signer_provider + .derive_ln_dlc_channel_signer(sub_channel.fund_value_satoshis, channel_keys_id), ); signer.get_holder_split_tx_signature( self.dlc_channel_manager.get_secp(), diff --git a/dlc-manager/src/subchannel/mod.rs b/dlc-manager/src/subchannel/mod.rs index f2ff072a..da557857 100644 --- a/dlc-manager/src/subchannel/mod.rs +++ b/dlc-manager/src/subchannel/mod.rs @@ -59,7 +59,10 @@ pub struct SubChannel { /// The revocation secrets from the remote party for already revoked split transactions. pub counter_party_secrets: CounterpartyCommitmentSecrets, /// The id used to derive the keys for the Lightning channel. - pub channel_keys_id: [u8; 32], + /// + /// This is an [`Option`] to maintain backwards compatibility with versions of this struct which + /// were persisted without this field. + pub channel_keys_id: Option<[u8; 32]>, } impl std::fmt::Debug for SubChannel { diff --git a/dlc-manager/src/subchannel/ser.rs b/dlc-manager/src/subchannel/ser.rs index 0dece5c0..58dd5940 100644 --- a/dlc-manager/src/subchannel/ser.rs +++ b/dlc-manager/src/subchannel/ser.rs @@ -25,7 +25,7 @@ impl_dlc_writeable!(SubChannel, { (own_fund_pk, writeable), (counter_fund_pk, writeable), (counter_party_secrets, writeable), - (channel_keys_id, writeable) + (channel_keys_id, option) }); impl_dlc_writeable_enum!(SubChannelState,