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

Function json_createinvoice

lightningd/invoice.c:1672–1855  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1670}
1671
1672static struct command_result *json_createinvoice(struct command *cmd,
1673 const char *buffer,
1674 const jsmntok_t *obj UNNEEDED,
1675 const jsmntok_t *params)
1676{
1677 const char *invstring;
1678 struct json_escape *label;
1679 struct preimage *preimage;
1680 u64 inv_dbid;
1681 struct sha256 payment_hash;
1682 struct json_stream *response;
1683 struct bolt11 *b11;
1684 struct sha256 hash;
1685 const u5 *sig;
1686 bool have_n;
1687 const char *fail;
1688
1689 if (!param_check(cmd, buffer, params,
1690 p_req("invstring", param_invstring, &invstring),
1691 p_req("label", param_label, &label),
1692 p_req("preimage", param_preimage, &preimage),
1693 NULL))
1694 return command_param_failed();
1695
1696 sha256(&payment_hash, preimage, sizeof(*preimage));
1697
1698 /* invoices_create() only catches duplicate labels (reported via
1699 * fail_exists() below), but payment_hash is UNIQUE in the db too. */
1700 if (!invoices_find_by_label(cmd->ld->wallet->invoices, &inv_dbid, label)
1701 && invoices_find_by_rhash(cmd->ld->wallet->invoices,
1702 &inv_dbid, &payment_hash))
1703 return command_fail(cmd, INVOICE_PREIMAGE_ALREADY_EXISTS,
1704 "preimage already used");
1705
1706 b11 = bolt11_decode_nosig(cmd, invstring, cmd->ld->our_features,
1707 NULL, chainparams, &hash, &sig, &have_n,
1708 &fail);
1709 if (b11) {
1710 /* Same bound as the invoice RPC: past 60 bits
1711 * bolt11_encode() below aborts, and the expiry timer
1712 * overflows its u64 nanosecond grain far below that. */
1713 if (b11->expiry >= (u64)1 << 32)
1714 return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
1715 "expiry must be below 2^32 seconds"
1716 " (~136 years)");
1717
1718 /* This adds the signature */
1719 char *b11enc = bolt11_encode(cmd, b11, have_n,
1720 hsm_sign_b11, cmd->ld);
1721
1722 if (!b11->description)
1723 return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
1724 "Missing description in invoice");
1725
1726 if (!b11->expiry)
1727 return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
1728 "Missing expiry in invoice");
1729

Callers

nothing calls this directly

Calls 15

param_checkFunction · 0.85
bolt11_decode_nosigFunction · 0.85
fail_existsFunction · 0.85
invoice_decode_minimalFunction · 0.85
add_stub_blindedpathFunction · 0.85
hsm_sign_b12_invoiceFunction · 0.85
invoice_encodeFunction · 0.85
invoice_offer_idFunction · 0.85
offer_status_activeFunction · 0.85
tal_bytelenFunction · 0.85
command_param_failedFunction · 0.70
sha256Class · 0.70

Tested by

no test coverage detected