diff --git a/.gitignore b/.gitignore index 928f0a382..aa083cd26 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ update-channel-accept update-channel-signature update-channel-complete create-commit-tx +txid-of ccan/tools/configurator/configurator libsecp256k1.a libsecp256k1.la diff --git a/Makefile b/Makefile index 4348c0b8b..e3087faeb 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ FEATURES := -DHAS_CSV=1 -DALPHA_TXSTYLE=1 -DUSE_SCHNORR=1 # Bitcoin uses DER for signatures #FEATURES := -DSCRIPTS_USE_DER -PROGRAMS := test-cli/open-channel test-cli/open-anchor-scriptsigs test-cli/open-commit-sig test-cli/check-commit-sig test-cli/check-anchor-scriptsigs 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 +PROGRAMS := test-cli/open-channel test-cli/open-anchor-scriptsigs test-cli/open-commit-sig test-cli/check-commit-sig test-cli/check-anchor-scriptsigs 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 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/bitcoin/tx.c b/bitcoin/tx.c index 91cd15f26..af4336866 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -503,6 +503,14 @@ bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len, return true; } +bool bitcoin_txid_to_hex(const struct sha256_double *txid, + char *hexstr, size_t hexstr_len) +{ + struct sha256_double rev = *txid; + reverse_bytes(rev.sha.u.u8, sizeof(rev.sha.u.u8)); + return hex_encode(&rev, sizeof(rev), hexstr, hexstr_len); +} + static bool write_input_amounts(int fd, const struct bitcoin_tx *tx) { /* Alpha required input amounts, so append them */ diff --git a/bitcoin/tx.h b/bitcoin/tx.h index 4397226c8..b490b337a 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -64,4 +64,8 @@ bool bitcoin_tx_write(int fd, const struct bitcoin_tx *tx); bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len, struct sha256_double *txid); +/* Get hex string of txid (reversed, a-la bitcoind). */ +bool bitcoin_txid_to_hex(const struct sha256_double *txid, + char *hexstr, size_t hexstr_len); + #endif /* LIGHTNING_BITCOIN_TX_H */ diff --git a/test-cli/txid-of.c b/test-cli/txid-of.c new file mode 100644 index 000000000..7f79bc48f --- /dev/null +++ b/test-cli/txid-of.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include +#include +#include "bitcoin/tx.h" +#include + +int main(int argc, char *argv[]) +{ + const tal_t *ctx = tal_arr(NULL, char, 0); + struct bitcoin_tx *tx; + struct sha256_double txid; + char str[hex_str_size(sizeof(txid))]; + + err_set_progname(argv[0]); + + /* FIXME: Take update.pbs to adjust channel */ + opt_register_noarg("--help|-h", opt_usage_and_exit, + "\n" + "Print txid of the transaction in the file", + "Print this message."); + + opt_parse(&argc, argv, opt_log_stderr_exit); + + if (argc != 2) + opt_usage_exit_fail("Expected 1 argument"); + + tx = bitcoin_tx_from_file(ctx, argv[1]); + bitcoin_txid(tx, &txid); + + if (!bitcoin_txid_to_hex(&txid, str, sizeof(str))) + abort(); + + /* Print it out. */ + if (!write_all(STDOUT_FILENO, str, strlen(str))) + err(1, "Writing out txid"); + + tal_free(ctx); + return 0; +}