Skip to content

Commit

Permalink
funding: add RPC arg to specify a 'close_to' address
Browse files Browse the repository at this point in the history
Takes advantage of upfront-shutdown-script to permit users to
specify the close-to address for a channel at open, by adding
a `close_to` field to `fundchannel_start`.

Note that this only is in effect if `fundchannel_start` returns
with `close_to` set -- otherwise, peer doesn't
support `option_upfront_shutdown_script`.
  • Loading branch information
niftynei committed Oct 15, 2019
1 parent 624b76e commit 422b450
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 118 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- JSON API: `fundchannel_start` now includes field `scriptpubkey`
- JSON API: New method `listtransactions`
- JSON API: `signmessage` will now create a signature from your node on a message; `checkmessage` will verify it.
- JSON API: `fundchannel_start` now accepts an optional parameter `close_to`, the address to which these channel funds should be sent to on close. Returns `using_close_to` if will use.
- Plugin: new notifications `sendpay_success` and `sendpay_failure`.
- Protocol: nodes now announce features in `node_announcement` broadcasts.
- Protocol: we now offer `option_gossip_queries_ex` for finegrained gossip control.
Expand Down
5 changes: 3 additions & 2 deletions contrib/pylightning/lightning/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,12 +568,13 @@ def fundchannel_start(self, node_id, *args, **kwargs):
if 'satoshi' in kwargs:
return self._deprecated_fundchannel_start(node_id, *args, **kwargs)

def _fundchannel_start(node_id, amount, feerate=None, announce=True):
def _fundchannel_start(node_id, amount, feerate=None, announce=True, close_to=None):
payload = {
"id": node_id,
"amount": amount,
"feerate": feerate,
"announce": announce
"announce": announce,
"close_to": close_to,
}
return self.call("fundchannel_start", payload)

Expand Down
13 changes: 8 additions & 5 deletions doc/lightning-fundchannel_start.7
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
lightning-fundchannel_start - Command for initiating channel establishment for a lightning channel
.SH SYNOPSIS

\fBfundchannel_start\fR \fIid\fR \fIamount\fR [\fIfeerate\fR \fIannounce\fR]
\fBfundchannel_start\fR \fIid\fR \fIamount\fR [\fIfeerate\fR \fIannounce\fR \fIclose_to\fR]

.SH DESCRIPTION

Expand All @@ -26,6 +26,11 @@ commitment transactions\.
\fIannounce\fR whether or not to announce this channel\.


\fIclose_to\fR is a Bitcoin address to which the channel funds should be sent to
on close\. Only valid if both peers have negotiated \fBoption_upfront_shutdown_script\fR\.
Returns \fBclose_to\fR set to closing script iff is negotiated\.


Note that the funding transaction MUST NOT be broadcast until after
channel establishment has been successfully completed by running
\fBfundchannel_complete\fR, as the commitment transactions for this channel
Expand All @@ -35,6 +40,8 @@ transaction before that can lead to unrecoverable loss of funds\.
.SH RETURN VALUE

On success, returns the \fIfunding_address\fR and the \fIscriptpubkey\fR for the channel funding output\.
If a \fBclose_to\fR address was provided, will close to this address iff the \fBclose_to\fR address is
returned in the response\. Otherwise, the peer does not support \fBoption_upfront_shutdownscript\fR\.


On failure, returns an error\.
Expand All @@ -52,7 +59,3 @@ lightning-fundchannel_\fBcomplete\fR(7), lightning-fundchannel_\fBcancel\fR(7)

Main web site: \fIhttps://github.com/ElementsProject/lightning\fR

.HL

Last updated 2019-06-12 11:16:20 CEST

8 changes: 7 additions & 1 deletion doc/lightning-fundchannel_start.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ lightning-fundchannel\_start -- Command for initiating channel establishment for
SYNOPSIS
--------

**fundchannel\_start** *id* *amount* \[*feerate* *announce*\]
**fundchannel\_start** *id* *amount* \[*feerate* *announce* *close_to*\]

DESCRIPTION
-----------
Expand All @@ -23,6 +23,10 @@ commitment transactions.

*announce* whether or not to announce this channel.

*close_to* is a Bitcoin address to which the channel funds should be sent to
on close. Only valid if both peers have negotiated `option_upfront_shutdown_script`.
Returns `close_to` set to closing script iff is negotiated.

Note that the funding transaction MUST NOT be broadcast until after
channel establishment has been successfully completed by running
`fundchannel_complete`, as the commitment transactions for this channel
Expand All @@ -33,6 +37,8 @@ RETURN VALUE
------------

On success, returns the *funding\_address* and the *scriptpubkey* for the channel funding output.
If a `close_to` address was provided, will close to this address iff the `close_to` address is
returned in the response. Otherwise, the peer does not support `option_upfront_shutdownscript`.

On failure, returns an error.

Expand Down
2 changes: 1 addition & 1 deletion lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
const struct channel_info *channel_info,
/* NULL or stolen */
u8 *remote_shutdown_scriptpubkey,
u8 *local_shutdown_scriptpubkey,
const u8 *local_shutdown_scriptpubkey,
u64 final_key_idx,
bool last_was_revoke,
/* NULL or stolen */
Expand Down
6 changes: 3 additions & 3 deletions lightningd/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ struct channel {
/* Our funding tx pubkey. */
struct pubkey local_funding_pubkey;

/* Their scriptpubkey if they sent shutdown. */
u8 *shutdown_scriptpubkey[NUM_SIDES];
/* scriptpubkey for shutdown, if applicable. */
const u8 *shutdown_scriptpubkey[NUM_SIDES];
/* Address for any final outputs */
u64 final_key_idx;

Expand Down Expand Up @@ -157,7 +157,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
const struct channel_info *channel_info,
/* NULL or stolen */
u8 *remote_shutdown_scriptpubkey,
u8 *local_shutdown_scriptpubkey,
const u8 *local_shutdown_scriptpubkey,
u64 final_key_idx,
bool last_was_revoke,
/* NULL or stolen */
Expand Down
32 changes: 28 additions & 4 deletions lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ struct funding_channel {
struct amount_msat push;
struct amount_sat funding;
u8 channel_flags;
const u8 *our_upfront_shutdown_script;

/* Variables we need to compose fields in cmd's response */
const char *hextx;
Expand Down Expand Up @@ -165,6 +166,7 @@ wallet_commit_channel(struct lightningd *ld,
u8 channel_flags,
struct channel_info *channel_info,
u32 feerate,
const u8 *our_upfront_shutdown_script,
const u8 *remote_upfront_shutdown_script)
{
struct channel *channel;
Expand Down Expand Up @@ -247,7 +249,7 @@ wallet_commit_channel(struct lightningd *ld,
NULL, /* No HTLC sigs yet */
channel_info,
NULL, /* No shutdown_scriptpubkey[REMOTE] yet */
NULL, /* No shutdown_scriptpubkey[LOCAL] yet. Generate the default one. */
our_upfront_shutdown_script,
final_key_idx, false,
NULL, /* No commit sent yet */
/* If we're fundee, could be a little before this
Expand Down Expand Up @@ -290,7 +292,8 @@ static void funding_success(struct channel *channel)
}

static void funding_started_success(struct funding_channel *fc,
u8 *scriptPubkey)
u8 *scriptPubkey,
bool supports_shutdown)
{
struct json_stream *response;
struct command *cmd = fc->cmd;
Expand All @@ -303,6 +306,8 @@ static void funding_started_success(struct funding_channel *fc,
if (out) {
json_add_string(response, "funding_address", out);
json_add_hex_talarr(response, "scriptpubkey", scriptPubkey);
if (fc->our_upfront_shutdown_script)
json_add_hex_talarr(response, "close_to", fc->our_upfront_shutdown_script);
}

/* Clear this so cancel doesn't think it's still in progress */
Expand All @@ -315,9 +320,11 @@ static void opening_funder_start_replied(struct subd *openingd, const u8 *resp,
struct funding_channel *fc)
{
u8 *funding_scriptPubkey;
bool supports_shutdown_script;

if (!fromwire_opening_funder_start_reply(resp, resp,
&funding_scriptPubkey)) {
&funding_scriptPubkey,
&supports_shutdown_script)) {
log_broken(fc->uc->log,
"bad OPENING_FUNDER_REPLY %s",
tal_hex(resp, resp));
Expand All @@ -327,7 +334,12 @@ static void opening_funder_start_replied(struct subd *openingd, const u8 *resp,
goto failed;
}

funding_started_success(fc, funding_scriptPubkey);
/* If we're not using the upfront shutdown script, forget it */
if (!supports_shutdown_script)
fc->our_upfront_shutdown_script =
tal_free(fc->our_upfront_shutdown_script);

funding_started_success(fc, funding_scriptPubkey, supports_shutdown_script);

/* Mark that we're in-flight */
fc->inflight = true;
Expand Down Expand Up @@ -401,6 +413,7 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp,
fc->channel_flags,
&channel_info,
feerate,
fc->our_upfront_shutdown_script,
remote_upfront_shutdown_script);
if (!channel) {
was_pending(command_fail(fc->cmd, LIGHTNINGD,
Expand Down Expand Up @@ -496,6 +509,7 @@ static void opening_fundee_finished(struct subd *openingd,
channel_flags,
&channel_info,
feerate,
NULL,
remote_upfront_shutdown_script);
if (!channel) {
uncommitted_channel_disconnect(uc, "Commit channel failed");
Expand Down Expand Up @@ -1078,6 +1092,7 @@ static struct command_result *json_fund_channel_start(struct command *cmd,
p_req("amount", param_sat, &amount),
p_opt("feerate", param_feerate, &feerate_per_kw),
p_opt_def("announce", param_bool, &announce_channel, true),
p_opt("close_to", param_bitcoin_address, &fc->our_upfront_shutdown_script),
NULL))
return command_param_failed();
} else {
Expand All @@ -1101,6 +1116,9 @@ static struct command_result *json_fund_channel_start(struct command *cmd,
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Need set 'amount' field");
}

/* No upfront shutdown script option for deprecated API */
fc->our_upfront_shutdown_script = NULL;
}

if (amount_sat_greater(*amount, max_funding_satoshi))
Expand Down Expand Up @@ -1161,9 +1179,15 @@ static struct command_result *json_fund_channel_start(struct command *cmd,
peer->uncommitted_channel->fc = tal_steal(peer->uncommitted_channel, fc);
fc->uc = peer->uncommitted_channel;

/* Needs to be stolen away from cmd */
if (fc->our_upfront_shutdown_script)
fc->our_upfront_shutdown_script
= tal_steal(fc, fc->our_upfront_shutdown_script);

msg = towire_opening_funder_start(NULL,
*amount,
fc->push,
fc->our_upfront_shutdown_script,
*feerate_per_kw,
fc->channel_flags);

Expand Down
Loading

0 comments on commit 422b450

Please sign in to comment.