From 3c8049f32c3f57360598a0ff2d55b28209dceb05 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 22 Sep 2020 19:22:37 +0930 Subject: [PATCH] bitcoin/psbt: psbt_input_add_unknown/psbt_output_add_unknown needs a tal ctx. Since it allocates something, it needs a context (used in the next patch!). Signed-off-by: Rusty Russell --- bitcoin/psbt.c | 8 ++++---- bitcoin/psbt.h | 8 ++++++-- common/psbt_open.c | 10 ++++++---- common/psbt_open.h | 10 +++++++--- common/test/exp-run-psbt_diff.c | 8 ++++---- lightningd/dual_open_control.c | 4 ++-- openingd/dualopend.c | 4 ++-- 7 files changed, 31 insertions(+), 21 deletions(-) diff --git a/bitcoin/psbt.c b/bitcoin/psbt.c index 7a45dedf5..40ad8f63d 100644 --- a/bitcoin/psbt.c +++ b/bitcoin/psbt.c @@ -464,7 +464,8 @@ u8 *psbt_make_key(const tal_t *ctx, u8 key_subtype, const u8 *key_data) return key; } -void psbt_input_add_unknown(struct wally_psbt_input *in, +void psbt_input_add_unknown(const tal_t *ctx, + struct wally_psbt_input *in, const u8 *key, const void *value, size_t value_len) @@ -506,7 +507,8 @@ void *psbt_get_lightning(const struct wally_map *map, } -void psbt_output_add_unknown(struct wally_psbt_output *out, +void psbt_output_add_unknown(const tal_t *ctx, + struct wally_psbt_output *out, const u8 *key, const void *value, size_t value_len) @@ -626,8 +628,6 @@ char *psbt_to_b64(const tal_t *ctx, const struct wally_psbt *psbt) wally_free_string(serialized_psbt); return ret_val; } - -/* Do not remove this line, it is magic */ REGISTER_TYPE_TO_STRING(wally_psbt, psbt_to_b64); const u8 *psbt_get_bytes(const tal_t *ctx, const struct wally_psbt *psbt, diff --git a/bitcoin/psbt.h b/bitcoin/psbt.h index c0d605208..da4819065 100644 --- a/bitcoin/psbt.h +++ b/bitcoin/psbt.h @@ -157,12 +157,14 @@ void psbt_elements_input_init_witness(struct wally_psbt *psbt, size_t in, bool psbt_input_set_redeemscript(struct wally_psbt *psbt, size_t in, const u8 *redeemscript); /* psbt_input_add_unknown - Add the given Key-Value to the psbt's input keymap + * @ctx - tal context for allocations * @in - psbt input to add key-value to * @key - key for key-value pair * @value - value to add * @value_len - length of {@value} */ -void psbt_input_add_unknown(struct wally_psbt_input *in, +void psbt_input_add_unknown(const tal_t *ctx, + struct wally_psbt_input *in, const u8 *key, const void *value, size_t value_len); @@ -190,12 +192,14 @@ void *psbt_get_lightning(const struct wally_map *map, /* psbt_output_add_unknown - Add the given Key-Value to the psbt's output keymap * + * @ctx - tal context for allocations * @out - psbt output to add key-value to * @key - key for key-value pair * @value - value to add * @value_len - length of {@value} */ -void psbt_output_add_unknown(struct wally_psbt_output *out, +void psbt_output_add_unknown(const tal_t *ctx, + struct wally_psbt_output *out, const u8 *key, const void *value, size_t value_len); diff --git a/common/psbt_open.c b/common/psbt_open.c index c84bc7de6..b505a34df 100644 --- a/common/psbt_open.c +++ b/common/psbt_open.c @@ -392,22 +392,24 @@ u8 *psbt_changeset_get_next(const tal_t *ctx, struct channel_id *cid, return NULL; } -void psbt_input_add_serial_id(struct wally_psbt_input *input, +void psbt_input_add_serial_id(const tal_t *ctx, + struct wally_psbt_input *input, u16 serial_id) { u8 *key = psbt_make_key(tmpctx, PSBT_TYPE_SERIAL_ID, NULL); beint16_t bev = cpu_to_be16(serial_id); - psbt_input_add_unknown(input, key, &bev, sizeof(bev)); + psbt_input_add_unknown(ctx, input, key, &bev, sizeof(bev)); } -void psbt_output_add_serial_id(struct wally_psbt_output *output, +void psbt_output_add_serial_id(const tal_t *ctx, + struct wally_psbt_output *output, u16 serial_id) { u8 *key = psbt_make_key(tmpctx, PSBT_TYPE_SERIAL_ID, NULL); beint16_t bev = cpu_to_be16(serial_id); - psbt_output_add_unknown(output, key, &bev, sizeof(bev)); + psbt_output_add_unknown(ctx, output, key, &bev, sizeof(bev)); } int psbt_find_serial_input(struct wally_psbt *psbt, u16 serial_id) diff --git a/common/psbt_open.h b/common/psbt_open.h index 7c1f96320..b4be8d093 100644 --- a/common/psbt_open.h +++ b/common/psbt_open.h @@ -85,17 +85,21 @@ u8 *psbt_changeset_get_next(const tal_t *ctx, struct channel_id *cid, /* psbt_input_add_serial_id - Adds a serial id to given input * + * @ctx - tal context for allocations * @input - to add serial_id to * @serial_id - to add */ -void psbt_input_add_serial_id(struct wally_psbt_input *input, - u16 serial_id); +void psbt_input_add_serial_id(const tal_t *ctx, + struct wally_psbt_input *input, + u16 serial_id); /* psbt_output_add_serial_id - Adds a serial id to given output * + * @ctx - tal context for allocations * @output - to add serial_id to * @serial_id - to add */ -void psbt_output_add_serial_id(struct wally_psbt_output *output, +void psbt_output_add_serial_id(const tal_t *ctx, + struct wally_psbt_output *output, u16 serial_id); /* psbt_sort_by_serial_id - Sorts the inputs + outputs by serial_id diff --git a/common/test/exp-run-psbt_diff.c b/common/test/exp-run-psbt_diff.c index 9d077c09a..ae2be627b 100644 --- a/common/test/exp-run-psbt_diff.c +++ b/common/test/exp-run-psbt_diff.c @@ -114,7 +114,7 @@ static void add_in_out_with_serial(struct wally_psbt *psbt, NULL, NULL, NULL); if (!in) abort(); - psbt_input_add_serial_id(in, serial_id); + psbt_input_add_serial_id(psbt, in, serial_id); script = tal_arr(tmpctx, u8, 20); memset(script, default_value, 20); @@ -122,7 +122,7 @@ static void add_in_out_with_serial(struct wally_psbt *psbt, out = psbt_append_output(psbt, script, sat); if (!out) abort(); - psbt_output_add_serial_id(out, serial_id); + psbt_output_add_serial_id(psbt, out, serial_id); } int main(int argc, const char *argv[]) @@ -182,8 +182,8 @@ int main(int argc, const char *argv[]) /* Add some extra unknown info to a PSBT */ u8 *key = psbt_make_key(tmpctx, 0x05, NULL); char *val = tal_fmt(tmpctx, "hello"); - psbt_input_add_unknown(&end->inputs[1], key, val, tal_bytelen(val)); - psbt_input_add_unknown(&start->inputs[1], key, val, tal_bytelen(val)); + psbt_input_add_unknown(end, &end->inputs[1], key, val, tal_bytelen(val)); + psbt_input_add_unknown(start, &start->inputs[1], key, val, tal_bytelen(val)); /* Swap locations */ struct wally_map_item tmp; diff --git a/lightningd/dual_open_control.c b/lightningd/dual_open_control.c index b9e3690e4..5dd1e5bda 100644 --- a/lightningd/dual_open_control.c +++ b/lightningd/dual_open_control.c @@ -431,7 +431,7 @@ static void psbt_add_serials(struct wally_psbt *psbt, enum side opener) psbt_find_serial_input(psbt, serial_id) != -1) { /* keep going; */ } - psbt_input_add_serial_id(&psbt->inputs[i], serial_id); + psbt_input_add_serial_id(psbt, &psbt->inputs[i], serial_id); } for (size_t i = 0; i < psbt->num_outputs; i++) { /* Skip ones that already have a serial id */ @@ -442,7 +442,7 @@ static void psbt_add_serials(struct wally_psbt *psbt, enum side opener) psbt_find_serial_output(psbt, serial_id) != -1) { /* keep going; */ } - psbt_output_add_serial_id(&psbt->outputs[i], serial_id); + psbt_output_add_serial_id(psbt, &psbt->outputs[i], serial_id); } } diff --git a/openingd/dualopend.c b/openingd/dualopend.c index 9889ef7e9..fac2456d9 100644 --- a/openingd/dualopend.c +++ b/openingd/dualopend.c @@ -686,7 +686,7 @@ static bool run_tx_interactive(struct state *state, struct wally_psbt **orig_psb psbt_elements_input_set_asset(psbt, outnum, &asset); } - psbt_input_add_serial_id(in, serial_id); + psbt_input_add_serial_id(psbt, in, serial_id); /* FIXME: what's in the tlv? */ break; @@ -747,7 +747,7 @@ static bool run_tx_interactive(struct state *state, struct wally_psbt **orig_psb "Duplicate serial_id rcvd. %u", serial_id); amt = amount_sat(value); out = psbt_append_output(psbt, scriptpubkey, amt); - psbt_output_add_serial_id(out, serial_id); + psbt_output_add_serial_id(psbt, out, serial_id); break; } case WIRE_TX_REMOVE_OUTPUT: {