Browse Source

psbt: use new wally functions to add PSBT inputs/outputs

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
bump-pyln-proto
Jon Griffiths 4 years ago
committed by Rusty Russell
parent
commit
1fe53880a7
  1. 91
      bitcoin/psbt.c

91
bitcoin/psbt.c

@ -15,13 +15,6 @@
#include <wally_transaction.h> #include <wally_transaction.h>
#include <wire/wire.h> #include <wire/wire.h>
#define MAKE_ROOM(arr, pos, num) \
memmove((arr) + (pos) + 1, (arr) + (pos), \
sizeof(*(arr)) * ((num) - ((pos) + 1)))
#define REMOVE_ELEM(arr, pos, num) \
memmove((arr) + (pos), (arr) + (pos) + 1, \
sizeof(*(arr)) * ((num) - ((pos) + 1)))
void psbt_destroy(struct wally_psbt *psbt) void psbt_destroy(struct wally_psbt *psbt)
{ {
@ -82,99 +75,35 @@ struct wally_psbt_input *psbt_add_input(struct wally_psbt *psbt,
struct wally_tx_input *input, struct wally_tx_input *input,
size_t insert_at) size_t insert_at)
{ {
struct wally_tx *tx; const u32 flags = WALLY_PSBT_FLAG_NON_FINAL; /* Skip script/witness */
struct wally_tx_input tmp_in; int wally_err;
u8 *script;
size_t scriptlen = 0;
struct wally_tx_witness_stack *witness = NULL;
tx = psbt->tx;
assert(insert_at <= tx->num_inputs);
/* Remove any script sig or witness info before adding it ! */
if (input->script_len > 0) {
scriptlen = input->script_len;
input->script_len = 0;
script = (u8 *)input->script;
input->script = NULL;
}
if (input->witness) {
witness = input->witness;
input->witness = NULL;
}
wally_tx_add_input(tx, input);
/* Put the script + witness info back */
if (scriptlen > 0) {
input->script_len = scriptlen;
input->script = script;
}
if (witness)
input->witness = witness;
tmp_in = tx->inputs[tx->num_inputs - 1];
MAKE_ROOM(tx->inputs, insert_at, tx->num_inputs);
tx->inputs[insert_at] = tmp_in;
if (psbt->inputs_allocation_len < tx->num_inputs) {
struct wally_psbt_input *p = tal_arr(psbt, struct wally_psbt_input, tx->num_inputs);
memcpy(p, psbt->inputs, sizeof(*psbt->inputs) * psbt->inputs_allocation_len);
tal_free(psbt->inputs);
psbt->inputs = p;
psbt->inputs_allocation_len = tx->num_inputs;
}
psbt->num_inputs += 1; wally_err = wally_psbt_add_input_at(psbt, insert_at, flags, input);
MAKE_ROOM(psbt->inputs, insert_at, psbt->num_inputs); assert(wally_err == WALLY_OK);
memset(&psbt->inputs[insert_at], 0, sizeof(psbt->inputs[insert_at]));
return &psbt->inputs[insert_at]; return &psbt->inputs[insert_at];
} }
void psbt_rm_input(struct wally_psbt *psbt, void psbt_rm_input(struct wally_psbt *psbt,
size_t remove_at) size_t remove_at)
{ {
assert(remove_at < psbt->tx->num_inputs); int wally_err = wally_psbt_remove_input(psbt, remove_at);
wally_tx_remove_input(psbt->tx, remove_at); assert(wally_err == WALLY_OK);
REMOVE_ELEM(psbt->inputs, remove_at, psbt->num_inputs);
psbt->num_inputs -= 1;
} }
struct wally_psbt_output *psbt_add_output(struct wally_psbt *psbt, struct wally_psbt_output *psbt_add_output(struct wally_psbt *psbt,
struct wally_tx_output *output, struct wally_tx_output *output,
size_t insert_at) size_t insert_at)
{ {
struct wally_tx *tx; int wally_err = wally_psbt_add_output_at(psbt, insert_at, 0, output);
struct wally_tx_output tmp_out; assert(wally_err == WALLY_OK);
tx = psbt->tx;
assert(insert_at <= tx->num_outputs);
wally_tx_add_output(tx, output);
tmp_out = tx->outputs[tx->num_outputs - 1];
MAKE_ROOM(tx->outputs, insert_at, tx->num_outputs);
tx->outputs[insert_at] = tmp_out;
if (psbt->outputs_allocation_len < tx->num_outputs) {
struct wally_psbt_output *p = tal_arr(psbt, struct wally_psbt_output, tx->num_outputs);
memcpy(p, psbt->outputs, sizeof(*psbt->outputs) * psbt->outputs_allocation_len);
tal_free(psbt->outputs);
psbt->outputs = p;
psbt->outputs_allocation_len = tx->num_outputs;
}
psbt->num_outputs += 1;
MAKE_ROOM(psbt->outputs, insert_at, psbt->num_outputs);
memset(&psbt->outputs[insert_at], 0, sizeof(psbt->outputs[insert_at]));
return &psbt->outputs[insert_at]; return &psbt->outputs[insert_at];
} }
void psbt_rm_output(struct wally_psbt *psbt, void psbt_rm_output(struct wally_psbt *psbt,
size_t remove_at) size_t remove_at)
{ {
assert(remove_at < psbt->tx->num_outputs); int wally_err = wally_psbt_remove_output(psbt, remove_at);
wally_tx_remove_output(psbt->tx, remove_at); assert(wally_err == WALLY_OK);
REMOVE_ELEM(psbt->outputs, remove_at, psbt->num_outputs);
psbt->num_outputs -= 1;
} }
void psbt_input_add_pubkey(struct wally_psbt *psbt, size_t in, void psbt_input_add_pubkey(struct wally_psbt *psbt, size_t in,

Loading…
Cancel
Save