From 0f59ac92235b9abb60ba38e72ad98bcb006ef3f8 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Wed, 3 Jan 2018 14:51:30 +0800 Subject: [PATCH] CallValidator. --- src/Eth.php | 3 +- src/Validators/CallValidator.php | 55 ++++++++++++++++++++++++++++++++ test/unit/EthApiTest.php | 2 +- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/Validators/CallValidator.php diff --git a/src/Eth.php b/src/Eth.php index b053d5b..e97fad7 100644 --- a/src/Eth.php +++ b/src/Eth.php @@ -25,6 +25,7 @@ use Web3\Validators\BooleanValidator; use Web3\Validators\StringValidator; use Web3\Validators\FilterValidator; use Web3\Validators\NonceValidator; +use Web3\Validators\CallValidator; class Eth { @@ -159,7 +160,7 @@ class Eth 'eth_call' => [ 'params' => [ [ - 'validators' => TransactionValidator::class + 'validators' => CallValidator::class ], [ 'default' => 'latest', 'validators' => [ diff --git a/src/Validators/CallValidator.php b/src/Validators/CallValidator.php new file mode 100644 index 0000000..428ddb7 --- /dev/null +++ b/src/Validators/CallValidator.php @@ -0,0 +1,55 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Validators; + +use Web3\Validators\IValidator; +use Web3\Validators\QuantityValidator; +use Web3\Validators\TagValidator; +use Web3\Validators\HexValidator; + +class CallValidator +{ + /** + * validate + * + * @param array $value + * @return bool + */ + public static function validate($value) + { + if (!is_array($value)) { + return false; + } + if (isset($value['from']) && AddressValidator::validate($value['from']) === false && $value['from'] !== '') { + return false; + } + if (isset($value['to']) && AddressValidator::validate($value['to']) === false && $value['to'] !== '') { + return false; + } + if (isset($value['gas']) && QuantityValidator::validate($value['gas']) === false) { + return false; + } + if (isset($value['gasPrice']) && QuantityValidator::validate($value['gasPrice']) === false) { + return false; + } + if (isset($value['value']) && QuantityValidator::validate($value['value']) === false) { + return false; + } + if (isset($value['data']) && HexValidator::validate($value['data']) === false) { + return false; + } + if (isset($value['nonce']) && QuantityValidator::validate($value['nonce']) === false) { + return false; + } + return true; + } +} \ No newline at end of file diff --git a/test/unit/EthApiTest.php b/test/unit/EthApiTest.php index 4e0fcd1..85b8328 100644 --- a/test/unit/EthApiTest.php +++ b/test/unit/EthApiTest.php @@ -472,7 +472,7 @@ class EthApiTest extends TestCase $eth = $this->eth; $eth->call([ - 'from' => "0xb60e8dd61c5d32be8058bb8eb970870f07233155", + // 'from' => "0xb60e8dd61c5d32be8058bb8eb970870f07233155", 'to' => "0xd46e8dd67c5d32be8058bb8eb970870f07244567", 'gas' => "0x76c0", 'gasPrice' => "0x9184e72a000",