Created
April 15, 2014 09:34
-
-
Save Cediddi/10717740 to your computer and use it in GitHub Desktop.
BitCoinAddressValidator is a class that controls if the given address is valid or not. Works with plain python or django as a custom validator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = "Umut Karci" | |
try: | |
from django.core.validators import ValidationError | |
from django.utils.translation import ugettext_lazy as _ | |
except ImportError: | |
ValidationError = Exception | |
_ = lambda x: x | |
class BitCoinAddressValidator(object): | |
""" | |
Parameters | |
---------- | |
int min_length : Minimum length possible for BitCoin Addresses. | |
default = 27 | |
int max_length : Maximum length possible for BitCoin Addresses. | |
default = 50 | |
str prefixes : Possible prefixes for BitCoin Addresses. | |
default = "123" | |
str base : Base characters for BitCoin Addresses. | |
default = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" | |
Returns | |
------- | |
None | |
Raises | |
-------- | |
ValidationError: If used with django library, raises ValidationError | |
Exception: If used without django library, raises Exception | |
Examples | |
-------- | |
>>> from bitcoinaddressvalidator import BitCoinAddressValidator | |
>>> validator = BitCoinAddressValidator() | |
>>> validator("1EEgeViug9stgEajgr6WKWTh4EmssdUMUT") | |
>>> validator("1EEge") | |
Exception: BitCoin address is too short. | |
>>> validator("1EEgeViug9stgEajgr6WKWTh4EmssdUMUT12345") | |
Exception: BitCoin address is too long. | |
>>> validator("4EEgeViug9stgEajgr6WKWTh4EmssdUMUT") | |
Exception: BitCoin address has unsupported prefix. | |
>>> validator("1EEgeViug9stgEajgr6WKWTh4EmssdUMUT0OIl+/") | |
Exception: BitCoin address has unsupported characters. | |
""" | |
def __init__(self, min_length=27, max_length=35, prefixes="123", | |
base="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"): | |
self._min_length = min_length | |
self._max_length = max_length | |
self._prefixes = prefixes | |
self._base = base | |
self.validations = [self.check_length, self.check_prefix, self.check_base58] | |
self._adr = None | |
def __call__(self, btc_address): | |
"""Python Magic method for calling the class, returns None""" | |
for validation in self.validations: | |
validation(btc_address) | |
def check_length(self, address): | |
"""Checks if the address is between minimum and maximum value, returns None""" | |
if not (len(address) >= self._min_length): | |
raise ValidationError(_(u'BitCoin address is too short.')) | |
if not (len(address) <= self._max_length): | |
raise ValidationError(_(u'BitCoin address is too long.')) | |
def check_prefix(self, address): | |
"""Checks if the address's prefix is correct, returns None""" | |
if not set(address[0]).issubset(set(self._prefixes)): | |
raise ValidationError(_(u'BitCoin address has unsupported prefix.')) | |
def check_base58(self, address): | |
"""Checks if the address is base58 compatible, returns None""" | |
if not set(address).issubset(set(self._base)): | |
raise ValidationError(_(u'BitCoin address has unsupported characters.')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment