17

Is there a code snippet that validates a bitcoin address?

I'm looking for both javascript and java snippets. The code should support both testnet and real addresses.

ripper234
  • 26,452
  • 30
  • 111
  • 246

5 Answers5

9

also, the official client bitcoind has a 'validateaddress' command. see https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list

you could call that from your code if you happen to have bitcoind running already.

nanotube
  • 2,260
  • 14
  • 13
9

part of https://github.com/mikegogulski/bitcoin-php/blob/master/src/bitcoin.inc

 private static $hexchars = "0123456789ABCDEF";
 private static $base58chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

public static function checkAddress($addr, $addressversion = "00") // hex byte
{
    $addr = self::decodeBase58($addr);
    if (strlen($addr) != 50) {
        return false;
    }
    $version = substr($addr, 0, 2);
    if (hexdec($version) > hexdec($addressversion)) {
        return false;
    }
    $check = substr($addr, 0, strlen($addr) - 8);
    $check = pack("H*", $check);
    $check = strtoupper(hash("sha256", hash("sha256", $check, true)));
    $check = substr($check, 0, 8);
    return $check == substr($addr, strlen($addr) - 8);
}


private function decodeBase58($base58)
{
    $origbase58 = $base58;

    //only valid chars allowed
    if (preg_match('/[^1-9A-HJ-NP-Za-km-z]/', $base58)) {
        return "";
    }

    $return = "0";
    for ($i = 0; $i < strlen($base58); $i++) {
        $current = (string)strpos(self::$base58chars, $base58[$i]);
        $return = (string)bcmul($return, "58", 0);
        $return = (string)bcadd($return, $current, 0);
    }

    $return = self::encodeHex($return);

    //leading zeros
    for ($i = 0; $i < strlen($origbase58) && $origbase58[$i] == "1"; $i++) {
        $return = "00" . $return;
    }

    if (strlen($return) % 2 != 0) {
        $return = "0" . $return;
    }

    return $return;
}

private function encodeHex($dec)
{
    $return = "";
    while (bccomp($dec, 0) == 1) {
        $dv = (string)bcdiv($dec, "16", 0);
        $rem = (integer)bcmod($dec, "16");
        $dec = $dv;
        $return = $return . self::$hexchars[$rem];
    }
    return strrev($return);
}
Aldekein
  • 105
  • 4
Bitcoineer
  • 91
  • 1
  • 2
5

For Java, have a look at the validateAddress(String) method in this class: https://github.com/jim618/multibit/blob/master/src/main/java/org/multibit/viewsystem/swing/action/Validator.java

jnnk
  • 1,906
  • 15
  • 23
jim618
  • 3,325
  • 14
  • 16
2

For BitcoinJ (Java) it should be something like this:

public boolean isValidAddress(String address) {
    try {
        new Address(params, address);
        return true;
    } catch(AddressFormatException e) {
        return false;
    }
}

API for address can be found here: https://bitcoinj.github.io/javadoc/0.11/com/google/bitcoin/core/Address.html

pm_labs
  • 121
  • 2
1

For Javascript, you can use http://bitcore.io/ library. Here is an example:

https://github.com/bitpay/bitcore/blob/master/examples/browser/example.html

Jose Celano
  • 139
  • 2