UnknownRealmByte,
/// Failed to decode a public key (ie it's invalid)
BadPublicKey,
+ /// Failed to decode a signature (ie it's invalid)
+ BadSignature,
/// Buffer not of right length (either too short or too long)
WrongLength,
}
pub payment_preimage: [u8; 32],
}
+#[derive(Clone)]
pub struct UpdateFailHTLC {
pub channel_id: Uint256,
pub htlc_id: u64,
pub reason: OnionErrorPacket,
}
+#[derive(Clone)]
pub struct UpdateFailMalformedHTLC {
pub channel_id: Uint256,
pub htlc_id: u64,
pub msg: Option<ErrorMessage>, //TODO: Move into an Action enum and require it!
}
+/// 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.
pub trait ChannelMessageHandler : events::EventsProvider {
//Channel init:
fn handle_open_channel(&self, their_node_id: &PublicKey, msg: &OpenChannel) -> Result<AcceptChannel, HandleError>;
pub pad: Vec<u8>,
}
+#[derive(Clone)]
pub struct OnionErrorPacket {
// This really should be a constant size slice, but the spec lets these things be up to 128KB?
// (TODO) We limit it in decode to much lower...
match *self {
DecodeError::UnknownRealmByte => "Unknown realm byte in Onion packet",
DecodeError::BadPublicKey => "Invalid public key in packet",
+ DecodeError::BadSignature => "Invalid signature in packet",
DecodeError::WrongLength => "Data was wrong length for packet",
}
}
};
}
+macro_rules! secp_signature {
+ ( $ctx: expr, $slice: expr ) => {
+ match Signature::from_compact($ctx, $slice) {
+ Ok(sig) => sig,
+ Err(_) => return Err(DecodeError::BadSignature)
+ }
+ };
+}
+
impl MsgDecodable for LocalFeatures {
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
if v.len() < 3 { return Err(DecodeError::WrongLength); }
let len = byte_utils::slice_to_be16(&v[0..2]) as usize;
- if v.len() != len + 2 { return Err(DecodeError::WrongLength); }
+ if v.len() < len + 2 { return Err(DecodeError::WrongLength); }
let mut flags = Vec::with_capacity(len);
flags.extend_from_slice(&v[2..]);
Ok(Self {
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
if v.len() < 3 { return Err(DecodeError::WrongLength); }
let len = byte_utils::slice_to_be16(&v[0..2]) as usize;
- if v.len() != len + 2 { return Err(DecodeError::WrongLength); }
+ if v.len() < len + 2 { return Err(DecodeError::WrongLength); }
let mut flags = Vec::with_capacity(len);
flags.extend_from_slice(&v[2..]);
Ok(Self {
impl MsgDecodable for Init {
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
- let global_features = try!(GlobalFeatures::decode(v));
- if global_features.flags.len() + 4 <= v.len() {
- return Err(DecodeError::WrongLength);
- }
- let local_features = try!(LocalFeatures::decode(&v[global_features.flags.len() + 2..]));
- if global_features.flags.len() + local_features.flags.len() + 4 != v.len() {
+ let global_features = GlobalFeatures::decode(v)?;
+ if v.len() < global_features.flags.len() + 4 {
return Err(DecodeError::WrongLength);
}
+ let local_features = LocalFeatures::decode(&v[global_features.flags.len() + 2..])?;
Ok(Self {
global_features: global_features,
local_features: local_features,
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 {
return Err(DecodeError::WrongLength);
}
let ctx = Secp256k1::without_caps();
- let funding_pubkey = secp_pubkey!(&ctx, &v[120..153]);
- let revocation_basepoint = secp_pubkey!(&ctx, &v[153..186]);
- let payment_basepoint = secp_pubkey!(&ctx, &v[186..219]);
- let delayed_payment_basepoint = secp_pubkey!(&ctx, &v[219..252]);
- let htlc_basepoint = secp_pubkey!(&ctx, &v[252..285]);
- let first_per_commitment_point = secp_pubkey!(&ctx, &v[285..318]);
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 {
+ if v.len() < 321+len {
return Err(DecodeError::WrongLength);
}
shutdown_scriptpubkey = Some(Script::from(v[321..321+len].to_vec()));
+ } else if v.len() != 2*32+6*8+4+2*2+6*33+1 { // Message cant have 1 extra byte
+ return Err(DecodeError::WrongLength);
}
Ok(OpenChannel {
feerate_per_kw: byte_utils::slice_to_be32(&v[112..116]),
to_self_delay: byte_utils::slice_to_be16(&v[116..118]),
max_accepted_htlcs: byte_utils::slice_to_be16(&v[118..120]),
- funding_pubkey: funding_pubkey,
- revocation_basepoint: revocation_basepoint,
- payment_basepoint: payment_basepoint,
- delayed_payment_basepoint: delayed_payment_basepoint,
- htlc_basepoint: htlc_basepoint,
- first_per_commitment_point: first_per_commitment_point,
+ funding_pubkey: secp_pubkey!(&ctx, &v[120..153]),
+ revocation_basepoint: secp_pubkey!(&ctx, &v[153..186]),
+ payment_basepoint: secp_pubkey!(&ctx, &v[186..219]),
+ delayed_payment_basepoint: secp_pubkey!(&ctx, &v[219..252]),
+ htlc_basepoint: secp_pubkey!(&ctx, &v[252..285]),
+ first_per_commitment_point: secp_pubkey!(&ctx, &v[285..318]),
channel_flags: v[318],
shutdown_scriptpubkey: shutdown_scriptpubkey
})
}
}
-
impl MsgDecodable for AcceptChannel {
- fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
- unimplemented!();
+ fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+ if v.len() < 32+4*8+4+2*2+6*33 {
+ return Err(DecodeError::WrongLength);
+ }
+ let ctx = Secp256k1::without_caps();
+
+ let mut shutdown_scriptpubkey = None;
+ if v.len() >= 272 {
+ let len = byte_utils::slice_to_be16(&v[270..272]) as usize;
+ if v.len() < 272+len {
+ return Err(DecodeError::WrongLength);
+ }
+ shutdown_scriptpubkey = Some(Script::from(v[272..272+len].to_vec()));
+ } else if v.len() != 32+4*8+4+2*2+6*33 { // Message cant have 1 extra byte
+ return Err(DecodeError::WrongLength);
+ }
+
+ Ok(Self {
+ temporary_channel_id: deserialize(&v[0..32]).unwrap(),
+ 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]),
+ htlc_minimum_msat: byte_utils::slice_to_be64(&v[56..64]),
+ minimum_depth: byte_utils::slice_to_be32(&v[64..68]),
+ to_self_delay: byte_utils::slice_to_be16(&v[68..70]),
+ max_accepted_htlcs: byte_utils::slice_to_be16(&v[70..72]),
+ funding_pubkey: secp_pubkey!(&ctx, &v[72..105]),
+ revocation_basepoint: secp_pubkey!(&ctx, &v[105..138]),
+ payment_basepoint: secp_pubkey!(&ctx, &v[138..171]),
+ delayed_payment_basepoint: secp_pubkey!(&ctx, &v[171..204]),
+ htlc_basepoint: secp_pubkey!(&ctx, &v[204..237]),
+ first_per_commitment_point: secp_pubkey!(&ctx, &v[237..270]),
+ shutdown_scriptpubkey: shutdown_scriptpubkey
+ })
}
}
impl MsgEncodable for AcceptChannel {
}
impl MsgDecodable for FundingCreated {
- fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
- unimplemented!();
+ fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+ if v.len() < 32+32+2+64 {
+ return Err(DecodeError::WrongLength);
+ }
+ let ctx = Secp256k1::without_caps();
+ Ok(Self {
+ temporary_channel_id: deserialize(&v[0..32]).unwrap(),
+ 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]),
+ })
}
}
impl MsgEncodable for FundingCreated {
}
impl MsgDecodable for FundingSigned {
- fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
- unimplemented!();
+ fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+ if v.len() < 32+64 {
+ return Err(DecodeError::WrongLength);
+ }
+ let ctx = Secp256k1::without_caps();
+ Ok(Self {
+ channel_id: deserialize(&v[0..32]).unwrap(),
+ signature: secp_signature!(&ctx, &v[32..96]),
+ })
}
}
impl MsgEncodable for FundingSigned {
}
impl MsgDecodable for FundingLocked {
- fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
- unimplemented!();
+ fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+ if v.len() < 32+33 {
+ return Err(DecodeError::WrongLength);
+ }
+ let ctx = Secp256k1::without_caps();
+ Ok(Self {
+ channel_id: deserialize(&v[0..32]).unwrap(),
+ next_per_commitment_point: secp_pubkey!(&ctx, &v[32..65]),
+ })
}
}
impl MsgEncodable for FundingLocked {
}
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 {
}
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 {
}
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 {
}
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 {
}
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 {
}
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 {
}
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 {
impl MsgDecodable for OnionRealm0HopData {
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
- if v.len() != 32 {
+ if v.len() < 32 {
return Err(DecodeError::WrongLength);
}
Ok(OnionRealm0HopData {
impl MsgDecodable for OnionHopData {
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
- if v.len() != 65 {
+ if v.len() < 65 {
return Err(DecodeError::WrongLength);
}
let realm = v[0];
hmac[..].copy_from_slice(&v[33..65]);
Ok(OnionHopData {
realm: realm,
- data: try!(OnionRealm0HopData::decode(&v[1..33])),
+ data: OnionRealm0HopData::decode(&v[1..33])?,
hmac: hmac,
})
}
}
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 {
}
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 {