1
1
#include "config.h"
2
2
#include <bitcoin/feerate.h>
3
3
#include <bitcoin/script.h>
4
+ #include <ccan/array_size/array_size.h>
4
5
#include <ccan/asort/asort.h>
5
6
#include <ccan/cast/cast.h>
6
7
#include <ccan/mem/mem.h>
@@ -624,14 +625,6 @@ static u8 *remote_htlc_to_us(const tal_t *ctx,
624
625
option_anchor_outputs );
625
626
}
626
627
627
- static u8 * penalty_to_us (const tal_t * ctx ,
628
- struct bitcoin_tx * tx ,
629
- const u8 * wscript )
630
- {
631
- return towire_hsmd_sign_penalty_to_us (ctx , remote_per_commitment_secret ,
632
- tx , wscript );
633
- }
634
-
635
628
/*
636
629
* This covers:
637
630
* 1. to-us output spend (`<local_delayedsig> 0`)
@@ -927,6 +920,17 @@ static void propose_resolution_to_master(struct tracked_output *out,
927
920
queue_until_msg (tmpctx , WIRE_ONCHAIND_SPEND_CREATED ));
928
921
}
929
922
923
+ /* Create and broadcast this tx now */
924
+ static void propose_immediate_resolution (struct tracked_output * out ,
925
+ const u8 * send_message TAKES ,
926
+ enum tx_type tx_type )
927
+ {
928
+ /* We add 1 to blockheight (meaning you can broadcast it now) to avoid
929
+ * having to check for < 0 in various places we print messages */
930
+ propose_resolution_to_master (out , send_message , out -> tx_blockheight + 1 ,
931
+ tx_type );
932
+ }
933
+
930
934
static bool is_valid_sig (const u8 * e )
931
935
{
932
936
struct bitcoin_signature sig ;
@@ -1417,11 +1421,10 @@ static void steal_htlc_tx(struct tracked_output *out,
1417
1421
enum tx_type htlc_tx_type ,
1418
1422
const struct bitcoin_outpoint * htlc_outpoint )
1419
1423
{
1420
- struct bitcoin_tx * tx ;
1421
- enum tx_type tx_type = OUR_PENALTY_TX ;
1422
1424
struct tracked_output * htlc_out ;
1423
1425
struct amount_asset asset ;
1424
1426
struct amount_sat htlc_out_amt ;
1427
+ const u8 * msg ;
1425
1428
1426
1429
u8 * wscript = bitcoin_wscript_htlc_tx (htlc_tx , to_self_delay [REMOTE ],
1427
1430
& keyset -> self_revocation_key ,
@@ -1437,22 +1440,23 @@ static void steal_htlc_tx(struct tracked_output *out,
1437
1440
htlc_out_amt ,
1438
1441
DELAYED_CHEAT_OUTPUT_TO_THEM ,
1439
1442
& out -> htlc , wscript , NULL );
1443
+
1444
+ /* mark commitment tx htlc output as 'resolved by them' */
1445
+ resolved_by_other (out , & htlc_tx -> txid , htlc_tx_type );
1446
+
1440
1447
/* BOLT #3:
1441
1448
*
1442
1449
* To spend this via penalty, the remote node uses a witness stack
1443
1450
* `<revocationsig> 1`
1444
1451
*/
1445
- tx = tx_to_us (htlc_out , penalty_to_us , htlc_out ,
1446
- BITCOIN_TX_RBF_SEQUENCE , 0 ,
1447
- & ONE , sizeof (ONE ),
1448
- htlc_out -> wscript ,
1449
- & tx_type , penalty_feerate );
1450
-
1451
- /* mark commitment tx htlc output as 'resolved by them' */
1452
- resolved_by_other (out , & htlc_tx -> txid , htlc_tx_type );
1452
+ msg = towire_onchaind_spend_penalty (NULL ,
1453
+ htlc_outpoint , htlc_out_amt ,
1454
+ remote_per_commitment_secret ,
1455
+ tal_dup (tmpctx , u8 , & ONE ),
1456
+ htlc_out -> wscript );
1453
1457
1454
- /* annnd done! */
1455
- propose_resolution (htlc_out , tx , 0 , tx_type );
1458
+ /* Spend this immediately. */
1459
+ propose_immediate_resolution (htlc_out , take ( msg ), OUR_PENALTY_TX );
1456
1460
}
1457
1461
1458
1462
static void onchain_annotate_txout (const struct bitcoin_outpoint * outpoint ,
@@ -1964,6 +1968,7 @@ static void wait_for_resolved(struct tracked_output **outs)
1964
1968
case WIRE_ONCHAIND_ANNOTATE_TXIN :
1965
1969
case WIRE_ONCHAIND_NOTIFY_COIN_MVT :
1966
1970
case WIRE_ONCHAIND_SPEND_TO_US :
1971
+ case WIRE_ONCHAIND_SPEND_PENALTY :
1967
1972
break ;
1968
1973
}
1969
1974
master_badmsg (-1 , msg );
@@ -2753,9 +2758,7 @@ static void handle_our_unilateral(const struct tx_parts *tx,
2753
2758
* delay */
2754
2759
static void steal_to_them_output (struct tracked_output * out , u32 csv )
2755
2760
{
2756
- u8 * wscript ;
2757
- struct bitcoin_tx * tx ;
2758
- enum tx_type tx_type = OUR_PENALTY_TX ;
2761
+ const u8 * wscript , * msg ;
2759
2762
2760
2763
/* BOLT #3:
2761
2764
*
@@ -2768,16 +2771,19 @@ static void steal_to_them_output(struct tracked_output *out, u32 csv)
2768
2771
& keyset -> self_revocation_key ,
2769
2772
& keyset -> self_delayed_payment_key );
2770
2773
2771
- tx = tx_to_us (tmpctx , penalty_to_us , out , BITCOIN_TX_RBF_SEQUENCE , 0 ,
2772
- & ONE , sizeof (ONE ), wscript , & tx_type , penalty_feerate );
2774
+ msg = towire_onchaind_spend_penalty (NULL ,
2775
+ & out -> outpoint , out -> sat ,
2776
+ remote_per_commitment_secret ,
2777
+ tal_dup (tmpctx , u8 , & ONE ),
2778
+ wscript );
2773
2779
2774
- propose_resolution (out , tx , 0 , tx_type );
2780
+ /* Spend this immediately. */
2781
+ propose_immediate_resolution (out , take (msg ), OUR_PENALTY_TX );
2775
2782
}
2776
2783
2777
2784
static void steal_htlc (struct tracked_output * out )
2778
2785
{
2779
- struct bitcoin_tx * tx ;
2780
- enum tx_type tx_type = OUR_PENALTY_TX ;
2786
+ const u8 * msg ;
2781
2787
u8 der [PUBKEY_CMPR_LEN ];
2782
2788
2783
2789
/* BOLT #3:
@@ -2788,11 +2794,15 @@ static void steal_htlc(struct tracked_output *out)
2788
2794
* <revocation_sig> <revocationpubkey>
2789
2795
*/
2790
2796
pubkey_to_der (der , & keyset -> self_revocation_key );
2791
- tx = tx_to_us (out , penalty_to_us , out , BITCOIN_TX_RBF_SEQUENCE , 0 ,
2792
- der , sizeof (der ), out -> wscript , & tx_type ,
2793
- penalty_feerate );
2794
2797
2795
- propose_resolution (out , tx , 0 , tx_type );
2798
+ msg = towire_onchaind_spend_penalty (NULL ,
2799
+ & out -> outpoint , out -> sat ,
2800
+ remote_per_commitment_secret ,
2801
+ tal_dup_arr (tmpctx , u8 , der , ARRAY_SIZE (der ), 0 ),
2802
+ out -> wscript );
2803
+
2804
+ /* Spend this immediately. */
2805
+ propose_immediate_resolution (out , take (msg ), OUR_PENALTY_TX );
2796
2806
}
2797
2807
2798
2808
/* Tell wallet that we have discovered a UTXO from a to-remote output,
0 commit comments