Skip to content

Commit

Permalink
[!] Fix ENS but suffix is ​​not .eth
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongFuze committed Nov 14, 2024
1 parent 7516806 commit b0bf883
Showing 1 changed file with 90 additions and 6 deletions.
96 changes: 90 additions & 6 deletions src/resolver/domain_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Author: Zella Zhong
Date: 2024-11-13 21:47:39
LastEditors: Zella Zhong
LastEditTime: 2024-11-14 20:58:07
LastEditTime: 2024-11-14 21:57:28
FilePath: /data_service/src/resolver/domain_search.py
Description:
'''
Expand Down Expand Up @@ -71,11 +71,15 @@ def trim_name(name):
# Check if suffix is in the EXT enum
if suffix in EXT.__members__:
return _name
else:
return _name
elif len(parts) > 2:
_name = parts[0]
suffix = parts[-1]
if suffix in EXT.__members__:
return _name
else:
return _name

return process_name

Expand All @@ -89,6 +93,13 @@ async def ensname_search(name: str) -> typing.List[AvailableDomain]:
.options(load_only(*ensname_fields))
.filter(EnsnameModel.label_name == name)
)
if name.find('.') != -1:
ensname_sql = (
select(EnsnameModel)
.options(load_only(*ensname_fields))
.filter(EnsnameModel.name == name)
)

ensname_result = await s.execute(ensname_sql)
ensname_records = ensname_result.scalars().all()
for row in ensname_records:
Expand Down Expand Up @@ -121,6 +132,9 @@ async def ensname_search(name: str) -> typing.List[AvailableDomain]:
return exist_domains

async def lens_search(name: str) -> typing.List[AvailableDomain]:
if name.find('.') != -1:
if not name.endswith(EXT.lens.value):
return []
exist_domains = []
async with get_session() as s:
lens_fields = [getattr(LensV2Profile, f) for f in ["name", "namespace", "handle_name"]]
Expand Down Expand Up @@ -178,6 +192,8 @@ async def farcaster_search(name: str) -> typing.List[AvailableDomain]:
return exist_domains

async def clusters_search(name: str) -> typing.List[AvailableDomain]:
if name.find('.') != -1:
return []
exist_domains = []
async with get_session() as s:
clusters_fields = [getattr(ClustersProfile, f) for f in ["cluster_name", "name", "delete_time"]]
Expand Down Expand Up @@ -210,6 +226,9 @@ async def clusters_search(name: str) -> typing.List[AvailableDomain]:
return exist_domains

async def basename_search(name: str) -> typing.List[AvailableDomain]:
if name.find('.') != -1:
if not name.endswith(EXT.baseeth.value):
return []
exist_domains = []
async with get_session() as s:
# ============================ ENS =====================================
Expand Down Expand Up @@ -241,6 +260,9 @@ async def basename_search(name: str) -> typing.List[AvailableDomain]:
return exist_domains

async def sns_search(name: str) -> typing.List[AvailableDomain]:
if name.find('.') != -1:
if not name.endswith(EXT.sol.value):
return []
exist_domains = []
async with get_session() as s:
# ============================ ENS =====================================
Expand All @@ -264,6 +286,12 @@ async def sns_search(name: str) -> typing.List[AvailableDomain]:
return exist_domains

async def unstoppabledomains_search(name: str) -> typing.List[AvailableDomain]:
if '.' in name:
ud_exts = EXTENSION[Platform.unstoppabledomains]
ud_exts = ["." + x.value for x in ud_exts]
if not name.endswith(tuple(ud_exts)):
return []

headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + setting.UPSTREAM["unstoppabledomains_api"]["api_key"],
Expand Down Expand Up @@ -320,6 +348,17 @@ async def unstoppabledomains_search(name: str) -> typing.List[AvailableDomain]:
return exist_domains

async def spaceid_search(name: str) -> typing.List[AvailableDomain]:
if name.find('.') != -1:
spaceid_exts = [
EXT.bnb, EXT.cake, EXT.floki, EXT.burger, EXT.zeta,
EXT.mode, EXT.arb, EXT.taiko, EXT.mint, EXT.zkf,
EXT.manta, EXT.ll, EXT.gno, EXT.merlin, EXT.alien,
EXT.tomo, EXT.ail
]
spaceid_exts = ["." + x.value for x in spaceid_exts]
if not name.endswith(tuple(spaceid_exts)):
return []

QUERY_BY_EXACT = """
query domainsByName($name: String!) {
domains(input: {query: $name, first: 200,
Expand Down Expand Up @@ -429,6 +468,10 @@ async def spaceid_search(name: str) -> typing.List[AvailableDomain]:
return exist_domains

async def dotbit_search(name: str) -> typing.List[AvailableDomain]:
if name.find('.') != -1:
if not name.endswith(EXT.bit.value):
return []

dotbit_fullname = "{}.{}".format(name, EXT.bit.value)
param = {"account": dotbit_fullname}
json_params = json.dumps(param)
Expand Down Expand Up @@ -564,13 +607,18 @@ async def domain_search(name: str) -> typing.List[AvailableDomain]:

available_domains = []
exist_tld_map = {}
exist_name_map = {}
for exist in exist_result:
exist_tld_map[(exist.platform, exist.tld)] = exist
exist_name_map[exist.name] = exist

unique_names = set()
for domain_order in return_order:
if domain_order == Platform.clusters:
if (domain_order, EXT.clustersroot.value) in exist_tld_map:
available_domains.append(exist_tld_map[(domain_order, EXT.clustersroot.value)])
exact = exist_tld_map[(domain_order, EXT.clustersroot.value)]
available_domains.append(exact)
unique_names.add(exact.name)
else:
available_domains.append(AvailableDomain(
platform=domain_order,
Expand All @@ -582,10 +630,14 @@ async def domain_search(name: str) -> typing.List[AvailableDomain]:
))
elif domain_order == Platform.farcaster:
if (domain_order, EXT.eth.value) in exist_tld_map:
available_domains.append(exist_tld_map[(domain_order, EXT.eth.value)])
exact = exist_tld_map[(domain_order, EXT.eth.value)]
available_domains.append(exact)
unique_names.add(exact.name)

if (domain_order, "") in exist_tld_map:
available_domains.append(exist_tld_map[(domain_order, "")])
exact = exist_tld_map[(domain_order, "")]
available_domains.append(exact)
unique_names.add(exact.name)
else:
available_domains.append(AvailableDomain(
platform=domain_order,
Expand All @@ -599,7 +651,9 @@ async def domain_search(name: str) -> typing.List[AvailableDomain]:
domain_name = "{}.{}".format(trimed_name, EXT.lens.value)
if len(trimed_name) > 4:
if (domain_order, EXT.lens.value) in exist_tld_map:
available_domains.append(exist_tld_map[(domain_order, EXT.lens.value)])
exact = exist_tld_map[(domain_order, EXT.lens.value)]
available_domains.append(exact)
unique_names.add(exact.name)
else:
available_domains.append(AvailableDomain(
platform=domain_order,
Expand All @@ -618,13 +672,25 @@ async def domain_search(name: str) -> typing.List[AvailableDomain]:
expired_at=None,
status=DomainStatus.unavailable
))
elif domain_order == Platform.ens:
required_exts = EXTENSION.get(domain_order)
if not required_exts:
continue
for ext in required_exts:
if (domain_order, ext.value) in exist_tld_map:
exact = exist_tld_map[(domain_order, ext.value)]
available_domains.append(exact)
unique_names.add(exact.name)
# NOTICE: ENS: do not add availability=True at here
else:
required_exts = EXTENSION.get(domain_order)
if not required_exts:
continue
for ext in required_exts:
if (domain_order, ext.value) in exist_tld_map:
available_domains.append(exist_tld_map[(domain_order, ext.value)])
exact = exist_tld_map[(domain_order, ext.value)]
available_domains.append(exact)
unique_names.add(exact.name)
else:
domain_name = "{}.{}".format(trimed_name, ext.value)
available_domains.append(AvailableDomain(
Expand All @@ -635,4 +701,22 @@ async def domain_search(name: str) -> typing.List[AvailableDomain]:
expired_at=None,
status=DomainStatus.available
))

extra_names = list(set(list(exist_name_map.keys())) - unique_names)
if extra_names:
# special for domain in ENS but not .eth
for extra_name in extra_names:
available_domains.insert(0, exist_name_map[extra_name])
else:
if (Platform.ens, EXT.eth.value) not in exist_tld_map:
domain_name = "{}.{}".format(trimed_name, EXT.eth.value)
available_domains.append(AvailableDomain(
platform=domain_order,
name=domain_name,
tld=EXT.eth.value,
availability=True,
expired_at=None,
status=DomainStatus.available
))

return available_domains

0 comments on commit b0bf883

Please sign in to comment.