Bitcoin Forum
May 08, 2024, 02:32:22 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: [Help] Bitcoin address validation  (Read 891 times)
creepland (OP)
Sr. Member
****
Offline Offline

Activity: 420
Merit: 250


View Profile
March 20, 2016, 02:17:22 PM
 #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;$i<strlen($input);$i++){
if(($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

1715178742
Hero Member
*
Offline Offline

Posts: 1715178742

View Profile Personal Message (Offline)

Ignore
1715178742
Reply with quote  #2

1715178742
Report to moderator
1715178742
Hero Member
*
Offline Offline

Posts: 1715178742

View Profile Personal Message (Offline)

Ignore
1715178742
Reply with quote  #2

1715178742
Report to moderator
Activity + Trust + Earned Merit == The Most Recognized Users on Bitcointalk
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715178742
Hero Member
*
Offline Offline

Posts: 1715178742

View Profile Personal Message (Offline)

Ignore
1715178742
Reply with quote  #2

1715178742
Report to moderator
1715178742
Hero Member
*
Offline Offline

Posts: 1715178742

View Profile Personal Message (Offline)

Ignore
1715178742
Reply with quote  #2

1715178742
Report to moderator
1715178742
Hero Member
*
Offline Offline

Posts: 1715178742

View Profile Personal Message (Offline)

Ignore
1715178742
Reply with quote  #2

1715178742
Report to moderator
achow101
Staff
Legendary
*
Offline Offline

Activity: 3388
Merit: 6631


Just writing some code


View Profile WWW
March 20, 2016, 02:24:37 PM
 #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.

SanaButt
Member
**
Offline Offline

Activity: 112
Merit: 10


View Profile
March 20, 2016, 02:30:19 PM
 #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;$i<strlen($input);$i++){
if(($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.

▲▼▲▼▲▼▲▼  No.1 Bitcoin Binary Options and Double Dice  ▲▼▲▼▲▼▲▼
████████████████████████████████  sec◔nds trade  ████████████████████████████████
↑↓ Instant Bets ↑↓ Flexible 1~1440 minutes Expiry time ↑↓ Highest Reward 190% ↑↓ 16 Assets [btc, forex, gold, 1% edge double dice] ↑↓
creepland (OP)
Sr. Member
****
Offline Offline

Activity: 420
Merit: 250


View Profile
March 20, 2016, 02:47:42 PM
 #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);
}

coinableS
Legendary
*
Offline Offline

Activity: 1442
Merit: 1179



View Profile WWW
March 21, 2016, 01:49:09 AM
 #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

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!