15

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.

4

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

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.

  • 1
    I won't be running a bitcoind instance, but thanks regardless. – ripper234 Jan 13 '12 at 16:21
8

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);
}
1

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

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

  • 1
    above link is dead – derrend Nov 5 '15 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.