| 1641 | } |
| 1642 | |
| 1643 | static struct command_result *json_createinvoice(struct command *cmd, |
| 1644 | const char *buffer, |
| 1645 | const jsmntok_t *obj UNNEEDED, |
| 1646 | const jsmntok_t *params) |
| 1647 | { |
| 1648 | const char *invstring; |
| 1649 | struct json_escape *label; |
| 1650 | struct preimage *preimage; |
| 1651 | struct invoice invoice; |
| 1652 | struct sha256 payment_hash; |
| 1653 | struct json_stream *response; |
| 1654 | struct bolt11 *b11; |
| 1655 | struct sha256 hash; |
| 1656 | u5 *sig; |
| 1657 | bool have_n; |
| 1658 | char *fail; |
| 1659 | |
| 1660 | if (!param(cmd, buffer, params, |
| 1661 | p_req("invstring", param_string, &invstring), |
| 1662 | p_req("label", param_label, &label), |
| 1663 | p_req("preimage", param_preimage, &preimage), |
| 1664 | NULL)) |
| 1665 | return command_param_failed(); |
| 1666 | |
| 1667 | sha256(&payment_hash, preimage, sizeof(*preimage)); |
| 1668 | b11 = bolt11_decode_nosig(cmd, invstring, cmd->ld->our_features, |
| 1669 | NULL, chainparams, &hash, &sig, &have_n, |
| 1670 | &fail); |
| 1671 | if (b11) { |
| 1672 | /* This adds the signature */ |
| 1673 | char *b11enc = bolt11_encode(cmd, b11, have_n, |
| 1674 | hsm_sign_b11, cmd->ld); |
| 1675 | |
| 1676 | if (!b11->description) |
| 1677 | return command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
| 1678 | "Missing description in invoice"); |
| 1679 | |
| 1680 | if (!b11->expiry) |
| 1681 | return command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
| 1682 | "Missing expiry in invoice"); |
| 1683 | |
| 1684 | if (!sha256_eq(&payment_hash, &b11->payment_hash)) |
| 1685 | return command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
| 1686 | "Incorrect preimage"); |
| 1687 | |
| 1688 | if (!wallet_invoice_create(cmd->ld->wallet, |
| 1689 | &invoice, |
| 1690 | b11->msat, |
| 1691 | label, |
| 1692 | b11->expiry, |
| 1693 | b11enc, |
| 1694 | b11->description, |
| 1695 | b11->features, |
| 1696 | preimage, |
| 1697 | &payment_hash, |
| 1698 | NULL)) |
| 1699 | return fail_exists(cmd, label); |
| 1700 |
nothing calls this directly
no test coverage detected