Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: likl3sfuhjh on May 06, 2013, 11:41:21 PM



Title: Validate Bitcoin address thru web api or php
Post by: likl3sfuhjh on May 06, 2013, 11:41:21 PM
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.


Title: Re: Validate Bitcoin address thru web api or php
Post by: minzie on May 06, 2013, 11:46:09 PM
What do you mean by 'validate' exactly?


Title: Re: Validate Bitcoin address thru web api or php
Post by: likl3sfuhjh on May 06, 2013, 11:47:18 PM
to see if its a real address


Title: Re: Validate Bitcoin address thru web api or php
Post by: Terk on May 06, 2013, 11:53:10 PM
Not sure what you understand by “real”. This thread provides validation functions if various languages (https://bitcointalk.org/index.php?topic=1026.0). You can only check if the address is valid. If by real you mean that someone is actually using this address (someone has a private key), then you can't know that.


Title: Re: Validate Bitcoin address thru web api or php
Post by: giantdragon on May 07, 2013, 12:16:35 AM
In PHP JSON-RPC you can use this code:

Code:
$isvalid = $bitcoin->validateaddress($address);
if($isvalid['isvalid']) {
//Address valid
}


Title: Re: Validate Bitcoin address thru web api or php
Post by: likl3sfuhjh on May 07, 2013, 03:41:11 AM
In PHP JSON-RPC you can use this code:

Code:
$isvalid = $bitcoin->validateaddress($address);
if($isvalid['isvalid']) {
//Address valid
}





how can that be used in on a website that dosnt have access to the bitcoin client


https://blockchain.info/api/json_rpc_api
i guess i can use this right?


Title: Re: Validate Bitcoin address thru web api or php
Post by: kjj on May 07, 2013, 03:52:07 AM
You can do it without a bitcoind node.

Look up the base58check routines.  There are several PHP implementations available.  Basically, that is all a bitcoind node would do.  (If the address belonged to a known key, it would provide other info too, but that doesn't matter.)


Title: Re: Validate Bitcoin address thru web api or php
Post by: likl3sfuhjh on May 07, 2013, 04:29:58 AM
someone explain to me how to use the json rpc, with blockchains api. There is little to no documentation i want to stop wasting my time with this. Just help me.


Title: Re: Validate Bitcoin address thru web api or php
Post by: robin0van0der0vliet on May 07, 2013, 12:19:28 PM
<?php

function checkAddress($address)
{
    $origbase58 = $address;
    $dec = "0";

    for ($i = 0; $i < strlen($address); $i++)
    {
        $dec = bcadd(bcmul($dec,"58",0),strpos("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",substr($address,$i,1)),0);
    }

    $address = "";

    while (bccomp($dec,0) == 1)
    {
        $dv = bcdiv($dec,"16",0);
        $rem = (integer)bcmod($dec,"16");
        $dec = $dv;
        $address = $address.substr("0123456789ABCDEF",$rem,1);
    }

    $address = strrev($address);

    for ($i = 0; $i < strlen($origbase58) && substr($origbase58,$i,1) == "1"; $i++)
    {
        $address = "00".$address;
    }

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

    if (strlen($address) != 50)
    {
        return false;
    }

    if (hexdec(substr($address,0,2)) > 0)
    {
        return false;
    }

    return substr(strtoupper(hash("sha256",hash("sha256",pack("H*",substr($address,0,strlen($address)-8)),true))),0,8) == substr($address,strlen($address)-8);
}

?>
Here is a PHP function to validate a bitcoin address. ;)


Title: Re: Validate Bitcoin address thru web api or php
Post by: likl3sfuhjh on May 07, 2013, 09:42:53 PM
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:
<?php
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.'---------';
}
?>



Title: Re: Validate Bitcoin address thru web api or php
Post by: robin0van0der0vliet on May 08, 2013, 08:42:59 PM
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:
<?php
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.'---------';
}
?>

The code I posted is much faster than this, and does not need to connect to any other server (such as blockchain.info). ;)


Title: Re: Validate Bitcoin address thru web api or php
Post by: minzie on May 09, 2013, 01:04:10 AM
Sometimes speed is not an issue.


Title: Re: Validate Bitcoin address thru web api or php
Post by: dave111223 on July 15, 2014, 04:28:44 PM
The code I posted is much faster than this, and does not need to connect to any other server (such as blockchain.info). ;)

The code you posted does not work with multisig addresses


Title: Re: Validate Bitcoin address thru web api or php
Post by: DeathAndTaxes on July 15, 2014, 10:32:25 PM
The code I posted is much faster than this, and does not need to connect to any other server (such as blockchain.info). ;)

The code you posted does not work with multisig P2SH addresses

Minor correction.  There actually is no "multisig addresses" just Pay2PubKeyHash addresses and Pay2ScriptHash addresses.  The code provided only properly handles the former.