chaingogogo / web3.php
Ethereum web3 interface. Fork of web3p/web3.php with XLoad encrypted ethereum-tx.
Requires
- php: >=7.4
- ext-mbstring: *
- guzzlehttp/guzzle: ^6.3|^7.0
- kornrunner/keccak: ~1.0
- phpseclib/phpseclib: ~2.0.30
- ratchet/pawl: ^0.4.1
- react/async: ^4.0.0|^3.1.0
- react/event-loop: ^1.2
- react/http: ^1.6.0
- react/promise: ^2.9.0
- react/promise-timer: ^1.10
- react/socket: ^1.13
- simplito/elliptic-php: ~1.0.6
Requires (Dev)
- phpunit/phpunit: ~8.0|~9.0
Suggests
None
Provides
None
Conflicts
None
Replaces
- web3p/ethereum-tx: v1.1
- web3p/ethereum-util: v1.1
- web3p/rlp: v1.1
- web3p/web3.php: v1.1
README
本仓库是 web3p/web3.php 生态的继承(fork)仓库,打包了 web3p 系列的四个包,供内部 / 商业项目使用。
与上游 web3p/web3.php 的主要区别
-
ethereum-tx 包源码经 XLoad 扩展加密
ethereum-tx/src下的所有类(Transaction、EIP1559Transaction、EIP2930Transaction、TypeTransaction)由 PHP 扩展加密工具 XLoad(Build 3.03.29)加密,运行时必须在 php.ini 中加载XLOAD扩展,否则直接exit('XLoader Not Found!')。其余包(web3.php、ethereum-util、rlp)保持明文源码。 -
私钥需加密,签名前必须先校验 传入的私钥必须是加密后的私钥(XLoad 加密格式)。在调用
sign()之前,必须先用Transaction::decodePrivateKey()提前验证私钥与地址是否匹配,校验失败立即中断,严禁带着无效私钥继续签名流程:use Web3p\EthereumTx\Transaction; // 签名前必须提前验证:加密私钥与地址不匹配则直接抛异常 if (!Transaction::decodePrivateKey($privateKey, $privateAddress)) { throw new \Exception('0x'); } $signedTx = $transaction->sign($privateKey);
环境要求
- PHP >= 7.4
- XLOAD PHP 扩展(必须,用于解密运行 ethereum-tx 加密源码)
- ext-mbstring
- Composer 依赖:guzzlehttp/guzzle、kornrunner/keccak、phpseclib/phpseclib、simplito/elliptic-php、react 系列等(见各子包 composer.json)
未加载 XLOAD 扩展时,任何包含 Web3p\EthereumTx 类的请求都会终止并输出 XLoader Not Found!。
仓库结构
web3.php/ 主包:Ethereum web3 接口(Eth / Net / Personal / Contract / Utils / Shh ...)
ethereum-tx/ 交易构造与签名(XLoad 加密,含 decodePrivateKey 校验)
ethereum-util/ secp256k1 / keccak 等工具函数
rlp/ RLP 编解码
各子包均为独立的 composer 包(PSR-4:Web3\、Web3p\EthereumTx\、Web3p\Util\、Web3p\RLP\),在对应目录下 composer install 即可单独使用。
安装
前提:目标环境已按 XLoad 官方(phpXLoad.com)指引安装并启用
XLOAD扩展。
方式一:Composer 安装(推荐)
本包聚合了全部四个子包的命名空间(Web3\、Web3p\EthereumTx\、Web3p\EthereumUtil\、Web3p\RLP\),一次安装即可使用全部功能。
未发布 Packagist 时,先在项目 composer.json 中配置 VCS 仓库:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/chaingogogo/web3.php"
}
]
}
然后执行:
composer require chaingogogo/web3.php
已发布 Packagist 后(通过 packagist.org 提交仓库地址即可),直接执行:
composer require chaingogogo/web3.php
方式二:源码安装
git clone git@github.com:chaingogogo/web3.php.git
cd web3.php
composer install
基本用法
连接节点
use Web3\Web3; $web3 = new Web3('http://localhost:8545');
离线签名并发送交易(本仓库关键差异点)
use Web3\Web3; use Web3\Utils; use Web3p\EthereumTx\Transaction; $web3 = new Web3('http://localhost:8545'); $eth = $web3->eth; $from = '0x...'; // 发起地址 $to = '0x...'; // 目标地址 $privateKey = '...'; // XLoad 加密后的私钥 // 1. 提前校验加密私钥与地址是否匹配(必做,失败即终止) if (!Transaction::decodePrivateKey($privateKey, $from)) { throw new \Exception('0x'); } // 2. 查询 nonce / gasPrice $eth->getTransactionCount($from, function ($err, $nonce) use ($eth, $from, $to, $privateKey) { if ($err !== null) { throw $err; } $eth->gasPrice(function ($err, $gasPrice) use ($nonce, $eth, $from, $to, $privateKey) { if ($err !== null) { throw $err; } // 3. 构造交易并签名 $transaction = new Transaction([ 'nonce' => '0x' . dechex($nonce->toString()), 'from' => $from, 'to' => $to, 'gas' => '0x5208', 'gasPrice' => '0x' . dechex($gasPrice->toString()), 'value' => '0xde0b6b3a7640000', // 1 ETH 'chainId' => 1, ]); $signedTx = $transaction->sign($privateKey); // 4. 广播 $eth->sendRawTransaction('0x' . $signedTx, function ($err, $txHash) { if ($err !== null) { throw $err; } echo 'Tx hash: ' . $txHash . PHP_EOL; }); }); });
EIP-1559 交易同理,使用 EIP1559Transaction,签名前同样必须先通过 Transaction::decodePrivateKey() 校验。
注意事项
- 不要把明文私钥传给
sign():本仓库约定私钥一律以 XLoad 加密形式传递,签名前的decodePrivateKey校验是强制步骤。 - 加密源码受法律保护(见文件头声明),严禁反编译、逆向等行为。
- 升级上游 web3p 各包时,需重新对
ethereum-tx源码执行 XLoad 加密后再提交。
致谢
基于 web3p/web3.php(sc0Vu / Peter Lai,MIT License)及其生态包 ethereum-tx、ethereum-util、rlp 修改而成,遵循 MIT 许可证。