Author

Topic: [Help] Bitcoin address validation (Read 892 times)

legendary
Activity: 1442
Merit: 1179
March 20, 2016, 09:49:09 PM
#5
As a reference for other readers rosettacode has a resource on how to verify a bitcoin address in 20 different programming languages.

https://rosettacode.org/wiki/Bitcoin/address_validation
sr. member
Activity: 420
Merit: 250
March 20, 2016, 10:47:42 AM
#4
Thank you all very much solved it Smiley
If someone have the same trouble here you go:

Quote

function ValidateBTC($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);
}
member
Activity: 112
Merit: 10
March 20, 2016, 10:30:19 AM
#3
Hello I have on my site bitcoin address validation but it's not working I don't know why. When I enter valid address it shows that this address not valid. Sad

There is code:
Quote
function ValidateBTC($address){
$decoded = decodeBase58($address);
$d1 = hash("sha256", substr($decoded,0,21), true);
$d2 = hash("sha256", $d1, true);
if(substr_compare($decoded, $d2, 21, 4)) Message(1, 'Invalid bitcoin wallet adress.');
return true;
}


function decodeBase58($input) {
$alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
$out = array_fill(0, 25, 0);
for($i=0;$iif(($p=strpos($alphabet, $input[$i])) === false)Message(1, 'Invalid bitcoin wallet adress.');
$c = $p;
for ($j = 25; $j--; ) {
$c += (int)(58 * $out[$j]);
$out[$j] = (int)($c % 256);
$c /= 256;
$c = (int)$c;
}
if($c != 0) Message(1, 'Invalid bitcoin wallet adress.');
}
$result = "";
foreach($out as $val) $result .= chr($val);
return $result;
}

Please help. Smiley

You can also check your address at blockchain . Its the best place to check for the btc transcations.
Hope it helps.
staff
Activity: 3374
Merit: 6530
Just writing some code
March 20, 2016, 10:24:37 AM
#2
I think you are just comparing it incorrectly. You need to compare the first 4 bytes of the double sha256 hash to the last 4 bytes of the decoded address.

Also, a byte is two characters, not one.
sr. member
Activity: 420
Merit: 250
March 20, 2016, 10:17:22 AM
#1
Hello I have on my site bitcoin address validation but it's not working I don't know why. When I enter valid address it shows that this address not valid. Sad

There is code:
Quote
function ValidateBTC($address){
$decoded = decodeBase58($address);
$d1 = hash("sha256", substr($decoded,0,21), true);
$d2 = hash("sha256", $d1, true);
if(substr_compare($decoded, $d2, 21, 4)) Message(1, 'Invalid bitcoin wallet adress.');
return true;
}


function decodeBase58($input) {
$alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
$out = array_fill(0, 25, 0);
for($i=0;$iif(($p=strpos($alphabet, $input[$i])) === false)Message(1, 'Invalid bitcoin wallet adress.');
$c = $p;
for ($j = 25; $j--; ) {
$c += (int)(58 * $out[$j]);
$out[$j] = (int)($c % 256);
$c /= 256;
$c = (int)$c;
}
if($c != 0) Message(1, 'Invalid bitcoin wallet adress.');
}
$result = "";
foreach($out as $val) $result .= chr($val);
return $result;
}

Please help. Smiley
Jump to: