2018-07-23 04:23:02 +02:00
|
|
|
#include <common/derive_basepoints.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/key_derive.h>
|
|
|
|
#include <common/keyset.h>
|
2017-08-18 06:43:53 +02:00
|
|
|
|
|
|
|
bool derive_keyset(const struct pubkey *per_commitment_point,
|
2018-07-23 04:23:02 +02:00
|
|
|
const struct basepoints *self,
|
|
|
|
const struct basepoints *other,
|
2019-09-10 04:25:27 +02:00
|
|
|
bool option_static_remotekey,
|
2017-08-18 06:43:53 +02:00
|
|
|
struct keyset *keyset)
|
|
|
|
{
|
2020-02-04 01:03:22 +01:00
|
|
|
/* BOLT #3:
|
2017-08-18 06:43:53 +02:00
|
|
|
*
|
2019-09-10 04:25:27 +02:00
|
|
|
* ### `localpubkey`, `local_htlcpubkey`, `remote_htlcpubkey`, `local_delayedpubkey`, and `remote_delayedpubkey` Derivation
|
2017-08-18 06:43:53 +02:00
|
|
|
*
|
2018-06-17 12:11:53 +02:00
|
|
|
* These pubkeys are simply generated by addition from their base points:
|
2017-08-18 06:43:53 +02:00
|
|
|
*
|
2018-06-17 12:11:53 +02:00
|
|
|
* pubkey = basepoint + SHA256(per_commitment_point || basepoint) * G
|
2017-08-18 06:43:53 +02:00
|
|
|
*
|
2019-01-14 03:26:25 +01:00
|
|
|
* The `localpubkey` uses the local node's `payment_basepoint`;
|
2020-08-20 08:49:47 +02:00
|
|
|
* The `remotepubkey` uses the remote node's `payment_basepoint`;
|
2019-01-14 03:26:25 +01:00
|
|
|
* the `local_htlcpubkey` uses the local node's `htlc_basepoint`;
|
|
|
|
* the `remote_htlcpubkey` uses the remote node's `htlc_basepoint`;
|
|
|
|
* the `local_delayedpubkey` uses the local node's `delayed_payment_basepoint`;
|
|
|
|
* and the `remote_delayedpubkey` uses the remote node's `delayed_payment_basepoint`.
|
2017-08-18 06:43:53 +02:00
|
|
|
*/
|
2018-07-23 04:23:02 +02:00
|
|
|
if (!derive_simple_key(&self->payment,
|
2017-08-18 06:43:53 +02:00
|
|
|
per_commitment_point,
|
|
|
|
&keyset->self_payment_key))
|
|
|
|
return false;
|
|
|
|
|
2019-09-26 08:30:41 +02:00
|
|
|
/* BOLT #3:
|
2019-09-10 04:25:27 +02:00
|
|
|
*
|
|
|
|
* ### `remotepubkey` Derivation
|
|
|
|
*
|
2021-09-08 02:08:14 +02:00
|
|
|
* If `option_static_remotekey` or `option_anchors` is
|
2020-08-20 08:49:47 +02:00
|
|
|
* negotiated, the `remotepubkey` is simply the remote node's
|
|
|
|
* `payment_basepoint`, otherwise it is calculated as above using the
|
|
|
|
* remote node's `payment_basepoint`.
|
2019-09-10 04:25:27 +02:00
|
|
|
*/
|
|
|
|
if (option_static_remotekey)
|
|
|
|
keyset->other_payment_key = other->payment;
|
|
|
|
else if (!derive_simple_key(&other->payment,
|
|
|
|
per_commitment_point,
|
|
|
|
&keyset->other_payment_key))
|
2017-08-18 06:43:53 +02:00
|
|
|
return false;
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
if (!derive_simple_key(&self->htlc,
|
2017-11-15 07:16:39 +01:00
|
|
|
per_commitment_point,
|
|
|
|
&keyset->self_htlc_key))
|
|
|
|
return false;
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
if (!derive_simple_key(&other->htlc,
|
2017-11-15 07:16:39 +01:00
|
|
|
per_commitment_point,
|
|
|
|
&keyset->other_htlc_key))
|
|
|
|
return false;
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
if (!derive_simple_key(&self->delayed_payment,
|
2017-08-18 06:43:53 +02:00
|
|
|
per_commitment_point,
|
|
|
|
&keyset->self_delayed_payment_key))
|
2017-11-23 13:32:38 +01:00
|
|
|
return false;
|
2017-08-18 06:43:53 +02:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2018-06-17 12:11:53 +02:00
|
|
|
* ### `revocationpubkey` Derivation
|
2017-08-18 06:43:53 +02:00
|
|
|
*
|
2018-06-17 12:11:53 +02:00
|
|
|
* The `revocationpubkey` is a blinded key: when the local node wishes
|
|
|
|
* to create a new commitment for the remote node, it uses its own
|
2017-08-18 06:43:53 +02:00
|
|
|
* `revocation_basepoint` and the remote node's `per_commitment_point`
|
2018-06-17 12:11:53 +02:00
|
|
|
* to derive a new `revocationpubkey` for the commitment.
|
2017-08-18 06:43:53 +02:00
|
|
|
*/
|
2018-07-23 04:23:02 +02:00
|
|
|
if (!derive_revocation_key(&other->revocation,
|
2017-08-18 06:43:53 +02:00
|
|
|
per_commitment_point,
|
|
|
|
&keyset->self_revocation_key))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|