1
1
use bitcoin:: blockdata:: script:: { Script , Builder } ;
2
2
use bitcoin:: blockdata:: opcodes;
3
- use bitcoin:: util:: hash:: Hash160 ;
3
+ use bitcoin:: blockdata:: transaction:: { TxIn , TxOut , Transaction } ;
4
+ use bitcoin:: util:: hash:: { Hash160 , Sha256dHash } ;
4
5
5
6
use secp256k1:: key:: { PublicKey , SecretKey } ;
6
7
use secp256k1:: Secp256k1 ;
@@ -11,6 +12,9 @@ use crypto::ripemd160::Ripemd160;
11
12
12
13
use util:: sha2:: Sha256 ;
13
14
15
+ pub const HTLC_SUCCESS_TX_WEIGHT : u64 = 703 ;
16
+ pub const HTLC_TIMEOUT_TX_WEIGHT : u64 = 663 ;
17
+
14
18
// Various functions for key derivation and transaction creation for use within channels. Primarily
15
19
// used in Channel and ChannelMonitor.
16
20
@@ -233,3 +237,33 @@ pub fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommitment, a
233
237
pub fn get_htlc_redeemscript ( htlc : & HTLCOutputInCommitment , keys : & TxCreationKeys ) -> Script {
234
238
get_htlc_redeemscript_with_explicit_keys ( htlc, & keys. a_htlc_key , & keys. b_htlc_key , & keys. revocation_key )
235
239
}
240
+
241
+ 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 {
242
+ let mut txins: Vec < TxIn > = Vec :: new ( ) ;
243
+ txins. push ( TxIn {
244
+ prev_hash : prev_hash. clone ( ) ,
245
+ prev_index : htlc. transaction_output_index ,
246
+ script_sig : Script :: new ( ) ,
247
+ sequence : 0 ,
248
+ witness : Vec :: new ( ) ,
249
+ } ) ;
250
+
251
+ let total_fee = if htlc. offered {
252
+ feerate_per_kw * HTLC_TIMEOUT_TX_WEIGHT / 1000
253
+ } else {
254
+ feerate_per_kw * HTLC_SUCCESS_TX_WEIGHT / 1000
255
+ } ;
256
+
257
+ let mut txouts: Vec < TxOut > = Vec :: new ( ) ;
258
+ txouts. push ( TxOut {
259
+ script_pubkey : get_revokeable_redeemscript ( revocation_key, to_self_delay, a_delayed_payment_key) . to_v0_p2wsh ( ) ,
260
+ 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)
261
+ } ) ;
262
+
263
+ Transaction {
264
+ version : 2 ,
265
+ lock_time : if htlc. offered { htlc. cltv_expiry } else { 0 } ,
266
+ input : txins,
267
+ output : txouts,
268
+ }
269
+ }
0 commit comments