Skip to content

Commit 382b936

Browse files
authored
Create BitcoinAddressValidator.py
1 parent 5c11d7c commit 382b936

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

BitcoinAddressValidator.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import hashlib,base58,binascii
2+
3+
# it is not used but it is another way to convert Byte to Hex
4+
def convertByteToHex(inp):
5+
return ''.join(["%02x" % x for x in inp])
6+
7+
# mypublickey = '1M2tgkBsUeo2XYAjgapENNb2kxH1gzY3VH'
8+
9+
bitcoinAddress = input("Enter a bitcoin address:")
10+
print("--------------------------------------")
11+
print("Bitcoin Address: ", bitcoinAddress)
12+
# base58.b58decode method generate Byte and we should convert it to Hex with hex() method
13+
base58Decoder = base58.b58decode(bitcoinAddress).hex()
14+
print("Base58 Decoder: ", base58Decoder)
15+
prefixAndHash = base58Decoder[:len(base58Decoder)-8]
16+
checksum = base58Decoder[len(base58Decoder)-8:]
17+
print("\t|___> Prefix & Hash: ", prefixAndHash)
18+
print("\t|___> Checksum: ", checksum)
19+
print("--------------------------------------")
20+
# to handle true result, we should pass our input to hashlib.sha256() method() as Byte format
21+
# so we use binascii.unhexlify() method to convert our input from Hex to Byte
22+
# finally, hexdigest() method convert value to human-readable
23+
hash = prefixAndHash
24+
for x in range(1,3):
25+
hash = hashlib.sha256(binascii.unhexlify(hash)).hexdigest()
26+
print("Hash#", x, " : ", hash)
27+
print("--------------------------------------")
28+
if(checksum == hash[:8]):
29+
print("[TRUE] checksum is valid!")
30+
else:
31+
print("[FALSE] checksum is not valid!")

0 commit comments

Comments
 (0)