tencapsulate get_invoice in lnworker - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 02c39a950f5f547ed9b294f81e3f064d4cd7f319 DIR parent 095de2dd2260aa4e7edbf38ca138b0d5513f1917 HTML Author: ThomasV <thomasv@electrum.org> Date: Thu, 18 Oct 2018 13:17:29 +0200 encapsulate get_invoice in lnworker Diffstat: M electrum/lnbase.py | 13 +++---------- M electrum/lnworker.py | 8 ++++++++ 2 files changed, 11 insertions(+), 10 deletions(-) --- DIR diff --git a/electrum/lnbase.py b/electrum/lnbase.py t@@ -214,7 +214,6 @@ class Peer(PrintError): if request_initial_sync: self.localfeatures |= LnLocalFeatures.INITIAL_ROUTING_SYNC self.localfeatures |= LnLocalFeatures.OPTION_DATA_LOSS_PROTECT_OPT - self.invoices = lnworker.invoices self.attempted_route = {} def send_message(self, message_name, **kwargs): t@@ -996,16 +995,11 @@ class Peer(PrintError): @log_exceptions async def on_update_add_htlc(self, payload): # no onion routing for the moment: we assume we are the end node - self.print_error('on_update_add_htlc', payload) + self.print_error('on_update_add_htlc') # check if this in our list of requests payment_hash = payload["payment_hash"] - for k in self.invoices.keys(): - preimage = bfh(k) - if sha256(preimage) == payment_hash: - break - else: - raise Exception('unknown payment hash') - request = lndecode(self.invoices[k], expected_hrp=constants.net.SEGWIT_HRP) + preimage, invoice = self.lnworker.get_invoice(payment_hash) + expected_received_msat = int(invoice.amount * bitcoin.COIN * 1000) channel_id = payload['channel_id'] htlc_id = int.from_bytes(payload["id"], 'big') cltv_expiry = int.from_bytes(payload["cltv_expiry"], 'big') t@@ -1014,7 +1008,6 @@ class Peer(PrintError): assert htlc_id == chan.config[REMOTE].next_htlc_id, (htlc_id, chan.config[REMOTE].next_htlc_id) assert chan.get_state() == "OPEN" # TODO verify sanity of their cltv expiry - expected_received_msat = int(request.amount * bitcoin.COIN * 1000) assert amount_msat == expected_received_msat htlc = {'amount_msat': amount_msat, 'payment_hash':payment_hash, 'cltv_expiry':cltv_expiry} chan.receive_htlc(htlc) DIR diff --git a/electrum/lnworker.py b/electrum/lnworker.py t@@ -325,6 +325,14 @@ class LNWorker(PrintError): self.wallet.storage.write() return pay_req + def get_invoice(self, payment_hash): + for k in self.invoices.keys(): + preimage = bfh(k) + if sha256(preimage) == payment_hash: + return preimage, lndecode(self.invoices[k], expected_hrp=constants.net.SEGWIT_HRP) + else: + raise Exception('unknown payment hash') + def _calc_routing_hints_for_invoice(self, amount_sat): """calculate routing hints (BOLT-11 'r' field)""" routing_hints = []