|
| 1 | +#define template c_template // avoid C++ keyword conflict just during includes |
| 2 | + |
| 3 | +extern "C" { |
| 4 | + #include "common/bolt11.h" |
| 5 | + #include "bitcoin/pubkey.h" |
| 6 | + #include "common/node_id.h" |
| 7 | + #include "common/utils.h" |
| 8 | + #include <bitcoin/chainparams.h> |
| 9 | + #include <ccan/tal/tal.h> |
| 10 | +} |
| 11 | + |
| 12 | +#undef template |
| 13 | + |
| 14 | +#include <string> |
| 15 | +#include <sstream> |
| 16 | +#include <iomanip> |
| 17 | +#include <cstring> |
| 18 | +#include <vector> |
| 19 | +#include <memory> |
| 20 | +#include <iostream> |
| 21 | +#include <iostream> |
| 22 | +#include <span> |
| 23 | +#include "module.h" |
| 24 | + |
| 25 | +struct TalFree { |
| 26 | + void operator()(void* ptr) const { tal_free(ptr); } |
| 27 | +}; |
| 28 | + |
| 29 | +std::string hex_encode(const unsigned char* data, size_t len) { |
| 30 | + std::ostringstream oss; |
| 31 | + oss << std::hex << std::setfill('0'); |
| 32 | + for (size_t i = 0; i < len; ++i) { |
| 33 | + oss << std::setw(2) << static_cast<int>(data[i]); |
| 34 | + } |
| 35 | + return oss.str(); |
| 36 | +} |
| 37 | + |
| 38 | +std::string clightning_des_invoice(const std::string& input) { |
| 39 | + char* fail = nullptr; |
| 40 | + const struct chainparams* params = chainparams_for_network("bitcoin"); |
| 41 | + |
| 42 | + std::unique_ptr<bolt11, TalFree> invoice( |
| 43 | + bolt11_decode(nullptr, input.c_str(), nullptr, nullptr, params, &fail) |
| 44 | + ); |
| 45 | + |
| 46 | + if (!invoice) { |
| 47 | + tal_free(fail); |
| 48 | + return ""; |
| 49 | + } |
| 50 | + |
| 51 | + std::ostringstream result; |
| 52 | + result << "HASH=" << hex_encode(invoice->payment_hash.u.u8, 32) << ";"; |
| 53 | + |
| 54 | + result << "AMOUNT="; |
| 55 | + if (invoice->msat) { |
| 56 | + result << invoice->msat->millisatoshis; |
| 57 | + } else { |
| 58 | + result << "0"; |
| 59 | + } |
| 60 | + result << ";"; |
| 61 | + |
| 62 | + result << "DESCRIPTION="; |
| 63 | + if (invoice->description) { |
| 64 | + result << invoice->description; |
| 65 | + } |
| 66 | + result << ";"; |
| 67 | + |
| 68 | + struct pubkey key; |
| 69 | + assert(pubkey_from_node_id(&key, &invoice->receiver_id)); |
| 70 | + |
| 71 | + uint8_t compressed[33]; |
| 72 | + pubkey_to_der(compressed, &key); |
| 73 | + result << "RECIPIENT=" << hex_encode(compressed, 33) << ";"; |
| 74 | + |
| 75 | + result << "EXPIRY=" << invoice->expiry << ";"; |
| 76 | + result << "TIMESTAMP=" << invoice->timestamp << ";"; |
| 77 | + result << "ROUTING_HINTS=" << tal_count(invoice->routes) << ";"; |
| 78 | + result << "MIN_CLTV=" << invoice->min_final_cltv_expiry; |
| 79 | + |
| 80 | + return result.str(); |
| 81 | +} |
| 82 | + |
| 83 | +namespace bitcoinfuzz |
| 84 | +{ |
| 85 | + namespace module |
| 86 | + { |
| 87 | + CLightning::CLightning(void) : BaseModule("CLightning") {} |
| 88 | + |
| 89 | + std::optional<std::string> CLightning::deserialize_invoice(std::string str) const |
| 90 | + { |
| 91 | + return clightning_des_invoice(str.c_str()); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments