Ethabi
1. encodeFunctionSignature 2. encodeEventSignature
This commit is contained in:
parent
e25e0cc3f1
commit
89caf38edc
87
src/Contracts/Ethabi.php
Normal file
87
src/Contracts/Ethabi.php
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
<?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\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);
|
||||||
|
}
|
||||||
|
}
|
97
test/unit/EthabiTest.php
Normal file
97
test/unit/EthabiTest.php
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Test\Unit;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Test\TestCase;
|
||||||
|
use Web3\Utils;
|
||||||
|
use Web3\Contracts\Ethabi;
|
||||||
|
|
||||||
|
class EthabiTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* testJsonMethodString
|
||||||
|
* from GameToken approve function
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $testJsonMethodString = '{
|
||||||
|
"constant": false,
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"name": "_spender",
|
||||||
|
"type": "address"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "_value",
|
||||||
|
"type": "uint256"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"name": "approve",
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "success",
|
||||||
|
"type": "bool"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"payable": false,
|
||||||
|
"stateMutability": "nonpayable",
|
||||||
|
"type": "function"
|
||||||
|
}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* abi
|
||||||
|
*
|
||||||
|
* @var \Web3\Contracts\Ethabi
|
||||||
|
*/
|
||||||
|
protected $abi;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* setUp
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->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');
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user