Browse Source

lnaddr: fix decoding of min_final_cltv_expiry

Previously we failed to decode min_final_cltv_expiry properly if the highest bit was 1:
in practice, we could not pay invoices that had a value in [16-31] or [512-1023].
Many invoices use a value around 144, so this was simply unnoticed.

also update default value to follow BOLT change:
c5693d336d
patch-4
SomberNight 4 years ago
parent
commit
18066c72a0
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 10
      electrum/lnaddr.py
  2. 9
      electrum/tests/test_bolt11.py

10
electrum/lnaddr.py

@ -273,7 +273,7 @@ class LnAddr(object):
self.pubkey = None
self.currency = constants.net.SEGWIT_HRP if currency is None else currency
self.amount = amount # type: Optional[Decimal] # in bitcoins
self._min_final_cltv_expiry = 9
self._min_final_cltv_expiry = 18
def get_amount_sat(self) -> Optional[Decimal]:
# note that this has msat resolution potentially
@ -393,9 +393,9 @@ def lndecode(invoice: str, *, verbose=False, expected_hrp=None) -> LnAddr:
while s.pos + 264 + 64 + 32 + 32 + 16 < s.len:
route.append((s.read(264).tobytes(),
s.read(64).tobytes(),
s.read(32).intbe,
s.read(32).intbe,
s.read(16).intbe))
s.read(32).uintbe,
s.read(32).uintbe,
s.read(16).uintbe))
addr.tags.append(('r',route))
elif tag == 'f':
fallback = parse_fallback(tagdata, addr.currency)
@ -438,7 +438,7 @@ def lndecode(invoice: str, *, verbose=False, expected_hrp=None) -> LnAddr:
addr.pubkey = pubkeybytes
elif tag == 'c':
addr._min_final_cltv_expiry = tagdata.int
addr._min_final_cltv_expiry = tagdata.uint
elif tag == '9':
features = tagdata.uint

9
electrum/tests/test_bolt11.py

@ -115,10 +115,15 @@ class TestBolt11(ElectrumTestCase):
expected_hrp="sb")
self.assertEqual(144, lnaddr.get_min_final_cltv_expiry())
lnaddr = lndecode("lntb15u1p0m6lzupp5zqjthgvaad9mewmdjuehwddyze9d8zyxcc43zhaddeegt37sndgsdq4xysyymr0vd4kzcmrd9hx7cqp7xqrrss9qy9qsqsp5vlhcs24hwm747w8f3uau2tlrdkvjaglffnsstwyamj84cxuhrn2s8tut3jqumepu42azyyjpgqa4w9w03204zp9h4clk499y2umstl6s29hqyj8vv4as6zt5567ux7l3f66m8pjhk65zjaq2esezk7ll2kcpljewkg",
expected_hrp="tb")
self.assertEqual(30, lnaddr.get_min_final_cltv_expiry())
def test_min_final_cltv_expiry_roundtrip(self):
lnaddr = LnAddr(paymenthash=RHASH, amount=Decimal('0.001'), tags=[('d', '1 cup coffee'), ('x', 60), ('c', 150)])
for cltv in (1, 15, 16, 31, 32, 33, 150, 511, 512, 513, 1023, 1024, 1025):
lnaddr = LnAddr(paymenthash=RHASH, amount=Decimal('0.001'), tags=[('d', '1 cup coffee'), ('x', 60), ('c', cltv)])
invoice = lnencode(lnaddr, PRIVKEY)
self.assertEqual(150, lndecode(invoice).get_min_final_cltv_expiry())
self.assertEqual(cltv, lndecode(invoice).get_min_final_cltv_expiry())
def test_features(self):
lnaddr = lndecode("lnbc25m1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdq5vdhkven9v5sxyetpdees9qzsze992adudgku8p05pstl6zh7av6rx2f297pv89gu5q93a0hf3g7lynl3xq56t23dpvah6u7y9qey9lccrdml3gaqwc6nxsl5ktzm464sq73t7cl")

Loading…
Cancel
Save