Add NSID support.

Changed files
+116
src
atpasser
+116
src/atpasser/nsid/__init__.py
···
+
import dns.resolver, requests
+
+
from atpasser import did
+
+
+
class NSID:
+
"""
+
A class representing a NSID.
+
+
+
Attributes:
+
nsid (str): The NSID URI.
+
"""
+
+
def __init__(self, nsid: str) -> None:
+
"""
+
Initalizes an NSID object.
+
+
Parameters:
+
nsid (str): The NSID.
+
"""
+
+
if "#" in nsid:
+
nsidWithoutFragment, fragment = nsid.split("#")
+
else:
+
nsidWithoutFragment, fragment = nsid, None
+
+
if not set([x for x in nsidWithoutFragment]).issubset(
+
set([chr(i) for i in range(0x80)])
+
):
+
raise ValueError("nsid contains non-ascii chars")
+
+
if len(nsidWithoutFragment) > 317:
+
raise ValueError("length longer than 317 chars")
+
+
segments = nsidWithoutFragment.split(".")
+
+
if nsidWithoutFragment.startswith(".") or nsidWithoutFragment.endswith("."):
+
raise ValueError("invalid, but is that undocumented lol")
+
+
if len(segments) < 3:
+
raise ValueError("less than 3 segments")
+
+
domainAuthority = [segment.lower() for segment in segments[0:-1]]
+
+
if len(".".join(domainAuthority)) > 253:
+
raise ValueError("domain authority more than 253 chars")
+
+
for segment in domainAuthority:
+
print(segment)
+
if len(segment) > 63 or segment == "":
+
raise ValueError("segment not in 1~63 chars")
+
if not set(segment).issubset(set("abcdefghijklmnopqrstuvwxyz0123456789-")):
+
raise ValueError("domain authority contains invalid chars")
+
if segment.startswith("-") or segment.endswith("-"):
+
raise ValueError("segment starts or ends with hyphen")
+
if segments[0][0] in "0123456789":
+
raise ValueError("tld starts with a digit")
+
+
self.domainAuthority = domainAuthority
+
self.domainAuthorityAsText = ".".join(domainAuthority)
+
+
name = segments[-1]
+
+
if name == "" or len(name) > 63:
+
raise ValueError("name null or longer than 63 chars")
+
+
if not set(name).issubset(
+
set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
+
):
+
raise ValueError("name contains invalid chars")
+
+
if name[0] in "0123456789":
+
raise ValueError("first char of name is a digit")
+
+
self.name = name
+
+
if fragment != None:
+
+
if fragment == "" or len(fragment) > 63:
+
raise ValueError("fragment null or longer than 63 chars")
+
+
if not set(fragment).issubset(
+
set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
+
):
+
raise ValueError("fragment contains invalid chars")
+
+
if fragment[0] in "0123456789":
+
raise ValueError("first char of fragment is a digit")
+
+
self.fragment = fragment
+
+
else:
+
self.fragment = None
+
+
self.nsid = ".".join(domainAuthority) + f".{name}" + f"#{fragment}"
+
+
def __str__(self) -> str:
+
"""
+
+
Convert the NSID to a string by given the URI.
+
"""
+
return self.nsid
+
+
def __eq__(self, value: object, /) -> bool:
+
"""
+
+
Check if the 2 values are exactly the same.
+
"""
+
+
if isinstance(value, NSID):
+
+
return str(self) == str(value)
+
else:
+
+
raise TypeError