]> git.bitcoin.ninja Git - rust-lightning/commitdiff
impl a bunch of additional messages, remove some useless mut
authorMatt Corallo <git@bluematt.me>
Thu, 22 Mar 2018 21:42:57 +0000 (17:42 -0400)
committerMatt Corallo <git@bluematt.me>
Thu, 22 Mar 2018 21:43:20 +0000 (17:43 -0400)
src/ln/msgs.rs
src/ln/peer_channel_encryptor.rs

index 3cdd4c77cb0efbd198b2c1ef782186e488088a6f..97ed1e320f9350b4ac69fc04ca7a8ce49abfbb5b 100644 (file)
@@ -684,8 +684,20 @@ impl MsgEncodable for ClosingSigned {
 }
 
 impl MsgDecodable for UpdateAddHTLC {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 32+8+8+32+4+1+33+20*65+32 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let mut payment_hash = [0; 32];
+               payment_hash.copy_from_slice(&v[48..80]);
+               Ok(Self{
+                       channel_id: deserialize(&v[0..32]).unwrap(),
+                       htlc_id: byte_utils::slice_to_be64(&v[32..40]),
+                       amount_msat: byte_utils::slice_to_be64(&v[40..48]),
+                       payment_hash,
+                       cltv_expiry: byte_utils::slice_to_be32(&v[80..84]),
+                       onion_routing_packet: OnionPacket::decode(&v[84..])?,
+               })
        }
 }
 impl MsgEncodable for UpdateAddHTLC {
@@ -695,8 +707,17 @@ impl MsgEncodable for UpdateAddHTLC {
 }
 
 impl MsgDecodable for UpdateFulfillHTLC {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 32+8+32 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let mut payment_preimage = [0; 32];
+               payment_preimage.copy_from_slice(&v[40..72]);
+               Ok(Self{
+                       channel_id: deserialize(&v[0..32]).unwrap(),
+                       htlc_id: byte_utils::slice_to_be64(&v[32..40]),
+                       payment_preimage,
+               })
        }
 }
 impl MsgEncodable for UpdateFulfillHTLC {
@@ -706,8 +727,15 @@ impl MsgEncodable for UpdateFulfillHTLC {
 }
 
 impl MsgDecodable for UpdateFailHTLC {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 32+8 {
+                       return Err(DecodeError::WrongLength);
+               }
+               Ok(Self{
+                       channel_id: deserialize(&v[0..32]).unwrap(),
+                       htlc_id: byte_utils::slice_to_be64(&v[32..40]),
+                       reason: OnionErrorPacket::decode(&v[40..])?,
+               })
        }
 }
 impl MsgEncodable for UpdateFailHTLC {
@@ -717,8 +745,18 @@ impl MsgEncodable for UpdateFailHTLC {
 }
 
 impl MsgDecodable for UpdateFailMalformedHTLC {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 32+8+32+2 {
+                       return Err(DecodeError::WrongLength);
+               }
+               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(),
+                       htlc_id: byte_utils::slice_to_be64(&v[32..40]),
+                       sha256_of_onion,
+                       failure_code: byte_utils::slice_to_be16(&v[72..74]),
+               })
        }
 }
 impl MsgEncodable for UpdateFailMalformedHTLC {
@@ -728,8 +766,24 @@ impl MsgEncodable for UpdateFailMalformedHTLC {
 }
 
 impl MsgDecodable for CommitmentSigned {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 32+64+2 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let htlcs = byte_utils::slice_to_be16(&v[96..98]) as usize;
+               if v.len() < 32+64+2+htlcs*64 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let mut htlc_signatures = Vec::with_capacity(htlcs);
+               let secp_ctx = Secp256k1::without_caps();
+               for i in 0..htlcs {
+                       htlc_signatures.push(secp_signature!(&secp_ctx, &v[98+i*64..98+(i+1)*64]));
+               }
+               Ok(Self {
+                       channel_id: deserialize(&v[0..32]).unwrap(),
+                       signature: secp_signature!(&secp_ctx, &v[32..96]),
+                       htlc_signatures,
+               })
        }
 }
 impl MsgEncodable for CommitmentSigned {
@@ -739,8 +793,18 @@ impl MsgEncodable for CommitmentSigned {
 }
 
 impl MsgDecodable for RevokeAndACK {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 32+32+33 {
+                       return Err(DecodeError::WrongLength);
+               }
+               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(),
+                       per_commitment_secret,
+                       next_per_commitment_point: secp_pubkey!(&secp_ctx, &v[64..97]),
+               })
        }
 }
 impl MsgEncodable for RevokeAndACK {
@@ -750,8 +814,14 @@ impl MsgEncodable for RevokeAndACK {
 }
 
 impl MsgDecodable for UpdateFee {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 32+4 {
+                       return Err(DecodeError::WrongLength);
+               }
+               Ok(Self {
+                       channel_id: deserialize(&v[0..32]).unwrap(),
+                       feerate_per_kw: byte_utils::slice_to_be32(&v[32..36]),
+               })
        }
 }
 impl MsgEncodable for UpdateFee {
@@ -954,8 +1024,21 @@ impl MsgEncodable for OnionHopData {
 }
 
 impl MsgDecodable for OnionPacket {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 1+33+20*65+32 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let mut hop_data = [0; 20*65];
+               hop_data.copy_from_slice(&v[34..1334]);
+               let mut hmac = [0; 32];
+               hmac.copy_from_slice(&v[1334..1366]);
+               let secp_ctx = Secp256k1::without_caps();
+               Ok(Self {
+                       version: v[0],
+                       public_key: secp_pubkey!(&secp_ctx, &v[1..34]),
+                       hop_data,
+                       hmac,
+               })
        }
 }
 impl MsgEncodable for OnionPacket {
@@ -987,8 +1070,17 @@ impl MsgEncodable for DecodedOnionErrorPacket {
 }
 
 impl MsgDecodable for OnionErrorPacket {
-       fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
-               unimplemented!();
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 2 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let len = byte_utils::slice_to_be16(&v[0..2]) as usize;
+               if v.len() < 2 + len {
+                       return Err(DecodeError::WrongLength);
+               }
+               Ok(Self {
+                       data: v[2..len+2].to_vec(),
+               })
        }
 }
 impl MsgEncodable for OnionErrorPacket {
index 9c400ba2b7057986c8a44bced868378f3e3a3213..e22d8bd3574969cdee2ff1470a1207b5f4607480 100644 (file)
@@ -747,7 +747,7 @@ mod tests {
                        let res = outbound_peer.encrypt_message(&msg);
                        assert_eq!(res.len(), 5 + 2*16 + 2);
 
-                       let mut len_header = res[0..2+16].to_vec();
+                       let len_header = res[0..2+16].to_vec();
                        assert_eq!(inbound_peer.decrypt_length_header(&len_header[..]).unwrap() as usize, msg.len());
                        assert_eq!(inbound_peer.decrypt_message(&res[2+16..]).unwrap()[..], msg[..]);