txprepare: Use param_outpoint_arr helper to validate input

This commit is contained in:
Christian Decker
2020-12-04 11:27:37 +01:00
committed by Rusty Russell
parent 32000b6660
commit 55d9492d9e
3 changed files with 24 additions and 6 deletions

View File

@@ -193,6 +193,14 @@ void json_add_txid(struct json_stream *result, const char *fieldname,
json_add_string(result, fieldname, hex); 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, void json_add_short_channel_id(struct json_stream *response,
const char *fieldname, const char *fieldname,
const struct short_channel_id *scid) const struct short_channel_id *scid)

View File

@@ -102,6 +102,10 @@ void json_add_channel_id(struct json_stream *response,
void json_add_txid(struct json_stream *result, const char *fieldname, void json_add_txid(struct json_stream *result, const char *fieldname,
const struct bitcoin_txid *txid); 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"' */ /* '"fieldname" : "1234:5:6"' */
void json_add_short_channel_id(struct json_stream *response, void json_add_short_channel_id(struct json_stream *response,
const char *fieldname, const char *fieldname,

View File

@@ -324,7 +324,7 @@ static struct command_result *txprepare_continue(struct command *cmd,
struct txprepare *txp, struct txprepare *txp,
const char *feerate, const char *feerate,
unsigned int *minconf, unsigned int *minconf,
const char *utxos, struct bitcoin_outpoint *utxos,
bool is_withdraw) bool is_withdraw)
{ {
struct out_req *req; 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", req = jsonrpc_request_start(cmd->plugin, cmd, "utxopsbt",
psbt_created, forward_error, psbt_created, forward_error,
txp); 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 { } else {
req = jsonrpc_request_start(cmd->plugin, cmd, "fundpsbt", req = jsonrpc_request_start(cmd->plugin, cmd, "fundpsbt",
psbt_created, forward_error, psbt_created, forward_error,
@@ -365,14 +369,15 @@ static struct command_result *json_txprepare(struct command *cmd,
const jsmntok_t *params) const jsmntok_t *params)
{ {
struct txprepare *txp = tal(cmd, struct txprepare); struct txprepare *txp = tal(cmd, struct txprepare);
const char *feerate, *utxos; const char *feerate;
struct bitcoin_outpoint *utxos;
unsigned int *minconf; unsigned int *minconf;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("outputs", param_outputs, txp), p_req("outputs", param_outputs, txp),
p_opt("feerate", param_string, &feerate), p_opt("feerate", param_string, &feerate),
p_opt_def("minconf", param_number, &minconf, 1), p_opt_def("minconf", param_number, &minconf, 1),
p_opt("utxos", param_string, &utxos), p_opt("utxos", param_outpoint_arr, &utxos),
NULL)) NULL))
return command_param_failed(); 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 txprepare *txp = tal(cmd, struct txprepare);
struct amount_sat *amount; struct amount_sat *amount;
const u8 *scriptpubkey; const u8 *scriptpubkey;
const char *feerate, *utxos; const char *feerate;
struct bitcoin_outpoint *utxos;
unsigned int *minconf; unsigned int *minconf;
if (!param(cmd, buffer, params, 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_req("satoshi", param_sat_or_all, &amount),
p_opt("feerate", param_string, &feerate), p_opt("feerate", param_string, &feerate),
p_opt_def("minconf", param_number, &minconf, 1), p_opt_def("minconf", param_number, &minconf, 1),
p_opt("utxos", param_string, &utxos), p_opt("utxos", param_outpoint_arr, &utxos),
NULL)) NULL))
return command_param_failed(); return command_param_failed();