From 7d463b2ce4a86d57cd9a7cae128cc2dc73a34af4 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 3 Feb 2020 10:59:42 +1030 Subject: [PATCH] common: add check that pico-valued invoices are round numbers. Otherwise you can ask for a sub-millisatoshi amount, which is dumb and violates the spec. See-also: https://github.com/lightningnetwork/lightning-rfc/pull/736 Signed-off-by: Rusty Russell --- common/bolt11.c | 11 +++++++++++ common/test/run-bolt11.c | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/common/bolt11.c b/common/bolt11.c index d0c84315cf4a..1874d50896a2 100644 --- a/common/bolt11.c +++ b/common/bolt11.c @@ -649,6 +649,17 @@ struct bolt11 *bolt11_decode(const tal_t *ctx, const char *str, * amount required for payment. */ b11->msat = tal(b11, struct amount_msat); + /* BOLT-50143e388e16a449a92ed574fc16eb35b51426b9 #11: + * + * - if multiplier is `p` and the last decimal of `amount` is + * not 0: + * - MUST fail the payment. + */ + if (amount * m10 % 10 != 0) + return decode_fail(b11, fail, + "Invalid sub-millisatoshi amount" + " '%sp'", amountstr); + b11->msat->millisatoshis = amount * m10 / 10; /* Raw: raw amount multiplier calculation */ } diff --git a/common/test/run-bolt11.c b/common/test/run-bolt11.c index 02a33650920c..26027da14518 100644 --- a/common/test/run-bolt11.c +++ b/common/test/run-bolt11.c @@ -562,6 +562,13 @@ int main(void) assert(!bolt11_decode(tmpctx, "lnbc2500x1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdq5xysxxatsyp3k7enxv4jsxqzpujr6jxr9gq9pv6g46y7d20jfkegkg4gljz2ea2a3m9lmvvr95tq2s0kvu70u3axgelz3kyvtp2ywwt0y8hkx2869zq5dll9nelr83zzqqpgl2zg", NULL, &fail)); assert(streq(fail, "Invalid amount postfix 'x'")); + /* BOLT- #11: + * > ### Invalid sub-millisatoshi precision. + * > lnbc2500000001p1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdq5xysxxatsyp3k7enxv4jsxqzpu7hqtk93pkf7sw55rdv4k9z2vj050rxdr6za9ekfs3nlt5lr89jqpdmxsmlj9urqumg0h9wzpqecw7th56tdms40p2ny9q4ddvjsedzcplva53s + */ + assert(!bolt11_decode(tmpctx, "lnbc2500000001p1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdq5xysxxatsyp3k7enxv4jsxqzpu7hqtk93pkf7sw55rdv4k9z2vj050rxdr6za9ekfs3nlt5lr89jqpdmxsmlj9urqumg0h9wzpqecw7th56tdms40p2ny9q4ddvjsedzcplva53s", NULL, &fail)); + assert(streq(fail, "Invalid sub-millisatoshi amount '2500000001p'")); + /* FIXME: Test the others! */ wally_cleanup(0); tal_free(tmpctx);