2017-03-07 02:26:12 +01:00
|
|
|
#include <bitcoin/privkey.h>
|
|
|
|
#include <bitcoin/script.h>
|
2017-03-10 14:17:23 +01:00
|
|
|
#include <ccan/container_of/container_of.h>
|
2017-03-07 02:26:12 +01:00
|
|
|
#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h>
|
|
|
|
#include <ccan/crypto/shachain/shachain.h>
|
|
|
|
#include <ccan/fdpass/fdpass.h>
|
|
|
|
#include <ccan/io/io.h>
|
|
|
|
#include <ccan/structeq/structeq.h>
|
2017-03-11 21:49:52 +01:00
|
|
|
#include <ccan/take/take.h>
|
|
|
|
#include <ccan/time/time.h>
|
2017-03-07 02:26:12 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <lightningd/channel.h>
|
2017-03-10 11:56:58 +01:00
|
|
|
#include <lightningd/channel/gen_channel_wire.h>
|
2017-03-07 02:26:12 +01:00
|
|
|
#include <lightningd/commit_tx.h>
|
|
|
|
#include <lightningd/crypto_sync.h>
|
|
|
|
#include <lightningd/cryptomsg.h>
|
2017-03-19 21:32:44 +01:00
|
|
|
#include <lightningd/daemon_conn.h>
|
2017-03-07 02:26:12 +01:00
|
|
|
#include <lightningd/debug.h>
|
|
|
|
#include <lightningd/derive_basepoints.h>
|
|
|
|
#include <lightningd/key_derive.h>
|
2017-03-13 17:24:05 +01:00
|
|
|
#include <lightningd/msg_queue.h>
|
2017-03-07 02:26:12 +01:00
|
|
|
#include <lightningd/peer_failed.h>
|
2017-03-19 21:24:12 +01:00
|
|
|
#include <lightningd/status.h>
|
2017-03-07 02:26:12 +01:00
|
|
|
#include <secp256k1.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <type_to_string.h>
|
|
|
|
#include <version.h>
|
|
|
|
#include <wire/gen_peer_wire.h>
|
|
|
|
#include <wire/wire.h>
|
|
|
|
#include <wire/wire_io.h>
|
|
|
|
#include <wire/wire_sync.h>
|
|
|
|
|
2017-03-19 21:32:44 +01:00
|
|
|
/* stdin == requests, 3 == peer, 4 = gossip */
|
2017-03-07 02:26:12 +01:00
|
|
|
#define REQ_FD STDIN_FILENO
|
|
|
|
#define PEER_FD 3
|
2017-03-19 21:32:44 +01:00
|
|
|
#define GOSSIP_FD 4
|
2017-03-07 02:26:12 +01:00
|
|
|
|
|
|
|
struct peer {
|
|
|
|
struct peer_crypto_state pcs;
|
|
|
|
struct channel_config conf[NUM_SIDES];
|
|
|
|
struct pubkey next_per_commit[NUM_SIDES];
|
|
|
|
bool funding_locked[NUM_SIDES];
|
2017-03-11 17:16:17 +01:00
|
|
|
struct pubkey funding_pubkey[NUM_SIDES];
|
2017-03-07 02:26:12 +01:00
|
|
|
|
|
|
|
/* Their sig for current commit. */
|
|
|
|
secp256k1_ecdsa_signature their_commit_sig;
|
|
|
|
|
|
|
|
/* Secret keys and basepoint secrets. */
|
|
|
|
struct secrets our_secrets;
|
|
|
|
|
|
|
|
/* Our shaseed for generating per-commitment-secrets. */
|
|
|
|
struct sha256 shaseed;
|
|
|
|
|
|
|
|
struct channel_id channel_id;
|
|
|
|
struct channel *channel;
|
|
|
|
|
2017-03-13 17:24:05 +01:00
|
|
|
struct msg_queue peer_out;
|
2017-03-09 16:08:57 +01:00
|
|
|
|
2017-03-10 14:17:23 +01:00
|
|
|
struct daemon_conn gossip_client;
|
2017-03-19 21:32:44 +01:00
|
|
|
struct daemon_conn master;
|
2017-03-11 21:49:52 +01:00
|
|
|
|
|
|
|
/* Announcement related information */
|
2017-03-11 17:16:17 +01:00
|
|
|
struct pubkey node_ids[NUM_SIDES];
|
2017-03-11 21:49:52 +01:00
|
|
|
struct short_channel_id short_channel_ids[NUM_SIDES];
|
|
|
|
secp256k1_ecdsa_signature announcement_node_sigs[NUM_SIDES];
|
|
|
|
secp256k1_ecdsa_signature announcement_bitcoin_sigs[NUM_SIDES];
|
2017-03-07 02:26:12 +01:00
|
|
|
};
|
|
|
|
|
2017-03-10 14:17:23 +01:00
|
|
|
static struct io_plan *gossip_client_recv(struct io_conn *conn,
|
|
|
|
struct daemon_conn *dc)
|
|
|
|
{
|
|
|
|
u8 *msg = dc->msg_in;
|
|
|
|
struct peer *peer = container_of(dc, struct peer, gossip_client);
|
|
|
|
u16 type = fromwire_peektype(msg);
|
|
|
|
|
|
|
|
if (type == WIRE_CHANNEL_ANNOUNCEMENT || type == WIRE_CHANNEL_UPDATE ||
|
|
|
|
type == WIRE_NODE_ANNOUNCEMENT)
|
2017-03-19 21:32:44 +01:00
|
|
|
msg_enqueue(&peer->peer_out, msg);
|
2017-03-10 14:17:23 +01:00
|
|
|
|
|
|
|
return daemon_conn_read_next(conn, dc);
|
|
|
|
}
|
|
|
|
|
2017-03-11 21:49:52 +01:00
|
|
|
static void send_announcement_signatures(struct peer *peer)
|
|
|
|
{
|
|
|
|
tal_t *tmpctx = tal_tmpctx(peer);
|
|
|
|
u8 *msg;
|
|
|
|
// TODO(cdecker) Use the HSM to generate this signature
|
|
|
|
secp256k1_ecdsa_signature *sig =
|
|
|
|
talz(tmpctx, secp256k1_ecdsa_signature);
|
|
|
|
|
|
|
|
msg = towire_announcement_signatures(tmpctx, &peer->channel_id,
|
|
|
|
&peer->short_channel_ids[LOCAL],
|
|
|
|
sig, sig);
|
2017-03-19 21:32:44 +01:00
|
|
|
msg_enqueue(&peer->peer_out, take(msg));
|
2017-03-11 21:49:52 +01:00
|
|
|
tal_free(tmpctx);
|
|
|
|
}
|
|
|
|
|
2017-03-21 21:21:17 +01:00
|
|
|
/* The direction bit is 0 if our local node-id is lexicographically
|
|
|
|
* smaller than the remote node-id. */
|
|
|
|
static int get_direction_bit(struct peer *peer)
|
2017-03-11 21:49:52 +01:00
|
|
|
{
|
|
|
|
u8 local_der[33], remote_der[33];
|
|
|
|
/* Find out in which order we have to list the endpoints */
|
|
|
|
pubkey_to_der(local_der, &peer->node_ids[LOCAL]);
|
|
|
|
pubkey_to_der(remote_der, &peer->node_ids[REMOTE]);
|
2017-03-21 21:21:17 +01:00
|
|
|
return memcmp(local_der, remote_der, sizeof(local_der)) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void send_channel_update(struct peer *peer, bool disabled)
|
|
|
|
{
|
|
|
|
tal_t *tmpctx = tal_tmpctx(peer);
|
|
|
|
u32 timestamp = time_now().ts.tv_sec;
|
|
|
|
u16 flags;
|
|
|
|
u8 *cupdate;
|
|
|
|
// TODO(cdecker) Create a real signature for this update
|
|
|
|
secp256k1_ecdsa_signature *sig =
|
|
|
|
talz(tmpctx, secp256k1_ecdsa_signature);
|
|
|
|
|
|
|
|
flags = get_direction_bit(peer) | (disabled << 1);
|
|
|
|
cupdate = towire_channel_update(
|
|
|
|
tmpctx, sig, &peer->short_channel_ids[LOCAL], timestamp, flags, 36,
|
|
|
|
1, 10, peer->channel->view[LOCAL].feerate_per_kw);
|
|
|
|
|
|
|
|
daemon_conn_send(&peer->gossip_client, take(cupdate));
|
|
|
|
|
|
|
|
msg_enqueue(&peer->peer_out, cupdate);
|
|
|
|
tal_free(tmpctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now that we have a working channel, tell the world. */
|
|
|
|
static void send_channel_announcement(struct peer *peer)
|
|
|
|
{
|
|
|
|
tal_t *tmpctx = tal_tmpctx(peer);
|
|
|
|
int first, second;
|
|
|
|
u8 *cannounce, *features = tal_arr(peer, u8, 0);
|
|
|
|
|
|
|
|
if (get_direction_bit(peer) == 1) {
|
2017-03-11 21:49:52 +01:00
|
|
|
first = LOCAL;
|
|
|
|
second = REMOTE;
|
|
|
|
} else {
|
|
|
|
first = REMOTE;
|
|
|
|
second = LOCAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cannounce = towire_channel_announcement(
|
|
|
|
tmpctx, &peer->announcement_node_sigs[first],
|
|
|
|
&peer->announcement_node_sigs[second],
|
|
|
|
&peer->announcement_bitcoin_sigs[first],
|
|
|
|
&peer->announcement_bitcoin_sigs[second],
|
|
|
|
&peer->short_channel_ids[LOCAL], &peer->node_ids[first],
|
|
|
|
&peer->node_ids[second], &peer->funding_pubkey[first],
|
|
|
|
&peer->funding_pubkey[second], features);
|
|
|
|
|
2017-03-19 21:32:44 +01:00
|
|
|
msg_enqueue(&peer->peer_out, cannounce);
|
2017-03-11 21:49:52 +01:00
|
|
|
daemon_conn_send(&peer->gossip_client, take(cannounce));
|
|
|
|
tal_free(tmpctx);
|
|
|
|
}
|
|
|
|
|
2017-03-07 02:26:12 +01:00
|
|
|
static struct io_plan *peer_out(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
|
|
|
const u8 *out = msg_dequeue(&peer->peer_out);
|
|
|
|
if (!out)
|
2017-03-19 21:32:44 +01:00
|
|
|
return msg_queue_wait(conn, &peer->peer_out, peer_out, peer);
|
2017-03-07 02:26:12 +01:00
|
|
|
|
|
|
|
return peer_write_message(conn, &peer->pcs, out, peer_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *peer_in(struct io_conn *conn, struct peer *peer, u8 *msg)
|
|
|
|
{
|
|
|
|
struct channel_id chanid;
|
2017-03-10 15:11:54 +01:00
|
|
|
int type = fromwire_peektype(msg);
|
|
|
|
|
2017-03-07 02:26:12 +01:00
|
|
|
if (fromwire_funding_locked(msg, NULL, &chanid,
|
|
|
|
&peer->next_per_commit[REMOTE])) {
|
|
|
|
if (!structeq(&chanid, &peer->channel_id))
|
|
|
|
status_failed(WIRE_CHANNEL_PEER_BAD_MESSAGE,
|
|
|
|
"Wrong channel id in %s",
|
|
|
|
tal_hex(trc, msg));
|
|
|
|
if (peer->funding_locked[REMOTE])
|
|
|
|
status_failed(WIRE_CHANNEL_PEER_BAD_MESSAGE,
|
|
|
|
"Funding locked twice");
|
|
|
|
peer->funding_locked[REMOTE] = true;
|
2017-03-19 21:32:44 +01:00
|
|
|
daemon_conn_send(&peer->master,
|
|
|
|
take(towire_channel_received_funding_locked(peer)));
|
2017-03-07 02:26:12 +01:00
|
|
|
|
2017-03-11 21:49:52 +01:00
|
|
|
if (peer->funding_locked[LOCAL]) {
|
2017-03-19 21:32:44 +01:00
|
|
|
daemon_conn_send(&peer->master,
|
|
|
|
take(towire_channel_normal_operation(peer)));
|
2017-03-11 21:49:52 +01:00
|
|
|
}
|
|
|
|
} else if (type == WIRE_ANNOUNCEMENT_SIGNATURES) {
|
|
|
|
fromwire_announcement_signatures(
|
|
|
|
msg, NULL, &chanid, &peer->short_channel_ids[REMOTE],
|
|
|
|
&peer->announcement_node_sigs[REMOTE],
|
|
|
|
&peer->announcement_bitcoin_sigs[REMOTE]);
|
|
|
|
|
|
|
|
/* Make sure we agree on the channel ids */
|
|
|
|
if (!structeq(&chanid, &peer->channel_id)) {
|
|
|
|
status_failed(
|
|
|
|
WIRE_CHANNEL_PEER_BAD_MESSAGE,
|
|
|
|
"Wrong channel_id or short_channel_id in %s or %s",
|
|
|
|
tal_hexstr(trc, &chanid, sizeof(struct channel_id)),
|
|
|
|
tal_hexstr(trc, &peer->short_channel_ids[REMOTE],
|
|
|
|
sizeof(struct short_channel_id)));
|
|
|
|
}
|
|
|
|
if (peer->funding_locked[LOCAL]) {
|
2017-03-21 21:21:17 +01:00
|
|
|
send_channel_announcement(peer);
|
|
|
|
send_channel_update(peer, false);
|
2017-03-11 21:49:52 +01:00
|
|
|
}
|
|
|
|
} else if (type == WIRE_CHANNEL_ANNOUNCEMENT ||
|
|
|
|
type == WIRE_CHANNEL_UPDATE ||
|
|
|
|
type == WIRE_NODE_ANNOUNCEMENT) {
|
2017-03-10 15:11:54 +01:00
|
|
|
daemon_conn_send(&peer->gossip_client, msg);
|
|
|
|
}
|
2017-03-07 02:26:12 +01:00
|
|
|
|
|
|
|
return peer_read_message(conn, &peer->pcs, peer_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *setup_peer_conn(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
|
|
|
return io_duplex(conn, peer_read_message(conn, &peer->pcs, peer_in),
|
|
|
|
peer_out(conn, peer));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void peer_conn_broken(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
2017-03-21 21:21:17 +01:00
|
|
|
send_channel_update(peer, true);
|
|
|
|
/* Make sure gossipd actually gets this message before dying */
|
|
|
|
daemon_conn_sync_flush(&peer->gossip_client);
|
2017-03-07 02:26:12 +01:00
|
|
|
status_failed(WIRE_CHANNEL_PEER_READ_FAILED,
|
|
|
|
"peer connection broken: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
|
2017-03-19 21:32:44 +01:00
|
|
|
static void init_channel(struct peer *peer, const u8 *msg)
|
2017-03-07 02:26:12 +01:00
|
|
|
{
|
|
|
|
struct privkey seed;
|
|
|
|
struct basepoints points[NUM_SIDES];
|
|
|
|
u32 feerate;
|
|
|
|
u64 funding_satoshi, push_msat;
|
|
|
|
u16 funding_txout;
|
|
|
|
struct sha256_double funding_txid;
|
|
|
|
bool am_funder;
|
|
|
|
|
|
|
|
if (!fromwire_channel_init(msg, NULL,
|
|
|
|
&funding_txid, &funding_txout,
|
|
|
|
&peer->conf[LOCAL], &peer->conf[REMOTE],
|
|
|
|
&peer->their_commit_sig,
|
|
|
|
&peer->pcs.cs,
|
2017-03-11 17:16:17 +01:00
|
|
|
&peer->funding_pubkey[REMOTE],
|
2017-03-07 02:26:12 +01:00
|
|
|
&points[REMOTE].revocation,
|
|
|
|
&points[REMOTE].payment,
|
|
|
|
&points[REMOTE].delayed_payment,
|
|
|
|
&peer->next_per_commit[REMOTE],
|
|
|
|
&am_funder,
|
|
|
|
&feerate, &funding_satoshi, &push_msat,
|
2017-03-11 17:16:17 +01:00
|
|
|
&seed,
|
|
|
|
&peer->node_ids[LOCAL],
|
|
|
|
&peer->node_ids[REMOTE]))
|
2017-03-07 02:26:12 +01:00
|
|
|
status_failed(WIRE_CHANNEL_BAD_COMMAND, "%s",
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
|
|
|
|
/* We derive everything from the one secret seed. */
|
2017-03-11 17:16:17 +01:00
|
|
|
derive_basepoints(&seed, &peer->funding_pubkey[LOCAL], &points[LOCAL],
|
2017-03-07 02:26:12 +01:00
|
|
|
&peer->our_secrets, &peer->shaseed,
|
|
|
|
&peer->next_per_commit[LOCAL], 1);
|
|
|
|
|
|
|
|
peer->channel = new_channel(peer, &funding_txid, funding_txout,
|
|
|
|
funding_satoshi, push_msat, feerate,
|
|
|
|
&peer->conf[LOCAL], &peer->conf[REMOTE],
|
|
|
|
&points[LOCAL], &points[REMOTE],
|
|
|
|
am_funder ? LOCAL : REMOTE);
|
|
|
|
|
|
|
|
/* OK, now we can process peer messages. */
|
|
|
|
io_set_finish(io_new_conn(peer, PEER_FD, setup_peer_conn, peer),
|
|
|
|
peer_conn_broken, peer);
|
2017-03-19 21:32:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *req_in(struct io_conn *conn, struct daemon_conn *master)
|
|
|
|
{
|
|
|
|
struct peer *peer = container_of(master, struct peer, master);
|
|
|
|
|
|
|
|
if (!peer->channel)
|
|
|
|
init_channel(peer, master->msg_in);
|
|
|
|
else if (fromwire_channel_funding_locked(master->msg_in, NULL,
|
|
|
|
&peer->short_channel_ids[LOCAL])) {
|
|
|
|
u8 *msg = towire_funding_locked(peer,
|
|
|
|
&peer->channel_id,
|
|
|
|
&peer->next_per_commit[LOCAL]);
|
2017-03-19 21:32:44 +01:00
|
|
|
msg_enqueue(&peer->peer_out, take(msg));
|
2017-03-19 21:32:44 +01:00
|
|
|
peer->funding_locked[LOCAL] = true;
|
|
|
|
|
|
|
|
if (peer->funding_locked[REMOTE]) {
|
2017-03-21 21:21:17 +01:00
|
|
|
send_channel_announcement(peer);
|
|
|
|
send_channel_update(peer, false);
|
2017-03-19 21:32:44 +01:00
|
|
|
daemon_conn_send(master,
|
|
|
|
take(towire_channel_normal_operation(peer)));
|
|
|
|
}
|
2017-03-20 17:09:12 +01:00
|
|
|
} else if(fromwire_channel_funding_announce_depth(master->msg_in, NULL)) {
|
|
|
|
status_trace("Exchanging announcement signatures.");
|
|
|
|
send_announcement_signatures(peer);
|
2017-03-19 21:32:44 +01:00
|
|
|
} else
|
|
|
|
status_failed(WIRE_CHANNEL_BAD_COMMAND, "%s", strerror(errno));
|
|
|
|
|
|
|
|
return daemon_conn_read_next(conn, master);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TESTING
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct peer *peer = tal(NULL, struct peer);
|
|
|
|
|
|
|
|
if (argc == 2 && streq(argv[1], "--version")) {
|
|
|
|
printf("%s\n", version());
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
subdaemon_debug(argc, argv);
|
|
|
|
|
|
|
|
/* We handle write returning errors! */
|
|
|
|
signal(SIGCHLD, SIG_IGN);
|
|
|
|
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
|
|
|
|
| SECP256K1_CONTEXT_SIGN);
|
|
|
|
|
|
|
|
daemon_conn_init(peer, &peer->master, REQ_FD, req_in);
|
|
|
|
peer->channel = NULL;
|
|
|
|
|
2017-03-19 21:32:44 +01:00
|
|
|
status_setup_async(&peer->master);
|
2017-03-19 21:32:44 +01:00
|
|
|
msg_queue_init(&peer->peer_out, peer);
|
|
|
|
|
|
|
|
daemon_conn_init(peer, &peer->gossip_client, GOSSIP_FD,
|
|
|
|
gossip_client_recv);
|
|
|
|
|
|
|
|
init_peer_crypto_state(peer, &peer->pcs);
|
|
|
|
peer->funding_locked[LOCAL] = peer->funding_locked[REMOTE] = false;
|
2017-03-07 02:26:12 +01:00
|
|
|
|
|
|
|
/* We don't expect to exit here. */
|
|
|
|
io_loop(NULL, NULL);
|
|
|
|
tal_free(peer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* TESTING */
|