work on flood fill network
This commit is contained in:
parent
b5fe4453ed
commit
299980f126
4 changed files with 90 additions and 0 deletions
1
src/streamfill/__init__.py
Normal file
1
src/streamfill/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from .extracted25519 import extract_ed25519_from_onion_address
|
14
src/streamfill/extracted25519.py
Normal file
14
src/streamfill/extracted25519.py
Normal 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
|
22
src/streamfill/neighbors.py
Normal file
22
src/streamfill/neighbors.py
Normal 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]
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue