X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fln%2Fchan_utils.rs;h=0cace11c1cb0dd601474229d738a23526ed6029d;hb=6ab31a0d501db9fd96436deff190da8801ac73b7;hp=81fa29f554113a5cb156205ab5be351ceb3290fb;hpb=4d75d4c099cd6875f7ba95050012de340367278a;p=rust-lightning diff --git a/src/ln/chan_utils.rs b/src/ln/chan_utils.rs index 81fa29f5..0cace11c 100644 --- a/src/ln/chan_utils.rs +++ b/src/ln/chan_utils.rs @@ -1,6 +1,7 @@ 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; @@ -11,6 +12,9 @@ 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. @@ -136,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, @@ -233,3 +237,33 @@ pub fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommitment, a 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, + } +}