Bit-Wasp/bitcoin-lib-php

A bug in base58_encode of BitcoinLib.php

sbwdlihao opened this issue · 3 comments

when i take 00010966776006953D5567439E5E39F86A0D273BEED61967F6 as param of base58_encode, the result is 1, i debug step by step and find that
$hex = "00010966776006953D5567439E5E39F86A0D273BEED61967F6"
$num = gmp_strval(gmp_init($hex, 16), 58);
where the value of num is false
so strange

i find another way to get the encode
public static function base58_encode($hex) {
if(strlen($hex) % 2 != 0)
{
die("encodeBase58: uneven number of hex characters");
}
$orighex = $hex;
$chars = self::$base58chars;
$hex = self::hex_decode($hex);
$return = "";
while (bccomp($hex, 0) == 1)
{
$dv = (string)bcdiv($hex, "58", 0);
$rem = (integer)bcmod($hex, "58");
$hex = $dv;
$return = $return . $chars[$rem];
}
$return = strrev($return);
//leading zeros
for($i = 0; $i < strlen($orighex) && substr($orighex, $i, 2) == "00"; $i += 2)
{
$return = "1".$return;
}
return $return;
}
this function return the right result:16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM

This is strange - it works for me! It's hardly caps.. and GMP is installed alright?

use \BitWasp\BitcoinLib\BitcoinLib;
require_once "../vendor/autoload.php";

$hex = "00010966776006953D5567439E5E39F86A0D273BEED61967F6";
echo gmp_strval( gmp_init($hex, 16), 58)."\n";
echo BitcoinLib::base58_encode($hex)."\n";

gives me:
5RsJJ8OfoZ2NcMmARrIkcGiAN6sKpgrK
16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM

I am aware of that code, but it uses bmath, which is slower than GMP.

seems to work fine for me too!

$hex = "00010966776006953D5567439E5E39F86A0D273BEED61967F6";
$num = gmp_strval(gmp_init($hex, 16), 58);
var_dump($num);

results in:

string(32) "5RsJJ8OfoZ2NcMmARrIkcGiAN6sKpgrK"

what php version are you running?

I will close this, as we have tests for this and GMP is required.