is there source code or even pseudo code anywhere for validating a bitcoin address.
I will be writing in c# but am familiar enough with other languages to translate. Even better would be psuedo code as it would provide a more clear understanding on what checks are needed and why. I am aware this can be accomplished via RPC call to bitcoind but I wold like to avoid that dependency.
My understanding of bitcoin addresses is that the address is created as follows
base = version + RIPEMD-160(SHA256(public key)))
checksum = lower4bytes(SHA-256(SHA-256(base))
fulladdress=Base58(base+checksum)
given reversing the hashes is impossible we can consider base to be discrete and recreate the checksum.
so my stab at validation psuedo code would be
bool IsValid(string address)
{
if (address == null || address = String.Empty || address.length < 34)
return false
byte[] decodedAddress = Base58.ToByteArray(address)
byte[] base = Array.Copy(decodedAddress, 0, 21) //copy first 21 bytes (version + 20 byte RIPEMD-160 hash)
byte[] checksum = Array.Copy(decodedAddress, 22, 4) // copy last 4 bytes
if (base[0] != 0) //main network
return false
byte[] computedCheckSum = SHA256(SHA256(base)
if (checkSum != computedCheckSum)
return false
return true
}
Anything I am missing?