Skip to content

Commit

Permalink
Add check address validator for evm and solana
Browse files Browse the repository at this point in the history
  • Loading branch information
PooyaFekri committed Jan 5, 2024
1 parent 0987ac9 commit 028aaa8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
27 changes: 27 additions & 0 deletions core/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.core.exceptions import BadRequest
from solders.pubkey import Pubkey

from core.utils import Web3Utils

from .models import Chain, NetworkTypes


def address_validator(address, chain: Chain):
is_address_valid = False
if chain.chain_type == NetworkTypes.LIGHTNING:
return
elif chain.chain_type == NetworkTypes.EVM:
try:
Web3Utils.to_checksum_address(address)
return
except ValueError:
is_address_valid = False
elif chain.chain_type == NetworkTypes.SOLANA:
try:
pub_key = Pubkey.from_string(address)
is_address_valid = pub_key.is_on_curve()
except ValueError:
is_address_valid = False

if not is_address_valid:
raise BadRequest(f"Address: {address} is not valid")
5 changes: 2 additions & 3 deletions faucet/faucet_manager/lnpay_manager/utility_helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pprint
import requests
import json

import requests


def get_request(location):
from .lnpay_main import __ENDPOINT_URL__, __PUBLIC_API_KEY__, __VERSION__
Expand Down Expand Up @@ -53,5 +53,4 @@ def post_request(location, params):
data = json.dumps(params)

r = requests.post(url=endpoint, data=data, headers=headers)
print("salam", r.text)
return r.json()
17 changes: 15 additions & 2 deletions faucet/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ def test_claim_max_api_should_fail_if_not_verified(self):
def test_claim_max_api_should_claim_all(self):
endpoint = reverse("FAUCET:claim-max", kwargs={"chain_pk": self.x_dai.pk})

response = self.client.post(endpoint, data={"address": "0x12345"})
response = self.client.post(
endpoint, data={"address": "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"}
)
claim_receipt = json.loads(response.content)

self.assertEqual(response.status_code, 200)
Expand All @@ -463,13 +465,24 @@ def test_claim_max_api_should_claim_all(self):
)
def test_claim_max_twice_should_fail(self):
endpoint = reverse("FAUCET:claim-max", kwargs={"chain_pk": self.x_dai.pk})
response_1 = self.client.post(endpoint, data={"address": "0x12345"})
response_1 = self.client.post(
endpoint, data={"address": "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"}
)
self.assertEqual(response_1.status_code, 200)
try:
self.client.post(endpoint)
except CustomException:
self.assertEqual(True, True)

@patch(
"authentication.helpers.BrightIDSoulboundAPIInterface.get_verification_status",
lambda a, b, c: (True, None),
)
def test_address_validator_evm(self):
endpoint = reverse("FAUCET:claim-max", kwargs={"chain_pk": self.x_dai.pk})
response_1 = self.client.post(endpoint, data={"address": "0x132546"})
self.assertEqual(response_1.status_code, 400)

@patch(
"authentication.helpers.BrightIDSoulboundAPIInterface.get_verification_status",
lambda a, b, c: (True, None),
Expand Down
7 changes: 7 additions & 0 deletions faucet/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from authentication.models import UserProfile
from core.filters import ChainFilterBackend, IsOwnerFilterBackend
from core.paginations import StandardResultsSetPagination
from core.validators import address_validator
from faucet.faucet_manager.claim_manager import (
ClaimManagerFactory,
LimitedChainClaimManager,
Expand Down Expand Up @@ -203,6 +204,11 @@ def get_chain(self) -> Chain:
def get_claim_manager(self):
return ClaimManagerFactory(self.get_chain(), self.get_user()).get_manager()

def check_to_address_is_validate(self):
chain = self.get_chain()
to_address = self.request.data.get("address", None)
address_validator(to_address, chain)

def claim_max(self, to_address) -> ClaimReceipt:
manager = self.get_claim_manager()
max_credit = manager.get_credit_strategy().get_unclaimed()
Expand All @@ -218,6 +224,7 @@ def claim_max(self, to_address) -> ClaimReceipt:
def post(self, request, *args, **kwargs):
self.check_user_is_verified()
self.to_address_is_provided()
self.check_to_address_is_validate()

receipt = self.claim_max(to_address=request.data.get("address"))
return Response(ReceiptSerializer(instance=receipt).data)
Expand Down

0 comments on commit 028aaa8

Please sign in to comment.