Skip to content

Commit

Permalink
add single function
Browse files Browse the repository at this point in the history
  • Loading branch information
larisa17 committed Dec 4, 2024
1 parent 0bc8316 commit 9bf8de5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 107 deletions.
103 changes: 50 additions & 53 deletions api/v2/api/api_stamps.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ninja_extra.exceptions import APIException

import api_logging as logging
from account.models import Community, Nonce
from account.models import Account, Community, Nonce
from ceramic_cache.models import CeramicCache
from registry.api.schema import (
CursorPaginatedStampCredentialResponse,
Expand Down Expand Up @@ -61,6 +61,52 @@
log = logging.getLogger(__name__)


async def a_get_score(
payload: SubmitPassportPayload, account: Account
) -> DetailedScoreResponseV2:
address_lower = payload.address.lower()
if not is_valid_address(address_lower):
raise InvalidAddressException()

try:
scorer_id = get_scorer_id(payload)
except Exception as e:
raise e

# Get community object
user_community = await aget_scorer_by_id(scorer_id, account)

# Verify the signer
if payload.signature or community_requires_signature(user_community):
if get_signer(payload.nonce, payload.signature).lower() != address_lower:
raise InvalidSignerException()

# Verify nonce
if not await Nonce.ause_nonce(payload.nonce):
log.error(
"Invalid nonce %s for address %s",
payload.nonce,
payload.address,
)
raise InvalidNonceException()

# Create an empty passport instance, only needed to be able to create a pending Score
# The passport will be updated by the score_passport task
db_passport, _ = await Passport.objects.aupdate_or_create(
address=payload.address.lower(),
community=user_community,
)

score, _ = await Score.objects.select_related("passport").aget_or_create(
passport=db_passport,
defaults=dict(score=None, status=Score.Status.PROCESSING),
)

await ascore_passport(user_community, db_passport, payload.address, score)
await score.asave()
return DetailedScoreResponseV2.from_orm(score)


@api_router.get(
"/stamps/{scorer_id}/score/{address}",
auth=aapi_key,
Expand All @@ -82,59 +128,10 @@ async def a_submit_passport(request, scorer_id: int, address: str) -> V2ScoreRes
if not request.api_key.submit_passports:
raise InvalidAPIKeyPermissions()

submit_passport_payload = SubmitPassportPayload(
address=address, scorer_id=str(scorer_id)
)
address_lower = address.lower()
if not is_valid_address(address_lower):
raise InvalidAddressException()

try:
scorer_id = get_scorer_id(submit_passport_payload)
except Exception as e:
raise e

# Get community object
user_community = await aget_scorer_by_id(scorer_id, request.auth)

# Verify the signer
if submit_passport_payload.signature or community_requires_signature(
user_community
):
if (
get_signer(
submit_passport_payload.nonce, submit_passport_payload.signature
).lower()
!= address_lower
):
raise InvalidSignerException()

# Verify nonce
if not await Nonce.ause_nonce(submit_passport_payload.nonce):
log.error(
"Invalid nonce %s for address %s",
submit_passport_payload.nonce,
submit_passport_payload.address,
)
raise InvalidNonceException()

# Create an empty passport instance, only needed to be able to create a pending Score
# The passport will be updated by the score_passport task
db_passport, _ = await Passport.objects.aupdate_or_create(
address=submit_passport_payload.address.lower(),
community=user_community,
)

score, _ = await Score.objects.select_related("passport").aget_or_create(
passport=db_passport,
defaults=dict(score=None, status=Score.Status.PROCESSING),
)

await ascore_passport(
user_community, db_passport, submit_passport_payload.address, score
v1_score = await a_get_score(
SubmitPassportPayload(address=address, scorer_id=str(scorer_id)),
request.auth,
)
await score.asave()
v1_score = DetailedScoreResponseV2.from_orm(score)
threshold = v1_score.evidence.threshold if v1_score.evidence else "20"
score = v1_score.evidence.rawScore if v1_score.evidence else v1_score.score

Expand Down
64 changes: 11 additions & 53 deletions api/v2/aws_lambdas/stamp_score_GET.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
from registry.utils import (
get_signer,
)
from v2.schema import DetailedScoreResponseV2, V2ScoreResponse
from v2.api.api_stamps import a_get_score
from v2.schema import V2ScoreResponse

# Now this script or any imported module can use any part of Django it needs.
# from myapp import models
Expand All @@ -46,61 +47,18 @@ def _handler(event, _context, request, user_account, body):
address = split_url[-1]
scorer_id = split_url[-3]

submit_passport_payload = SubmitPassportPayload(
address=address, scorer_id=str(scorer_id)
)
address_lower = address.lower()
if not is_valid_address(address_lower):
raise InvalidAddressException()

try:
scorer_id = get_scorer_id(submit_passport_payload)
except Exception as e:
raise e

# Get community object
user_community = get_scorer_by_id(scorer_id, user_account)

# Verify the signer
if submit_passport_payload.signature or community_requires_signature(
user_community
):
if (
get_signer(
submit_passport_payload.nonce, submit_passport_payload.signature
).lower()
!= address_lower
):
raise InvalidSignerException()

# Verify nonce
if not Nonce.use_nonce(submit_passport_payload.nonce):
log.error(
"Invalid nonce %s for address %s",
submit_passport_payload.nonce,
submit_passport_payload.address,
)
raise InvalidNonceException()

# Create an empty passport instance, only needed to be able to create a pending Score
# The passport will be updated by the score_passport task
db_passport, _ = Passport.objects.update_or_create(
address=address_lower,
community=user_community,
)
score, _ = Score.objects.select_related("passport").get_or_create(
passport=db_passport,
defaults=dict(score=None, status=Score.Status.PROCESSING),
)
async_to_sync(
ascore_passport(
user_community, db_passport, submit_passport_payload.address, score
# THIS
v1_score = async_to_sync(
a_get_score(
SubmitPassportPayload(
address=address,
scorer_id=scorer_id,
),
user_account,
)
)
score.save()
v1_score = DetailedScoreResponseV2.from_orm(score)

threshold = v1_score.evidence.threshold if v1_score.evidence else "20"
score = v1_score.evidence.rawScore if v1_score.evidence else v1_score.score

return V2ScoreResponse(
address=v1_score.address,
Expand Down
5 changes: 4 additions & 1 deletion api/v2/aws_lambdas/tests/test_stamp_score_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ def test_successful_authentication(
assert body["threshold"] == "20.0"

assert body["error"] is None
assert body["stamp_scores"] == {"Ens": "0.408", "Google": "0.525"}
assert body["stamps"] == {
"Ens": {"score": "0.408", "dedup": False, "expiration_date": None},
"Google": {"score": "0.525", "dedup": False, "expiration_date": None},
}
# We just check that something != None was recorded for the last timestamp
assert body["expiration_timestamp"] is not None

Expand Down

0 comments on commit 9bf8de5

Please sign in to comment.