From 81c89aaef8f515d07d00ac2626c4c4d619fce643 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 23 Nov 2019 10:49:23 +1030 Subject: [PATCH] wallet: add invoice features into db. In a future version, we will use features to insist that payers provide the secret. In transition, we may have old invoices which didn't insist on that, so we need to know this on a per-invoice basis. Not sure if I got the right syntax for adding an empty blob though! Signed-off-by: Rusty Russell --- lightningd/invoice.c | 1 + lightningd/test/run-invoice-select-inchan.c | 1 + wallet/db.c | 1 + wallet/invoices.c | 11 +++++++++-- wallet/invoices.h | 1 + wallet/test/run-wallet.c | 1 + wallet/wallet.c | 3 ++- wallet/wallet.h | 3 +++ 8 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lightningd/invoice.c b/lightningd/invoice.c index 840be27bb..cf1bfb4c8 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -502,6 +502,7 @@ static void gossipd_incoming_channels_reply(struct subd *gossipd, info->b11->expiry, b11enc, info->b11->description, + info->b11->features, &info->payment_preimage, &info->b11->payment_hash)) { was_pending(command_fail(info->cmd, INVOICE_LABEL_ALREADY_EXISTS, diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 2beecd502..bf8fe52dc 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -510,6 +510,7 @@ bool wallet_invoice_create(struct wallet *wallet UNNEEDED, u64 expiry UNNEEDED, const char *b11enc UNNEEDED, const char *description UNNEEDED, + const u8 *features UNNEEDED, const struct preimage *r UNNEEDED, const struct sha256 *rhash UNNEEDED) { fprintf(stderr, "wallet_invoice_create called!\n"); abort(); } diff --git a/wallet/db.c b/wallet/db.c index 318be806f..0a3d4559c 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -480,6 +480,7 @@ static struct migration dbmigrations[] = { /* See https://github.com/ElementsProject/lightning/issues/3189 */ {SQL("UPDATE forwarded_payments SET received_time=0 WHERE received_time IS NULL;"), NULL}, + {SQL("ALTER TABLE invoices ADD COLUMN features BLOB DEFAULT '';"), NULL}, }; /* Leak tracking. */ diff --git a/wallet/invoices.c b/wallet/invoices.c index 41f7dada0..cf8832f54 100644 --- a/wallet/invoices.c +++ b/wallet/invoices.c @@ -116,6 +116,9 @@ static struct invoice_details *wallet_stmt2invoice_details(const tal_t *ctx, else dtl->description = NULL; + dtl->features = tal_dup_arr(dtl, u8, + db_column_blob(stmt, 11), + db_column_bytes(stmt, 11), 0); return dtl; } @@ -253,6 +256,7 @@ bool invoices_create(struct invoices *invoices, u64 expiry, const char *b11enc, const char *description, + const u8 *features, const struct preimage *r, const struct sha256 *rhash) { @@ -279,11 +283,11 @@ bool invoices_create(struct invoices *invoices, " ( payment_hash, payment_key, state" " , msatoshi, label, expiry_time" " , pay_index, msatoshi_received" - " , paid_timestamp, bolt11, description)" + " , paid_timestamp, bolt11, description, features)" " VALUES ( ?, ?, ?" " , ?, ?, ?" " , NULL, NULL" - " , NULL, ?, ?);")); + " , NULL, ?, ?, ?);")); db_bind_sha256(stmt, 0, rhash); db_bind_preimage(stmt, 1, r); @@ -296,6 +300,7 @@ bool invoices_create(struct invoices *invoices, db_bind_u64(stmt, 5, expiry_time); db_bind_text(stmt, 6, b11enc); db_bind_text(stmt, 7, description); + db_bind_blob(stmt, 8, features, tal_bytelen(features)); db_exec_prepared_v2(stmt); @@ -435,6 +440,7 @@ bool invoices_iterate(struct invoices *invoices, ", paid_timestamp" ", bolt11" ", description" + ", features" " FROM invoices;")); db_query_prepared(stmt); it->p = stmt; @@ -622,6 +628,7 @@ const struct invoice_details *invoices_get_details(const tal_t *ctx, ", paid_timestamp" ", bolt11" ", description" + ", features" " FROM invoices" " WHERE id = ?;")); db_bind_u64(stmt, 0, invoice.id); diff --git a/wallet/invoices.h b/wallet/invoices.h index bb96b82f4..6577cffdb 100644 --- a/wallet/invoices.h +++ b/wallet/invoices.h @@ -50,6 +50,7 @@ bool invoices_create(struct invoices *invoices, u64 expiry, const char *b11enc, const char *description, + const u8 *features, const struct preimage *r, const struct sha256 *rhash); diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index db41d83ba..6b1c751fe 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -138,6 +138,7 @@ bool invoices_create(struct invoices *invoices UNNEEDED, u64 expiry UNNEEDED, const char *b11enc UNNEEDED, const char *description UNNEEDED, + const u8 *features UNNEEDED, const struct preimage *r UNNEEDED, const struct sha256 *rhash UNNEEDED) { fprintf(stderr, "invoices_create called!\n"); abort(); } diff --git a/wallet/wallet.c b/wallet/wallet.c index edeb151fd..7edd2ddc2 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1931,10 +1931,11 @@ bool wallet_invoice_create(struct wallet *wallet, u64 expiry, const char *b11enc, const char *description, + const u8 *features, const struct preimage *r, const struct sha256 *rhash) { - return invoices_create(wallet->invoices, pinvoice, msat, label, expiry, b11enc, description, r, rhash); + return invoices_create(wallet->invoices, pinvoice, msat, label, expiry, b11enc, description, features, r, rhash); } bool wallet_invoice_find_by_label(struct wallet *wallet, struct invoice *pinvoice, diff --git a/wallet/wallet.h b/wallet/wallet.h index 82aba77b7..dafb1085b 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -673,6 +673,8 @@ struct invoice_details { /* The description of the payment. */ char *description; + /* The features, if any (tal_arr) */ + u8 *features; }; /* An object that handles iteration over the set of invoices */ @@ -713,6 +715,7 @@ bool wallet_invoice_create(struct wallet *wallet, u64 expiry, const char *b11enc, const char *description, + const u8 *features, const struct preimage *r, const struct sha256 *rhash);