Browse Source

wally: Add setters for output amounts, input witnesses and scripts

These are used when grinding the feerate and signing. These are just simple
facades that keep both wally and old style transactions in sync.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
pr-2587
Christian Decker 6 years ago
committed by Rusty Russell
parent
commit
03329a61da
  1. 35
      bitcoin/tx.c
  2. 25
      bitcoin/tx.h

35
bitcoin/tx.c

@ -84,6 +84,41 @@ bool bitcoin_tx_check(const struct bitcoin_tx *tx)
return memeq(oldtx, tal_bytelen(oldtx), newtx, tal_bytelen(newtx)); return memeq(oldtx, tal_bytelen(oldtx), newtx, tal_bytelen(newtx));
} }
void bitcoin_tx_output_set_amount(struct bitcoin_tx *tx, int outnum,
struct amount_sat *amount)
{
assert(outnum < tx->used_outputs);
tx->output[outnum].amount = *amount;
tx->wtx->outputs[outnum].satoshi = amount->satoshis; /* Raw: low-level helper */
}
void bitcoin_tx_input_set_witness(struct bitcoin_tx *tx, int innum,
u8 **witness)
{
struct wally_tx_witness_stack *stack = NULL;
size_t stack_size = tal_count(witness);
/* Free any lingering witness */
tal_free(tx->input[innum].witness);
tx->input[innum].witness = witness;
if (witness) {
wally_tx_witness_stack_init_alloc(stack_size, &stack);
for (size_t i = 0; i < stack_size; i++)
wally_tx_witness_stack_add(stack, witness[i],
tal_bytelen(witness[i]));
}
wally_tx_set_input_witness(tx->wtx, innum, stack);
if (stack)
wally_tx_witness_stack_free(stack);
}
void bitcoin_tx_input_set_script(struct bitcoin_tx *tx, int innum, u8 *script)
{
tx->input[innum].script = script;
wally_tx_set_input_script(tx->wtx, innum, script, tal_bytelen(script));
}
static void push_tx_input(const struct bitcoin_tx_input *input, static void push_tx_input(const struct bitcoin_tx_input *input,
const u8 *input_script, const u8 *input_script,
void (*push)(const void *, size_t, void *), void *pushp) void (*push)(const void *, size_t, void *), void *pushp)

25
bitcoin/tx.h

@ -92,6 +92,31 @@ int bitcoin_tx_add_input(struct bitcoin_tx *tx, const struct bitcoin_txid *txid,
u32 outnum, u32 sequence, u32 outnum, u32 sequence,
const struct amount_sat *amount, u8 *script); const struct amount_sat *amount, u8 *script);
/**
* 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,
struct amount_sat *amount);
/**
* 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,
u8 **witness);
/**
* Set the input script on the given input.
*/
void bitcoin_tx_input_set_script(struct bitcoin_tx *tx, int innum, u8 *script);
/** /**
* Check a transaction for consistency. * Check a transaction for consistency.
* *

Loading…
Cancel
Save