Bitcoin Forum
November 02, 2024, 10:41:52 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Re: Validate Bitcoin address thru web api or php  (Read 329 times)
dhavin (OP)
Hero Member
*****
Offline Offline

Activity: 683
Merit: 500


View Profile
August 17, 2016, 03:03:57 AM
 #1

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.'---------';
}
?>


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.


http://www.scratch4satoshis.com/ - 1600 satoshi/hour 2 MILLION SATOSHI minimum weekly lottery draw! Get your tickets submitted!
fbueller
Sr. Member
****
Offline Offline

Activity: 412
Merit: 287


View Profile
August 18, 2016, 09:44:43 PM
 #2

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.

Bitwasp Developer.
coinableS
Legendary
*
Offline Offline

Activity: 1442
Merit: 1186



View Profile WWW
August 19, 2016, 04:46:27 AM
 #3

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<strlen($input);$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();

fikihafana
Sr. Member
****
Offline Offline

Activity: 462
Merit: 250



View Profile
August 21, 2016, 03:08:27 AM
 #4

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<strlen($input);$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();

Anduck
Legendary
*
Offline Offline

Activity: 1511
Merit: 1072


quack


View Profile
August 21, 2016, 02:28:41 PM
 #5

You can use PHPs exec-function to call bitcoin-cli validateaddress. Or you can use JSON-RPC php library to do the same.

konovalets
Sr. Member
****
Offline Offline

Activity: 248
Merit: 250


View Profile
August 22, 2016, 07:19:27 PM
 #6

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.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!