X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Fmsgs.rs;h=a10a50e22d3f1ba25644e3a55a05824a658d4327;hb=00bedd0a107b0ca5bf8f46812a951cdd9b714b2a;hp=f0f1d4890e13370f83cc3c8c633da57f7b03ce20;hpb=d7b33fcd847f5480e438b2df176449d73f582090;p=rust-lightning diff --git a/src/ln/msgs.rs b/src/ln/msgs.rs index f0f1d489..a10a50e2 100644 --- a/src/ln/msgs.rs +++ b/src/ln/msgs.rs @@ -1,6 +1,5 @@ use secp256k1::key::PublicKey; use secp256k1::{Secp256k1, Signature}; -use bitcoin::util::uint::Uint256; use bitcoin::util::hash::Sha256dHash; use bitcoin::network::serialize::{deserialize,serialize}; use bitcoin::blockdata::script::Script; @@ -61,9 +60,16 @@ impl LocalFeatures { self.flags.len() > 0 && (self.flags[0] & 1) != 0 } - pub fn supports_initial_routing_sync(&self) -> bool { + pub fn initial_routing_sync(&self) -> bool { self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0 } + pub fn set_initial_routing_sync(&mut self) { + if self.flags.len() == 0 { + self.flags.resize(1, 1 << 3); + } else { + self.flags[0] |= 1 << 3; + } + } pub fn supports_upfront_shutdown_script(&self) -> bool { self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0 @@ -132,9 +138,18 @@ pub struct Init { pub local_features: LocalFeatures, } +pub struct Ping { + pub ponglen: u16, + pub byteslen: u16, +} + +pub struct Pong { + pub byteslen: u16, +} + pub struct OpenChannel { pub chain_hash: Sha256dHash, - pub temporary_channel_id: Uint256, + pub temporary_channel_id: [u8; 32], pub funding_satoshis: u64, pub push_msat: u64, pub dust_limit_satoshis: u64, @@ -155,7 +170,7 @@ pub struct OpenChannel { } pub struct AcceptChannel { - pub temporary_channel_id: Uint256, + pub temporary_channel_id: [u8; 32], pub dust_limit_satoshis: u64, pub max_htlc_value_in_flight_msat: u64, pub channel_reserve_satoshis: u64, @@ -173,36 +188,36 @@ pub struct AcceptChannel { } pub struct FundingCreated { - pub temporary_channel_id: Uint256, + pub temporary_channel_id: [u8; 32], pub funding_txid: Sha256dHash, pub funding_output_index: u16, pub signature: Signature, } pub struct FundingSigned { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub signature: Signature, } pub struct FundingLocked { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub next_per_commitment_point: PublicKey, } pub struct Shutdown { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub scriptpubkey: Script, } pub struct ClosingSigned { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub fee_satoshis: u64, pub signature: Signature, } #[derive(Clone)] pub struct UpdateAddHTLC { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub htlc_id: u64, pub amount_msat: u64, pub payment_hash: [u8; 32], @@ -212,21 +227,21 @@ pub struct UpdateAddHTLC { #[derive(Clone)] pub struct UpdateFulfillHTLC { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub htlc_id: u64, pub payment_preimage: [u8; 32], } #[derive(Clone)] pub struct UpdateFailHTLC { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub htlc_id: u64, pub reason: OnionErrorPacket, } #[derive(Clone)] pub struct UpdateFailMalformedHTLC { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub htlc_id: u64, pub sha256_of_onion: [u8; 32], pub failure_code: u16, @@ -234,24 +249,24 @@ pub struct UpdateFailMalformedHTLC { #[derive(Clone)] pub struct CommitmentSigned { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub signature: Signature, pub htlc_signatures: Vec, } pub struct RevokeAndACK { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub per_commitment_secret: [u8; 32], pub next_per_commitment_point: PublicKey, } pub struct UpdateFee { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub feerate_per_kw: u32, } pub struct ChannelReestablish { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub next_local_commitment_number: u64, pub next_remote_commitment_number: u64, pub your_last_per_commitment_secret: Option<[u8; 32]>, @@ -260,7 +275,7 @@ pub struct ChannelReestablish { #[derive(Clone)] pub struct AnnouncementSignatures { - pub channel_id: Uint256, + pub channel_id: [u8; 32], pub short_channel_id: u64, pub node_signature: Signature, pub bitcoin_signature: Signature, @@ -364,7 +379,7 @@ pub enum ErrorAction { pub struct HandleError { //TODO: rename me pub err: &'static str, - pub msg: Option, //TODO: Make this required and rename it + pub action: Option, //TODO: Make this required } /// Struct used to return values from revoke_and_ack messages, containing a bunch of commitment @@ -572,6 +587,54 @@ impl MsgEncodable for Init { } } +impl MsgDecodable for Ping { + fn decode(v: &[u8]) -> Result { + if v.len() < 4 { + return Err(DecodeError::WrongLength); + } + let ponglen = byte_utils::slice_to_be16(&v[0..2]); + let byteslen = byte_utils::slice_to_be16(&v[2..4]); + if v.len() < 4 + byteslen as usize { + return Err(DecodeError::WrongLength); + } + Ok(Self { + ponglen, + byteslen, + }) + } +} +impl MsgEncodable for Ping { + fn encode(&self) -> Vec { + let mut res = Vec::with_capacity(self.byteslen as usize + 2); + res.extend_from_slice(&byte_utils::be16_to_array(self.byteslen)); + res.resize(2 + self.byteslen as usize, 0); + res + } +} + +impl MsgDecodable for Pong { + fn decode(v: &[u8]) -> Result { + if v.len() < 2 { + return Err(DecodeError::WrongLength); + } + let byteslen = byte_utils::slice_to_be16(&v[0..2]); + if v.len() < 2 + byteslen as usize { + return Err(DecodeError::WrongLength); + } + Ok(Self { + byteslen + }) + } +} +impl MsgEncodable for Pong { + fn encode(&self) -> Vec { + let mut res = Vec::with_capacity(self.byteslen as usize + 2); + res.extend_from_slice(&byte_utils::be16_to_array(self.byteslen)); + res.resize(2 + self.byteslen as usize, 0); + res + } +} + impl MsgDecodable for OpenChannel { fn decode(v: &[u8]) -> Result { if v.len() < 2*32+6*8+4+2*2+6*33+1 { @@ -663,8 +726,10 @@ impl MsgDecodable for AcceptChannel { return Err(DecodeError::WrongLength); } + let mut temporary_channel_id = [0; 32]; + temporary_channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - temporary_channel_id: deserialize(&v[0..32]).unwrap(), + temporary_channel_id, dust_limit_satoshis: byte_utils::slice_to_be64(&v[32..40]), max_htlc_value_in_flight_msat: byte_utils::slice_to_be64(&v[40..48]), channel_reserve_satoshis: byte_utils::slice_to_be64(&v[48..56]), @@ -688,7 +753,7 @@ impl MsgEncodable for AcceptChannel { &Some(ref script) => Vec::with_capacity(270 + 2 + script.len()), &None => Vec::with_capacity(270), }; - res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap()[..]); + res.extend_from_slice(&self.temporary_channel_id); res.extend_from_slice(&byte_utils::be64_to_array(self.dust_limit_satoshis)); res.extend_from_slice(&byte_utils::be64_to_array(self.max_htlc_value_in_flight_msat)); res.extend_from_slice(&byte_utils::be64_to_array(self.channel_reserve_satoshis)); @@ -716,8 +781,10 @@ impl MsgDecodable for FundingCreated { return Err(DecodeError::WrongLength); } let ctx = Secp256k1::without_caps(); + let mut temporary_channel_id = [0; 32]; + temporary_channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - temporary_channel_id: deserialize(&v[0..32]).unwrap(), + temporary_channel_id, funding_txid: deserialize(&v[32..64]).unwrap(), funding_output_index: byte_utils::slice_to_be16(&v[64..66]), signature: secp_signature!(&ctx, &v[66..130]), @@ -727,7 +794,7 @@ impl MsgDecodable for FundingCreated { impl MsgEncodable for FundingCreated { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+32+2+64); - res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap()[..]); + res.extend_from_slice(&self.temporary_channel_id); res.extend_from_slice(&serialize(&self.funding_txid).unwrap()[..]); res.extend_from_slice(&byte_utils::be16_to_array(self.funding_output_index)); let secp_ctx = Secp256k1::without_caps(); @@ -742,8 +809,10 @@ impl MsgDecodable for FundingSigned { return Err(DecodeError::WrongLength); } let ctx = Secp256k1::without_caps(); + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, signature: secp_signature!(&ctx, &v[32..96]), }) } @@ -751,7 +820,7 @@ impl MsgDecodable for FundingSigned { impl MsgEncodable for FundingSigned { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+64); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&self.signature.serialize_compact(&Secp256k1::without_caps())); res } @@ -763,8 +832,10 @@ impl MsgDecodable for FundingLocked { return Err(DecodeError::WrongLength); } let ctx = Secp256k1::without_caps(); + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, next_per_commitment_point: secp_pubkey!(&ctx, &v[32..65]), }) } @@ -772,7 +843,7 @@ impl MsgDecodable for FundingLocked { impl MsgEncodable for FundingLocked { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+33); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&self.next_per_commitment_point.serialize()); res } @@ -787,8 +858,10 @@ impl MsgDecodable for Shutdown { if v.len() < 32 + 2 + scriptlen { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, scriptpubkey: Script::from(v[34..34 + scriptlen].to_vec()), }) } @@ -796,7 +869,7 @@ impl MsgDecodable for Shutdown { impl MsgEncodable for Shutdown { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32 + 2 + self.scriptpubkey.len()); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be16_to_array(self.scriptpubkey.len() as u16)); res.extend_from_slice(&self.scriptpubkey[..]); res @@ -809,8 +882,10 @@ impl MsgDecodable for ClosingSigned { return Err(DecodeError::WrongLength); } let secp_ctx = Secp256k1::without_caps(); + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, fee_satoshis: byte_utils::slice_to_be64(&v[32..40]), signature: secp_signature!(&secp_ctx, &v[40..104]), }) @@ -819,7 +894,7 @@ impl MsgDecodable for ClosingSigned { impl MsgEncodable for ClosingSigned { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+8+64); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be64_to_array(self.fee_satoshis)); let secp_ctx = Secp256k1::without_caps(); res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx)); @@ -832,10 +907,12 @@ impl MsgDecodable for UpdateAddHTLC { if v.len() < 32+8+8+32+4+1+33+20*65+32 { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); let mut payment_hash = [0; 32]; payment_hash.copy_from_slice(&v[48..80]); Ok(Self{ - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, htlc_id: byte_utils::slice_to_be64(&v[32..40]), amount_msat: byte_utils::slice_to_be64(&v[40..48]), payment_hash, @@ -847,7 +924,7 @@ impl MsgDecodable for UpdateAddHTLC { impl MsgEncodable for UpdateAddHTLC { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+8+8+32+4+1+1366); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id)); res.extend_from_slice(&byte_utils::be64_to_array(self.amount_msat)); res.extend_from_slice(&self.payment_hash); @@ -862,10 +939,12 @@ impl MsgDecodable for UpdateFulfillHTLC { if v.len() < 32+8+32 { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); let mut payment_preimage = [0; 32]; payment_preimage.copy_from_slice(&v[40..72]); Ok(Self{ - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, htlc_id: byte_utils::slice_to_be64(&v[32..40]), payment_preimage, }) @@ -874,7 +953,7 @@ impl MsgDecodable for UpdateFulfillHTLC { impl MsgEncodable for UpdateFulfillHTLC { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+8+32); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id)); res.extend_from_slice(&self.payment_preimage); res @@ -886,8 +965,10 @@ impl MsgDecodable for UpdateFailHTLC { if v.len() < 32+8 { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); Ok(Self{ - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, htlc_id: byte_utils::slice_to_be64(&v[32..40]), reason: OnionErrorPacket::decode(&v[40..])?, }) @@ -897,7 +978,7 @@ impl MsgEncodable for UpdateFailHTLC { fn encode(&self) -> Vec { let reason = self.reason.encode(); let mut res = Vec::with_capacity(32+8+reason.len()); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id)); res.extend_from_slice(&reason[..]); res @@ -909,10 +990,12 @@ impl MsgDecodable for UpdateFailMalformedHTLC { if v.len() < 32+8+32+2 { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); let mut sha256_of_onion = [0; 32]; sha256_of_onion.copy_from_slice(&v[40..72]); Ok(Self{ - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, htlc_id: byte_utils::slice_to_be64(&v[32..40]), sha256_of_onion, failure_code: byte_utils::slice_to_be16(&v[72..74]), @@ -922,7 +1005,7 @@ impl MsgDecodable for UpdateFailMalformedHTLC { impl MsgEncodable for UpdateFailMalformedHTLC { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+8+32+2); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id)); res.extend_from_slice(&self.sha256_of_onion); res.extend_from_slice(&byte_utils::be16_to_array(self.failure_code)); @@ -935,6 +1018,9 @@ impl MsgDecodable for CommitmentSigned { if v.len() < 32+64+2 { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); + let htlcs = byte_utils::slice_to_be16(&v[96..98]) as usize; if v.len() < 32+64+2+htlcs*64 { return Err(DecodeError::WrongLength); @@ -945,7 +1031,7 @@ impl MsgDecodable for CommitmentSigned { htlc_signatures.push(secp_signature!(&secp_ctx, &v[98+i*64..98+(i+1)*64])); } Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, signature: secp_signature!(&secp_ctx, &v[32..96]), htlc_signatures, }) @@ -954,7 +1040,7 @@ impl MsgDecodable for CommitmentSigned { impl MsgEncodable for CommitmentSigned { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+64+2+self.htlc_signatures.len()*64); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); let secp_ctx = Secp256k1::without_caps(); res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx)); res.extend_from_slice(&byte_utils::be16_to_array(self.htlc_signatures.len() as u16)); @@ -970,11 +1056,13 @@ impl MsgDecodable for RevokeAndACK { if v.len() < 32+32+33 { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); let mut per_commitment_secret = [0; 32]; per_commitment_secret.copy_from_slice(&v[32..64]); let secp_ctx = Secp256k1::without_caps(); Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, per_commitment_secret, next_per_commitment_point: secp_pubkey!(&secp_ctx, &v[64..97]), }) @@ -983,7 +1071,7 @@ impl MsgDecodable for RevokeAndACK { impl MsgEncodable for RevokeAndACK { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+32+33); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&self.per_commitment_secret); res.extend_from_slice(&self.next_per_commitment_point.serialize()); res @@ -995,8 +1083,10 @@ impl MsgDecodable for UpdateFee { if v.len() < 32+4 { return Err(DecodeError::WrongLength); } + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, feerate_per_kw: byte_utils::slice_to_be32(&v[32..36]), }) } @@ -1004,20 +1094,57 @@ impl MsgDecodable for UpdateFee { impl MsgEncodable for UpdateFee { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+4); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be32_to_array(self.feerate_per_kw)); res } } impl MsgDecodable for ChannelReestablish { - fn decode(_v: &[u8]) -> Result { - unimplemented!(); + fn decode(v: &[u8]) -> Result { + if v.len() < 32+2*8+33 { + return Err(DecodeError::WrongLength); + } + + let your_last_per_commitment_secret = if v.len() > 32+2*8+33 { + if v.len() < 32+2*8+33 + 32 { + return Err(DecodeError::WrongLength); + } + let mut inner_array = [0; 32]; + inner_array.copy_from_slice(&v[48..48+32]); + Some(inner_array) + } else { None }; + + let option_size = match &your_last_per_commitment_secret { + &Some(ref _ary) => 32, + &None => 0, + }; + Ok(Self { + channel_id: deserialize(&v[0..32]).unwrap(), + next_local_commitment_number: byte_utils::slice_to_be64(&v[32..40]), + next_remote_commitment_number: byte_utils::slice_to_be64(&v[40..48]), + your_last_per_commitment_secret: your_last_per_commitment_secret, + my_current_per_commitment_point: { + let ctx = Secp256k1::without_caps(); + secp_pubkey!(&ctx, &v[48+option_size..48+option_size+33]) + } + }) } } impl MsgEncodable for ChannelReestablish { fn encode(&self) -> Vec { - unimplemented!(); + 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 }); + + res.extend_from_slice(&serialize(&self.channel_id).unwrap()[..]); + res.extend_from_slice(&byte_utils::be64_to_array(self.next_local_commitment_number)); + res.extend_from_slice(&byte_utils::be64_to_array(self.next_remote_commitment_number)); + + if let &Some(ref ary) = &self.your_last_per_commitment_secret { + res.extend_from_slice(&ary[..]); + } + + res.extend_from_slice(&self.my_current_per_commitment_point.serialize()); + res } } @@ -1027,8 +1154,10 @@ impl MsgDecodable for AnnouncementSignatures { return Err(DecodeError::WrongLength); } let secp_ctx = Secp256k1::without_caps(); + let mut channel_id = [0; 32]; + channel_id[..].copy_from_slice(&v[0..32]); Ok(Self { - channel_id: deserialize(&v[0..32]).unwrap(), + channel_id, short_channel_id: byte_utils::slice_to_be64(&v[32..40]), node_signature: secp_signature!(&secp_ctx, &v[40..104]), bitcoin_signature: secp_signature!(&secp_ctx, &v[104..168]), @@ -1038,7 +1167,7 @@ impl MsgDecodable for AnnouncementSignatures { impl MsgEncodable for AnnouncementSignatures { fn encode(&self) -> Vec { let mut res = Vec::with_capacity(32+8+64*2); - res.extend_from_slice(&serialize(&self.channel_id).unwrap()); + res.extend_from_slice(&self.channel_id); res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id)); let secp_ctx = Secp256k1::without_caps(); res.extend_from_slice(&self.node_signature.serialize_compact(&secp_ctx)); @@ -1470,3 +1599,56 @@ impl MsgEncodable for OnionErrorPacket { res } } + +#[cfg(test)] +mod tests { + use bitcoin::util::misc::hex_bytes; + use ln::msgs::MsgEncodable; + use ln::msgs; + use secp256k1::key::{PublicKey,SecretKey}; + use secp256k1::Secp256k1; + + #[test] + fn encoding_channel_reestablish_no_secret() { + let public_key = { + let secp_ctx = Secp256k1::new(); + PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap()).unwrap() + }; + + let cr = msgs::ChannelReestablish { + channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0], + next_local_commitment_number: 3, + next_remote_commitment_number: 4, + your_last_per_commitment_secret: None, + my_current_per_commitment_point: public_key, + }; + + let encoded_value = cr.encode(); + assert_eq!( + encoded_value, + vec![4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 3, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30, 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143] + ); + } + + #[test] + fn encoding_channel_reestablish_with_secret() { + let public_key = { + let secp_ctx = Secp256k1::new(); + PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex_bytes("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap()).unwrap() + }; + + let cr = msgs::ChannelReestablish { + channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0], + next_local_commitment_number: 3, + next_remote_commitment_number: 4, + your_last_per_commitment_secret: Some([9; 32]), + my_current_per_commitment_point: public_key, + }; + + let encoded_value = cr.encode(); + assert_eq!( + encoded_value, + vec![4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 3, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30, 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143] + ); + } +}