详解php与ethereum客户端交互


Posted in PHP onApril 28, 2018

php与ethereum rpc server通信

一、Json RPC

Json RPC就是基于json的远程过程调用,这么解释比较抽象。简单来说,就是post一个json格式的数据调用rpc server中的方法. 而这个json格式是固定的, 总的来说有这么几项:

{
  "method": "",
  "params": [],
  "id": idNumber
}
  • method: 方法名
  • params: 参数列表
  • id: 对过程调用的唯一标识号

二、构建一个Json RPC客户端

<?php

class jsonRPCClient {
  
  /**
   * Debug state
   *
   * @var boolean
   */
  private $debug;
  
  /**
   * The server URL
   *
   * @var string
   */
  private $url;
  /**
   * The request id
   *
   * @var integer
   */
  private $id;
  /**
   * If true, notifications are performed instead of requests
   *
   * @var boolean
   */
  private $notification = false;
  
  /**
   * Takes the connection parameters
   *
   * @param string $url
   * @param boolean $debug
   */
  public function __construct($url,$debug = false) {
    // server URL
    $this->url = $url;
    // proxy
    empty($proxy) ? $this->proxy = '' : $this->proxy = $proxy;
    // debug state
    empty($debug) ? $this->debug = false : $this->debug = true;
    // message id
    $this->id = 1;
  }
  
  /**
   * Sets the notification state of the object. In this state, notifications are performed, instead of requests.
   *
   * @param boolean $notification
   */
  public function setRPCNotification($notification) {
    empty($notification) ?
              $this->notification = false
              :
              $this->notification = true;
  }
  
  /**
   * Performs a jsonRCP request and gets the results as an array
   *
   * @param string $method
   * @param array $params
   * @return array
   */
  public function __call($method,$params) {
    
    // check
    if (!is_scalar($method)) {
      throw new Exception('Method name has no scalar value');
    }
    
    // check
    if (is_array($params)) {
      // no keys
      $params = $params[0];
    } else {
      throw new Exception('Params must be given as array');
    }
    
    // sets notification or request task
    if ($this->notification) {
      $currentId = NULL;
    } else {
      $currentId = $this->id;
    }
    
    // prepares the request
    $request = array(
            'method' => $method,
            'params' => $params,
            'id' => $currentId
            );
    $request = json_encode($request);
    $this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";

    // performs the HTTP POST
    $opts = array ('http' => array (
              'method' => 'POST',
              'header' => 'Content-type: application/json',
              'content' => $request
              ));
    $context = stream_context_create($opts);
    if ($fp = fopen($this->url, 'r', false, $context)) {
      $response = '';
      while($row = fgets($fp)) {
        $response.= trim($row)."\n";
      }
      $this->debug && $this->debug.='***** Server response *****'."\n".$response.'***** End of server response *****'."\n";
      $response = json_decode($response,true);
    } else {
      throw new Exception('Unable to connect to '.$this->url);
    }
    
    // debug output
    if ($this->debug) {
      echo nl2br($debug);
    }
    
    // final checks and return
    if (!$this->notification) {
      // check
      if ($response['id'] != $currentId) {
        throw new Exception('Incorrect response id (request id: '.$currentId.', response id: '.$response['id'].')');
      }
      if (!is_null($response['error'])) {
        throw new Exception('Request error: '. var_export($response['error'], true));
      }
      
      return $response['result'];
      
    } else {
      return true;
    }
  }
}
?>

比较简单的代码,如果比较懒,拿过去用就行了。也可以上packagist.org自己找一个rpc client.

三、调用RPC的两类方法

有两类方法需要调用. 一类是RPC server自带方法,另一类就是合约方法.

RPC server方法调用json格式

{
  "method": "eth_accounts",
  "params": [],
  "id": 1
}

RPC Server自带方法的列表

调用自带方法比较简单,参考上述链接,大部分都有示例.

合约方法调用json格式

调用合约方法必须使用自带方法中的eth_call. 而合约方法名称和合约方法参数列表则使用params进行体现, 比如: 我们要调用合约中的balanceOf方法, 则json数据应该如何构造呢?

首先看看getBalanace的函数实现:

function balanceOf(address _owner) public view returns (uint256 balance)

提炼出函数原型:

balanceOf(address)

在geth控制台下运行命令:

web3.sha3("balanceOf(address)").substring(0, 10)

得到函数hash "0x70a08231"

假设待查询的地址 address _owner = "0x38aabef4cd283ccd5091298dedc88d27c5ec5750", 则去掉前面的"0x", 并在左边补24个零(一般地址长度为42位, 去掉'0x'后为40位),构成64位十六进制参数.

最终得到的参数为 "0x70a0823100000000000000000000000038aabef4cd283ccd5091298dedc88d27c5ec5750"

假设我们的合约地址为 "0xaeab4084194B2a425096fb583Fbcd67385210ac3".

则得到最终的json数据为:

{
  "method": "eth_call",
  "params": [{"from": "0x38aabef4cd283ccd5091298dedc88d27c5ec5750", "to": "0xaeab4084194B2a425096fb583Fbcd67385210ac3", "data": "0x70a0823100000000000000000000000038aabef4cd283ccd5091298dedc88d27c5ec5750"}, "latest"],
  "id": 1
}

把以上json数据以post方式发送给服务器,就可以调用合约方法"balanceOf", 查询给定的地址中的代币余额.

调用合约中的其他方法也要新遵循上面的方式, 我们再分析一下transfer方法, 加深印象:

首先, 看看代码中的函数实现:

function transfer(address _to, uint256 _value) public returns (bool)

其次, 提炼出函数原型:

transfer(address,uint256) //注意逗号后面不能有空格

再次, 在控制台运行sha3函数:

web3.sha3("transfer(address,uint256)").substring(0, 10)

得到函数hash "0xa9059cbb"

第一个参数假设 address _to = "0x38aabef4cd283ccd5091298dedc88d27c5ec5750", 则去"0x", 补零到64位.

第二个参数假设 uint256 _value = 43776, 则化为十六进制"0xab00"后, 去"0x", 补零到64位.

连接起来

"0xa9059cbb00000000000000000000000038aabef4cd283ccd5091298dedc88d27c5ec5750000000000000000000000000000000000000000000000000000000000000ab00"

构建json数据:

{
  "method": "eth_call",
  "params": [{"from": "0x38aabef4cd283ccd5091298dedc88d27c5ec5750", "to": "0xaeab4084194B2a425096fb583Fbcd67385210ac3", "data": "0xa9059cbb00000000000000000000000038aabef4cd283ccd5091298dedc88d27c5ec5750000000000000000000000000000000000000000000000000000000000000ab00"}, "latest"],
  "id": 1
}
  • from 转出者地址
  • to 合约地址
  • data 上述操作得到的十六进制数

把以上的步骤转化为代码.

构建一个以太坊RPC client

<?php 

require './jsonRPCClient.php';

//php自带的dechex无法把大整型转换为十六进制
function bc_dechex($decimal)
{
  $result = [];

  while ($decimal != 0) {
    $mod = $decimal % 16;
    $decimal = floor($decimal / 16);
    array_push($result, dechex($mod));    
  }

  return join(array_reverse($result));
}

class EthereumRPCClient
{
  public static $client = null;
  
  //布署合约的账户地址
  const COINBASE = '0x38aabef4cd283ccd5091298dedc88d27c5ec5750';
  
  //合约地址
  const CONTRACT = '0xaeab4084194B2a425096fb583Fbcd67385210ac3';

  public static function __callStatic($method, $params)
  {
    $params = count($params) < 1 ? [] : $params[0];

    try {
      if (is_null(self::$client)) {
        self::$client = new jsonRPCClient('http://127.0.0.1:8545', true);  
      }
    } catch (\Exception $e) {
      echo $e->getMessage();
    }

    return call_user_func([self::$client, $method], $params);

  }

  public static function getBalance($address)
  {
    $method_hash = '0x70a08231';
    $method_param1_hex = str_pad(substr($address, 2), 64, '0', STR_PAD_LEFT);
    $data = $method_hash . $method_param1_hex;

    $params = ['from' => $address, 'to' => self::CONTRACT, 'data' => $data];

    $total_balance = self::eth_call([$params, "latest"]);

    return hexdec($total_balance) / (pow(10, 18));
  }

  public static function transfer($to, $value)
  {
    self::personal_unlockAccount([self::COINBASE, "123456", 3600]);

    $value = bcpow(10, 18) * $value;

    $method_hash = '0xa9059cbb';
    $method_param1_hex =str_pad(substr($to, 2), 64, '0', STR_PAD_LEFT);  
    $method_param2_hex = str_pad(strval(bc_dechex($value)), 64, '0', STR_PAD_LEFT);

    $data = $method_hash . $method_param1_hex . $method_param2_hex;
    $params = ['from' => self::COINBASE, 'to' => self::CONTRACT, 'data' => $data];

    return self::eth_sendTransaction([$params]);

  }

}

代码比较简单, 要注意几点:

  • transfer函数的value单位很小, 是 10 ^ -18, 所以如果你想转1000个,其实是要乘于 10的18次方, 这里的18是decimals.
  • 由于第1点, 应该使用bcpow代替pow函数.
  • 不能使用php自带的dechex函数. 因为dechex要求整型不能大于 PHP_INT_MAX, 而这个数在32位机上为4294967295。由于第1 点, 所有的数都要乘于10的18次方, 所以得到的数要远远大于PHP_INT_MAX. 建议自己实现10进制转16进制,如果你不知道如何实现,参考上述代码。
  • 在运行某些合约方法, 比如transfer时, 要先unlock用户.
  • 发送交易之后, 一定要在服务器端启动挖矿, 这样交易才会真的写入到区块, 比如你调用transfer之后,却发现对方没有到账,先别吃惊,启动挖矿试试。如果想启用自动挖码, 在geth --rpc ...最后加上 --mine.

测试:

<?php 
var_dump(EthereumRPCClient::personal_newAccount(['password']));
var_dump(EthereumRPCClient::personal_unlockAccount([EthereumRPCClient::COINBASE, "password", 3600]);
var_dump(EthereumRPCClient::getBalance("0x...."));
PHP 相关文章推荐
用PHP实现登陆验证码(类似条行码状)
Oct 09 PHP
基于Discuz security.inc.php代码的深入分析
Jun 03 PHP
php实现用于验证所有类型的信用卡类
Mar 24 PHP
php编程每天必学之验证码
Mar 03 PHP
使用JavaScript创建新样式表和新样式规则
Jun 14 PHP
功能强大的php分页函数
Jul 20 PHP
php解析base64数据生成图片的方法
Dec 06 PHP
PHP获取当前日期及本周一是几月几号的方法
Mar 28 PHP
PHP查询分页的实现代码
Jun 09 PHP
PHP验证码无法显示的原因及解决办法
Aug 11 PHP
php实现的mongoDB单例模式操作类
Jan 20 PHP
PHP实现一个按钮点击上传多个图片操作示例
Jan 23 PHP
360搜索引擎自动收录php改写方案
Apr 28 #PHP
PHP使用Curl实现模拟登录及抓取数据功能示例
Apr 27 #PHP
PHP获取文件扩展名的常用方法小结【五种方式】
Apr 27 #PHP
PHP四种排序算法实现及效率分析【冒泡排序,插入排序,选择排序和快速排序】
Apr 27 #PHP
php-fpm服务启动脚本的方法
Apr 27 #PHP
php-fpm添加service服务的例子
Apr 27 #PHP
laravel 5.4 + vue + vux + element的环境搭配过程介绍
Apr 26 #PHP
You might like
体育彩票排列三组选三算法分享
2014/03/07 PHP
ThinkPHP 表单自动验证运用示例
2014/10/13 PHP
WordPress中制作导航菜单的PHP核心方法讲解
2015/12/11 PHP
iis6手工创建网站后无法运行php脚本的解决方法
2017/06/08 PHP
比较简单实用的使用正则三种版本的js去空格处理方法
2007/11/18 Javascript
从JavaScript 到 JQuery (1)学习小结
2009/02/12 Javascript
jquery validation插件表单验证的一个例子
2010/03/03 Javascript
JavaScript 打地鼠游戏代码说明
2010/10/12 Javascript
20个非常棒的 jQuery 幻灯片插件和教程分享
2011/08/23 Javascript
jquery数据验证插件(自制,简单,练手)实例代码
2013/10/24 Javascript
js中的json对象详细介绍
2014/10/29 Javascript
jQuery实现灰蓝风格标准二级下拉菜单效果代码
2015/08/31 Javascript
JS实现表单多文件上传样式美化支持选中文件后删除相关项
2016/09/30 Javascript
React中使用collections时key的重要性详解
2017/08/07 Javascript
jquery实现企业定位式导航效果
2018/01/01 jQuery
自定义Vue中的v-module双向绑定的实现
2019/04/17 Javascript
Vue开发环境中修改端口号的实现方法
2019/08/15 Javascript
node.js通过url读取文件
2020/10/16 Javascript
[53:29]完美世界DOTA2联赛循环赛 DM vs Matador BO2第二场 11.04
2020/11/05 DOTA
Python的Flask框架中实现简单的登录功能的教程
2015/04/20 Python
Python实现对象转换为xml的方法示例
2017/06/08 Python
python检测IP地址变化并触发事件
2018/12/26 Python
Python 网络编程之TCP客户端/服务端功能示例【基于socket套接字】
2019/10/12 Python
Python使用plt.boxplot() 参数绘制箱线图
2020/06/04 Python
移动端Web页面的CSS3 flex布局快速上手指南
2016/05/31 HTML / CSS
利用html5 file api读取本地文件示例(如图片、PDF等)
2018/03/07 HTML / CSS
美国最大的农村生活方式零售店:Tractor Supply Company(TSC)
2017/05/15 全球购物
美国正宗设计师眼镜在线零售商:EYEZZ
2019/03/23 全球购物
什么是servlet
2012/05/08 面试题
办公室副主任岗位职责
2013/11/25 职场文书
齐云山导游词
2015/02/06 职场文书
万能检讨书开头与结尾怎么写
2015/02/17 职场文书
医学生自荐信范文(2016精选篇)
2016/01/28 职场文书
关于战胜挫折的名言警句大全!
2019/07/05 职场文书
详解CSS中的特指度和层叠问题
2021/07/15 HTML / CSS
再谈python_tkinter弹出对话框创建
2022/03/20 Python