bech32 working

This commit is contained in:
callebtc
2023-02-13 22:11:23 +01:00
parent f598039e44
commit 0328fc35f5
2 changed files with 34 additions and 23 deletions

43
main.py
View File

@@ -27,11 +27,11 @@ async def dm():
f"\nFrom {event.public_key[:3]}..{event.public_key[-3:]}: {decrypted_content}" f"\nFrom {event.public_key[:3]}..{event.public_key[-3:]}: {decrypted_content}"
) )
client = NostrClient(privatekey_hex=pk) client = NostrClient(private_key=pk)
if not pk: if not pk:
print(f"Your private key: {client.private_key.hex()}") print(f"Your private key: {client.private_key.bech32()}")
print(f"Your public key: {client.public_key.hex()}") print(f"Your public key: {client.public_key.bech32()}")
# await asyncio.sleep(1) # await asyncio.sleep(1)
t = threading.Thread( t = threading.Thread(
@@ -43,16 +43,20 @@ async def dm():
) )
t.start() t.start()
to_pubk_hex = ( pubkey_to_str = (
input("Enter other pubkey to post to (enter nothing to DM yourself): ") input("Enter other pubkey to DM to (enter nothing to DM yourself): ")
or client.public_key.hex() or client.public_key.hex()
) )
print(f"Subscribing to DMs to {to_pubk_hex}") if pubkey_to_str.startswith("npub"):
pubkey_to = PublicKey().from_npub(pubkey_to_str)
else:
pubkey_to = PublicKey(bytes.fromhex(pubkey_to_str))
print(f"Sending DMs to {pubkey_to.bech32()}")
while True: while True:
print_status(client) print_status(client)
await asyncio.sleep(1) await asyncio.sleep(1)
msg = input("\nEnter message: ") msg = input("\nEnter message: ")
client.dm(msg, PublicKey(bytes.fromhex(to_pubk_hex))) client.dm(msg, pubkey_to)
async def post(): async def post():
@@ -67,20 +71,23 @@ async def post():
f"\nFrom {event.public_key[:3]}..{event.public_key[-3:]}: {event.content}" f"\nFrom {event.public_key[:3]}..{event.public_key[-3:]}: {event.content}"
) )
sender_client = NostrClient(privatekey_hex=pk) sender_client = NostrClient(private_key=pk)
# await asyncio.sleep(1) # await asyncio.sleep(1)
to_pubk_hex = ( pubkey_to_str = (
input( input(
"Enter other pubkey (enter nothing to read your own posts, enter * for all): " "Enter other pubkey (enter nothing to read your own posts, enter * for all): "
) )
or sender_client.public_key.hex() or sender_client.public_key.hex()
) )
if to_pubk_hex == "*": if pubkey_to_str == "*":
to_pubk = None pubkey_to = None
elif pubkey_to_str.startswith("npub"):
pubkey_to = PublicKey().from_npub(pubkey_to_str)
else: else:
print(f"Subscribing to posts by {to_pubk_hex}") pubkey_to = PublicKey(bytes.fromhex(pubkey_to_str))
to_pubk = PublicKey(bytes.fromhex(to_pubk_hex))
print(f"Subscribing to posts by {pubkey_to.bech32() if pubkey_to else 'everyone'}")
filters = { filters = {
"since": int( "since": int(
@@ -93,7 +100,7 @@ async def post():
t = threading.Thread( t = threading.Thread(
target=sender_client.get_post, target=sender_client.get_post,
args=( args=(
to_pubk, pubkey_to,
callback, callback,
filters, filters,
), ),
@@ -107,9 +114,9 @@ async def post():
sender_client.post(msg) sender_client.post(msg)
if input("Enter '1' for DM, '2' for Posts (Default: 1):") or 1 == 1: if input("Enter '1' for DM, '2' for Posts (Default: 1): ") == "2":
# write a DM and receive DMs
asyncio.run(dm())
else:
# make a post and subscribe to posts # make a post and subscribe to posts
asyncio.run(post()) asyncio.run(post())
else:
# write a DM and receive DMs
asyncio.run(dm())

View File

@@ -30,8 +30,8 @@ class NostrClient:
private_key: PrivateKey private_key: PrivateKey
public_key: PublicKey public_key: PublicKey
def __init__(self, privatekey_hex: str = "", relays: List[str] = [], connect=True): def __init__(self, private_key: str = "", relays: List[str] = [], connect=True):
self.generate_keys(privatekey_hex) self.generate_keys(private_key)
if len(relays): if len(relays):
self.relays = relays self.relays = relays
@@ -48,9 +48,13 @@ class NostrClient:
def close(self): def close(self):
self.relay_manager.close_connections() self.relay_manager.close_connections()
def generate_keys(self, privatekey_hex: str = None): def generate_keys(self, private_key: str = None):
pk = bytes.fromhex(privatekey_hex) if privatekey_hex else None if private_key.startswith("nsec"):
self.private_key = PrivateKey(pk) self.private_key = PrivateKey.from_nsec(private_key)
elif private_key:
self.private_key = PrivateKey(bytes.fromhex(private_key))
else:
self.private_key = PrivateKey() # generate random key
self.public_key = self.private_key.public_key self.public_key = self.private_key.public_key
def post(self, message: str): def post(self, message: str):