mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 21:35:11 +01:00
wally: Add accessor methods for script and amount
These are handled internally in the `wally_tx` and do not conform to our usual tallocated strings that can by inspected using `tal_bytelen`, and we don't really want to litter our code with whitelisting comments for the `amount_sat.satoshis` access, so these just do read-only on the fly conversions. Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
parent
64273b5ec5
commit
8d0500228e
44
bitcoin/tx.c
44
bitcoin/tx.c
@ -93,6 +93,27 @@ void bitcoin_tx_output_set_amount(struct bitcoin_tx *tx, int outnum,
|
||||
tx->wtx->outputs[outnum].satoshi = amount->satoshis; /* Raw: low-level helper */
|
||||
}
|
||||
|
||||
const u8 *bitcoin_tx_output_get_script(const tal_t *ctx,
|
||||
const struct bitcoin_tx *tx, int outnum)
|
||||
{
|
||||
const struct wally_tx_output *output;
|
||||
u8 *res;
|
||||
assert(outnum < tx->wtx->num_outputs);
|
||||
output = &tx->wtx->outputs[outnum];
|
||||
res = tal_arr(ctx, u8, output->script_len);
|
||||
memcpy(res, output->script, output->script_len);
|
||||
return res;
|
||||
}
|
||||
|
||||
struct amount_sat bitcoin_tx_output_get_amount(const struct bitcoin_tx *tx,
|
||||
int outnum)
|
||||
{
|
||||
struct amount_sat amount;
|
||||
assert(outnum < tx->wtx->num_outputs);
|
||||
amount.satoshis = tx->wtx->outputs[outnum].satoshi; /* Raw: helper */
|
||||
return amount;
|
||||
}
|
||||
|
||||
void bitcoin_tx_input_set_witness(struct bitcoin_tx *tx, int innum,
|
||||
u8 **witness)
|
||||
{
|
||||
@ -120,6 +141,29 @@ void bitcoin_tx_input_set_script(struct bitcoin_tx *tx, int innum, u8 *script)
|
||||
wally_tx_set_input_script(tx->wtx, innum, script, tal_bytelen(script));
|
||||
}
|
||||
|
||||
const u8 *bitcoin_tx_input_get_witness(const tal_t *ctx,
|
||||
const struct bitcoin_tx *tx, int innum,
|
||||
int witnum)
|
||||
{
|
||||
const u8 *witness_item;
|
||||
struct wally_tx_witness_item *item;
|
||||
assert(innum < tx->wtx->num_inputs);
|
||||
assert(witnum < tx->wtx->inputs[innum].witness->num_items);
|
||||
item = &tx->wtx->inputs[innum].witness->items[witnum];
|
||||
witness_item =
|
||||
tal_dup_arr(ctx, u8, item->witness, item->witness_len, 0);
|
||||
return witness_item;
|
||||
}
|
||||
|
||||
void bitcoin_tx_input_get_txid(const struct bitcoin_tx *tx, int innum,
|
||||
struct bitcoin_txid *out)
|
||||
{
|
||||
assert(innum < tx->wtx->num_inputs);
|
||||
assert(sizeof(struct bitcoin_txid) ==
|
||||
sizeof(tx->wtx->inputs[innum].txhash));
|
||||
memcpy(out, tx->wtx->inputs[innum].txhash, sizeof(struct bitcoin_txid));
|
||||
}
|
||||
|
||||
/* BIP 141:
|
||||
* It is followed by stack items, with each item starts with a var_int
|
||||
* to indicate the length. */
|
||||
|
29
bitcoin/tx.h
29
bitcoin/tx.h
@ -95,6 +95,22 @@ int bitcoin_tx_add_input(struct bitcoin_tx *tx, const struct bitcoin_txid *txid,
|
||||
void bitcoin_tx_output_set_amount(struct bitcoin_tx *tx, int outnum,
|
||||
struct amount_sat *amount);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* Helper to just get an amount_sat for the output amount.
|
||||
*/
|
||||
struct amount_sat bitcoin_tx_output_get_amount(const struct bitcoin_tx *tx,
|
||||
int outnum);
|
||||
|
||||
/**
|
||||
* Set the input witness.
|
||||
*
|
||||
@ -109,6 +125,19 @@ void bitcoin_tx_input_set_witness(struct bitcoin_tx *tx, int innum,
|
||||
*/
|
||||
void bitcoin_tx_input_set_script(struct bitcoin_tx *tx, int innum, u8 *script);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* Check a transaction for consistency.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user