How to validate a Bitcoin address is a real one?

14

3

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

Posted 2012-01-13T09:32:23.333

Reputation: 25 192

Answers

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

jim618

Posted 2012-01-13T09:32:23.333

Reputation: 3 205

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

Posted 2012-01-13T09:32:23.333

Reputation: 2 254

1I won't be running a bitcoind instance, but thanks regardless.ripper234 2012-01-13T16:21:44.930

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

Bitcoineer

Posted 2012-01-13T09:32:23.333

Reputation: 81

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

pm_labs

Posted 2012-01-13T09:32:23.333

Reputation: 111

1

josecelano

Posted 2012-01-13T09:32:23.333

Reputation: 119

1above link is deadderrend 2015-11-05T23:35:19.780