+
+impl MsgEncodable for ErrorMessage {
+ fn encode(&self) -> Vec<u8> {
+ let mut res = Vec::with_capacity(34 + self.data.len());
+ res.extend_from_slice(&self.channel_id);
+ res.extend_from_slice(&byte_utils::be16_to_array(self.data.len() as u16));
+ res.extend_from_slice(&self.data.as_bytes());
+ res
+ }
+}
+impl MsgDecodable for ErrorMessage {
+ fn decode(v: &[u8]) -> Result<Self,DecodeError> {
+ if v.len() < 34 {
+ return Err(DecodeError::ShortRead);
+ }
+ // Unlike most messages, BOLT 1 requires we truncate our read if the value is out of range
+ let len = cmp::min(byte_utils::slice_to_be16(&v[32..34]) as usize, v.len() - 34);
+ let data = match String::from_utf8(v[34..34 + len].to_vec()) {
+ Ok(s) => s,
+ Err(_) => return Err(DecodeError::BadText),
+ };
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
+ Ok(Self {
+ channel_id,
+ data,
+ })
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use hex;
+ 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::decode("0101010101010101010101010101010101010101010101010101010101010101").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::decode("0101010101010101010101010101010101010101010101010101010101010101").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]
+ );
+ }
+}