Methods and validator

Web3 allowed methods:
* clientVersion
* sha3

Validator:
* HexValidator
This commit is contained in:
sc0Vu 2017-12-12 18:08:03 +08:00
parent 5d33c2ade3
commit 829e65bbb9
4 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,22 @@
<?php
namespace Web3\Validators;
use Web3\Validators\IValidator;
class HexValidator
{
/**
* validate
*
* @param string $value
* @return bool
*/
public static function validate($value)
{
if (!is_string($value)) {
return false;
}
return (preg_match('/^0x[a-fA-F0-9]+$/', $value) >= 1);
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Web3\Validators;
interface IValidators
{
/**
* validate
*
* @param mixed $value
* @return bool
*/
public static function validate($value);
}

View File

@ -7,6 +7,7 @@ use Web3\Providers\Provider;
use Web3\Providers\HttpProvider; use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager; use Web3\RequestManagers\RequestManager;
use Web3\RequestManagers\HttpRequestManager; use Web3\RequestManagers\HttpRequestManager;
use Web3\Validators\HexValidator;
class Web3 class Web3
{ {
@ -24,6 +25,20 @@ class Web3
*/ */
protected $eth; protected $eth;
/**
* methods
*
* @var array
*/
private $methods = [
'web3_clientVersion' => [],
'web3_sha3' => [
'params' => [
HexValidator::class
]
]
];
/** /**
* construct * construct
* *
@ -62,6 +77,19 @@ class Web3
if (strtolower($class[0]) === 'web3' && preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) { if (strtolower($class[0]) === 'web3' && preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
$method = strtolower($class[1]) . '_' . $name; $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) { if ($this->provider->isBatch) {
$this->provider->send($method, $arguments, null); $this->provider->send($method, $arguments, null);
} else { } else {

View File

@ -2,6 +2,7 @@
namespace Test\Unit; namespace Test\Unit;
use RuntimeException;
use Test\TestCase; use Test\TestCase;
use Web3\Web3; use Web3\Web3;
use Web3\Eth; use Web3\Eth;
@ -111,4 +112,50 @@ class Web3Test extends TestCase
$this->assertEquals($data[1]->result, $this->testHash); $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);
}
});
}
} }