From 41583dfe17cecc655e7dea239646f97a2b153fe8 Mon Sep 17 00:00:00 2001 From: Chris Guida Date: Mon, 31 Jul 2023 16:55:00 -0600 Subject: [PATCH] add autogenerated fallbacks (not custom fallbacks) to invoice_fallbacks table --- lightningd/invoice.c | 14 +++++++- lightningd/test/run-invoice-select-inchan.c | 5 +++ wallet/invoices.c | 36 +++++++++++++++++++++ wallet/invoices.h | 14 ++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) diff --git a/lightningd/invoice.c b/lightningd/invoice.c index 36dc3fbac40b..f7be95371c55 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -697,6 +697,7 @@ struct invoice_info { struct bolt11 *b11; struct json_escape *label; struct chanhints *chanhints; + bool custom_fallbacks; }; /* Add routehints based on listincoming results: NULL means success. */ @@ -881,6 +882,13 @@ invoice_complete(struct invoice_info *info, info->label->s); } + if (info->cmd->ld->unified_invoices && info->b11->fallbacks && !info->custom_fallbacks) { + for (size_t i = 0; i < tal_count(info->b11->fallbacks); i++) { + const u8 *fallback_script = info->b11->fallbacks[i]; + invoices_create_fallback(wallet->invoices, inv_dbid, fallback_script); + } + } + /* Get details */ details = invoices_get_details(info, wallet->invoices, inv_dbid); @@ -917,6 +925,10 @@ invoice_complete(struct invoice_info *info, json_add_string(response, "warning_private_unused", "Insufficient incoming capacity, once private channels were excluded (try exposeprivatechannels=true?)"); + if (info->cmd->ld->unified_invoices && info->custom_fallbacks) + json_add_string(response, "warning_custom_fallbacks", + "WARNING: Not tracking on-chain payments for custom fallback addresses"); + return command_success(info->cmd, response); } @@ -1169,6 +1181,7 @@ static struct command_result *json_invoice(struct command *cmd, } if (fallbacks) { + info->custom_fallbacks = true; if (cmd->ld->unified_invoices) { log_info(cmd->ld->log, "WARNING: Not tracking on-chain payments " @@ -1197,7 +1210,6 @@ static struct command_result *json_invoice(struct command *cmd, p2tr = scriptpubkey_p2tr(fallback_scripts, &pubkey); fallback_scripts[0] = p2tr; - } // fprintf(stderr, "what's in here?? %s\n", tal_hex(tmpctx, fallback_scripts[0])); diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 201855e8d2f9..ef55f9eaa6ce 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -365,6 +365,11 @@ bool invoices_create(struct invoices *invoices UNNEEDED, const struct sha256 *rhash UNNEEDED, const struct sha256 *local_offer_id UNNEEDED) { fprintf(stderr, "invoices_create called!\n"); abort(); } +/* Generated stub for invoices_create_fallback */ +bool invoices_create_fallback(struct invoices *invoices UNNEEDED, + u64 inv_dbid UNNEEDED, + const u8 *scriptPubkey UNNEEDED) +{ fprintf(stderr, "invoices_create_fallback called!\n"); abort(); } /* Generated stub for invoices_delete */ bool invoices_delete(struct invoices *invoices UNNEEDED, u64 inv_dbid UNNEEDED, diff --git a/wallet/invoices.c b/wallet/invoices.c index ba45086f5a4d..48044100921e 100644 --- a/wallet/invoices.c +++ b/wallet/invoices.c @@ -375,6 +375,42 @@ bool invoices_find_by_rhash(struct invoices *invoices, } } +bool invoices_create_fallback(struct invoices *invoices, + u64 inv_dbid, + const u8 *scriptPubkey) +{ + struct db_stmt *stmt; + + // *inv_dbid = invoice_index_created(invoices->wallet->ld, UNPAID, label, b11enc); + + /* Save to database. */ + stmt = db_prepare_v2( + invoices->wallet->db, + SQL("INSERT INTO invoice_fallbacks" + " ( invoice_id, scriptpubkey )" + " VALUES ( ?, ?);")); + + fprintf(stderr, "CAG invoices_find_by_fallback_script\n"); + + db_bind_u64(stmt, inv_dbid); + db_bind_blob(stmt, scriptPubkey, + tal_bytelen(scriptPubkey)); + // db_query_prepared(stmt); + + db_exec_prepared_v2(stmt); + tal_free(stmt); + + return true; + // if (!db_step(stmt)) { + // tal_free(stmt); + // return false; + // } else { + // *inv_dbid = db_col_u64(stmt, "invoice_id"); + // tal_free(stmt); + // return true; + // } +} + bool invoices_find_by_fallback_script(struct invoices *invoices, u64 *inv_dbid, const u8 *scriptPubkey) diff --git a/wallet/invoices.h b/wallet/invoices.h index eb6f357f1d68..f7ac37ca3867 100644 --- a/wallet/invoices.h +++ b/wallet/invoices.h @@ -82,6 +82,20 @@ bool invoices_find_by_label(struct invoices *invoices, bool invoices_find_by_rhash(struct invoices *invoices, u64 *inv_dbid, const struct sha256 *rhash); +/** + * invoices_create_fallback - Search for an invoice by + * scriptpubkey in invoice_fallbacks child table + * + * @param invoices - the invoice handler. + * @param inv_dbid - invoice id + * @param scriptPubKey - the fallback scriptpubkey associated with + * the above invoice id + * + * Returns true if creation was successful. + */ +bool invoices_create_fallback(struct invoices *invoices, + u64 inv_dbid, + const u8 *scriptPubkey); /** * invoices_find_by_fallback_script - Search for an invoice by * scriptpubkey in invoice_fallbacks child table