diff --git a/lightningd/channel/channel.c b/lightningd/channel/channel.c index 0769a336d..93931f836 100644 --- a/lightningd/channel/channel.c +++ b/lightningd/channel/channel.c @@ -945,6 +945,9 @@ static void init_channel(struct peer *peer, const u8 *msg) status_failed(WIRE_CHANNEL_BAD_COMMAND, "%s", tal_hex(msg, msg)); + /* channel_id is set from funding txout */ + derive_channel_id(&peer->channel_id, &funding_txid, funding_txout); + /* We derive everything from the one secret seed. */ derive_basepoints(&seed, &funding_pubkey[LOCAL], &points[LOCAL], &peer->our_secrets, &peer->shaseed, diff --git a/lightningd/opening/opening.c b/lightningd/opening/opening.c index 133ff9ae5..5f3c9f2c5 100644 --- a/lightningd/opening/opening.c +++ b/lightningd/opening/opening.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -150,22 +149,6 @@ static void set_reserve(u64 *reserve, u64 funding) *reserve = (funding + 99) / 100; } -/* BOLT #2: - * - * This message introduces the `channel-id` which identifies , which is - * derived from the funding transaction by combining the `funding-txid` and - * the `funding-output-index` using big-endian exclusive-OR - * (ie. `funding-output-index` alters the last two bytes). - */ -static void derive_channel_id(struct channel_id *channel_id, - struct sha256_double *txid, u16 txout) -{ - BUILD_ASSERT(sizeof(*channel_id) == sizeof(*txid)); - memcpy(channel_id, txid, sizeof(*channel_id)); - channel_id->id[sizeof(*channel_id)-2] ^= txout >> 8; - channel_id->id[sizeof(*channel_id)-1] ^= txout; -} - /* BOLT #2: * * A sending node MUST ensure `temporary-channel-id` is unique from any other diff --git a/wire/fromwire.c b/wire/fromwire.c index ef7ebf656..da7a4c2b7 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -178,3 +179,20 @@ static char *fmt_short_channel_id(const tal_t *ctx, } REGISTER_TYPE_TO_STRING(short_channel_id, fmt_short_channel_id); REGISTER_TYPE_TO_HEXSTR(channel_id); + +/* BOLT #2: + * + * This message introduces the `channel-id` which identifies , which is + * derived from the funding transaction by combining the `funding-txid` and + * the `funding-output-index` using big-endian exclusive-OR + * (ie. `funding-output-index` alters the last two bytes). + */ +void derive_channel_id(struct channel_id *channel_id, + struct sha256_double *txid, u16 txout) +{ + BUILD_ASSERT(sizeof(*channel_id) == sizeof(*txid)); + memcpy(channel_id, txid, sizeof(*channel_id)); + channel_id->id[sizeof(*channel_id)-2] ^= txout >> 8; + channel_id->id[sizeof(*channel_id)-1] ^= txout; +} + diff --git a/wire/wire.h b/wire/wire.h index 2188334a6..ae93e607a 100644 --- a/wire/wire.h +++ b/wire/wire.h @@ -22,6 +22,9 @@ struct ipv6 { }; struct preimage; +void derive_channel_id(struct channel_id *channel_id, + struct sha256_double *txid, u16 txout); + /* Read the type; returns -1 if not long enough. cursor is a tal ptr. */ int fromwire_peektype(const u8 *cursor);