EthTool开发包适用于但愿采用裸交易的PHP以太坊应用开发,主要包含如下特性:php
采用裸交易的一个好处是开发者没必要本身部署以太坊节点 —— 同步区块是很痛苦的过程。使用EthTool构造 好裸交易以后,只须要使用第三方(etherscan/infura/...)提供的服务来广播交易便可。算法
EthTool运行在**Php 7.1+**环境下,当前版本1.0.0,主要代码文件清单以下:api
代码文件 | 说明 |
---|---|
ethtool/src/Callback.php | RPC回调类 |
ethtool/src/Credential.php | 身份标识类 |
ethtool/src/KeyStore.php | Keystore类,V3算法 |
ethtool/src/EthApi.php | 以太坊交易接口基类 |
ethtool/src/EthApiEtherscan.php | Etherscan交易接口类 |
ethtool/src/EthApiWeb3.php | Infura/Web3交易接口类 |
ethtool/src/RawContract.php | 裸交易合约类 |
ethtool/src/RawTxBuilder.php | 裸交易构造器 |
test/demo-etherscan.php | 裸交易演示代码,使用etherscan提交 |
test/demo-infura.php | 裸交易演示代码,使用infura提交 |
test/demo-keystore.php | keystore演示代码 |
vendor | 第三方依赖包目录 |
composer.json | composer配置文件 |
代码包的主要类以下:composer
裸交易构造测试
使用RawTxBuilder的create()
静态方法获取一个构造器实例,例如:ui
$rtb = RawTxBuilder::create();
裸交易构造器须要绑定一个帐户对象,使用credential()
方法:spa
$crendetial = Crendetial::fromKey('...'); $rtb->credential($credential);
RawTxBuilder目前支持三种裸交易的构造:code
getPlaintx()
方法获取。getDeployTx()
方法获取。getSendTx()
方法获取。例如,下面的代码生成合约部署裸交易:对象
$rawtx = RawTxBuilder::create() ->credential($credential) ->gasLimit('4000000') ->gasPrice('10000000000') ->chainId($chainId) ->nonce($nonce) ->contract($abi) //建立合约对象,一个RawContract实例 ->bytecode($bin) //设置合约对象的字节码 ->getDeployTx(1000000,'HAPPY COIN',0,'HAPY'); //获取部署裸交易码流
裸交易广播
EthApi基类目前定义如下方法,能够根据本身的须要扩充
EthApiEtherscan是基于Etherscan服务的EthApi实现,EthApiWeb3是基于以太坊RPC的EthApi 实现。例如,下面的代码使用Etherscan在rinkeby测试链部署前面得到的裸交易:
$eth = new EthApiEtherscan($your_api_key/*etherscan api key*/,4/*rinkeby*/); $txid = $eth->sendRawTransaction($rawtx);
KeyStore读写
KeyStore类能够读取geth/parity生成的keystore文件,其生成的 keystore文件也能够被geth/parity读取。
使用KeyStore类的静态方法save()
将私钥转化为keystore格式写入指定目录:
$credential = Credential::new(); $prv = $credential->getPrivateKey(); $fileName = KeyStore::save($prv,'123','./keystore')
使用KeyStore类的静态方法load()
载入指定的keystore文件并解密私钥:
$fileName = './keystore/....' $prv = KeyStore::load($fileName,'123');