Skip to content

Commit

Permalink
use btctxstore address validator
Browse files Browse the repository at this point in the history
  • Loading branch information
F483 committed Aug 31, 2015
1 parent 525b750 commit a87279a
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 41 deletions.
4 changes: 3 additions & 1 deletion dataserv/Farmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from datetime import timedelta
from sqlalchemy import DateTime
from btctxstore import BtcTxStore
from dataserv.Validator import is_btc_address


is_btc_address = BtcTxStore().validate_address


class AuthError(Exception):
Expand Down
27 changes: 0 additions & 27 deletions dataserv/Validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,3 @@ def is_sha256(content):
return len(content) == 64


def is_btc_address(content):
"""
Does simple validation of a bitcoin-like address.
Source: http://bit.ly/17OhFP5
param : address : an ASCII or unicode string, of a bitcoin address.
returns : boolean, indicating that the address has a correct format.
"""

# The first character indicates the "version" of the address.
chars_ok_first = "123"
# alphanumeric characters without : l I O 0
chars_ok = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

# We do not check the high length limit of the address.
# Usually, it is 35, but nobody knows what could happen in the future.
if len(content) < 27:
return False
# Changed from the original code, we do want to check the upper bounds
elif len(content) > 35:
return False
elif content[0] not in chars_ok_first:
return False

# We use the function "all" by passing it an enumerator as parameter.
# It does a little optimization :
# if one of the character is not valid, the next ones are not tested.
return all((char in chars_ok for char in content[1:]))
14 changes: 1 addition & 13 deletions tests/test_Validator.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import unittest
from dataserv.Validator import is_sha256, is_btc_address
from dataserv.Validator import is_sha256


class ValidatorTest(unittest.TestCase):
def test_valid_address(self):
addr1 = '191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc'
addr2 = '191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc9999ghjfghj99'
addr3 = 'not valid address'
addr4 = 'not valid &address'
addr5 = '791GVvAaTRxLmz3rW3nU5jAV1rF186VxQc'

self.assertTrue(is_btc_address(addr1))
self.assertFalse(is_btc_address(addr2))
self.assertFalse(is_btc_address(addr3))
self.assertFalse(is_btc_address(addr4))
self.assertFalse(is_btc_address(addr5))

def test_valid_sha256(self):
valid_hash = '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'
Expand Down

0 comments on commit a87279a

Please sign in to comment.