Skip to content

Fix encoding/decoding for OpenChannel #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,17 +668,17 @@ impl MsgEncodable for Pong {

impl MsgDecodable for OpenChannel {
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
if v.len() < 2*32+6*8+4+2*2+6*33+1 {
if v.len() < 2*32+6*8+4+2*2+6*33+1+2 {
return Err(DecodeError::ShortRead);
}
let ctx = Secp256k1::without_caps();

let len = byte_utils::slice_to_be16(&v[319..321]) as usize;
if v.len() < 321+len {
return Err(DecodeError::ShortRead);
}
let mut shutdown_scriptpubkey = None;
if v.len() >= 321 {
let len = byte_utils::slice_to_be16(&v[319..321]) as usize;
if v.len() < 321+len {
return Err(DecodeError::ShortRead);
}
if len > 0 {
shutdown_scriptpubkey = Some(Script::from(v[321..321+len].to_vec()));
}
let mut temp_channel_id = [0; 32];
Expand Down Expand Up @@ -710,7 +710,7 @@ impl MsgEncodable for OpenChannel {
fn encode(&self) -> Vec<u8> {
let mut res = match &self.shutdown_scriptpubkey {
&Some(ref script) => Vec::with_capacity(319 + 2 + script.len()),
&None => Vec::with_capacity(319),
&None => Vec::with_capacity(319 + 2),
};
res.extend_from_slice(&serialize(&self.chain_hash).unwrap());
res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap());
Expand All @@ -734,6 +734,9 @@ impl MsgEncodable for OpenChannel {
res.extend_from_slice(&byte_utils::be16_to_array(script.len() as u16));
res.extend_from_slice(&script[..]);
}
else {
res.extend_from_slice(&byte_utils::be16_to_array(0u16));
}
res
}
}
Expand Down