2017-03-29 12:54:15 +02:00
|
|
|
#include <bitcoin/block.h>
|
2017-07-11 15:50:10 +02:00
|
|
|
#include <bitcoin/chainparams.h>
|
2017-02-21 05:45:29 +01:00
|
|
|
#include <bitcoin/privkey.h>
|
|
|
|
#include <bitcoin/script.h>
|
|
|
|
#include <ccan/breakpoint/breakpoint.h>
|
2018-02-08 02:24:46 +01:00
|
|
|
#include <ccan/cast/cast.h>
|
2017-02-21 05:45:29 +01:00
|
|
|
#include <ccan/fdpass/fdpass.h>
|
|
|
|
#include <ccan/structeq/structeq.h>
|
2017-12-06 04:22:35 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/crypto_sync.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/derive_basepoints.h>
|
|
|
|
#include <common/funding_tx.h>
|
2018-03-19 00:25:46 +01:00
|
|
|
#include <common/gen_peer_status_wire.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/initial_channel.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/key_derive.h>
|
2018-02-23 06:53:47 +01:00
|
|
|
#include <common/peer_billboard.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/peer_failed.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <common/pseudorand.h>
|
2018-01-31 03:53:42 +01:00
|
|
|
#include <common/read_peer_msg.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/status.h>
|
2018-01-08 11:01:09 +01:00
|
|
|
#include <common/subdaemon.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/type_to_string.h>
|
|
|
|
#include <common/version.h>
|
2017-12-06 04:22:35 +01:00
|
|
|
#include <common/wire_error.h>
|
2017-02-21 05:45:29 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <inttypes.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <openingd/gen_opening_wire.h>
|
2017-02-21 05:45:29 +01:00
|
|
|
#include <secp256k1.h>
|
|
|
|
#include <stdio.h>
|
2017-05-23 13:07:42 +02:00
|
|
|
#include <wally_bip32.h>
|
2017-02-21 05:45:29 +01:00
|
|
|
#include <wire/gen_peer_wire.h>
|
2017-05-05 08:41:45 +02:00
|
|
|
#include <wire/peer_wire.h>
|
2017-02-21 05:45:29 +01:00
|
|
|
#include <wire/wire.h>
|
|
|
|
#include <wire/wire_sync.h>
|
|
|
|
|
2017-05-23 13:56:04 +02:00
|
|
|
/* stdin == requests, 3 == peer, 4 == gossip */
|
2017-02-21 05:45:29 +01:00
|
|
|
#define REQ_FD STDIN_FILENO
|
|
|
|
#define PEER_FD 3
|
2017-05-23 13:56:04 +02:00
|
|
|
#define GOSSIP_FD 4
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
struct state {
|
2017-02-21 05:49:02 +01:00
|
|
|
struct crypto_state cs;
|
2017-02-21 05:45:29 +01:00
|
|
|
struct pubkey next_per_commit[NUM_SIDES];
|
|
|
|
|
2017-09-06 03:25:49 +02:00
|
|
|
/* Initially temporary, then final channel id. */
|
|
|
|
struct channel_id channel_id;
|
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
/* Funding and feerate: set by opening peer. */
|
|
|
|
u64 funding_satoshis, push_msat;
|
|
|
|
u32 feerate_per_kw;
|
2017-12-18 07:41:52 +01:00
|
|
|
struct bitcoin_txid funding_txid;
|
2017-03-02 13:21:49 +01:00
|
|
|
u16 funding_txout;
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
/* Secret keys and basepoint secrets. */
|
|
|
|
struct secrets our_secrets;
|
|
|
|
|
|
|
|
/* Our shaseed for generating per-commitment-secrets. */
|
|
|
|
struct sha256 shaseed;
|
2017-02-24 06:52:56 +01:00
|
|
|
struct channel_config localconf, *remoteconf;
|
|
|
|
|
|
|
|
/* Limits on what remote config we accept */
|
|
|
|
u32 max_to_self_delay;
|
|
|
|
u64 min_effective_htlc_capacity_msat;
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
struct channel *channel;
|
2017-07-11 15:50:10 +02:00
|
|
|
|
|
|
|
const struct chainparams *chainparams;
|
2017-02-21 05:45:29 +01:00
|
|
|
};
|
|
|
|
|
2018-03-19 00:25:46 +01:00
|
|
|
/* For negotiation failures: we tell them it's their fault. Same
|
|
|
|
* as peer_failed, with slightly different local and remote wording. */
|
2018-02-19 02:06:15 +01:00
|
|
|
static void negotiation_failed(struct state *state, const char *fmt, ...)
|
2017-12-06 04:22:35 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
const char *errmsg;
|
2018-03-19 00:25:46 +01:00
|
|
|
u8 *msg;
|
2017-12-06 04:22:35 +01:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
errmsg = tal_vfmt(state, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2018-03-19 00:25:46 +01:00
|
|
|
peer_billboard(true, errmsg);
|
|
|
|
msg = towire_status_peer_error(NULL, &state->channel_id,
|
2018-04-26 06:51:01 +02:00
|
|
|
errmsg, &state->cs,
|
2018-03-19 00:25:46 +01:00
|
|
|
towire_errorfmt(errmsg,
|
|
|
|
&state->channel_id,
|
|
|
|
"You gave bad parameters:%s",
|
|
|
|
errmsg));
|
|
|
|
tal_free(errmsg);
|
|
|
|
status_send_fatal(take(msg), PEER_FD, GOSSIP_FD);
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&state->cs, &state->channel_id,
|
2018-03-19 00:25:46 +01:00
|
|
|
"You gave bad parameters: %s", errmsg);
|
2017-12-06 04:22:35 +01:00
|
|
|
}
|
|
|
|
|
2017-02-24 06:52:55 +01:00
|
|
|
static void check_config_bounds(struct state *state,
|
2017-02-24 06:52:56 +01:00
|
|
|
const struct channel_config *remoteconf)
|
2017-02-21 05:45:29 +01:00
|
|
|
{
|
2017-02-24 06:52:56 +01:00
|
|
|
u64 capacity_msat;
|
|
|
|
u64 reserve_msat;
|
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The receiving node MUST fail the channel if:
|
|
|
|
*...
|
|
|
|
* - `to_self_delay` is unreasonably large.
|
2017-02-21 05:45:29 +01:00
|
|
|
*/
|
2017-02-24 06:52:56 +01:00
|
|
|
if (remoteconf->to_self_delay > state->max_to_self_delay)
|
2018-02-19 02:06:15 +01:00
|
|
|
negotiation_failed(state,
|
2017-12-06 04:22:35 +01:00
|
|
|
"to_self_delay %u larger than %u",
|
|
|
|
remoteconf->to_self_delay,
|
|
|
|
state->max_to_self_delay);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The receiving node MAY fail the channel if:
|
|
|
|
*...
|
|
|
|
* - `funding_satoshis` is too small.
|
|
|
|
* - it considers `htlc_minimum_msat` too large.
|
|
|
|
* - it considers `max_htlc_value_in_flight_msat` too small.
|
|
|
|
* - it considers `channel_reserve_satoshis` too large.
|
|
|
|
* - it considers `max_accepted_htlcs` too small.
|
2017-02-24 06:52:56 +01:00
|
|
|
*/
|
|
|
|
/* We accumulate this into an effective bandwidth minimum. */
|
|
|
|
|
|
|
|
/* Overflow check before capacity calc. */
|
|
|
|
if (remoteconf->channel_reserve_satoshis > state->funding_satoshis)
|
2018-02-19 02:06:15 +01:00
|
|
|
negotiation_failed(state,
|
2018-03-19 00:25:46 +01:00
|
|
|
"channel_reserve_satoshis %"PRIu64
|
|
|
|
" too large for funding_satoshis %"PRIu64,
|
2017-12-06 04:22:35 +01:00
|
|
|
remoteconf->channel_reserve_satoshis,
|
|
|
|
state->funding_satoshis);
|
2017-02-24 06:52:56 +01:00
|
|
|
|
|
|
|
/* Consider highest reserve. */
|
|
|
|
reserve_msat = remoteconf->channel_reserve_satoshis * 1000;
|
|
|
|
if (state->localconf.channel_reserve_satoshis * 1000 > reserve_msat)
|
|
|
|
reserve_msat = state->localconf.channel_reserve_satoshis * 1000;
|
|
|
|
|
|
|
|
capacity_msat = state->funding_satoshis * 1000 - reserve_msat;
|
|
|
|
|
|
|
|
if (remoteconf->max_htlc_value_in_flight_msat < capacity_msat)
|
|
|
|
capacity_msat = remoteconf->max_htlc_value_in_flight_msat;
|
|
|
|
|
|
|
|
if (remoteconf->htlc_minimum_msat * (u64)1000 > capacity_msat)
|
2018-02-19 02:06:15 +01:00
|
|
|
negotiation_failed(state,
|
2018-03-19 00:25:46 +01:00
|
|
|
"htlc_minimum_msat %"PRIu64
|
|
|
|
" too large for funding_satoshis %"PRIu64
|
2017-12-06 04:22:35 +01:00
|
|
|
" capacity_msat %"PRIu64,
|
|
|
|
remoteconf->htlc_minimum_msat,
|
|
|
|
state->funding_satoshis,
|
|
|
|
capacity_msat);
|
2017-02-24 06:52:56 +01:00
|
|
|
|
|
|
|
if (capacity_msat < state->min_effective_htlc_capacity_msat)
|
2018-02-19 02:06:15 +01:00
|
|
|
negotiation_failed(state,
|
2018-03-19 00:25:46 +01:00
|
|
|
"channel capacity with funding %"PRIu64" msat,"
|
2017-12-06 04:22:35 +01:00
|
|
|
" reserves %"PRIu64"/%"PRIu64" msat,"
|
|
|
|
" max_htlc_value_in_flight_msat %"PRIu64
|
|
|
|
" is %"PRIu64" msat, which is below %"PRIu64" msat",
|
|
|
|
state->funding_satoshis * 1000,
|
|
|
|
remoteconf->channel_reserve_satoshis * 1000,
|
|
|
|
state->localconf.channel_reserve_satoshis * 1000,
|
|
|
|
remoteconf->max_htlc_value_in_flight_msat,
|
|
|
|
capacity_msat,
|
|
|
|
state->min_effective_htlc_capacity_msat);
|
2017-02-24 06:52:56 +01:00
|
|
|
|
|
|
|
/* We don't worry about how many HTLCs they accept, as long as > 0! */
|
|
|
|
if (remoteconf->max_accepted_htlcs == 0)
|
2018-02-19 02:06:15 +01:00
|
|
|
negotiation_failed(state,
|
2017-12-06 04:22:35 +01:00
|
|
|
"max_accepted_htlcs %u invalid",
|
|
|
|
remoteconf->max_accepted_htlcs);
|
2017-02-24 06:52:56 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
2017-02-21 05:45:29 +01:00
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The receiving node MUST fail the channel if:
|
|
|
|
*...
|
|
|
|
* - `max_accepted_htlcs` is greater than 483.
|
2017-02-21 05:45:29 +01:00
|
|
|
*/
|
2017-03-07 06:46:59 +01:00
|
|
|
if (remoteconf->max_accepted_htlcs > 483)
|
2018-03-19 00:25:46 +01:00
|
|
|
negotiation_failed(state,
|
|
|
|
"max_accepted_htlcs %u too large",
|
|
|
|
remoteconf->max_accepted_htlcs);
|
2018-03-17 04:03:36 +01:00
|
|
|
|
2018-06-17 12:13:44 +02:00
|
|
|
/* FIXME #2:
|
2018-03-17 04:03:36 +01:00
|
|
|
*
|
|
|
|
* The receiving node MUST fail the channel if:
|
|
|
|
*...
|
|
|
|
* - `dust_limit_satoshis` is greater than `channel_reserve_satoshis`.
|
|
|
|
*/
|
|
|
|
if (remoteconf->dust_limit_satoshis
|
|
|
|
> remoteconf->channel_reserve_satoshis)
|
2018-03-19 00:25:46 +01:00
|
|
|
negotiation_failed(state,
|
|
|
|
"dust_limit_satoshis %"PRIu64
|
|
|
|
" too large for channel_reserve_satoshis %"
|
|
|
|
PRIu64,
|
|
|
|
remoteconf->dust_limit_satoshis,
|
|
|
|
remoteconf->channel_reserve_satoshis);
|
2017-02-21 05:45:29 +01:00
|
|
|
}
|
|
|
|
|
2017-02-24 06:52:56 +01:00
|
|
|
/* We always set channel_reserve_satoshis to 1%, rounded up. */
|
2018-03-17 04:03:36 +01:00
|
|
|
static void set_reserve(struct state *state)
|
2017-02-24 06:52:56 +01:00
|
|
|
{
|
2018-03-17 04:03:36 +01:00
|
|
|
state->localconf.channel_reserve_satoshis
|
|
|
|
= (state->funding_satoshis + 99) / 100;
|
|
|
|
|
2018-06-17 12:13:44 +02:00
|
|
|
/* FIXME #2:
|
2018-03-17 04:03:36 +01:00
|
|
|
*
|
|
|
|
* The sending node:
|
|
|
|
*...
|
|
|
|
* - MUST set `channel_reserve_satoshis` greater than or equal to
|
|
|
|
* `dust_limit_satoshis`.
|
|
|
|
*/
|
|
|
|
if (state->localconf.channel_reserve_satoshis
|
|
|
|
< state->localconf.dust_limit_satoshis)
|
|
|
|
state->localconf.channel_reserve_satoshis
|
|
|
|
= state->localconf.dust_limit_satoshis;
|
2017-02-24 06:52:56 +01:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The sending node:
|
|
|
|
*...
|
|
|
|
* - MUST ensure `temporary_channel_id` is unique from any other channel ID
|
|
|
|
* with the same peer.
|
2017-03-02 13:21:49 +01:00
|
|
|
*/
|
|
|
|
static void temporary_channel_id(struct channel_id *channel_id)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(*channel_id); i++)
|
|
|
|
channel_id->id[i] = pseudorand(256);
|
|
|
|
}
|
|
|
|
|
2018-01-31 03:53:42 +01:00
|
|
|
/* Handle random messages we might get, returning the first non-handled one. */
|
|
|
|
static u8 *opening_read_peer_msg(struct state *state)
|
2018-01-30 01:44:08 +01:00
|
|
|
{
|
|
|
|
u8 *msg;
|
|
|
|
|
2018-04-26 06:51:01 +02:00
|
|
|
while ((msg = read_peer_msg(state, &state->cs,
|
2018-02-19 02:06:15 +01:00
|
|
|
&state->channel_id,
|
2018-01-31 03:53:42 +01:00
|
|
|
sync_crypto_write_arg,
|
|
|
|
status_fail_io,
|
2018-03-15 05:30:37 +01:00
|
|
|
state)) == NULL)
|
|
|
|
clean_tmpctx();
|
2018-01-30 01:44:08 +01:00
|
|
|
|
|
|
|
return msg;
|
2017-05-05 08:41:45 +02:00
|
|
|
}
|
|
|
|
|
2017-05-23 13:04:17 +02:00
|
|
|
static u8 *funder_channel(struct state *state,
|
|
|
|
const struct pubkey *our_funding_pubkey,
|
|
|
|
const struct basepoints *ours,
|
2017-05-23 13:07:42 +02:00
|
|
|
u64 change_satoshis, u32 change_keyindex,
|
2017-06-27 04:55:01 +02:00
|
|
|
u8 channel_flags,
|
2018-02-08 02:24:46 +01:00
|
|
|
struct utxo **utxos,
|
2017-08-18 06:43:52 +02:00
|
|
|
const struct ext_key *bip32_base)
|
2017-02-21 05:45:29 +01:00
|
|
|
{
|
2017-09-06 03:25:49 +02:00
|
|
|
struct channel_id id_in;
|
2017-02-21 05:45:29 +01:00
|
|
|
u8 *msg;
|
2017-08-28 18:02:01 +02:00
|
|
|
struct bitcoin_tx *tx;
|
2017-03-07 02:07:06 +01:00
|
|
|
struct basepoints theirs;
|
2018-03-29 03:59:27 +02:00
|
|
|
struct pubkey their_funding_pubkey;
|
2018-03-29 09:09:08 +02:00
|
|
|
struct pubkey *changekey;
|
2017-02-21 05:45:29 +01:00
|
|
|
secp256k1_ecdsa_signature sig;
|
2017-04-12 08:52:57 +02:00
|
|
|
u32 minimum_depth;
|
2017-08-28 18:02:01 +02:00
|
|
|
const u8 *wscript;
|
2017-05-23 13:07:42 +02:00
|
|
|
struct bitcoin_tx *funding;
|
|
|
|
|
2018-03-17 04:03:36 +01:00
|
|
|
set_reserve(state);
|
2017-02-24 06:52:56 +01:00
|
|
|
|
2017-09-06 03:25:49 +02:00
|
|
|
temporary_channel_id(&state->channel_id);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2018-01-15 05:29:55 +01:00
|
|
|
if (state->funding_satoshis > MAX_FUNDING_SATOSHI)
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO,
|
2017-09-06 03:25:49 +02:00
|
|
|
"funding_satoshis must be < 2^24, not %"PRIu64,
|
|
|
|
state->funding_satoshis);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The sending node:
|
|
|
|
*...
|
|
|
|
* - MUST set `push_msat` to equal or less than 1000 *
|
|
|
|
* `funding_satoshis`.
|
2017-02-21 05:45:29 +01:00
|
|
|
*/
|
|
|
|
if (state->push_msat > 1000 * state->funding_satoshis)
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO,
|
2017-02-21 05:45:29 +01:00
|
|
|
"push-msat must be < %"PRIu64,
|
|
|
|
1000 * state->funding_satoshis);
|
|
|
|
|
2017-08-18 06:43:53 +02:00
|
|
|
msg = towire_open_channel(state,
|
2017-08-22 07:04:37 +02:00
|
|
|
&state->chainparams->genesis_blockhash,
|
2017-09-06 03:25:49 +02:00
|
|
|
&state->channel_id,
|
2017-02-21 05:45:29 +01:00
|
|
|
state->funding_satoshis, state->push_msat,
|
2017-02-21 05:49:02 +01:00
|
|
|
state->localconf.dust_limit_satoshis,
|
|
|
|
state->localconf.max_htlc_value_in_flight_msat,
|
|
|
|
state->localconf.channel_reserve_satoshis,
|
|
|
|
state->localconf.htlc_minimum_msat,
|
2017-02-21 05:45:29 +01:00
|
|
|
state->feerate_per_kw,
|
2017-02-21 05:49:02 +01:00
|
|
|
state->localconf.to_self_delay,
|
|
|
|
state->localconf.max_accepted_htlcs,
|
2017-03-07 02:07:06 +01:00
|
|
|
our_funding_pubkey,
|
|
|
|
&ours->revocation,
|
|
|
|
&ours->payment,
|
|
|
|
&ours->delayed_payment,
|
2017-11-15 10:25:19 +01:00
|
|
|
&ours->htlc,
|
2017-06-27 04:55:01 +02:00
|
|
|
&state->next_per_commit[LOCAL],
|
|
|
|
channel_flags);
|
2017-02-21 05:49:02 +01:00
|
|
|
if (!sync_crypto_write(&state->cs, PEER_FD, msg))
|
2018-02-19 02:06:15 +01:00
|
|
|
peer_failed_connection_lost();
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
state->remoteconf = tal(state, struct channel_config);
|
|
|
|
|
2018-02-23 06:53:47 +01:00
|
|
|
peer_billboard(false,
|
|
|
|
"Funding channel: offered, now waiting for accept_channel");
|
2018-01-31 03:53:42 +01:00
|
|
|
msg = opening_read_peer_msg(state);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The receiving node MUST fail the channel if:
|
|
|
|
*...
|
|
|
|
* - `funding_pubkey`, `revocation_basepoint`, `htlc_basepoint`,
|
|
|
|
* `payment_basepoint`, or `delayed_payment_basepoint` are not
|
|
|
|
* valid DER-encoded compressed secp256k1 pubkeys.
|
2017-02-21 05:45:29 +01:00
|
|
|
*/
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_accept_channel(msg, &id_in,
|
2017-02-21 05:45:29 +01:00
|
|
|
&state->remoteconf->dust_limit_satoshis,
|
|
|
|
&state->remoteconf
|
2017-05-23 13:04:17 +02:00
|
|
|
->max_htlc_value_in_flight_msat,
|
2017-02-21 05:45:29 +01:00
|
|
|
&state->remoteconf
|
2017-05-23 13:04:17 +02:00
|
|
|
->channel_reserve_satoshis,
|
2017-02-21 05:45:29 +01:00
|
|
|
&state->remoteconf->htlc_minimum_msat,
|
2017-06-06 01:49:10 +02:00
|
|
|
&minimum_depth,
|
2017-02-21 05:45:29 +01:00
|
|
|
&state->remoteconf->to_self_delay,
|
|
|
|
&state->remoteconf->max_accepted_htlcs,
|
2017-03-07 02:07:06 +01:00
|
|
|
&their_funding_pubkey,
|
|
|
|
&theirs.revocation,
|
|
|
|
&theirs.payment,
|
|
|
|
&theirs.delayed_payment,
|
2017-11-15 10:25:19 +01:00
|
|
|
&theirs.htlc,
|
2017-02-21 05:45:29 +01:00
|
|
|
&state->next_per_commit[REMOTE]))
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&state->cs,
|
2018-02-19 02:06:14 +01:00
|
|
|
&state->channel_id,
|
2017-09-06 03:25:49 +02:00
|
|
|
"Parsing accept_channel %s", tal_hex(msg, msg));
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* The `temporary_channel_id` MUST be the same as the
|
|
|
|
* `temporary_channel_id` in the `open_channel` message. */
|
2017-09-06 03:25:49 +02:00
|
|
|
if (!structeq(&id_in, &state->channel_id))
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&state->cs,
|
2018-02-19 02:06:14 +01:00
|
|
|
&state->channel_id,
|
2017-09-06 03:25:49 +02:00
|
|
|
"accept_channel ids don't match: sent %s got %s",
|
|
|
|
type_to_string(msg, struct channel_id, &id_in),
|
|
|
|
type_to_string(msg, struct channel_id,
|
|
|
|
&state->channel_id));
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-02-24 06:52:56 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The receiver:
|
|
|
|
*...
|
|
|
|
* - if `minimum_depth` is unreasonably large:
|
|
|
|
* - MAY reject the channel.
|
2017-02-24 06:52:56 +01:00
|
|
|
*/
|
2018-05-17 06:46:22 +02:00
|
|
|
if (minimum_depth > 10)
|
2018-02-19 02:06:15 +01:00
|
|
|
negotiation_failed(state,
|
2017-12-06 04:22:35 +01:00
|
|
|
"minimum_depth %u larger than %u",
|
2018-05-17 06:46:22 +02:00
|
|
|
minimum_depth, 10);
|
2018-03-18 05:41:26 +01:00
|
|
|
|
2018-06-17 12:13:44 +02:00
|
|
|
/* FIXME #2:
|
2018-03-18 05:41:26 +01:00
|
|
|
*
|
|
|
|
* The receiver:
|
|
|
|
*...
|
|
|
|
* - if `channel_reserve_satoshis` is less than `dust_limit_satoshis`
|
|
|
|
* within the `open_channel` message:
|
|
|
|
* - MUST reject the channel.
|
|
|
|
*
|
|
|
|
* - if `channel_reserve_satoshis` from the `open_channel` message is
|
|
|
|
* less than `dust_limit_satoshis`:
|
|
|
|
* - MUST reject the channel.
|
|
|
|
*/
|
|
|
|
if (state->remoteconf->channel_reserve_satoshis
|
|
|
|
< state->localconf.dust_limit_satoshis)
|
|
|
|
negotiation_failed(state,
|
2018-03-19 00:25:46 +01:00
|
|
|
"channel reserve %"PRIu64
|
2018-03-18 05:41:26 +01:00
|
|
|
" would be below our dust %"PRIu64,
|
|
|
|
state->remoteconf->channel_reserve_satoshis,
|
|
|
|
state->localconf.dust_limit_satoshis);
|
|
|
|
if (state->localconf.channel_reserve_satoshis
|
|
|
|
< state->remoteconf->dust_limit_satoshis)
|
|
|
|
negotiation_failed(state,
|
2018-03-19 00:25:46 +01:00
|
|
|
"dust limit %"PRIu64
|
2018-03-18 05:41:26 +01:00
|
|
|
" would be above our reserve %"PRIu64,
|
|
|
|
state->remoteconf->dust_limit_satoshis,
|
|
|
|
state->localconf.channel_reserve_satoshis);
|
|
|
|
|
2017-02-24 06:52:56 +01:00
|
|
|
check_config_bounds(state, state->remoteconf);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-05-23 13:07:42 +02:00
|
|
|
/* Now, ask create funding transaction to pay those two addresses. */
|
|
|
|
if (change_satoshis) {
|
2018-03-29 03:59:27 +02:00
|
|
|
changekey = tal(tmpctx, struct pubkey);
|
|
|
|
if (!bip32_pubkey(bip32_base, changekey, change_keyindex))
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO,
|
2017-05-23 13:07:42 +02:00
|
|
|
"Bad change key %u", change_keyindex);
|
2018-03-29 03:59:27 +02:00
|
|
|
} else
|
|
|
|
changekey = NULL;
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-05-23 13:07:42 +02:00
|
|
|
funding = funding_tx(state, &state->funding_txout,
|
2018-02-08 02:24:46 +01:00
|
|
|
cast_const2(const struct utxo **, utxos),
|
|
|
|
state->funding_satoshis,
|
2017-05-23 13:07:42 +02:00
|
|
|
our_funding_pubkey,
|
|
|
|
&their_funding_pubkey,
|
2018-03-29 03:59:27 +02:00
|
|
|
change_satoshis, changekey,
|
2017-08-18 06:43:52 +02:00
|
|
|
bip32_base);
|
2017-05-23 13:07:42 +02:00
|
|
|
bitcoin_txid(funding, &state->funding_txid);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-08-28 18:02:01 +02:00
|
|
|
state->channel = new_initial_channel(state,
|
|
|
|
&state->funding_txid,
|
|
|
|
state->funding_txout,
|
|
|
|
state->funding_satoshis,
|
|
|
|
state->funding_satoshis * 1000
|
|
|
|
- state->push_msat,
|
|
|
|
state->feerate_per_kw,
|
|
|
|
&state->localconf,
|
|
|
|
state->remoteconf,
|
|
|
|
ours, &theirs,
|
|
|
|
our_funding_pubkey,
|
|
|
|
&their_funding_pubkey,
|
|
|
|
LOCAL);
|
2017-02-21 05:45:29 +01:00
|
|
|
if (!state->channel)
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&state->cs,
|
2018-02-19 02:06:14 +01:00
|
|
|
&state->channel_id,
|
2017-09-06 03:25:49 +02:00
|
|
|
"could not create channel with given config");
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* ### The `funding_created` Message
|
2017-02-21 05:45:29 +01:00
|
|
|
*
|
|
|
|
* This message describes the outpoint which the funder has created
|
|
|
|
* for the initial commitment transactions. After receiving the
|
2018-06-17 12:13:44 +02:00
|
|
|
* peer's signature, via `funding_signed`, it will broadcast the funding
|
|
|
|
* transaction.
|
2017-02-21 05:45:29 +01:00
|
|
|
*/
|
2017-08-28 18:02:01 +02:00
|
|
|
tx = initial_channel_tx(state, &wscript, state->channel,
|
|
|
|
&state->next_per_commit[REMOTE], REMOTE);
|
2018-03-18 05:41:32 +01:00
|
|
|
if (!tx)
|
|
|
|
negotiation_failed(state,
|
|
|
|
"Could not meet their fees and reserve");
|
2017-03-29 12:58:15 +02:00
|
|
|
|
2017-08-28 18:02:01 +02:00
|
|
|
sign_tx_input(tx, 0, NULL, wscript,
|
2017-03-29 12:58:15 +02:00
|
|
|
&state->our_secrets.funding_privkey,
|
|
|
|
our_funding_pubkey, &sig);
|
2017-02-24 06:52:56 +01:00
|
|
|
status_trace("signature %s on tx %s using key %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, secp256k1_ecdsa_signature, &sig),
|
|
|
|
type_to_string(tmpctx, struct bitcoin_tx, tx),
|
|
|
|
type_to_string(tmpctx, struct pubkey, our_funding_pubkey));
|
2017-02-24 06:52:56 +01:00
|
|
|
|
2017-09-06 03:25:49 +02:00
|
|
|
msg = towire_funding_created(state, &state->channel_id,
|
2017-12-18 07:41:52 +01:00
|
|
|
&state->funding_txid,
|
2017-02-21 05:45:29 +01:00
|
|
|
state->funding_txout,
|
|
|
|
&sig);
|
2017-02-21 05:49:02 +01:00
|
|
|
if (!sync_crypto_write(&state->cs, PEER_FD, msg))
|
2018-02-19 02:06:15 +01:00
|
|
|
peer_failed_connection_lost();
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* ### The `funding_signed` Message
|
2017-02-21 05:45:29 +01:00
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* This message gives the funder the signature it needs for the first
|
|
|
|
* commitment transaction, so it can broadcast the signature knowing
|
|
|
|
* that funds can be redeemed, if need be.
|
2017-02-21 05:45:29 +01:00
|
|
|
*/
|
2018-02-23 06:53:47 +01:00
|
|
|
peer_billboard(false,
|
|
|
|
"Funding channel: create first tx, now waiting for their signature");
|
|
|
|
|
2018-01-31 03:53:42 +01:00
|
|
|
msg = opening_read_peer_msg(state);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_funding_signed(msg, &id_in, &sig))
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&state->cs,
|
2018-02-19 02:06:14 +01:00
|
|
|
&state->channel_id,
|
2017-09-12 06:55:52 +02:00
|
|
|
"Parsing funding_signed: %s", tal_hex(msg, msg));
|
2017-03-02 13:21:49 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* This message introduces the `channel_id` to identify the channel.
|
|
|
|
* It's derived from the funding transaction by combining the
|
|
|
|
* `funding_txid` and the `funding_output_index`, using big-endian
|
|
|
|
* exclusive-OR (i.e. `funding_output_index` alters the last 2
|
2017-03-02 13:21:49 +01:00
|
|
|
* bytes).
|
|
|
|
*/
|
2017-09-06 03:25:49 +02:00
|
|
|
derive_channel_id(&state->channel_id,
|
2017-03-02 13:21:49 +01:00
|
|
|
&state->funding_txid, state->funding_txout);
|
|
|
|
|
2017-09-06 03:25:49 +02:00
|
|
|
if (!structeq(&id_in, &state->channel_id))
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&state->cs, &id_in,
|
2018-02-13 19:54:03 +01:00
|
|
|
"funding_signed ids don't match: expected %s got %s",
|
2017-09-06 03:25:49 +02:00
|
|
|
type_to_string(msg, struct channel_id,
|
|
|
|
&state->channel_id),
|
|
|
|
type_to_string(msg, struct channel_id, &id_in));
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The recipient:
|
|
|
|
* - if `signature` is incorrect:
|
|
|
|
* - MUST fail the channel.
|
2017-02-21 05:45:29 +01:00
|
|
|
*/
|
2017-08-28 18:02:01 +02:00
|
|
|
tx = initial_channel_tx(state, &wscript, state->channel,
|
|
|
|
&state->next_per_commit[LOCAL], LOCAL);
|
2018-03-18 05:41:32 +01:00
|
|
|
if (!tx)
|
|
|
|
negotiation_failed(state,
|
|
|
|
"Could not meet our fees and reserve");
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-08-28 18:02:01 +02:00
|
|
|
if (!check_tx_sig(tx, 0, NULL, wscript, &their_funding_pubkey, &sig)) {
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&state->cs,
|
2018-02-19 02:06:14 +01:00
|
|
|
&state->channel_id,
|
2017-09-06 03:25:49 +02:00
|
|
|
"Bad signature %s on tx %s using key %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, secp256k1_ecdsa_signature,
|
2017-09-06 03:25:49 +02:00
|
|
|
&sig),
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct bitcoin_tx, tx),
|
|
|
|
type_to_string(tmpctx, struct pubkey,
|
2017-09-06 03:25:49 +02:00
|
|
|
&their_funding_pubkey));
|
2017-03-29 12:58:15 +02:00
|
|
|
}
|
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The recipient:
|
|
|
|
*...
|
|
|
|
* - on receipt of a valid `funding_signed`:
|
|
|
|
* - SHOULD broadcast the funding transaction.
|
2017-02-21 05:45:29 +01:00
|
|
|
*/
|
2017-05-23 13:07:42 +02:00
|
|
|
return towire_opening_funder_reply(state,
|
|
|
|
state->remoteconf,
|
2017-08-28 18:02:01 +02:00
|
|
|
tx,
|
2017-05-23 13:07:42 +02:00
|
|
|
&sig,
|
2018-04-26 06:51:01 +02:00
|
|
|
&state->cs,
|
2017-05-23 13:07:42 +02:00
|
|
|
&theirs.revocation,
|
|
|
|
&theirs.payment,
|
2017-11-15 07:21:39 +01:00
|
|
|
&theirs.htlc,
|
2017-05-23 13:07:42 +02:00
|
|
|
&theirs.delayed_payment,
|
|
|
|
&state->next_per_commit[REMOTE],
|
|
|
|
minimum_depth,
|
|
|
|
&their_funding_pubkey,
|
2017-06-29 18:45:47 +02:00
|
|
|
&state->funding_txid,
|
2017-10-11 06:31:57 +02:00
|
|
|
state->feerate_per_kw);
|
2017-02-21 05:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is handed the message the peer sent which caused gossip to stop:
|
|
|
|
* it should be an open_channel */
|
2017-05-23 13:04:17 +02:00
|
|
|
static u8 *fundee_channel(struct state *state,
|
|
|
|
const struct pubkey *our_funding_pubkey,
|
|
|
|
const struct basepoints *ours,
|
|
|
|
u32 minimum_depth,
|
|
|
|
u32 min_feerate, u32 max_feerate, const u8 *peer_msg)
|
2017-02-21 05:45:29 +01:00
|
|
|
{
|
2017-09-06 03:25:49 +02:00
|
|
|
struct channel_id id_in;
|
2017-03-07 02:07:06 +01:00
|
|
|
struct basepoints theirs;
|
|
|
|
struct pubkey their_funding_pubkey;
|
2017-02-21 05:45:29 +01:00
|
|
|
secp256k1_ecdsa_signature theirsig, sig;
|
2017-09-26 23:02:48 +02:00
|
|
|
struct bitcoin_tx *their_commit, *our_commit;
|
2017-12-18 07:44:10 +01:00
|
|
|
struct bitcoin_blkid chain_hash;
|
2017-05-24 12:10:17 +02:00
|
|
|
u8 *msg;
|
2017-08-28 18:02:01 +02:00
|
|
|
const u8 *wscript;
|
2017-06-27 04:55:01 +02:00
|
|
|
u8 channel_flags;
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
state->remoteconf = tal(state, struct channel_config);
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The receiving node MUST fail the channel if:
|
|
|
|
*...
|
|
|
|
* - `funding_pubkey`, `revocation_basepoint`, `htlc_basepoint`,
|
|
|
|
* `payment_basepoint`, or `delayed_payment_basepoint` are not valid
|
|
|
|
* DER-encoded compressed secp256k1 pubkeys.
|
2017-02-21 05:45:29 +01:00
|
|
|
*/
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_open_channel(peer_msg, &chain_hash,
|
2017-09-06 03:25:49 +02:00
|
|
|
&state->channel_id,
|
2017-02-21 05:45:29 +01:00
|
|
|
&state->funding_satoshis, &state->push_msat,
|
|
|
|
&state->remoteconf->dust_limit_satoshis,
|
|
|
|
&state->remoteconf->max_htlc_value_in_flight_msat,
|
|
|
|
&state->remoteconf->channel_reserve_satoshis,
|
|
|
|
&state->remoteconf->htlc_minimum_msat,
|
|
|
|
&state->feerate_per_kw,
|
|
|
|
&state->remoteconf->to_self_delay,
|
|
|
|
&state->remoteconf->max_accepted_htlcs,
|
2017-03-07 02:07:06 +01:00
|
|
|
&their_funding_pubkey,
|
|
|
|
&theirs.revocation,
|
|
|
|
&theirs.payment,
|
|
|
|
&theirs.delayed_payment,
|
2017-11-15 10:25:19 +01:00
|
|
|
&theirs.htlc,
|
2017-06-27 04:55:01 +02:00
|
|
|
&state->next_per_commit[REMOTE],
|
|
|
|
&channel_flags))
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&state->cs, NULL,
|
2017-09-12 06:55:52 +02:00
|
|
|
"Bad open_channel %s",
|
2017-09-06 03:25:49 +02:00
|
|
|
tal_hex(peer_msg, peer_msg));
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-03-29 12:54:15 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The receiver:
|
|
|
|
* - if the `chain_hash` value, within the `open_channel`, message is
|
|
|
|
* set to a hash of a chain that is unknown to the receiver:
|
|
|
|
* - MUST reject the channel.
|
2017-03-29 12:54:15 +02:00
|
|
|
*/
|
2017-07-11 15:50:10 +02:00
|
|
|
if (!structeq(&chain_hash, &state->chainparams->genesis_blockhash)) {
|
2018-02-19 02:06:15 +01:00
|
|
|
negotiation_failed(state,
|
2017-12-06 04:22:35 +01:00
|
|
|
"Unknown chain-hash %s",
|
|
|
|
type_to_string(peer_msg,
|
2017-12-18 07:44:10 +01:00
|
|
|
struct bitcoin_blkid,
|
2017-12-06 04:22:35 +01:00
|
|
|
&chain_hash));
|
2017-03-29 12:54:15 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
/* BOLT #2 FIXME:
|
2017-02-21 05:45:29 +01:00
|
|
|
*
|
|
|
|
* The receiving node ... MUST fail the channel if `funding-satoshis`
|
|
|
|
* is greater than or equal to 2^24 */
|
2018-01-15 05:29:55 +01:00
|
|
|
if (state->funding_satoshis > MAX_FUNDING_SATOSHI)
|
2018-03-19 00:25:46 +01:00
|
|
|
negotiation_failed(state,
|
|
|
|
"funding_satoshis %"PRIu64" too large",
|
|
|
|
state->funding_satoshis);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The receiving node MUST fail the channel if:
|
|
|
|
* - `push_msat` is greater than `funding_satoshis` * 1000.
|
2017-02-21 05:45:29 +01:00
|
|
|
*/
|
|
|
|
if (state->push_msat > state->funding_satoshis * 1000)
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&state->cs,
|
2018-02-19 02:06:14 +01:00
|
|
|
&state->channel_id,
|
2018-03-19 00:25:46 +01:00
|
|
|
"Our push_msat %"PRIu64
|
|
|
|
" would be too large for funding_satoshis %"PRIu64,
|
2017-09-06 03:25:49 +02:00
|
|
|
state->push_msat, state->funding_satoshis);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
/* BOLT #2:
|
2017-02-24 06:52:56 +01:00
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The receiving node MUST fail the channel if:
|
|
|
|
*...
|
|
|
|
* - it considers `feerate_per_kw` too small for timely processing or
|
|
|
|
* unreasonably large.
|
2017-02-24 06:52:56 +01:00
|
|
|
*/
|
|
|
|
if (state->feerate_per_kw < min_feerate)
|
2018-02-19 02:06:15 +01:00
|
|
|
negotiation_failed(state,
|
2017-12-06 04:22:35 +01:00
|
|
|
"feerate_per_kw %u below minimum %u",
|
|
|
|
state->feerate_per_kw, min_feerate);
|
2017-02-24 06:52:56 +01:00
|
|
|
|
|
|
|
if (state->feerate_per_kw > max_feerate)
|
2018-02-19 02:06:15 +01:00
|
|
|
negotiation_failed(state,
|
2017-12-06 04:22:35 +01:00
|
|
|
"feerate_per_kw %u above maximum %u",
|
|
|
|
state->feerate_per_kw, max_feerate);
|
2017-02-24 06:52:56 +01:00
|
|
|
|
2018-03-17 04:03:36 +01:00
|
|
|
set_reserve(state);
|
2018-03-18 05:41:26 +01:00
|
|
|
|
2018-06-17 12:13:44 +02:00
|
|
|
/* FIXME #2:
|
2018-03-18 05:41:26 +01:00
|
|
|
*
|
|
|
|
* The sender:
|
|
|
|
*...
|
|
|
|
* - MUST set `channel_reserve_satoshis` greater than or equal to
|
|
|
|
* `dust_limit_satoshis` from the `open_channel` message.
|
2018-03-18 05:41:30 +01:00
|
|
|
* - MUST set `dust_limit_satoshis` less than or equal to
|
2018-03-18 05:41:26 +01:00
|
|
|
* `channel_reserve_satoshis` from the `open_channel` message.
|
|
|
|
*/
|
|
|
|
if (state->localconf.channel_reserve_satoshis
|
|
|
|
< state->remoteconf->dust_limit_satoshis)
|
|
|
|
negotiation_failed(state,
|
|
|
|
"Our channel reserve %"PRIu64
|
|
|
|
" would be below their dust %"PRIu64,
|
|
|
|
state->localconf.channel_reserve_satoshis,
|
|
|
|
state->remoteconf->dust_limit_satoshis);
|
|
|
|
if (state->localconf.dust_limit_satoshis
|
2018-03-18 05:41:30 +01:00
|
|
|
> state->remoteconf->channel_reserve_satoshis)
|
2018-03-18 05:41:26 +01:00
|
|
|
negotiation_failed(state,
|
|
|
|
"Our dust limit %"PRIu64
|
|
|
|
" would be above their reserve %"PRIu64,
|
|
|
|
state->localconf.dust_limit_satoshis,
|
|
|
|
state->remoteconf->channel_reserve_satoshis);
|
|
|
|
|
2017-02-24 06:52:56 +01:00
|
|
|
check_config_bounds(state, state->remoteconf);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-09-06 03:25:49 +02:00
|
|
|
msg = towire_accept_channel(state, &state->channel_id,
|
2017-02-21 05:49:02 +01:00
|
|
|
state->localconf.dust_limit_satoshis,
|
2017-02-21 05:45:29 +01:00
|
|
|
state->localconf
|
2017-02-21 05:49:02 +01:00
|
|
|
.max_htlc_value_in_flight_msat,
|
|
|
|
state->localconf.channel_reserve_satoshis,
|
|
|
|
state->localconf.htlc_minimum_msat,
|
2017-07-11 20:02:46 +02:00
|
|
|
minimum_depth,
|
2017-02-21 05:49:02 +01:00
|
|
|
state->localconf.to_self_delay,
|
|
|
|
state->localconf.max_accepted_htlcs,
|
2017-03-07 02:07:06 +01:00
|
|
|
our_funding_pubkey,
|
|
|
|
&ours->revocation,
|
|
|
|
&ours->payment,
|
|
|
|
&ours->delayed_payment,
|
2017-11-15 10:25:19 +01:00
|
|
|
&ours->htlc,
|
2017-02-24 06:52:56 +01:00
|
|
|
&state->next_per_commit[LOCAL]);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-06-27 04:55:06 +02:00
|
|
|
if (!sync_crypto_write(&state->cs, PEER_FD, take(msg)))
|
2018-02-19 02:06:15 +01:00
|
|
|
peer_failed_connection_lost();
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2018-02-23 06:53:47 +01:00
|
|
|
peer_billboard(false,
|
|
|
|
"Incoming channel: accepted, now waiting for them to create funding tx");
|
|
|
|
|
2018-01-31 03:53:42 +01:00
|
|
|
msg = opening_read_peer_msg(state);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_funding_created(msg, &id_in,
|
2017-12-18 07:41:52 +01:00
|
|
|
&state->funding_txid,
|
2017-02-21 05:45:29 +01:00
|
|
|
&state->funding_txout,
|
|
|
|
&theirsig))
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&state->cs,
|
2018-02-19 02:06:14 +01:00
|
|
|
&state->channel_id,
|
2017-09-06 03:25:49 +02:00
|
|
|
"Parsing funding_created");
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The `temporary_channel_id` MUST be the same as the
|
|
|
|
* `temporary_channel_id` in the `open_channel` message.
|
|
|
|
*/
|
2017-09-06 03:25:49 +02:00
|
|
|
if (!structeq(&id_in, &state->channel_id))
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&state->cs, &id_in,
|
2017-03-02 13:21:49 +01:00
|
|
|
"funding_created ids don't match: sent %s got %s",
|
2017-09-06 03:25:49 +02:00
|
|
|
type_to_string(msg, struct channel_id,
|
|
|
|
&state->channel_id),
|
2017-03-02 13:21:49 +01:00
|
|
|
type_to_string(msg, struct channel_id, &id_in));
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-08-28 18:02:01 +02:00
|
|
|
state->channel = new_initial_channel(state,
|
|
|
|
&state->funding_txid,
|
|
|
|
state->funding_txout,
|
|
|
|
state->funding_satoshis,
|
|
|
|
state->push_msat,
|
|
|
|
state->feerate_per_kw,
|
|
|
|
&state->localconf,
|
|
|
|
state->remoteconf,
|
|
|
|
ours, &theirs,
|
|
|
|
our_funding_pubkey,
|
|
|
|
&their_funding_pubkey,
|
|
|
|
REMOTE);
|
2017-02-21 05:45:29 +01:00
|
|
|
if (!state->channel)
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&state->cs,
|
2018-02-19 02:06:14 +01:00
|
|
|
&state->channel_id,
|
2018-03-19 00:25:46 +01:00
|
|
|
"We could not create channel with given config");
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The recipient:
|
|
|
|
* - if `signature` is incorrect:
|
|
|
|
* - MUST fail the channel.
|
2017-02-21 05:45:29 +01:00
|
|
|
*/
|
2017-09-26 23:02:48 +02:00
|
|
|
their_commit = initial_channel_tx(state, &wscript, state->channel,
|
|
|
|
&state->next_per_commit[LOCAL], LOCAL);
|
2018-03-18 05:41:32 +01:00
|
|
|
if (!their_commit)
|
|
|
|
negotiation_failed(state,
|
|
|
|
"Could not meet our fees and reserve");
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-09-26 23:02:48 +02:00
|
|
|
if (!check_tx_sig(their_commit, 0, NULL, wscript, &their_funding_pubkey,
|
2017-03-29 12:58:15 +02:00
|
|
|
&theirsig)) {
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&state->cs,
|
2018-02-19 02:06:14 +01:00
|
|
|
&state->channel_id,
|
2017-09-06 03:25:49 +02:00
|
|
|
"Bad signature %s on tx %s using key %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, secp256k1_ecdsa_signature,
|
2017-09-06 03:25:49 +02:00
|
|
|
&theirsig),
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct bitcoin_tx, their_commit),
|
|
|
|
type_to_string(tmpctx, struct pubkey,
|
2017-09-06 03:25:49 +02:00
|
|
|
&their_funding_pubkey));
|
2017-03-29 12:58:15 +02:00
|
|
|
}
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* This message introduces the `channel_id` to identify the
|
|
|
|
* channel. It's derived from the funding transaction by combining the
|
|
|
|
* `funding_txid` and the `funding_output_index`, using big-endian
|
|
|
|
* exclusive-OR (i.e. `funding_output_index` alters the last 2 bytes).
|
2017-03-02 13:21:49 +01:00
|
|
|
*/
|
2017-09-06 03:25:49 +02:00
|
|
|
derive_channel_id(&state->channel_id,
|
2017-03-02 13:21:49 +01:00
|
|
|
&state->funding_txid, state->funding_txout);
|
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* ### The `funding_signed` Message
|
2017-02-21 05:45:29 +01:00
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* This message gives the funder the signature it needs for the first
|
|
|
|
* commitment transaction, so it can broadcast the signature knowing
|
|
|
|
* that funds can be redeemed, if need be.
|
2017-02-21 05:45:29 +01:00
|
|
|
*/
|
2017-09-26 23:02:48 +02:00
|
|
|
our_commit = initial_channel_tx(state, &wscript, state->channel,
|
|
|
|
&state->next_per_commit[REMOTE], REMOTE);
|
2018-03-18 05:41:32 +01:00
|
|
|
if (!our_commit)
|
|
|
|
negotiation_failed(state,
|
|
|
|
"Could not meet their fees and reserve");
|
|
|
|
|
2017-09-26 23:02:48 +02:00
|
|
|
sign_tx_input(our_commit, 0, NULL, wscript,
|
2017-03-29 12:58:15 +02:00
|
|
|
&state->our_secrets.funding_privkey,
|
|
|
|
our_funding_pubkey, &sig);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-05-24 12:10:17 +02:00
|
|
|
/* We don't send this ourselves: channeld does, because master needs
|
|
|
|
* to save state to disk before doing so. */
|
2017-09-06 03:25:49 +02:00
|
|
|
msg = towire_funding_signed(state, &state->channel_id, &sig);
|
2017-05-23 13:07:42 +02:00
|
|
|
|
|
|
|
return towire_opening_fundee_reply(state,
|
|
|
|
state->remoteconf,
|
2017-09-26 23:02:48 +02:00
|
|
|
their_commit,
|
2017-05-23 13:07:42 +02:00
|
|
|
&theirsig,
|
|
|
|
&state->cs,
|
|
|
|
&theirs.revocation,
|
|
|
|
&theirs.payment,
|
2017-11-15 07:21:39 +01:00
|
|
|
&theirs.htlc,
|
2017-05-23 13:07:42 +02:00
|
|
|
&theirs.delayed_payment,
|
|
|
|
&state->next_per_commit[REMOTE],
|
|
|
|
&their_funding_pubkey,
|
|
|
|
&state->funding_txid,
|
|
|
|
state->funding_txout,
|
|
|
|
state->funding_satoshis,
|
|
|
|
state->push_msat,
|
2017-06-27 04:55:01 +02:00
|
|
|
channel_flags,
|
2017-06-29 18:45:47 +02:00
|
|
|
state->feerate_per_kw,
|
2017-10-11 06:31:57 +02:00
|
|
|
msg);
|
2017-02-21 05:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TESTING
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-04-25 12:55:34 +02:00
|
|
|
setup_locale();
|
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
u8 *msg, *peer_msg;
|
|
|
|
struct state *state = tal(NULL, struct state);
|
2017-02-24 06:52:56 +01:00
|
|
|
struct privkey seed;
|
2017-03-07 02:07:06 +01:00
|
|
|
struct basepoints our_points;
|
|
|
|
struct pubkey our_funding_pubkey;
|
2018-05-17 06:46:22 +02:00
|
|
|
u32 minimum_depth;
|
2017-02-24 06:52:56 +01:00
|
|
|
u32 min_feerate, max_feerate;
|
2017-05-23 13:07:42 +02:00
|
|
|
u64 change_satoshis;
|
|
|
|
u32 change_keyindex;
|
2017-06-27 04:55:01 +02:00
|
|
|
u8 channel_flags;
|
2018-02-08 02:24:46 +01:00
|
|
|
struct utxo **utxos;
|
2017-08-18 06:43:52 +02:00
|
|
|
struct ext_key bip32_base;
|
2017-07-10 13:35:43 +02:00
|
|
|
u32 network_index;
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2018-01-08 11:01:09 +01:00
|
|
|
subdaemon_setup(argc, argv);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-03-19 21:32:44 +01:00
|
|
|
status_setup_sync(REQ_FD);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
msg = wire_sync_read(state, REQ_FD);
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_opening_init(msg,
|
2017-07-10 13:35:43 +02:00
|
|
|
&network_index,
|
2017-02-21 05:45:29 +01:00
|
|
|
&state->localconf,
|
2017-02-24 06:52:56 +01:00
|
|
|
&state->max_to_self_delay,
|
|
|
|
&state->min_effective_htlc_capacity_msat,
|
2017-02-21 05:49:02 +01:00
|
|
|
&state->cs,
|
2017-10-11 06:31:57 +02:00
|
|
|
&seed))
|
2017-09-12 06:55:52 +02:00
|
|
|
master_badmsg(WIRE_OPENING_INIT, msg);
|
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
tal_free(msg);
|
|
|
|
|
2017-07-11 15:50:10 +02:00
|
|
|
state->chainparams = chainparams_by_index(network_index);
|
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
/* We derive everything from the one secret seed. */
|
2017-03-07 02:07:06 +01:00
|
|
|
if (!derive_basepoints(&seed, &our_funding_pubkey,
|
|
|
|
&our_points, &state->our_secrets,
|
2017-06-20 08:10:03 +02:00
|
|
|
&state->shaseed))
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-03-07 02:07:06 +01:00
|
|
|
"Secret derivation failed, secret = %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct privkey, &seed));
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-06-20 08:10:03 +02:00
|
|
|
if (!per_commit_point(&state->shaseed, &state->next_per_commit[LOCAL],
|
|
|
|
0))
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-06-20 08:10:03 +02:00
|
|
|
"First per_commitment_point derivation failed,"
|
|
|
|
" secret = %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct privkey, &seed));
|
2017-06-20 08:10:03 +02:00
|
|
|
|
2017-04-01 12:58:30 +02:00
|
|
|
status_trace("First per_commit_point = %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey,
|
2017-04-01 12:58:30 +02:00
|
|
|
&state->next_per_commit[LOCAL]));
|
2017-02-21 05:45:29 +01:00
|
|
|
msg = wire_sync_read(state, REQ_FD);
|
2018-02-20 21:59:09 +01:00
|
|
|
if (fromwire_opening_funder(state, msg,
|
2017-05-23 13:04:17 +02:00
|
|
|
&state->funding_satoshis,
|
|
|
|
&state->push_msat,
|
2018-05-17 06:46:22 +02:00
|
|
|
&state->feerate_per_kw,
|
2017-05-23 13:07:42 +02:00
|
|
|
&change_satoshis, &change_keyindex,
|
2018-02-23 06:53:47 +01:00
|
|
|
&channel_flags, &utxos, &bip32_base)) {
|
2017-05-23 13:04:17 +02:00
|
|
|
msg = funder_channel(state, &our_funding_pubkey, &our_points,
|
2018-05-17 06:46:22 +02:00
|
|
|
change_satoshis,
|
2017-10-11 06:31:57 +02:00
|
|
|
change_keyindex, channel_flags,
|
2017-08-18 06:43:52 +02:00
|
|
|
utxos, &bip32_base);
|
2018-02-23 06:53:47 +01:00
|
|
|
peer_billboard(false,
|
|
|
|
"Funding channel: opening negotiation succeeded");
|
|
|
|
} else if (fromwire_opening_fundee(state, msg, &minimum_depth,
|
|
|
|
&min_feerate, &max_feerate, &peer_msg)) {
|
2017-05-23 13:04:17 +02:00
|
|
|
msg = fundee_channel(state, &our_funding_pubkey, &our_points,
|
2017-10-11 06:31:57 +02:00
|
|
|
minimum_depth, min_feerate, max_feerate,
|
|
|
|
peer_msg);
|
2018-02-23 06:53:47 +01:00
|
|
|
peer_billboard(false,
|
|
|
|
"Incoming channel: opening negotiation succeeded");
|
|
|
|
} else
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO,
|
|
|
|
"neither funder nor fundee: %s",
|
|
|
|
tal_hex(msg, msg));
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-05-24 12:07:57 +02:00
|
|
|
/* Write message and hand back the fd. */
|
2017-02-24 06:52:56 +01:00
|
|
|
wire_sync_write(REQ_FD, msg);
|
2017-02-21 05:45:29 +01:00
|
|
|
fdpass_send(REQ_FD, PEER_FD);
|
2017-06-24 08:50:23 +02:00
|
|
|
fdpass_send(REQ_FD, GOSSIP_FD);
|
2017-05-24 12:07:57 +02:00
|
|
|
status_trace("Sent %s with fd",
|
2017-03-10 11:56:53 +01:00
|
|
|
opening_wire_type_name(fromwire_peektype(msg)));
|
2017-02-21 05:45:29 +01:00
|
|
|
tal_free(state);
|
2018-03-29 04:06:45 +02:00
|
|
|
daemon_shutdown();
|
2017-02-21 05:45:29 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* TESTING */
|