From 5778b004dd2c1a55455b8347c5f694c543c496b1 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: new util. Signed-off-by: Rusty Russell --- Makefile | 2 +- pkt.c | 19 +++++++++ pkt.h | 14 +++++++ test-cli/.gitignore | 1 + test-cli/update-channel-htlc.c | 74 ++++++++++++++++++++++++++++++++++ 5 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 test-cli/update-channel-htlc.c diff --git a/Makefile b/Makefile index ac39ea5aa..43f284444 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 +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 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 a224e8cd4..0229a522d 100644 --- a/pkt.c +++ b/pkt.c @@ -140,6 +140,25 @@ struct pkt *update_pkt(const tal_t *ctx, return to_pkt(ctx, PKT__PKT_UPDATE, &u); } +struct pkt *update_htlc_add_pkt(const tal_t *ctx, + const struct sha256 *revocation_hash, + u64 value, + const struct sha256 *htlc_rhash, + u32 abs_locktime_seconds) +{ + UpdateAddHtlc u = UPDATE_ADD_HTLC__INIT; + Locktime l = LOCKTIME__INIT; + + u.revocation_hash = sha256_to_proto(ctx, revocation_hash); + u.amount = value; + u.r_hash = sha256_to_proto(ctx, htlc_rhash); + l.locktime_case = LOCKTIME__LOCKTIME_SECONDS; + l.seconds = abs_locktime_seconds; + u.expiry = &l; + + return to_pkt(ctx, PKT__PKT_UPDATE_ADD_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 4d300db0a..c39ff24ab 100644 --- a/pkt.h +++ b/pkt.h @@ -90,6 +90,20 @@ struct pkt *update_pkt(const tal_t *ctx, const struct sha256 *revocation_hash, s64 delta); +/** + * update_htlc_add_pkt - create an update message adding a HTLC + * @ctx: tal context to allocate off. + * @revocation_hash: the revocation hash for the next commitment tx. + * @val: the change in satoshis (from me). + * @htlc_rhash: the hash of the htlc secret. + * @abs_locktime_seconds: the HTLC timeout. + */ +struct pkt *update_htlc_add_pkt(const tal_t *ctx, + const struct sha256 *revocation_hash, + u64 value, + const struct sha256 *htlc_rhash, + u32 abs_locktime_seconds); + /** * 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 aed1548f3..63726a857 100644 --- a/test-cli/.gitignore +++ b/test-cli/.gitignore @@ -17,3 +17,4 @@ create-commit-tx open-anchor txid-of create-anchor-tx +update-channel-htlc diff --git a/test-cli/update-channel-htlc.c b/test-cli/update-channel-htlc.c new file mode 100644 index 000000000..a4f5540ec --- /dev/null +++ b/test-cli/update-channel-htlc.c @@ -0,0 +1,74 @@ +#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 + +int main(int argc, char *argv[]) +{ + const tal_t *ctx = tal_arr(NULL, char, 0); + struct sha256 seed, revocation_hash, rval, rhash; + struct pkt *pkt; + uint64_t htlc_val; + u32 locktime_seconds; + unsigned update_num; + char *endp; + + err_set_progname(argv[0]); + + opt_register_noarg("--help|-h", opt_usage_and_exit, + " \n" + "Create a new HTLC update message", + "Print this message."); + + opt_parse(&argc, argv, opt_log_stderr_exit); + + if (argc != 6) + opt_usage_exit_fail("Expected 5 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]); + + htlc_val = strtol(argv[3], &endp, 10); + if (*endp || !htlc_val) + errx(1, "Expected number for %s", argv[3]); + + if (!hex_decode(argv[4], strlen(argv[4]), &rval, sizeof(rval))) + errx(1, "Invalid rvalue '%s' - need 256 hex bits", argv[4]); + + locktime_seconds = strtol(argv[5], &endp, 10); + if (*endp || !locktime_seconds) + errx(1, "Expected locktime for %s", argv[5]); + + /* Get next revocation hash. */ + shachain_from_seed(&seed, update_num, &revocation_hash); + sha256(&revocation_hash, + revocation_hash.u.u8, sizeof(revocation_hash.u.u8)); + + /* Get htlc rvalue hash. */ + sha256(&rhash, &rval, sizeof(rval)); + + pkt = update_htlc_add_pkt(ctx, &revocation_hash, htlc_val, + &rhash, locktime_seconds); + if (!write_all(STDOUT_FILENO, pkt, pkt_totlen(pkt))) + err(1, "Writing out packet"); + + tal_free(ctx); + return 0; +} +