Bitcoin Forum
December 10, 2016, 03:25:02 AM *
News: To be able to use the next phase of the beta forum software, please ensure that your email address is correct/functional.
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: psuedo code for validating a bitcoin address.  (Read 1903 times)
TangibleCryptography
Sr. Member
****
Offline Offline

Activity: 476


Tangible Cryptography LLC


View Profile WWW
July 09, 2012, 12:29:29 PM
 #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?
1481340302
Hero Member
*
Offline Offline

Posts: 1481340302

View Profile Personal Message (Offline)

Ignore
1481340302
Reply with quote  #2

1481340302
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1481340302
Hero Member
*
Offline Offline

Posts: 1481340302

View Profile Personal Message (Offline)

Ignore
1481340302
Reply with quote  #2

1481340302
Report to moderator
1481340302
Hero Member
*
Offline Offline

Posts: 1481340302

View Profile Personal Message (Offline)

Ignore
1481340302
Reply with quote  #2

1481340302
Report to moderator
Gavin Andresen
Legendary
*
qt
Offline Offline

Activity: 1652


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


0xFB0D8D1534241423


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

How many languages don't support RegEx these days?

I recommend asking me for a signature from my GPG key before doing a trade. I will NEVER deny such a request.
TangibleCryptography
Sr. Member
****
Offline Offline

Activity: 476


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


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

I recommend asking me for a signature from my GPG key before doing a trade. I will NEVER deny such a request.
CodesInChaos
Newbie
*
Offline Offline

Activity: 19



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:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!