X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fchan_utils.rs;h=4438579657eecc74e6cf1cb9458b339c2db82292;hb=71d640a64be1232ac464e6be645b7907e688c06b;hp=8037de64eb562f4ef854b511a382625b77f0e035;hpb=151d4ac0a3a6aa94d3f93995b71c874b7ea95967;p=rust-lightning diff --git a/lightning/src/ln/chan_utils.rs b/lightning/src/ln/chan_utils.rs index 8037de64..44385796 100644 --- a/lightning/src/ln/chan_utils.rs +++ b/lightning/src/ln/chan_utils.rs @@ -8,8 +8,7 @@ // licenses. //! Various utilities for building scripts and deriving keys related to channels. These are -//! largely of interest for those implementing chain::keysinterface::ChannelKeys message signing -//! by hand. +//! largely of interest for those implementing chain::keysinterface::Sign message signing by hand. use bitcoin::blockdata::script::{Script,Builder}; use bitcoin::blockdata::opcodes; @@ -21,7 +20,7 @@ use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::hashes::ripemd160::Hash as Ripemd160; use bitcoin::hash_types::{Txid, PubkeyHash}; -use ln::channelmanager::{PaymentHash, PaymentPreimage}; +use ln::{PaymentHash, PaymentPreimage}; use ln::msgs::DecodeError; use util::ser::{Readable, Writeable, Writer, MAX_BUF_SIZE}; use util::byte_utils; @@ -40,6 +39,7 @@ use std::io::Read; use std::ops::Deref; use chain; +// Maximum size of a serialized HTLCOutputInCommitment const HTLC_OUTPUT_IN_COMMITMENT_SIZE: usize = 1 + 8 + 4 + 32 + 5; pub(crate) const MAX_HTLCS: u16 = 483; @@ -321,7 +321,8 @@ pub struct TxCreationKeys { /// Broadcaster's Payment Key (which isn't allowed to be spent from for some delay) pub broadcaster_delayed_payment_key: PublicKey, } -impl_writeable!(TxCreationKeys, 33*6, + +impl_writeable!(TxCreationKeys, 33*5, { per_commitment_point, revocation_key, broadcaster_htlc_key, countersignatory_htlc_key, broadcaster_delayed_payment_key }); /// One counterparty's public keys which do not change over the life of a channel. @@ -384,11 +385,16 @@ impl TxCreationKeys { } } +/// The maximum length of a script returned by get_revokeable_redeemscript. +// Calculated as 6 bytes of opcodes, 1 byte push plus 2 bytes for contest_delay, and two public +// keys of 33 bytes (+ 1 push). +pub const REVOKEABLE_REDEEMSCRIPT_MAX_LENGTH: usize = 6 + 3 + 34*2; + /// A script either spendable by the revocation /// key or the broadcaster_delayed_payment_key and satisfying the relative-locktime OP_CSV constrain. /// Encumbering a `to_holder` output on a commitment transaction or 2nd-stage HTLC transactions. pub fn get_revokeable_redeemscript(revocation_key: &PublicKey, contest_delay: u16, broadcaster_delayed_payment_key: &PublicKey) -> Script { - Builder::new().push_opcode(opcodes::all::OP_IF) + let res = Builder::new().push_opcode(opcodes::all::OP_IF) .push_slice(&revocation_key.serialize()) .push_opcode(opcodes::all::OP_ELSE) .push_int(contest_delay as i64) @@ -397,7 +403,9 @@ pub fn get_revokeable_redeemscript(revocation_key: &PublicKey, contest_delay: u1 .push_slice(&broadcaster_delayed_payment_key.serialize()) .push_opcode(opcodes::all::OP_ENDIF) .push_opcode(opcodes::all::OP_CHECKSIG) - .into_script() + .into_script(); + debug_assert!(res.len() <= REVOKEABLE_REDEEMSCRIPT_MAX_LENGTH); + res } #[derive(Clone, PartialEq)] @@ -421,7 +429,10 @@ pub struct HTLCOutputInCommitment { pub transaction_output_index: Option, } -impl_writeable!(HTLCOutputInCommitment, HTLC_OUTPUT_IN_COMMITMENT_SIZE, { +impl_writeable_len_match!(HTLCOutputInCommitment, { + { HTLCOutputInCommitment { transaction_output_index: None, .. }, HTLC_OUTPUT_IN_COMMITMENT_SIZE - 4 }, + { _, HTLC_OUTPUT_IN_COMMITMENT_SIZE } + }, { offered, amount_msat, cltv_expiry, @@ -843,6 +854,7 @@ impl PartialEq for CommitmentTransaction { } } +/// (C-not exported) as users never need to call this directly impl Writeable for Vec { #[inline] fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { @@ -854,6 +866,7 @@ impl Writeable for Vec { } } +/// (C-not exported) as users never need to call this directly impl Readable for Vec { #[inline] fn read(r: &mut R) -> Result { @@ -889,6 +902,8 @@ impl CommitmentTransaction { /// This auxiliary data is not stored in this object. /// /// Only include HTLCs that are above the dust limit for the channel. + /// + /// (C-not exported) due to the generic though we likely should expose a version without pub fn new_with_auxiliary_htlc_data(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, keys: TxCreationKeys, feerate_per_kw: u32, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters) -> CommitmentTransaction { // Sort outputs and populate output indices while keeping track of the auxiliary data let (outputs, htlcs) = Self::internal_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters).unwrap(); @@ -1056,6 +1071,9 @@ impl CommitmentTransaction { /// The non-dust HTLCs (direction, amt, height expiration, hash, transaction output index) /// which were included in this commitment transaction in output order. /// The transaction index is always populated. + /// + /// (C-not exported) as we cannot currently convert Vec references to/from C, though we should + /// expose a less effecient version which creates a Vec of references in the future. pub fn htlcs(&self) -> &Vec { &self.htlcs }