Skip to content

Commit

Permalink
bitcoin: add wally_tx_output helper to create standalone output.
Browse files Browse the repository at this point in the history
In preparation for when we don't have a tx.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and niftynei committed Aug 6, 2020
1 parent 00de7fd commit 03d034d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
38 changes: 26 additions & 12 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,12 @@ struct bitcoin_tx_output *new_tx_output(const tal_t *ctx,
return output;
}

int bitcoin_tx_add_output(struct bitcoin_tx *tx, const u8 *script,
u8 *wscript, struct amount_sat amount)
struct wally_tx_output *wally_tx_output(const u8 *script,
struct amount_sat amount)
{
size_t i = tx->wtx->num_outputs;
u64 satoshis = amount.satoshis; /* Raw: wally API */
struct wally_tx_output *output;
struct wally_psbt_output *psbt_out;
int ret;
u64 satoshis = amount.satoshis; /* Raw: low-level helper */
const struct chainparams *chainparams = tx->chainparams;
assert(i < tx->wtx->outputs_allocation_len);

assert(tx->wtx != NULL);
assert(chainparams);

if (chainparams->is_elements) {
u8 value[9];
Expand All @@ -63,15 +56,36 @@ int bitcoin_tx_add_output(struct bitcoin_tx *tx, const u8 *script,
ret = wally_tx_elements_output_init_alloc(
script, tal_bytelen(script), chainparams->fee_asset_tag, 33,
value, sizeof(value), NULL, 0, NULL, 0, NULL, 0, &output);
assert(ret == WALLY_OK);
if (ret != WALLY_OK)
return NULL;

/* Cheat a bit by also setting the numeric satoshi value,
* otherwise we end up converting a number of times */
output->satoshi = satoshis;
} else {
ret = wally_tx_output_init_alloc(satoshis, script,
tal_bytelen(script), &output);
assert(ret == WALLY_OK);
if (ret != WALLY_OK)
return NULL;
}
return output;
}

int bitcoin_tx_add_output(struct bitcoin_tx *tx, const u8 *script,
u8 *wscript, struct amount_sat amount)
{
size_t i = tx->wtx->num_outputs;
struct wally_tx_output *output;
struct wally_psbt_output *psbt_out;
int ret;
const struct chainparams *chainparams = tx->chainparams;
assert(i < tx->wtx->outputs_allocation_len);

assert(tx->wtx != NULL);
assert(chainparams);

output = wally_tx_output(script, amount);
assert(output);
ret = wally_tx_add_output(tx->wtx, output);
assert(ret == WALLY_OK);

Expand Down
7 changes: 7 additions & 0 deletions bitcoin/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ struct bitcoin_tx *bitcoin_tx_with_psbt(const tal_t *ctx, struct wally_psbt *psb
/* Internal de-linearization functions. */
struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx,
const u8 **cursor, size_t *max);

/* Helper to create a wally_tx_output: make sure to wally_tx_output_free!
* Returns NULL if amount is extreme (wally doesn't like).
*/
struct wally_tx_output *wally_tx_output(const u8 *script,
struct amount_sat amount);

/* Add one output to tx. */
int bitcoin_tx_add_output(struct bitcoin_tx *tx, const u8 *script,
u8 *wscript,
Expand Down

0 comments on commit 03d034d

Please sign in to comment.