Skip to content

Commit

Permalink
mfc-psbt: mark all of our inputs as "ours", then only sign those
Browse files Browse the repository at this point in the history
we only want to sign the inputs that we've reserved via utxopsbt or
fundpsbt. we mark them with a flag (reusing the now defunct max-len
flag is fine), then look for inputs with that flag to pass to signonly
  • Loading branch information
niftynei committed Nov 23, 2020
1 parent 6077eca commit 29c3532
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
17 changes: 17 additions & 0 deletions common/psbt_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,20 @@ void psbt_add_serials(struct wally_psbt *psbt, enum tx_role role)
psbt_output_set_serial_id(psbt, &psbt->outputs[i], serial_id);
}
}

void psbt_input_mark_ours(const tal_t *ctx,
struct wally_psbt_input *input)
{
u8 *key = psbt_make_key(tmpctx, PSBT_TYPE_INPUT_MARKER, NULL);
beint16_t bev = cpu_to_be16(1);

psbt_input_set_unknown(ctx, input, key, &bev, sizeof(bev));
}

bool psbt_input_is_ours(const struct wally_psbt_input *input)
{
size_t unused;
void *result = psbt_get_lightning(&input->unknowns,
PSBT_TYPE_INPUT_MARKER, &unused);
return !(!result);
}
13 changes: 12 additions & 1 deletion common/psbt_open.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct psbt_changeset {
};

#define PSBT_TYPE_SERIAL_ID 0x01
#define PSBT_TYPE_MAX_WITNESS_LEN 0x02
#define PSBT_TYPE_INPUT_MARKER 0x02

/* psbt_get_serial_id - Returns the serial_id from an unknowns map
*
Expand Down Expand Up @@ -163,4 +163,15 @@ bool psbt_side_finalized(const struct wally_psbt *psbt,
* @role - the role we should use to select serial parity
*/
void psbt_add_serials(struct wally_psbt *psbt, enum tx_role role);

/* psbt_input_mark_ours - Sets the PSBT_TYPE_INPUT_MARKER on this input
*/
void psbt_input_mark_ours(const tal_t *ctx,
struct wally_psbt_input *input);

/* psbt_input_is_ours - Returns true if this psbt input has
* the PSBT_TYPE_INPUT_MARKER set on it.
*/
bool psbt_input_is_ours(const struct wally_psbt_input *input);

#endif /* LIGHTNING_COMMON_PSBT_OPEN_H */
15 changes: 14 additions & 1 deletion plugins/spender/multifundchannel.c
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,11 @@ mfc_psbt_acquired(struct multifundchannel_command *mfc)
* for the life of the tx */
psbt_add_serials(mfc->psbt, TX_INITIATOR);

/* We also mark all of our inputs as *ours*, so we
* can easily identify them for `signpsbt`, later */
for (size_t i = 0; i < mfc->psbt->num_inputs; i++)
psbt_input_mark_ours(mfc->psbt, &mfc->psbt->inputs[i]);

return perform_channel_start(mfc);
}

Expand Down Expand Up @@ -1522,13 +1527,21 @@ perform_signpsbt(struct multifundchannel_command *mfc)
plugin_log(mfc->cmd->plugin, LOG_DBG,
"mfc %"PRIu64": signpsbt.", mfc->id);

/* FIXME: indicate our inputs with signonly */
req = jsonrpc_request_start(mfc->cmd->plugin, mfc->cmd,
"signpsbt",
&after_signpsbt,
&mfc_forward_error,
mfc);
json_add_psbt(req->js, "psbt", mfc->psbt);

/* Use input markers to identify which inputs
* are ours, only sign those */
json_array_start(req->js, "signonly");
for (size_t i = 0; i < mfc->psbt->num_inputs; i++) {
if (psbt_input_is_ours(&mfc->psbt->inputs[i]))
json_add_num(req->js, NULL, i);
}
json_array_end(req->js);
return send_outreq(mfc->cmd->plugin, req);
}

Expand Down

0 comments on commit 29c3532

Please sign in to comment.