From b123b1867d6ba421d5830e8f6cbd156f88220fc8 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 17 Aug 2018 13:46:33 +0930 Subject: [PATCH] shachain: shachain_get_secret helper. This is a wrapper around shachain_get_hash, which converts the commit_num to an index and returns a 'struct secret' rather than a 'struct sha256' (which is really an internal detail). Signed-off-by: Rusty Russell --- common/derive_basepoints.c | 13 +++++++++++++ common/derive_basepoints.h | 4 ++++ onchaind/onchain.c | 9 ++++----- onchaind/test/run-grind_feerate.c | 5 +++++ 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/common/derive_basepoints.c b/common/derive_basepoints.c index afae3217a..1f03236a4 100644 --- a/common/derive_basepoints.c +++ b/common/derive_basepoints.c @@ -228,3 +228,16 @@ void fromwire_basepoints(const u8 **ptr, size_t *max, fromwire_pubkey(ptr, max, &b->htlc); fromwire_pubkey(ptr, max, &b->delayed_payment); } + +bool shachain_get_secret(const struct shachain *shachain, + u64 commit_num, + struct secret *preimage) +{ + struct sha256 sha; + + if (!shachain_get_hash(shachain, shachain_index(commit_num), &sha)) + return false; + BUILD_ASSERT(sizeof(*preimage) == sizeof(sha)); + memcpy(preimage, &sha, sizeof(*preimage)); + return true; +} diff --git a/common/derive_basepoints.h b/common/derive_basepoints.h index 9c78a94b0..6dc24a9b7 100644 --- a/common/derive_basepoints.h +++ b/common/derive_basepoints.h @@ -145,6 +145,10 @@ static inline u64 revocations_received(const struct shachain *shachain) return (1ULL << SHACHAIN_BITS) - (shachain_next_index(shachain) + 1); } +bool shachain_get_secret(const struct shachain *shachain, + u64 commit_num, + struct secret *preimage); + void towire_basepoints(u8 **pptr, const struct basepoints *b); void fromwire_basepoints(const u8 **ptr, size_t *max, struct basepoints *b); diff --git a/onchaind/onchain.c b/onchaind/onchain.c index 683a48efc..6ad04e3b8 100644 --- a/onchaind/onchain.c +++ b/onchaind/onchain.c @@ -1698,7 +1698,7 @@ static void steal_htlc(struct tracked_output *out) static void handle_their_cheat(const struct bitcoin_tx *tx, const struct bitcoin_txid *txid, u32 tx_blockheight, - const struct sha256 *revocation_preimage, + const struct secret *revocation_preimage, const struct basepoints basepoints[NUM_SIDES], const struct htlc_stub *htlcs, const bool *tell_if_missing, @@ -2223,7 +2223,7 @@ int main(int argc, char *argv[]) * party crashed, for instance. One side publishes its * *latest commitment transaction*. */ - struct sha256 revocation_preimage; + struct secret revocation_preimage; commit_num = unmask_commit_number(tx, funder, &basepoints[LOCAL].payment, &basepoints[REMOTE].payment); @@ -2246,9 +2246,8 @@ int main(int argc, char *argv[]) * *outdated commitment transaction* (presumably, a prior * version, which is more in its favor). */ - else if (shachain_get_hash(&shachain, - shachain_index(commit_num), - &revocation_preimage)) { + else if (shachain_get_secret(&shachain, commit_num, + &revocation_preimage)) { handle_their_cheat(tx, &txid, tx_blockheight, &revocation_preimage, diff --git a/onchaind/test/run-grind_feerate.c b/onchaind/test/run-grind_feerate.c index adb26ab2c..09e551ac1 100644 --- a/onchaind/test/run-grind_feerate.c +++ b/onchaind/test/run-grind_feerate.c @@ -81,6 +81,11 @@ void master_badmsg(u32 type_expected UNNEEDED, const u8 *msg) /* Generated stub for peer_billboard */ void peer_billboard(bool perm UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "peer_billboard called!\n"); abort(); } +/* Generated stub for shachain_get_secret */ +bool shachain_get_secret(const struct shachain *shachain UNNEEDED, + u64 commit_num UNNEEDED, + struct secret *preimage UNNEEDED) +{ fprintf(stderr, "shachain_get_secret called!\n"); abort(); } /* Generated stub for status_failed */ void status_failed(enum status_failreason code UNNEEDED, const char *fmt UNNEEDED, ...)