Skip to content

Commit

Permalink
Update GroupManager's is_member to accept a Wallet instance
Browse files Browse the repository at this point in the history
  • Loading branch information
payton committed Feb 5, 2022
1 parent 5b36f24 commit ff05b3a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion siwe_auth/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def authenticate(self, request, signature: str = None, siwe_message: SiweMessage
logging.info(f"Created group '{custom_group[0]}'.")
group_manager: GroupManager = custom_group[1]
if group_manager.is_member(
ethereum_address=wallet.ethereum_address,
wallet=wallet,
provider=HTTPProvider(settings.PROVIDER),
):
logging.info(
Expand Down
10 changes: 6 additions & 4 deletions siwe_auth/custom_groups/erc1155.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,22 @@ def _is_member(
return expression(balance)

@abstractmethod
def is_member(self, ethereum_address: str, provider: HTTPProvider) -> bool:
def is_member(self, wallet: object, provider: HTTPProvider) -> bool:
pass


class ERC1155OwnerManager(ERC1155Manager):
def is_member(self, ethereum_address: str, provider: HTTPProvider) -> bool:
def is_member(self, wallet: object, provider: HTTPProvider) -> bool:
if not self._valid_wallet(wallet=wallet):
return False
try:
return self._is_member(
ethereum_address=ethereum_address,
ethereum_address=wallet.ethereum_address,
provider=provider,
expression=lambda x: x > 0,
)
except ValueError:
logging.error(
f"Unable to verify membership of invalid address: {ethereum_address}"
f"Unable to verify membership of invalid address: {wallet.ethereum_address}"
)
return False
11 changes: 7 additions & 4 deletions siwe_auth/custom_groups/erc20.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Callable

from siwe_auth.custom_groups.group_manager import GroupManager
from siwe_auth.utils.data_classes import EthereumBaseClass

from web3 import Web3, HTTPProvider

Expand Down Expand Up @@ -41,20 +42,22 @@ def _is_member(
return expression(balance)

@abstractmethod
def is_member(self, ethereum_address: str, provider: HTTPProvider) -> bool:
def is_member(self, wallet: EthereumBaseClass, provider: HTTPProvider) -> bool:
pass


class ERC20OwnerManager(ERC20Manager):
def is_member(self, ethereum_address: str, provider: HTTPProvider) -> bool:
def is_member(self, wallet: EthereumBaseClass, provider: HTTPProvider) -> bool:
if not self._valid_wallet(wallet=wallet):
return False
try:
return self._is_member(
ethereum_address=ethereum_address,
ethereum_address=wallet.ethereum_address,
provider=provider,
expression=lambda x: x > 0,
)
except ValueError:
logging.error(
f"Unable to verify membership of invalid address: {ethereum_address}"
f"Unable to verify membership of invalid address: {wallet.ethereum_address}"
)
return False
10 changes: 6 additions & 4 deletions siwe_auth/custom_groups/erc721.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,22 @@ def _is_member(
return expression(balance)

@abstractmethod
def is_member(self, ethereum_address: str, provider: HTTPProvider) -> bool:
def is_member(self, wallet: object, provider: HTTPProvider) -> bool:
pass


class ERC721OwnerManager(ERC721Manager):
def is_member(self, ethereum_address: str, provider: HTTPProvider) -> bool:
def is_member(self, wallet: object, provider: HTTPProvider) -> bool:
if not self._valid_wallet(wallet=wallet):
return False
try:
return self._is_member(
ethereum_address=ethereum_address,
ethereum_address=wallet.ethereum_address,
provider=provider,
expression=lambda x: x > 0,
)
except ValueError:
logging.error(
f"Unable to verify membership of invalid address: {ethereum_address}"
f"Unable to verify membership of invalid address: {wallet.ethereum_address}"
)
return False
7 changes: 5 additions & 2 deletions siwe_auth/custom_groups/group_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ def __init__(self, config: dict):
pass

@abstractmethod
def is_member(self, ethereum_address: str, provider: HTTPProvider) -> bool:
def is_member(self, wallet: object, provider: HTTPProvider) -> bool:
"""
Membership function to identify if a given ethereum address is part of this class' group.
:param provider: Web3 provider to use for membership check.
:param ethereum_address: Address to check membership of.
:param wallet: Object with ethereum_address attribute to check membership of.
:return: True if address is a member else False
"""
pass

def _valid_wallet(self, wallet: object):
return wallet.__getattribute__('ethereum_address') is not None
2 changes: 0 additions & 2 deletions siwe_auth/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from datetime import datetime

from django.db import models

from django.contrib.auth.models import (
BaseUserManager,
AbstractBaseUser,
Expand Down

0 comments on commit ff05b3a

Please sign in to comment.