diff --git a/src/Validators/TransactionValidator.php b/src/Validators/TransactionValidator.php index 4fd7b67..ccb3994 100644 --- a/src/Validators/TransactionValidator.php +++ b/src/Validators/TransactionValidator.php @@ -20,7 +20,9 @@ class TransactionValidator { /** * 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 * @return bool */ @@ -35,7 +37,7 @@ class TransactionValidator if (AddressValidator::validate($value['from']) === 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; } if (isset($value['gas']) && QuantityValidator::validate($value['gas']) === false) { @@ -47,9 +49,15 @@ class TransactionValidator if (isset($value['value']) && QuantityValidator::validate($value['value']) === false) { return false; } - if (isset($value['data']) && HexValidator::validate($value['data']) === false) { + if (!isset($value['data'])) { 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) { return false; } diff --git a/test/unit/TransactionValidatorTest.php b/test/unit/TransactionValidatorTest.php new file mode 100644 index 0000000..ce12915 --- /dev/null +++ b/test/unit/TransactionValidatorTest.php @@ -0,0 +1,59 @@ +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' + ])); + } +} \ No newline at end of file