Browse Source

common/keyset: use struct basepoints rather than open-coding fields.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
dd2773dfc0
  1. 8
      channeld/full_channel.c
  2. 8
      common/initial_channel.c
  3. 21
      common/keyset.c
  4. 11
      common/keyset.h
  5. 119
      onchaind/onchain.c
  6. 8
      onchaind/test/run-grind_feerate.c

8
channeld/full_channel.c

@ -248,12 +248,8 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx,
struct keyset keyset; struct keyset keyset;
if (!derive_keyset(per_commitment_point, if (!derive_keyset(per_commitment_point,
&channel->basepoints[side].payment, &channel->basepoints[side],
&channel->basepoints[!side].payment, &channel->basepoints[!side],
&channel->basepoints[side].htlc,
&channel->basepoints[!side].htlc,
&channel->basepoints[side].delayed_payment,
&channel->basepoints[!side].revocation,
&keyset)) &keyset))
return NULL; return NULL;

8
common/initial_channel.c

@ -75,12 +75,8 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx,
assert(!channel->htlcs); assert(!channel->htlcs);
if (!derive_keyset(per_commitment_point, if (!derive_keyset(per_commitment_point,
&channel->basepoints[side].payment, &channel->basepoints[side],
&channel->basepoints[!side].payment, &channel->basepoints[!side],
&channel->basepoints[side].htlc,
&channel->basepoints[!side].htlc,
&channel->basepoints[side].delayed_payment,
&channel->basepoints[!side].revocation,
&keyset)) &keyset))
return NULL; return NULL;

21
common/keyset.c

@ -1,13 +1,10 @@
#include <common/derive_basepoints.h>
#include <common/key_derive.h> #include <common/key_derive.h>
#include <common/keyset.h> #include <common/keyset.h>
bool derive_keyset(const struct pubkey *per_commitment_point, bool derive_keyset(const struct pubkey *per_commitment_point,
const struct pubkey *self_payment_basepoint, const struct basepoints *self,
const struct pubkey *other_payment_basepoint, const struct basepoints *other,
const struct pubkey *self_htlc_basepoint,
const struct pubkey *other_htlc_basepoint,
const struct pubkey *self_delayed_basepoint,
const struct pubkey *other_revocation_basepoint,
struct keyset *keyset) struct keyset *keyset)
{ {
/* BOLT #3: /* BOLT #3:
@ -27,27 +24,27 @@ bool derive_keyset(const struct pubkey *per_commitment_point,
* node's `htlc_basepoint`; and the `remote_delayedpubkey` uses the * node's `htlc_basepoint`; and the `remote_delayedpubkey` uses the
* remote node's `delayed_payment_basepoint`. * remote node's `delayed_payment_basepoint`.
*/ */
if (!derive_simple_key(self_payment_basepoint, if (!derive_simple_key(&self->payment,
per_commitment_point, per_commitment_point,
&keyset->self_payment_key)) &keyset->self_payment_key))
return false; return false;
if (!derive_simple_key(other_payment_basepoint, if (!derive_simple_key(&other->payment,
per_commitment_point, per_commitment_point,
&keyset->other_payment_key)) &keyset->other_payment_key))
return false; return false;
if (!derive_simple_key(self_htlc_basepoint, if (!derive_simple_key(&self->htlc,
per_commitment_point, per_commitment_point,
&keyset->self_htlc_key)) &keyset->self_htlc_key))
return false; return false;
if (!derive_simple_key(other_htlc_basepoint, if (!derive_simple_key(&other->htlc,
per_commitment_point, per_commitment_point,
&keyset->other_htlc_key)) &keyset->other_htlc_key))
return false; return false;
if (!derive_simple_key(self_delayed_basepoint, if (!derive_simple_key(&self->delayed_payment,
per_commitment_point, per_commitment_point,
&keyset->self_delayed_payment_key)) &keyset->self_delayed_payment_key))
return false; return false;
@ -61,7 +58,7 @@ bool derive_keyset(const struct pubkey *per_commitment_point,
* `revocation_basepoint` and the remote node's `per_commitment_point` * `revocation_basepoint` and the remote node's `per_commitment_point`
* to derive a new `revocationpubkey` for the commitment. * to derive a new `revocationpubkey` for the commitment.
*/ */
if (!derive_revocation_key(other_revocation_basepoint, if (!derive_revocation_key(&other->revocation,
per_commitment_point, per_commitment_point,
&keyset->self_revocation_key)) &keyset->self_revocation_key))
return false; return false;

11
common/keyset.h

@ -4,6 +4,8 @@
#include <bitcoin/pubkey.h> #include <bitcoin/pubkey.h>
#include <stdbool.h> #include <stdbool.h>
struct basepoints;
/* Keys needed to derive a particular commitment tx. */ /* Keys needed to derive a particular commitment tx. */
struct keyset { struct keyset {
struct pubkey self_revocation_key; struct pubkey self_revocation_key;
@ -12,12 +14,9 @@ struct keyset {
struct pubkey self_payment_key, other_payment_key; struct pubkey self_payment_key, other_payment_key;
}; };
/* Self == owner of commitment tx, other == non-owner. */
bool derive_keyset(const struct pubkey *per_commitment_point, bool derive_keyset(const struct pubkey *per_commitment_point,
const struct pubkey *self_payment_basepoint, const struct basepoints *self,
const struct pubkey *other_payment_basepoint, const struct basepoints *other,
const struct pubkey *self_htlc_basepoint,
const struct pubkey *other_htlc_basepoint,
const struct pubkey *self_delayed_basepoint,
const struct pubkey *other_revocation_basepoint,
struct keyset *keyset); struct keyset *keyset);
#endif /* LIGHTNING_COMMON_KEYSET_H */ #endif /* LIGHTNING_COMMON_KEYSET_H */

119
onchaind/onchain.c

@ -1369,12 +1369,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
const struct bitcoin_txid *txid, const struct bitcoin_txid *txid,
const struct secrets *secrets, const struct secrets *secrets,
const struct sha256 *shaseed, const struct sha256 *shaseed,
const struct pubkey *remote_revocation_basepoint, const struct basepoints basepoints[NUM_SIDES],
const struct pubkey *remote_payment_basepoint,
const struct pubkey *local_payment_basepoint,
const struct pubkey *remote_htlc_basepoint,
const struct pubkey *local_htlc_basepoint,
const struct pubkey *local_delayed_payment_basepoint,
const struct htlc_stub *htlcs, const struct htlc_stub *htlcs,
const bool *tell_if_missing, const bool *tell_if_missing,
const bool *tell_immediately, const bool *tell_immediately,
@ -1405,12 +1400,8 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
/* keyset is const, we need a non-const ptr to set it up */ /* keyset is const, we need a non-const ptr to set it up */
keyset = ks = tal(tx, struct keyset); keyset = ks = tal(tx, struct keyset);
if (!derive_keyset(&local_per_commitment_point, if (!derive_keyset(&local_per_commitment_point,
local_payment_basepoint, &basepoints[LOCAL],
remote_payment_basepoint, &basepoints[REMOTE],
local_htlc_basepoint,
remote_htlc_basepoint,
local_delayed_payment_basepoint,
remote_revocation_basepoint,
ks)) ks))
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Deriving keyset for %"PRIu64, commit_num); "Deriving keyset for %"PRIu64, commit_num);
@ -1438,7 +1429,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
&keyset->other_htlc_key)); &keyset->other_htlc_key));
if (!derive_simple_privkey(&secrets->delayed_payment_basepoint_secret, if (!derive_simple_privkey(&secrets->delayed_payment_basepoint_secret,
local_delayed_payment_basepoint, &basepoints[LOCAL].delayed_payment,
&local_per_commitment_point, &local_per_commitment_point,
&delayed_payment_privkey)) &delayed_payment_privkey))
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
@ -1446,7 +1437,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
commit_num); commit_num);
if (!derive_simple_privkey(&secrets->payment_basepoint_secret, if (!derive_simple_privkey(&secrets->payment_basepoint_secret,
local_payment_basepoint, &basepoints[LOCAL].payment,
&local_per_commitment_point, &local_per_commitment_point,
&payment_privkey)) &payment_privkey))
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
@ -1454,7 +1445,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
commit_num); commit_num);
if (!derive_simple_privkey(&secrets->htlc_basepoint_secret, if (!derive_simple_privkey(&secrets->htlc_basepoint_secret,
local_htlc_basepoint, &basepoints[LOCAL].htlc,
&local_per_commitment_point, &local_per_commitment_point,
&htlc_privkey)) &htlc_privkey))
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
@ -1674,12 +1665,7 @@ static void handle_their_cheat(const struct bitcoin_tx *tx,
u32 tx_blockheight, u32 tx_blockheight,
const struct sha256 *revocation_preimage, const struct sha256 *revocation_preimage,
const struct secrets *secrets, const struct secrets *secrets,
const struct pubkey *local_revocation_basepoint, const struct basepoints basepoints[NUM_SIDES],
const struct pubkey *local_payment_basepoint,
const struct pubkey *remote_payment_basepoint,
const struct pubkey *remote_htlc_basepoint,
const struct pubkey *local_htlc_basepoint,
const struct pubkey *remote_delayed_payment_basepoint,
const struct htlc_stub *htlcs, const struct htlc_stub *htlcs,
const bool *tell_if_missing, const bool *tell_if_missing,
const bool *tell_immediately, const bool *tell_immediately,
@ -1729,27 +1715,23 @@ static void handle_their_cheat(const struct bitcoin_tx *tx,
type_to_string(tmpctx, struct pubkey, type_to_string(tmpctx, struct pubkey,
&per_commitment_point), &per_commitment_point),
type_to_string(tmpctx, struct pubkey, type_to_string(tmpctx, struct pubkey,
remote_payment_basepoint), &basepoints[REMOTE].payment),
type_to_string(tmpctx, struct pubkey, type_to_string(tmpctx, struct pubkey,
local_payment_basepoint), &basepoints[LOCAL].payment),
type_to_string(tmpctx, struct pubkey, type_to_string(tmpctx, struct pubkey,
remote_htlc_basepoint), &basepoints[REMOTE].htlc),
type_to_string(tmpctx, struct pubkey, type_to_string(tmpctx, struct pubkey,
local_htlc_basepoint), &basepoints[LOCAL].htlc),
type_to_string(tmpctx, struct pubkey, type_to_string(tmpctx, struct pubkey,
remote_delayed_payment_basepoint), &basepoints[REMOTE].delayed_payment),
type_to_string(tmpctx, struct pubkey, type_to_string(tmpctx, struct pubkey,
local_revocation_basepoint)); &basepoints[LOCAL].revocation));
/* keyset is const, we need a non-const ptr to set it up */ /* keyset is const, we need a non-const ptr to set it up */
keyset = ks = tal(tx, struct keyset); keyset = ks = tal(tx, struct keyset);
if (!derive_keyset(&per_commitment_point, if (!derive_keyset(&per_commitment_point,
remote_payment_basepoint, &basepoints[REMOTE],
local_payment_basepoint, &basepoints[LOCAL],
local_htlc_basepoint,
remote_htlc_basepoint,
remote_delayed_payment_basepoint,
local_revocation_basepoint,
ks)) ks))
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Deriving keyset for %"PRIu64, commit_num); "Deriving keyset for %"PRIu64, commit_num);
@ -1779,7 +1761,7 @@ static void handle_their_cheat(const struct bitcoin_tx *tx,
revocation_privkey = tal(tx, struct privkey); revocation_privkey = tal(tx, struct privkey);
if (!derive_revocation_privkey(&secrets->revocation_basepoint_secret, if (!derive_revocation_privkey(&secrets->revocation_basepoint_secret,
&per_commitment_secret, &per_commitment_secret,
local_revocation_basepoint, &basepoints[LOCAL].revocation,
&per_commitment_point, &per_commitment_point,
revocation_privkey)) revocation_privkey))
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
@ -1906,12 +1888,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
const struct bitcoin_txid *txid, const struct bitcoin_txid *txid,
const struct secrets *secrets, const struct secrets *secrets,
const struct pubkey *remote_per_commitment_point, const struct pubkey *remote_per_commitment_point,
const struct pubkey *local_revocation_basepoint, const struct basepoints basepoints[NUM_SIDES],
const struct pubkey *local_payment_basepoint,
const struct pubkey *remote_payment_basepoint,
const struct pubkey *remote_htlc_basepoint,
const struct pubkey *local_htlc_basepoint,
const struct pubkey *remote_delayed_payment_basepoint,
const struct htlc_stub *htlcs, const struct htlc_stub *htlcs,
const bool *tell_if_missing, const bool *tell_if_missing,
const bool *tell_immediately, const bool *tell_immediately,
@ -1950,27 +1927,23 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
type_to_string(tmpctx, struct pubkey, type_to_string(tmpctx, struct pubkey,
remote_per_commitment_point), remote_per_commitment_point),
type_to_string(tmpctx, struct pubkey, type_to_string(tmpctx, struct pubkey,
remote_payment_basepoint), &basepoints[REMOTE].payment),
type_to_string(tmpctx, struct pubkey, type_to_string(tmpctx, struct pubkey,
local_payment_basepoint), &basepoints[LOCAL].payment),
type_to_string(tmpctx, struct pubkey, type_to_string(tmpctx, struct pubkey,
remote_htlc_basepoint), &basepoints[REMOTE].htlc),
type_to_string(tmpctx, struct pubkey, type_to_string(tmpctx, struct pubkey,
local_htlc_basepoint), &basepoints[LOCAL].htlc),
type_to_string(tmpctx, struct pubkey, type_to_string(tmpctx, struct pubkey,
remote_delayed_payment_basepoint), &basepoints[REMOTE].delayed_payment),
type_to_string(tmpctx, struct pubkey, type_to_string(tmpctx, struct pubkey,
local_revocation_basepoint)); &basepoints[LOCAL].revocation));
/* keyset is const, we need a non-const ptr to set it up */ /* keyset is const, we need a non-const ptr to set it up */
keyset = ks = tal(tx, struct keyset); keyset = ks = tal(tx, struct keyset);
if (!derive_keyset(remote_per_commitment_point, if (!derive_keyset(remote_per_commitment_point,
remote_payment_basepoint, &basepoints[REMOTE],
local_payment_basepoint, &basepoints[LOCAL],
remote_htlc_basepoint,
local_htlc_basepoint,
remote_delayed_payment_basepoint,
local_revocation_basepoint,
ks)) ks))
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Deriving keyset for %"PRIu64, commit_num); "Deriving keyset for %"PRIu64, commit_num);
@ -1998,7 +1971,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
&keyset->other_htlc_key)); &keyset->other_htlc_key));
if (!derive_simple_privkey(&secrets->payment_basepoint_secret, if (!derive_simple_privkey(&secrets->payment_basepoint_secret,
local_payment_basepoint, &basepoints[LOCAL].payment,
remote_per_commitment_point, remote_per_commitment_point,
&payment_privkey)) &payment_privkey))
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
@ -2006,7 +1979,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
commit_num); commit_num);
if (!derive_simple_privkey(&secrets->htlc_basepoint_secret, if (!derive_simple_privkey(&secrets->htlc_basepoint_secret,
local_htlc_basepoint, &basepoints[LOCAL].htlc,
remote_per_commitment_point, remote_per_commitment_point,
&htlc_privkey)) &htlc_privkey))
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
@ -2138,7 +2111,7 @@ int main(int argc, char *argv[])
struct secret seed; struct secret seed;
struct pubkey remote_per_commit_point, old_remote_per_commit_point; struct pubkey remote_per_commit_point, old_remote_per_commit_point;
enum side funder; enum side funder;
struct basepoints basepoints, remote_basepoints; struct basepoints basepoints[NUM_SIDES];
struct shachain shachain; struct shachain shachain;
struct bitcoin_tx *tx; struct bitcoin_tx *tx;
struct secrets secrets; struct secrets secrets;
@ -2173,7 +2146,7 @@ int main(int argc, char *argv[])
&scriptpubkey[REMOTE], &scriptpubkey[REMOTE],
&our_wallet_pubkey, &our_wallet_pubkey,
&funder, &funder,
&remote_basepoints, &basepoints[REMOTE],
&tx, &tx,
&tx_blockheight, &tx_blockheight,
&reasonable_depth, &reasonable_depth,
@ -2184,7 +2157,7 @@ int main(int argc, char *argv[])
master_badmsg(WIRE_ONCHAIN_INIT, msg); master_badmsg(WIRE_ONCHAIN_INIT, msg);
} }
derive_basepoints(&seed, NULL, &basepoints, &secrets, &shaseed); derive_basepoints(&seed, NULL, &basepoints[LOCAL], &secrets, &shaseed);
bitcoin_txid(tx, &txid); bitcoin_txid(tx, &txid);
/* FIXME: Filter as we go, don't load them all into mem! */ /* FIXME: Filter as we go, don't load them all into mem! */
@ -2240,8 +2213,8 @@ int main(int argc, char *argv[])
*/ */
struct sha256 revocation_preimage; struct sha256 revocation_preimage;
commit_num = unmask_commit_number(tx, funder, commit_num = unmask_commit_number(tx, funder,
&basepoints.payment, &basepoints[LOCAL].payment,
&remote_basepoints.payment); &basepoints[REMOTE].payment);
status_trace("commitnum = %"PRIu64 status_trace("commitnum = %"PRIu64
", revocations_received = %"PRIu64, ", revocations_received = %"PRIu64,
@ -2251,12 +2224,7 @@ int main(int argc, char *argv[])
handle_our_unilateral(tx, tx_blockheight, &txid, handle_our_unilateral(tx, tx_blockheight, &txid,
&secrets, &secrets,
&shaseed, &shaseed,
&remote_basepoints.revocation, basepoints,
&remote_basepoints.payment,
&basepoints.payment,
&remote_basepoints.htlc,
&basepoints.htlc,
&basepoints.delayed_payment,
htlcs, htlcs,
tell_if_missing, tell_immediately, tell_if_missing, tell_immediately,
remote_htlc_sigs, remote_htlc_sigs,
@ -2275,12 +2243,7 @@ int main(int argc, char *argv[])
tx_blockheight, tx_blockheight,
&revocation_preimage, &revocation_preimage,
&secrets, &secrets,
&basepoints.revocation, basepoints,
&basepoints.payment,
&remote_basepoints.payment,
&basepoints.htlc,
&remote_basepoints.htlc,
&remote_basepoints.delayed_payment,
htlcs, htlcs,
tell_if_missing, tell_immediately, tell_if_missing, tell_immediately,
outs); outs);
@ -2298,12 +2261,7 @@ int main(int argc, char *argv[])
handle_their_unilateral(tx, tx_blockheight, handle_their_unilateral(tx, tx_blockheight,
&txid, &secrets, &txid, &secrets,
&old_remote_per_commit_point, &old_remote_per_commit_point,
&basepoints.revocation, basepoints,
&basepoints.payment,
&remote_basepoints.payment,
&remote_basepoints.htlc,
&basepoints.htlc,
&remote_basepoints.delayed_payment,
htlcs, htlcs,
tell_if_missing, tell_if_missing,
tell_immediately, tell_immediately,
@ -2313,12 +2271,7 @@ int main(int argc, char *argv[])
handle_their_unilateral(tx, tx_blockheight, handle_their_unilateral(tx, tx_blockheight,
&txid, &secrets, &txid, &secrets,
&remote_per_commit_point, &remote_per_commit_point,
&basepoints.revocation, basepoints,
&basepoints.payment,
&remote_basepoints.payment,
&remote_basepoints.htlc,
&basepoints.htlc,
&remote_basepoints.delayed_payment,
htlcs, htlcs,
tell_if_missing, tell_if_missing,
tell_immediately, tell_immediately,

8
onchaind/test/run-grind_feerate.c

@ -27,12 +27,8 @@ bool derive_basepoints(const struct secret *seed UNNEEDED,
{ fprintf(stderr, "derive_basepoints called!\n"); abort(); } { fprintf(stderr, "derive_basepoints called!\n"); abort(); }
/* Generated stub for derive_keyset */ /* Generated stub for derive_keyset */
bool derive_keyset(const struct pubkey *per_commitment_point UNNEEDED, bool derive_keyset(const struct pubkey *per_commitment_point UNNEEDED,
const struct pubkey *self_payment_basepoint UNNEEDED, const struct basepoints *self UNNEEDED,
const struct pubkey *other_payment_basepoint UNNEEDED, const struct basepoints *other UNNEEDED,
const struct pubkey *self_htlc_basepoint UNNEEDED,
const struct pubkey *other_htlc_basepoint UNNEEDED,
const struct pubkey *self_delayed_basepoint UNNEEDED,
const struct pubkey *other_revocation_basepoint UNNEEDED,
struct keyset *keyset UNNEEDED) struct keyset *keyset UNNEEDED)
{ fprintf(stderr, "derive_keyset called!\n"); abort(); } { fprintf(stderr, "derive_keyset called!\n"); abort(); }
/* Generated stub for derive_revocation_privkey */ /* Generated stub for derive_revocation_privkey */

Loading…
Cancel
Save