2015-05-28 23:38:27 +02:00
|
|
|
#ifndef LIGHTNING_BITCOIN_TX_H
|
|
|
|
#define LIGHTNING_BITCOIN_TX_H
|
2016-01-21 21:08:08 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include "shadouble.h"
|
2016-04-11 09:15:53 +02:00
|
|
|
#include "signature.h"
|
2016-04-12 05:37:03 +02:00
|
|
|
#include "varint.h"
|
2015-05-28 23:38:27 +02:00
|
|
|
#include <ccan/short_types/short_types.h>
|
2018-07-04 07:30:02 +02:00
|
|
|
#include <ccan/structeq/structeq.h>
|
2015-05-29 04:03:18 +02:00
|
|
|
#include <ccan/tal/tal.h>
|
2019-02-21 04:45:55 +01:00
|
|
|
#include <common/amount.h>
|
2020-05-21 03:57:00 +02:00
|
|
|
#include <wally_psbt.h>
|
2019-03-01 13:23:22 +01:00
|
|
|
#include <wally_transaction.h>
|
2015-05-28 23:38:27 +02:00
|
|
|
|
2019-03-05 14:28:29 +01:00
|
|
|
#define BITCOIN_TX_DEFAULT_SEQUENCE 0xFFFFFFFF
|
2020-07-15 07:34:49 +02:00
|
|
|
|
|
|
|
/* BIP 125: Any nsequence < 0xFFFFFFFE is replacable.
|
|
|
|
* And bitcoind uses this value. */
|
|
|
|
#define BITCOIN_TX_RBF_SEQUENCE 0xFFFFFFFD
|
2020-05-21 03:57:00 +02:00
|
|
|
struct wally_psbt;
|
2019-03-05 14:28:29 +01:00
|
|
|
|
2017-12-18 07:41:52 +01:00
|
|
|
struct bitcoin_txid {
|
|
|
|
struct sha256_double shad;
|
|
|
|
};
|
2018-07-04 07:30:02 +02:00
|
|
|
/* Define bitcoin_txid_eq */
|
|
|
|
STRUCTEQ_DEF(bitcoin_txid, 0, shad.sha.u);
|
2017-12-18 07:41:52 +01:00
|
|
|
|
2015-05-28 23:38:27 +02:00
|
|
|
struct bitcoin_tx {
|
2019-03-01 13:23:22 +01:00
|
|
|
struct wally_tx *wtx;
|
2019-07-30 16:14:43 +02:00
|
|
|
|
|
|
|
/* Keep a reference to the ruleset we have to abide by */
|
|
|
|
const struct chainparams *chainparams;
|
2020-05-21 03:57:00 +02:00
|
|
|
|
|
|
|
/* psbt struct */
|
|
|
|
struct wally_psbt *psbt;
|
2015-05-28 23:38:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct bitcoin_tx_output {
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat amount;
|
2015-05-28 23:38:27 +02:00
|
|
|
u8 *script;
|
|
|
|
};
|
|
|
|
|
2019-09-17 02:08:05 +02:00
|
|
|
struct bitcoin_tx_output *new_tx_output(const tal_t *ctx,
|
|
|
|
struct amount_sat amount,
|
|
|
|
const u8 *script);
|
|
|
|
|
2020-05-26 05:39:25 +02:00
|
|
|
/* SHA256^2 the tx in legacy format. */
|
2017-12-18 07:41:52 +01:00
|
|
|
void bitcoin_txid(const struct bitcoin_tx *tx, struct bitcoin_txid *txid);
|
2020-05-26 05:39:25 +02:00
|
|
|
void wally_txid(const struct wally_tx *wtx, struct bitcoin_txid *txid);
|
2015-06-02 05:29:13 +02:00
|
|
|
|
2015-06-02 05:46:05 +02:00
|
|
|
/* Linear bytes of tx. */
|
|
|
|
u8 *linearize_tx(const tal_t *ctx, const struct bitcoin_tx *tx);
|
2020-05-26 05:39:25 +02:00
|
|
|
u8 *linearize_wtx(const tal_t *ctx, const struct wally_tx *wtx);
|
2015-06-02 05:46:05 +02:00
|
|
|
|
2018-02-03 01:16:23 +01:00
|
|
|
/* Get weight of tx in Sipa. */
|
2019-07-01 16:02:04 +02:00
|
|
|
size_t bitcoin_tx_weight(const struct bitcoin_tx *tx);
|
2016-04-24 12:23:35 +02:00
|
|
|
|
2015-05-29 04:03:18 +02:00
|
|
|
/* Allocate a tx: you just need to fill in inputs and outputs (they're
|
|
|
|
* zeroed with inputs' sequence_number set to FFFFFFFF) */
|
2019-07-30 16:14:43 +02:00
|
|
|
struct bitcoin_tx *bitcoin_tx(const tal_t *ctx,
|
|
|
|
const struct chainparams *chainparams,
|
2020-01-29 12:59:06 +01:00
|
|
|
varint_t input_count, varint_t output_count,
|
|
|
|
u32 nlocktime);
|
2015-05-29 04:03:18 +02:00
|
|
|
|
2016-04-11 09:02:43 +02:00
|
|
|
/* This takes a raw bitcoin tx in hex. */
|
2016-01-21 21:11:47 +01:00
|
|
|
struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex,
|
|
|
|
size_t hexlen);
|
2015-06-04 06:18:11 +02:00
|
|
|
|
2015-06-05 05:05:46 +02:00
|
|
|
/* Parse hex string to get txid (reversed, a-la bitcoind). */
|
|
|
|
bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len,
|
2017-12-18 07:41:52 +01:00
|
|
|
struct bitcoin_txid *txid);
|
2015-06-05 05:05:46 +02:00
|
|
|
|
2015-07-24 08:30:10 +02:00
|
|
|
/* Get hex string of txid (reversed, a-la bitcoind). */
|
2017-12-18 07:41:52 +01:00
|
|
|
bool bitcoin_txid_to_hex(const struct bitcoin_txid *txid,
|
2015-07-24 08:30:10 +02:00
|
|
|
char *hexstr, size_t hexstr_len);
|
|
|
|
|
2020-05-22 05:18:33 +02:00
|
|
|
/* Create a bitcoin_tx from a psbt */
|
|
|
|
struct bitcoin_tx *bitcoin_tx_with_psbt(const tal_t *ctx, struct wally_psbt *psbt);
|
|
|
|
|
2016-04-24 12:10:29 +02:00
|
|
|
/* Internal de-linearization functions. */
|
|
|
|
struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx,
|
|
|
|
const u8 **cursor, size_t *max);
|
2020-08-07 03:21:33 +02:00
|
|
|
|
|
|
|
/* Helper to create a wally_tx_output: make sure to wally_tx_output_free!
|
|
|
|
* Returns NULL if amount is extreme (wally doesn't like).
|
|
|
|
*/
|
2020-09-21 12:24:26 +02:00
|
|
|
struct wally_tx_output *wally_tx_output(const tal_t *ctx,
|
|
|
|
const u8 *script,
|
2020-08-07 03:21:33 +02:00
|
|
|
struct amount_sat amount);
|
|
|
|
|
2019-08-12 21:02:06 +02:00
|
|
|
/* Add one output to tx. */
|
2019-06-05 07:28:55 +02:00
|
|
|
int bitcoin_tx_add_output(struct bitcoin_tx *tx, const u8 *script,
|
2020-05-21 03:57:00 +02:00
|
|
|
u8 *wscript,
|
2019-08-21 05:52:44 +02:00
|
|
|
struct amount_sat amount);
|
2019-03-14 16:20:03 +01:00
|
|
|
|
2019-08-12 21:02:06 +02:00
|
|
|
/* Add mutiple output to tx. */
|
|
|
|
int bitcoin_tx_add_multi_outputs(struct bitcoin_tx *tx,
|
|
|
|
struct bitcoin_tx_output **outputs);
|
|
|
|
|
2020-05-22 05:16:11 +02:00
|
|
|
/* Set the locktime for a transaction */
|
|
|
|
void bitcoin_tx_set_locktime(struct bitcoin_tx *tx, u32 locktime);
|
|
|
|
|
2020-05-21 21:46:19 +02:00
|
|
|
/* Add a new input to a bitcoin tx.
|
|
|
|
*
|
|
|
|
* For P2WSH inputs, we'll also store the wscript and/or scriptPubkey
|
|
|
|
* Passing in just the {input_wscript}, we'll generate the scriptPubkey for you.
|
|
|
|
* In some cases we may not have the wscript, in which case the scriptPubkey
|
|
|
|
* should be provided. We'll check that it's P2WSH before saving it */
|
2019-03-14 16:20:03 +01:00
|
|
|
int bitcoin_tx_add_input(struct bitcoin_tx *tx, const struct bitcoin_txid *txid,
|
2020-05-21 21:46:19 +02:00
|
|
|
u32 outnum, u32 sequence, const u8 *scriptSig,
|
|
|
|
struct amount_sat amount, const u8 *scriptPubkey,
|
|
|
|
const u8 *input_wscript);
|
2019-03-14 16:20:03 +01:00
|
|
|
|
2020-05-26 05:39:25 +02:00
|
|
|
/* This helps is useful because wally uses a raw byte array for txids */
|
|
|
|
bool wally_tx_input_spends(const struct wally_tx_input *input,
|
|
|
|
const struct bitcoin_txid *txid,
|
|
|
|
int outnum);
|
|
|
|
|
|
|
|
struct amount_asset
|
|
|
|
wally_tx_output_get_amount(const struct wally_tx_output *output);
|
2019-03-20 16:41:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the output amount on the transaction.
|
|
|
|
*
|
|
|
|
* Allows changing the amount on the transaction output after it was set on
|
|
|
|
* creation. This is useful to grind a feerate or subtract the fee from an
|
|
|
|
* existing output.
|
|
|
|
*/
|
|
|
|
void bitcoin_tx_output_set_amount(struct bitcoin_tx *tx, int outnum,
|
2019-08-21 05:52:44 +02:00
|
|
|
struct amount_sat amount);
|
2019-03-20 16:41:29 +01:00
|
|
|
|
2019-03-22 22:45:41 +01:00
|
|
|
/**
|
|
|
|
* Helper to get the script of a script's output as a tal_arr
|
|
|
|
*
|
|
|
|
* Internally we use a `wally_tx` to represent the transaction. The script
|
|
|
|
* attached to a `wally_tx_output` is not a `tal_arr`, so in order to keep the
|
|
|
|
* comfort of being able to call `tal_bytelen` and similar on a script we just
|
|
|
|
* return a `tal_arr` clone of the original script.
|
|
|
|
*/
|
|
|
|
const u8 *bitcoin_tx_output_get_script(const tal_t *ctx, const struct bitcoin_tx *tx, int outnum);
|
|
|
|
|
2020-06-16 20:40:13 +02:00
|
|
|
/**
|
|
|
|
* Helper to get the script of a script's output as a tal_arr
|
|
|
|
*
|
|
|
|
* The script attached to a `wally_tx_output` is not a `tal_arr`, so in order to keep the
|
|
|
|
* comfort of being able to call `tal_bytelen` and similar on a script we just
|
|
|
|
* return a `tal_arr` clone of the original script.
|
|
|
|
*/
|
|
|
|
const u8 *wally_tx_output_get_script(const tal_t *ctx,
|
|
|
|
const struct wally_tx_output *output);
|
2020-05-21 03:57:00 +02:00
|
|
|
/**
|
|
|
|
* Helper to get a witness script for an output.
|
|
|
|
*/
|
2020-05-21 05:57:54 +02:00
|
|
|
u8 *bitcoin_tx_output_get_witscript(const tal_t *ctx, const struct bitcoin_tx *tx, int outnum);
|
2020-05-21 03:57:00 +02:00
|
|
|
|
2019-11-14 17:38:17 +01:00
|
|
|
/** bitcoin_tx_output_get_amount_sat - Helper to get transaction output's amount
|
|
|
|
*
|
|
|
|
* Internally we use a `wally_tx` to represent the transaction. The
|
|
|
|
* satoshi amount isn't a struct amount_sat, so we need a conversion
|
|
|
|
*/
|
2020-09-08 05:22:02 +02:00
|
|
|
void bitcoin_tx_output_get_amount_sat(const struct bitcoin_tx *tx, int outnum,
|
2019-11-14 17:38:17 +01:00
|
|
|
struct amount_sat *amount);
|
2019-03-22 22:45:41 +01:00
|
|
|
/**
|
|
|
|
* Helper to just get an amount_sat for the output amount.
|
|
|
|
*/
|
2019-09-26 21:07:20 +02:00
|
|
|
struct amount_asset bitcoin_tx_output_get_amount(const struct bitcoin_tx *tx,
|
|
|
|
int outnum);
|
2019-03-22 22:45:41 +01:00
|
|
|
|
2019-03-20 16:41:29 +01:00
|
|
|
/**
|
|
|
|
* Set the input witness.
|
|
|
|
*
|
|
|
|
* Given that we generate the witness after constructing the transaction
|
|
|
|
* itself, we need a way to attach a witness to an existing input.
|
|
|
|
*/
|
|
|
|
void bitcoin_tx_input_set_witness(struct bitcoin_tx *tx, int innum,
|
2019-08-21 05:52:42 +02:00
|
|
|
u8 **witness TAKES);
|
2019-03-20 16:41:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the input script on the given input.
|
|
|
|
*/
|
|
|
|
void bitcoin_tx_input_set_script(struct bitcoin_tx *tx, int innum, u8 *script);
|
|
|
|
|
2019-03-22 22:45:41 +01:00
|
|
|
/**
|
|
|
|
* Helper to get a witness as a tal_arr array.
|
|
|
|
*/
|
|
|
|
const u8 *bitcoin_tx_input_get_witness(const tal_t *ctx,
|
|
|
|
const struct bitcoin_tx *tx, int innum,
|
|
|
|
int witnum);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrap the raw txhash in the wally_tx_input into a bitcoin_txid
|
|
|
|
*/
|
|
|
|
void bitcoin_tx_input_get_txid(const struct bitcoin_tx *tx, int innum,
|
|
|
|
struct bitcoin_txid *out);
|
2020-05-26 05:39:25 +02:00
|
|
|
void wally_tx_input_get_txid(const struct wally_tx_input *in,
|
|
|
|
struct bitcoin_txid *txid);
|
2019-03-22 22:45:41 +01:00
|
|
|
|
2019-03-15 14:09:11 +01:00
|
|
|
/**
|
|
|
|
* Check a transaction for consistency.
|
|
|
|
*
|
|
|
|
* Mainly for the transition from `bitcoin_tx` to the `wally_tx`. Checks that
|
|
|
|
* both transactions serialize to two identical representations.
|
|
|
|
*/
|
|
|
|
bool bitcoin_tx_check(const struct bitcoin_tx *tx);
|
|
|
|
|
2020-02-08 21:26:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Finalize a transaction by truncating overallocated and temporary
|
|
|
|
* fields. This includes adding a fee output for elements transactions or
|
|
|
|
* adjusting an existing fee output, and resizing metadata arrays for inputs
|
|
|
|
* and outputs.
|
|
|
|
*/
|
|
|
|
void bitcoin_tx_finalize(struct bitcoin_tx *tx);
|
|
|
|
|
2020-09-10 00:37:31 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the given outnum is a fee output
|
|
|
|
*/
|
|
|
|
bool elements_wtx_output_is_fee(const struct wally_tx *tx, int outnum);
|
|
|
|
|
2020-04-02 03:34:00 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the given outnum is a fee output
|
|
|
|
*/
|
|
|
|
bool elements_tx_output_is_fee(const struct bitcoin_tx *tx, int outnum);
|
|
|
|
|
2020-04-02 03:35:04 +02:00
|
|
|
/**
|
|
|
|
* Calculate the fees for this transaction
|
|
|
|
*/
|
|
|
|
struct amount_sat bitcoin_tx_compute_fee(const struct bitcoin_tx *tx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the fees for this transaction, given a pre-computed input balance.
|
|
|
|
*
|
2020-05-21 21:49:14 +02:00
|
|
|
* This is needed for cases where the transaction's psbt metadata isn't properly filled
|
|
|
|
* in typically due to being instantiated from a tx hex (i.e. from a block scan)
|
2020-04-02 03:35:04 +02:00
|
|
|
*/
|
|
|
|
struct amount_sat bitcoin_tx_compute_fee_w_inputs(const struct bitcoin_tx *tx,
|
|
|
|
struct amount_sat input_val);
|
|
|
|
|
2020-05-15 12:30:43 +02:00
|
|
|
/* Wire marshalling and unmarshalling */
|
|
|
|
void fromwire_bitcoin_txid(const u8 **cursor, size_t *max,
|
|
|
|
struct bitcoin_txid *txid);
|
|
|
|
struct bitcoin_tx *fromwire_bitcoin_tx(const tal_t *ctx,
|
|
|
|
const u8 **cursor, size_t *max);
|
|
|
|
struct bitcoin_tx_output *fromwire_bitcoin_tx_output(const tal_t *ctx,
|
|
|
|
const u8 **cursor, size_t *max);
|
|
|
|
void towire_bitcoin_txid(u8 **pptr, const struct bitcoin_txid *txid);
|
|
|
|
void towire_bitcoin_tx(u8 **pptr, const struct bitcoin_tx *tx);
|
|
|
|
void towire_bitcoin_tx_output(u8 **pptr, const struct bitcoin_tx_output *output);
|
|
|
|
|
2020-07-06 07:26:14 +02:00
|
|
|
/* Various weights of transaction parts. */
|
|
|
|
size_t bitcoin_tx_core_weight(size_t num_inputs, size_t num_outputs);
|
|
|
|
size_t bitcoin_tx_output_weight(size_t outscript_len);
|
|
|
|
|
|
|
|
/* Weight to push sig on stack. */
|
|
|
|
size_t bitcoin_tx_input_sig_weight(void);
|
|
|
|
|
|
|
|
/* We only do segwit inputs, and we assume witness is sig + key */
|
|
|
|
size_t bitcoin_tx_simple_input_weight(bool p2sh);
|
|
|
|
|
2020-07-07 22:50:26 +02:00
|
|
|
/**
|
|
|
|
* change_amount - Is it worth making a P2WPKH change output at this feerate?
|
|
|
|
* @excess: input amount we have above the tx fee and other outputs.
|
|
|
|
* @feerate_perkw: feerate.
|
|
|
|
*
|
|
|
|
* If it's not worth (or possible) to make change, returns AMOUNT_SAT(0).
|
|
|
|
* Otherwise returns the amount of the change output to add (@excess minus
|
|
|
|
* the additional fee for the change output itself).
|
|
|
|
*/
|
|
|
|
struct amount_sat change_amount(struct amount_sat excess, u32 feerate_perkw);
|
|
|
|
|
2015-05-28 23:38:27 +02:00
|
|
|
#endif /* LIGHTNING_BITCOIN_TX_H */
|