diff --git a/volara_proof/extract/__init__.py b/volara_proof/extract/__init__.py index cc85e87..0b0a10f 100644 --- a/volara_proof/extract/__init__.py +++ b/volara_proof/extract/__init__.py @@ -1,7 +1,19 @@ import zipfile +import json +from volara_proof.models.user_data import UserData from volara_proof.buffers.tweets import Tweets +def extract_user_data(zip_file_path: str) -> UserData | None: + with zipfile.ZipFile(zip_file_path, "r") as zip_ref: + file_names = zip_ref.namelist() + if "user_data.json" in file_names: + with zip_ref.open("user_data.json", "r") as file: + return UserData(**json.load(file)) + else: + return None + + def extract_data(zip_file_path: str): """ Extracts the data from the zip file diff --git a/volara_proof/models/user_data.py b/volara_proof/models/user_data.py new file mode 100644 index 0000000..8e4597f --- /dev/null +++ b/volara_proof/models/user_data.py @@ -0,0 +1,11 @@ +""" +Contains the dataclass models for the Tweet data structure +""" + +from dataclasses import dataclass + + +@dataclass +class UserData: + handle: str + wallet_address: str diff --git a/volara_proof/proofs/proof.py b/volara_proof/proofs/proof.py index 34bea99..b6bea08 100644 --- a/volara_proof/proofs/proof.py +++ b/volara_proof/proofs/proof.py @@ -1,19 +1,26 @@ import copy -from volara_proof.extract import extract_data +from volara_proof.extract import extract_data, extract_user_data from volara_proof.proofs.proof_of_quality import proof_of_quality from volara_proof.models.proof_response import ProofResponse from volara_proof.models.proof_config import ProofConfig from volara_proof.storage.rewards import RewardsStorage +from volara_proof.storage.user_info import UserInfoStorage rewards_storage = RewardsStorage() +user_info_storage = UserInfoStorage() def proof( input_file: str, proof_response: ProofResponse, config: ProofConfig ) -> ProofResponse: proof_response = copy.deepcopy(proof_response) + user_data = extract_user_data(input_file) + if user_data is not None: + proof_response.score = 0 + proof_response.valid = user_info_storage.verify_user(user_data) + return proof_response tweets_data = extract_data(input_file) is_valid, file_score, tweet_info, unique_tweets, total_tweets = ( proof_of_quality(tweets_data, config.file_id, config) diff --git a/volara_proof/storage/user_info.py b/volara_proof/storage/user_info.py new file mode 100644 index 0000000..3fac12b --- /dev/null +++ b/volara_proof/storage/user_info.py @@ -0,0 +1,26 @@ +import logging +import os +import requests + +from volara_proof.models.user_data import UserData +from volara_proof.constants import VOLARA_API_URL + + +class UserInfoStorage: + def verify_user(self, user_info: UserData) -> bool: + request_body = { + "handle": user_info.handle, + "walletAddress": user_info.wallet_address, + } + try: + resp = requests.post( + url=f"{VOLARA_API_URL}/v1/validator/validate-user", + json=request_body, + headers={"Authorization": f"Bearer {os.environ['VOLARA_API_KEY']}"}, + ) + resp.raise_for_status() + logging.info("Succesfully determined if user exists.") + return resp.json()["userValidated"] + except Exception: + logging.exception("[CRITICAL FAILURE] Failed to determine if user exists.") + raise