Gyr0MAN commited on
Commit
16693c3
·
verified ·
1 Parent(s): ca382e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -30
app.py CHANGED
@@ -19,41 +19,18 @@ client.connect()
19
  now = Timestamp.now()
20
 
21
  nip04_filter = Filter().pubkey(pk).kind(Kind.from_enum(KindEnum.ENCRYPTED_DIRECT_MESSAGE())).since(now)
22
- nip59_filter = Filter().pubkey(pk).kind(Kind.from_enum(KindEnum.GIFT_WRAP())).since(
23
- Timestamp.from_secs(now.as_secs() - 60 * 60 * 24 * 7)) # NIP59 have a tweaked timestamp (in the past)
24
- client.subscribe([nip04_filter, nip59_filter], None)
25
 
26
 
27
  class NotificationHandler(HandleNotification):
28
  def handle(self, relay_url, subscription_id, event: Event):
29
  print(f"Received new event from {relay_url}: {event.as_json()}")
30
- print(event.kind == KindEnum.ENCRYPTED_DIRECT_MESSAGE())
31
- if event.kind().match_enum(KindEnum.ENCRYPTED_DIRECT_MESSAGE()):
32
- print("Decrypting NIP04 event")
33
- try:
34
- msg = nip04_decrypt(sk, event.author(), event.content())
35
- print(f"Received new msg: {msg}")
36
- client.send_direct_msg(event.author(), f"Echo: {msg}", event.id())
37
- except Exception as e:
38
- print(f"Error during content NIP04 decryption: {e}")
39
- elif event.kind().match_enum(KindEnum.GIFT_WRAP()):
40
- print("Decrypting NIP59 event")
41
- try:
42
- # Extract rumor
43
- unwrapped_gift = UnwrappedGift.from_gift_wrap(keys, event)
44
- sender = unwrapped_gift.sender()
45
- rumor: UnsignedEvent = unwrapped_gift.rumor()
46
-
47
- # Check timestamp of rumor
48
- if rumor.created_at().as_secs() >= now.as_secs():
49
- if rumor.kind().match_enum(KindEnum.SEALED_DIRECT()):
50
- msg = rumor.content()
51
- print(f"Received new msg [sealed]: {msg}")
52
- client.send_sealed_msg(sender, f"Echo: {msg}", None)
53
- else:
54
- print(f"{rumor.as_json()}")
55
- except Exception as e:
56
- print(f"Error during content NIP59 decryption: {e}")
57
 
58
  def handle_msg(self, relay_url, msg):
59
  None
 
19
  now = Timestamp.now()
20
 
21
  nip04_filter = Filter().pubkey(pk).kind(Kind.from_enum(KindEnum.ENCRYPTED_DIRECT_MESSAGE())).since(now)
22
+ client.subscribe([nip04_filter], None)
 
 
23
 
24
 
25
  class NotificationHandler(HandleNotification):
26
  def handle(self, relay_url, subscription_id, event: Event):
27
  print(f"Received new event from {relay_url}: {event.as_json()}")
28
+ try:
29
+ msg = nip04_decrypt(sk, event.author(), event.content())
30
+ print(f"Received new msg: {msg}")
31
+ client.send_direct_msg(event.author(), f"Echo: {msg}", event.id())
32
+ except Exception as e:
33
+ print(f"Error during content NIP04 decryption: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  def handle_msg(self, relay_url, msg):
36
  None