Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore any missing psbt metadata that resource constrained signers strip #6767

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions bitcoin/psbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ struct wally_psbt *new_psbt(const tal_t *ctx, const struct wally_tx *wtx)
return psbt;
}

struct wally_psbt *combine_psbt(const tal_t *ctx,
const struct wally_psbt *psbt0,
const struct wally_psbt *psbt1)
{
struct wally_psbt *combined_psbt;
tal_wally_start();
if (wally_psbt_clone_alloc(psbt0, 0, &combined_psbt) != WALLY_OK)
abort();
if (wally_psbt_combine(combined_psbt, psbt1) != WALLY_OK) {
tal_wally_end_onto(ctx, combined_psbt, struct wally_psbt);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right thing to do here? Should we free the memory immediately instead?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is right, because from the docs of tal_wally_end_onto I found that
this method iterates over an object allocated by libwally. So I guess it is the safe way to delete an object that wally worked on?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but leaks the combined_psbt. You should return tal_free(combined_psbt); after this (of course, this code shouldn't happen at all, since the HSM said the PSBT was good!).

return tal_free(combined_psbt);
}
tal_wally_end_onto(ctx, combined_psbt, struct wally_psbt);
return combined_psbt;
}

bool psbt_is_finalized(const struct wally_psbt *psbt)
{
size_t is_finalized;
Expand Down
11 changes: 11 additions & 0 deletions bitcoin/psbt.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ struct wally_psbt *new_psbt(const tal_t *ctx,
*/
struct wally_psbt *clone_psbt(const tal_t *ctx, struct wally_psbt *psbt);

/**
* combine_psbt - Combine two PSBT into a cloned copy
*
* @ctx - allocation context
* @psbt0 - one psbt
* @psbt1 - other psbt
*/
struct wally_psbt *combine_psbt(const tal_t *ctx,
const struct wally_psbt *psbt0,
const struct wally_psbt *psbt1);

/**
* psbt_is_finalized - Check if tx is ready to be extracted
*
Expand Down
19 changes: 16 additions & 3 deletions wallet/walletrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,15 +799,28 @@ static struct command_result *json_signpsbt(struct command *cmd,
"HSM gave bad sign_withdrawal_reply %s",
tal_hex(tmpctx, msg));

if (!psbt_set_version(signed_psbt, psbt_version)) {
/* Some signers (VLS) prune the input.utxo data as it's used
* because it is too large to store in the signer. We can
* restore this metadata by combining the signed psbt back
* into a clone of the original psbt. */
struct wally_psbt *combined_psbt;
combined_psbt = combine_psbt(cmd, psbt, signed_psbt);
if (!combined_psbt) {
return command_fail(cmd, LIGHTNINGD,
"Unable to combine signed psbt: %s",
type_to_string(tmpctx, struct wally_psbt,
signed_psbt));
}

if (!psbt_set_version(combined_psbt, psbt_version)) {
return command_fail(cmd, LIGHTNINGD,
"Signed PSBT unable to have version set: %s",
type_to_string(tmpctx, struct wally_psbt,
psbt));
combined_psbt));
}

response = json_stream_success(cmd);
json_add_psbt(response, "signed_psbt", signed_psbt);
json_add_psbt(response, "signed_psbt", combined_psbt);
return command_success(cmd, response);
}

Expand Down