@@ -3225,6 +3225,8 @@ mod tests {
3225
3225
3226
3226
use bitcoin:: util:: hash:: { BitcoinHash , Sha256dHash } ;
3227
3227
use bitcoin:: util:: bip143;
3228
+ use bitcoin:: util:: address:: Address ;
3229
+ use bitcoin:: util:: bip32:: { ChildNumber , ExtendedPubKey , ExtendedPrivKey } ;
3228
3230
use bitcoin:: blockdata:: block:: { Block , BlockHeader } ;
3229
3231
use bitcoin:: blockdata:: transaction:: { Transaction , TxOut , TxIn , SigHashType } ;
3230
3232
use bitcoin:: blockdata:: script:: { Builder , Script } ;
@@ -7536,7 +7538,7 @@ mod tests {
7536
7538
} else { panic ! ( "Unexpected result" ) ; }
7537
7539
}
7538
7540
7539
- macro_rules! check_dynamic_output {
7541
+ macro_rules! check_dynamic_output_p2wsh {
7540
7542
( $node: expr) => {
7541
7543
{
7542
7544
let events = $node. chan_monitor. simple_monitor. get_and_clear_pending_events( ) ;
@@ -7546,7 +7548,7 @@ mod tests {
7546
7548
Event :: SpendableOutputs { ref outputs } => {
7547
7549
for outp in outputs {
7548
7550
match * outp {
7549
- SpendableOutputDescriptor :: DynamicOutput { ref outpoint, ref local_delayedkey , ref witness_script, ref to_self_delay, ref output } => {
7551
+ SpendableOutputDescriptor :: DynamicOutputP2WSH { ref outpoint, ref key , ref witness_script, ref to_self_delay, ref output } => {
7550
7552
let input = TxIn {
7551
7553
previous_output: outpoint. clone( ) ,
7552
7554
script_sig: Script :: new( ) ,
@@ -7563,9 +7565,9 @@ mod tests {
7563
7565
input: vec![ input] ,
7564
7566
output: vec![ outp] ,
7565
7567
} ;
7566
- let sighash = Message :: from_slice( & bip143:: SighashComponents :: new( & spend_tx) . sighash_all( & spend_tx. input[ 0 ] , witness_script, output. value) [ ..] ) . unwrap( ) ;
7567
7568
let secp_ctx = Secp256k1 :: new( ) ;
7568
- let local_delaysig = secp_ctx. sign( & sighash, local_delayedkey) ;
7569
+ let sighash = Message :: from_slice( & bip143:: SighashComponents :: new( & spend_tx) . sighash_all( & spend_tx. input[ 0 ] , witness_script, output. value) [ ..] ) . unwrap( ) ;
7570
+ let local_delaysig = secp_ctx. sign( & sighash, key) ;
7569
7571
spend_tx. input[ 0 ] . witness. push( local_delaysig. serialize_der( & secp_ctx) . to_vec( ) ) ;
7570
7572
spend_tx. input[ 0 ] . witness[ 0 ] . push( SigHashType :: All as u8 ) ;
7571
7573
spend_tx. input[ 0 ] . witness. push( vec!( 0 ) ) ;
@@ -7584,6 +7586,106 @@ mod tests {
7584
7586
}
7585
7587
}
7586
7588
7589
+ macro_rules! check_dynamic_output_p2wpkh {
7590
+ ( $node: expr) => {
7591
+ {
7592
+ let events = $node. chan_monitor. simple_monitor. get_and_clear_pending_events( ) ;
7593
+ let mut txn = Vec :: new( ) ;
7594
+ for event in events {
7595
+ match event {
7596
+ Event :: SpendableOutputs { ref outputs } => {
7597
+ for outp in outputs {
7598
+ match * outp {
7599
+ SpendableOutputDescriptor :: DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => {
7600
+ let input = TxIn {
7601
+ previous_output: outpoint. clone( ) ,
7602
+ script_sig: Script :: new( ) ,
7603
+ sequence: 0 ,
7604
+ witness: Vec :: new( ) ,
7605
+ } ;
7606
+ let outp = TxOut {
7607
+ script_pubkey: Builder :: new( ) . push_opcode( opcodes:: All :: OP_RETURN ) . into_script( ) ,
7608
+ value: output. value,
7609
+ } ;
7610
+ let mut spend_tx = Transaction {
7611
+ version: 2 ,
7612
+ lock_time: 0 ,
7613
+ input: vec![ input] ,
7614
+ output: vec![ outp] ,
7615
+ } ;
7616
+ let secp_ctx = Secp256k1 :: new( ) ;
7617
+ let remotepubkey = PublicKey :: from_secret_key( & secp_ctx, & key) ;
7618
+ let witness_script = Address :: p2pkh( & remotepubkey, Network :: Testnet ) . script_pubkey( ) ;
7619
+ let sighash = Message :: from_slice( & bip143:: SighashComponents :: new( & spend_tx) . sighash_all( & spend_tx. input[ 0 ] , & witness_script, output. value) [ ..] ) . unwrap( ) ;
7620
+ let remotesig = secp_ctx. sign( & sighash, key) ;
7621
+ spend_tx. input[ 0 ] . witness. push( remotesig. serialize_der( & secp_ctx) . to_vec( ) ) ;
7622
+ spend_tx. input[ 0 ] . witness[ 0 ] . push( SigHashType :: All as u8 ) ;
7623
+ spend_tx. input[ 0 ] . witness. push( remotepubkey. serialize( ) . to_vec( ) ) ;
7624
+ txn. push( spend_tx) ;
7625
+ } ,
7626
+ _ => panic!( "Unexpected event" ) ,
7627
+ }
7628
+ }
7629
+ } ,
7630
+ _ => panic!( "Unexpected event" ) ,
7631
+ } ;
7632
+ }
7633
+ txn
7634
+ }
7635
+ }
7636
+ }
7637
+
7638
+ macro_rules! check_static_output {
7639
+ ( $event: expr, $node: expr, $event_idx: expr, $output_idx: expr, $der_idx: expr, $idx_node: expr) => {
7640
+ match $event[ $event_idx] {
7641
+ Event :: SpendableOutputs { ref outputs } => {
7642
+ match outputs[ $output_idx] {
7643
+ SpendableOutputDescriptor :: StaticOutput { ref outpoint, ref output } => {
7644
+ let secp_ctx = Secp256k1 :: new( ) ;
7645
+ let input = TxIn {
7646
+ previous_output: outpoint. clone( ) ,
7647
+ script_sig: Script :: new( ) ,
7648
+ sequence: 0 ,
7649
+ witness: Vec :: new( ) ,
7650
+ } ;
7651
+ let outp = TxOut {
7652
+ script_pubkey: Builder :: new( ) . push_opcode( opcodes:: All :: OP_RETURN ) . into_script( ) ,
7653
+ value: output. value,
7654
+ } ;
7655
+ let mut spend_tx = Transaction {
7656
+ version: 2 ,
7657
+ lock_time: 0 ,
7658
+ input: vec![ input] ,
7659
+ output: vec![ outp. clone( ) ] ,
7660
+ } ;
7661
+ let secret = {
7662
+ match ExtendedPrivKey :: new_master( & secp_ctx, Network :: Testnet , & $node[ $idx_node] . node_seed) {
7663
+ Ok ( master_key) => {
7664
+ match master_key. ckd_priv( & secp_ctx, ChildNumber :: from_hardened_idx( $der_idx) ) {
7665
+ Ok ( key) => key,
7666
+ Err ( _) => panic!( "Your RNG is busted" ) ,
7667
+ }
7668
+ }
7669
+ Err ( _) => panic!( "Your rng is busted" ) ,
7670
+ }
7671
+ } ;
7672
+ let pubkey = ExtendedPubKey :: from_private( & secp_ctx, & secret) . public_key;
7673
+ let witness_script = Address :: p2pkh( & pubkey, Network :: Testnet ) . script_pubkey( ) ;
7674
+ let sighash = Message :: from_slice( & bip143:: SighashComponents :: new( & spend_tx) . sighash_all( & spend_tx. input[ 0 ] , & witness_script, output. value) [ ..] ) . unwrap( ) ;
7675
+ let sig = secp_ctx. sign( & sighash, & secret. secret_key) ;
7676
+ spend_tx. input[ 0 ] . witness. push( sig. serialize_der( & secp_ctx) . to_vec( ) ) ;
7677
+ spend_tx. input[ 0 ] . witness[ 0 ] . push( SigHashType :: All as u8 ) ;
7678
+ spend_tx. input[ 0 ] . witness. push( pubkey. serialize( ) . to_vec( ) ) ;
7679
+ spend_tx
7680
+ } ,
7681
+ _ => panic!( "Unexpected event !" ) ,
7682
+ }
7683
+ } ,
7684
+ _ => panic!( "Unexpected event !" ) ,
7685
+ } ;
7686
+ }
7687
+ }
7688
+
7587
7689
#[ test]
7588
7690
fn test_claim_sizeable_push_msat ( ) {
7589
7691
// Incidentally test SpendableOutput event generation due to detection of to_local output on commitment tx
@@ -7603,8 +7705,48 @@ mod tests {
7603
7705
7604
7706
let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
7605
7707
nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ node_txn[ 0 ] . clone( ) ] } , 0 ) ;
7606
- let spend_txn = check_dynamic_output ! ( nodes[ 1 ] ) ;
7708
+ let spend_txn = check_dynamic_output_p2wsh ! ( nodes[ 1 ] ) ;
7607
7709
assert_eq ! ( spend_txn. len( ) , 1 ) ;
7608
7710
check_spends ! ( spend_txn[ 0 ] , node_txn[ 0 ] . clone( ) ) ;
7609
7711
}
7712
+
7713
+ #[ test]
7714
+ fn test_static_spendable_outputs_preimage_tx ( ) {
7715
+ let nodes = create_network ( 2 ) ;
7716
+
7717
+ // Create some initial channels
7718
+ let chan_1 = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
7719
+
7720
+ let payment_preimage = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] ) [ ..] , 3000000 ) . 0 ;
7721
+
7722
+ let commitment_tx = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get ( & chan_1. 2 ) . unwrap ( ) . last_local_commitment_txn . clone ( ) ;
7723
+ assert_eq ! ( commitment_tx[ 0 ] . input. len( ) , 1 ) ;
7724
+ assert_eq ! ( commitment_tx[ 0 ] . input[ 0 ] . previous_output. txid, chan_1. 3 . txid( ) ) ;
7725
+
7726
+ // Settle A's commitment tx on B's chain
7727
+ let header = BlockHeader { version : 0x20000000 , prev_blockhash : Default :: default ( ) , merkle_root : Default :: default ( ) , time : 42 , bits : 42 , nonce : 42 } ;
7728
+ assert ! ( nodes[ 1 ] . node. claim_funds( payment_preimage) ) ;
7729
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
7730
+ nodes[ 1 ] . chain_monitor . block_connected_with_filtering ( & Block { header, txdata : vec ! [ commitment_tx[ 0 ] . clone( ) ] } , 1 ) ;
7731
+ let events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
7732
+ match events[ 0 ] {
7733
+ MessageSendEvent :: UpdateHTLCs { .. } => { } ,
7734
+ _ => panic ! ( "Unexpected event" ) ,
7735
+ }
7736
+ match events[ 1 ] {
7737
+ MessageSendEvent :: BroadcastChannelUpdate { .. } => { } ,
7738
+ _ => panic ! ( "Unexepected event" ) ,
7739
+ }
7740
+
7741
+ // Check B's monitor was able to send back output descriptor event for preimage tx on A's commitment tx
7742
+ let node_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) ; // ChannelManager : 1 (local commitment tx), ChannelMonitor: 2 (1 preimage tx) * 2 (block-rescan)
7743
+ check_spends ! ( node_txn[ 0 ] , commitment_tx[ 0 ] . clone( ) ) ;
7744
+ assert_eq ! ( node_txn[ 0 ] , node_txn[ 2 ] ) ;
7745
+ assert_eq ! ( node_txn[ 0 ] . input[ 0 ] . witness. last( ) . unwrap( ) . len( ) , 133 ) ;
7746
+ check_spends ! ( node_txn[ 1 ] , chan_1. 3 . clone( ) ) ;
7747
+
7748
+ let events = nodes[ 1 ] . chan_monitor . simple_monitor . get_and_clear_pending_events ( ) ;
7749
+ let spend_tx = check_static_output ! ( events, nodes, 0 , 0 , 1 , 1 ) ;
7750
+ check_spends ! ( spend_tx, node_txn[ 0 ] . clone( ) ) ;
7751
+ }
7610
7752
}
0 commit comments