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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2020-09-22 19:22:37 +09:30
parent 77b62d9e42
commit 3c8049f32c
7 changed files with 31 additions and 21 deletions

View File

@ -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,

View File

@ -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);

View File

@ -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)

View File

@ -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

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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: {