def is_valid_db_blockchain_account( blockchain: str, account: str, ) -> bool: """Validates a blockchain address already stored in DB. """ if blockchain == SupportedBlockchain.BITCOIN.value: return True if blockchain == SupportedBlockchain.ETHEREUM.value: return is_checksum_address(account) if blockchain == SupportedBlockchain.KUSAMA.value: return is_valid_kusama_address(account) raise AssertionError(f'Unknown blockchain: {blockchain}')
def _validate_blockchain_account_schemas( data: Dict[str, Any], address_getter: Callable, ) -> None: """Validates schema input for the PUT/PATCH/DELETE on blockchain account data""" # Make sure no duplicates addresses are given given_addresses = set() # Make sure ethereum addresses are checksummed if data['blockchain'] == SupportedBlockchain.ETHEREUM: for account_data in data['accounts']: address_string = address_getter(account_data) if not address_string.endswith('.eth'): # Make sure that given value is an ethereum address try: address = to_checksum_address(address_string) except (ValueError, TypeError) as e: raise ValidationError( f'Given value {address_string} is not an ethereum address', field_name='address', ) from e else: # else it's ENS name and will be checked in the transformation step and not here address = address_string if address in given_addresses: raise ValidationError( f'Address {address} appears multiple times in the request data', field_name='address', ) given_addresses.add(address) # Make sure bitcoin addresses are valid elif data['blockchain'] == SupportedBlockchain.BITCOIN: for account_data in data['accounts']: address = address_getter(account_data) # ENS domain will be checked in the transformation step if not address.endswith('.eth') and not is_valid_btc_address( address): raise ValidationError( f'Given value {address} is not a valid bitcoin address', field_name='address', ) if address in given_addresses: raise ValidationError( f'Address {address} appears multiple times in the request data', field_name='address', ) given_addresses.add(address) # Make sure kusama addresses are valid (either ss58 format or ENS domain) elif data['blockchain'] == SupportedBlockchain.KUSAMA: for account_data in data['accounts']: address = address_getter(account_data) # ENS domain will be checked in the transformation step if not address.endswith('.eth') and not is_valid_kusama_address( address): raise ValidationError( f'Given value {address} is not a valid kusama address', field_name='address', ) if address in given_addresses: raise ValidationError( f'Address {address} appears multiple times in the request data', field_name='address', ) given_addresses.add(address)
def test_is_valid_kusama_address(value, is_valid): result = is_valid_kusama_address(value) assert result is is_valid