-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 96729e1
Showing
19 changed files
with
739 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
PRIVATE_KEY=nsec1eaye9ekhzzsngkfxvdhe7xdr2qnu2r6u2zmxzqu23ye3rtr57ljstpcugx | ||
PUBLIC_KEY=npub167jhh72c7d7g3gfg360rt2qrtsprqyt2jamae5rhvmxp486dny9sptl3vr | ||
PRIVATE_KEY_HEX=cf4992e6d710a1345926636f9f19a35027c50f5c50b661038a893311ac74f7e5 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import time | ||
import json | ||
from enum import IntEnum | ||
from secp256k1 import PrivateKey, PublicKey | ||
from hashlib import sha256 | ||
|
||
from nostr.message_type import ClientMessageType | ||
|
||
|
||
class EventKind(IntEnum): | ||
SET_METADATA = 0 | ||
TEXT_NOTE = 1 | ||
RECOMMEND_RELAY = 2 | ||
CONTACTS = 3 | ||
ENCRYPTED_DIRECT_MESSAGE = 4 | ||
DELETE = 5 | ||
|
||
|
||
class Event(): | ||
def __init__( | ||
self, | ||
public_key: str, | ||
content: str, | ||
created_at: int = None, | ||
kind: int=EventKind.TEXT_NOTE, | ||
# tags: "list[list[str]]"=[], | ||
tags: "list"=[], | ||
id: str=None, | ||
signature: str=None, | ||
full_message: "list"=[]) -> None: | ||
|
||
if not isinstance(content, str): | ||
raise TypeError("Argument 'content' must be of type str") | ||
|
||
self.public_key = public_key | ||
self.content = content | ||
self.created_at = created_at or int(time.time()) | ||
self.kind = kind | ||
self.tags = tags | ||
self.id = id or Event.compute_id(self.public_key, self.created_at, self.kind, self.tags, self.content) | ||
self.signature = signature | ||
self.full_message = full_message | ||
|
||
@staticmethod | ||
def serialize(public_key: str, created_at: int, kind: int, tags: "list[list[str]]", content: str) -> bytes: | ||
data = [0, public_key, created_at, kind, tags, content] | ||
data_str = json.dumps(data, separators=(',', ':'), ensure_ascii=False) | ||
return data_str.encode() | ||
|
||
@staticmethod | ||
def compute_id(public_key: str, created_at: int, kind: int, tags: "list[list[str]]", content: str) -> str: | ||
return sha256(Event.serialize(public_key, created_at, kind, tags, content)).hexdigest() | ||
|
||
def verify(self) -> bool: | ||
pub_key = PublicKey(bytes.fromhex("02" + self.public_key), True) # add 02 for schnorr (bip340) | ||
event_id = Event.compute_id(self.public_key, self.created_at, self.kind, self.tags, self.content) | ||
return pub_key.schnorr_verify(bytes.fromhex(event_id), bytes.fromhex(self.signature), None, raw=True) | ||
|
||
def to_message(self) -> str: | ||
return json.dumps( | ||
[ | ||
ClientMessageType.EVENT, | ||
{ | ||
"id": self.id, | ||
"pubkey": self.public_key, | ||
"created_at": self.created_at, | ||
"kind": self.kind, | ||
"tags": self.tags, | ||
"content": self.content, | ||
"sig": self.signature, | ||
"full_message": self.full_message | ||
} | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import json | ||
from queue import Queue | ||
from threading import Lock | ||
from .message_type import RelayMessageType | ||
from .event import Event | ||
|
||
class EventMessage: | ||
def __init__(self, event: Event, subscription_id: str, url: str) -> None: | ||
self.event = event | ||
self.subscription_id = subscription_id | ||
self.url = url | ||
|
||
class NoticeMessage: | ||
def __init__(self, content: str, url: str) -> None: | ||
self.content = content | ||
self.url = url | ||
|
||
class EndOfStoredEventsMessage: | ||
def __init__(self, subscription_id: str, url: str) -> None: | ||
self.subscription_id = subscription_id | ||
self.url = url | ||
|
||
class MessagePool: | ||
def __init__(self) -> None: | ||
self.events: Queue[EventMessage] = Queue() | ||
self.notices: Queue[NoticeMessage] = Queue() | ||
self.eose_notices: Queue[EndOfStoredEventsMessage] = Queue() | ||
self._unique_events: set = set() | ||
self.lock: Lock = Lock() | ||
|
||
def add_message(self, message: str, url: str): | ||
self._process_message(message, url) | ||
|
||
def get_event(self): | ||
return self.events.get() | ||
|
||
def get_notice(self): | ||
return self.notices.get() | ||
|
||
def get_eose_notice(self): | ||
return self.eose_notices.get() | ||
|
||
def has_events(self): | ||
return self.events.qsize() > 0 | ||
|
||
def has_notices(self): | ||
return self.notices.qsize() > 0 | ||
|
||
def has_eose_notices(self): | ||
return self.eose_notices.qsize() > 0 | ||
|
||
def _process_message(self, message: str, url: str): | ||
message_json = json.loads(message) | ||
message_type = message_json[0] | ||
print("\n",message_json) | ||
if message_type == RelayMessageType.EVENT: | ||
subscription_id = message_json[1] | ||
e = message_json[2] | ||
event = Event(e['pubkey'], e['content'], e['created_at'], e['kind'], e['tags'], e['id'], e['sig'], message_json) | ||
with self.lock: | ||
if not event.id in self._unique_events: | ||
self.events.put(EventMessage(event, subscription_id, url)) | ||
self._unique_events.add(event.id) | ||
elif message_type == RelayMessageType.NOTICE: | ||
self.notices.put(NoticeMessage(message_json[1], url)) | ||
elif message_type == RelayMessageType.END_OF_STORED_EVENTS: | ||
self.eose_notices.put(EndOfStoredEventsMessage(message_json[1], url)) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import time | ||
import json | ||
from enum import IntEnum | ||
from secp256k1 import PrivateKey, PublicKey | ||
from hashlib import sha256 | ||
|
||
from nostr.message_type import ClientMessageType | ||
|
||
|
||
class EventKind(IntEnum): | ||
SET_METADATA = 0 | ||
TEXT_NOTE = 1 | ||
RECOMMEND_RELAY = 2 | ||
CONTACTS = 3 | ||
ENCRYPTED_DIRECT_MESSAGE = 4 | ||
DELETE = 5 | ||
|
||
|
||
class Event(): | ||
def __init__( | ||
self, | ||
public_key: str, | ||
content: str, | ||
created_at: int = None, | ||
kind: int=EventKind.TEXT_NOTE, | ||
tags: "list[list[str]]"=[], | ||
id: str=None, | ||
signature: str=None, | ||
json: "list"=[]) -> None: | ||
|
||
if not isinstance(content, str): | ||
raise TypeError("Argument 'content' must be of type str") | ||
|
||
self.public_key = public_key | ||
self.content = content | ||
self.created_at = created_at or int(time.time()) | ||
self.kind = kind | ||
self.tags = tags | ||
self.id = id or Event.compute_id(self.public_key, self.created_at, self.kind, self.tags, self.content) | ||
self.signature = signature | ||
self.json = json | ||
|
||
@staticmethod | ||
def serialize(public_key: str, created_at: int, kind: int, tags: "list[list[str]]", content: str) -> bytes: | ||
data = [0, public_key, created_at, kind, tags, content] | ||
data_str = json.dumps(data, separators=(',', ':'), ensure_ascii=False) | ||
return data_str.encode() | ||
|
||
@staticmethod | ||
def compute_id(public_key: str, created_at: int, kind: int, tags: "list[list[str]]", content: str) -> str: | ||
return sha256(Event.serialize(public_key, created_at, kind, tags, content)).hexdigest() | ||
|
||
def verify(self) -> bool: | ||
pub_key = PublicKey(bytes.fromhex("02" + self.public_key), True) # add 02 for schnorr (bip340) | ||
event_id = Event.compute_id(self.public_key, self.created_at, self.kind, self.tags, self.content) | ||
return pub_key.schnorr_verify(bytes.fromhex(event_id), bytes.fromhex(self.signature), None, raw=True) | ||
|
||
def to_message(self) -> str: | ||
return json.dumps( | ||
[ | ||
ClientMessageType.EVENT, | ||
{ | ||
"id": self.id, | ||
"pubkey": self.public_key, | ||
"created_at": self.created_at, | ||
"kind": self.kind, | ||
"tags": self.tags, | ||
"content": self.content, | ||
"sig": self.signature, | ||
"json": self.json | ||
} | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import json | ||
from queue import Queue | ||
from threading import Lock | ||
from .message_type import RelayMessageType | ||
from .event import Event | ||
|
||
class EventMessage: | ||
def __init__(self, event: Event, subscription_id: str, url: str) -> None: | ||
self.event = event | ||
self.subscription_id = subscription_id | ||
self.url = url | ||
|
||
class NoticeMessage: | ||
def __init__(self, content: str, url: str) -> None: | ||
self.content = content | ||
self.url = url | ||
|
||
class EndOfStoredEventsMessage: | ||
def __init__(self, subscription_id: str, url: str) -> None: | ||
self.subscription_id = subscription_id | ||
self.url = url | ||
|
||
class MessagePool: | ||
def __init__(self) -> None: | ||
self.events: Queue[EventMessage] = Queue() | ||
self.notices: Queue[NoticeMessage] = Queue() | ||
self.eose_notices: Queue[EndOfStoredEventsMessage] = Queue() | ||
self._unique_events: set = set() | ||
self.lock: Lock = Lock() | ||
|
||
def add_message(self, message: str, url: str): | ||
self._process_message(message, url) | ||
|
||
def get_event(self): | ||
return self.events.get() | ||
|
||
def get_notice(self): | ||
return self.notices.get() | ||
|
||
def get_eose_notice(self): | ||
return self.eose_notices.get() | ||
|
||
def has_events(self): | ||
return self.events.qsize() > 0 | ||
|
||
def has_notices(self): | ||
return self.notices.qsize() > 0 | ||
|
||
def has_eose_notices(self): | ||
return self.eose_notices.qsize() > 0 | ||
|
||
def _process_message(self, message: str, url: str): | ||
message_json = json.loads(message) | ||
message_type = message_json[0] | ||
print("\n",message_json) | ||
if message_type == RelayMessageType.EVENT: | ||
subscription_id = message_json[1] | ||
e = message_json[2] | ||
event = Event(e['pubkey'], e['content'], e['created_at'], e['kind'], e['tags'], e['id'], e['sig'], message_json) | ||
with self.lock: | ||
if not event.id in self._unique_events: | ||
self.events.put(EventMessage(event, subscription_id, url)) | ||
self._unique_events.add(event.id) | ||
elif message_type == RelayMessageType.NOTICE: | ||
self.notices.put(NoticeMessage(message_json[1], url)) | ||
elif message_type == RelayMessageType.END_OF_STORED_EVENTS: | ||
self.eose_notices.put(EndOfStoredEventsMessage(message_json[1], url)) | ||
|
||
|
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
[ | ||
[ | ||
"EVENT", | ||
"a3c7c260eede11eda86236a166cdd3a9", | ||
{ | ||
"content": "Hey there 3580", | ||
"created_at": 1683686809, | ||
"id": "cae07f62c4618df2c87702b2ebde5484bd2fdcb092c6d1fad842508e81a82154", | ||
"kind": 1, | ||
"pubkey": "3298f64e6fa15ac8e425bd69f7250d9927f2ce190411e8753618c0dc699d8e14", | ||
"sig": "310c61751dd312b5e3829c5bb7aca29e7d66e9a0bcfce61dc86db8c2d67fb4840356b21079d77f25c2dc9ef637cf11a90a8a7e3982e26baed6411d67f4538718", | ||
"tags": [] | ||
} | ||
], | ||
[ | ||
"EVENT", | ||
"a3c7c260eede11eda86236a166cdd3a9", | ||
{ | ||
"content": "this is a note", | ||
"created_at": 1683685853, | ||
"id": "eb136a50e1c40c7386c456f473894a8de8560368f58014da7c06720e2872f715", | ||
"kind": 1, | ||
"pubkey": "3298f64e6fa15ac8e425bd69f7250d9927f2ce190411e8753618c0dc699d8e14", | ||
"sig": "278c93f7dedb6ae2dfa6c0d039064cecbf61657818b23a32ca42b5f198cf1e272d129794186ed9a148614e320d7fe0747667e84d2757d75ea911937bf1f21a91", | ||
"tags": [] | ||
} | ||
], | ||
[ | ||
"EVENT", | ||
"cf48adfeeedf11edb91d36a166cdd3a9", | ||
{ | ||
"content": "This is a reply", | ||
"created_at": 1683688164, | ||
"id": "78fa48c1394c61254b3c7d2b142f34d37b982eeecdefca41ebaffb800a1a63ee", | ||
"kind": 1, | ||
"pubkey": "3298f64e6fa15ac8e425bd69f7250d9927f2ce190411e8753618c0dc699d8e14", | ||
"sig": "059ac2d173ed3f5db3037b93d81d0dc1fa01d422bc06bbbec24d4b54c081409c3adc9f1cd0435593bb5b6898d4324481851fbb344ee9e3fc8c2ccbb3e9394251", | ||
"tags": [ | ||
[ | ||
"e", | ||
"cae07f62c4618df2c87702b2ebde5484bd2fdcb092c6d1fad842508e81a82154", | ||
"", | ||
"reply" | ||
] | ||
] | ||
} | ||
], | ||
[ | ||
"EVENT", | ||
"cf48adfeeedf11edb91d36a166cdd3a9", | ||
{ | ||
"content": "This is a new note", | ||
"created_at": 1683688221, | ||
"id": "7e191190ddad7e63a976e99253eec0bd2844dfe9b4abf3cbd41878305f6ee1f1", | ||
"kind": 1, | ||
"pubkey": "3298f64e6fa15ac8e425bd69f7250d9927f2ce190411e8753618c0dc699d8e14", | ||
"sig": "fae3a3f69baba0675b0de19823900ec4c35fa507f36f95a340b2a0ac855d4e73ca6bfbce886368b1bb5f227bb8f63b0e5e0abc2d73adce12e5e1ab28bfdf8478", | ||
"tags": [] | ||
} | ||
] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from nostr.key import PrivateKey | ||
# import secp256k1 | ||
|
||
private_key = PrivateKey() | ||
public_key = private_key.public_key | ||
print(f"Private key: {private_key.bech32()}") | ||
print(f"Public key: {public_key.bech32()}") | ||
|
||
with open('keys.txt','r+') as f: | ||
f.write(f"Private key: {private_key.bech32()}\nPublic key: {public_key.bech32()}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import time | ||
import uuid | ||
from nostr.event import EventKind | ||
from nostr.relay_manager import RelayManager | ||
from nostr.key import PublicKey | ||
from nostr.filter import Filter, Filters | ||
|
||
pub_key = PublicKey.from_npub('npub1ljraxpufmzjnfdvsw0tq9kwnypctwxus8n9w388uhkd8h73pzzlqgmdzfy') | ||
filters = Filters([Filter(authors=[pub_key.hex()], kinds=[EventKind.SET_METADATA])]) | ||
subscription_id = uuid.uuid1().hex | ||
|
||
relay_manager = RelayManager() | ||
relay_manager.add_relay("wss://relay.snort.social") | ||
relay_manager.add_relay("wss://relay.damus.io") | ||
relay_manager.add_relay("wss://nostr-pub.wellorder.net") | ||
relay_manager.add_relay("wss://relay.damus.io") | ||
relay_manager.add_relay("wss://nostr1.current.fyi") | ||
relay_manager.add_relay("wss://relay.current.fyi") | ||
relay_manager.add_relay("wss://relay.nostr.bg") | ||
relay_manager.add_subscription_on_all_relays(subscription_id, filters) | ||
|
||
time.sleep(1) | ||
|
||
event_msg = relay_manager.message_pool.get_event() | ||
print(event_msg.event.content) | ||
|
||
relay_manager.close_all_relay_connections() |
Oops, something went wrong.