Fix decode dynamic bytes

* Fix outputFormat function in dynamic bytes
* Add test test for issue 85 #85
This commit is contained in:
sc0Vu 2018-06-24 21:59:08 +08:00
parent d5c21afec0
commit 07f730d8ce
10 changed files with 305 additions and 9 deletions

View File

@ -22,6 +22,7 @@ use Web3\Contracts\Ethabi;
use Web3\Contracts\Types\Address; use Web3\Contracts\Types\Address;
use Web3\Contracts\Types\Boolean; use Web3\Contracts\Types\Boolean;
use Web3\Contracts\Types\Bytes; use Web3\Contracts\Types\Bytes;
use Web3\Contracts\Types\DynamicBytes;
use Web3\Contracts\Types\Integer; use Web3\Contracts\Types\Integer;
use Web3\Contracts\Types\Str; use Web3\Contracts\Types\Str;
use Web3\Contracts\Types\Uinteger; use Web3\Contracts\Types\Uinteger;
@ -133,6 +134,7 @@ class Contract
'address' => new Address, 'address' => new Address,
'bool' => new Boolean, 'bool' => new Boolean,
'bytes' => new Bytes, 'bytes' => new Bytes,
'dynamicBytes' => new DynamicBytes,
'int' => new Integer, 'int' => new Integer,
'string' => new Str, 'string' => new Str,
'uint' => new Uinteger, 'uint' => new Uinteger,

View File

@ -280,7 +280,12 @@ class Ethabi
$className = $this->types[$match[0]]; $className = $this->types[$match[0]];
if (call_user_func([$this->types[$match[0]], 'isType'], $type) === false) { if (call_user_func([$this->types[$match[0]], 'isType'], $type) === false) {
throw new InvalidArgumentException('Unsupport solidity parameter type: ' . $type); // check dynamic bytes
if ($match[0] === 'bytes') {
$className = $this->types['dynamicBytes'];
} else {
throw new InvalidArgumentException('Unsupport solidity parameter type: ' . $type);
}
} }
$solidityTypes[$key] = $className; $solidityTypes[$key] = $className;
} }

View File

@ -37,7 +37,7 @@ class Address extends SolidityType implements IType
*/ */
public function isType($name) public function isType($name)
{ {
return (preg_match('/^address(\[([0-9]*)\])*/', $name) === 1); return (preg_match('/^address(\[([0-9]*)\])*$/', $name) === 1);
} }
/** /**

View File

@ -35,7 +35,7 @@ class Boolean extends SolidityType implements IType
*/ */
public function isType($name) public function isType($name)
{ {
return (preg_match('/^bool(\[([0-9]*)\])*/', $name) === 1); return (preg_match('/^bool(\[([0-9]*)\])*$/', $name) === 1);
} }
/** /**

View File

@ -36,7 +36,7 @@ class DynamicBytes extends SolidityType implements IType
*/ */
public function isType($name) public function isType($name)
{ {
return (preg_match('/^bytes(\[([0-9]*)\])*/', $name) === 1); return (preg_match('/^bytes(\[([0-9]*)\])*$/', $name) === 1);
} }
/** /**
@ -89,6 +89,9 @@ class DynamicBytes extends SolidityType implements IType
if (empty($checkZero)) { if (empty($checkZero)) {
return '0'; return '0';
} }
return '0x' . $value; $size = intval(Utils::toBn(mb_substr($value, 0, 64))->toString());
$length = 2 * $size;
return '0x' . mb_substr($value, 64, $length);
} }
} }

View File

@ -37,7 +37,7 @@ class Integer extends SolidityType implements IType
*/ */
public function isType($name) public function isType($name)
{ {
return (preg_match('/^int([0-9]{1,})?(\[([0-9]*)\])*/', $name) === 1); return (preg_match('/^int([0-9]{1,})?(\[([0-9]*)\])*$/', $name) === 1);
} }
/** /**

View File

@ -37,7 +37,7 @@ class Str extends SolidityType implements IType
*/ */
public function isType($name) public function isType($name)
{ {
return (preg_match('/^string(\[([0-9]*)\])*/', $name) === 1); return (preg_match('/^string(\[([0-9]*)\])*$/', $name) === 1);
} }
/** /**

View File

@ -37,7 +37,7 @@ class Uinteger extends SolidityType implements IType
*/ */
public function isType($name) public function isType($name)
{ {
return (preg_match('/^uint([0-9]{1,})?(\[([0-9]*)\])*/', $name) === 1); return (preg_match('/^uint([0-9]{1,})?(\[([0-9]*)\])*$/', $name) === 1);
} }
/** /**

File diff suppressed because one or more lines are too long

View File

@ -9,6 +9,7 @@ use Web3\Contracts\Ethabi;
use Web3\Contracts\Types\Address; use Web3\Contracts\Types\Address;
use Web3\Contracts\Types\Boolean; use Web3\Contracts\Types\Boolean;
use Web3\Contracts\Types\Bytes; use Web3\Contracts\Types\Bytes;
use Web3\Contracts\Types\DynamicBytes;
use Web3\Contracts\Types\Integer; use Web3\Contracts\Types\Integer;
use Web3\Contracts\Types\Str; use Web3\Contracts\Types\Str;
use Web3\Contracts\Types\Uinteger; use Web3\Contracts\Types\Uinteger;
@ -179,9 +180,10 @@ class EthabiTest extends TestCase
'address' => new Address, 'address' => new Address,
'bool' => new Boolean, 'bool' => new Boolean,
'bytes' => new Bytes, 'bytes' => new Bytes,
'dynamicBytes' => new DynamicBytes,
'int' => new Integer, 'int' => new Integer,
'string' => new Str, 'string' => new Str,
'uint' => new Uinteger, 'uint' => new Uinteger
]); ]);
} }