Skip to content

Commit

Permalink
test-cli/txid-of: simple helper to get txid.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jul 24, 2015
1 parent 114161a commit ee3af28
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
4 changes: 4 additions & 0 deletions bitcoin/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
43 changes: 43 additions & 0 deletions test-cli/txid-of.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <ccan/crypto/shachain/shachain.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <ccan/opt/opt.h>
#include <ccan/str/hex/hex.h>
#include <ccan/err/err.h>
#include "bitcoin/tx.h"
#include <unistd.h>

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,
"<tx>\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;
}

0 comments on commit ee3af28

Please sign in to comment.