Author

Topic: psuedo code for validating a bitcoin address. (Read 2129 times)

newbie
Activity: 19
Merit: 0
For the Base58 and check sum parts you can use my C# code:
https://gist.github.com/3175971#file_base58_encoding.cs

It passes all test cases from https://github.com/bitcoin/bitcoin/blob/master/src/test/base58_tests.cpp

It doesn't contain anything bitcoin address specific. So you'll need to hash the public key(or script with p2sh) and add/verify the version byte yourself.
hero member
Activity: 784
Merit: 1000
0xFB0D8D1534241423
Sorry I thought this was a different thread. It had a much simpler validation method which basically boiled down to a regex.

That said, if we can compute prime-ness with regEx, I'm not sure that sha256 is impossible Wink
sr. member
Activity: 476
Merit: 250
Tangible Cryptography LLC
How many languages don't support RegEx these days?

I am not sure what your point is unless you know how to use RegEx to calculate a double SHA256 checksum.
hero member
Activity: 784
Merit: 1000
0xFB0D8D1534241423
How many languages don't support RegEx these days?
legendary
Activity: 1652
Merit: 2216
Chief Scientist
Anything I am missing?
Depending on what you're doing, you should support BIP 13: https://en.bitcoin.it/wiki/BIP_0013

Also see: https://en.bitcoin.it/wiki/Base58Check_encoding  for pseudo-code.
sr. member
Activity: 476
Merit: 250
Tangible Cryptography LLC
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
Code:
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

Code:
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?
Jump to: