Ethereum的API接口类型
Ethereum官方提供了Go、python、C++和Parity四种语言的版本。四种语言都提供了JSON-RPC API,供使用者调用,可以通过geth RPC终端开启。
在开启geth的时候可以增加 --${interface}api新选项来选择开启哪一个api。${interface}的类型可以为 rpc开启HTTP, ws开启Web Scocket, ipc开启Unix socket(Unix)或者named pipe(Windows)。
例如,geth --ipcapi admin, eth, miner --rpcapi eth,web3 --rpc
表示:开启了admin,eth,miner功能的ipcAPI,开启eth,web3功能的HTTP API。
其中HTTP RPC功能需要加上--rpc来激活。
Ethereum JSON RPC的使用
开启JSON RPC
默认的JSON-RPC端口
| Cliet 1 | Url 2 |
|---|---|
| C++ | http://localhost:8545 |
| Go | http://localhost:8545 |
| Py | http://localhost:4000 |
| Parity | http://localhost:8545 |
以go-ethereum为例,开启JSON-RPC服务
开启默认接口:
geth --rpc
自定义监听端口和地址
geth --rpc --rpcaddr <ip> --rpcport <portnumber>
如果需要从浏览器访问RPC,则需要用适当的域名配置CORS。[这个好像是说跨域的。。。。]
geth --rpc --rpccorsdomain "http://localhost:3000"
同时,在geth consle里面也可以通过输入命令来开启RPC服务 >startRPC(addr,port)
调用Ethereum JSON RPC
接口类型
除了DApp Api的命名空间(eth, shh, web3)之外,geth还提供了如下API命名空间
| Namespace | Usage |
|---|---|
admin | Geth node management |
debug | Geth node debugging |
miner | Miner and DAG management |
personal | Account management |
txpool | Transaction pool inspection |
调用格式
cur addr:port -X POST --data '{"jsonrpc":"2.0","id":id, "method":"${method}","params":"${params}"}'
ethereum-php
下载lib
git clone https://github.com/btelle/ethereum-php.git
deamon
// include the class filerequire 'ethereum.php';// create a new object$ethereum = new Ethereum('127.0.0.1', 8545);// do your thingecho $ethereum->net_version();12345678
常用操作
新建account
Ethereum的Account长度为20字节,40个16进制数字。
curl
cur -X POST --data {"id":${id}, "jsonrpc":"2.0","method":"personal_newAccount", "params":[${passphrase}]}1
php
通过自己添加函数
echo $ethereum->personal_newAccount('passphrase');1
查询账号
curl
curl addr:port -X POST --data '{"id":${id},"jsonrpc":"2.0","method":"eth_accounts", "params":[]}'1
或者
curl addr:port -X POST --data '{"id":${id},"jsonrpc":"2.0","method":"personal_listAccounts", "params":[]}'1
php
echo json_encode($ethereum->eth_accounts());1
或者
echo json_encode($thereum->personal_listAccounts());1
查询余额
curl
params:
@DATA, 20 bytes, address to check for balance
@Qutantity|TAG, int of block number, or “latest, earliest, pending”
curl -X POST --data '{"id":${id},"jsonrpc":"2.0","method":"eth_getBalance", "params":["0xb75b32047b7a9018964867a8bc6ef294659859c3","latest"]}' addr:port 1
php
echo $ethereum->eth_getBalance('0xb75b32047b7a9018964867a8bc6ef294659859c3','latest',True);1
发送ETH
- [x] tx: Transaction
- [x] from: 发出的Account
- [x] to: 接收的Account
- [x] amount: 数量
- [x] passphrase: tx.from的passphrase
curl
curl -X POST --data '{"id":${id}, "jsonrpc":"2.0", "method": "personal_sendTransaction", "params": [tx, string]}'1
可用资源 第二个是官网
https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_sendtransaction
https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_protocolversion
https://ethereum.org/token#the-code
转载于:https://blog.51cto.com/iceman123/2089540
5164

被折叠的 条评论
为什么被折叠?



