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>
|
|
|
|
#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-12-11 04:33:16 +01:00
|
|
|
u64 gossip_index;
|
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
|
|
|
};
|
|
|
|
|
2017-12-06 04:22:35 +01:00
|
|
|
/* For negotiation failures: we can still gossip with client. */
|
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;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
errmsg = tal_vfmt(state, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2018-02-19 02:06:15 +01:00
|
|
|
peer_failed(&state->cs, state->gossip_index, &state->channel_id,
|
|
|
|
"%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:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* The receiving node MUST fail the channel if `to_self_delay` is
|
2017-02-24 06:52:56 +01:00
|
|
|
* 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:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* The receiver MAY fail the channel if `funding_satoshis` is too
|
|
|
|
* small, and MUST fail the channel if `push_msat` is greater than
|
|
|
|
* `funding_satoshis` * 1000. The receiving node MAY fail the channel
|
|
|
|
* if it considers `htlc_minimum_msat` too large,
|
|
|
|
* `max_htlc_value_in_flight_msat` too small, `channel_reserve_satoshis`
|
|
|
|
* too large, or `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,
|
2017-12-06 04:22:35 +01:00
|
|
|
"Invalid channel_reserve_satoshis %"PRIu64
|
|
|
|
" for funding_satoshis %"PRIu64,
|
|
|
|
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,
|
2017-12-06 04:22:35 +01:00
|
|
|
"Invalid htlc_minimum_msat %"PRIu64
|
|
|
|
" for funding_satoshis %"PRIu64
|
|
|
|
" 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,
|
2017-12-06 04:22:35 +01:00
|
|
|
"Channel capacity with funding %"PRIu64" msat,"
|
|
|
|
" 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
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* It MUST fail the channel if `max_accepted_htlcs` is greater
|
2017-03-07 06:46:59 +01:00
|
|
|
* 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-02-19 02:06:14 +01:00
|
|
|
peer_failed(&state->cs, state->gossip_index,
|
|
|
|
&state->channel_id,
|
2017-02-24 06:52:56 +01:00
|
|
|
"max_accepted_htlcs %u too large",
|
|
|
|
remoteconf->max_accepted_htlcs);
|
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. */
|
|
|
|
static void set_reserve(u64 *reserve, u64 funding)
|
|
|
|
{
|
|
|
|
*reserve = (funding + 99) / 100;
|
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* A sending node MUST ensure `temporary_channel_id` is unique from any other
|
2017-03-02 13:21:49 +01:00
|
|
|
* channel id with the same peer.
|
|
|
|
*/
|
|
|
|
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-02-19 02:06:15 +01:00
|
|
|
while ((msg = read_peer_msg(state, &state->cs, state->gossip_index,
|
|
|
|
&state->channel_id,
|
2018-01-31 03:53:42 +01:00
|
|
|
sync_crypto_write_arg,
|
|
|
|
status_fail_io,
|
|
|
|
state)) == NULL);
|
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
|
|
|
u32 max_minimum_depth,
|
|
|
|
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;
|
2017-05-23 13:07:42 +02:00
|
|
|
struct pubkey their_funding_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;
|
|
|
|
|
2017-02-24 06:52:56 +01:00
|
|
|
set_reserve(&state->localconf.channel_reserve_satoshis,
|
|
|
|
state->funding_satoshis);
|
|
|
|
|
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:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* The sender MUST set `push_msat` to equal or less than to 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:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* The receiver MUST fail the channel if `funding_pubkey`,
|
2017-11-15 07:18:39 +01:00
|
|
|
* `revocation_basepoint`, `htlc_basepoint`, `payment_basepoint` or
|
2017-06-06 01:48:10 +02:00
|
|
|
* `delayed_payment_basepoint` are not valid DER-encoded compressed
|
2017-02-21 05:45:29 +01:00
|
|
|
* secp256k1 pubkeys.
|
|
|
|
*/
|
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-02-19 02:06:14 +01:00
|
|
|
peer_failed(&state->cs, state->gossip_index,
|
|
|
|
&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-02-19 02:06:14 +01:00
|
|
|
peer_failed(&state->cs, state->gossip_index,
|
|
|
|
&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:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* The receiver MAY reject the `minimum_depth` if it considers it
|
2017-02-24 06:52:56 +01:00
|
|
|
* unreasonably large.
|
|
|
|
*
|
|
|
|
* Other fields have the same requirements as their counterparts in
|
|
|
|
* `open_channel`.
|
|
|
|
*/
|
2017-04-12 08:52:57 +02:00
|
|
|
if (minimum_depth > max_minimum_depth)
|
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",
|
|
|
|
minimum_depth, max_minimum_depth);
|
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) {
|
2017-08-18 06:43:52 +02:00
|
|
|
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);
|
|
|
|
}
|
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,
|
|
|
|
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-02-19 02:06:14 +01:00
|
|
|
peer_failed(&state->cs, state->gossip_index,
|
|
|
|
&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:
|
|
|
|
*
|
|
|
|
* ### The `funding_created` message
|
|
|
|
*
|
|
|
|
* This message describes the outpoint which the funder has created
|
|
|
|
* for the initial commitment transactions. After receiving the
|
|
|
|
* peer's signature, it will broadcast the funding transaction.
|
|
|
|
*/
|
2017-08-28 18:02:01 +02:00
|
|
|
tx = initial_channel_tx(state, &wscript, state->channel,
|
|
|
|
&state->next_per_commit[REMOTE], REMOTE);
|
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",
|
|
|
|
type_to_string(trc, secp256k1_ecdsa_signature, &sig),
|
2017-08-28 18:02:01 +02:00
|
|
|
type_to_string(trc, struct bitcoin_tx, tx),
|
2017-03-07 02:07:06 +01:00
|
|
|
type_to_string(trc, 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:
|
|
|
|
*
|
|
|
|
* ### The `funding_signed` message
|
|
|
|
*
|
|
|
|
* This message gives the funder the signature they need for the first
|
|
|
|
* commitment transaction, so they can broadcast it knowing they can
|
|
|
|
* redeem their funds if they need to.
|
|
|
|
*/
|
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-02-19 02:06:14 +01:00
|
|
|
peer_failed(&state->cs, state->gossip_index,
|
|
|
|
&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:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* This message introduces the `channel_id` to identify the channel, which
|
2017-03-02 13:21:49 +01:00
|
|
|
* is derived from the funding transaction by combining the
|
2017-06-06 01:48:10 +02:00
|
|
|
* `funding_txid` and the `funding_output_index` using big-endian
|
|
|
|
* exclusive-OR (ie. `funding_output_index` alters the last two
|
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-02-19 02:06:14 +01:00
|
|
|
peer_failed(&state->cs, state->gossip_index, &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:
|
|
|
|
*
|
|
|
|
* The recipient MUST fail the channel if `signature` is incorrect.
|
|
|
|
*/
|
2017-08-28 18:02:01 +02:00
|
|
|
tx = initial_channel_tx(state, &wscript, state->channel,
|
|
|
|
&state->next_per_commit[LOCAL], LOCAL);
|
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-02-19 02:06:14 +01:00
|
|
|
peer_failed(&state->cs, state->gossip_index,
|
|
|
|
&state->channel_id,
|
2017-09-06 03:25:49 +02:00
|
|
|
"Bad signature %s on tx %s using key %s",
|
|
|
|
type_to_string(trc, secp256k1_ecdsa_signature,
|
|
|
|
&sig),
|
|
|
|
type_to_string(trc, struct bitcoin_tx, tx),
|
|
|
|
type_to_string(trc, struct pubkey,
|
|
|
|
&their_funding_pubkey));
|
2017-03-29 12:58:15 +02:00
|
|
|
}
|
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* Once the channel funder receives the `funding_signed` message, they
|
|
|
|
* must broadcast the funding transaction to the Bitcoin network.
|
|
|
|
*/
|
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,
|
2017-12-11 04:33:16 +01:00
|
|
|
&state->cs, state->gossip_index,
|
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:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* The receiver MUST fail the channel if `funding_pubkey`,
|
2017-11-15 07:18:39 +01:00
|
|
|
* `revocation_basepoint`, `htlc_basepoint`, `payment_basepoint` or
|
2017-06-06 01:48:10 +02:00
|
|
|
* `delayed_payment_basepoint` are not valid DER-encoded compressed
|
2017-02-21 05:45:29 +01:00
|
|
|
* secp256k1 pubkeys.
|
|
|
|
*/
|
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-02-19 02:06:14 +01:00
|
|
|
peer_failed(&state->cs, state->gossip_index, 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:
|
|
|
|
*
|
2017-10-09 03:22:13 +02:00
|
|
|
* The receiving node MUST reject the channel if the `chain_hash` value
|
2017-03-29 12:54:15 +02:00
|
|
|
* within the `open_channel` message is set to a hash of a chain
|
|
|
|
* unknown to the receiver.
|
|
|
|
*/
|
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-02-19 02:06:14 +01:00
|
|
|
peer_failed(&state->cs, state->gossip_index,
|
|
|
|
&state->channel_id,
|
2017-09-06 03:25:49 +02:00
|
|
|
"funding_satoshis %"PRIu64" too large",
|
|
|
|
state->funding_satoshis);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2017-06-06 01:48:10 +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-02-19 02:06:14 +01:00
|
|
|
peer_failed(&state->cs, state->gossip_index,
|
|
|
|
&state->channel_id,
|
2017-09-06 03:25:49 +02:00
|
|
|
"push_msat %"PRIu64
|
|
|
|
" too large for funding_satoshis %"PRIu64,
|
|
|
|
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
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* The receiver MUST fail the channel if it considers `feerate_per_kw`
|
2017-02-24 06:52:56 +01:00
|
|
|
* too small for timely processing, or unreasonably large.
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
set_reserve(&state->localconf.channel_reserve_satoshis,
|
|
|
|
state->funding_satoshis);
|
|
|
|
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-02-19 02:06:14 +01:00
|
|
|
peer_failed(&state->cs, state->gossip_index,
|
|
|
|
&state->channel_id,
|
2017-09-06 03:25:49 +02:00
|
|
|
"Parsing funding_created");
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* The sender MUST set `temporary_channel_id` 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-02-19 02:06:14 +01:00
|
|
|
peer_failed(&state->cs, state->gossip_index, &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-02-19 02:06:14 +01:00
|
|
|
peer_failed(&state->cs, state->gossip_index,
|
|
|
|
&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:
|
|
|
|
*
|
|
|
|
* The recipient MUST fail the channel if `signature` is incorrect.
|
|
|
|
*/
|
2017-09-26 23:02:48 +02:00
|
|
|
their_commit = initial_channel_tx(state, &wscript, state->channel,
|
|
|
|
&state->next_per_commit[LOCAL], LOCAL);
|
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-02-19 02:06:14 +01:00
|
|
|
peer_failed(&state->cs, state->gossip_index,
|
|
|
|
&state->channel_id,
|
2017-09-06 03:25:49 +02:00
|
|
|
"Bad signature %s on tx %s using key %s",
|
|
|
|
type_to_string(trc, secp256k1_ecdsa_signature,
|
|
|
|
&theirsig),
|
2017-09-26 23:02:48 +02:00
|
|
|
type_to_string(trc, struct bitcoin_tx, their_commit),
|
2017-09-06 03:25:49 +02:00
|
|
|
type_to_string(trc, struct pubkey,
|
|
|
|
&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:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* This message introduces the `channel_id` to identify the channel,
|
2017-04-27 06:18:40 +02:00
|
|
|
* which is derived from the funding transaction by combining the
|
2017-06-06 01:48:10 +02:00
|
|
|
* `funding_txid` and the `funding_output_index` using big-endian
|
|
|
|
* exclusive-OR (ie. `funding_output_index` alters the last two
|
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-02-21 05:45:29 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* ### The `funding_signed` message
|
|
|
|
*
|
|
|
|
* This message gives the funder the signature they need for the first
|
|
|
|
* commitment transaction, so they can broadcast it knowing they can
|
|
|
|
* redeem their funds if they need to.
|
|
|
|
*/
|
2017-09-26 23:02:48 +02:00
|
|
|
our_commit = initial_channel_tx(state, &wscript, state->channel,
|
|
|
|
&state->next_per_commit[REMOTE], REMOTE);
|
|
|
|
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,
|
2017-12-11 04:33:16 +01:00
|
|
|
state->gossip_index,
|
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],
|
|
|
|
&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[])
|
|
|
|
{
|
|
|
|
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;
|
2017-04-12 08:52:57 +02:00
|
|
|
u32 minimum_depth, max_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-12-11 04:33:16 +01:00
|
|
|
&state->gossip_index,
|
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",
|
|
|
|
type_to_string(trc, 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",
|
|
|
|
type_to_string(trc, struct privkey, &seed));
|
|
|
|
|
2017-04-01 12:58:30 +02:00
|
|
|
status_trace("First per_commit_point = %s",
|
|
|
|
type_to_string(trc, struct pubkey,
|
|
|
|
&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,
|
2017-05-23 13:07:42 +02:00
|
|
|
&state->feerate_per_kw, &max_minimum_depth,
|
|
|
|
&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,
|
2017-10-11 06:31:57 +02:00
|
|
|
max_minimum_depth, change_satoshis,
|
|
|
|
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);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* TESTING */
|