From b5ff69e236d790d53bcd26b2259f3f6b503ce85f Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 7 Aug 2015 12:46:59 +0930 Subject: [PATCH] test-cli/update-channel-htlc-remove: new util. Signed-off-by: Rusty Russell --- Makefile | 2 +- pkt.c | 12 ++++++ pkt.h | 12 +++++- test-cli/.gitignore | 2 + test-cli/update-channel-htlc-remove.c | 61 +++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 test-cli/update-channel-htlc-remove.c diff --git a/Makefile b/Makefile index fd1bc989c..67c8d0639 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ FEATURES := -DHAS_CSV=1 -DALPHA_TXSTYLE=1 -DUSE_SCHNORR=1 -DHAS_CLTV=1 # Bitcoin uses DER for signatures #FEATURES := -DSCRIPTS_USE_DER -PROGRAMS := test-cli/open-channel test-cli/create-anchor-tx test-cli/open-commit-sig test-cli/check-commit-sig test-cli/get-anchor-depth test-cli/create-steal-tx test-cli/create-commit-spend-tx test-cli/close-channel test-cli/create-close-tx test-cli/update-channel test-cli/update-channel-accept test-cli/update-channel-signature test-cli/update-channel-complete test-cli/create-commit-tx test-cli/txid-of test-cli/open-anchor test-cli/update-channel-htlc test-cli/update-channel-htlc-complete +PROGRAMS := test-cli/open-channel test-cli/create-anchor-tx test-cli/open-commit-sig test-cli/check-commit-sig test-cli/get-anchor-depth test-cli/create-steal-tx test-cli/create-commit-spend-tx test-cli/close-channel test-cli/create-close-tx test-cli/update-channel test-cli/update-channel-accept test-cli/update-channel-signature test-cli/update-channel-complete test-cli/create-commit-tx test-cli/txid-of test-cli/open-anchor test-cli/update-channel-htlc test-cli/update-channel-htlc-complete test-cli/update-channel-htlc-remove BITCOIN_OBJS := bitcoin/address.o bitcoin/base58.o bitcoin/pubkey.o bitcoin/script.o bitcoin/shadouble.o bitcoin/signature.o bitcoin/tx.o diff --git a/pkt.c b/pkt.c index 12495a3c9..a1528065a 100644 --- a/pkt.c +++ b/pkt.c @@ -171,6 +171,18 @@ struct pkt *update_htlc_complete_pkt(const tal_t *ctx, return to_pkt(ctx, PKT__PKT_UPDATE_COMPLETE_HTLC, &u); } +struct pkt *update_htlc_remove_pkt(const tal_t *ctx, + const struct sha256 *revocation_hash, + const struct sha256 *htlc_rhash) +{ + UpdateRemoveHtlc u = UPDATE_REMOVE_HTLC__INIT; + + u.revocation_hash = sha256_to_proto(ctx, revocation_hash); + u.r_hash = sha256_to_proto(ctx, htlc_rhash); + + return to_pkt(ctx, PKT__PKT_UPDATE_REMOVE_HTLC, &u); +} + struct pkt *update_accept_pkt(const tal_t *ctx, struct signature *sig, const struct sha256 *revocation_hash) diff --git a/pkt.h b/pkt.h index 0eaf1394d..7133271c4 100644 --- a/pkt.h +++ b/pkt.h @@ -105,7 +105,7 @@ struct pkt *update_htlc_add_pkt(const tal_t *ctx, u32 abs_locktime_seconds); /** - * update_htlc_complete_pkt - create an update message completing a HTLC + * update_htlc_complete_pkt - create an update message removing a HTLC * @ctx: tal context to allocate off. * @revocation_hash: the revocation hash for the next commitment tx. * @rval: the r value for the HTLC @@ -114,6 +114,16 @@ struct pkt *update_htlc_complete_pkt(const tal_t *ctx, const struct sha256 *revocation_hash, const struct sha256 *rval); +/** + * update_htlc_remove_pkt - create an update message completing a HTLC + * @ctx: tal context to allocate off. + * @revocation_hash: the revocation hash for the next commitment tx. + * @htlc_rhash: the hash of the htlc secret. + */ +struct pkt *update_htlc_remove_pkt(const tal_t *ctx, + const struct sha256 *revocation_hash, + const struct sha256 *htlc_rhash); + /** * update_accept_pkt - create an update_accept message * @ctx: tal context to allocate off. diff --git a/test-cli/.gitignore b/test-cli/.gitignore index 41795d7f7..19b89121a 100644 --- a/test-cli/.gitignore +++ b/test-cli/.gitignore @@ -19,3 +19,5 @@ txid-of create-anchor-tx update-channel-htlc update-channel-htlc-complete +update-channel-htlc-remove + diff --git a/test-cli/update-channel-htlc-remove.c b/test-cli/update-channel-htlc-remove.c new file mode 100644 index 000000000..8c6c3602b --- /dev/null +++ b/test-cli/update-channel-htlc-remove.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include +#include "lightning.pb-c.h" +#include "bitcoin/base58.h" +#include "pkt.h" +#include "bitcoin/script.h" +#include "permute_tx.h" +#include "bitcoin/signature.h" +#include "commit_tx.h" +#include "bitcoin/pubkey.h" +#include "find_p2sh_out.h" +#include "protobuf_convert.h" +#include + +int main(int argc, char *argv[]) +{ + const tal_t *ctx = tal_arr(NULL, char, 0); + struct sha256 seed, htlc_rhash, revocation_hash; + struct pkt *pkt; + unsigned update_num; + UpdateAddHtlc *u; + + err_set_progname(argv[0]); + + opt_register_noarg("--help|-h", opt_usage_and_exit, + " \n" + "Create a new HTLC remove message", + "Print this message."); + + opt_parse(&argc, argv, opt_log_stderr_exit); + + if (argc != 4) + opt_usage_exit_fail("Expected 3 arguments"); + + if (!hex_decode(argv[1], strlen(argv[1]), &seed, sizeof(seed))) + errx(1, "Invalid seed '%s' - need 256 hex bits", argv[1]); + update_num = atoi(argv[2]); + if (!update_num) + errx(1, "Update number %s invalid", argv[2]); + + u = pkt_from_file(argv[3], PKT__PKT_UPDATE_ADD_HTLC)->update_add_htlc; + proto_to_sha256(u->r_hash, &htlc_rhash); + + /* Get next revocation hash. */ + shachain_from_seed(&seed, update_num, &revocation_hash); + sha256(&revocation_hash, + revocation_hash.u.u8, sizeof(revocation_hash.u.u8)); + + pkt = update_htlc_remove_pkt(ctx, &revocation_hash, &htlc_rhash); + if (!write_all(STDOUT_FILENO, pkt, pkt_totlen(pkt))) + err(1, "Writing out packet"); + + tal_free(ctx); + return 0; +} +