diff --git a/src/Contracts/Ethabi.php b/src/Contracts/Ethabi.php new file mode 100644 index 0000000..cb23c97 --- /dev/null +++ b/src/Contracts/Ethabi.php @@ -0,0 +1,87 @@ + + * + * @author Peter Lai + * @license MIT + */ + +namespace Web3\Contracts; + +use Web3\Utils; + +class Ethabi +{ + /** + * construct + * + * @return void + */ + public function __contruct() + { + // + } + + /** + * get + * + * @param string $name + * @return mixed + */ + public function __get($name) + { + $method = 'get' . ucfirst($name); + + if (method_exists($this, $method)) { + return call_user_func_array([$this, $method], []); + } + } + + /** + * set + * + * @param string $name + * @param mixed $value + * @return bool + */ + public function __set($name, $value) + { + $method = 'set' . ucfirst($name); + + if (method_exists($this, $method)) { + return call_user_func_array([$this, $method], [$value]); + } + return false; + } + + /** + * encodeFunctionSignature + * + * @param string|stdClass|array $functionName + * @return string + */ + public function encodeFunctionSignature($functionName) + { + if (!is_string($functionName)) { + $functionName = Utils::jsonMethodToString($functionName); + } + return mb_substr(Utils::sha3($functionName), 0, 10); + } + + /** + * encodeEventSignature + * + * @param string|stdClass|array $functionName + * @return string + */ + public function encodeEventSignature($functionName) + { + if (!is_string($functionName)) { + $functionName = Utils::jsonMethodToString($functionName); + } + return Utils::sha3($functionName); + } +} \ No newline at end of file diff --git a/test/unit/EthabiTest.php b/test/unit/EthabiTest.php new file mode 100644 index 0000000..4e0a9f6 --- /dev/null +++ b/test/unit/EthabiTest.php @@ -0,0 +1,97 @@ +abi = new Ethabi(); + } + + /** + * testEncodeFunctionSignature + * + * @return void + */ + public function testEncodeFunctionSignature() + { + $abi = $this->abi; + $str = $abi->encodeFunctionSignature('baz(uint32,bool)'); + + $this->assertEquals($str, '0xcdcd77c0'); + + $json = json_decode($this->testJsonMethodString); + $methodString = Utils::jsonMethodToString($json); + $str = $abi->encodeFunctionSignature($methodString); + + $this->assertEquals($str, '0x095ea7b3'); + } + + /** + * testEncodeEventSignature + * + * @return void + */ + public function testEncodeEventSignature() + { + $abi = $this->abi; + $str = $abi->encodeEventSignature('baz(uint32,bool)'); + + $this->assertEquals($str, '0xcdcd77c0992ec5bbfc459984220f8c45084cc24d9b6efed1fae540db8de801d2'); + + $json = json_decode($this->testJsonMethodString); + $methodString = Utils::jsonMethodToString($json); + $str = $abi->encodeEventSignature($methodString); + + $this->assertEquals($str, '0x095ea7b334ae44009aa867bfb386f5c3b4b443ac6f0ee573fa91c4608fbadfba'); + } +} \ No newline at end of file