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 1Url 2
C++http://localhost:8545
Gohttp://localhost:8545
Pyhttp://localhost:4000
Parityhttp://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命名空间

NamespaceUsage
adminGeth node management
debugGeth node debugging
minerMiner and DAG management
personalAccount management
txpoolTransaction 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值