From d52a8ddfa0e5f875728f333ff55c094b7f209487 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Sun, 21 Jan 2018 16:20:53 +0800 Subject: [PATCH 1/3] Add test for unlockAccount with duration. --- test/unit/PersonalApiTest.php | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/test/unit/PersonalApiTest.php b/test/unit/PersonalApiTest.php index 9924b0a..a082f8b 100644 --- a/test/unit/PersonalApiTest.php +++ b/test/unit/PersonalApiTest.php @@ -88,8 +88,33 @@ class PersonalApiTest extends TestCase $personal->unlockAccount($this->newAccount, '123456', function ($err, $unlocked) { if ($err !== null) { - // infura banned us to use unlock account - return $this->assertTrue($err->getCode() === 405); + return $this->fail($err->getMessage()); + } + $this->assertTrue($unlocked); + }); + } + + /** + * testUnlockAccountWithDuration + * + * @return void + */ + public function testUnlockAccountWithDuration() + { + $personal = $this->personal; + + // create account + $personal->newAccount('123456', function ($err, $account) { + if ($err !== null) { + return $this->fail($e->getMessage()); + } + $this->newAccount = $account; + $this->assertTrue(is_string($account)); + }); + + $personal->unlockAccount($this->newAccount, '123456', 100, function ($err, $unlocked) { + if ($err !== null) { + return $this->fail($err->getMessage()); } $this->assertTrue($unlocked); }); From 85b5cea599dc654bd547cb217ccee21d3914ee3e Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Sun, 21 Jan 2018 16:21:29 +0800 Subject: [PATCH 2/3] Pass reference Let EthMethod validate pass reference that we can add default values directly. --- src/Methods/EthMethod.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Methods/EthMethod.php b/src/Methods/EthMethod.php index ca11ce7..f6621f1 100644 --- a/src/Methods/EthMethod.php +++ b/src/Methods/EthMethod.php @@ -84,7 +84,7 @@ class EthMethod extends JSONRPC implements IMethod * @param array $params * @return bool */ - public function validate($params) + public function validate(&$params) { if (!is_array($params)) { throw new InvalidArgumentException('Please use array params when call validate.'); From f45b34f33db930ee4f546914da351d0f9d565a66 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Sun, 21 Jan 2018 16:37:17 +0800 Subject: [PATCH 3/3] Number formatter. --- src/Formatters/NumberFormatter.php | 34 ++++++++++++++++++++++++++ src/Methods/EthMethod.php | 26 ++++++++++---------- src/Methods/Personal/UnlockAccount.php | 4 +-- 3 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 src/Formatters/NumberFormatter.php diff --git a/src/Formatters/NumberFormatter.php b/src/Formatters/NumberFormatter.php new file mode 100644 index 0000000..45fd6de --- /dev/null +++ b/src/Formatters/NumberFormatter.php @@ -0,0 +1,34 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Formatters; + +use InvalidArgumentException; +use Web3\Utils; +use Web3\Formatters\IFormatter; + +class NumberFormatter implements IFormatter +{ + /** + * format + * + * @param mixed $value + * @return int + */ + public static function format($value) + { + $value = Utils::toString($value); + $bn = Utils::toBn($value); + $int = (int) $bn->toString(); + + return $int; + } +} \ No newline at end of file diff --git a/src/Methods/EthMethod.php b/src/Methods/EthMethod.php index f6621f1..593b381 100644 --- a/src/Methods/EthMethod.php +++ b/src/Methods/EthMethod.php @@ -147,22 +147,22 @@ class EthMethod extends JSONRPC implements IMethod if (!is_array($rules)) { throw new InvalidArgumentException('Please use array rules when call transform.'); } - if (count($params) < count($rules)) { - if (!isset($this->defaultValues) || empty($this->defaultValues)) { - throw new \InvalidArgumentException('The params are less than inputFormatters.'); - } - $defaultValues = $this->defaultValues; + // $defaultValues = $this->defaultValues; - foreach ($defaultValues as $key => $value) { - if (!isset($params[$key])) { - $params[$key] = $value; - } - - } - } foreach ($params as $key => $param) { if (isset($rules[$key])) { - $params[$key] = call_user_func([$rules[$key], 'format'], $param); + $formatted = call_user_func([$rules[$key], 'format'], $param); + + // if (is_int($formatted)) { + // if ($formatted >= 0) { + // $params[$key] = $formatted; + // } elseif (isset($defaultValues[$key])) { + // $params[$key] = $defaultValues[$key]; + // } + // } else { + // $params[$key] = $formatted; + // } + $params[$key] = $formatted; } } return $params; diff --git a/src/Methods/Personal/UnlockAccount.php b/src/Methods/Personal/UnlockAccount.php index 0e2155d..a28143b 100644 --- a/src/Methods/Personal/UnlockAccount.php +++ b/src/Methods/Personal/UnlockAccount.php @@ -18,7 +18,7 @@ use Web3\Validators\StringValidator; use Web3\Validators\QuantityValidator; use Web3\Formatters\AddressFormatter; use Web3\Formatters\StringFormatter; -use Web3\Formatters\QuantityFormatter; +use Web3\Formatters\NumberFormatter; class UnlockAccount extends EthMethod { @@ -37,7 +37,7 @@ class UnlockAccount extends EthMethod * @var array */ protected $inputFormatters = [ - AddressFormatter::class, StringFormatter::class, QuantityFormatter::class + AddressFormatter::class, StringFormatter::class, NumberFormatter::class ]; /**