use secp256k1::key::PublicKey;
use secp256k1::{Secp256k1, Signature};
-use bitcoin::util::uint::Uint256;
use bitcoin::util::hash::Sha256dHash;
use bitcoin::network::serialize::{deserialize,serialize};
use bitcoin::blockdata::script::Script;
pub struct OpenChannel {
pub chain_hash: Sha256dHash,
- pub temporary_channel_id: Uint256,
+ pub temporary_channel_id: [u8; 32],
pub funding_satoshis: u64,
pub push_msat: u64,
pub dust_limit_satoshis: u64,
}
pub struct AcceptChannel {
- pub temporary_channel_id: Uint256,
+ pub temporary_channel_id: [u8; 32],
pub dust_limit_satoshis: u64,
pub max_htlc_value_in_flight_msat: u64,
pub channel_reserve_satoshis: u64,
}
pub struct FundingCreated {
- pub temporary_channel_id: Uint256,
+ pub temporary_channel_id: [u8; 32],
pub funding_txid: Sha256dHash,
pub funding_output_index: u16,
pub signature: Signature,
}
pub struct FundingSigned {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub signature: Signature,
}
pub struct FundingLocked {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub next_per_commitment_point: PublicKey,
}
pub struct Shutdown {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub scriptpubkey: Script,
}
pub struct ClosingSigned {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub fee_satoshis: u64,
pub signature: Signature,
}
#[derive(Clone)]
pub struct UpdateAddHTLC {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub htlc_id: u64,
pub amount_msat: u64,
pub payment_hash: [u8; 32],
#[derive(Clone)]
pub struct UpdateFulfillHTLC {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub htlc_id: u64,
pub payment_preimage: [u8; 32],
}
#[derive(Clone)]
pub struct UpdateFailHTLC {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub htlc_id: u64,
pub reason: OnionErrorPacket,
}
#[derive(Clone)]
pub struct UpdateFailMalformedHTLC {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub htlc_id: u64,
pub sha256_of_onion: [u8; 32],
pub failure_code: u16,
#[derive(Clone)]
pub struct CommitmentSigned {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub signature: Signature,
pub htlc_signatures: Vec<Signature>,
}
pub struct RevokeAndACK {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub per_commitment_secret: [u8; 32],
pub next_per_commitment_point: PublicKey,
}
pub struct UpdateFee {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub feerate_per_kw: u32,
}
pub struct ChannelReestablish {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub next_local_commitment_number: u64,
pub next_remote_commitment_number: u64,
pub your_last_per_commitment_secret: Option<[u8; 32]>,
#[derive(Clone)]
pub struct AnnouncementSignatures {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub short_channel_id: u64,
pub node_signature: Signature,
pub bitcoin_signature: Signature,
return Err(DecodeError::WrongLength);
}
+ let mut temporary_channel_id = [0; 32];
+ temporary_channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- temporary_channel_id: deserialize(&v[0..32]).unwrap(),
+ temporary_channel_id,
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]),
&Some(ref script) => Vec::with_capacity(270 + 2 + script.len()),
&None => Vec::with_capacity(270),
};
- res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap()[..]);
+ res.extend_from_slice(&self.temporary_channel_id);
res.extend_from_slice(&byte_utils::be64_to_array(self.dust_limit_satoshis));
res.extend_from_slice(&byte_utils::be64_to_array(self.max_htlc_value_in_flight_msat));
res.extend_from_slice(&byte_utils::be64_to_array(self.channel_reserve_satoshis));
return Err(DecodeError::WrongLength);
}
let ctx = Secp256k1::without_caps();
+ let mut temporary_channel_id = [0; 32];
+ temporary_channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- temporary_channel_id: deserialize(&v[0..32]).unwrap(),
+ temporary_channel_id,
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 {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+32+2+64);
- res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap()[..]);
+ res.extend_from_slice(&self.temporary_channel_id);
res.extend_from_slice(&serialize(&self.funding_txid).unwrap()[..]);
res.extend_from_slice(&byte_utils::be16_to_array(self.funding_output_index));
let secp_ctx = Secp256k1::without_caps();
return Err(DecodeError::WrongLength);
}
let ctx = Secp256k1::without_caps();
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
signature: secp_signature!(&ctx, &v[32..96]),
})
}
impl MsgEncodable for FundingSigned {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+64);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&self.signature.serialize_compact(&Secp256k1::without_caps()));
res
}
return Err(DecodeError::WrongLength);
}
let ctx = Secp256k1::without_caps();
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
next_per_commitment_point: secp_pubkey!(&ctx, &v[32..65]),
})
}
impl MsgEncodable for FundingLocked {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+33);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&self.next_per_commitment_point.serialize());
res
}
if v.len() < 32 + 2 + scriptlen {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
scriptpubkey: Script::from(v[34..34 + scriptlen].to_vec()),
})
}
impl MsgEncodable for Shutdown {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32 + 2 + self.scriptpubkey.len());
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be16_to_array(self.scriptpubkey.len() as u16));
res.extend_from_slice(&self.scriptpubkey[..]);
res
return Err(DecodeError::WrongLength);
}
let secp_ctx = Secp256k1::without_caps();
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
fee_satoshis: byte_utils::slice_to_be64(&v[32..40]),
signature: secp_signature!(&secp_ctx, &v[40..104]),
})
impl MsgEncodable for ClosingSigned {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+8+64);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
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));
if v.len() < 32+8+8+32+4+1+33+20*65+32 {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
let mut payment_hash = [0; 32];
payment_hash.copy_from_slice(&v[48..80]);
Ok(Self{
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
htlc_id: byte_utils::slice_to_be64(&v[32..40]),
amount_msat: byte_utils::slice_to_be64(&v[40..48]),
payment_hash,
impl MsgEncodable for UpdateAddHTLC {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+8+8+32+4+1+1366);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
res.extend_from_slice(&byte_utils::be64_to_array(self.amount_msat));
res.extend_from_slice(&self.payment_hash);
if v.len() < 32+8+32 {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
let mut payment_preimage = [0; 32];
payment_preimage.copy_from_slice(&v[40..72]);
Ok(Self{
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
htlc_id: byte_utils::slice_to_be64(&v[32..40]),
payment_preimage,
})
impl MsgEncodable for UpdateFulfillHTLC {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+8+32);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
res.extend_from_slice(&self.payment_preimage);
res
if v.len() < 32+8 {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self{
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
htlc_id: byte_utils::slice_to_be64(&v[32..40]),
reason: OnionErrorPacket::decode(&v[40..])?,
})
fn encode(&self) -> Vec<u8> {
let reason = self.reason.encode();
let mut res = Vec::with_capacity(32+8+reason.len());
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
res.extend_from_slice(&reason[..]);
res
if v.len() < 32+8+32+2 {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
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(),
+ channel_id,
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 {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+8+32+2);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
res.extend_from_slice(&self.sha256_of_onion);
res.extend_from_slice(&byte_utils::be16_to_array(self.failure_code));
if v.len() < 32+64+2 {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
+
let htlcs = byte_utils::slice_to_be16(&v[96..98]) as usize;
if v.len() < 32+64+2+htlcs*64 {
return Err(DecodeError::WrongLength);
htlc_signatures.push(secp_signature!(&secp_ctx, &v[98+i*64..98+(i+1)*64]));
}
Ok(Self {
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
signature: secp_signature!(&secp_ctx, &v[32..96]),
htlc_signatures,
})
impl MsgEncodable for CommitmentSigned {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+64+2+self.htlc_signatures.len()*64);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
let secp_ctx = Secp256k1::without_caps();
res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx));
res.extend_from_slice(&byte_utils::be16_to_array(self.htlc_signatures.len() as u16));
if v.len() < 32+32+33 {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
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(),
+ channel_id,
per_commitment_secret,
next_per_commitment_point: secp_pubkey!(&secp_ctx, &v[64..97]),
})
impl MsgEncodable for RevokeAndACK {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+32+33);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&self.per_commitment_secret);
res.extend_from_slice(&self.next_per_commitment_point.serialize());
res
if v.len() < 32+4 {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
feerate_per_kw: byte_utils::slice_to_be32(&v[32..36]),
})
}
impl MsgEncodable for UpdateFee {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+4);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be32_to_array(self.feerate_per_kw));
res
}
return Err(DecodeError::WrongLength);
}
let secp_ctx = Secp256k1::without_caps();
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
short_channel_id: byte_utils::slice_to_be64(&v[32..40]),
node_signature: secp_signature!(&secp_ctx, &v[40..104]),
bitcoin_signature: secp_signature!(&secp_ctx, &v[104..168]),
impl MsgEncodable for AnnouncementSignatures {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+8+64*2);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id));
let secp_ctx = Secp256k1::without_caps();
res.extend_from_slice(&self.node_signature.serialize_compact(&secp_ctx));