Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

canonical flag #172

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/nameguard/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
FakeEthNameCheckStatus,
FakeEthNameCheckResult,
ImpersonationStatus,
ConfusableGuardReport,
)

from .request import (
Expand Down
6 changes: 5 additions & 1 deletion api/nameguard/models/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ class BulkNameGuardBulkReport(BaseModel):
results: list[ConsolidatedNameGuardReport]


class ConfusableGuardReport(ConsolidatedGraphemeGuardReport):
is_canonical: bool = Field(description='Whether this confusable is canonical for the grapheme.')


class GraphemeGuardReport(ConsolidatedGraphemeGuardReport):
checks: list[GenericCheckResult] = Field(
description='A list of checks that were performed on the grapheme.')
Expand All @@ -276,7 +280,7 @@ def grapheme_link_name(self) -> str:
def codepoints(self) -> list[str]:
return [f'U+{ord(c):04X}' for c in self.grapheme]

confusables: list[ConsolidatedGraphemeGuardReport] = Field(
confusables: list[ConfusableGuardReport] = Field(
description='A list graphemes that can be confused with the analyzed grapheme. '
'The list does not contain the analyzed grapheme. '
'A canonical form of the grapheme is the first element of the list, if it is known. '
Expand Down
17 changes: 12 additions & 5 deletions api/nameguard/nameguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
FakeEthNameCheckResult,
ImpersonationStatus,
ConsolidatedNameGuardReport,
Rating
Rating,
ConfusableGuardReport,
)
from nameguard.provider import get_nft_metadata
from nameguard.utils import (
Expand Down Expand Up @@ -299,6 +300,12 @@ def inspect_grapheme(self, grapheme: str) -> GraphemeGuardReport:
grapheme_analysis = label_analysis.graphemes[0]
grapheme_checks = [check(grapheme_analysis) for check in GRAPHEME_CHECKS + [c[0] for c in DNA_CHECKS]]

if grapheme_analysis.confusables_canonical:
canonical = self._inspect_confusable(grapheme_analysis.confusables_canonical)
canonical.is_canonical = True
else:
canonical = None

return GraphemeGuardReport(
normalization=GraphemeNormalization.NORMALIZED
if any(check.status == CheckStatus.PASS and check.check is Check.NORMALIZED
Expand All @@ -313,18 +320,17 @@ def inspect_grapheme(self, grapheme: str) -> GraphemeGuardReport:
risk_count=count_risks(grapheme_checks),
highest_risk=get_highest_risk(grapheme_checks),
checks=sorted(grapheme_checks, reverse=True),
confusables=([self._inspect_confusable(grapheme_analysis.confusables_canonical)]
if grapheme_analysis.confusables_canonical else []) +
confusables=([canonical] if canonical else []) +
([self._inspect_confusable(c)
for c in grapheme_analysis.confusables_other]
if grapheme_analysis.confusables_other else []),
canonical_grapheme=label_analysis.canonical_label,
grapheme_description=grapheme_analysis.description,
)

def _inspect_confusable(self, grapheme: InspectorConfusableGraphemeResult) -> ConsolidatedGraphemeGuardReport:
def _inspect_confusable(self, grapheme: InspectorConfusableGraphemeResult) -> ConfusableGuardReport:
grapheme_checks = [check(grapheme) for check in GRAPHEME_CHECKS + [c[0] for c in DNA_CHECKS]]
return ConsolidatedGraphemeGuardReport(
return ConfusableGuardReport(
normalization=GraphemeNormalization.NORMALIZED
if any(check.status == CheckStatus.PASS and check.check is Check.NORMALIZED
for check in grapheme_checks)
Expand All @@ -338,6 +344,7 @@ def _inspect_confusable(self, grapheme: InspectorConfusableGraphemeResult) -> Co
risk_count=count_risks(grapheme_checks),
highest_risk=get_highest_risk(grapheme_checks),
grapheme_description=grapheme.description,
is_canonical=False,
)

async def secure_primary_name(self, address: str, network_name: str) -> SecurePrimaryNameResult:
Expand Down
2 changes: 2 additions & 0 deletions api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ def test_inspect_grapheme(test_client):
assert check['check_name'][0].isupper() and '_' not in check['check_name']

assert res_json['canonical_grapheme'] in [confusable['grapheme'] for confusable in res_json['confusables']]
assert res_json['confusables'][0]['is_canonical']
assert not res_json['confusables'][1]['is_canonical']

def test_inspect_grapheme_multi(test_client):
response = test_client.get(f'/inspect-grapheme/aś')
Expand Down