Browse Source

channel_id: add v2 channel_id derivtion helper

v2 of channel open uses the channel revocation basepoints to calculate
the channel_id, instead of the funding_txid + outnum

Moving away from the funding_txid opens the way for splicing + rbf
bump-pyln-proto
niftynei 4 years ago
committed by Rusty Russell
parent
commit
42a38b9808
  1. 34
      common/channel_id.c
  2. 4
      common/channel_id.h

34
common/channel_id.c

@ -1,3 +1,4 @@
#include <bitcoin/pubkey.h>
#include <bitcoin/tx.h>
#include <common/channel_id.h>
#include <common/type_to_string.h>
@ -12,6 +13,39 @@ void derive_channel_id(struct channel_id *channel_id,
channel_id->id[sizeof(*channel_id)-1] ^= txout;
}
void derive_channel_id_v2(struct channel_id *channel_id,
const struct pubkey *basepoint_1,
const struct pubkey *basepoint_2)
{
/* BOLT-df8bb5994d99e4c78053f7cb57694795f8393dc5 #2:
* `channel_id`, v2
* For channels established using the v2 protocol, the
* `channel_id` is the
* SHA256(lesser-revocation-basepoint ||
* greater-revocation-basepoint),
* where the lesser and greater is based off the order of
* the basepoint. The basepoints are compact
* DER-encoded public keys.
*/
u8 der_keys[PUBKEY_CMPR_LEN * 2];
struct sha256 sha;
int offset_1, offset_2;
/* basepoint_1 is first? */
if (pubkey_idx(basepoint_1, basepoint_2) == 0) {
offset_1 = 0;
offset_2 = PUBKEY_CMPR_LEN;
} else {
offset_1 = PUBKEY_CMPR_LEN;
offset_2 = 0;
}
pubkey_to_der(der_keys + offset_1, basepoint_1);
pubkey_to_der(der_keys + offset_2, basepoint_2);
sha256(&sha, der_keys, sizeof(der_keys));
BUILD_ASSERT(sizeof(*channel_id) == sizeof(sha));
memcpy(channel_id, &sha, sizeof(*channel_id));
}
void towire_channel_id(u8 **pptr, const struct channel_id *channel_id)
{
towire(pptr, channel_id, sizeof(*channel_id));

4
common/channel_id.h

@ -5,6 +5,7 @@
#include <ccan/structeq/structeq.h>
struct bitcoin_txid;
struct pubkey;
/* BOLT #2:
*
@ -22,6 +23,9 @@ STRUCTEQ_DEF(channel_id, 0, id);
void derive_channel_id(struct channel_id *channel_id,
const struct bitcoin_txid *txid, u16 txout);
void derive_channel_id_v2(struct channel_id *channel_id,
const struct pubkey *basepoint_1,
const struct pubkey *basepoint_2);
/* Marshalling/unmarshalling functions */
void towire_channel_id(u8 **pptr, const struct channel_id *channel_id);
void fromwire_channel_id(const u8 **cursor, size_t *max,

Loading…
Cancel
Save