<s> import re <EOL> from django import forms <EOL> from django . forms . util import ValidationError <EOL> import hashlib <EOL> class BCAddressField ( forms . CharField ) : <EOL> default_error_messages = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( BCAddressField , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean ( self , value ) : <EOL> if not value and not self . required : <EOL> return None <EOL> if not value . startswith ( u"<STR_LIT:1>" ) and not value . startswith ( u"<STR_LIT:3>" ) : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> value = value . strip ( ) <EOL> if "<STR_LIT:\n>" in value : <EOL> raise ValidationError ( u"<STR_LIT>" ) <EOL> if "<STR_LIT:U+0020>" in value : <EOL> raise ValidationError ( u"<STR_LIT>" ) <EOL> if re . match ( r"<STR_LIT>" , value ) is None : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> version = get_bcaddress_version ( value ) <EOL> if version is None : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] ) <EOL> return value <EOL> def is_valid_btc_address ( value ) : <EOL> value = value . strip ( ) <EOL> if re . match ( r"<STR_LIT>" , value ) is None : <EOL> return False <EOL> version = get_bcaddress_version ( value ) <EOL> if version is None : <EOL> return False <EOL> return True <EOL> __b58chars = '<STR_LIT>' <EOL> __b58base = len ( __b58chars ) <EOL> def b58encode ( v ) : <EOL> """<STR_LIT>""" <EOL> long_value = <NUM_LIT:0> L <EOL> for ( i , c ) in enumerate ( v [ : : - <NUM_LIT:1> ] ) : <EOL> long_value += ( <NUM_LIT> ** i ) * ord ( c ) <EOL> result = '<STR_LIT>' <EOL> while long_value >= __b58base : <EOL> div , mod = divmod ( long_value , __b58base ) <EOL> result = __b58chars [ mod ] + result <EOL> long_value = div <EOL> result = __b58chars [ long_value ] + result <EOL> nPad = <NUM_LIT:0> <EOL> for c in v : <EOL> if c == '<STR_LIT>' : nPad += <NUM_LIT:1> <EOL> else : break <EOL> return ( __b58chars [ <NUM_LIT:0> ] * nPad ) + result <EOL> def b58decode ( v , length ) : <EOL> """<STR_LIT>""" <EOL> long_value = <NUM_LIT:0> L <EOL> for ( i , c ) in enumerate ( v [ : : - <NUM_LIT:1> ] ) : <EOL> long_value += __b58chars . find ( c ) * ( __b58base ** i ) <EOL> result = '<STR_LIT>' <EOL> while long_value >= <NUM_LIT> : <EOL> div , mod = divmod ( long_value , <NUM_LIT> ) <EOL> result = chr ( mod ) + result <EOL> long_value = div <EOL> result = chr ( long_value ) + result <EOL> nPad = <NUM_LIT:0> <EOL> for c in v : <EOL> if c == __b58chars [ <NUM_LIT:0> ] : nPad += <NUM_LIT:1> <EOL> else : break <EOL> result = chr ( <NUM_LIT:0> ) * nPad + result <EOL> if length is not None and len ( result ) != length : <EOL> return None <EOL> return result <EOL> def b36encode ( number , alphabet = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( number , ( int , long ) ) : <EOL> long_value = <NUM_LIT:0> L <EOL> for ( i , c ) in enumerate ( number [ : : - <NUM_LIT:1> ] ) : <EOL> long_value += ( <NUM_LIT> ** i ) * ord ( c ) <EOL> number = long_value <EOL> base36 = '<STR_LIT>' if number != <NUM_LIT:0> else '<STR_LIT:0>' <EOL> sign = '<STR_LIT>' <EOL> if number < <NUM_LIT:0> : <EOL> sign = '<STR_LIT:->' <EOL> number = - number <EOL> while number != <NUM_LIT:0> : <EOL> number , i = divmod ( number , len ( alphabet ) ) <EOL> base36 = alphabet [ i ] + base36 <EOL> return sign + base36 <EOL> def b36decode ( number ) : <EOL> return int ( number , <NUM_LIT> ) <EOL> def get_bcaddress_version ( strAddress ) : <EOL> """<STR_LIT>""" <EOL> addr = b58decode ( strAddress , <NUM_LIT> ) <EOL> if addr is None : return None <EOL> version = addr [ <NUM_LIT:0> ] <EOL> checksum = addr [ - <NUM_LIT:4> : ] <EOL> vh160 = addr [ : - <NUM_LIT:4> ] <EOL> h3 = hashlib . sha256 ( hashlib . sha256 ( vh160 ) . digest ( ) ) . digest ( ) <EOL> if h3 [ <NUM_LIT:0> : <NUM_LIT:4> ] == checksum : <EOL> return ord ( version ) <EOL> return None </s>
