Browse Source
bolt11: fix amount encoding for large values
patch-4
bitromortac
4 years ago
No known key found for this signature in database
GPG Key ID: 1965063FC13BEBE2
2 changed files with
5 additions and
2 deletions
-
electrum/lnaddr.py
-
electrum/tests/test_bolt11.py
|
|
@ -32,12 +32,14 @@ def shorten_amount(amount): |
|
|
|
""" |
|
|
|
# Convert to pico initially |
|
|
|
amount = int(amount * 10**12) |
|
|
|
units = ['p', 'n', 'u', 'm', ''] |
|
|
|
units = ['p', 'n', 'u', 'm'] |
|
|
|
for unit in units: |
|
|
|
if amount % 1000 == 0: |
|
|
|
amount //= 1000 |
|
|
|
else: |
|
|
|
break |
|
|
|
else: |
|
|
|
unit = '' |
|
|
|
return str(amount) + unit |
|
|
|
|
|
|
|
def unshorten_amount(amount) -> Decimal: |
|
|
|
|
|
@ -28,10 +28,11 @@ class TestBolt11(ElectrumTestCase): |
|
|
|
Decimal(123)/10**6: '123u', |
|
|
|
Decimal(123)/1000: '123m', |
|
|
|
Decimal(3): '3', |
|
|
|
Decimal(1000): '1000', |
|
|
|
} |
|
|
|
|
|
|
|
for i, o in tests.items(): |
|
|
|
assert shorten_amount(i) == o |
|
|
|
self.assertEqual(shorten_amount(i), o) |
|
|
|
assert unshorten_amount(shorten_amount(i)) == i |
|
|
|
|
|
|
|
@staticmethod |
|
|
|