From 829e65bbb98915b2bcce384511993a7044e089f6 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Tue, 12 Dec 2017 18:08:03 +0800 Subject: [PATCH] Methods and validator Web3 allowed methods: * clientVersion * sha3 Validator: * HexValidator --- src/Validators/HexValidator.php | 22 +++++++++++++++ src/Validators/IValidators.php | 14 ++++++++++ src/Web3.php | 28 ++++++++++++++++++++ test/unit/Web3Test.php | 47 +++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 src/Validators/HexValidator.php create mode 100644 src/Validators/IValidators.php diff --git a/src/Validators/HexValidator.php b/src/Validators/HexValidator.php new file mode 100644 index 0000000..21e13f7 --- /dev/null +++ b/src/Validators/HexValidator.php @@ -0,0 +1,22 @@ += 1); + } +} \ No newline at end of file diff --git a/src/Validators/IValidators.php b/src/Validators/IValidators.php new file mode 100644 index 0000000..c92dc7d --- /dev/null +++ b/src/Validators/IValidators.php @@ -0,0 +1,14 @@ + [], + 'web3_sha3' => [ + 'params' => [ + HexValidator::class + ] + ] + ]; + /** * construct * @@ -62,6 +77,19 @@ class Web3 if (strtolower($class[0]) === 'web3' && preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) { $method = strtolower($class[1]) . '_' . $name; + if (!array_key_exists($method, $this->methods)) { + throw new \RuntimeException('Unallowed rpc method: ' . $method); + } + $allowedMethod = $this->methods[$method]; + + if (isset($allowedMethod['params'])) { + // validate params + foreach ($allowedMethod['params'] as $key => $rule) { + if (call_user_func([$rule, 'validate'], $arguments[$key]) === false) { + throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.'); + } + } + } if ($this->provider->isBatch) { $this->provider->send($method, $arguments, null); } else { diff --git a/test/unit/Web3Test.php b/test/unit/Web3Test.php index 315f72e..013de51 100644 --- a/test/unit/Web3Test.php +++ b/test/unit/Web3Test.php @@ -2,6 +2,7 @@ namespace Test\Unit; +use RuntimeException; use Test\TestCase; use Web3\Web3; use Web3\Eth; @@ -111,4 +112,50 @@ class Web3Test extends TestCase $this->assertEquals($data[1]->result, $this->testHash); }); } + + /** + * testUnallowedMethod + * + * @return void + */ + public function testUnallowedMethod() + { + $this->expectException(RuntimeException::class); + + $web3 = $this->web3; + + $web3->hello(function ($err, $hello) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + if (isset($hello->result)) { + $this->assertTrue(true); + } else { + $this->fail($hello->error->message); + } + }); + } + + /** + * testWrongParam + * + * @return void + */ + public function testWrongParam() + { + $this->expectException(RuntimeException::class); + + $web3 = $this->web3; + + $web3->sha3('hello world', function ($err, $hash) { + if ($err !== null) { + return $this->fail($err->getMessage()); + } + if (isset($hash->result)) { + $this->assertTrue(true); + } else { + $this->fail($hash->error->message); + } + }); + } } \ No newline at end of file