common: round out the short_channel_id_dir JSON routines.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2024-10-04 08:50:53 +09:30
parent d8ca9baa3b
commit f46219b505
7 changed files with 46 additions and 14 deletions

View File

@ -642,6 +642,19 @@ struct command_result *param_short_channel_id(struct command *cmd,
"should be a short_channel_id of form NxNxN");
}
struct command_result *param_short_channel_id_dir(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct short_channel_id_dir **scidd)
{
*scidd = tal(cmd, struct short_channel_id_dir);
if (!json_to_short_channel_id_dir(buffer, tok, *scidd))
return command_fail_badparam(cmd, name, buffer, tok,
"should be a short_channel_id_dir of form NxNxN/N");
return NULL;
}
struct command_result *param_secret(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
struct secret **secret)

View File

@ -275,6 +275,12 @@ struct command_result *param_short_channel_id(struct command *cmd,
const jsmntok_t *tok,
struct short_channel_id **scid);
struct command_result *param_short_channel_id_dir(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct short_channel_id_dir **scidd);
/* Ignore the token. Not usually used. */
struct command_result *param_ignore(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,

View File

@ -109,6 +109,17 @@ bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num)
return true;
}
bool json_to_zero_or_one(const char *buffer, const jsmntok_t *tok, int *num)
{
u32 v32;
if (!json_to_u32(buffer, tok, &v32))
return false;
if (v32 != 0 && v32 != 1)
return false;
*num = v32;
return true;
}
bool json_to_jsonrpc_errcode(const char *buffer, const jsmntok_t *tok,
enum jsonrpc_errcode *errcode)
{
@ -577,7 +588,6 @@ bool json_to_short_channel_id_dir(const char *buffer, const jsmntok_t *tok,
struct short_channel_id_dir *scidd)
{
jsmntok_t scidtok, numtok;
u32 dir;
if (!split_tok(buffer, tok, '/', &scidtok, &numtok))
return false;
@ -585,10 +595,9 @@ bool json_to_short_channel_id_dir(const char *buffer, const jsmntok_t *tok,
if (!json_to_short_channel_id(buffer, &scidtok, &scidd->scid))
return false;
if (!json_to_u32(buffer, &numtok, &dir) || (dir > 1))
if (!json_to_zero_or_one(buffer, &numtok, &scidd->dir))
return false;
scidd->dir = dir;
return true;
}

View File

@ -31,6 +31,9 @@ u8 *json_tok_bin_from_hex(const tal_t *ctx, const char *buffer, const jsmntok_t
bool json_to_number(const char *buffer, const jsmntok_t *tok,
unsigned int *num);
/* Extract 0/1 from this */
bool json_to_zero_or_one(const char *buffer, const jsmntok_t *tok, int *num);
/* Extract signed 64 bit integer from this (may be a string, or a number literal) */
bool json_to_s64(const char *buffer, const jsmntok_t *tok, s64 *num);
@ -86,6 +89,10 @@ bool json_to_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
bool json_to_short_channel_id(const char *buffer, const jsmntok_t *tok,
struct short_channel_id *scid);
/* Extract a short_channel_id_dir from this */
bool json_to_short_channel_id_dir(const char *buffer, const jsmntok_t *tok,
struct short_channel_id_dir *scidd);
/* Extract a satoshis amount from this */
bool json_to_sat(const char *buffer, const jsmntok_t *tok,
struct amount_sat *sat);

View File

@ -120,6 +120,10 @@ bool json_to_pubkey(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
bool json_to_short_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
struct short_channel_id *scid UNNEEDED)
{ fprintf(stderr, "json_to_short_channel_id called!\n"); abort(); }
/* Generated stub for json_to_short_channel_id_dir */
bool json_to_short_channel_id_dir(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
struct short_channel_id_dir *scidd UNNEEDED)
{ fprintf(stderr, "json_to_short_channel_id_dir called!\n"); abort(); }
/* Generated stub for json_to_txid */
bool json_to_txid(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
struct bitcoin_txid *txid UNNEEDED)

View File

@ -146,6 +146,10 @@ bool json_to_pubkey(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
bool json_to_short_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
struct short_channel_id *scid UNNEEDED)
{ fprintf(stderr, "json_to_short_channel_id called!\n"); abort(); }
/* Generated stub for json_to_short_channel_id_dir */
bool json_to_short_channel_id_dir(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
struct short_channel_id_dir *scidd UNNEEDED)
{ fprintf(stderr, "json_to_short_channel_id_dir called!\n"); abort(); }
/* Generated stub for json_to_txid */
bool json_to_txid(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
struct bitcoin_txid *txid UNNEEDED)

View File

@ -102,17 +102,6 @@ static struct command_result *param_known_layer(struct command *cmd,
return NULL;
}
static bool json_to_zero_or_one(const char *buffer, const jsmntok_t *tok, int *num)
{
u32 v32;
if (!json_to_u32(buffer, tok, &v32))
return false;
if (v32 != 0 && v32 != 1)
return false;
*num = v32;
return true;
}
static struct command_result *param_zero_or_one(struct command *cmd,
const char *name,
const char *buffer,