From 8f0cd90c6ce08c30d07bdfdba28b3a0e2c929dfe Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 4 Feb 2022 17:39:46 +0000 Subject: [PATCH] fix serialization backwards compat --- lightning/src/ln/channel.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 72f6df1e..82ad6c88 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -39,7 +39,7 @@ use util::events::ClosureReason; use util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter}; use util::logger::Logger; use util::errors::APIError; -use util::config::{UserConfig,ChannelConfig,ChannelHandshakeConfig}; +use util::config::{UserConfig,ChannelConfig}; use util::scid_utils::scid_from_parts; use io; @@ -5193,7 +5193,6 @@ impl Writeable for Channel { self.config.forwarding_fee_proportional_millionths.write(writer)?; self.config.cltv_expiry_delta.write(writer)?; self.config.announced_channel.write(writer)?; - // self.config.commit_upfront_shutdown_pubkey.write(writer)?; self.commit_upfront_shutdown_pubkey.write(writer)?; self.channel_id.write(writer)?; @@ -5437,6 +5436,7 @@ impl Writeable for Channel { (9, self.target_closing_feerate_sats_per_kw, option), (11, self.monitor_pending_finalized_fulfills, vec_type), (13, self.channel_creation_height, required), + (15, self.commit_upfront_shutdown_pubkey, required), }); Ok(()) @@ -5458,12 +5458,12 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel config.as_mut().unwrap().forwarding_fee_proportional_millionths = Readable::read(reader)?; config.as_mut().unwrap().cltv_expiry_delta = Readable::read(reader)?; config.as_mut().unwrap().announced_channel = Readable::read(reader)?; - // config.as_mut().unwrap().commit_upfront_shutdown_pubkey = Readable::read(reader)?; + config.as_mut().unwrap().commit_upfront_shutdown_pubkey = Some(Readable::read(reader)?); } else { // Read the 8 bytes of backwards-compatibility ChannelConfig data. let mut _val: u64 = Readable::read(reader)?; } - let handshake_config = Some(ChannelHandshakeConfig::default()); let channel_id = Readable::read(reader)?; + let channel_id = Readable::read(reader)?; let channel_state = Readable::read(reader)?; let channel_value_satoshis = Readable::read(reader)?; @@ -5677,6 +5677,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel // only, so we default to that if none was written. let mut channel_type = Some(ChannelTypeFeatures::only_static_remote_key()); let mut channel_creation_height = Some(serialized_height); + let mut commit_upfront_shutdown_pubkey = None; read_tlv_fields!(reader, { (0, announcement_sigs, option), (1, minimum_depth, option), @@ -5689,6 +5690,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel (9, target_closing_feerate_sats_per_kw, option), (11, monitor_pending_finalized_fulfills, vec_type), (13, channel_creation_height, option), + (15, commit_upfront_shutdown_pubkey, option), }); let chan_features = channel_type.as_ref().unwrap(); @@ -5703,12 +5705,28 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel return Err(DecodeError::InvalidValue); } + if commit_upfront_shutdown_pubkey.is_none() { + // commit_upfront_shutdown_pubkey has moved around a good bit, in version 1 + // serialization, it was written out as a part of the explicit field list of the + // `ChannelConfig`. Then, it was written out as a field in the `ChannelConfig` itself. + // Now, it is written out explicitly as its own TLV (as the field has moved to + // `ChannelHandshakeConfig`). + // Thus, if its not in a TLV, we here pull it from the `ChannelConfig`, and if we can't + // find it at all, fail. + let legacy_commit_upfront_shutdown_pubkey = config.as_ref().unwrap().commit_upfront_shutdown_pubkey; + if let Some(val) = legacy_commit_upfront_shutdown_pubkey { + commit_upfront_shutdown_pubkey = Some(val); + } else { + return Err(DecodeError::InvalidValue); + } + } + let mut secp_ctx = Secp256k1::new(); secp_ctx.seeded_randomize(&keys_source.get_secure_random_bytes()); Ok(Channel { user_id, - commit_upfront_shutdown_pubkey: handshake_config.unwrap().commit_upfront_shutdown_pubkey, + commit_upfront_shutdown_pubkey: commit_upfront_shutdown_pubkey.unwrap(), config: config.unwrap(), channel_id, channel_state, -- 2.30.2