WIP
[rust-lightning] / lightning / src / ln / chan_utils.rs
index 3ab440d95f254d18da9ed677e6435ed864d26aa2..2d0af708f2c5e801759d391bec6f3d15e68afa56 100644 (file)
@@ -11,8 +11,7 @@ use bitcoin::util::bip143;
 use bitcoin::hashes::{Hash, HashEngine};
 use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::hashes::ripemd160::Hash as Ripemd160;
-use bitcoin::hashes::hash160::Hash as Hash160;
-use bitcoin::hashes::sha256d::Hash as Sha256dHash;
+use bitcoin::hash_types::{Txid, PubkeyHash};
 
 use ln::channelmanager::{PaymentHash, PaymentPreimage};
 use ln::msgs::DecodeError;
@@ -27,8 +26,8 @@ use std::{cmp, mem};
 
 const MAX_ALLOC_SIZE: usize = 64*1024;
 
-pub(super) const HTLC_SUCCESS_TX_WEIGHT: u64 = 703;
-pub(super) const HTLC_TIMEOUT_TX_WEIGHT: u64 = 663;
+pub(super) const HTLC_SUCCESS_TX_WEIGHT: u64 = 703; // XXX
+pub(super) const HTLC_TIMEOUT_TX_WEIGHT: u64 = 663; // XXX
 
 #[derive(PartialEq)]
 pub(crate) enum HTLCType {
@@ -39,9 +38,9 @@ pub(crate) enum HTLCType {
 impl HTLCType {
        /// Check if a given tx witnessScript len matchs one of a pre-signed HTLC
        pub(crate) fn scriptlen_to_htlctype(witness_script_len: usize) ->  Option<HTLCType> {
-               if witness_script_len == 133 {
+               if witness_script_len == 136 {
                        Some(HTLCType::OfferedHTLC)
-               } else if witness_script_len >= 136 && witness_script_len <= 139 {
+               } else if witness_script_len >= 139 && witness_script_len <= 142 {
                        Some(HTLCType::AcceptedHTLC)
                } else {
                        None
@@ -263,11 +262,9 @@ pub struct TxCreationKeys {
        pub(crate) b_htlc_key: PublicKey,
        /// A's Payment Key (which isn't allowed to be spent from for some delay)
        pub(crate) a_delayed_payment_key: PublicKey,
-       /// B's Payment Key
-       pub(crate) b_payment_key: PublicKey,
 }
 impl_writeable!(TxCreationKeys, 33*6,
-       { per_commitment_point, revocation_key, a_htlc_key, b_htlc_key, a_delayed_payment_key, b_payment_key });
+       { per_commitment_point, revocation_key, a_htlc_key, b_htlc_key, a_delayed_payment_key });
 
 /// One counterparty's public keys which do not change over the life of a channel.
 #[derive(Clone, PartialEq)]
@@ -280,9 +277,10 @@ pub struct ChannelPublicKeys {
        /// a commitment transaction so that their counterparty can claim all available funds if they
        /// broadcast an old state.
        pub revocation_basepoint: PublicKey,
-       /// The base point which is used (with derive_public_key) to derive a per-commitment payment
-       /// public key which receives immediately-spendable non-HTLC-encumbered funds.
-       pub payment_basepoint: PublicKey,
+       /// The public key which receives our immediately spendable primary channel balance in
+       /// remote-broadcasted commitment transactions. This key is static across every commitment
+       /// transaction.
+       pub payment_point: PublicKey,
        /// The base point which is used (with derive_public_key) to derive a per-commitment payment
        /// public key which receives non-HTLC-encumbered funds which are only available for spending
        /// after some delay (or can be claimed via the revocation path).
@@ -295,21 +293,20 @@ pub struct ChannelPublicKeys {
 impl_writeable!(ChannelPublicKeys, 33*5, {
        funding_pubkey,
        revocation_basepoint,
-       payment_basepoint,
+       payment_point,
        delayed_payment_basepoint,
        htlc_basepoint
 });
 
 
 impl TxCreationKeys {
-       pub(crate) fn new<T: secp256k1::Signing + secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, a_delayed_payment_base: &PublicKey, a_htlc_base: &PublicKey, b_revocation_base: &PublicKey, b_payment_base: &PublicKey, b_htlc_base: &PublicKey) -> Result<TxCreationKeys, secp256k1::Error> {
+       pub(crate) fn new<T: secp256k1::Signing + secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, a_delayed_payment_base: &PublicKey, a_htlc_base: &PublicKey, b_revocation_base: &PublicKey, b_htlc_base: &PublicKey) -> Result<TxCreationKeys, secp256k1::Error> {
                Ok(TxCreationKeys {
                        per_commitment_point: per_commitment_point.clone(),
                        revocation_key: derive_public_revocation_key(&secp_ctx, &per_commitment_point, &b_revocation_base)?,
                        a_htlc_key: derive_public_key(&secp_ctx, &per_commitment_point, &a_htlc_base)?,
                        b_htlc_key: derive_public_key(&secp_ctx, &per_commitment_point, &b_htlc_base)?,
                        a_delayed_payment_key: derive_public_key(&secp_ctx, &per_commitment_point, &a_delayed_payment_base)?,
-                       b_payment_key: derive_public_key(&secp_ctx, &per_commitment_point, &b_payment_base)?,
                })
        }
 }
@@ -364,7 +361,7 @@ pub(crate) fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommit
        if htlc.offered {
                Builder::new().push_opcode(opcodes::all::OP_DUP)
                              .push_opcode(opcodes::all::OP_HASH160)
-                             .push_slice(&Hash160::hash(&revocation_key.serialize())[..])
+                             .push_slice(&PubkeyHash::hash(&revocation_key.serialize())[..])
                              .push_opcode(opcodes::all::OP_EQUAL)
                              .push_opcode(opcodes::all::OP_IF)
                              .push_opcode(opcodes::all::OP_CHECKSIG)
@@ -388,11 +385,14 @@ pub(crate) fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommit
                              .push_opcode(opcodes::all::OP_CHECKSIG)
                              .push_opcode(opcodes::all::OP_ENDIF)
                              .push_opcode(opcodes::all::OP_ENDIF)
+                             .push_int(1)
+                             .push_opcode(opcodes::all::OP_CSV)
+                             .push_opcode(opcodes::all::OP_DROP)
                              .into_script()
        } else {
                Builder::new().push_opcode(opcodes::all::OP_DUP)
                              .push_opcode(opcodes::all::OP_HASH160)
-                             .push_slice(&Hash160::hash(&revocation_key.serialize())[..])
+                             .push_slice(&PubkeyHash::hash(&revocation_key.serialize())[..])
                              .push_opcode(opcodes::all::OP_EQUAL)
                              .push_opcode(opcodes::all::OP_IF)
                              .push_opcode(opcodes::all::OP_CHECKSIG)
@@ -419,6 +419,9 @@ pub(crate) fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommit
                              .push_opcode(opcodes::all::OP_CHECKSIG)
                              .push_opcode(opcodes::all::OP_ENDIF)
                              .push_opcode(opcodes::all::OP_ENDIF)
+                             .push_int(1)
+                             .push_opcode(opcodes::all::OP_CSV)
+                             .push_opcode(opcodes::all::OP_DROP)
                              .into_script()
        }
 }
@@ -447,7 +450,7 @@ pub fn make_funding_redeemscript(a: &PublicKey, b: &PublicKey) -> Script {
 }
 
 /// panics if htlc.transaction_output_index.is_none()!
-pub fn build_htlc_transaction(prev_hash: &Sha256dHash, feerate_per_kw: u64, to_self_delay: u16, htlc: &HTLCOutputInCommitment, a_delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
+pub fn build_htlc_transaction(prev_hash: &Txid, feerate_per_kw: u64, to_self_delay: u16, htlc: &HTLCOutputInCommitment, a_delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
        let mut txins: Vec<TxIn> = Vec::new();
        txins.push(TxIn {
                previous_output: OutPoint {
@@ -455,7 +458,7 @@ pub fn build_htlc_transaction(prev_hash: &Sha256dHash, feerate_per_kw: u64, to_s
                        vout: htlc.transaction_output_index.expect("Can't build an HTLC transaction for a dust output"),
                },
                script_sig: Script::new(),
-               sequence: 0,
+               sequence: 1,
                witness: Vec::new(),
        });
 
@@ -538,7 +541,6 @@ impl LocalCommitmentTransaction {
                                        a_htlc_key: dummy_key.clone(),
                                        b_htlc_key: dummy_key.clone(),
                                        a_delayed_payment_key: dummy_key.clone(),
-                                       b_payment_key: dummy_key.clone(),
                                },
                        feerate_per_kw: 0,
                        per_htlc: Vec::new()
@@ -563,7 +565,7 @@ impl LocalCommitmentTransaction {
 
        /// Get the txid of the local commitment transaction contained in this
        /// LocalCommitmentTransaction
-       pub fn txid(&self) -> Sha256dHash {
+       pub fn txid(&self) -> Txid {
                self.unsigned_tx.txid()
        }