···
1
+
import dns.resolver, requests
3
+
from atpasser import did
8
+
A class representing a NSID.
12
+
nsid (str): The NSID URI.
15
+
def __init__(self, nsid: str) -> None:
17
+
Initalizes an NSID object.
20
+
nsid (str): The NSID.
24
+
nsidWithoutFragment, fragment = nsid.split("#")
26
+
nsidWithoutFragment, fragment = nsid, None
28
+
if not set([x for x in nsidWithoutFragment]).issubset(
29
+
set([chr(i) for i in range(0x80)])
31
+
raise ValueError("nsid contains non-ascii chars")
33
+
if len(nsidWithoutFragment) > 317:
34
+
raise ValueError("length longer than 317 chars")
36
+
segments = nsidWithoutFragment.split(".")
38
+
if nsidWithoutFragment.startswith(".") or nsidWithoutFragment.endswith("."):
39
+
raise ValueError("invalid, but is that undocumented lol")
41
+
if len(segments) < 3:
42
+
raise ValueError("less than 3 segments")
44
+
domainAuthority = [segment.lower() for segment in segments[0:-1]]
46
+
if len(".".join(domainAuthority)) > 253:
47
+
raise ValueError("domain authority more than 253 chars")
49
+
for segment in domainAuthority:
51
+
if len(segment) > 63 or segment == "":
52
+
raise ValueError("segment not in 1~63 chars")
53
+
if not set(segment).issubset(set("abcdefghijklmnopqrstuvwxyz0123456789-")):
54
+
raise ValueError("domain authority contains invalid chars")
55
+
if segment.startswith("-") or segment.endswith("-"):
56
+
raise ValueError("segment starts or ends with hyphen")
57
+
if segments[0][0] in "0123456789":
58
+
raise ValueError("tld starts with a digit")
60
+
self.domainAuthority = domainAuthority
61
+
self.domainAuthorityAsText = ".".join(domainAuthority)
65
+
if name == "" or len(name) > 63:
66
+
raise ValueError("name null or longer than 63 chars")
68
+
if not set(name).issubset(
69
+
set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
71
+
raise ValueError("name contains invalid chars")
73
+
if name[0] in "0123456789":
74
+
raise ValueError("first char of name is a digit")
78
+
if fragment != None:
80
+
if fragment == "" or len(fragment) > 63:
81
+
raise ValueError("fragment null or longer than 63 chars")
83
+
if not set(fragment).issubset(
84
+
set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
86
+
raise ValueError("fragment contains invalid chars")
88
+
if fragment[0] in "0123456789":
89
+
raise ValueError("first char of fragment is a digit")
91
+
self.fragment = fragment
94
+
self.fragment = None
96
+
self.nsid = ".".join(domainAuthority) + f".{name}" + f"#{fragment}"
98
+
def __str__(self) -> str:
101
+
Convert the NSID to a string by given the URI.
105
+
def __eq__(self, value: object, /) -> bool:
108
+
Check if the 2 values are exactly the same.
111
+
if isinstance(value, NSID):
113
+
return str(self) == str(value)