X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Fchan_utils.rs;h=0cace11c1cb0dd601474229d738a23526ed6029d;hb=73932d75dcf890f32a7992494a156b2f8e7def44;hp=dd5515c88046ac18b0686e7a810d5cfdeed65c3e;hpb=b94365f3f560b095350bd36d4eb9bf63268679b0;p=rust-lightning diff --git a/src/ln/chan_utils.rs b/src/ln/chan_utils.rs index dd5515c8..0cace11c 100644 --- a/src/ln/chan_utils.rs +++ b/src/ln/chan_utils.rs @@ -1,15 +1,20 @@ use bitcoin::blockdata::script::{Script,Builder}; use bitcoin::blockdata::opcodes; -use bitcoin::util::hash::Hash160; +use bitcoin::blockdata::transaction::{TxIn,TxOut,Transaction}; +use bitcoin::util::hash::{Hash160,Sha256dHash}; use secp256k1::key::{PublicKey,SecretKey}; use secp256k1::Secp256k1; use secp256k1; use crypto::digest::Digest; -use crypto::sha2::Sha256; use crypto::ripemd160::Ripemd160; +use util::sha2::Sha256; + +pub const HTLC_SUCCESS_TX_WEIGHT: u64 = 703; +pub const HTLC_TIMEOUT_TX_WEIGHT: u64 = 663; + // Various functions for key derivation and transaction creation for use within channels. Primarily // used in Channel and ChannelMonitor. @@ -135,18 +140,18 @@ impl TxCreationKeys { /// the revocation key pub fn get_revokeable_redeemscript(revocation_key: &PublicKey, to_self_delay: u16, delayed_payment_key: &PublicKey) -> Script { Builder::new().push_opcode(opcodes::All::OP_IF) - .push_slice(&revocation_key.serialize()) - .push_opcode(opcodes::All::OP_ELSE) - .push_int(to_self_delay as i64) - .push_opcode(opcodes::OP_CSV) - .push_opcode(opcodes::All::OP_DROP) - .push_slice(&delayed_payment_key.serialize()) - .push_opcode(opcodes::All::OP_ENDIF) - .push_opcode(opcodes::All::OP_CHECKSIG) - .into_script() + .push_slice(&revocation_key.serialize()) + .push_opcode(opcodes::All::OP_ELSE) + .push_int(to_self_delay as i64) + .push_opcode(opcodes::OP_CSV) + .push_opcode(opcodes::All::OP_DROP) + .push_slice(&delayed_payment_key.serialize()) + .push_opcode(opcodes::All::OP_ENDIF) + .push_opcode(opcodes::All::OP_CHECKSIG) + .into_script() } -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub struct HTLCOutputInCommitment { pub offered: bool, pub amount_msat: u64, @@ -156,7 +161,7 @@ pub struct HTLCOutputInCommitment { } #[inline] -pub fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommitment, a_htlc_key: &PublicKey, b_htlc_key: &PublicKey, revocation_key: &PublicKey, offered: bool) -> Script { +pub fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommitment, a_htlc_key: &PublicKey, b_htlc_key: &PublicKey, revocation_key: &PublicKey) -> Script { let payment_hash160 = { let mut ripemd = Ripemd160::new(); ripemd.input(&htlc.payment_hash); @@ -164,7 +169,7 @@ pub fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommitment, a ripemd.result(&mut res); res }; - if offered { + if htlc.offered { Builder::new().push_opcode(opcodes::All::OP_DUP) .push_opcode(opcodes::All::OP_HASH160) .push_slice(&Hash160::from_data(&revocation_key.serialize())[..]) @@ -229,6 +234,36 @@ pub fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommitment, a /// note here that 'a_revocation_key' is generated using b_revocation_basepoint and a's /// commitment secret. 'htlc' does *not* need to have its previous_output_index filled. #[inline] -pub fn get_htlc_redeemscript(htlc: &HTLCOutputInCommitment, keys: &TxCreationKeys, offered: bool) -> Script { - get_htlc_redeemscript_with_explicit_keys(htlc, &keys.a_htlc_key, &keys.b_htlc_key, &keys.revocation_key, offered) +pub fn get_htlc_redeemscript(htlc: &HTLCOutputInCommitment, keys: &TxCreationKeys) -> Script { + get_htlc_redeemscript_with_explicit_keys(htlc, &keys.a_htlc_key, &keys.b_htlc_key, &keys.revocation_key) +} + +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 { + let mut txins: Vec = Vec::new(); + txins.push(TxIn { + prev_hash: prev_hash.clone(), + prev_index: htlc.transaction_output_index, + script_sig: Script::new(), + sequence: 0, + witness: Vec::new(), + }); + + let total_fee = if htlc.offered { + feerate_per_kw * HTLC_TIMEOUT_TX_WEIGHT / 1000 + } else { + feerate_per_kw * HTLC_SUCCESS_TX_WEIGHT / 1000 + }; + + let mut txouts: Vec = Vec::new(); + txouts.push(TxOut { + script_pubkey: get_revokeable_redeemscript(revocation_key, to_self_delay, a_delayed_payment_key).to_v0_p2wsh(), + value: htlc.amount_msat / 1000 - total_fee //TODO: BOLT 3 does not specify if we should add amount_msat before dividing or if we should divide by 1000 before subtracting (as we do here) + }); + + Transaction { + version: 2, + lock_time: if htlc.offered { htlc.cltv_expiry } else { 0 }, + input: txins, + output: txouts, + } }