Skip to content

Commit

Permalink
add autogenerated fallbacks (not custom fallbacks) to invoice_fallbac…
Browse files Browse the repository at this point in the history
…ks table
  • Loading branch information
chrisguida committed Jul 31, 2023
1 parent c75443f commit 41583df
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lightningd/invoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -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]));

Expand Down
5 changes: 5 additions & 0 deletions lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 36 additions & 0 deletions wallet/invoices.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions wallet/invoices.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 41583df

Please sign in to comment.