work on flood fill network

This commit is contained in:
Kevin Froman 2020-10-24 08:07:54 +00:00
parent b5fe4453ed
commit 299980f126
4 changed files with 90 additions and 0 deletions

View file

@ -0,0 +1 @@
from .extracted25519 import extract_ed25519_from_onion_address

View file

@ -0,0 +1,14 @@
from base64 import b32decode
from typing import TYPE_CHECKING
from onionrutils.bytesconverter import str_to_bytes
if TYPE_CHECKING:
from onionrtypes import Ed25519PublicKeyBytes, OnionAddressString
def extract_ed25519_from_onion_address(
address: OnionAddressString) -> Ed25519PublicKeyBytes:
address = str_to_bytes(address).replace(b'.onion', b'').upper()
ed25519 = b32decode(address)[:-3]
return ed25519

View file

@ -0,0 +1,22 @@
from onionrtypes import OnionAddressString
from typing import Iterable
from .extracted25519 import extract_ed25519_from_onion_address
def identify_neighbors(
address: OnionAddressString,
peers: Iterable[OnionAddressString],
closest_n: int) -> OnionAddressString:
address = extract_ed25519_from_onion_address(address)
address_int = int.from_bytes(address, "big")
def _calc_closeness(y):
return abs(address_int - int.from_bytes(y, "big"))
peer_ed_keys = list(map(extract_ed25519_from_onion_address, peers))
differences = list(map(_calc_closeness, peer_ed_keys))
return sorted(differences)[:closest_n]