Fix toBn bug

Fix $bn not define when call toBn with BigNumber instance.
This commit is contained in:
sc0Vu 2018-02-06 18:05:55 +08:00
parent 926ecca129
commit f231b23bb9
2 changed files with 12 additions and 10 deletions

View File

@ -235,11 +235,8 @@ class Utils
*/
public static function toString($value)
{
try {
$value = (string) $value;
} catch (\Exception $e) {
throw new RuntimeException('Cannot transform value to string!');
}
$value = (string) $value;
return $value;
}
@ -436,7 +433,9 @@ class Utils
} else {
$bn = new BigNumber($number);
}
} elseif (!$number instanceof BigNumber){
} elseif ($number instanceof BigNumber){
$bn = $number;
} else {
throw new InvalidArgumentException('toBn number must be BigNumber, string or int.');
}
return $bn;

View File

@ -3,6 +3,7 @@
namespace Test\Unit;
use InvalidArgumentException;
use stdClass;
use Test\TestCase;
use phpseclib\Math\BigInteger as BigNumber;
use Web3\Utils;
@ -410,19 +411,21 @@ class UtilsTest extends TestCase
public function testToBn()
{
$bn = Utils::toBn(11);
$this->assertEquals($bn->toString(), '11');
$bn = Utils::toBn('0x12');
$this->assertEquals($bn->toString(), '18');
$bn = Utils::toBn(0x12);
$this->assertEquals($bn->toString(), '18');
$bn = Utils::toBn('ae');
$this->assertEquals($bn->toString(), '174');
$bn = Utils::toBn(new BigNumber(1));
$this->assertEquals($bn->toString(), '1');
$this->expectException(InvalidArgumentException::class);
$bn = Utils::toBn(new stdClass);
}
}