Piggy: fix instant payment notifications in NWC timestamp, set correct static receive QR code

This commit is contained in:
Thomas Farstrike
2025-05-31 09:02:38 +02:00
parent 18d3d0677e
commit c7d50e0474
2 changed files with 25 additions and 20 deletions
@@ -193,6 +193,12 @@ def settings_button_tap(event):
wallet.stop()
mpos.ui.load_screen(settings_screen)
def main_ui_set_defaults():
global balance_label, payments_label, receive_qr
balance_label.set_text(lv.SYMBOL.REFRESH)
payments_label.set_text(lv.SYMBOL.REFRESH)
receive_qr.update("", len(""))
def build_main_ui():
global main_screen, balance_label, payments_label, receive_qr
main_screen = lv.obj()
@@ -200,7 +206,6 @@ def build_main_ui():
balance_label = lv.label(main_screen)
balance_label.align(lv.ALIGN.TOP_LEFT, 0, 0)
balance_label.set_style_text_font(lv.font_montserrat_22, 0)
balance_label.set_text(lv.SYMBOL.REFRESH)
receive_qr = lv.qrcode(main_screen)
receive_qr.set_size(50)
receive_qr.set_dark_color(lv.color_black())
@@ -219,7 +224,6 @@ def build_main_ui():
payments_label = lv.label(main_screen)
payments_label.align_to(balance_line,lv.ALIGN.OUT_BOTTOM_LEFT,0,10)
payments_label.set_style_text_font(lv.font_montserrat_16, 0)
payments_label.set_text(lv.SYMBOL.REFRESH)
settings_button = lv.button(main_screen)
settings_button.align(lv.ALIGN.BOTTOM_RIGHT, 0, 0)
snap_label = lv.label(settings_button)
@@ -240,27 +244,29 @@ def redraw_payments_cb():
def janitor_cb(timer):
global wallet, config
if lv.screen_active() == main_screen and (not wallet or not wallet.is_running()):
build_main_ui()
# just started the app or just returned from settings_screen
if lv.screen_active() == main_screen and (not wallet or not wallet.is_running()): # just started the app or just returned from settings_screen
main_ui_set_defaults()
config = mpos.config.SharedPreferences("com.lightningpiggy.displaywallet")
static_receive_code = config.get_string("static_receive_code")
if static_receive_code:
receive_qr.update(static_receive_code, len(static_receive_code))
wallet_type = config.get_string("wallet_type")
if wallet_type == "lnbits":
try:
static_receive_code = config.get_string("static_receive_code")
wallet = LNBitsWallet(config.get_string("lnbits_url"), config.get_string("lnbits_readkey"))
except Exception as e:
print(f"Couldn't initialize LNBitsWallet because: {e}")
elif wallet_type == "nwc":
try:
wallet = NWCWallet(config.get_string("nwc_url"))
static_receive_code = wallet.lud16
except Exception as e:
print(f"Couldn't initialize NWCWallet because: {e}")
else:
print(f"No or unsupported wallet type configured: '{wallet_type}'")
if static_receive_code:
print(f"Setting static_receive_code: {static_receive_code}")
receive_qr.update(static_receive_code, len(static_receive_code))
if wallet:
print("Starting wallet...")
wallet.start(redraw_balance_cb, redraw_payments_cb)
else:
print("ERROR: could not start any wallet!") # maybe call the error callback to show the error to the user
@@ -24,7 +24,7 @@ class UniqueSortedList:
self._items = []
def add(self, item):
print(f"before add: {str(self)}")
#print(f"before add: {str(self)}")
# Check if item already exists (using __eq__)
if item not in self._items:
# Insert item in sorted position for descending order (using __gt__)
@@ -34,7 +34,7 @@ class UniqueSortedList:
return
# If item is smaller than all existing items, append it
self._items.append(item)
print(f"after add: {str(self)}")
#print(f"after add: {str(self)}")
def __iter__(self):
# Return iterator for the internal list
@@ -71,13 +71,14 @@ class Payment:
sattext = "sats"
if self.amount_sats == 1:
sattext = "sat"
return f"{self.amount_sats} {sattext} @ {self.epoch_time}: {self.comment}"
#return f"{self.amount_sats} {sattext} @ {self.epoch_time}: {self.comment}"
return f"{self.amount_sats} {sattext}: {self.comment}"
def __eq__(self, other):
if not isinstance(other, Payment):
return False
return self.epoch_time == other.epoch_time and self.amount_sats == other.amount_sats and self.comment == other.comment
'''
def __lt__(self, other):
if not isinstance(other, Payment):
return NotImplemented
@@ -87,18 +88,16 @@ class Payment:
if not isinstance(other, Payment):
return NotImplemented
return (self.epoch_time, self.amount_sats, self.comment) <= (other.epoch_time, other.amount_sats, other.comment)
'''
def __gt__(self, other):
if not isinstance(other, Payment):
return NotImplemented
#return (self.epoch_time, self.amount_sats, self.comment) > (other.epoch_time, other.amount_sats, other.comment)
return self.epoch_time > other.epoch_time
'''
return (self.epoch_time, self.amount_sats, self.comment) > (other.epoch_time, other.amount_sats, other.comment)
def __ge__(self, other):
if not isinstance(other, Payment):
return NotImplemented
return (self.epoch_time, self.amount_sats, self.comment) >= (other.epoch_time, other.amount_sats, other.comment)
'''
class Wallet:
@@ -278,6 +277,7 @@ class NWCWallet(Wallet):
super().__init__()
self.nwc_url = nwc_url
self.connected = False
self.relay, self.wallet_pubkey, self.secret, self.lud16 = self.parse_nwc_url(self.nwc_url)
def getCommentFromTransaction(self, transaction):
comment = ""
@@ -295,7 +295,6 @@ class NWCWallet(Wallet):
return comment
def wallet_manager_thread(self):
self.relay, self.wallet_pubkey, self.secret, self.lud16 = self.parse_nwc_url(self.nwc_url)
self.private_key = PrivateKey(bytes.fromhex(self.secret))
self.relay_manager = RelayManager()
self.relay_manager.add_relay(self.relay)
@@ -375,7 +374,7 @@ class NWCWallet(Wallet):
continue
new_balance = self.last_known_balance + amount
self.handle_new_balance(new_balance, False)
epoch_time = transaction["created_at"]
epoch_time = notification["created_at"]
comment = self.getCommentFromTransaction(notification)
paymentObj = Payment(epoch_time, amount, comment)
self.handle_new_payment(paymentObj)
@@ -383,7 +382,7 @@ class NWCWallet(Wallet):
print("Unsupported response, ignoring.")
except Exception as e:
print(f"DEBUG: Error processing response: {e}")
time.sleep(1)
time.sleep(0.2)
print("NWCWallet: manage_wallet_thread stopping, closing connections...")
self.relay_manager.close_connections()