from hashlib import sha256
digits58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
def decode_base58(bc, length):
n = 0
for char in bc:
n = n * 58 + digits58.index(char)
return n.to_bytes(length, 'big')
def check_bc(bc):
bcbytes = decode_base58(bc, 25)
return bcbytes[-4:] == sha256(sha256(bcbytes[:-4]).digest()).digest()[:4]
if __name__ == '__main__':
bc = '1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i'
assert check_bc(bc)
assert not check_bc( bc.replace('N', 'P', 1) )
assert check_bc('1111111111111111111114oLvT2')
assert check_bc("17NdbrSGoUotzeGCcMMCqnFkEvLymoou9j")
ZnJvbSBoYXNobGliIGltcG9ydCBzaGEyNTYKCmRpZ2l0czU4ID0gJzEyMzQ1Njc4OUFCQ0RFRkdISktMTU5QUVJTVFVWV1hZWmFiY2RlZmdoaWprbW5vcHFyc3R1dnd4eXonCgpkZWYgZGVjb2RlX2Jhc2U1OChiYywgbGVuZ3RoKToKIG4gPSAwCiBmb3IgY2hhciBpbiBiYzoKICBuID0gbiAqIDU4ICsgZGlnaXRzNTguaW5kZXgoY2hhcikKIHJldHVybiBuLnRvX2J5dGVzKGxlbmd0aCwgJ2JpZycpCiAKZGVmIGNoZWNrX2JjKGJjKToKIGJjYnl0ZXMgPSBkZWNvZGVfYmFzZTU4KGJjLCAyNSkKIHJldHVybiBiY2J5dGVzWy00Ol0gPT0gc2hhMjU2KHNoYTI1NihiY2J5dGVzWzotNF0pLmRpZ2VzdCgpKS5kaWdlc3QoKVs6NF0KCmlmIF9fbmFtZV9fID09ICdfX21haW5fXyc6CiBiYyA9ICcxQUdOYTE1WlFYQVpVZ0ZpcUoyaTdaMkRQVTJKNmhXNjJpJwogYXNzZXJ0IGNoZWNrX2JjKGJjKQogYXNzZXJ0IG5vdCBjaGVja19iYyggYmMucmVwbGFjZSgnTicsICdQJywgMSkgKQogYXNzZXJ0IGNoZWNrX2JjKCcxMTExMTExMTExMTExMTExMTExMTE0b0x2VDInKQogYXNzZXJ0IGNoZWNrX2JjKCIxN05kYnJTR29Vb3R6ZUdDY01NQ3FuRmtFdkx5bW9vdTlqIik=
ZnJvbSBoYXNobGliIGltcG9ydCBzaGEyNTYKCmRpZ2l0czU4ID0gJzEyMzQ1Njc4OUFCQ0RFRkdISktMTU5QUVJTVFVWV1hZWmFiY2RlZmdoaWprbW5vcHFyc3R1dnd4eXonCgpkZWYgZGVjb2RlX2Jhc2U1OChiYywgbGVuZ3RoKToKIG4gPSAwCiBmb3IgY2hhciBpbiBiYzoKICBuID0gbiAqIDU4ICsgZGlnaXRzNTguaW5kZXgoY2hhcikKIHJldHVybiBuLnRvX2J5dGVzKGxlbmd0aCwgJ2JpZycpCiAKZGVmIGNoZWNrX2JjKGJjKToKIGJjYnl0ZXMgPSBkZWNvZGVfYmFzZTU4KGJjLCAyNSkKIHJldHVybiBiY2J5dGVzWy00Ol0gPT0gc2hhMjU2KHNoYTI1NihiY2J5dGVzWzotNF0pLmRpZ2VzdCgpKS5kaWdlc3QoKVs6NF0KCmlmIF9fbmFtZV9fID09ICdfX21haW5fXyc6CiBiYyA9ICcxQUdOYTE1WlFYQVpVZ0ZpcUoyaTdaMkRQVTJKNmhXNjJpJwogYXNzZXJ0IGNoZWNrX2JjKGJjKQogYXNzZXJ0IG5vdCBjaGVja19iYyggYmMucmVwbGFjZSgnTicsICdQJywgMSkgKQogYXNzZXJ0IGNoZWNrX2JjKCcxMTExMTExMTExMTExMTExMTExMTE0b0x2VDInKQogYXNzZXJ0IGNoZWNrX2JjKCZxdW90OzE3TmRiclNHb1VvdHplR0NjTU1DcW5Ga0V2THltb291OWomcXVvdDsp
from hashlib import sha256
digits58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
def decode_base58(bc, length):
n = 0
for char in bc:
n = n * 58 + digits58.index(char)
return n.to_bytes(length, 'big')
def check_bc(bc):
bcbytes = decode_base58(bc, 25)
return bcbytes[-4:] == sha256(sha256(bcbytes[:-4]).digest()).digest()[:4]
if __name__ == '__main__':
bc = '1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i'
assert check_bc(bc)
assert not check_bc( bc.replace('N', 'P', 1) )
assert check_bc('1111111111111111111114oLvT2')
assert check_bc("17NdbrSGoUotzeGCcMMCqnFkEvLymoou9j")