core-lightning/bitcoin/tx.h
Christian Decker 60fbf1f7d0 wally: Add shims to generate both transaction versions in parallel
We are slowly migrating towards a wally-transactions only world, but to make
this reviewable we start building both old and new style transactions in
parallel. In a second pass we'll then start removing the old ones and use
libwally only.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00

96 lines
2.8 KiB
C

#ifndef LIGHTNING_BITCOIN_TX_H
#define LIGHTNING_BITCOIN_TX_H
#include "config.h"
#include "shadouble.h"
#include "signature.h"
#include "varint.h"
#include <ccan/short_types/short_types.h>
#include <ccan/structeq/structeq.h>
#include <ccan/tal/tal.h>
#include <common/amount.h>
#include <wally_transaction.h>
#define BITCOIN_TX_DEFAULT_SEQUENCE 0xFFFFFFFF
struct bitcoin_txid {
struct sha256_double shad;
};
/* Define bitcoin_txid_eq */
STRUCTEQ_DEF(bitcoin_txid, 0, shad.sha.u);
struct bitcoin_tx {
struct bitcoin_tx_input *input;
struct bitcoin_tx_output *output;
struct wally_tx *wtx;
/* Keep track of how many inputs and outputs were filled so far. This
* is needed since we allocate them in bulk. */
size_t used_inputs, used_outputs;
};
struct bitcoin_tx_output {
struct amount_sat amount;
u8 *script;
};
struct bitcoin_tx_input {
struct bitcoin_txid txid;
u32 index; /* output number referred to by above */
u8 *script;
u32 sequence_number;
/* Value of the output we're spending (NULL if unknown). */
struct amount_sat *amount;
/* Only if BIP141 used. */
u8 **witness;
};
/* SHA256^2 the tx: simpler than sha256_tx */
void bitcoin_txid(const struct bitcoin_tx *tx, struct bitcoin_txid *txid);
/* Useful for signature code. Only supports SIGHASH_ALL and
* (for segwit) SIGHASH_SINGLE|SIGHASH_ANYONECANPAY. */
void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx,
unsigned int input_num,
const u8 *script,
const u8 *witness_script,
enum sighash_type sighash_type);
/* Linear bytes of tx. */
u8 *linearize_tx(const tal_t *ctx, const struct bitcoin_tx *tx);
/* Get weight of tx in Sipa. */
size_t measure_tx_weight(const struct bitcoin_tx *tx);
/* Allocate a tx: you just need to fill in inputs and outputs (they're
* zeroed with inputs' sequence_number set to FFFFFFFF) */
struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count,
varint_t output_count);
/* This takes a raw bitcoin tx in hex. */
struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex,
size_t hexlen);
/* Parse hex string to get txid (reversed, a-la bitcoind). */
bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len,
struct bitcoin_txid *txid);
/* Get hex string of txid (reversed, a-la bitcoind). */
bool bitcoin_txid_to_hex(const struct bitcoin_txid *txid,
char *hexstr, size_t hexstr_len);
/* Internal de-linearization functions. */
struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx,
const u8 **cursor, size_t *max);
int bitcoin_tx_add_output(struct bitcoin_tx *tx, u8 *script,
struct amount_sat *amount);
int bitcoin_tx_add_input(struct bitcoin_tx *tx, const struct bitcoin_txid *txid,
u32 outnum, u32 sequence,
const struct amount_sat *amount, u8 *script);
#endif /* LIGHTNING_BITCOIN_TX_H */