mirror of
https://github.com/lightningdevkit/rust-lightning.git
synced 2025-03-04 10:58:11 +01:00
Impl a few more msg deserializations, fix a channel panic!() bug
This commit is contained in:
parent
df11c05fa6
commit
4f4c0a8179
2 changed files with 85 additions and 23 deletions
|
@ -374,7 +374,7 @@ impl Channel {
|
||||||
if msg.htlc_minimum_msat >= (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000 {
|
if msg.htlc_minimum_msat >= (msg.funding_satoshis - msg.channel_reserve_satoshis) * 1000 {
|
||||||
return Err(HandleError{err: "Minimum htlc value is full channel value", msg: None});
|
return Err(HandleError{err: "Minimum htlc value is full channel value", msg: None});
|
||||||
}
|
}
|
||||||
Channel::check_remote_fee(fee_estimator, msg.feerate_per_kw).unwrap();
|
Channel::check_remote_fee(fee_estimator, msg.feerate_per_kw)?;
|
||||||
if msg.to_self_delay > MAX_LOCAL_BREAKDOWN_TIMEOUT {
|
if msg.to_self_delay > MAX_LOCAL_BREAKDOWN_TIMEOUT {
|
||||||
return Err(HandleError{err: "They wanted our payments to be delayed by a needlessly long period", msg: None});
|
return Err(HandleError{err: "They wanted our payments to be delayed by a needlessly long period", msg: None});
|
||||||
}
|
}
|
||||||
|
|
106
src/ln/msgs.rs
106
src/ln/msgs.rs
|
@ -20,6 +20,8 @@ pub enum DecodeError {
|
||||||
UnknownRealmByte,
|
UnknownRealmByte,
|
||||||
/// Failed to decode a public key (ie it's invalid)
|
/// Failed to decode a public key (ie it's invalid)
|
||||||
BadPublicKey,
|
BadPublicKey,
|
||||||
|
/// Failed to decode a signature (ie it's invalid)
|
||||||
|
BadSignature,
|
||||||
/// Buffer not of right length (either too short or too long)
|
/// Buffer not of right length (either too short or too long)
|
||||||
WrongLength,
|
WrongLength,
|
||||||
}
|
}
|
||||||
|
@ -408,6 +410,7 @@ impl Error for DecodeError {
|
||||||
match *self {
|
match *self {
|
||||||
DecodeError::UnknownRealmByte => "Unknown realm byte in Onion packet",
|
DecodeError::UnknownRealmByte => "Unknown realm byte in Onion packet",
|
||||||
DecodeError::BadPublicKey => "Invalid public key in packet",
|
DecodeError::BadPublicKey => "Invalid public key in packet",
|
||||||
|
DecodeError::BadSignature => "Invalid signature in packet",
|
||||||
DecodeError::WrongLength => "Data was wrong length for packet",
|
DecodeError::WrongLength => "Data was wrong length for packet",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -433,6 +436,15 @@ macro_rules! secp_pubkey {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
impl MsgDecodable for LocalFeatures {
|
||||||
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
|
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
|
||||||
if v.len() < 3 { return Err(DecodeError::WrongLength); }
|
if v.len() < 3 { return Err(DecodeError::WrongLength); }
|
||||||
|
@ -502,16 +514,10 @@ impl MsgEncodable for Init {
|
||||||
|
|
||||||
impl MsgDecodable for OpenChannel {
|
impl MsgDecodable for OpenChannel {
|
||||||
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
|
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);
|
return Err(DecodeError::WrongLength);
|
||||||
}
|
}
|
||||||
let ctx = Secp256k1::without_caps();
|
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;
|
let mut shutdown_scriptpubkey = None;
|
||||||
if v.len() >= 321 {
|
if v.len() >= 321 {
|
||||||
|
@ -520,6 +526,8 @@ impl MsgDecodable for OpenChannel {
|
||||||
return Err(DecodeError::WrongLength);
|
return Err(DecodeError::WrongLength);
|
||||||
}
|
}
|
||||||
shutdown_scriptpubkey = Some(Script::from(v[321..321+len].to_vec()));
|
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 {
|
||||||
|
return Err(DecodeError::WrongLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(OpenChannel {
|
Ok(OpenChannel {
|
||||||
|
@ -534,12 +542,12 @@ impl MsgDecodable for OpenChannel {
|
||||||
feerate_per_kw: byte_utils::slice_to_be32(&v[112..116]),
|
feerate_per_kw: byte_utils::slice_to_be32(&v[112..116]),
|
||||||
to_self_delay: byte_utils::slice_to_be16(&v[116..118]),
|
to_self_delay: byte_utils::slice_to_be16(&v[116..118]),
|
||||||
max_accepted_htlcs: byte_utils::slice_to_be16(&v[118..120]),
|
max_accepted_htlcs: byte_utils::slice_to_be16(&v[118..120]),
|
||||||
funding_pubkey: funding_pubkey,
|
funding_pubkey: secp_pubkey!(&ctx, &v[120..153]),
|
||||||
revocation_basepoint: revocation_basepoint,
|
revocation_basepoint: secp_pubkey!(&ctx, &v[153..186]),
|
||||||
payment_basepoint: payment_basepoint,
|
payment_basepoint: secp_pubkey!(&ctx, &v[186..219]),
|
||||||
delayed_payment_basepoint: delayed_payment_basepoint,
|
delayed_payment_basepoint: secp_pubkey!(&ctx, &v[219..252]),
|
||||||
htlc_basepoint: htlc_basepoint,
|
htlc_basepoint: secp_pubkey!(&ctx, &v[252..285]),
|
||||||
first_per_commitment_point: first_per_commitment_point,
|
first_per_commitment_point: secp_pubkey!(&ctx, &v[285..318]),
|
||||||
channel_flags: v[318],
|
channel_flags: v[318],
|
||||||
shutdown_scriptpubkey: shutdown_scriptpubkey
|
shutdown_scriptpubkey: shutdown_scriptpubkey
|
||||||
})
|
})
|
||||||
|
@ -551,10 +559,41 @@ impl MsgEncodable for OpenChannel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl MsgDecodable for AcceptChannel {
|
impl MsgDecodable for AcceptChannel {
|
||||||
fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
|
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
|
||||||
unimplemented!();
|
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 {
|
||||||
|
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 MsgEncodable for AcceptChannel {
|
||||||
|
@ -564,8 +603,17 @@ impl MsgEncodable for AcceptChannel {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MsgDecodable for FundingCreated {
|
impl MsgDecodable for FundingCreated {
|
||||||
fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
|
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
|
||||||
unimplemented!();
|
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 MsgEncodable for FundingCreated {
|
||||||
|
@ -575,8 +623,15 @@ impl MsgEncodable for FundingCreated {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MsgDecodable for FundingSigned {
|
impl MsgDecodable for FundingSigned {
|
||||||
fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
|
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
|
||||||
unimplemented!();
|
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 MsgEncodable for FundingSigned {
|
||||||
|
@ -586,8 +641,15 @@ impl MsgEncodable for FundingSigned {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MsgDecodable for FundingLocked {
|
impl MsgDecodable for FundingLocked {
|
||||||
fn decode(_v: &[u8]) -> Result<Self, DecodeError> {
|
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
|
||||||
unimplemented!();
|
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 MsgEncodable for FundingLocked {
|
||||||
|
|
Loading…
Add table
Reference in a new issue