Author

Topic: Re: Validate Bitcoin address thru web api or php (Read 329 times)

sr. member
Activity: 248
Merit: 250
You can use PHPs exec-function to call bitcoin-cli validateaddress. Or you can use JSON-RPC php library to do the same.

It's not very good choice if you need to check big database of addresses. Validation is a simple process and it was implemented with all major languages. So it's much faster to get addresses list from db and validate them locally.
legendary
Activity: 1511
Merit: 1072
quack
You can use PHPs exec-function to call bitcoin-cli validateaddress. Or you can use JSON-RPC php library to do the same.
sr. member
Activity: 462
Merit: 250
I have a database of bitcoin addresses and i need to validate them. Please explain the process , i don't care if its an api or a script as long as its efficient.

you can use several methods such as using api or create your own rpc daemon. Both of this have advantage and disadvantage . I think it's very simple and efficient.

another way you can use this

Quote
function validate($address){
        $decoded = decodeBase58($address);
 
        $d1 = hash("sha256", substr($decoded,0,21), true);
        $d2 = hash("sha256", $d1, true);
 
        if(substr_compare($decoded, $d2, 21, 4)){
                throw new \Exception("bad digest");
        }
        return true;
}
function decodeBase58($input) {
        $alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
 
        $out = array_fill(0, 25, 0);
        for($i=0;$i                if(($p=strpos($alphabet, $input[$i]))===false){
                        throw new \Exception("invalid character found");
                }
                $c = $p;
                for ($j = 25; $j--; ) {
                        $c += (int)(58 * $out[$j]);
                        $out[$j] = (int)($c % 256);
                        $c /= 256;
                        $c = (int)$c;
                }
                if($c != 0){
                    throw new \Exception("address too long");
                }
        }
 
        $result = "";
        foreach($out as $val){
                $result .= chr($val);
        }
 
        return $result;
}
 
function main () {
  $s = array(
                "1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9",
                "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i",
                "1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nJ9",
                "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I",
        );
  foreach($s as $btc){
    $message = "OK";
    try{
        validate($btc);
    }catch(\Exception $e){ $message = $e->getMessage(); }
    echo "$btc: $message\n";
  }
}
 
main();

legendary
Activity: 1442
Merit: 1186
The script on rosettacode works. Also it provides examples to validate bitcoin addresses in 20 different languages.

https://rosettacode.org/wiki/Bitcoin/address_validation#PHP

Code:
function validate($address){
        $decoded = decodeBase58($address);
 
        $d1 = hash("sha256", substr($decoded,0,21), true);
        $d2 = hash("sha256", $d1, true);
 
        if(substr_compare($decoded, $d2, 21, 4)){
                throw new \Exception("bad digest");
        }
        return true;
}
function decodeBase58($input) {
        $alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
 
        $out = array_fill(0, 25, 0);
        for($i=0;$i                if(($p=strpos($alphabet, $input[$i]))===false){
                        throw new \Exception("invalid character found");
                }
                $c = $p;
                for ($j = 25; $j--; ) {
                        $c += (int)(58 * $out[$j]);
                        $out[$j] = (int)($c % 256);
                        $c /= 256;
                        $c = (int)$c;
                }
                if($c != 0){
                    throw new \Exception("address too long");
                }
        }
 
        $result = "";
        foreach($out as $val){
                $result .= chr($val);
        }
 
        return $result;
}
 
function main () {
  $s = array(
                "1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9",
                "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i",
                "1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nJ9",
                "1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62I",
        );
  foreach($s as $btc){
    $message = "OK";
    try{
        validate($btc);
    }catch(\Exception $e){ $message = $e->getMessage(); }
    echo "$btc: $message\n";
  }
}
 
main();
sr. member
Activity: 412
Merit: 287
I maintain `bitwasp/bitcoin`which is available through composer. It lets you do quite a lot, and no bitcoind required. https://github.com/Bit-Wasp/bitcoin-php/blob/master/src/Address/AddressFactory.php#L87 contains the function to check an address.
hero member
Activity: 683
Merit: 500
thanks, i figured it out myself using the blockchain json rpc api. Ill try that code you took the time to post also thanks.


Code:
require_once('jsonrpcphp/includes/jsonRPCClient.php');
/** SIMPLE BITCOIN JSON RPC PHP**/
 
$btc_connect = array("user" => "",            
            
"pass" =>   "",                   
            
"host" =>   "blockchain.info",                     
            
"port" =>   80);                        
 
 
//create an array with connection settings
 
$bitcoin = new jsonRPCClient("http://{$btc_connect['user']}:{$btc_connect['pass']}@{$btc_connect['host']}:{$btc_connect['port']}");

$address 'bitcoinaddress';
$isvalid $bitcoin->validateaddress($address);
if($isvalid['isvalid']) {
//Address valid
}
else{
echo '---------'.$address.'---------';
}
?>


I don't mean to resurrect a dead post, but this API is no longer working. Can someone link me to the current replacement API? Blockchain is driving me nuts. Or any alternative way to validate a BTC address that will catch all addresses.. that would also be awesome.

Jump to: