Skip to content

Commit c7d50e0

Browse files
Piggy: fix instant payment notifications in NWC timestamp, set correct static receive QR code
1 parent 18d3d06 commit c7d50e0

2 files changed

Lines changed: 25 additions & 20 deletions

File tree

internal_filesystem/apps/com.lightningpiggy.displaywallet/assets/displaywallet.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,19 @@ def settings_button_tap(event):
193193
wallet.stop()
194194
mpos.ui.load_screen(settings_screen)
195195

196+
def main_ui_set_defaults():
197+
global balance_label, payments_label, receive_qr
198+
balance_label.set_text(lv.SYMBOL.REFRESH)
199+
payments_label.set_text(lv.SYMBOL.REFRESH)
200+
receive_qr.update("", len(""))
201+
196202
def build_main_ui():
197203
global main_screen, balance_label, payments_label, receive_qr
198204
main_screen = lv.obj()
199205
main_screen.set_style_pad_all(10, 0)
200206
balance_label = lv.label(main_screen)
201207
balance_label.align(lv.ALIGN.TOP_LEFT, 0, 0)
202208
balance_label.set_style_text_font(lv.font_montserrat_22, 0)
203-
balance_label.set_text(lv.SYMBOL.REFRESH)
204209
receive_qr = lv.qrcode(main_screen)
205210
receive_qr.set_size(50)
206211
receive_qr.set_dark_color(lv.color_black())
@@ -219,7 +224,6 @@ def build_main_ui():
219224
payments_label = lv.label(main_screen)
220225
payments_label.align_to(balance_line,lv.ALIGN.OUT_BOTTOM_LEFT,0,10)
221226
payments_label.set_style_text_font(lv.font_montserrat_16, 0)
222-
payments_label.set_text(lv.SYMBOL.REFRESH)
223227
settings_button = lv.button(main_screen)
224228
settings_button.align(lv.ALIGN.BOTTOM_RIGHT, 0, 0)
225229
snap_label = lv.label(settings_button)
@@ -240,27 +244,29 @@ def redraw_payments_cb():
240244

241245
def janitor_cb(timer):
242246
global wallet, config
243-
if lv.screen_active() == main_screen and (not wallet or not wallet.is_running()):
244-
build_main_ui()
245-
# just started the app or just returned from settings_screen
247+
if lv.screen_active() == main_screen and (not wallet or not wallet.is_running()): # just started the app or just returned from settings_screen
248+
main_ui_set_defaults()
246249
config = mpos.config.SharedPreferences("com.lightningpiggy.displaywallet")
247-
static_receive_code = config.get_string("static_receive_code")
248-
if static_receive_code:
249-
receive_qr.update(static_receive_code, len(static_receive_code))
250250
wallet_type = config.get_string("wallet_type")
251251
if wallet_type == "lnbits":
252252
try:
253+
static_receive_code = config.get_string("static_receive_code")
253254
wallet = LNBitsWallet(config.get_string("lnbits_url"), config.get_string("lnbits_readkey"))
254255
except Exception as e:
255256
print(f"Couldn't initialize LNBitsWallet because: {e}")
256257
elif wallet_type == "nwc":
257258
try:
258259
wallet = NWCWallet(config.get_string("nwc_url"))
260+
static_receive_code = wallet.lud16
259261
except Exception as e:
260262
print(f"Couldn't initialize NWCWallet because: {e}")
261263
else:
262264
print(f"No or unsupported wallet type configured: '{wallet_type}'")
265+
if static_receive_code:
266+
print(f"Setting static_receive_code: {static_receive_code}")
267+
receive_qr.update(static_receive_code, len(static_receive_code))
263268
if wallet:
269+
print("Starting wallet...")
264270
wallet.start(redraw_balance_cb, redraw_payments_cb)
265271
else:
266272
print("ERROR: could not start any wallet!") # maybe call the error callback to show the error to the user

internal_filesystem/apps/com.lightningpiggy.displaywallet/assets/wallet.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self):
2424
self._items = []
2525

2626
def add(self, item):
27-
print(f"before add: {str(self)}")
27+
#print(f"before add: {str(self)}")
2828
# Check if item already exists (using __eq__)
2929
if item not in self._items:
3030
# Insert item in sorted position for descending order (using __gt__)
@@ -34,7 +34,7 @@ def add(self, item):
3434
return
3535
# If item is smaller than all existing items, append it
3636
self._items.append(item)
37-
print(f"after add: {str(self)}")
37+
#print(f"after add: {str(self)}")
3838

3939
def __iter__(self):
4040
# Return iterator for the internal list
@@ -71,13 +71,14 @@ def __str__(self):
7171
sattext = "sats"
7272
if self.amount_sats == 1:
7373
sattext = "sat"
74-
return f"{self.amount_sats} {sattext} @ {self.epoch_time}: {self.comment}"
74+
#return f"{self.amount_sats} {sattext} @ {self.epoch_time}: {self.comment}"
75+
return f"{self.amount_sats} {sattext}: {self.comment}"
7576

7677
def __eq__(self, other):
7778
if not isinstance(other, Payment):
7879
return False
7980
return self.epoch_time == other.epoch_time and self.amount_sats == other.amount_sats and self.comment == other.comment
80-
'''
81+
8182
def __lt__(self, other):
8283
if not isinstance(other, Payment):
8384
return NotImplemented
@@ -87,18 +88,16 @@ def __le__(self, other):
8788
if not isinstance(other, Payment):
8889
return NotImplemented
8990
return (self.epoch_time, self.amount_sats, self.comment) <= (other.epoch_time, other.amount_sats, other.comment)
90-
'''
91+
9192
def __gt__(self, other):
9293
if not isinstance(other, Payment):
9394
return NotImplemented
94-
#return (self.epoch_time, self.amount_sats, self.comment) > (other.epoch_time, other.amount_sats, other.comment)
95-
return self.epoch_time > other.epoch_time
96-
'''
95+
return (self.epoch_time, self.amount_sats, self.comment) > (other.epoch_time, other.amount_sats, other.comment)
96+
9797
def __ge__(self, other):
9898
if not isinstance(other, Payment):
9999
return NotImplemented
100100
return (self.epoch_time, self.amount_sats, self.comment) >= (other.epoch_time, other.amount_sats, other.comment)
101-
'''
102101

103102
class Wallet:
104103

@@ -278,6 +277,7 @@ def __init__(self, nwc_url):
278277
super().__init__()
279278
self.nwc_url = nwc_url
280279
self.connected = False
280+
self.relay, self.wallet_pubkey, self.secret, self.lud16 = self.parse_nwc_url(self.nwc_url)
281281

282282
def getCommentFromTransaction(self, transaction):
283283
comment = ""
@@ -295,7 +295,6 @@ def getCommentFromTransaction(self, transaction):
295295
return comment
296296

297297
def wallet_manager_thread(self):
298-
self.relay, self.wallet_pubkey, self.secret, self.lud16 = self.parse_nwc_url(self.nwc_url)
299298
self.private_key = PrivateKey(bytes.fromhex(self.secret))
300299
self.relay_manager = RelayManager()
301300
self.relay_manager.add_relay(self.relay)
@@ -375,15 +374,15 @@ def wallet_manager_thread(self):
375374
continue
376375
new_balance = self.last_known_balance + amount
377376
self.handle_new_balance(new_balance, False)
378-
epoch_time = transaction["created_at"]
377+
epoch_time = notification["created_at"]
379378
comment = self.getCommentFromTransaction(notification)
380379
paymentObj = Payment(epoch_time, amount, comment)
381380
self.handle_new_payment(paymentObj)
382381
else:
383382
print("Unsupported response, ignoring.")
384383
except Exception as e:
385384
print(f"DEBUG: Error processing response: {e}")
386-
time.sleep(1)
385+
time.sleep(0.2)
387386

388387
print("NWCWallet: manage_wallet_thread stopping, closing connections...")
389388
self.relay_manager.close_connections()

0 commit comments

Comments
 (0)