Transaction validator test.
This commit is contained in:
parent
a1a707a966
commit
f60aad9abf
@ -20,6 +20,8 @@ class TransactionValidator
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* validate
|
* validate
|
||||||
|
* To do: check is data optional?
|
||||||
|
* Data is not optional on spec, see https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sendtransaction
|
||||||
*
|
*
|
||||||
* @param array $value
|
* @param array $value
|
||||||
* @return bool
|
* @return bool
|
||||||
@ -35,7 +37,7 @@ class TransactionValidator
|
|||||||
if (AddressValidator::validate($value['from']) === false) {
|
if (AddressValidator::validate($value['from']) === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (isset($value['to']) && AddressValidator::validate($value['to']) === false && $value['to'] !== '') {
|
if (isset($value['to']) && AddressValidator::validate($value['to']) === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (isset($value['gas']) && QuantityValidator::validate($value['gas']) === false) {
|
if (isset($value['gas']) && QuantityValidator::validate($value['gas']) === false) {
|
||||||
@ -47,9 +49,15 @@ class TransactionValidator
|
|||||||
if (isset($value['value']) && QuantityValidator::validate($value['value']) === false) {
|
if (isset($value['value']) && QuantityValidator::validate($value['value']) === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (isset($value['data']) && HexValidator::validate($value['data']) === false) {
|
if (!isset($value['data'])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (HexValidator::validate($value['data']) === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// if (isset($value['data']) && HexValidator::validate($value['data']) === false) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
if (isset($value['nonce']) && QuantityValidator::validate($value['nonce']) === false) {
|
if (isset($value['nonce']) && QuantityValidator::validate($value['nonce']) === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
59
test/unit/TransactionValidatorTest.php
Normal file
59
test/unit/TransactionValidatorTest.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Test\Unit;
|
||||||
|
|
||||||
|
use Test\TestCase;
|
||||||
|
use Web3\Validators\TransactionValidator;
|
||||||
|
|
||||||
|
class TransactionValidatorTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* validator
|
||||||
|
*
|
||||||
|
* @var \Web3\Validators\TransactionValidator
|
||||||
|
*/
|
||||||
|
protected $validator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* setUp
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->validator = new TransactionValidator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testValidate
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testValidate()
|
||||||
|
{
|
||||||
|
$validator = $this->validator;
|
||||||
|
|
||||||
|
$this->assertEquals(false, $validator->validate('hello web3.php'));
|
||||||
|
$this->assertEquals(false, $validator->validate([]));
|
||||||
|
$this->assertEquals(false, $validator->validate([
|
||||||
|
'from' => '',
|
||||||
|
]));
|
||||||
|
$this->assertEquals(false, $validator->validate([
|
||||||
|
'from' => '0xb60e8dd61c5d32be8058bb8eb970870f07233155',
|
||||||
|
'data' => ''
|
||||||
|
]));
|
||||||
|
$this->assertEquals(true, $validator->validate([
|
||||||
|
'from' => '0xb60e8dd61c5d32be8058bb8eb970870f07233155',
|
||||||
|
'data' => '0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675'
|
||||||
|
]));
|
||||||
|
$this->assertEquals(true, $validator->validate([
|
||||||
|
'from' => '0xb60e8dd61c5d32be8058bb8eb970870f07233155',
|
||||||
|
'to' => '0xd46e8dd67c5d32be8058bb8eb970870f07244567',
|
||||||
|
'gas' => '0x76c0',
|
||||||
|
'gasPrice' => '0x9184e72a000',
|
||||||
|
'value' => '0x9184e72a',
|
||||||
|
'data' => '0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675'
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user