Bitcoin Forum
May 29, 2024, 11:24:42 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: psuedo code for validating a bitcoin address.  (Read 2129 times)
TangibleCryptography (OP)
Sr. Member
****
Offline Offline

Activity: 476
Merit: 250


Tangible Cryptography LLC


View Profile WWW
July 09, 2012, 12:29:29 PM
Last edit: July 09, 2012, 12:46:23 PM by TangibleCryptography
 #1

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?
Gavin Andresen
Legendary
*
qt
Offline Offline

Activity: 1652
Merit: 2217


Chief Scientist


View Profile WWW
July 09, 2012, 03:20:01 PM
 #2

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.

How often do you get the chance to work on a potentially world-changing project?
nimda
Hero Member
*****
Offline Offline

Activity: 784
Merit: 1000


0xFB0D8D1534241423


View Profile
July 10, 2012, 06:07:51 PM
 #3

How many languages don't support RegEx these days?
TangibleCryptography (OP)
Sr. Member
****
Offline Offline

Activity: 476
Merit: 250


Tangible Cryptography LLC


View Profile WWW
July 10, 2012, 06:31:52 PM
 #4

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.
nimda
Hero Member
*****
Offline Offline

Activity: 784
Merit: 1000


0xFB0D8D1534241423


View Profile
July 11, 2012, 12:59:20 AM
 #5

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
CodesInChaos
Newbie
*
Offline Offline

Activity: 19
Merit: 0



View Profile
July 25, 2012, 12:54:17 PM
 #6

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.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!