Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shuffle new relays #41

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ bip32
nostr
colorama
python-bitcoinlib
requests
random
3 changes: 2 additions & 1 deletion src/coordinator/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from colorama import Fore

from src.utils.nostr_utils import add_relays, construct_and_publish_event, generate_nostr_message, init_relay_manager, read_nsec, read_public_keys
from src.utils.nostr_utils import shuffle_relays, add_relays, construct_and_publish_event, generate_nostr_message, init_relay_manager, read_nsec, read_public_keys
from src.utils.payload import is_valid_json, is_valid_payload, PayloadKeys
from src.coordinator.wallet import add_xpub, create_wallet, is_valid_command, get_address, save_nonce, start_spend, save_signature
from src.coordinator.db import DB
Expand Down Expand Up @@ -43,6 +43,7 @@ def run():
# start up the db
db = DB('src/coordinator/db.json')

shuffle_relays()
relay_manager = add_relays()
nostr_private_key, nostr_public_key = read_nsec('src/coordinator/nsec.txt')

Expand Down
23 changes: 21 additions & 2 deletions src/utils/nostr_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import ssl
import time
import uuid
import requests
import sys
import random

from nostr.event import Event, EventKind
from nostr.filter import Filter, Filters
Expand All @@ -13,6 +16,22 @@
from src.utils.payload import PayloadKeys

NOSTR_RELAYS = ["wss://nostr-pub.wellorder.net", "wss://relay.damus.io"]
NOSTR_WATCH = "https://api.nostr.watch/v1/online"

def shuffle_relays():
if (input(f"Default relays: {NOSTR_RELAYS}\nShuffle more relays? y/n ")) == 'y':
r = (requests.get(NOSTR_WATCH, {})).json()
while(1):
new_relays = [x for x in r[:15] if 'damus' not in x and 'nostr-pub.wellorder' not in x]

if (ans := input(f"New online relays found:\n{new_relays}\nIs that OK? y/n/r (n to go back to original, r to reshuffle) ")) == 'y':
for val in new_relays:
NOSTR_RELAYS.append(val)
break
elif ans == 'r':
random.shuffle(r)
elif ans == 'n':
break

def add_relays():
relay_manager = RelayManager()
Expand Down Expand Up @@ -77,7 +96,7 @@ def read_nsec(nsec_file_name):
public_key = private_key.public_key.hex()
logging.info("[nostr] My public key: %s", public_key)
return private_key, public_key
except(error):
except Exception:
logging.error("[nostr] Unexpected error reading nsec from %s", nsec_file_name)
sys.exit(1)

Expand All @@ -87,7 +106,7 @@ def read_public_keys(file_name):
try:
lines = f.readlines()
return [line.strip() for line in lines]
except(error):
except Exception:
logging.error("[nostr] Unexpected error reading public keys from %s", file_name)
sys.exit(1)