From e680f1adaf28026e014d613a9cc192be74bb64f5 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Thu, 11 Jan 2018 16:30:54 +0800 Subject: [PATCH] Personal apis. --- src/Methods/Personal/ListAccounts.php | 88 +++++++++++++++++ src/Methods/Personal/NewAccount.php | 91 ++++++++++++++++++ src/Methods/Personal/SendTransaction.php | 92 ++++++++++++++++++ src/Methods/Personal/UnlockAccount.php | 116 +++++++++++++++++++++++ src/Personal.php | 90 ++++-------------- test/unit/PersonalApiTest.php | 2 +- 6 files changed, 409 insertions(+), 70 deletions(-) create mode 100644 src/Methods/Personal/ListAccounts.php create mode 100644 src/Methods/Personal/NewAccount.php create mode 100644 src/Methods/Personal/SendTransaction.php create mode 100644 src/Methods/Personal/UnlockAccount.php diff --git a/src/Methods/Personal/ListAccounts.php b/src/Methods/Personal/ListAccounts.php new file mode 100644 index 0000000..09ae40a --- /dev/null +++ b/src/Methods/Personal/ListAccounts.php @@ -0,0 +1,88 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Personal; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; + +class ListAccounts extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = []; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file diff --git a/src/Methods/Personal/NewAccount.php b/src/Methods/Personal/NewAccount.php new file mode 100644 index 0000000..3ac77a6 --- /dev/null +++ b/src/Methods/Personal/NewAccount.php @@ -0,0 +1,91 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Personal; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; +use Web3\Formatters\StringFormatter; + +class NewAccount extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + StringFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file diff --git a/src/Methods/Personal/SendTransaction.php b/src/Methods/Personal/SendTransaction.php new file mode 100644 index 0000000..a5a11b7 --- /dev/null +++ b/src/Methods/Personal/SendTransaction.php @@ -0,0 +1,92 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Personal; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; +use Web3\Formatters\TransactionFormatter; +use Web3\Formatters\StringFormatter; + +class SendTransaction extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + TransactionFormatter::class, StringFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + foreach ($params as $key => $param) { + if (isset($rules[$key])) { + $params[$key] = call_user_func([$rules[$key], 'format'], $param); + } + } + return $params; + } +} \ No newline at end of file diff --git a/src/Methods/Personal/UnlockAccount.php b/src/Methods/Personal/UnlockAccount.php new file mode 100644 index 0000000..67ab8c7 --- /dev/null +++ b/src/Methods/Personal/UnlockAccount.php @@ -0,0 +1,116 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Methods\Personal; + +use InvalidArgumentException; +use Web3\Methods\IMethod; +use Web3\Methods\JSONRPC; +use Web3\Formatters\AddressFormatter; +use Web3\Formatters\StringFormatter; +use Web3\Formatters\QuantityFormatter; + +class UnlockAccount extends JSONRPC implements IMethod +{ + /** + * inputFormatters + * + * @var array + */ + protected $inputFormatters = [ + AddressFormatter::class, StringFormatter::class, QuantityFormatter::class + ]; + + /** + * outputFormatters + * + * @var array + */ + protected $outputFormatters = []; + + /** + * defaultValues + * + * @var array + */ + private $defaultValues = [ + 'personal_unlockAccount' => [ + 2 => 300 + ] + ]; + + /** + * construct + * + * @param string $method + * @param array $arguments + * @return void + */ + // public function __construct($method='', $arguments=[]) + // { + // parent::__construct($method, $arguments); + // } + + /** + * getInputFormatters + * + * @return array + */ + public function getInputFormatters() + { + return $this->inputFormatters; + } + + /** + * getOutputFormatters + * + * @return array + */ + public function getOutputFormatters() + { + return $this->outputFormatters; + } + + /** + * transform + * + * @param array $params + * @param array $rules + * @return array + */ + public function transform($params, $rules) + { + if (!is_array($params)) { + throw new InvalidArgumentException('Please use array params when call transform.'); + } + if (!is_array($rules)) { + throw new InvalidArgumentException('Please use array rules when call transform.'); + } + if (count($params) < count($rules)) { + if (!isset($this->defaultValues[$this->method])) { + throw new \InvalidArgumentException('The params are less than inputFormatters.'); + } + $defaultValues = $this->defaultValues[$this->method]; + + 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); + } + } + return $params; + } +} \ No newline at end of file diff --git a/src/Personal.php b/src/Personal.php index a177b7f..5563581 100644 --- a/src/Personal.php +++ b/src/Personal.php @@ -34,36 +34,15 @@ class Personal * * @var array */ - private $methods = [ - 'personal_listAccounts' => [], - 'personal_newAccount' => [ - 'params' => [ - [ - 'validators' => StringValidator::class - ] - ] - ], - 'personal_unlockAccount' => [ - 'params' => [ - [ - 'validators' => AddressValidator::class - ], [ - 'validators' => StringValidator::class - ], [ - 'default' => 300, - 'validators' => QuantityValidator::class - ] - ] - ], - 'personal_sendTransaction' => [ - 'params' => [ - [ - 'validators' => TransactionValidator::class - ], [ - 'validators' => StringValidator::class - ] - ] - ] + private $methods = []; + + /** + * allowedMethods + * + * @var array + */ + private $allowedMethods = [ + 'personal_listAccounts', 'personal_newAccount', 'personal_unlockAccount', 'personal_sendTransaction' ]; /** @@ -104,11 +83,9 @@ class Personal if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) { $method = strtolower($class[1]) . '_' . $name; - if (!array_key_exists($method, $this->methods)) { + if (!in_array($method, $this->allowedMethods)) { throw new \RuntimeException('Unallowed rpc method: ' . $method); } - $allowedMethod = $this->methods[$method]; - if ($this->provider->isBatch) { $callback = null; } else { @@ -118,43 +95,18 @@ class Personal throw new \InvalidArgumentException('The last param must be callback function.'); } } - if (isset($allowedMethod['params']) && is_array($allowedMethod['params'])) { - // validate params - foreach ($allowedMethod['params'] as $key => $param) { - if (isset($param['validators'])) { - if (is_array($param['validators'])) { - $isError = true; - - foreach ($param['validators'] as $rule) { - if (isset($arguments[$key])) { - if (call_user_func([$rule, 'validate'], $arguments[$key]) === true) { - $isError = false; - break; - } - } else { - if (isset($param['default'])) { - $arguments[$key] = $param['default']; - $isError = false; - break; - } - } - } - if ($isError === true) { - throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.'); - } - } else { - if (!isset($arguments[$key]) || call_user_func([$param['validators'], 'validate'], $arguments[$key]) === false) { - if (isset($param['default']) && !isset($arguments[$key])) { - $arguments[$key] = $param['default']; - } else { - throw new \RuntimeException('Wrong type of ' . $name . ' method argument ' . $key . '.'); - } - } - } - } - } + if (!array_key_exists($method, $this->methods)) { + // new the method + $methodClass = sprintf("\Web3\Methods\%s\%s", ucfirst($class[1]), ucfirst($name)); + $methodObject = new $methodClass($method, $arguments); + $this->methods[$method] = $methodObject; + } else { + $methodObject = $this->methods[$method]; } - $this->provider->send($method, $arguments, $callback); + $inputs = $methodObject->transform($arguments, $methodObject->inputFormatters); + $methodObject->arguments = $inputs; + + $this->provider->send($methodObject, $callback); } } diff --git a/test/unit/PersonalApiTest.php b/test/unit/PersonalApiTest.php index 616094c..e9a449a 100644 --- a/test/unit/PersonalApiTest.php +++ b/test/unit/PersonalApiTest.php @@ -135,7 +135,7 @@ class PersonalApiTest extends TestCase $personal = $this->personal; - $personal->newAccount(123456, function ($err, $account) { + $personal->newAccount($personal, function ($err, $account) { if ($err !== null) { return $this->fail($err->getMessage()); }