2
2
#include <common/json_param.h>
3
3
#include <common/json_stream.h>
4
4
#include <common/onion_encode.h>
5
+ #include <plugins/renepay/json.h>
5
6
#include <plugins/renepay/payplugin.h>
6
7
#include <plugins/renepay/sendpay.h>
7
8
@@ -133,22 +134,37 @@ struct renesendpay {
133
134
unsigned int blockheight ;
134
135
};
135
136
136
- static struct command_result * sendpay_rpc_failure (struct command * cmd ,
137
- const char * method UNUSED ,
138
- const char * buffer ,
139
- const jsmntok_t * toks ,
140
- struct renesendpay * renesendpay )
137
+ static struct command_result * rpc_fail (struct command * cmd ,
138
+ const char * method ,
139
+ const char * buffer ,
140
+ const jsmntok_t * toks ,
141
+ struct renesendpay * renesendpay )
141
142
{
143
+ plugin_log (cmd -> plugin , LOG_UNUSUAL ,
144
+ "renesendpay failed calling %s: %.*s" , method ,
145
+ json_tok_full_len (toks ), json_tok_full (buffer , toks ));
142
146
const jsmntok_t * codetok = json_get_member (buffer , toks , "code" );
147
+ const jsmntok_t * msgtok = json_get_member (buffer , toks , "message" );
148
+ const char * msg ;
149
+ if (msgtok )
150
+ msg = json_strdup (tmpctx , buffer , msgtok );
151
+ else
152
+ msg = "" ;
143
153
u32 errcode ;
144
154
if (codetok != NULL )
145
155
json_to_u32 (buffer , codetok , & errcode );
146
156
else
147
- errcode = LIGHTNINGD ;
148
- // FIXME: make this response more useful, maybe similar to waitsendpay
149
- return command_fail (
150
- cmd , errcode , "renesendpay failed to error in RPC: %.*s" ,
151
- json_tok_full_len (toks ), json_tok_full (buffer , toks ));
157
+ errcode = PLUGIN_ERROR ;
158
+ struct json_stream * response = jsonrpc_stream_fail (
159
+ cmd , errcode ,
160
+ tal_fmt (tmpctx , "%s failed: %s" , method , msg ? msg : "\"\"" ));
161
+ json_object_start (response , "data" );
162
+ json_add_sha256 (response , "payment_hash" , & renesendpay -> payment_hash );
163
+ json_add_string (response , "status" , "failed" );
164
+ json_add_amount_msat (response , "amount_sent_msat" ,
165
+ renesendpay -> sent_amount );
166
+ json_object_end (response );
167
+ return command_finished (cmd , response );
152
168
}
153
169
154
170
static void sphinx_append_blinded_path (const tal_t * ctx ,
@@ -261,37 +277,6 @@ static const u8 *create_onion(const tal_t *ctx,
261
277
return onion ;
262
278
}
263
279
264
- static struct command_result * sendpay_fail (struct command * cmd ,
265
- const char * method UNUSED ,
266
- const char * buffer ,
267
- const jsmntok_t * toks ,
268
- struct renesendpay * renesendpay )
269
- {
270
- plugin_log (cmd -> plugin , LOG_UNUSUAL ,
271
- "renesendpay failed calling sendpay: %.*s" ,
272
- json_tok_full_len (toks ), json_tok_full (buffer , toks ));
273
- const jsmntok_t * codetok = json_get_member (buffer , toks , "code" );
274
- const jsmntok_t * msgtok = json_get_member (buffer , toks , "message" );
275
- const char * msg = NULL ;
276
- if (msgtok )
277
- msg = json_strdup (tmpctx , buffer , msgtok );
278
- u32 errcode ;
279
- if (codetok != NULL )
280
- json_to_u32 (buffer , codetok , & errcode );
281
- else
282
- errcode = PLUGIN_ERROR ;
283
- struct json_stream * response = jsonrpc_stream_fail (
284
- cmd , errcode ,
285
- tal_fmt (tmpctx , "sendpay failed: %s" , msg ? msg : "\"\"" ));
286
- json_object_start (response , "data" );
287
- json_add_sha256 (response , "payment_hash" , & renesendpay -> payment_hash );
288
- json_add_string (response , "status" , "failed" );
289
- json_add_amount_msat (response , "amount_sent_msat" ,
290
- renesendpay -> sent_amount );
291
- json_object_end (response );
292
- return command_finished (cmd , response );
293
- }
294
-
295
280
static struct command_result * renesendpay_done (struct command * cmd ,
296
281
const char * method UNUSED ,
297
282
const char * buffer ,
@@ -357,39 +342,6 @@ static struct command_result *renesendpay_done(struct command *cmd,
357
342
return command_finished (cmd , response );
358
343
}
359
344
360
- static struct command_result * waitblockheight_fail (struct command * cmd ,
361
- const char * method UNUSED ,
362
- const char * buffer ,
363
- const jsmntok_t * toks ,
364
- struct renesendpay * renesendpay )
365
- {
366
- plugin_log (cmd -> plugin , LOG_UNUSUAL ,
367
- "renesendpay failed calling waitblockheight: %.*s" ,
368
- json_tok_full_len (toks ), json_tok_full (buffer , toks ));
369
- const jsmntok_t * codetok = json_get_member (buffer , toks , "code" );
370
- const jsmntok_t * msgtok = json_get_member (buffer , toks , "message" );
371
- const char * msg ;
372
- if (msgtok )
373
- msg = json_strdup (tmpctx , buffer , msgtok );
374
- else
375
- msg = "" ;
376
- u32 errcode ;
377
- if (codetok != NULL )
378
- json_to_u32 (buffer , codetok , & errcode );
379
- else
380
- errcode = PLUGIN_ERROR ;
381
- struct json_stream * response = jsonrpc_stream_fail (
382
- cmd , errcode ,
383
- tal_fmt (tmpctx , "waitblockheight failed: %s" , msg ? msg : "\"\"" ));
384
- json_object_start (response , "data" );
385
- json_add_sha256 (response , "payment_hash" , & renesendpay -> payment_hash );
386
- json_add_string (response , "status" , "failed" );
387
- json_add_amount_msat (response , "amount_sent_msat" ,
388
- renesendpay -> sent_amount );
389
- json_object_end (response );
390
- return command_finished (cmd , response );
391
- }
392
-
393
345
static struct command_result * waitblockheight_done (struct command * cmd ,
394
346
const char * method UNUSED ,
395
347
const char * buffer ,
@@ -412,7 +364,7 @@ static struct command_result *waitblockheight_done(struct command *cmd,
412
364
onion = create_onion (tmpctx , renesendpay ,
413
365
renesendpay -> route [0 ].node_id , 1 );
414
366
req = jsonrpc_request_start (cmd , "sendonion" , renesendpay_done ,
415
- sendpay_rpc_failure , renesendpay );
367
+ rpc_fail , renesendpay );
416
368
json_add_hex_talarr (req -> js , "onion" , onion );
417
369
418
370
json_add_amount_msat (req -> js , "amount_msat" ,
@@ -442,16 +394,13 @@ static struct command_result *waitblockheight_done(struct command *cmd,
442
394
/* self payment */
443
395
onion = NULL ;
444
396
req = jsonrpc_request_start (cmd , "sendpay" , renesendpay_done ,
445
- sendpay_fail , renesendpay );
397
+ rpc_fail , renesendpay );
446
398
json_array_start (req -> js , "route" );
447
399
json_array_end (req -> js );
448
400
json_add_amount_msat (req -> js , "amount_msat" ,
449
401
renesendpay -> total_amount );
450
402
if (renesendpay -> payment_secret )
451
403
json_add_secret (req -> js , "payment_secret" , renesendpay -> payment_secret );
452
- else {
453
- // FIXME: get payment_secret from blinded path
454
- }
455
404
}
456
405
457
406
json_add_sha256 (req -> js , "payment_hash" , & renesendpay -> payment_hash );
@@ -547,7 +496,7 @@ struct command_result *json_renesendpay(struct command *cmd,
547
496
548
497
struct out_req * req =
549
498
jsonrpc_request_start (cmd , "waitblockheight" , waitblockheight_done ,
550
- waitblockheight_fail , renesendpay );
499
+ rpc_fail , renesendpay );
551
500
json_add_num (req -> js , "blockheight" , 0 );
552
501
return send_outreq (req );
553
502
}
0 commit comments