In the last blog post, we presented how we created our Bitcoin Burning Address. But for verification, we also need to make sure that this address is in fact valid.
In this article, we are going to examine how you can check any Bitcoin address for validity in Python.
Like last time, we published the python source code on Github:
The algorithm is not too complicated.
On line 16, we first decode our Bitcoin address from Base58 into byte representation.
After decoding, the first byte of the Bitcoin address specifies the address version and the last 4 bytes reveal the checksum.
In line 22, we calculate our checksum by calculating the hashing function sha256 two times and taking the first 4 bytes of the result.
If the calculated checksum matches the one which we found in the Bitcoin address, we know that the Bitcoin address is encoded correctly and therefore valid.
From lines 24 to 37, we are just checking what kind of Bitcoin address we dealt with:
- 0x00 (Base58: 1) stands for a regular Bitcoin Address
- 0x05 (Base58: 3) means a Pay-to-Script-Hash Address
- 0x6F (Base58: m or n) is a Bitcoin address only for the Bitcoin Testnet (the network which holds no value and is meant for testing purposes)
- 0x80 (Base58: 5, K, or L) is a Private Key in WIF format
- 0x0142 (Base58: 6P) is a BIP-38 Encrypted Private Key
- 0x0488B21E (Base58: xpub) is a BIP-32 Extended Public Key
In the last lines, we just print out our results.
If you like this explanation, you can leave us a like on Github and follow our project of decentralizing CO2 certificates.