diff --git a/lightningd/json.c b/lightningd/json.c index f1076c2a0..16c0068e9 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -529,25 +529,6 @@ void json_add_hex_talarr(struct json_stream *result, json_add_hex(result, fieldname, data, tal_bytelen(data)); } -void json_add_object(struct json_stream *result, ...) -{ - va_list ap; - const char *field; - - va_start(ap, result); - json_object_start(result, NULL); - while ((field = va_arg(ap, const char *)) != NULL) { - jsmntype_t type = va_arg(ap, jsmntype_t); - const char *value = va_arg(ap, const char *); - if (type == JSMN_STRING) - json_add_string(result, field, value); - else - json_add_literal(result, field, value, strlen(value)); - } - json_object_end(result); - va_end(ap); -} - void json_add_escaped_string(struct json_stream *result, const char *fieldname, const struct json_escaped *esc TAKES) { @@ -555,60 +536,3 @@ void json_add_escaped_string(struct json_stream *result, const char *fieldname, if (taken(esc)) tal_free(esc); } - -static struct json_stream *attach_json_stream(struct command *cmd) -{ - struct json_stream *js = new_json_stream(cmd, cmd); - - /* If they still care about the result, wake them */ - if (cmd->jcon) { - /* FIXME: We only allow one command at a time */ - assert(!cmd->jcon->js); - cmd->jcon->js = js; - io_wake(cmd->jcon); - } - assert(!cmd->have_json_stream); - cmd->have_json_stream = true; - return js; -} - -static struct json_stream *json_start(struct command *cmd) -{ - struct json_stream *js = attach_json_stream(cmd); - - json_stream_append_fmt(js, "{ \"jsonrpc\": \"2.0\", \"id\" : %s, ", - cmd->id); - return js; -} - -struct json_stream *json_stream_success(struct command *cmd) -{ - struct json_stream *r = json_start(cmd); - json_stream_append(r, "\"result\" : "); - return r; -} - -struct json_stream *json_stream_fail_nodata(struct command *cmd, - int code, - const char *errmsg) -{ - struct json_stream *r = json_start(cmd); - - assert(code); - assert(errmsg); - - json_stream_append_fmt(r, " \"error\" : " - "{ \"code\" : %d," - " \"message\" : \"%s\"", code, errmsg); - return r; -} - -struct json_stream *json_stream_fail(struct command *cmd, - int code, - const char *errmsg) -{ - struct json_stream *r = json_stream_fail_nodata(cmd, code, errmsg); - - json_stream_append(r, ", \"data\" : "); - return r; -} diff --git a/lightningd/json.h b/lightningd/json.h index 10e5b5b2f..e960eb7a7 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -15,6 +15,7 @@ # include struct bitcoin_txid; +struct chainparams; struct channel_id; struct command; struct json_escaped; @@ -24,6 +25,7 @@ struct route_hop; struct sha256; struct short_channel_id; struct wallet_payment; +struct wallet_tx; struct wireaddr; struct wireaddr_internal; @@ -159,40 +161,6 @@ bool json_tok_tok(struct command *cmd, const char *name, const jsmntok_t **out); -/** - * json_stream_success - start streaming a successful json result. - * @cmd: the command we're running. - * - * The returned value should go to command_success() when done. - * json_add_* will be placed into the 'result' field of the JSON reply. - */ -struct json_stream *json_stream_success(struct command *cmd); - -/** - * json_stream_fail - start streaming a failed json result. - * @cmd: the command we're running. - * @code: the error code from lightningd/jsonrpc_errors.h - * @errmsg: the error string. - * - * The returned value should go to command_failed() when done; - * json_add_* will be placed into the 'data' field of the 'error' JSON reply. - */ -struct json_stream *json_stream_fail(struct command *cmd, - int code, - const char *errmsg); - -/** - * json_stream_fail_nodata - start streaming a failed json result. - * @cmd: the command we're running. - * @code: the error code from lightningd/jsonrpc_errors.h - * @errmsg: the error string. - * - * This is used by command_fail(), which doesn't add any JSON data. - */ -struct json_stream *json_stream_fail_nodata(struct command *cmd, - int code, - const char *errmsg); - /* '"fieldname" : "value"' or '"value"' if fieldname is NULL. Turns * any non-printable chars into JSON escapes, but leaves existing escapes alone. */ @@ -226,6 +194,25 @@ void json_add_hex(struct json_stream *result, const char *fieldname, void json_add_hex_talarr(struct json_stream *result, const char *fieldname, const tal_t *data); -void json_add_object(struct json_stream *result, ...); + +enum address_parse_result { + /* Not recognized as an onchain address */ + ADDRESS_PARSE_UNRECOGNIZED, + /* Recognized as an onchain address, but targets wrong network */ + ADDRESS_PARSE_WRONG_NETWORK, + /* Recognized and succeeds */ + ADDRESS_PARSE_SUCCESS, +}; +/* Return result of address parsing and fills in *scriptpubkey + * allocated off ctx if ADDRESS_PARSE_SUCCESS + */ +enum address_parse_result json_tok_address_scriptpubkey(const tal_t *ctx, + const struct chainparams *chainparams, + const char *buffer, + const jsmntok_t *tok, const u8 **scriptpubkey); + +/* Parse the satoshi token in wallet_tx. */ +bool json_tok_wtx(struct wallet_tx * tx, const char * buffer, + const jsmntok_t * sattok, u64 max); #endif /* LIGHTNING_LIGHTNINGD_JSON_H */ diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 9161c97f2..cd746a370 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -361,6 +361,63 @@ static void json_command_malformed(struct json_connection *jcon, json_stream_close(jcon->js, NULL); } +static struct json_stream *attach_json_stream(struct command *cmd) +{ + struct json_stream *js = new_json_stream(cmd, cmd); + + /* If they still care about the result, wake them */ + if (cmd->jcon) { + /* FIXME: We only allow one command at a time */ + assert(!cmd->jcon->js); + cmd->jcon->js = js; + io_wake(cmd->jcon); + } + assert(!cmd->have_json_stream); + cmd->have_json_stream = true; + return js; +} + +static struct json_stream *json_start(struct command *cmd) +{ + struct json_stream *js = attach_json_stream(cmd); + + json_stream_append_fmt(js, "{ \"jsonrpc\": \"2.0\", \"id\" : %s, ", + cmd->id); + return js; +} + +struct json_stream *json_stream_success(struct command *cmd) +{ + struct json_stream *r = json_start(cmd); + json_stream_append(r, "\"result\" : "); + return r; +} + +struct json_stream *json_stream_fail_nodata(struct command *cmd, + int code, + const char *errmsg) +{ + struct json_stream *r = json_start(cmd); + + assert(code); + assert(errmsg); + + json_stream_append_fmt(r, " \"error\" : " + "{ \"code\" : %d," + " \"message\" : \"%s\"", code, errmsg); + return r; +} + +struct json_stream *json_stream_fail(struct command *cmd, + int code, + const char *errmsg) +{ + struct json_stream *r = json_stream_fail_nodata(cmd, code, errmsg); + + json_stream_append(r, ", \"data\" : "); + return r; +} + /* Returns true if command already completed. */ static bool parse_request(struct json_connection *jcon, const jsmntok_t tok[]) { diff --git a/lightningd/jsonrpc.h b/lightningd/jsonrpc.h index 46e366f36..d73b978d0 100644 --- a/lightningd/jsonrpc.h +++ b/lightningd/jsonrpc.h @@ -8,10 +8,6 @@ #include #include -struct bitcoin_txid; -struct wireaddr; -struct wallet_tx; - /* The command mode tells param() how to process. */ enum command_mode { /* Normal command processing */ @@ -80,6 +76,40 @@ struct json_command { const char *verbose; }; +/** + * json_stream_success - start streaming a successful json result. + * @cmd: the command we're running. + * + * The returned value should go to command_success() when done. + * json_add_* will be placed into the 'result' field of the JSON reply. + */ +struct json_stream *json_stream_success(struct command *cmd); + +/** + * json_stream_fail - start streaming a failed json result. + * @cmd: the command we're running. + * @code: the error code from lightningd/jsonrpc_errors.h + * @errmsg: the error string. + * + * The returned value should go to command_failed() when done; + * json_add_* will be placed into the 'data' field of the 'error' JSON reply. + */ +struct json_stream *json_stream_fail(struct command *cmd, + int code, + const char *errmsg); + +/** + * json_stream_fail_nodata - start streaming a failed json result. + * @cmd: the command we're running. + * @code: the error code from lightningd/jsonrpc_errors.h + * @errmsg: the error string. + * + * This is used by command_fail(), which doesn't add any JSON data. + */ +struct json_stream *json_stream_fail_nodata(struct command *cmd, + int code, + const char *errmsg); + struct json_stream *null_response(struct command *cmd); void command_success(struct command *cmd, struct json_stream *response); void command_failed(struct command *cmd, struct json_stream *result); @@ -92,25 +122,5 @@ void command_still_pending(struct command *cmd); /* For initialization */ void setup_jsonrpc(struct lightningd *ld, const char *rpc_filename); -enum address_parse_result { - /* Not recognized as an onchain address */ - ADDRESS_PARSE_UNRECOGNIZED, - /* Recognized as an onchain address, but targets wrong network */ - ADDRESS_PARSE_WRONG_NETWORK, - /* Recognized and succeeds */ - ADDRESS_PARSE_SUCCESS, -}; -/* Return result of address parsing and fills in *scriptpubkey - * allocated off ctx if ADDRESS_PARSE_SUCCESS - */ -enum address_parse_result json_tok_address_scriptpubkey(const tal_t *ctx, - const struct chainparams *chainparams, - const char *buffer, - const jsmntok_t *tok, const u8 **scriptpubkey); - -/* Parse the satoshi token in wallet_tx. */ -bool json_tok_wtx(struct wallet_tx * tx, const char * buffer, - const jsmntok_t * sattok, u64 max); - AUTODATA_TYPE(json_command, struct json_command); #endif /* LIGHTNING_LIGHTNINGD_JSONRPC_H */