2
2
//! spendable on-chain outputs which the user owns and is responsible for using just as any other
3
3
//! on-chain output which is theirs.
4
4
5
- use bitcoin:: blockdata:: transaction:: { Transaction , OutPoint , TxOut } ;
5
+ use bitcoin:: blockdata:: transaction:: { Transaction , OutPoint , TxOut , SigHashType } ;
6
6
use bitcoin:: blockdata:: script:: { Script , Builder } ;
7
7
use bitcoin:: blockdata:: opcodes;
8
8
use bitcoin:: network:: constants:: Network ;
@@ -25,6 +25,7 @@ use util::ser::{Writeable, Writer, Readable};
25
25
26
26
use ln:: chan_utils;
27
27
use ln:: chan_utils:: { TxCreationKeys , HTLCOutputInCommitment , make_funding_redeemscript, ChannelPublicKeys , LocalCommitmentTransaction } ;
28
+ use ln:: channelmanager:: PaymentPreimage ;
28
29
use ln:: msgs;
29
30
30
31
use std:: sync:: Arc ;
@@ -222,6 +223,10 @@ pub trait ChannelKeys : Send+Clone {
222
223
/// making the callee generate it via some util function we expose)!
223
224
fn sign_local_commitment < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , local_commitment_tx : & mut LocalCommitmentTransaction , funding_redeemscript : & Script , channel_value_satoshis : u64 , secp_ctx : & Secp256k1 < T > ) ;
224
225
226
+ /// Signs a transaction created by build_htlc_transaction. If the transaction is an
227
+ /// HTLC-Success transaction (ie htlc.offered is false), preimage must be set!
228
+ fn sign_htlc_transaction < T : secp256k1:: Signing > ( & self , htlc_tx : & mut Transaction , their_sig : & Signature , preimage : & Option < PaymentPreimage > , htlc : & HTLCOutputInCommitment , a_htlc_key : & PublicKey , b_htlc_key : & PublicKey , revocation_key : & PublicKey , per_commitment_point : & PublicKey , secp_ctx : & Secp256k1 < T > ) ;
229
+
225
230
/// Create a signature for a (proposed) closing transaction.
226
231
///
227
232
/// Note that, due to rounding, there may be one "missing" satoshi, and either party may have
@@ -353,6 +358,40 @@ impl ChannelKeys for InMemoryChannelKeys {
353
358
local_commitment_tx. add_local_sig ( & self . funding_key , funding_redeemscript, channel_value_satoshis, secp_ctx) ;
354
359
}
355
360
361
+ fn sign_htlc_transaction < T : secp256k1:: Signing > ( & self , htlc_tx : & mut Transaction , their_sig : & Signature , preimage : & Option < PaymentPreimage > , htlc : & HTLCOutputInCommitment , a_htlc_key : & PublicKey , b_htlc_key : & PublicKey , revocation_key : & PublicKey , per_commitment_point : & PublicKey , secp_ctx : & Secp256k1 < T > ) {
362
+ if htlc_tx. input . len ( ) != 1 { return ; }
363
+ if htlc_tx. input [ 0 ] . witness . len ( ) != 0 { return ; }
364
+
365
+ let htlc_redeemscript = chan_utils:: get_htlc_redeemscript_with_explicit_keys ( & htlc, a_htlc_key, b_htlc_key, revocation_key) ;
366
+
367
+ if let Ok ( our_htlc_key) = chan_utils:: derive_private_key ( secp_ctx, per_commitment_point, & self . htlc_base_key ) {
368
+ let sighash = hash_to_message ! ( & bip143:: SighashComponents :: new( & htlc_tx) . sighash_all( & htlc_tx. input[ 0 ] , & htlc_redeemscript, htlc. amount_msat / 1000 ) [ ..] ) ;
369
+ let local_tx = PublicKey :: from_secret_key ( & secp_ctx, & our_htlc_key) == * a_htlc_key;
370
+ let our_sig = secp_ctx. sign ( & sighash, & our_htlc_key) ;
371
+
372
+ htlc_tx. input [ 0 ] . witness . push ( Vec :: new ( ) ) ; // First is the multisig dummy
373
+
374
+ if local_tx { // b, then a
375
+ htlc_tx. input [ 0 ] . witness . push ( their_sig. serialize_der ( ) . to_vec ( ) ) ;
376
+ htlc_tx. input [ 0 ] . witness . push ( our_sig. serialize_der ( ) . to_vec ( ) ) ;
377
+ } else {
378
+ htlc_tx. input [ 0 ] . witness . push ( our_sig. serialize_der ( ) . to_vec ( ) ) ;
379
+ htlc_tx. input [ 0 ] . witness . push ( their_sig. serialize_der ( ) . to_vec ( ) ) ;
380
+ }
381
+ htlc_tx. input [ 0 ] . witness [ 1 ] . push ( SigHashType :: All as u8 ) ;
382
+ htlc_tx. input [ 0 ] . witness [ 2 ] . push ( SigHashType :: All as u8 ) ;
383
+
384
+ if htlc. offered {
385
+ htlc_tx. input [ 0 ] . witness . push ( Vec :: new ( ) ) ;
386
+ assert ! ( preimage. is_none( ) ) ;
387
+ } else {
388
+ htlc_tx. input [ 0 ] . witness . push ( preimage. unwrap ( ) . 0 . to_vec ( ) ) ;
389
+ }
390
+
391
+ htlc_tx. input [ 0 ] . witness . push ( htlc_redeemscript. as_bytes ( ) . to_vec ( ) ) ;
392
+ } else { return ; }
393
+ }
394
+
356
395
fn sign_closing_transaction < T : secp256k1:: Signing > ( & self , closing_tx : & Transaction , secp_ctx : & Secp256k1 < T > ) -> Result < Signature , ( ) > {
357
396
if closing_tx. input . len ( ) != 1 { return Err ( ( ) ) ; }
358
397
if closing_tx. input [ 0 ] . witness . len ( ) != 0 { return Err ( ( ) ) ; }
0 commit comments