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 hashlib import sha256
from binascii import hexlify from binascii import hexlify
from decimal import Decimal 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 random
import electrum_ecc as ecc import electrum_ecc as ecc
@@ -535,7 +535,7 @@ def lndecode(invoice: str, *, verbose=False, net=None) -> LnAddr:
features = int_from_data5(tagdata) features = int_from_data5(tagdata)
addr.tags.append(('9', features)) addr.tags.append(('9', features))
# note: The features are not validated here in the parser, # 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 # 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 # 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). # 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). # field specified below).
addr.signature = sigdecoded[:65] addr.signature = sigdecoded[:65]
hrp_hash = sha256(hrp.encode("ascii") + bytes(convertbits(data5, 5, 8, True))).digest() 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: # BOLT #11:
# #
# A reader MUST use the `n` field to validate the signature instead of # 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): if not ecc.ECPubkey(addr.pubkey).ecdsa_verify(sigdecoded[:64], hrp_hash):
raise LnDecodeException("bad signature") raise LnDecodeException("bad signature")
pubkey_copy = addr.pubkey pubkey_copy = addr.pubkey
class WrappedBytesKey: class WrappedBytesKey:
serialize = lambda: pubkey_copy serialize = lambda: pubkey_copy
addr.pubkey = WrappedBytesKey addr.pubkey = WrappedBytesKey
else: # Recover pubkey from signature. else: # Recover pubkey from signature.
addr.pubkey = SerializableKey(ecc.ECPubkey.from_ecdsa_sig64(sigdecoded[:64], sigdecoded[64], hrp_hash)) 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 copy import deepcopy
from typing import Optional, Sequence, Tuple, List, Dict, TYPE_CHECKING, Set from typing import Sequence, Tuple, Dict, TYPE_CHECKING, Set
import threading
from .lnutil import SENT, RECEIVED, LOCAL, REMOTE, HTLCOwner, UpdateAddHtlc, Direction, FeeUpdate from .lnutil import SENT, RECEIVED, LOCAL, REMOTE, HTLCOwner, UpdateAddHtlc, Direction, FeeUpdate
from .util import bfh, with_lock from .util import bfh, with_lock
@@ -11,7 +10,7 @@ if TYPE_CHECKING:
class HTLCManager: class HTLCManager:
def __init__(self, log:'StoredDict', *, initial_feerate=None): def __init__(self, log: 'StoredDict', *, initial_feerate=None):
if len(log) == 0: if len(log) == 0:
initial = { initial = {
@@ -490,8 +489,7 @@ class HTLCManager:
return d return d
@with_lock @with_lock
def all_settled_htlcs_ever(self, subject: HTLCOwner, ctn: int = None) \ def all_settled_htlcs_ever(self, subject: HTLCOwner, ctn: int = None) -> Sequence[Tuple[Direction, UpdateAddHtlc]]:
-> Sequence[Tuple[Direction, UpdateAddHtlc]]:
"""Return the list of all HTLCs that have been ever settled in subject's """Return the list of all HTLCs that have been ever settled in subject's
ctx up to ctn. ctx up to ctn.
""" """
@@ -527,14 +525,16 @@ class HTLCManager:
# sent htlcs # sent htlcs
for htlc_id in considered_sent_htlc_ids: for htlc_id in considered_sent_htlc_ids:
ctns = self.log[whose]['settles'].get(htlc_id, None) 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: if ctns[ctx_owner] is not None and ctns[ctx_owner] <= ctn:
htlc = self.log[whose]['adds'][htlc_id] htlc = self.log[whose]['adds'][htlc_id]
balance -= htlc.amount_msat balance -= htlc.amount_msat
# recv htlcs # recv htlcs
for htlc_id in considered_recv_htlc_ids: for htlc_id in considered_recv_htlc_ids:
ctns = self.log[-whose]['settles'].get(htlc_id, None) 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: if ctns[ctx_owner] is not None and ctns[ctx_owner] <= ctn:
htlc = self.log[-whose]['adds'][htlc_id] htlc = self.log[-whose]['adds'][htlc_id]
balance += htlc.amount_msat balance += htlc.amount_msat
@@ -551,7 +551,8 @@ class HTLCManager:
htlcs = [] htlcs = []
for htlc_id in considered_htlc_ids: for htlc_id in considered_htlc_ids:
ctns = self.log[htlc_proposer][log_action].get(htlc_id, None) 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: if ctns[ctx_owner] == ctn:
htlcs.append(self.log[htlc_proposer]['adds'][htlc_id]) htlcs.append(self.log[htlc_proposer]['adds'][htlc_id])
return htlcs return htlcs
@@ -603,7 +604,7 @@ class HTLCManager:
right = len(fee_log) right = len(fee_log)
while True: while True:
i = (left + right) // 2 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: if right - left <= 1:
break break
if ctn_at_i is None: # Nones can only be on the right end if ctn_at_i is None: # Nones can only be on the right end