Personal apis.

This commit is contained in:
sc0Vu 2018-01-11 16:30:54 +08:00
parent b48ac9f97f
commit e680f1adaf
6 changed files with 409 additions and 70 deletions

View File

@ -0,0 +1,88 @@
<?php
/**
* This file is part of web3.php package.
*
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
*
* @author Peter Lai <alk03073135@gmail.com>
* @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;
}
}

View File

@ -0,0 +1,91 @@
<?php
/**
* This file is part of web3.php package.
*
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
*
* @author Peter Lai <alk03073135@gmail.com>
* @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;
}
}

View File

@ -0,0 +1,92 @@
<?php
/**
* This file is part of web3.php package.
*
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
*
* @author Peter Lai <alk03073135@gmail.com>
* @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;
}
}

View File

@ -0,0 +1,116 @@
<?php
/**
* This file is part of web3.php package.
*
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
*
* @author Peter Lai <alk03073135@gmail.com>
* @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;
}
}

View File

@ -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);
}
}

View File

@ -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());
}