From 600d0a4a1d8de61bcc9b484678179dffb82b5ac9 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 7 Aug 2020 10:50:51 +0930 Subject: [PATCH] psbt: make psbt_from_b64 more conventional. Signed-off-by: Rusty Russell --- bitcoin/psbt.c | 17 +++++++++++++---- bitcoin/psbt.h | 4 +++- wallet/walletrpc.c | 7 ++----- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/bitcoin/psbt.c b/bitcoin/psbt.c index ba8f74b4e..a57d5514b 100644 --- a/bitcoin/psbt.c +++ b/bitcoin/psbt.c @@ -361,11 +361,20 @@ struct wally_tx *psbt_finalize(struct wally_psbt *psbt, bool finalize_in_place) return NULL; } -bool psbt_from_b64(const char *b64str, struct wally_psbt **psbt) +struct wally_psbt *psbt_from_b64(const tal_t *ctx, + const char *b64, + size_t b64len) { - int wally_err; - wally_err = wally_psbt_from_base64(b64str, psbt); - return wally_err == WALLY_OK; + struct wally_psbt *psbt; + char *str = tal_strndup(tmpctx, b64, b64len); + + if (wally_psbt_from_base64(str, &psbt) != WALLY_OK) + return NULL; + + /* We promised it would be owned by ctx: libwally uses a dummy owner */ + tal_steal(ctx, psbt); + tal_add_destructor(psbt, psbt_destroy); + return psbt; } char *psbt_to_b64(const tal_t *ctx, const struct wally_psbt *psbt) diff --git a/bitcoin/psbt.h b/bitcoin/psbt.h index 335c8b5fb..512bdd9b5 100644 --- a/bitcoin/psbt.h +++ b/bitcoin/psbt.h @@ -74,7 +74,9 @@ bool psbt_input_set_redeemscript(struct wally_psbt *psbt, size_t in, struct amount_sat psbt_input_get_amount(struct wally_psbt *psbt, size_t in); -bool psbt_from_b64(const char *b64str, struct wally_psbt **psbt); +struct wally_psbt *psbt_from_b64(const tal_t *ctx, + const char *b64, + size_t b64len); char *psbt_to_b64(const tal_t *ctx, const struct wally_psbt *psbt); const u8 *psbt_get_bytes(const tal_t *ctx, const struct wally_psbt *psbt, size_t *bytes_written); diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 383d45280..19cd21f93 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -1203,11 +1203,8 @@ struct command_result *param_psbt(struct command *cmd, const jsmntok_t *tok, struct wally_psbt **psbt) { - /* Pull out the token into a string, then pass to - * the PSBT parser; PSBT parser can't handle streaming - * atm as it doesn't accept a len value */ - char *psbt_buff = json_strdup(cmd, buffer, tok); - if (psbt_from_b64(psbt_buff, psbt)) + *psbt = psbt_from_b64(cmd, buffer + tok->start, tok->end - tok->start); + if (*psbt) return NULL; return command_fail(cmd, JSONRPC2_INVALID_PARAMS,