Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
4499948
hsmd: add support for lightningd signing onchain txs.
rustyrussell Apr 5, 2023
3bfce44
lightningd: handle first case of onchaind handing a tx to us to create.
rustyrussell Apr 5, 2023
2210551
onchaind: infrastructure to offload tx creation to lightningd.
rustyrussell Apr 5, 2023
8ec916f
pyln-testing: adapt wait_for_onchaind_broadcast function for when onc…
rustyrussell Apr 5, 2023
d48ad0d
onchaind: use lightningd to send "delayed_output_to_us" from HTLC txs.
rustyrussell Apr 5, 2023
aa34bb9
onchaind: use lightningd for spending our unilateral "to us" output.
rustyrussell Apr 5, 2023
3c57452
lightningd: remember depth of closing transaction.
rustyrussell Apr 5, 2023
6653f69
onchaind, pytest: disable RBF logic.
rustyrussell Apr 5, 2023
77d6d26
onchaind: have lightningd create our penalty txs.
rustyrussell Apr 5, 2023
2609888
common: expose low-level htlc_tx function.
rustyrussell Apr 5, 2023
ace8dc2
onchaind: use lightningd to sign and broadcast htlc_success transacti…
rustyrussell Apr 5, 2023
e775180
onchaind: use lightningd to sign and broadcast htlc spending txs.
rustyrussell Apr 5, 2023
09b59b5
onchaind: use lightningd to sign and broadcast htlc_timeout transacti…
rustyrussell Apr 5, 2023
70a0387
onchaind: use lightningd to sign and broadcast htlc expired txs.
rustyrussell Apr 5, 2023
97af067
pytest: clean up wait_for_onchaind_tx interface, remove wait_for_onch…
rustyrussell Apr 5, 2023
94d217c
onchaind: propose_ignore specifically to ignore if output reaches depth.
rustyrussell Apr 5, 2023
fb32374
onchaind: remove now-unused direct tx creation.
rustyrussell Apr 5, 2023
450ae4d
onchaind: no longer need information about current feerates.
rustyrussell Apr 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/hsm_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* v3 without v1: 3f813898f7de490e9126ab817e1c9a29af79c0413d5e37068acedce3ea7b5429
* v4: 41a730986c51b930e2d8d12b3169d24966c2004e08d424bdda310edbbde5ba70
* v4 with check_pubkey: 48b3992745aa3c6ab6ce5cdaee9082cb7d70017f523d322015e9710bf49fd193
* v4 with sign_any_penalty_to_us: ead7963185194a515d1f14d2c44401392575299d68ce9a13d8a12baff3cf4f35
*/
#define HSM_MIN_VERSION 3
#define HSM_MAX_VERSION 4
Expand Down
59 changes: 31 additions & 28 deletions common/htlc_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@
#include <common/htlc_tx.h>
#include <common/keyset.h>

static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
const struct chainparams *chainparams,
const struct bitcoin_outpoint *commit,
const u8 *commit_wscript,
struct amount_msat msat,
u16 to_self_delay,
const struct pubkey *revocation_pubkey,
const struct pubkey *local_delayedkey,
struct amount_sat htlc_fee,
u32 locktime,
bool option_anchor_outputs)
/* Low-level tx creator: used when onchaind has done most of the work! */
struct bitcoin_tx *htlc_tx(const tal_t *ctx,
const struct chainparams *chainparams,
const struct bitcoin_outpoint *commit,
const u8 *commit_wscript,
struct amount_sat amount,
const u8 *htlc_tx_wscript,
struct amount_sat htlc_fee,
u32 locktime,
bool option_anchor_outputs)
{
/* BOLT #3:
* * locktime: `0` for HTLC-success, `cltv_expiry` for HTLC-timeout
*/
struct bitcoin_tx *tx = bitcoin_tx(ctx, chainparams, 1, 1, locktime);
u8 *wscript;
struct amount_sat amount;

/* BOLT #3:
*
Expand All @@ -45,7 +42,6 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
* transaction
* * `txin[0]` sequence: `0` (set to `1` for `option_anchors`)
*/
amount = amount_msat_to_sat_round_down(msat);
bitcoin_tx_add_input(tx, commit,
option_anchor_outputs ? 1 : 0,
NULL, amount, NULL, commit_wscript);
Expand All @@ -59,18 +55,14 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
* below
*/
if (!amount_sat_sub(&amount, amount, htlc_fee))
abort();
return tal_free(tx);

wscript = bitcoin_wscript_htlc_tx(tx, to_self_delay, revocation_pubkey,
local_delayedkey);
bitcoin_tx_add_output(tx, scriptpubkey_p2wsh(tmpctx, wscript),
wscript, amount);
bitcoin_tx_add_output(tx, scriptpubkey_p2wsh(tmpctx, htlc_tx_wscript),
htlc_tx_wscript, amount);

bitcoin_tx_finalize(tx);
assert(bitcoin_tx_check(tx));

tal_free(wscript);

return tx;
}

Expand All @@ -84,14 +76,19 @@ struct bitcoin_tx *htlc_success_tx(const tal_t *ctx,
const struct keyset *keyset,
bool option_anchor_outputs)
{
const u8 *htlc_wscript;

htlc_wscript = bitcoin_wscript_htlc_tx(tmpctx,
to_self_delay,
&keyset->self_revocation_key,
&keyset->self_delayed_payment_key);
/* BOLT #3:
* * locktime: `0` for HTLC-success, `cltv_expiry` for HTLC-timeout
*/
return htlc_tx(ctx, chainparams, commit,
commit_wscript, htlc_msatoshi,
to_self_delay,
&keyset->self_revocation_key,
&keyset->self_delayed_payment_key,
commit_wscript,
amount_msat_to_sat_round_down(htlc_msatoshi),
htlc_wscript,
htlc_success_fee(feerate_per_kw,
option_anchor_outputs),
0,
Expand Down Expand Up @@ -137,13 +134,19 @@ struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
const struct keyset *keyset,
bool option_anchor_outputs)
{
const u8 *htlc_wscript;

htlc_wscript = bitcoin_wscript_htlc_tx(tmpctx,
to_self_delay,
&keyset->self_revocation_key,
&keyset->self_delayed_payment_key);
/* BOLT #3:
* * locktime: `0` for HTLC-success, `cltv_expiry` for HTLC-timeout
*/
return htlc_tx(ctx, chainparams, commit,
commit_wscript, htlc_msatoshi, to_self_delay,
&keyset->self_revocation_key,
&keyset->self_delayed_payment_key,
commit_wscript,
amount_msat_to_sat_round_down(htlc_msatoshi),
htlc_wscript,
htlc_timeout_fee(feerate_per_kw,
option_anchor_outputs),
cltv_expiry,
Expand Down
10 changes: 10 additions & 0 deletions common/htlc_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,14 @@ u8 *htlc_offered_wscript(const tal_t *ctx,
const struct keyset *keyset,
bool option_anchor_outputs);

/* Low-level HTLC tx creator */
struct bitcoin_tx *htlc_tx(const tal_t *ctx,
const struct chainparams *chainparams,
const struct bitcoin_outpoint *commit,
const u8 *commit_wscript,
struct amount_sat amount,
const u8 *htlc_tx_wscript,
struct amount_sat htlc_fee,
u32 locktime,
bool option_anchor_outputs);
#endif /* LIGHTNING_COMMON_HTLC_TX_H */
6 changes: 6 additions & 0 deletions common/test/run-channel_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ struct amount_sat amount_sat(u64 satoshis UNNEEDED)
struct amount_sat a UNNEEDED,
struct amount_sat b UNNEEDED)
{ fprintf(stderr, "amount_sat_add called!\n"); abort(); }
/* Generated stub for amount_sat_div */
struct amount_sat amount_sat_div(struct amount_sat sat UNNEEDED, u64 div UNNEEDED)
{ fprintf(stderr, "amount_sat_div called!\n"); abort(); }
/* Generated stub for amount_sat_eq */
bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
{ fprintf(stderr, "amount_sat_eq called!\n"); abort(); }
/* Generated stub for amount_sat_greater_eq */
bool amount_sat_greater_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
{ fprintf(stderr, "amount_sat_greater_eq called!\n"); abort(); }
/* Generated stub for amount_sat_mul */
bool amount_sat_mul(struct amount_sat *res UNNEEDED, struct amount_sat sat UNNEEDED, u64 mul UNNEEDED)
{ fprintf(stderr, "amount_sat_mul called!\n"); abort(); }
/* Generated stub for amount_sat_sub */
bool amount_sat_sub(struct amount_sat *val UNNEEDED,
struct amount_sat a UNNEEDED,
Expand Down
23 changes: 23 additions & 0 deletions contrib/pyln-testing/pyln/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,29 @@ def force_feerates(self, rate):
self.daemon.wait_for_log('peer_out WIRE_UPDATE_FEE')
assert(self.rpc.feerates('perkw')['perkw']['opening'] == rate)

def wait_for_onchaind_txs(self, *args):
"""Wait for onchaind to ask lightningd to create one or more txs. Each arg is a pair of typename, resolvename. Returns tuples of the rawtx, txid and number of blocks delay for each pair.
"""
# Could happen in any order.
needle = self.daemon.logsearch_start
ret = ()
for (name, resolve) in args:
self.daemon.logsearch_start = needle
r = self.daemon.wait_for_log('Telling lightningd about {} to resolve {}'
.format(name, resolve))
blocks = int(re.search(r'\(([-0-9]*) more blocks\)', r).group(1))

# The next 'Broadcast for onchaind' will be the tx.
# Now grab the corresponding broadcast lightningd did, to get actual tx:
r = self.daemon.wait_for_log('Broadcast for onchaind tx')
rawtx = re.search(r'.* tx ([0-9a-fA-F]*)', r).group(1)
txid = self.bitcoin.rpc.decoderawtransaction(rawtx, True)['txid']
ret = ret + ((rawtx, txid, blocks),)
return ret

def wait_for_onchaind_tx(self, name, resolve):
return self.wait_for_onchaind_txs((name, resolve))[0]

def wait_for_onchaind_broadcast(self, name, resolve=None):
"""Wait for onchaind to drop tx name to resolve (if any)"""
if resolve:
Expand Down
4 changes: 4 additions & 0 deletions hsmd/hsmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,10 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
case WIRE_HSMD_SIGN_REMOTE_HTLC_TO_US:
case WIRE_HSMD_SIGN_DELAYED_PAYMENT_TO_US:
case WIRE_HSMD_CHECK_PUBKEY:
case WIRE_HSMD_SIGN_ANY_PENALTY_TO_US:
case WIRE_HSMD_SIGN_ANY_DELAYED_PAYMENT_TO_US:
case WIRE_HSMD_SIGN_ANY_REMOTE_HTLC_TO_US:
case WIRE_HSMD_SIGN_ANY_LOCAL_HTLC_TX:
/* Hand off to libhsmd for processing */
return req_reply(conn, c,
take(hsmd_handle_client_message(
Expand Down
40 changes: 39 additions & 1 deletion hsmd/hsmd_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ msgtype,hsmd_validate_revocation_reply,136

# Onchaind asks HSM to sign a spend to-us. Four variants, since each set
# of keys is derived differently...
# FIXME: Have master tell hsmd the keyindex, so it can validate output!
msgtype,hsmd_sign_delayed_payment_to_us,12
msgdata,hsmd_sign_delayed_payment_to_us,commit_num,u64,
msgdata,hsmd_sign_delayed_payment_to_us,tx,bitcoin_tx,
Expand Down Expand Up @@ -332,3 +331,42 @@ msgdata,hsmd_check_pubkey,pubkey,pubkey,
# Reply
msgtype,hsmd_check_pubkey_reply,128
msgdata,hsmd_check_pubkey_reply,ok,bool,

# These are where lightningd asks for signatures on onchaind's behalf.
msgtype,hsmd_sign_any_delayed_payment_to_us,142
msgdata,hsmd_sign_any_delayed_payment_to_us,commit_num,u64,
msgdata,hsmd_sign_any_delayed_payment_to_us,tx,bitcoin_tx,
msgdata,hsmd_sign_any_delayed_payment_to_us,wscript_len,u16,
msgdata,hsmd_sign_any_delayed_payment_to_us,wscript,u8,wscript_len
msgdata,hsmd_sign_any_delayed_payment_to_us,input,u32,
msgdata,hsmd_sign_any_delayed_payment_to_us,peerid,node_id,
msgdata,hsmd_sign_any_delayed_payment_to_us,channel_dbid,u64,

msgtype,hsmd_sign_any_remote_htlc_to_us,143
msgdata,hsmd_sign_any_remote_htlc_to_us,remote_per_commitment_point,pubkey,
msgdata,hsmd_sign_any_remote_htlc_to_us,tx,bitcoin_tx,
msgdata,hsmd_sign_any_remote_htlc_to_us,wscript_len,u16,
msgdata,hsmd_sign_any_remote_htlc_to_us,wscript,u8,wscript_len
msgdata,hsmd_sign_any_remote_htlc_to_us,option_anchor_outputs,bool,
msgdata,hsmd_sign_any_remote_htlc_to_us,input,u32,
msgdata,hsmd_sign_any_remote_htlc_to_us,peerid,node_id,
msgdata,hsmd_sign_any_remote_htlc_to_us,channel_dbid,u64,

msgtype,hsmd_sign_any_penalty_to_us,144
msgdata,hsmd_sign_any_penalty_to_us,revocation_secret,secret,
msgdata,hsmd_sign_any_penalty_to_us,tx,bitcoin_tx,
msgdata,hsmd_sign_any_penalty_to_us,wscript_len,u16,
msgdata,hsmd_sign_any_penalty_to_us,wscript,u8,wscript_len
msgdata,hsmd_sign_any_penalty_to_us,input,u32,
msgdata,hsmd_sign_any_penalty_to_us,peerid,node_id,
msgdata,hsmd_sign_any_penalty_to_us,channel_dbid,u64,

msgtype,hsmd_sign_any_local_htlc_tx,146
msgdata,hsmd_sign_any_local_htlc_tx,commit_num,u64,
msgdata,hsmd_sign_any_local_htlc_tx,tx,bitcoin_tx,
msgdata,hsmd_sign_any_local_htlc_tx,wscript_len,u16,
msgdata,hsmd_sign_any_local_htlc_tx,wscript,u8,wscript_len
msgdata,hsmd_sign_any_local_htlc_tx,option_anchor_outputs,bool,
msgdata,hsmd_sign_any_local_htlc_tx,input,u32,
msgdata,hsmd_sign_any_local_htlc_tx,peerid,node_id,
msgdata,hsmd_sign_any_local_htlc_tx,channel_dbid,u64,
Loading