1
0

lnaddr: fix outdated doc reference to lnworker._check_invoice

(and clean some imports/whitespace while we're at it)
This commit is contained in:
Sander van Grieken
2025-05-02 15:57:01 +02:00
parent d8551990e4
commit eda88b2972
2 changed files with 15 additions and 12 deletions

View File

@@ -7,7 +7,7 @@ import time
from hashlib import sha256
from binascii import hexlify
from decimal import Decimal
from typing import Optional, TYPE_CHECKING, Type, Dict, Any, Union, Sequence, List, Tuple
from typing import Optional, TYPE_CHECKING, Type, Dict, Any, Sequence, Tuple
import random
import electrum_ecc as ecc
@@ -535,7 +535,7 @@ def lndecode(invoice: str, *, verbose=False, net=None) -> LnAddr:
features = int_from_data5(tagdata)
addr.tags.append(('9', features))
# note: The features are not validated here in the parser,
# instead, validation is done just before we try paying the invoice (in lnworker._check_invoice).
# instead, validation is done just before we try paying the invoice (in lnworker._check_bolt11_invoice).
# Context: invoice parsing happens when opening a wallet. If there was a backwards-incompatible
# change to a feature, and we raised, some existing wallets could not be opened. Such a change
# can happen to features not-yet-merged-to-BOLTs (e.g. trampoline feature bit was moved and reused).
@@ -557,7 +557,7 @@ def lndecode(invoice: str, *, verbose=False, net=None) -> LnAddr:
# field specified below).
addr.signature = sigdecoded[:65]
hrp_hash = sha256(hrp.encode("ascii") + bytes(convertbits(data5, 5, 8, True))).digest()
if addr.pubkey: # Specified by `n`
if addr.pubkey: # Specified by `n`
# BOLT #11:
#
# A reader MUST use the `n` field to validate the signature instead of
@@ -565,8 +565,10 @@ def lndecode(invoice: str, *, verbose=False, net=None) -> LnAddr:
if not ecc.ECPubkey(addr.pubkey).ecdsa_verify(sigdecoded[:64], hrp_hash):
raise LnDecodeException("bad signature")
pubkey_copy = addr.pubkey
class WrappedBytesKey:
serialize = lambda: pubkey_copy
addr.pubkey = WrappedBytesKey
else: # Recover pubkey from signature.
addr.pubkey = SerializableKey(ecc.ECPubkey.from_ecdsa_sig64(sigdecoded[:64], sigdecoded[64], hrp_hash))

View File

@@ -1,6 +1,5 @@
from copy import deepcopy
from typing import Optional, Sequence, Tuple, List, Dict, TYPE_CHECKING, Set
import threading
from typing import Sequence, Tuple, Dict, TYPE_CHECKING, Set
from .lnutil import SENT, RECEIVED, LOCAL, REMOTE, HTLCOwner, UpdateAddHtlc, Direction, FeeUpdate
from .util import bfh, with_lock
@@ -11,7 +10,7 @@ if TYPE_CHECKING:
class HTLCManager:
def __init__(self, log:'StoredDict', *, initial_feerate=None):
def __init__(self, log: 'StoredDict', *, initial_feerate=None):
if len(log) == 0:
initial = {
@@ -490,8 +489,7 @@ class HTLCManager:
return d
@with_lock
def all_settled_htlcs_ever(self, subject: HTLCOwner, ctn: int = None) \
-> Sequence[Tuple[Direction, UpdateAddHtlc]]:
def all_settled_htlcs_ever(self, subject: HTLCOwner, ctn: int = None) -> Sequence[Tuple[Direction, UpdateAddHtlc]]:
"""Return the list of all HTLCs that have been ever settled in subject's
ctx up to ctn.
"""
@@ -527,14 +525,16 @@ class HTLCManager:
# sent htlcs
for htlc_id in considered_sent_htlc_ids:
ctns = self.log[whose]['settles'].get(htlc_id, None)
if ctns is None: continue
if ctns is None:
continue
if ctns[ctx_owner] is not None and ctns[ctx_owner] <= ctn:
htlc = self.log[whose]['adds'][htlc_id]
balance -= htlc.amount_msat
# recv htlcs
for htlc_id in considered_recv_htlc_ids:
ctns = self.log[-whose]['settles'].get(htlc_id, None)
if ctns is None: continue
if ctns is None:
continue
if ctns[ctx_owner] is not None and ctns[ctx_owner] <= ctn:
htlc = self.log[-whose]['adds'][htlc_id]
balance += htlc.amount_msat
@@ -551,7 +551,8 @@ class HTLCManager:
htlcs = []
for htlc_id in considered_htlc_ids:
ctns = self.log[htlc_proposer][log_action].get(htlc_id, None)
if ctns is None: continue
if ctns is None:
continue
if ctns[ctx_owner] == ctn:
htlcs.append(self.log[htlc_proposer]['adds'][htlc_id])
return htlcs
@@ -603,7 +604,7 @@ class HTLCManager:
right = len(fee_log)
while True:
i = (left + right) // 2
ctn_at_i = fee_log[i].ctn_local if subject==LOCAL else fee_log[i].ctn_remote
ctn_at_i = fee_log[i].ctn_local if subject == LOCAL else fee_log[i].ctn_remote
if right - left <= 1:
break
if ctn_at_i is None: # Nones can only be on the right end