Validate bitcoin address python

validate bitcoin address python

rosettacode.org › wiki › Bitcoin › address_validation. DJango field type for a Bitcoin Address. #. import re. from django import forms. from django.forms.util import ValidationError. from Crypto.Hash import SHA256. Enter the address generated by the above wallet. You can check your testnet transaction here. Once the transaction is confirmed, you can also. validate bitcoin address python

Validate bitcoin address python - opinion

Curious question: Validate bitcoin address python

Validate bitcoin address python 157
Validate bitcoin address python 999
Validate bitcoin address python 823
Validate bitcoin address python 65

savasadar/BCAddressField.py

## DJango field type for a Bitcoin Address#importrefromdjangoimportformsfromdjango.forms.utilimportValidationErrorfromCrypto.HashimportSHA256classBCAddressField(forms.CharField):default_error_messages= {'invalid': 'Invalid Bitcoin address.', }def__init__(self, *args, **kwargs):super(BCAddressField, self).__init__(*args, **kwargs)defclean(self, value):value=value.strip()ifre.match(r"[a-zA-Z1-9]{27,35}$", value) isNone:raiseValidationError(self.error_messages['invalid'])version=get_bcaddress_version(value)ifversionisNone:raiseValidationError(self.error_messages['invalid'])returnvalueimportmath__b58chars='123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'__b58base=len(__b58chars)defb58encode(v):""" encode v, which is a string of bytes, to base58. """long_value=0Lfor (i, c) inenumerate(v[::-1]):long_value+= (256**i) *ord(c)result=''whilelong_value>=__b58base:div, mod=divmod(long_value, __b58base)result=__b58chars[mod] +resultlong_value=divresult=__b58chars[long_value] +result# Bitcoin does a little leading-zero-compression: # leading 0-bytes in the input become leading-1s nPad=0forcinv:ifc=='\0': nPad+=1else: breakreturn (__b58chars[0]*nPad) +resultdefb58decode(v, length):""" decode v into a string of len bytes """long_value=0Lfor (i, c) inenumerate(v[::-1]):long_value+=__b58chars.find(c) * (__b58base**i)result=''whilelong_value>=256:div, mod=divmod(long_value, 256)result=chr(mod) +resultlong_value=divresult=chr(long_value) +resultnPad=0forcinv:ifc==__b58chars[0]: nPad+=1else: breakresult=chr(0)*nPad+resultiflengthisnotNoneandlen(result) !=length:returnNonereturnresultdefget_bcaddress_version(strAddress):""" Returns None if strAddress is invalid. Otherwise returns integer version of address. """addr=b58decode(strAddress,25)ifaddrisNone: returnNoneversion=addr[0]checksum=addr[-4:]vh160=addr[:-4] # Version plus hash160 is what is checksummed h3=SHA256.new(SHA256.new(vh160).digest()).digest()ifh3[0:4] ==checksum:returnord(version)returnNone
Источник: https://gist.github.com/savasadar/4e12f4d008c2f91f4c836339b7bb99a4

3 thoughts to “Validate bitcoin address python”

Leave a Reply

Your email address will not be published. Required fields are marked *