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) public static function toString($value)
{ {
try { $value = (string) $value;
$value = (string) $value;
} catch (\Exception $e) {
throw new RuntimeException('Cannot transform value to string!');
}
return $value; return $value;
} }
@ -436,7 +433,9 @@ class Utils
} else { } else {
$bn = new BigNumber($number); $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.'); throw new InvalidArgumentException('toBn number must be BigNumber, string or int.');
} }
return $bn; return $bn;

View File

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