fn encode(&self) -> Vec<u8>;
#[inline]
fn encoded_len(&self) -> usize { self.encode().len() }
+ #[inline]
+ fn encode_with_len(&self) -> Vec<u8> {
+ let enc = self.encode();
+ let mut res = Vec::with_capacity(enc.len() + 2);
+ res.extend_from_slice(&byte_utils::be16_to_array(enc.len() as u16));
+ res.extend_from_slice(&enc);
+ res
+ }
}
#[derive(Debug)]
pub enum DecodeError {
/// Used to put an error message in a HandleError
pub enum ErrorAction {
+ /// Indicates an inbound HTLC add resulted in a failure, and the UpdateFailHTLC provided in msg
+ /// should be sent back to the sender.
UpdateFailHTLC {
msg: UpdateFailHTLC
},
+ /// The peer took some action which made us think they were useless. Disconnect them.
DisconnectPeer {},
}
pub msg: Option<ErrorAction>, //TODO: Make this required and rename it
}
+/// Struct used to return values from revoke_and_ack messages, containing a bunch of commitment
+/// transaction updates if they were pending.
+pub struct CommitmentUpdate {
+ pub update_add_htlcs: Vec<UpdateAddHTLC>,
+ pub update_fulfill_htlcs: Vec<UpdateFulfillHTLC>,
+ pub update_fail_htlcs: Vec<UpdateFailHTLC>,
+ pub commitment_signed: CommitmentSigned,
+}
+
+pub enum HTLCFailChannelUpdate {
+ ChannelUpdateMessage {
+ msg: ChannelUpdate,
+ },
+ ChannelClosed {
+ short_channel_id: u64,
+ },
+}
+
/// A trait to describe an object which can receive channel messages. Messages MAY be called in
/// paralell when they originate from different their_node_ids, however they MUST NOT be called in
/// paralell when the two calls have the same their_node_id.
// HTLC handling:
fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &UpdateAddHTLC) -> Result<(), HandleError>;
fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFulfillHTLC) -> Result<(), HandleError>;
- fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC) -> Result<(), HandleError>;
+ fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailHTLC) -> Result<Option<HTLCFailChannelUpdate>, HandleError>;
fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &UpdateFailMalformedHTLC) -> Result<(), HandleError>;
- fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned) -> Result<RevokeAndACK, HandleError>;
- fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &RevokeAndACK) -> Result<Option<(Vec<UpdateAddHTLC>, CommitmentSigned)>, HandleError>;
+ fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &CommitmentSigned) -> Result<(RevokeAndACK, Option<CommitmentSigned>), HandleError>;
+ fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &RevokeAndACK) -> Result<Option<CommitmentUpdate>, HandleError>;
fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &UpdateFee) -> Result<(), HandleError>;
/// or returning an Err otherwise.
fn handle_channel_announcement(&self, msg: &ChannelAnnouncement) -> Result<bool, HandleError>;
fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result<(), HandleError>;
+ fn handle_htlc_fail_channel_update(&self, update: &HTLCFailChannelUpdate);
}
pub struct OnionRealm0HopData {
}
impl MsgEncodable for Shutdown {
fn encode(&self) -> Vec<u8> {
- unimplemented!();
+ let mut res = Vec::with_capacity(32 + 2 + self.scriptpubkey.len());
+ res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&byte_utils::be16_to_array(self.scriptpubkey.len() as u16));
+ res.extend_from_slice(&self.scriptpubkey[..]);
+ res
}
}
}
impl MsgEncodable for ClosingSigned {
fn encode(&self) -> Vec<u8> {
- unimplemented!();
+ let mut res = Vec::with_capacity(32+8+64);
+ res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ 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));
+ res
}
}
}
impl MsgDecodable for DecodedOnionErrorPacket {
- fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
- unimplemented!();
+ fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+ if v.len() < 32 + 4 {
+ return Err(DecodeError::WrongLength);
+ }
+ let failuremsg_len = byte_utils::slice_to_be16(&v[32..34]) as usize;
+ if v.len() < 32 + 4 + failuremsg_len {
+ return Err(DecodeError::WrongLength);
+ }
+ let padding_len = byte_utils::slice_to_be16(&v[34 + failuremsg_len..]) as usize;
+ if v.len() < 32 + 4 + failuremsg_len + padding_len {
+ return Err(DecodeError::WrongLength);
+ }
+
+ let mut hmac = [0; 32];
+ hmac.copy_from_slice(&v[0..32]);
+ Ok(Self {
+ hmac,
+ failuremsg: v[34..34 + failuremsg_len].to_vec(),
+ pad: v[36 + failuremsg_len..36 + failuremsg_len + padding_len].to_vec(),
+ })
}
}
impl MsgEncodable for DecodedOnionErrorPacket {