MCPcopy Create free account
hub / github.com/ElementsProject/lightning / jsonrpc_request_start_

Function jsonrpc_request_start_

lightningd/jsonrpc.c:1543–1600  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1541}
1542
1543struct jsonrpc_request *jsonrpc_request_start_(
1544 const tal_t *ctx, const char *method,
1545 const char *id_prefix, struct logger *log,
1546 bool add_header,
1547 void (*notify_cb)(const char *buffer,
1548 const jsmntok_t *methodtok,
1549 const jsmntok_t *paramtoks,
1550 const jsmntok_t *idtok,
1551 void *),
1552 void (*response_cb)(const char *buffer, const jsmntok_t *toks,
1553 const jsmntok_t *idtok, void *),
1554 void *response_cb_arg)
1555{
1556 struct jsonrpc_request *r = tal(ctx, struct jsonrpc_request);
1557 static u64 next_request_id = 0;
1558
1559 r->id_is_string = true;
1560 if (id_prefix) {
1561 /* Strip "" and otherwise sanity-check */
1562 if (strstarts(id_prefix, "\"")
1563 && strlen(id_prefix) > 1
1564 && strends(id_prefix, "\"")) {
1565 id_prefix = tal_strndup(tmpctx, id_prefix + 1,
1566 strlen(id_prefix) - 2);
1567 }
1568 /* We could try escaping, but TBH they're
1569 * messing with us at this point! */
1570 if (json_escape_needed(id_prefix, strlen(id_prefix)))
1571 id_prefix = "weird-id";
1572
1573 r->id = tal_fmt(r, "\"%s/cln:%s#%"PRIu64"\"",
1574 id_prefix, method, next_request_id);
1575 } else {
1576 r->id = tal_fmt(r, "\"cln:%s#%"PRIu64"\"", method, next_request_id);
1577 }
1578 tal_free_if_taken(id_prefix);
1579 next_request_id++;
1580 r->notify_cb = notify_cb;
1581 r->response_cb = response_cb;
1582 r->response_cb_arg = response_cb_arg;
1583 r->method = tal_strdup(r, method);
1584 r->stream = new_json_stream(r, NULL, log);
1585
1586 /* Disabling this serves as an escape hatch for plugin code to
1587 * get a raw request to paste into, but get a valid request-id
1588 * assigned. */
1589 if (add_header) {
1590 json_object_start(r->stream, NULL);
1591 json_add_string(r->stream, "jsonrpc", "2.0");
1592 json_add_id(r->stream, r->id);
1593 json_add_string(r->stream, "method", method);
1594 json_object_start(r->stream, "params");
1595 }
1596 if (log)
1597 log_io(log, LOG_IO_OUT, NULL, r->id, NULL, 0);
1598
1599 return r;
1600}

Callers

nothing calls this directly

Calls 8

strendsFunction · 0.85
json_escape_neededFunction · 0.85
tal_free_if_takenFunction · 0.85
new_json_streamFunction · 0.85
json_object_startFunction · 0.85
json_add_idFunction · 0.85
log_ioFunction · 0.70
json_add_stringFunction · 0.50

Tested by

no test coverage detected