运行geth
geth --datadir /mnt/eth --rpc
# --rpc 默认在http://localhost:8545开启rpc
curl 请求
curl http://localhost:8545 -H 'content-type:application/json;' -X POST \
--data '{"method":"net_version","params":[],"id":1}'
curl http://localhost:8545 -H 'content-type:application/json;' -X POST \
--data '{"method":"eth_blockNumber","params":[],"id":1}'
# {"jsonrpc":"2.0","id":1,"result":"0x509e24"}
curl http://localhost:8545 -H 'content-type:application/json;' -X POST \
--data '{"method":"eth_syncing","params":[],"id":1}'
curl http://localhost:8545 -H 'content-type:application/json;' -X POST \
--data '{"method":"eth_getBlockByNumber","params":["0x1", true],"id":1}'
# true 返回transactions的详细信息
# false 只返回transactions的hash
curl http://localhost:8545 -H 'content-type:application/json;' -X POST \
--data '{"method":"eth_getBalance","params":["0xe89943ec20d856f064a87349a00ef6ab00aed042", "latest"],"id":1}'
#{"jsonrpc":"2.0","id":1,"result":"0x740414b151bf7e0ba2"}
curl http://127.0.0.1:8545 -H 'content-type:application/json;' -X POST \
--data '{"method":"eth_getTransactionByHash","params":["0x4ee29e4d3c07a461d7378fa29c74884b5d1e297d9f45324eb7be51ec7780bb3e"],"id":1}'
curl http://127.0.0.1:8545 -H 'content-type:application/json;' -X POST \
--data '{"method":"eth_getTransactionByBlockNumberAndIndex","params":["0x18d522", "0x5"],"id":1}'
python script
import json
import requests
url = "http://localhost:8545"
headers = {'content-type': 'application/json'}
payload = json.dumps({"method": 'eth_blockNumber', "params": [], "id": '1' })
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
personal 模块
开启 personal模块api
geth --datadir /mnt/vol/eth --rpc --rpcapi "personal"
import json
import requests
url = "http://localhost:8545"
headers = {'content-type': 'application/json'}
payload = json.dumps({"method": 'personal_newAccount', "params": ["zqw"], "id": '1' })
# 产生新地址
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
Golang
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/big"
"net/http"
"strings"
)
type rpcData struct {
Jsonrpc string `json:"jsonrpc"`
ID int `json:"id"`
Result string `json:"result"`
}
func main() {
url := "http://localhost:8545/"
payload := strings.NewReader("{\"method\":\"eth_getBalance\",\"params\":[\"0x6cafe7473925998db07a497ac3fd10405637a46d\", \"latest\"],\"id\":0}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("content-type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
var obj rpcData
json.Unmarshal([]byte(body), &obj)
balance := obj.Result // 0x29d669390feebf072 超过20位 是bigInt类型
n := new(big.Int)
n, err := n.SetString(balance[2:], 16)
if !err {
fmt.Println("SetString: error")
return
}
fmt.Printf("balance 十六进制: %s, 十进制: %d \n", balance, n)
}
参考:
https://infura.io/
https://blog.linux-mac.com/2018/06/01/33.html
https://github.com/ethereum/wiki/wiki/JSON-RPC
https://tokenmarket.net/blog/protecting-ethereum-json-rpc-api-with-password/