Personal apis.
This commit is contained in:
parent
b48ac9f97f
commit
e680f1adaf
88
src/Methods/Personal/ListAccounts.php
Normal file
88
src/Methods/Personal/ListAccounts.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
91
src/Methods/Personal/NewAccount.php
Normal file
91
src/Methods/Personal/NewAccount.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
92
src/Methods/Personal/SendTransaction.php
Normal file
92
src/Methods/Personal/SendTransaction.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
116
src/Methods/Personal/UnlockAccount.php
Normal file
116
src/Methods/Personal/UnlockAccount.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -34,36 +34,15 @@ class Personal
|
|||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $methods = [
|
private $methods = [];
|
||||||
'personal_listAccounts' => [],
|
|
||||||
'personal_newAccount' => [
|
/**
|
||||||
'params' => [
|
* allowedMethods
|
||||||
[
|
*
|
||||||
'validators' => StringValidator::class
|
* @var array
|
||||||
]
|
*/
|
||||||
]
|
private $allowedMethods = [
|
||||||
],
|
'personal_listAccounts', 'personal_newAccount', 'personal_unlockAccount', 'personal_sendTransaction'
|
||||||
'personal_unlockAccount' => [
|
|
||||||
'params' => [
|
|
||||||
[
|
|
||||||
'validators' => AddressValidator::class
|
|
||||||
], [
|
|
||||||
'validators' => StringValidator::class
|
|
||||||
], [
|
|
||||||
'default' => 300,
|
|
||||||
'validators' => QuantityValidator::class
|
|
||||||
]
|
|
||||||
]
|
|
||||||
],
|
|
||||||
'personal_sendTransaction' => [
|
|
||||||
'params' => [
|
|
||||||
[
|
|
||||||
'validators' => TransactionValidator::class
|
|
||||||
], [
|
|
||||||
'validators' => StringValidator::class
|
|
||||||
]
|
|
||||||
]
|
|
||||||
]
|
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -104,11 +83,9 @@ class Personal
|
|||||||
if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
|
if (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)) {
|
if (!in_array($method, $this->allowedMethods)) {
|
||||||
throw new \RuntimeException('Unallowed rpc method: ' . $method);
|
throw new \RuntimeException('Unallowed rpc method: ' . $method);
|
||||||
}
|
}
|
||||||
$allowedMethod = $this->methods[$method];
|
|
||||||
|
|
||||||
if ($this->provider->isBatch) {
|
if ($this->provider->isBatch) {
|
||||||
$callback = null;
|
$callback = null;
|
||||||
} else {
|
} else {
|
||||||
@ -118,43 +95,18 @@ class Personal
|
|||||||
throw new \InvalidArgumentException('The last param must be callback function.');
|
throw new \InvalidArgumentException('The last param must be callback function.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isset($allowedMethod['params']) && is_array($allowedMethod['params'])) {
|
if (!array_key_exists($method, $this->methods)) {
|
||||||
// validate params
|
// new the method
|
||||||
foreach ($allowedMethod['params'] as $key => $param) {
|
$methodClass = sprintf("\Web3\Methods\%s\%s", ucfirst($class[1]), ucfirst($name));
|
||||||
if (isset($param['validators'])) {
|
$methodObject = new $methodClass($method, $arguments);
|
||||||
if (is_array($param['validators'])) {
|
$this->methods[$method] = $methodObject;
|
||||||
$isError = true;
|
} else {
|
||||||
|
$methodObject = $this->methods[$method];
|
||||||
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 . '.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$this->provider->send($method, $arguments, $callback);
|
$inputs = $methodObject->transform($arguments, $methodObject->inputFormatters);
|
||||||
|
$methodObject->arguments = $inputs;
|
||||||
|
|
||||||
|
$this->provider->send($methodObject, $callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +135,7 @@ class PersonalApiTest extends TestCase
|
|||||||
|
|
||||||
$personal = $this->personal;
|
$personal = $this->personal;
|
||||||
|
|
||||||
$personal->newAccount(123456, function ($err, $account) {
|
$personal->newAccount($personal, function ($err, $account) {
|
||||||
if ($err !== null) {
|
if ($err !== null) {
|
||||||
return $this->fail($err->getMessage());
|
return $this->fail($err->getMessage());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user