2016-09-06 09:17:49 +02:00
|
|
|
#include "invoice.h"
|
|
|
|
#include "jsonrpc.h"
|
|
|
|
#include "lightningd.h"
|
2017-11-22 01:25:39 +01:00
|
|
|
#include <bitcoin/address.h>
|
|
|
|
#include <bitcoin/base58.h>
|
|
|
|
#include <bitcoin/script.h>
|
2016-09-06 09:17:49 +02:00
|
|
|
#include <ccan/str/hex/hex.h>
|
|
|
|
#include <ccan/structeq/structeq.h>
|
|
|
|
#include <ccan/tal/str/str.h>
|
2017-11-22 01:25:39 +01:00
|
|
|
#include <common/bech32.h>
|
|
|
|
#include <common/bolt11.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
2017-11-22 01:25:39 +01:00
|
|
|
#include <errno.h>
|
2017-11-30 17:07:38 +01:00
|
|
|
#include <hsmd/gen_hsm_client_wire.h>
|
2017-10-05 23:29:56 +02:00
|
|
|
#include <inttypes.h>
|
2017-11-22 01:25:39 +01:00
|
|
|
#include <lightningd/hsm_control.h>
|
2017-10-05 23:29:56 +02:00
|
|
|
#include <lightningd/log.h>
|
2018-01-17 04:04:29 +01:00
|
|
|
#include <lightningd/options.h>
|
2016-09-06 09:17:49 +02:00
|
|
|
#include <sodium/randombytes.h>
|
2017-11-22 01:25:39 +01:00
|
|
|
#include <wire/wire_sync.h>
|
2016-09-06 09:17:49 +02:00
|
|
|
|
2018-01-17 04:04:29 +01:00
|
|
|
static const char *invoice_status_str(const struct invoice *inv)
|
|
|
|
{
|
|
|
|
if (inv->state == PAID)
|
|
|
|
return "paid";
|
|
|
|
if (time_now().ts.tv_sec > inv->expiry_time)
|
|
|
|
return "expired";
|
|
|
|
return "unpaid";
|
|
|
|
}
|
|
|
|
|
2018-01-13 12:14:13 +01:00
|
|
|
static void json_add_invoice(struct json_result *response,
|
2018-01-17 04:04:29 +01:00
|
|
|
const struct invoice *inv,
|
|
|
|
bool modern)
|
2018-01-13 12:14:13 +01:00
|
|
|
{
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_string(response, "label", inv->label);
|
2018-01-13 12:17:34 +01:00
|
|
|
json_add_hex(response, "payment_hash", &inv->rhash, sizeof(inv->rhash));
|
2018-01-13 12:14:13 +01:00
|
|
|
if (inv->msatoshi)
|
|
|
|
json_add_u64(response, "msatoshi", *inv->msatoshi);
|
2018-01-17 04:04:29 +01:00
|
|
|
if (modern)
|
|
|
|
json_add_string(response, "status", invoice_status_str(inv));
|
|
|
|
else if (deprecated_apis && !modern)
|
|
|
|
json_add_bool(response, "complete", inv->state == PAID);
|
2018-01-13 12:19:33 +01:00
|
|
|
if (inv->state == PAID) {
|
2018-01-13 12:14:13 +01:00
|
|
|
json_add_u64(response, "pay_index", inv->pay_index);
|
2018-01-13 12:19:33 +01:00
|
|
|
json_add_u64(response, "msatoshi_received",
|
|
|
|
inv->msatoshi_received);
|
2018-01-16 13:59:22 +01:00
|
|
|
json_add_u64(response, "paid_timestamp",
|
|
|
|
inv->paid_timestamp);
|
2018-01-13 12:19:33 +01:00
|
|
|
}
|
2018-01-13 12:14:13 +01:00
|
|
|
json_add_u64(response, "expiry_time", inv->expiry_time);
|
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
|
2016-09-06 09:17:49 +02:00
|
|
|
static void tell_waiter(struct command *cmd, const struct invoice *paid)
|
|
|
|
{
|
|
|
|
struct json_result *response = new_json_result(cmd);
|
|
|
|
|
2018-01-17 04:04:29 +01:00
|
|
|
json_add_invoice(response, paid, true);
|
2016-09-06 09:17:49 +02:00
|
|
|
command_success(cmd, response);
|
|
|
|
}
|
2018-01-14 15:15:30 +01:00
|
|
|
static void tell_waiter_deleted(struct command *cmd)
|
2017-12-27 13:55:22 +01:00
|
|
|
{
|
|
|
|
command_fail(cmd, "invoice deleted during wait");
|
|
|
|
}
|
2018-01-14 15:15:30 +01:00
|
|
|
static void wait_on_invoice(const struct invoice *invoice, void *cmd)
|
2016-09-06 09:17:49 +02:00
|
|
|
{
|
2018-01-14 15:15:30 +01:00
|
|
|
if (invoice)
|
|
|
|
tell_waiter((struct command *) cmd, invoice);
|
|
|
|
else
|
|
|
|
tell_waiter_deleted((struct command *) cmd);
|
2016-09-06 09:17:49 +02:00
|
|
|
}
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2017-11-22 01:25:39 +01:00
|
|
|
static bool hsm_sign_b11(const u5 *u5bytes,
|
|
|
|
const u8 *hrpu8,
|
|
|
|
secp256k1_ecdsa_recoverable_signature *rsig,
|
|
|
|
struct lightningd *ld)
|
|
|
|
{
|
2017-11-30 17:07:38 +01:00
|
|
|
u8 *msg = towire_hsm_sign_invoice(ld, u5bytes, hrpu8);
|
2017-11-22 01:25:39 +01:00
|
|
|
|
|
|
|
if (!wire_sync_write(ld->hsm_fd, take(msg)))
|
|
|
|
fatal("Could not write to HSM: %s", strerror(errno));
|
|
|
|
|
|
|
|
msg = hsm_sync_read(ld, ld);
|
2017-11-30 17:07:38 +01:00
|
|
|
if (!fromwire_hsm_sign_invoice_reply(msg, NULL, rsig))
|
2017-11-22 01:25:39 +01:00
|
|
|
fatal("HSM gave bad sign_invoice_reply %s",
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
|
|
|
|
tal_free(msg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-06 09:17:49 +02:00
|
|
|
static void json_invoice(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
2018-01-14 15:15:30 +01:00
|
|
|
const struct invoice *invoice;
|
2017-11-21 07:26:15 +01:00
|
|
|
jsmntok_t *msatoshi, *label, *desc, *exp;
|
2018-01-14 15:15:30 +01:00
|
|
|
u64 *msatoshi_val;
|
|
|
|
const char *label_val;
|
2016-11-11 00:02:04 +01:00
|
|
|
struct json_result *response = new_json_result(cmd);
|
2018-01-14 15:15:30 +01:00
|
|
|
struct wallet *wallet = cmd->ld->wallet;
|
2017-10-26 05:06:19 +02:00
|
|
|
struct bolt11 *b11;
|
|
|
|
char *b11enc;
|
2017-11-21 07:26:15 +01:00
|
|
|
u64 expiry = 3600;
|
2016-09-06 09:17:49 +02:00
|
|
|
|
|
|
|
if (!json_get_params(buffer, params,
|
2018-01-12 15:39:57 +01:00
|
|
|
"msatoshi", &msatoshi,
|
2016-09-06 09:17:49 +02:00
|
|
|
"label", &label,
|
2017-10-26 05:06:19 +02:00
|
|
|
"description", &desc,
|
2017-11-21 07:26:15 +01:00
|
|
|
"?expiry", &exp,
|
2016-09-06 09:17:49 +02:00
|
|
|
NULL)) {
|
2018-01-12 15:39:57 +01:00
|
|
|
command_fail(cmd, "Need {msatoshi}, {label} and {description}");
|
2016-09-06 09:17:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-14 15:15:30 +01:00
|
|
|
/* Get arguments. */
|
|
|
|
/* msatoshi */
|
2018-01-10 02:28:44 +01:00
|
|
|
if (json_tok_streq(buffer, msatoshi, "any"))
|
2018-01-14 15:15:30 +01:00
|
|
|
msatoshi_val = NULL;
|
2018-01-10 02:28:44 +01:00
|
|
|
else {
|
2018-01-14 15:15:30 +01:00
|
|
|
msatoshi_val = tal(cmd, u64);
|
|
|
|
if (!json_tok_u64(buffer, msatoshi, msatoshi_val)
|
|
|
|
|| *msatoshi_val == 0) {
|
2018-01-10 02:28:44 +01:00
|
|
|
command_fail(cmd,
|
|
|
|
"'%.*s' is not a valid positive number",
|
|
|
|
msatoshi->end - msatoshi->start,
|
|
|
|
buffer + msatoshi->start);
|
|
|
|
return;
|
|
|
|
}
|
2016-09-06 09:17:49 +02:00
|
|
|
}
|
2018-01-14 15:15:30 +01:00
|
|
|
/* label */
|
|
|
|
label_val = tal_strndup(cmd, buffer + label->start,
|
|
|
|
label->end - label->start);
|
|
|
|
if (wallet_invoice_find_by_label(wallet, label_val)) {
|
|
|
|
command_fail(cmd, "Duplicate label '%s'", label_val);
|
2016-09-06 09:17:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-01-14 15:15:30 +01:00
|
|
|
if (strlen(label_val) > INVOICE_MAX_LABEL_LEN) {
|
|
|
|
command_fail(cmd, "label '%s' over %u bytes", label_val,
|
2016-09-06 09:17:49 +02:00
|
|
|
INVOICE_MAX_LABEL_LEN);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-21 07:26:15 +01:00
|
|
|
if (exp && !json_tok_u64(buffer, exp, &expiry)) {
|
|
|
|
command_fail(cmd, "expiry '%.*s' invalid seconds",
|
|
|
|
exp->end - exp->start,
|
|
|
|
buffer + exp->start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-14 15:15:30 +01:00
|
|
|
invoice = wallet_invoice_create(cmd->ld->wallet,
|
|
|
|
take(msatoshi_val),
|
|
|
|
take(label_val),
|
|
|
|
expiry);
|
|
|
|
if (!invoice) {
|
|
|
|
command_fail(cmd, "Failed to create invoice on database");
|
|
|
|
return;
|
|
|
|
}
|
2017-10-04 14:45:00 +02:00
|
|
|
|
2017-10-26 05:06:19 +02:00
|
|
|
/* Construct bolt11 string. */
|
2018-01-09 15:14:42 +01:00
|
|
|
b11 = new_bolt11(cmd, invoice->msatoshi);
|
2017-10-26 05:06:19 +02:00
|
|
|
b11->chain = get_chainparams(cmd->ld);
|
|
|
|
b11->timestamp = time_now().ts.tv_sec;
|
|
|
|
b11->payment_hash = invoice->rhash;
|
|
|
|
b11->receiver_id = cmd->ld->id;
|
2017-10-26 05:08:19 +02:00
|
|
|
b11->min_final_cltv_expiry = cmd->ld->config.cltv_final;
|
2017-11-21 07:26:15 +01:00
|
|
|
b11->expiry = expiry;
|
2017-10-26 05:06:19 +02:00
|
|
|
if (desc->end - desc->start >= BOLT11_FIELD_BYTE_LIMIT) {
|
|
|
|
b11->description_hash = tal(b11, struct sha256);
|
|
|
|
sha256(b11->description_hash, buffer + desc->start,
|
|
|
|
desc->end - desc->start);
|
|
|
|
} else
|
|
|
|
b11->description = tal_strndup(b11, buffer + desc->start,
|
|
|
|
desc->end - desc->start);
|
|
|
|
|
|
|
|
/* FIXME: add private routes if necessary! */
|
2017-11-22 01:25:39 +01:00
|
|
|
b11enc = bolt11_encode(cmd, b11, false, hsm_sign_b11, cmd->ld);
|
2017-10-26 05:06:19 +02:00
|
|
|
|
2016-09-06 09:17:49 +02:00
|
|
|
json_object_start(response, NULL);
|
2018-01-13 12:17:34 +01:00
|
|
|
json_add_hex(response, "payment_hash",
|
2016-09-06 09:17:49 +02:00
|
|
|
&invoice->rhash, sizeof(invoice->rhash));
|
2017-11-24 10:35:52 +01:00
|
|
|
json_add_u64(response, "expiry_time", invoice->expiry_time);
|
2017-10-26 05:06:19 +02:00
|
|
|
json_add_string(response, "bolt11", b11enc);
|
|
|
|
if (b11->description_hash)
|
|
|
|
json_add_string(response, "description", b11->description);
|
2016-09-06 09:17:49 +02:00
|
|
|
json_object_end(response);
|
|
|
|
|
|
|
|
command_success(cmd, response);
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:38:15 +01:00
|
|
|
static const struct json_command invoice_command = {
|
2016-09-06 09:17:49 +02:00
|
|
|
"invoice",
|
|
|
|
json_invoice,
|
2017-11-21 07:26:15 +01:00
|
|
|
"Create invoice for {msatoshi} with {label} and {description} with optional {expiry} seconds (default 1 hour)",
|
2018-01-13 12:17:34 +01:00
|
|
|
"Returns the {payment_hash}, {expiry_time} and {bolt11} on success, and {description} if too large for {bolt11}. "
|
2016-09-06 09:17:49 +02:00
|
|
|
};
|
2017-01-04 04:38:15 +01:00
|
|
|
AUTODATA(json_command, &invoice_command);
|
2016-09-06 09:17:49 +02:00
|
|
|
|
2016-09-06 09:17:49 +02:00
|
|
|
static void json_add_invoices(struct json_result *response,
|
2018-01-14 15:15:30 +01:00
|
|
|
struct wallet *wallet,
|
2018-01-17 04:04:29 +01:00
|
|
|
const char *buffer, const jsmntok_t *label,
|
|
|
|
bool modern)
|
2016-09-06 09:17:49 +02:00
|
|
|
{
|
2018-01-14 15:15:30 +01:00
|
|
|
const struct invoice *i;
|
2016-10-23 18:44:52 +02:00
|
|
|
char *lbl = NULL;
|
|
|
|
if (label)
|
|
|
|
lbl = tal_strndup(response, &buffer[label->start], label->end - label->start);
|
2016-09-06 09:17:49 +02:00
|
|
|
|
2018-01-14 15:15:30 +01:00
|
|
|
i = NULL;
|
|
|
|
while ((i = wallet_invoice_iterate(wallet, i)) != NULL) {
|
2016-10-23 18:44:52 +02:00
|
|
|
if (lbl && !streq(i->label, lbl))
|
2016-09-06 09:17:49 +02:00
|
|
|
continue;
|
2018-01-17 04:04:29 +01:00
|
|
|
json_add_invoice(response, i, modern);
|
2016-09-06 09:17:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 21:29:32 +01:00
|
|
|
static void json_listinvoice_internal(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *params,
|
|
|
|
bool modern)
|
2016-09-06 09:17:49 +02:00
|
|
|
{
|
|
|
|
jsmntok_t *label = NULL;
|
2016-11-11 00:02:04 +01:00
|
|
|
struct json_result *response = new_json_result(cmd);
|
2018-01-14 15:15:30 +01:00
|
|
|
struct wallet *wallet = cmd->ld->wallet;
|
2016-09-06 09:17:49 +02:00
|
|
|
|
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"?label", &label,
|
|
|
|
NULL)) {
|
|
|
|
command_fail(cmd, "Invalid arguments");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-16 21:29:32 +01:00
|
|
|
if (modern) {
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_array_start(response, "invoices");
|
|
|
|
} else
|
|
|
|
json_array_start(response, NULL);
|
2018-01-17 04:04:29 +01:00
|
|
|
json_add_invoices(response, wallet, buffer, label, modern);
|
2016-09-06 09:17:49 +02:00
|
|
|
json_array_end(response);
|
2018-01-16 21:29:32 +01:00
|
|
|
if (modern)
|
|
|
|
json_object_end(response);
|
2016-09-06 09:17:49 +02:00
|
|
|
command_success(cmd, response);
|
|
|
|
}
|
|
|
|
|
2018-01-16 21:29:32 +01:00
|
|
|
/* FIXME: Deprecated! */
|
|
|
|
static void json_listinvoice(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
return json_listinvoice_internal(cmd, buffer, params, false);
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:38:15 +01:00
|
|
|
static const struct json_command listinvoice_command = {
|
2016-09-06 09:17:49 +02:00
|
|
|
"listinvoice",
|
|
|
|
json_listinvoice,
|
2018-01-16 21:28:46 +01:00
|
|
|
"(DEPRECATED) Show invoice {label} (or all, if no {label}))",
|
|
|
|
"Returns an array of {label}, {payment_hash}, {msatoshi} (if set), {complete}, {pay_index} (if paid) and {expiry_time} on success. ",
|
|
|
|
.deprecated = true
|
2016-09-06 09:17:49 +02:00
|
|
|
};
|
2017-01-04 04:38:15 +01:00
|
|
|
AUTODATA(json_command, &listinvoice_command);
|
2016-09-06 09:17:49 +02:00
|
|
|
|
2018-01-16 21:29:32 +01:00
|
|
|
static void json_listinvoices(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
return json_listinvoice_internal(cmd, buffer, params, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command listinvoices_command = {
|
2018-01-16 21:28:46 +01:00
|
|
|
"listinvoices",
|
|
|
|
json_listinvoices,
|
|
|
|
"Show invoice {label} (or all, if no {label}))",
|
|
|
|
"Returns an array of {label}, {payment_hash}, {msatoshi} (if set), {complete}, {pay_index} (if paid) and {expiry_time} on success. ",
|
|
|
|
};
|
|
|
|
AUTODATA(json_command, &listinvoices_command);
|
|
|
|
|
2016-09-06 09:17:49 +02:00
|
|
|
static void json_delinvoice(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
2018-01-14 15:15:30 +01:00
|
|
|
const struct invoice *i;
|
2016-09-06 09:17:49 +02:00
|
|
|
jsmntok_t *labeltok;
|
|
|
|
struct json_result *response = new_json_result(cmd);
|
|
|
|
const char *label;
|
2018-01-14 15:15:30 +01:00
|
|
|
struct wallet *wallet = cmd->ld->wallet;
|
|
|
|
bool error;
|
2016-09-06 09:17:49 +02:00
|
|
|
|
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"label", &labeltok,
|
|
|
|
NULL)) {
|
|
|
|
command_fail(cmd, "Invalid arguments");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
label = tal_strndup(cmd, buffer + labeltok->start,
|
|
|
|
labeltok->end - labeltok->start);
|
2018-01-14 15:15:30 +01:00
|
|
|
i = wallet_invoice_find_by_label(wallet, label);
|
2016-09-06 09:17:49 +02:00
|
|
|
if (!i) {
|
|
|
|
command_fail(cmd, "Unknown invoice");
|
|
|
|
return;
|
|
|
|
}
|
2017-10-05 23:29:56 +02:00
|
|
|
|
2017-12-27 12:51:58 +01:00
|
|
|
/* Get invoice details before attempting to delete, as
|
|
|
|
* otherwise the invoice will be freed. */
|
2018-01-17 04:04:29 +01:00
|
|
|
json_add_invoice(response, i, true);
|
2017-12-27 12:51:58 +01:00
|
|
|
|
2018-01-14 15:15:30 +01:00
|
|
|
error = wallet_invoice_delete(wallet, i);
|
2017-12-27 12:51:58 +01:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
log_broken(cmd->ld->log, "Error attempting to remove invoice %"PRIu64,
|
|
|
|
i->id);
|
2018-01-14 15:15:30 +01:00
|
|
|
command_fail(cmd, "Database error");
|
2017-12-27 12:51:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-06 09:17:49 +02:00
|
|
|
command_success(cmd, response);
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:38:15 +01:00
|
|
|
static const struct json_command delinvoice_command = {
|
2016-09-06 09:17:49 +02:00
|
|
|
"delinvoice",
|
|
|
|
json_delinvoice,
|
|
|
|
"Delete unpaid invoice {label}))",
|
2018-01-13 12:17:34 +01:00
|
|
|
"Returns {label}, {payment_hash}, {msatoshi} (if set), {complete}, {pay_index} (if paid) and {expiry_time} on success. "
|
2016-09-06 09:17:49 +02:00
|
|
|
};
|
2017-01-04 04:38:15 +01:00
|
|
|
AUTODATA(json_command, &delinvoice_command);
|
2016-09-06 09:17:49 +02:00
|
|
|
|
2017-01-17 22:09:09 +01:00
|
|
|
static void json_waitanyinvoice(struct command *cmd,
|
2016-09-06 09:17:49 +02:00
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
2017-12-26 23:58:42 +01:00
|
|
|
jsmntok_t *pay_indextok;
|
|
|
|
u64 pay_index;
|
2017-12-26 14:47:27 +01:00
|
|
|
struct wallet *wallet = cmd->ld->wallet;
|
2016-09-06 09:17:49 +02:00
|
|
|
|
|
|
|
if (!json_get_params(buffer, params,
|
2017-12-26 23:58:42 +01:00
|
|
|
"?lastpay_index", &pay_indextok,
|
2016-09-06 09:17:49 +02:00
|
|
|
NULL)) {
|
|
|
|
command_fail(cmd, "Invalid arguments");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-26 23:58:42 +01:00
|
|
|
if (!pay_indextok) {
|
|
|
|
pay_index = 0;
|
2017-10-03 18:31:07 +02:00
|
|
|
} else {
|
2017-12-26 23:58:42 +01:00
|
|
|
if (!json_tok_u64(buffer, pay_indextok, &pay_index)) {
|
|
|
|
command_fail(cmd, "'%.*s' is not a valid number",
|
|
|
|
pay_indextok->end - pay_indextok->start,
|
|
|
|
buffer + pay_indextok->start);
|
|
|
|
return;
|
|
|
|
}
|
2017-12-26 14:47:27 +01:00
|
|
|
}
|
|
|
|
|
2018-01-14 15:15:30 +01:00
|
|
|
/* Set command as pending. We do not know if
|
|
|
|
* wallet_invoice_waitany will return immediately
|
|
|
|
* or not, so indicating pending is safest. */
|
2017-12-15 11:15:54 +01:00
|
|
|
command_still_pending(cmd);
|
2018-01-14 15:15:30 +01:00
|
|
|
|
|
|
|
/* Find next paid invoice. */
|
|
|
|
wallet_invoice_waitany(cmd, wallet, pay_index,
|
|
|
|
&wait_on_invoice, (void*) cmd);
|
2016-09-06 09:17:49 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 22:09:09 +01:00
|
|
|
static const struct json_command waitanyinvoice_command = {
|
|
|
|
"waitanyinvoice",
|
|
|
|
json_waitanyinvoice,
|
2017-12-26 23:58:42 +01:00
|
|
|
"Wait for the next invoice to be paid, after {lastpay_index} (if supplied)))",
|
2018-01-13 12:17:34 +01:00
|
|
|
"Returns {label}, {payment_hash}, {msatoshi} (if set), {complete}, {pay_index} and {expiry_time} on success. "
|
2016-09-06 09:17:49 +02:00
|
|
|
};
|
2017-01-17 22:09:09 +01:00
|
|
|
AUTODATA(json_command, &waitanyinvoice_command);
|
2017-01-10 12:18:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* Wait for an incoming payment matching the `label` in the JSON
|
|
|
|
* command. This will either return immediately if the payment has
|
|
|
|
* already been received or it may add the `cmd` to the list of
|
|
|
|
* waiters, if the payment is still pending.
|
|
|
|
*/
|
2017-01-17 22:09:09 +01:00
|
|
|
static void json_waitinvoice(struct command *cmd,
|
2017-01-10 12:18:40 +01:00
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
2018-01-14 15:15:30 +01:00
|
|
|
const struct invoice *i;
|
|
|
|
struct wallet *wallet = cmd->ld->wallet;
|
2017-01-10 12:18:40 +01:00
|
|
|
jsmntok_t *labeltok;
|
|
|
|
const char *label = NULL;
|
|
|
|
|
|
|
|
if (!json_get_params(buffer, params, "label", &labeltok, NULL)) {
|
|
|
|
command_fail(cmd, "Missing {label}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search in paid invoices, if found return immediately */
|
|
|
|
label = tal_strndup(cmd, buffer + labeltok->start, labeltok->end - labeltok->start);
|
2018-01-14 15:15:30 +01:00
|
|
|
i = wallet_invoice_find_by_label(wallet, label);
|
2017-01-10 12:18:40 +01:00
|
|
|
|
|
|
|
if (!i) {
|
|
|
|
command_fail(cmd, "Label not found");
|
|
|
|
return;
|
2017-10-03 18:31:07 +02:00
|
|
|
} else if (i->state == PAID) {
|
|
|
|
tell_waiter(cmd, i);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
/* There is an unpaid one matching, let's wait... */
|
2017-12-15 11:15:54 +01:00
|
|
|
command_still_pending(cmd);
|
2018-01-14 15:15:30 +01:00
|
|
|
wallet_invoice_waitone(cmd, wallet, i,
|
|
|
|
&wait_on_invoice, (void *) cmd);
|
2017-01-10 12:18:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 22:09:09 +01:00
|
|
|
static const struct json_command waitinvoice_command = {
|
|
|
|
"waitinvoice",
|
|
|
|
json_waitinvoice,
|
2017-01-10 12:18:40 +01:00
|
|
|
"Wait for an incoming payment matching the invoice with {label}",
|
2018-01-13 12:17:34 +01:00
|
|
|
"Returns {label}, {payment_hash}, {msatoshi} (if set), {complete}, {pay_index} and {expiry_time} on success"
|
2017-01-10 12:18:40 +01:00
|
|
|
};
|
2017-01-17 22:09:09 +01:00
|
|
|
AUTODATA(json_command, &waitinvoice_command);
|
2017-11-22 01:25:39 +01:00
|
|
|
|
|
|
|
static void json_decodepay(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
jsmntok_t *bolt11tok, *desctok;
|
|
|
|
struct bolt11 *b11;
|
|
|
|
struct json_result *response;
|
|
|
|
char *str, *desc, *fail;
|
|
|
|
|
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"bolt11", &bolt11tok,
|
|
|
|
"?description", &desctok,
|
|
|
|
NULL)) {
|
|
|
|
command_fail(cmd, "Need bolt11 string");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
str = tal_strndup(cmd, buffer + bolt11tok->start,
|
|
|
|
bolt11tok->end - bolt11tok->start);
|
|
|
|
|
|
|
|
if (desctok)
|
|
|
|
desc = tal_strndup(cmd, buffer + desctok->start,
|
|
|
|
desctok->end - desctok->start);
|
|
|
|
else
|
|
|
|
desc = NULL;
|
|
|
|
|
|
|
|
b11 = bolt11_decode(cmd, str, desc, &fail);
|
|
|
|
|
|
|
|
if (!b11) {
|
|
|
|
command_fail(cmd, "Invalid bolt11: %s", fail);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
response = new_json_result(cmd);
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
|
|
|
|
json_add_string(response, "currency", b11->chain->bip173_name);
|
|
|
|
json_add_u64(response, "timestamp", b11->timestamp);
|
|
|
|
json_add_u64(response, "expiry", b11->expiry);
|
|
|
|
json_add_pubkey(response, "payee", &b11->receiver_id);
|
|
|
|
if (b11->msatoshi)
|
|
|
|
json_add_u64(response, "msatoshi", *b11->msatoshi);
|
|
|
|
if (b11->description)
|
|
|
|
json_add_string(response, "description", b11->description);
|
|
|
|
if (b11->description_hash)
|
|
|
|
json_add_hex(response, "description_hash",
|
|
|
|
b11->description_hash,
|
|
|
|
sizeof(*b11->description_hash));
|
2018-01-16 20:44:32 +01:00
|
|
|
json_add_num(response, "min_final_cltv_expiry",
|
|
|
|
b11->min_final_cltv_expiry);
|
2017-11-22 01:25:39 +01:00
|
|
|
if (tal_len(b11->fallback)) {
|
|
|
|
struct bitcoin_address pkh;
|
|
|
|
struct ripemd160 sh;
|
|
|
|
struct sha256 wsh;
|
|
|
|
|
|
|
|
json_object_start(response, "fallback");
|
|
|
|
if (is_p2pkh(b11->fallback, &pkh)) {
|
|
|
|
json_add_string(response, "type", "P2PKH");
|
|
|
|
json_add_string(response, "addr",
|
|
|
|
bitcoin_to_base58(cmd,
|
|
|
|
b11->chain->testnet,
|
|
|
|
&pkh));
|
|
|
|
} else if (is_p2sh(b11->fallback, &sh)) {
|
|
|
|
json_add_string(response, "type", "P2SH");
|
|
|
|
json_add_string(response, "addr",
|
|
|
|
p2sh_to_base58(cmd,
|
|
|
|
b11->chain->testnet,
|
|
|
|
&sh));
|
|
|
|
} else if (is_p2wpkh(b11->fallback, &pkh)) {
|
|
|
|
char out[73 + strlen(b11->chain->bip173_name)];
|
|
|
|
json_add_string(response, "type", "P2WPKH");
|
|
|
|
if (segwit_addr_encode(out, b11->chain->bip173_name, 0,
|
|
|
|
(const u8 *)&pkh, sizeof(pkh)))
|
|
|
|
json_add_string(response, "addr", out);
|
|
|
|
} else if (is_p2wsh(b11->fallback, &wsh)) {
|
|
|
|
char out[73 + strlen(b11->chain->bip173_name)];
|
|
|
|
json_add_string(response, "type", "P2WSH");
|
|
|
|
if (segwit_addr_encode(out, b11->chain->bip173_name, 0,
|
|
|
|
(const u8 *)&wsh, sizeof(wsh)))
|
|
|
|
json_add_string(response, "addr", out);
|
|
|
|
}
|
|
|
|
json_add_hex(response, "hex",
|
|
|
|
b11->fallback, tal_len(b11->fallback));
|
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tal_count(b11->routes)) {
|
|
|
|
size_t i, n;
|
|
|
|
|
|
|
|
json_array_start(response, "routes");
|
|
|
|
for (i = 0; i < tal_count(b11->routes); i++) {
|
|
|
|
json_array_start(response, NULL);
|
|
|
|
for (n = 0; n < tal_count(b11->routes[i]); n++) {
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_pubkey(response, "pubkey",
|
|
|
|
&b11->routes[i][n].pubkey);
|
|
|
|
json_add_short_channel_id(response,
|
|
|
|
"short_channel_id",
|
|
|
|
&b11->routes[i][n]
|
|
|
|
.short_channel_id);
|
2017-12-12 01:34:07 +01:00
|
|
|
json_add_u64(response, "fee_base_msat",
|
|
|
|
b11->routes[i][n].fee_base_msat);
|
|
|
|
json_add_u64(response, "fee_proportional_millionths",
|
|
|
|
b11->routes[i][n].fee_proportional_millionths);
|
2017-11-22 01:25:39 +01:00
|
|
|
json_add_num(response, "cltv_expiry_delta",
|
|
|
|
b11->routes[i][n]
|
|
|
|
.cltv_expiry_delta);
|
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
json_array_end(response);
|
|
|
|
}
|
|
|
|
json_array_end(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!list_empty(&b11->extra_fields)) {
|
|
|
|
struct bolt11_field *extra;
|
|
|
|
|
|
|
|
json_array_start(response, "extra");
|
|
|
|
list_for_each(&b11->extra_fields, extra, list) {
|
|
|
|
char *data = tal_arr(cmd, char, tal_len(extra->data)+1);
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < tal_len(extra->data); i++)
|
|
|
|
data[i] = bech32_charset[extra->data[i]];
|
|
|
|
data[i] = '\0';
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_string(response, "tag",
|
|
|
|
tal_fmt(data, "%c", extra->tag));
|
|
|
|
json_add_string(response, "data", data);
|
|
|
|
tal_free(data);
|
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
json_array_end(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
json_add_hex(response, "payment_hash",
|
|
|
|
&b11->payment_hash, sizeof(b11->payment_hash));
|
|
|
|
|
|
|
|
json_add_string(response, "signature",
|
|
|
|
type_to_string(cmd, secp256k1_ecdsa_signature,
|
|
|
|
&b11->sig));
|
|
|
|
json_object_end(response);
|
|
|
|
command_success(cmd, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command decodepay_command = {
|
|
|
|
"decodepay",
|
|
|
|
json_decodepay,
|
2018-01-13 12:21:33 +01:00
|
|
|
"Parse and decode {bolt11} if possible, using {description} if necessary",
|
2017-11-22 01:25:39 +01:00
|
|
|
"Returns a verbose description on success"
|
|
|
|
};
|
|
|
|
AUTODATA(json_command, &decodepay_command);
|