PHP开发ETH钱包,助记词创建、助记词导入、钱包签名发起交易

用到的composer包

        "bitwasp/bitcoin-lib": "1.0.4",
        "minter/minter-php-bip-44": "^1.0",
        "web3p/ethereum-util": "^0.1.1",
        "sc0vu/web3.php": "^0.1.4",
        "digitaldonkey/ethereum-php": "dev-master",
        "web3p/ethereum-tx": "^0.3.4"

自己封装好的类拿出来共享,是否能使用大家自行判断,使用的infura,不用自己搭建ETH

<?php

namespace app\common\library;

use BIP\BIP44;
use BitWasp\BitcoinLib\BIP39\BIP39;
use Ethereum\Ethereum;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Utils;
use Web3\Web3;
use Web3p\EthereumTx\Transaction;
use Web3p\EthereumUtil\Util;

class EtherLib
{

    //protected static $eth_url = 'http://XXXXXX:8200';
    protected static $eth_url = 'https://mainnet.infura.io/v3/XXX';
    protected static $eth = null;
    protected static $personal = null;
    public static $web3 = null;
    /**
     * @var Ethereum
     */
    public static $new_web = null;

    /*
    * 单例获取eth实例
    */
    public static function getWeb3()
    {
        if (empty(self::$web3)) {
            // 30 Second timeout
            $timeout = 30;

            self::$web3 = new Web3(new HttpProvider(new HttpRequestManager(self::$eth_url, $timeout)));
            //self::$web3 = new Web3(self::$eth_url);
        }
        return self::$web3;
    }

    public static function getNewWeb3()
    {
        if (empty(self::$new_web)) {
            self::$new_web = new Ethereum(self::$eth_url);
        }
        return self::$new_web;
    }

    /*
     * 单例获取eth实例
     */
    public static function getEth()
    {
        if (empty(self::$eth)) {
            $web3 = self::getWeb3();
            self::$eth = $web3->getEth();
        }
        return self::$eth;
    }

    /*
     * 单例获取eth实例
     */
    public static function getPersonal()
    {
        if (empty(self::$personal)) {
            $web3 = self::getWeb3();
            self::$personal = $web3->getPersonal();
        }
        return self::$personal;
    }

    /*
     * 获取ETH价格
     */
    public static function getEthPrice()
    {
        $price = cache('eth_price');
        if (empty($price)) {
            $info = file_get_contents('http://api.coindog.com/api/v1/currency/ranks');
            if (!empty($info)) {
                $info_json = json_decode($info, true);
                foreach ($info_json as $item) {
                    if (strtoupper($item['currency']) == 'ETH') {
                        $price = $item['price'
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值