Skip to content

Commit 7dc215e

Browse files
committed
Make my_current_per_commitment_point Option
Per Bolt lightningdevkit#2, both your_last_per_commitment_secret & my_current_per_commitment_point are optional depending on `data_loss_protect`
1 parent 7659877 commit 7dc215e

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

src/ln/msgs.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ pub struct ChannelReestablish {
281281
pub next_local_commitment_number: u64,
282282
pub next_remote_commitment_number: u64,
283283
pub your_last_per_commitment_secret: Option<[u8; 32]>,
284-
pub my_current_per_commitment_point: PublicKey,
284+
pub my_current_per_commitment_point: Option<PublicKey>,
285285
}
286286

287287
#[derive(Clone)]
@@ -1118,48 +1118,43 @@ impl MsgEncodable for UpdateFee {
11181118

11191119
impl MsgDecodable for ChannelReestablish {
11201120
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1121-
if v.len() < 32+2*8+33 {
1121+
if v.len() < 32+2*8 {
11221122
return Err(DecodeError::ShortRead);
11231123
}
11241124

1125-
let your_last_per_commitment_secret = if v.len() > 32+2*8+33 {
1126-
if v.len() < 32+2*8+33 + 32 {
1125+
let (your_last_per_commitment_secret, my_current_per_commitment_point) = if v.len() > 32+2*8 {
1126+
if v.len() < 32+2*8 + 33+32 {
11271127
return Err(DecodeError::ShortRead);
11281128
}
11291129
let mut inner_array = [0; 32];
11301130
inner_array.copy_from_slice(&v[48..48+32]);
1131-
Some(inner_array)
1132-
} else { None };
1131+
(Some(inner_array), {
1132+
let ctx = Secp256k1::without_caps();
1133+
Some(secp_pubkey!(&ctx, &v[48+32..48+32+33]))
1134+
})
1135+
} else { (None, None) };
11331136

1134-
let option_size = match &your_last_per_commitment_secret {
1135-
&Some(ref _ary) => 32,
1136-
&None => 0,
1137-
};
11381137
Ok(Self {
11391138
channel_id: deserialize(&v[0..32]).unwrap(),
11401139
next_local_commitment_number: byte_utils::slice_to_be64(&v[32..40]),
11411140
next_remote_commitment_number: byte_utils::slice_to_be64(&v[40..48]),
11421141
your_last_per_commitment_secret: your_last_per_commitment_secret,
1143-
my_current_per_commitment_point: {
1144-
let ctx = Secp256k1::without_caps();
1145-
secp_pubkey!(&ctx, &v[48+option_size..48+option_size+33])
1146-
}
1142+
my_current_per_commitment_point: my_current_per_commitment_point,
11471143
})
11481144
}
11491145
}
11501146
impl MsgEncodable for ChannelReestablish {
11511147
fn encode(&self) -> Vec<u8> {
1152-
let mut res = Vec::with_capacity(if self.your_last_per_commitment_secret.is_some() { 32+2*3+33 + 32 } else { 32+2*8+33 });
1148+
let mut res = Vec::with_capacity(if self.your_last_per_commitment_secret.is_some() { 32+2*8+33+32 } else { 32+2*8 });
11531149

11541150
res.extend_from_slice(&serialize(&self.channel_id).unwrap()[..]);
11551151
res.extend_from_slice(&byte_utils::be64_to_array(self.next_local_commitment_number));
11561152
res.extend_from_slice(&byte_utils::be64_to_array(self.next_remote_commitment_number));
11571153

11581154
if let &Some(ref ary) = &self.your_last_per_commitment_secret {
11591155
res.extend_from_slice(&ary[..]);
1156+
res.extend_from_slice(&self.my_current_per_commitment_point.expect("my_current_per_commitment_point should have been filled").serialize());
11601157
}
1161-
1162-
res.extend_from_slice(&self.my_current_per_commitment_point.serialize());
11631158
res
11641159
}
11651160
}

0 commit comments

Comments
 (0)