Ethereum JSON-Api 的使用

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的命名空间(ethshhweb3)之外,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 file
require 'ethereum.php';

// create a new object
$ethereum = new Ethereum('127.0.0.1', 8545);

// do your thing
echo $ethereum->net_version();

常用操作

新建account

Ethereum的Account长度为20字节,40个16进制数字。

  • curl
cur -X POST --data {"id":${id}, "jsonrpc":"2.0","method":"personal_newAccount", "params":[${passphrase}]}
  • php

通过自己添加函数

echo $ethereum->personal_newAccount('passphrase');

查询账号

  • curl
curl addr:port -X POST --data '{"id":${id},"jsonrpc":"2.0","method":"eth_accounts", "params":[]}'

或者

curl addr:port -X POST --data '{"id":${id},"jsonrpc":"2.0","method":"personal_listAccounts", "params":[]}'
  • php
echo json_encode($ethereum->eth_accounts());

或者

echo json_encode($thereum->personal_listAccounts());

查询余额

  • curl

params:

@DATA, 20 bytes, address to check for balance

@Qutantity|TAG, int of block number, or “latestearliestpending

curl -X POST --data    '{"id":${id},"jsonrpc":"2.0","method":"eth_getBalance", "params":["0xb75b32047b7a9018964867a8bc6ef294659859c3","latest"]}' addr:port 
  • php
echo $ethereum->eth_getBalance('0xb75b32047b7a9018964867a8bc6ef294659859c3','latest',True);

发送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]}'

可用资源

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://steemit.com/ethereum/@maxnachamkin/how-to-create-your-own-ethereum-token-in-an-hour-erc20-verified

猜你喜欢

转载自blog.csdn.net/ffzhihua/article/details/80970732
今日推荐