From 55d9492d9efc45e91172bc3638a7bb87101c31a9 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 4 Dec 2020 11:27:37 +0100 Subject: [PATCH] txprepare: Use param_outpoint_arr helper to validate input --- common/json_helpers.c | 8 ++++++++ common/json_helpers.h | 4 ++++ plugins/txprepare.c | 18 ++++++++++++------ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/common/json_helpers.c b/common/json_helpers.c index 63bdc1543..ca8b09337 100644 --- a/common/json_helpers.c +++ b/common/json_helpers.c @@ -193,6 +193,14 @@ void json_add_txid(struct json_stream *result, const char *fieldname, json_add_string(result, fieldname, hex); } +void json_add_outpoint(struct json_stream *result, const char *fieldname, + const struct bitcoin_outpoint *out) +{ + char hex[hex_str_size(sizeof(out->txid))]; + bitcoin_txid_to_hex(&out->txid, hex, sizeof(hex)); + json_add_member(result, fieldname, true, "%s:%d", hex, out->n); +} + void json_add_short_channel_id(struct json_stream *response, const char *fieldname, const struct short_channel_id *scid) diff --git a/common/json_helpers.h b/common/json_helpers.h index 13a2044ab..0c8322947 100644 --- a/common/json_helpers.h +++ b/common/json_helpers.h @@ -102,6 +102,10 @@ void json_add_channel_id(struct json_stream *response, void json_add_txid(struct json_stream *result, const char *fieldname, const struct bitcoin_txid *txid); +/* '"fieldname" : "txid:n" */ +void json_add_outpoint(struct json_stream *result, const char *fieldname, + const struct bitcoin_outpoint *out); + /* '"fieldname" : "1234:5:6"' */ void json_add_short_channel_id(struct json_stream *response, const char *fieldname, diff --git a/plugins/txprepare.c b/plugins/txprepare.c index a37610363..eba0bbd08 100644 --- a/plugins/txprepare.c +++ b/plugins/txprepare.c @@ -324,7 +324,7 @@ static struct command_result *txprepare_continue(struct command *cmd, struct txprepare *txp, const char *feerate, unsigned int *minconf, - const char *utxos, + struct bitcoin_outpoint *utxos, bool is_withdraw) { struct out_req *req; @@ -341,7 +341,11 @@ static struct command_result *txprepare_continue(struct command *cmd, req = jsonrpc_request_start(cmd->plugin, cmd, "utxopsbt", psbt_created, forward_error, txp); - json_add_jsonstr(req->js, "utxos", utxos); + json_array_start(req->js, "utxos"); + for (size_t i = 0; i < tal_count(utxos); i++) { + json_add_outpoint(req->js, NULL, &utxos[i]); + } + json_array_end(req->js); } else { req = jsonrpc_request_start(cmd->plugin, cmd, "fundpsbt", psbt_created, forward_error, @@ -365,14 +369,15 @@ static struct command_result *json_txprepare(struct command *cmd, const jsmntok_t *params) { struct txprepare *txp = tal(cmd, struct txprepare); - const char *feerate, *utxos; + const char *feerate; + struct bitcoin_outpoint *utxos; unsigned int *minconf; if (!param(cmd, buffer, params, p_req("outputs", param_outputs, txp), p_opt("feerate", param_string, &feerate), p_opt_def("minconf", param_number, &minconf, 1), - p_opt("utxos", param_string, &utxos), + p_opt("utxos", param_outpoint_arr, &utxos), NULL)) return command_param_failed(); @@ -472,7 +477,8 @@ static struct command_result *json_withdraw(struct command *cmd, struct txprepare *txp = tal(cmd, struct txprepare); struct amount_sat *amount; const u8 *scriptpubkey; - const char *feerate, *utxos; + const char *feerate; + struct bitcoin_outpoint *utxos; unsigned int *minconf; if (!param(cmd, buffer, params, @@ -481,7 +487,7 @@ static struct command_result *json_withdraw(struct command *cmd, p_req("satoshi", param_sat_or_all, &amount), p_opt("feerate", param_string, &feerate), p_opt_def("minconf", param_number, &minconf, 1), - p_opt("utxos", param_string, &utxos), + p_opt("utxos", param_outpoint_arr, &utxos), NULL)) return command_param_failed();