forked from MicroPythonOS/MicroPythonOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanual_test_nostr_asyncio.py
More file actions
331 lines (292 loc) · 14.4 KB
/
manual_test_nostr_asyncio.py
File metadata and controls
331 lines (292 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import asyncio
import json
import ssl
import _thread
import time
import unittest
from mpos import App, AppManager
from nostr.relay_manager import RelayManager
from nostr.message_type import ClientMessageType
from nostr.filter import Filter, Filters
from nostr.event import EncryptedDirectMessage
from nostr.key import PrivateKey
# keeps a list of items
# The .add() method ensures the list remains unique (via __eq__)
# and sorted (via __lt__) by inserting new items in the correct position.
class UniqueSortedList:
def __init__(self):
self._items = []
def add(self, item):
#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__)
for i, existing_item in enumerate(self._items):
if item > existing_item:
self._items.insert(i, item)
return
# If item is smaller than all existing items, append it
self._items.append(item)
#print(f"after add: {str(self)}")
def __iter__(self):
# Return iterator for the internal list
return iter(self._items)
def get(self, index_nr):
# Retrieve item at given index, raise IndexError if invalid
try:
return self._items[index_nr]
except IndexError:
raise IndexError("Index out of range")
def __len__(self):
# Return the number of items for len() calls
return len(self._items)
def __str__(self):
#print("UniqueSortedList tostring called")
return "\n".join(str(item) for item in self._items)
def __eq__(self, other):
if len(self._items) != len(other):
return False
return all(p1 == p2 for p1, p2 in zip(self._items, other))
# Payment class remains unchanged
class Payment:
def __init__(self, epoch_time, amount_sats, comment):
self.epoch_time = epoch_time
self.amount_sats = amount_sats
self.comment = comment
def __str__(self):
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.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
return (self.epoch_time, self.amount_sats, self.comment) < (other.epoch_time, other.amount_sats, other.comment)
def __le__(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)
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)
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 TestNostr(unittest.TestCase):
PAYMENTS_TO_SHOW = 5
keep_running = None
connected = None
balance = -1
payment_list = []
transactions_welcome = False
relays = [ "ws://192.168.1.16:5000/nostrrelay/test", "ws://192.168.1.16:5000/nostrclient/api/v1/relay" ]
#relays = [ "ws://127.0.0.1:5000/nostrrelay/test", "ws://127.0.0.1:5000/nostrclient/api/v1/relay" ]
#relays = [ "wss://relay.damus.io", "wss://nostr-pub.wellorder.net" ]
#relays = [ "ws://127.0.0.1:5000/nostrrelay/test", "ws://127.0.0.1:5000/nostrclient/api/v1/relay", "wss://relay.damus.io", "wss://nostr-pub.wellorder.net" ]
#relays = [ "ws://127.0.0.1:5000/nostrclient/api/v1/relay", "wss://relay.damus.io", "wss://nostr-pub.wellorder.net" ]
secret = "fab0a9a11d4cf4b1d92e901a0b2c56634275e2fa1a7eb396ff1b942f95d59fd3" # not really a secret, just from a local fake wallet
wallet_pubkey = "e46762afab282c324278351165122345f9983ea447b47943b052100321227571"
async def fetch_balance(self):
if not self.keep_running:
return
# Create get_balance request
balance_request = {
"method": "get_balance",
"params": {}
}
print(f"DEBUG: Created balance request: {balance_request}")
print(f"DEBUG: Creating encrypted DM to wallet pubkey: {self.wallet_pubkey}")
dm = EncryptedDirectMessage(
recipient_pubkey=self.wallet_pubkey,
cleartext_content=json.dumps(balance_request),
kind=23194
)
print(f"DEBUG: Signing DM {json.dumps(dm)} with private key")
self.private_key.sign_event(dm) # sign also does encryption if it's a encrypted dm
print(f"DEBUG: Publishing encrypted DM")
self.relay_manager.publish_event(dm)
def handle_new_balance(self, new_balance, fetchPaymentsIfChanged=True):
if not self.keep_running or new_balance is None:
return
if fetchPaymentsIfChanged: # Fetching *all* payments isn't necessary if balance was changed by a payment notification
print("Refreshing payments...")
self.fetch_payments() # if the balance changed, then re-list transactions
def fetch_payments(self):
if not self.keep_running:
return
# Create get_balance request
list_transactions = {
"method": "list_transactions",
"params": {
"limit": self.PAYMENTS_TO_SHOW
}
}
dm = EncryptedDirectMessage(
recipient_pubkey=self.wallet_pubkey,
cleartext_content=json.dumps(list_transactions),
kind=23194
)
self.private_key.sign_event(dm) # sign also does encryption if it's a encrypted dm
print("\nPublishing DM to fetch payments...")
self.relay_manager.publish_event(dm)
self.transactions_welcome = True
def handle_new_payments(self, new_payments):
if not self.keep_running or not self.transactions_welcome:
return
print("handle_new_payments")
if self.payment_list != new_payments:
print("new list of payments")
self.payment_list = new_payments
self.payments_updated_cb()
def payments_updated_cb(self):
print("payments_updated_cb called, now closing everything!")
self.keep_running = False
def getCommentFromTransaction(self, transaction):
comment = ""
try:
comment = transaction["description"]
json_comment = json.loads(comment)
for field in json_comment:
if field[0] == "text/plain":
comment = field[1]
break
else:
print("text/plain field is missing from JSON description")
except Exception as e:
print(f"Info: could not parse comment as JSON, this is fine, using as-is ({e})")
return comment
async def NOmainHERE(self):
self.keep_running = True
self.private_key = PrivateKey(bytes.fromhex(self.secret))
self.relay_manager = RelayManager()
for relay in self.relays:
self.relay_manager.add_relay(relay)
print(f"DEBUG: Opening relay connections")
await self.relay_manager.open_connections({"cert_reqs": ssl.CERT_NONE})
self.allconnected = False
for _ in range(20):
print("Waiting for relay connection...")
await asyncio.sleep(0.5)
nrconnected = 0
for index, relay in enumerate(self.relays):
try:
relay = self.relay_manager.relays[self.relays[index]]
if relay.connected is True:
print(f"connected: {self.relays[index]}")
nrconnected += 1
else:
print(f"not connected: {self.relays[index]}")
except Exception as e:
print(f"could not find relay: {e}")
break # not all of them have been initialized, skip...
self.allconnected = ( nrconnected == len(self.relays) )
if self.allconnected:
print("All relays connected!")
break
if not self.allconnected or not self.keep_running:
print(f"ERROR: could not connect to relay or not self.keep_running, aborting...")
return
# Set up subscription to receive response
self.subscription_id = "micropython_nwc_" + str(round(time.time()))
print(f"DEBUG: Setting up subscription with ID: {self.subscription_id}")
self.filters = Filters([Filter(
#event_ids=[self.subscription_id], # would be nice to filter, but not like this
kinds=[23195, 23196], # NWC reponses and notifications
authors=[self.wallet_pubkey],
pubkey_refs=[self.private_key.public_key.hex()]
)])
print(f"DEBUG: Subscription filters: {self.filters.to_json_array()}")
self.relay_manager.add_subscription(self.subscription_id, self.filters)
print(f"DEBUG: Creating subscription request")
request_message = [ClientMessageType.REQUEST, self.subscription_id]
request_message.extend(self.filters.to_json_array())
print(f"DEBUG: Publishing subscription request")
self.relay_manager.publish_message(json.dumps(request_message))
print(f"DEBUG: Published subscription request")
for _ in range(4):
if not self.keep_running:
return
print("Waiting a bit before self.fetch_balance()")
await asyncio.sleep(0.5)
await self.fetch_balance()
while True:
print(f"checking for incoming events...")
await asyncio.sleep(1)
if not self.keep_running:
print("NWCWallet: not keep_running, closing connections...")
await self.relay_manager.close_connections()
break
start_time = time.ticks_ms()
if self.relay_manager.message_pool.has_events():
print(f"DEBUG: Event received from message pool after {time.ticks_ms()-start_time}ms")
event_msg = self.relay_manager.message_pool.get_event()
event_created_at = event_msg.event.created_at
print(f"Received at {time.localtime()} a message with timestamp {event_created_at} after {time.ticks_ms()-start_time}ms")
try:
# This takes a very long time, even for short messages:
decrypted_content = self.private_key.decrypt_message(
event_msg.event.content,
event_msg.event.public_key,
)
print(f"DEBUG: Decrypted content: {decrypted_content} after {time.ticks_ms()-start_time}ms")
response = json.loads(decrypted_content)
print(f"DEBUG: Parsed response: {response}")
result = response.get("result")
if result:
if result.get("balance") is not None:
new_balance = round(int(result["balance"]) / 1000)
print(f"Got balance: {new_balance}")
self.handle_new_balance(new_balance)
elif result.get("transactions") is not None:
print("Response contains transactions!")
new_payment_list = UniqueSortedList()
for transaction in result["transactions"]:
amount = transaction["amount"]
amount = round(amount / 1000)
comment = self.getCommentFromTransaction(transaction)
epoch_time = transaction["created_at"]
paymentObj = Payment(epoch_time, amount, comment)
new_payment_list.add(paymentObj)
if len(new_payment_list) > 0:
# do them all in one shot instead of one-by-one because the lv_async() isn't always chronological,
# so when a long list of payments is added, it may be overwritten by a short list
self.handle_new_payments(new_payment_list)
else:
notification = response.get("notification")
if notification:
amount = notification["amount"]
amount = round(amount / 1000)
type = notification["type"]
if type == "outgoing":
amount = -amount
elif type == "incoming":
new_balance = self.last_known_balance + amount
self.handle_new_balance(new_balance, False) # don't trigger full fetch because payment info is in notification
epoch_time = notification["created_at"]
comment = self.getCommentFromTransaction(notification)
paymentObj = Payment(epoch_time, amount, comment)
self.handle_new_payment(paymentObj)
else:
print(f"WARNING: invalid notification type {type}, ignoring.")
else:
print("Unsupported response, ignoring.")
except Exception as e:
print(f"DEBUG: Error processing response: {e}")
else:
#print(f"pool has no events after {time.ticks_ms()-start_time}ms") # completes in 0-1ms
pass
def test_it(self):
print("before do_two")
asyncio.run(self.do_two())
print("after do_two")
def do_two(self):
print("before await self.NOmainHERE()")
await self.NOmainHERE()
print("after await self.NOmainHERE()")