From 2d5c61dfc16589706a08a2279bc8c6d4b03cd1a3 Mon Sep 17 00:00:00 2001 From: niftynei Date: Thu, 21 May 2020 11:27:00 +0930 Subject: [PATCH] psbt: methods to mutate psbt in place add the missing psbt helpers for adding and removing an input and output --- bitcoin/Makefile | 2 ++ bitcoin/psbt.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ bitcoin/psbt.h | 25 ++++++++++++++ plugins/Makefile | 1 + 4 files changed, 118 insertions(+) create mode 100644 bitcoin/psbt.c create mode 100644 bitcoin/psbt.h diff --git a/bitcoin/Makefile b/bitcoin/Makefile index daf461af6..9d2635947 100644 --- a/bitcoin/Makefile +++ b/bitcoin/Makefile @@ -7,6 +7,7 @@ BITCOIN_SRC := \ bitcoin/locktime.c \ bitcoin/preimage.c \ bitcoin/privkey.c \ + bitcoin/psbt.c \ bitcoin/pubkey.c \ bitcoin/pullpush.c \ bitcoin/script.c \ @@ -26,6 +27,7 @@ BITCOIN_HEADERS := bitcoin/address.h \ bitcoin/locktime.h \ bitcoin/preimage.h \ bitcoin/privkey.h \ + bitcoin/psbt.h \ bitcoin/pubkey.h \ bitcoin/pullpush.h \ bitcoin/script.h \ diff --git a/bitcoin/psbt.c b/bitcoin/psbt.c new file mode 100644 index 000000000..e2b45a71b --- /dev/null +++ b/bitcoin/psbt.c @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +#include + +#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))) + +struct wally_psbt_input *psbt_add_input(struct wally_psbt *psbt, + struct wally_tx_input *input, + size_t insert_at) +{ + struct wally_tx *tx; + struct wally_tx_input tmp_in; + + tx = psbt->tx; + assert(insert_at <= tx->num_inputs); + wally_tx_add_input(tx, input); + 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; + MAKE_ROOM(psbt->inputs, insert_at, psbt->num_inputs); + memset(&psbt->inputs[insert_at], 0, sizeof(psbt->inputs[insert_at])); + return &psbt->inputs[insert_at]; +} + +void psbt_rm_input(struct wally_psbt *psbt, + size_t remove_at) +{ + assert(remove_at < psbt->tx->num_inputs); + wally_tx_remove_input(psbt->tx, remove_at); + 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_tx_output *output, + size_t insert_at) +{ + struct wally_tx *tx; + struct wally_tx_output tmp_out; + + 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]; +} + +void psbt_rm_output(struct wally_psbt *psbt, + size_t remove_at) +{ + assert(remove_at < psbt->tx->num_outputs); + wally_tx_remove_output(psbt->tx, remove_at); + REMOVE_ELEM(psbt->outputs, remove_at, psbt->num_outputs); + psbt->num_outputs -= 1; +} diff --git a/bitcoin/psbt.h b/bitcoin/psbt.h new file mode 100644 index 000000000..96327c7a3 --- /dev/null +++ b/bitcoin/psbt.h @@ -0,0 +1,25 @@ +#ifndef LIGHTNING_BITCOIN_PSBT_H +#define LIGHTNING_BITCOIN_PSBT_H +#include "config.h" +#include + +struct wally_tx_input; +struct wally_tx_output; +struct wally_psbt; +struct wally_psbt_input; + +struct wally_psbt_input *psbt_add_input(struct wally_psbt *psbt, + struct wally_tx_input *input, + size_t insert_at); + +void psbt_rm_input(struct wally_psbt *psbt, + size_t remove_at); + +struct wally_psbt_output *psbt_add_output(struct wally_psbt *psbt, + struct wally_tx_output *output, + size_t insert_at); + +void psbt_rm_output(struct wally_psbt *psbt, + size_t remove_at); + +#endif /* LIGHTNING_BITCOIN_PSBT_H */ diff --git a/plugins/Makefile b/plugins/Makefile index 8ddf63024..ca51322e2 100644 --- a/plugins/Makefile +++ b/plugins/Makefile @@ -20,6 +20,7 @@ PLUGIN_LIB_OBJS := $(PLUGIN_LIB_SRC:.c=.o) PLUGIN_COMMON_OBJS := \ bitcoin/base58.o \ bitcoin/privkey.o \ + bitcoin/psbt.o \ bitcoin/pubkey.o \ bitcoin/pullpush.o \ bitcoin/script.o \