|
| 1 | + |
| 2 | +class Wallet: |
| 3 | + |
| 4 | + # These values could be loading from a cache.json file at __init__ |
| 5 | + last_known_balance = 0 |
| 6 | + last_known_balance_timestamp = 0 |
| 7 | + |
| 8 | + def __init__(self): |
| 9 | + pass |
| 10 | + |
| 11 | + def __str__(self): |
| 12 | + if isinstance(self, LNBitsWallet): |
| 13 | + return "LNBitsWallet" |
| 14 | + elif isinstance(self, NWCWallet): |
| 15 | + return "NWCWallet" |
| 16 | + |
| 17 | + |
| 18 | +class LNBitsWallet(Wallet): |
| 19 | + |
| 20 | + def __init__(self, lnbits_url, lnbits_readkey): |
| 21 | + super().__init__() |
| 22 | + self.lnbits_url = lnbits_url |
| 23 | + self.lnbits_readkey = lnbits_readkey |
| 24 | + |
| 25 | + def fetch_balance_thread(): |
| 26 | + print("fetch_balance_thread") |
| 27 | + walleturl = self.lnbits_url + "/api/v1/wallet" |
| 28 | + headers = { |
| 29 | + "X-Api-Key": self.lnbits_readkey, |
| 30 | + } |
| 31 | + try: |
| 32 | + response = requests.get(self.lnbits_url, timeout=10, headers=headers) |
| 33 | + except Exception as e: |
| 34 | + print("GET request failed:", e) |
| 35 | + #lv.async_call(lambda l: please_wait_label.set_text(f"Error downloading app index: {e}"), None) |
| 36 | + if response and response.status_code == 200: |
| 37 | + print(f"Got response text: {response.text}") |
| 38 | + response.close() |
| 39 | + |
| 40 | + def start_refresh_balance(self): |
| 41 | + _thread.stack_size(mpos.apps.good_stack_size()) |
| 42 | + _thread.start_new_thread(self.fetch_balance_thread, ()) |
| 43 | + |
| 44 | +class NWCWallet(Wallet): |
| 45 | + |
| 46 | + def __init__(self, nwc_url): |
| 47 | + super().__init__() |
| 48 | + self.nwc_url = nwc_url |
| 49 | + nwc_data = parse_nwc_url(nwc_url) |
| 50 | + self.relay = nwc_data['relay'] |
| 51 | + self.wallet_pubkey = nwc_data['pubkey'] |
| 52 | + self.secret = nwc_data['secret'] |
| 53 | + self.lud16 = nwc_data['lud16'] |
| 54 | + print(f"DEBUG: Parsed NWC data - Relay: {relay}, Pubkey: {wallet_pubkey}, Secret: {secret}, lud16: {lud16}") |
| 55 | + # TODO: open connection to relay, subscribe to updates |
| 56 | + |
| 57 | + def start_refresh_balance(self) : |
| 58 | + # TODO: make sure connected to relay (otherwise connect) and fetch balance |
| 59 | + pass |
| 60 | + |
| 61 | + def parse_nwc_url(self, nwc_url): |
| 62 | + """Parse Nostr Wallet Connect URL to extract pubkey, relay, secret, and lud16.""" |
| 63 | + print(f"DEBUG: Starting to parse NWC URL: {nwc_url}") |
| 64 | + try: |
| 65 | + # Remove 'nostr+walletconnect://' or 'nwc:' prefix |
| 66 | + if nwc_url.startswith('nostr+walletconnect://'): |
| 67 | + print(f"DEBUG: Removing 'nostr+walletconnect://' prefix") |
| 68 | + nwc_url = nwc_url[22:] |
| 69 | + elif nwc_url.startswith('nwc:'): |
| 70 | + print(f"DEBUG: Removing 'nwc:' prefix") |
| 71 | + nwc_url = nwc_url[4:] |
| 72 | + else: |
| 73 | + print(f"DEBUG: No recognized prefix found in URL") |
| 74 | + raise ValueError("Invalid NWC URL: missing 'nostr+walletconnect://' or 'nwc:' prefix") |
| 75 | + |
| 76 | + print(f"DEBUG: URL after prefix removal: {nwc_url}") |
| 77 | + |
| 78 | + # Split into pubkey and query params |
| 79 | + parts = nwc_url.split('?') |
| 80 | + pubkey = parts[0] |
| 81 | + print(f"DEBUG: Extracted pubkey: {pubkey}") |
| 82 | + |
| 83 | + # Validate pubkey (should be 64 hex characters) |
| 84 | + if len(pubkey) != 64 or not all(c in '0123456789abcdef' for c in pubkey): |
| 85 | + raise ValueError("Invalid NWC URL: pubkey must be 64 hex characters") |
| 86 | + |
| 87 | + # Extract relay, secret, and lud16 from query params |
| 88 | + relay = None |
| 89 | + secret = None |
| 90 | + lud16 = None |
| 91 | + if len(parts) > 1: |
| 92 | + print(f"DEBUG: Query parameters found: {parts[1]}") |
| 93 | + params = parts[1].split('&') |
| 94 | + for param in params: |
| 95 | + if param.startswith('relay='): |
| 96 | + relay = param[6:] # TODO: urldecode because the relay might have %3A%2F%2F etc |
| 97 | + print(f"DEBUG: Extracted relay: {relay}") |
| 98 | + elif param.startswith('secret='): |
| 99 | + secret = param[7:] |
| 100 | + print(f"DEBUG: Extracted secret: {secret}") |
| 101 | + elif param.startswith('lud16='): |
| 102 | + lud16 = param[6:] |
| 103 | + print(f"DEBUG: Extracted lud16: {lud16}") |
| 104 | + else: |
| 105 | + print(f"DEBUG: No query parameters found") |
| 106 | + |
| 107 | + if not pubkey or not relay or not secret: |
| 108 | + raise ValueError("Invalid NWC URL: missing required fields (pubkey, relay, or secret)") |
| 109 | + |
| 110 | + # Validate secret (should be 64 hex characters) |
| 111 | + if len(secret) != 64 or not all(c in '0123456789abcdef' for c in secret): |
| 112 | + raise ValueError("Invalid NWC URL: secret must be 64 hex characters") |
| 113 | + |
| 114 | + return { |
| 115 | + 'relay': relay, |
| 116 | + 'pubkey': pubkey, |
| 117 | + 'secret': secret, |
| 118 | + 'lud16': lud16 |
| 119 | + } |
| 120 | + except Exception as e: |
| 121 | + print(f"DEBUG: Error parsing NWC URL: {e}") |
| 122 | + sys.exit(1) |
| 123 | + |
0 commit comments