Skip to content

Commit

Permalink
psbt: method for extracting witness stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
niftynei authored and rustyrussell committed Oct 20, 2020
1 parent 0df818c commit 254ea37
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
54 changes: 54 additions & 0 deletions common/psbt_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,57 @@ void psbt_input_set_final_witness_stack(struct wally_psbt_input *in,
elements[i]->witness,
tal_bytelen(elements[i]->witness));
}

const struct witness_stack **
psbt_to_witness_stacks(const tal_t *ctx,
const struct wally_psbt *psbt,
enum tx_role side_to_stack)
{
size_t stack_index;
u16 serial_id;
const struct witness_stack **stacks
= tal_arr(ctx, const struct witness_stack *, psbt->num_inputs);

stack_index = 0;
for (size_t i = 0; i < psbt->num_inputs; i++) {
if (!psbt_get_serial_id(&psbt->inputs[i].unknowns,
&serial_id))
/* FIXME: throw an error ? */
return NULL;

/* BOLT-78de9a79b491ae9fb84b1fdb4546bacf642dce87 #2:
* - if is the `initiator`:
* - MUST send even `serial_id`s
*/
if (serial_id % 2 == side_to_stack) {
struct wally_tx_witness_stack *wtx_s =
psbt->inputs[i].final_witness;
struct witness_stack *stack =
tal(stacks, struct witness_stack);
/* Convert the wally_tx_witness_stack to
* a witness_stack entry */
stack->witness_element =
tal_arr(stack, struct witness_element *,
wtx_s->num_items);
for (size_t j = 0; j < tal_count(stack->witness_element); j++) {
stack->witness_element[j] = tal(stack,
struct witness_element);
stack->witness_element[j]->witness =
tal_dup_arr(stack, u8,
wtx_s->items[j].witness,
wtx_s->items[j].witness_len,
0);

}

stacks[stack_index++] = stack;
}

}

if (stack_index == 0)
return tal_free(stacks);

tal_resize(&stacks, stack_index);
return stacks;
}
13 changes: 13 additions & 0 deletions common/psbt_open.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <common/tx_roles.h>
#include <stdbool.h>
#include <wally_psbt.h>
#include <wally_transaction.h>
Expand Down Expand Up @@ -148,4 +149,16 @@ bool psbt_has_required_fields(struct wally_psbt *psbt);
*/
void psbt_input_set_final_witness_stack(struct wally_psbt_input *in,
const struct witness_element **elements);

/* psbt_to_witness_stacks - Take all sigs on a PSBT and copy to a
* witness_stack
*
* @ctx - allocation context
* @psbt - PSBT to copy sigs from
* @opener - which side initiated this tx
*/
const struct witness_stack **
psbt_to_witness_stacks(const tal_t *ctx,
const struct wally_psbt *psbt,
enum tx_role side_to_stack);
#endif /* LIGHTNING_COMMON_PSBT_OPEN_H */

0 comments on commit 254ea37

Please sign in to comment.