lnsocket

A minimal C library for connecting to the lightning network
git clone git://jb55.com/lnsocket
Log | Files | Refs | Submodules | README | LICENSE

commando.c (857B)


      1 
      2 #include "lnsocket.h"
      3 #include "cursor.h"
      4 #include "endian.h"
      5 #include "commando.h"
      6 #include "export.h"
      7 
      8 int EXPORT commando_make_rpc_msg(const char *method, const char *params,
      9 		const char *rune, unsigned int req_id, unsigned char *buf, int buflen)
     10 {
     11 	struct cursor msgbuf;
     12 	int ok;
     13 
     14 	make_cursor(buf, buf+buflen, &msgbuf);
     15 
     16 	params = (params == NULL || params[0] == '\0') ? "[]" : params;
     17 
     18 	if (!cursor_push_u16(&msgbuf, COMMANDO_CMD))
     19 		return 0;
     20 
     21 	if (!cursor_push_u64(&msgbuf, (u64)req_id))
     22 		return 0;
     23 
     24 	ok = cursor_push_str(&msgbuf, "{\"method\":\"") &&
     25 	cursor_push_str(&msgbuf, method) &&
     26 	cursor_push_str(&msgbuf, "\",\"params\":") &&
     27 	cursor_push_str(&msgbuf, params) &&
     28 	cursor_push_str(&msgbuf, ",\"rune\":\"") &&
     29 	cursor_push_str(&msgbuf, rune) &&
     30 	cursor_push_str(&msgbuf, "\"}");
     31 
     32 	if (!ok)
     33 		return 0;
     34 
     35 	return msgbuf.p - msgbuf.start;
     36 }