1
0

LNPeerAddr: validate arguments

no longer subclassing NamedTuple (as it is difficult to do validation then...)
This commit is contained in:
SomberNight
2019-11-26 00:15:33 +01:00
parent edba59ef54
commit 13d6997355
4 changed files with 53 additions and 20 deletions

View File

@@ -658,14 +658,31 @@ class LnGlobalFeatures(IntFlag):
LN_GLOBAL_FEATURES_KNOWN_SET = set(LnGlobalFeatures)
class LNPeerAddr(NamedTuple):
host: str
port: int
pubkey: bytes
class LNPeerAddr:
def __init__(self, host: str, port: int, pubkey: bytes):
assert isinstance(host, str), repr(host)
assert isinstance(port, int), repr(port)
assert isinstance(pubkey, bytes), repr(pubkey)
try:
net_addr = NetAddress(host, port) # this validates host and port
except Exception as e:
raise ValueError(f"cannot construct LNPeerAddr: invalid host or port (host={host}, port={port})") from e
# note: not validating pubkey as it would be too expensive:
# if not ECPubkey.is_pubkey_bytes(pubkey): raise ValueError()
self.host = host
self.port = port
self.pubkey = pubkey
self._net_addr_str = str(net_addr)
def __str__(self):
host_and_port = str(NetAddress(self.host, self.port))
return '{}@{}'.format(self.pubkey.hex(), host_and_port)
return '{}@{}'.format(self.pubkey.hex(), self.net_addr_str())
def __repr__(self):
return f'<LNPeerAddr host={self.host} port={self.port} pubkey={self.pubkey.hex()}>'
def net_addr_str(self) -> str:
return self._net_addr_str
def get_compressed_pubkey_from_bech32(bech32_pubkey: str) -> bytes: