@@ -3226,6 +3226,8 @@ mod tests {
3226
3226
3227
3227
use bitcoin:: util:: hash:: Sha256dHash ;
3228
3228
use bitcoin:: util:: bip143;
3229
+ use bitcoin:: util:: address:: Address ;
3230
+ use bitcoin:: util:: bip32:: { ChildNumber , ExtendedPubKey , ExtendedPrivKey } ;
3229
3231
use bitcoin:: blockdata:: block:: { Block , BlockHeader } ;
3230
3232
use bitcoin:: blockdata:: transaction:: { Transaction , TxOut , TxIn , SigHashType } ;
3231
3233
use bitcoin:: blockdata:: script:: { Builder , Script } ;
@@ -7550,4 +7552,89 @@ mod tests {
7550
7552
} ;
7551
7553
check_spends ! ( spend_tx, node_txn[ 0 ] . clone( ) ) ;
7552
7554
}
7555
+
7556
+ #[ test]
7557
+ fn test_static_spendable_outputs_preimage_tx ( ) {
7558
+ let nodes = create_network ( 2 ) ;
7559
+
7560
+ // Create some initial channels
7561
+ let chan_1 = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
7562
+
7563
+ let payment_preimage = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] ) [ ..] , 3000000 ) . 0 ;
7564
+
7565
+ let commitment_tx = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan_1. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
7566
+ assert_eq ! ( commitment_tx[ 0 ] . input. len( ) , 1 ) ;
7567
+ assert_eq ! ( commitment_tx[ 0 ] . input[ 0 ] . previous_output. txid, chan_1. 3 . txid( ) ) ;
7568
+
7569
+ // Settle A's commitment tx on B's chain
7570
+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
7571
+ assert ! ( nodes[ 1 ] . node. claim_funds( payment_preimage) ) ;
7572
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
7573
+ nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ commitment_tx[ 0 ] . clone( ) ] } , 1 ) ;
7574
+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
7575
+ match events[ 0 ] {
7576
+ MessageSendEvent :: UpdateHTLCs { .. } => { } ,
7577
+ _ => panic ! ( "Unexpected event" ) ,
7578
+ }
7579
+ match events[ 1 ] {
7580
+ MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
7581
+ _ => panic ! ( "Unexepected event" ) ,
7582
+ }
7583
+
7584
+ // Check B's monitor was able to send back output descriptor event for preimage tx on A's commitment tx
7585
+ let node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ; // ChannelManager : 1 (local commitment tx), ChannelMonitor: 2 (1 preimage tx) * 2 (block-rescan)
7586
+ check_spends ! ( node_txn[ 0 ] , commitment_tx[ 0 ] . clone( ) ) ;
7587
+ assert_eq ! ( node_txn[ 0 ] , node_txn[ 2 ] ) ;
7588
+ assert_eq ! ( node_txn[ 0 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , 133 ) ;
7589
+ check_spends ! ( node_txn[ 1 ] , chan_1. 3 . clone( ) ) ;
7590
+
7591
+ let events = nodes[ 1 ] . chan_monitor . simple_monitor . get_and_clear_pending_events ( ) ;
7592
+ let spend_tx = match events[ 0 ] {
7593
+ Event :: SpendableOutputs { ref outputs } => {
7594
+ match outputs[ 0 ] {
7595
+ SpendableOutputDescriptor :: StaticOutput { ref outpoint, ref output } => {
7596
+ let secp_ctx = Secp256k1 :: new ( ) ;
7597
+ let input = TxIn {
7598
+ previous_output : outpoint. clone ( ) ,
7599
+ script_sig : Script :: new ( ) ,
7600
+ sequence : 0 ,
7601
+ witness : Vec :: new ( ) ,
7602
+ } ;
7603
+ let outp = TxOut {
7604
+ script_pubkey : Builder :: new ( ) . push_opcode ( opcodes:: All :: OP_RETURN ) . into_script ( ) ,
7605
+ value : output. value ,
7606
+ } ;
7607
+ let mut spend_tx = Transaction {
7608
+ version : 2 ,
7609
+ lock_time : 0 ,
7610
+ input : vec ! [ input] ,
7611
+ output : vec ! [ outp. clone( ) ] ,
7612
+ } ;
7613
+ let destination_secret = {
7614
+ match ExtendedPrivKey :: new_master ( & secp_ctx, Network :: Testnet , & nodes[ 1 ] . node_seed ) {
7615
+ Ok ( master_key) => {
7616
+ match master_key. ckd_priv ( & secp_ctx, ChildNumber :: from_hardened_idx ( 1 ) ) {
7617
+ Ok ( destination_key) => destination_key,
7618
+ Err ( _) => panic ! ( "Your RNG is busted" ) ,
7619
+ }
7620
+ }
7621
+ Err ( _) => panic ! ( "Your rng is busted" ) ,
7622
+ }
7623
+ } ;
7624
+ let destination_pubkey = ExtendedPubKey :: from_private ( & secp_ctx, & destination_secret) . public_key ;
7625
+ let witness_script = Address :: p2pkh ( & destination_pubkey, Network :: Testnet ) . script_pubkey ( ) ;
7626
+ let sighash = Message :: from_slice ( & bip143:: SighashComponents :: new ( & spend_tx) . sighash_all ( & spend_tx. input [ 0 ] , & witness_script, output. value ) [ ..] ) . unwrap ( ) ;
7627
+ let sig = secp_ctx. sign ( & sighash, & destination_secret. secret_key ) ;
7628
+ spend_tx. input [ 0 ] . witness . push ( sig. serialize_der ( & secp_ctx) . to_vec ( ) ) ;
7629
+ spend_tx. input [ 0 ] . witness [ 0 ] . push ( SigHashType :: All as u8 ) ;
7630
+ spend_tx. input [ 0 ] . witness . push ( destination_pubkey. serialize ( ) . to_vec ( ) ) ;
7631
+ spend_tx
7632
+ } ,
7633
+ _ => panic ! ( "Unexpected event !" ) ,
7634
+ }
7635
+ } ,
7636
+ _ => panic ! ( "Unexpected event !" ) ,
7637
+ } ;
7638
+ check_spends ! ( spend_tx, node_txn[ 0 ] . clone( ) ) ;
7639
+ }
7553
7640
}
0 commit comments