本文作者:陳進堅
博客地址:https://jian1098.github.io
CSDN博客:https://blog.csdn.net/c_jian
聯繫方式:[email protected]
接口說明
本文基於EOS私有鏈環境,理論上測試環境也可以,比如jungle測試鏈,但是測試鏈提供的節點服務器一般無法使用wallet相關的接口,比如創建錢包、交易簽名等,因爲沒有開啓–filter-on。私有鏈的搭建步驟可以參見我的另一篇文章《Ubuntu下EOS測試環境(私鏈)搭建》。
本文的所有參數都是json格式,以表單字段格式post是不行的。
舊版本的eos的chain和wallet功能都是有nodeos程序執行的,新版本更新後與wallet相關的RPC接口改爲keosd程序控制,所以要使用與wallet相關的RPC接口必須先啓動keosd程序,並且兩個程序的端口不能相同。本文的nodeos程序端口爲默認的8888,keosd程序端口設置爲8889。
nodeos程序是可以不運行在本地的,運行在本地的是私有鏈,也可以本地不運行,直接連接主網或者測試網絡的ip和端口。但是keosd是要運行在本地的,主網和測試網絡上沒有提供對錢包操作相關的接口,只能在本地操作錢包。所以如果要創建一個EOS節點,要操作錢包的話,不管是私鏈,主網還是測試網,都要一臺服務器編譯源碼,然後啓動keosd來管理錢包賬號。
啓動nodeos
jian@ubuntu:~$ cd ~/eos/build/programs/nodeos
jian@ubuntu:~$ sudo ./nodeos -e -p eosio -d /data/eos --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --plugin eosio::bnet_plugin --contracts-console --filter-on "*" --access-control-allow-origin "*" --max-transaction-time=1000 --verbose-http-errors
啓動成功後大約每0.5秒就產生一個區塊
啓動keosd
打開另一個終端執行,ip地址192.168.1.131:8889改爲你的本地ip
jian@ubuntu:~$ cd ~/eos/build/programs/keosd
jian@ubuntu:~/eos/build/programs/keosd$ ./keosd --http-server-address 192.168.1.115:8889 --access-control-allow-origin "*" --plugin eosio::wallet_api_plugin --verbose-http-errors --plugin eosio::wallet_plugin
這裏會出現一個錯誤警告,但是不影響後面的操作
error 2018-12-08T02:48:31.877 thread-0 wallet_api_plugin.cpp:123 plugin_initialize ]
********!!!SECURITY ERROR!!!********
* *
* -- Wallet API -- *
* - EXPOSED to the LOCAL NETWORK - *
* - HTTP RPC is NOT encrypted - *
* - Password and/or Private Keys - *
* - are at HIGH risk of exposure - *
* *
************************************
訪問RPC接口
在瀏覽器打開:http://127.0.0.1:8888/v1/chain/get_info ,推薦使用postman等http調試工具進行調試,雲服務器請在安全組策略開放8888和8889兩個端口。
如果需要開放給外部瀏覽器訪問RPC端口,需要修改配置文件:
jian@ubuntu:~/eos$ cd ~/.local/share/eosio/nodeos/config
jian@ubuntu:~/.local/share/eosio/nodeos/config$ sudo vi config.ini
將 http-server-address = 127.0.0.1:8888改爲你的公網ip和端口即可,或者在啓動命令添加參數 --http-server-address = 192.168.1.123:8888,然後在瀏覽器打開:http://192.168.1.123:8888/v1/chain/get_info 。修改後在命令行對使用cleos程序都必須帶上 --url http://192.168.1.131:8888,例如:
sudo ./cleos --url http://192.168.1.131:8888 get account ubuntueos111
chain相關接口
獲取節點信息
接口鏈接:http://192.168.1.131:8888/v1/chain/get_info
psot參數:無
返回數據:
{
"server_version": "549c96cd",
"chain_id": "e70aaab8997e1dfce58fbfac80cbbb8fecec7b99cf982a9444273cbc64c41473",
"head_block_num": 2334727,
"last_irreversible_block_num": 2334397,
"last_irreversible_block_id": "00239ebdb4ef886adebd4eb456326acb833325ac05f0c2b94aca523272ac5ca0",
"head_block_id": "0023a0070c2267d1bff305c62d0285d0ccf6ab021b3a624f1daab70866dae6e5",
"head_block_time": "2018-12-07T09:09:50.500",
"head_block_producer": "batinthedark",
"virtual_block_cpu_limit": 200000000,
"virtual_block_net_limit": 1048576000,
"block_cpu_limit": 199920,
"block_net_limit": 1048576,
"server_version_string": "v1.5.0-rc2"
}
獲取區塊信息
接口鏈接:http://192.168.1.131:8888/v1/chain/get_block
psot參數:{“block_num_or_id”:“13598552”}
返回數據:
{
"code": 500,
"message": "Internal Service Error",
"error": {
"code": 3010008,
"name": "block_id_type_exception",
"what": "Invalid block ID",
"details": [
{
"message": "Invalid Block number or ID, must be greater than 0 and less than 64 characters",
"file": "chain_plugin.cpp",
"line_number": 1484,
"method": "get_block"
}
]
}
}
獲取賬戶信息
接口鏈接:http://192.168.1.131:8888/v1/chain/get_account
psot參數:{“account_name”:“account1”}
返回數據:
{
"account_name": "exchange1234",
"head_block_num": 2337585,
"head_block_time": "2018-12-07T09:33:49.500",
"privileged": false,
"last_code_update": "1970-01-01T00:00:00.000",
"created": "2018-11-29T06:01:52.000",
"core_liquid_balance": "200.0000 EOS",
"ram_quota": 5467,
"net_weight": 10000,
"cpu_weight": 10000,
"net_limit": {
"used": 0,
"available": 273176,
"max": 273176
},
"cpu_limit": {
"used": 0,
"available": 51893,
"max": 51893
},
"ram_usage": 3446,
"permissions": [
{
"perm_name": "active",
"parent": "owner",
"required_auth": {
"threshold": 1,
"keys": [
{
"key": "EOS6PDC4Cn87RPPKgyrT3faf3pMrAuppLHqpJaxHRuZgkCTqzUduS",
"weight": 1
}
],
"accounts": [],
"waits": []
}
},
{
"perm_name": "owner",
"parent": "",
"required_auth": {
"threshold": 1,
"keys": [
{
"key": "EOS6PDC4Cn87RPPKgyrT3faf3pMrAuppLHqpJaxHRuZgkCTqzUduS",
"weight": 1
}
],
"accounts": [],
"waits": []
}
}
],
"total_resources": {
"owner": "exchange1234",
"net_weight": "1.0000 EOS",
"cpu_weight": "1.0000 EOS",
"ram_bytes": 4067
},
"self_delegated_bandwidth": {
"from": "exchange1234",
"to": "exchange1234",
"net_weight": "1.0000 EOS",
"cpu_weight": "1.0000 EOS"
},
"refund_request": null,
"voter_info": {
"owner": "exchange1234",
"proxy": "",
"producers": [],
"staked": 20000,
"last_vote_weight": "0.00000000000000000",
"proxied_vote_weight": "0.00000000000000000",
"is_proxy": 0,
"reserved1": 0,
"reserved2": 0,
"reserved3": "0 "
}
}
獲取指定幣種信息
接口鏈接:http://192.168.1.131:8888/v1/chain/get_currency_stats
psot參數:{“code”:“eosio.token”, “symbol”:“EOS”}
返回數據:
{
"EOS": {
"supply": "8014684620.4023 EOS",
"max_supply": "100000000000.0000 EOS",
"issuer": "eosio"
}
}
json 轉 bin
接口鏈接:http://192.168.1.131:8888/v1/chain/abi_json_to_bin
psot參數:
{
"code": "eosio.token",
"action": "transfer",
"args": {
"from": "eosio",
"to": "exchange1111",
"quantity": "100.0000 EOS",
"memo": "send"
}
}
返回數據:
{
"binargs": "0086088a4dd350571042088a4dd3505740420f000000000004454f53000000000473656e64"
}
查詢餘額
接口鏈接:http://192.168.1.131:8888/v1/chain/get_currency_balance
psot參數:
{“code”:“eosio.token”, “account”: “account1”, “symbol”:“EOS”}
參數說明:account爲賬號名,symbol爲指定代幣名
返回數據:
[
"200.0000 EOS"
]
wallet相關接口
- 注意與wallet相關的接口需要啓動keosd程序,並且端口爲8889
創建錢包
接口鏈接:http://192.168.1.131:8889/v1/wallet/create
psot參數:“account1”
參數說明:傳入需要創建的賬號名即可,返回結果是錢包的私鑰
返回數據:
"PW5HrziwLJZYntiyNQnS7F9FSFFJAGjeZSDT2U5tbR5WVxWVgapRv"
錢包列表
接口鏈接:http://192.168.1.131:8889/v1/wallet/list_wallets
psot參數:無
返回數據:
[
"account1",
"account2",
"account3 *"
]
鎖定指定錢包
接口鏈接:http://192.168.1.131:8889/v1/wallet/lock
psot參數:“accountname”
參數說明:傳入需要解鎖的賬號名即可
返回數據:
{}
鎖定所有錢包
接口鏈接:http://192.168.1.131:8889/v1/wallet/lock_all
psot參數:無
返回數據:
{}
解鎖錢包
接口鏈接:http://192.168.1.131:8889/v1/wallet/unlock
psot參數:[“account3”, “PW5Kxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”]
參數說明:傳入需要解鎖的賬號名和私鑰
返回數據:
{}
設置自動鎖定時間
接口鏈接:http://192.168.1.131:8889/v1/wallet/set_timeout
psot參數:3600
參數說明:單位是秒
返回數據:
{}
獲取所有錢包中的公鑰
接口鏈接:http://192.168.1.131:8889/v1/wallet/get_public_keys
psot參數:無
返回數據:
[
"EOS5Fwtc4vnQE8AigimXvHrERrK7XFpycQ8Dijq2611timbVTReDt"
]
導入私鑰
接口鏈接:http://192.168.1.131:8889/v1/wallet/import_key
psot參數:[“account3”,“5JxhYZZnFJJMRrf7KkSHNLFq2LM4eZigJfeKXXYSa1TvPMdWtLc”]
參數說明:賬號名和密鑰對的私鑰
返回數據:
{}
獲取指定錢包的公私鑰對
接口鏈接:http://192.168.1.131:8889/v1/wallet/list_keys
psot參數:[“account3”,“PW5HrziwLJZYntiyNQnS7F9FSFFJAGjeZSDT2U5tbR5WVxWVgapRv”]
參數說明:賬號名和私鑰,返回私鑰對的public key和private key
返回數據:
[
[
"EOS5Fwtc4vnQE8AigimXvHrERrK7XFpycQ8Dijq2611timbVTReDt",
"5JxhYZZnFJJMRrf7KkSHNLFq2LM4eZigJfeKXXYSa1TvPMdWtLc"
]
]
history相關接口
要使用history相關的接口,節點啓動必須--filter-on "*"
參數,一般網上公開的節點是不開啓的,所以查到的數據爲空
獲取指定賬號的交易記錄
接口鏈接:http://192.168.1.115:8888/v1/history/get_actions
psot參數:{“pos”:0,“offset”:5,“account_name”:“exchange2222”}
參數說明:偏移量、查找數量和要查詢的賬號名,前兩個參數好像不起作用。
如果是在jungle測試網絡,可以逐個節點測試,一般都會有幾個節點打開--filter-on
的,例如 http://jungle2-eos.blckchnd.com:8888 這個節點
返回數據:
{
"actions": [
{
"global_action_seq": 19298,
"account_action_seq": 0,
"block_num": 19240,
"block_time": "2018-07-22T02:55:42.000",
"action_trace": {
"receipt": {
"receiver": "exchange2222",
"act_digest": "f8ce7062290be813e6484e50c7c0e3c22a9898e3240bab76f7e11b99c07abe52",
"global_sequence": 19298,
"recv_sequence": 5,
"auth_sequence": [
[
"eosio",
]
],
"code_sequence": 1,
"abi_sequence": 1
},
"act": {
"account": "eosio.token",
"name": "transfer",
"authorization": [
{
"actor": "eosio",
"permission": "active"
}
],
"data": {
"from": "eosio",
"to": "exchange2222",
"quantity": "10.0000 SYS",
"memo": ""
},
"hex_data": "0000000000ea305500dca79ab1e98e49a086010000000000045359530000000000"
},
"elapsed": 6,
"cpu_usage": 0,
"console": "",
"total_cpu_usage": 0,
"trx_id": "b09395d938e594b838277453cea832ddc5d57ab5e92d8763ea011df9e35d7b34",
"inline_traces": []
}
},
{
"global_action_seq": 21095,
"account_action_seq": 1,
"block_num": 21034,
"block_time": "2018-07-22T03:10:39.000",
"action_trace": {
"receipt": {
"receiver": "exchange2222",
"act_digest": "72ae6a2af84b85d4b4e20190a56904f53afa2a79ab4c47ca2f15874c4c309205",
"global_sequence": 21095,
"recv_sequence": 6,
"auth_sequence": [
[
"eosio",
]
],
"code_sequence": 1,
"abi_sequence": 1
},
"act": {
"account": "eosio.token",
"name": "transfer",
"authorization": [
{
"actor": "eosio",
"permission": "active"
}
],
"data": {
"from": "eosio",
"to": "exchange2222",
"quantity": "2.0000 SYS",
"memo": ""
},
"hex_data": "0000000000ea305500dca79ab1e98e49204e000000000000045359530000000000"
},
"elapsed": 6,
"cpu_usage": 0,
"console": "",
"total_cpu_usage": 0,
"trx_id": "e1cafa2561470a0b489a4e2032375a5c88665f75e5172bac7b574cd051079d42",
"inline_traces": []
}
}
],
"last_irreversible_block": 64270
}
根據交易id查詢交易信息
接口鏈接:http://192.168.1.115:8888/v1/history/get_transaction
psot參數:{“id”:“50c9bedc542a5d17a66bace81486d2fc2e3b047ed67d34c2ad0c1c6d75040948”}
返回數據:
{
"id": "50c9bedc542a5d17a66bace81486d2fc2e3b047ed67d34c2ad0c1c6d75040948",
"trx": {
"receipt": {
"status": "executed",
"cpu_usage_us": 4933,
"net_usage_words": 17,
"trx": [
1,
{
"signatures": [
"SIG_K1_KiSVEWDyt5DcxB4wyKGBg1DQGMbxNqPBr7ac3ia7qgU3wopTmnvePWisi8ZvaxVerciCVrZaRaQxcYmjqzvXCZVbx4f66T"
],
"compression": "none",
"packed_context_free_data": "",
"packed_trx": "08600b5c7c634a617031000000000100a6823403ea3055000000572d3ccdcd010000000000ea305500000000a8ed3232250000000000ea3055000000214f4d113240420f000000000004454f5300000000046d656d6f00"
}
]
},
"trx": {
"expiration": "2018-12-08T06:09:12",
"ref_block_num": 25468,
"ref_block_prefix": 829448522,
"max_net_usage_words": 0,
"max_cpu_usage_ms": 0,
"delay_sec": 0,
"context_free_actions": [],
"actions": [
{
"account": "eosio.token",
"name": "transfer",
"authorization": [
{
"actor": "eosio",
"permission": "active"
}
],
"data": {
"from": "eosio",
"to": "account1",
"quantity": "100.0000 EOS",
"memo": "memo"
},
"hex_data": "0000000000ea3055000000214f4d113240420f000000000004454f5300000000046d656d6f"
}
],
"transaction_extensions": [],
"signatures": [
"SIG_K1_KiSVEWDyt5DcxB4wyKGBg1DQGMbxNqPBr7ac3ia7qgU3wopTmnvePWisi8ZvaxVerciCVrZaRaQxcYmjqzvXCZVbx4f66T"
],
"context_free_data": []
}
},
.............
}
]
}
查詢相同公鑰的賬號
接口鏈接:http://192.168.1.131:8888/v1/history/get_key_accounts
psot參數:{“public_key”:“EOS5Fwtc4vnQE8AigimXvHrERrK7XFpycQ8Dijq2611timbVTReDt”}
參數說明:公鑰
返回數據:
{
"account_names": [
"account1"
]
}
交易相關接口
通過RPC發送交易需要下面幾個步驟
1.序列化交易信息
接口鏈接:http://192.168.1.131:8888/v1/chain/abi_json_to_bin
psot參數:
{
"code": "eosio.token",
"action": "transfer",
"args": {
"from": "account1",
"to": "account2",
"quantity": "2.0000 EOS",
"memo": "send to account2"
}
}
參數說明:設置好from,to賬號和數量,memo是備註
返回數據:
{
"binargs": "000000214f4d1132000000224f4d1132204e00000000000004454f53000000001073656e6420746f206163636f756e7432"
}
2.獲取最新區塊
接口鏈接:http://192.168.1.131:8888/v1/chain/get_info
psot參數:無
返回數據:
{
"server_version": "ea08cfd3",
"chain_id": "cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f",
"head_block_num": 43593,
"last_irreversible_block_num": 43592,
"last_irreversible_block_id": "0000aa48c52fe5f17caf8adaca191a48c567bdb746feac4a92f65d3e7ced13ae",
"head_block_id": "0000aa49cc362b1a6eba086a329b14e1c1933c0629a3e72b06b16b7862b8c18a",
"head_block_time": "2018-12-08T08:42:33.500",
"head_block_producer": "eosio",
"virtual_block_cpu_limit": 200000000,
"virtual_block_net_limit": 1048576000,
"block_cpu_limit": 199900,
"block_net_limit": 1048576,
"server_version_string": "v1.5.0-dirty"
}
得到最新區塊爲43593
3. 獲取區塊id
接口鏈接:http://192.168.1.131:8888/v1/chain/get_block
psot參數:{“block_num_or_id”:“43593”}
返回數據:
{
"timestamp": "2018-12-08T08:42:33.500",
"producer": "eosio",
"confirmed": 0,
"previous": "0000aa48c52fe5f17caf8adaca191a48c567bdb746feac4a92f65d3e7ced13ae",
"transaction_mroot": "0000000000000000000000000000000000000000000000000000000000000000",
"action_mroot": "7d8b4c41547eedad2be4418c53160051976acd272bce9d8688064bd846319772",
"schedule_version": 0,
"new_producers": null,
"header_extensions": [],
"producer_signature": "SIG_K1_KctYCoHaaRHnVhyWj3AohoLwHzq8GbLShtT3H5yJ7uMG7GQujzfBmZ9JgNgv9JAxP6C6sgWXp89Fkvkdmbsx7zRLMJbfGF",
"transactions": [],
"block_extensions": [],
"id": "0000aa49cc362b1a6eba086a329b14e1c1933c0629a3e72b06b16b7862b8c18a",
"block_num": 43593,
"ref_block_prefix": 1778956910
}
得到id
和ref_block_prefix
4.獲取from賬號信息
接口鏈接:http://192.168.1.131:8888/v1/chain/get_account
psot參數:{“account_name”:“account1”}
返回數據:
{
"account_name": "account1",
"head_block_num": 44703,
"head_block_time": "2018-12-08T08:51:48.500",
"privileged": false,
"last_code_update": "1970-01-01T00:00:00.000",
"created": "2018-12-08T06:01:14.000",
"ram_quota": -1,
"net_weight": -1,
"cpu_weight": -1,
"net_limit": {
"used": -1,
"available": -1,
"max": -1
},
"cpu_limit": {
"used": -1,
"available": -1,
"max": -1
},
"ram_usage": 2724,
"permissions": [
{
"perm_name": "active",
"parent": "owner",
"required_auth": {
"threshold": 1,
"keys": [
{
"key": "EOS75hPAeMGgAw9FYLTAHqjHyicjW82bB1q9grXqDtCdMDMpe1eoa",
"weight": 1
}
],
"accounts": [],
"waits": []
}
},
{
"perm_name": "owner",
"parent": "",
"required_auth": {
"threshold": 1,
"keys": [
{
"key": "EOS5Fwtc4vnQE8AigimXvHrERrK7XFpycQ8Dijq2611timbVTReDt",
"weight": 1
}
],
"accounts": [],
"waits": []
}
}
],
"total_resources": null,
"self_delegated_bandwidth": null,
"refund_request": null,
"voter_info": null
}
可以得到active權限的公鑰是EOS75hPAeMGgAw9FYLTAHqjHyicjW82bB1q9grXqDtCdMDMpe1eoa
5.解鎖from錢包
接口鏈接:http://192.168.1.131:8889/v1/wallet/unlock
psot參數:[“account3”, “PW5Kxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”]
參數說明:傳入需要解鎖的賬號名和私鑰
返回數據:
{}
6.交易簽名
[{
"ref_block_num": 43593,
"ref_block_prefix": 1778956910,
"expiration": "2018-12-8T06:30:10.000",
"actions": [{
"account": "eosio.token",
"name": "transfer",
"authorization": [{
"actor": "account1",
"permission": "active"
}],
"data": "000000214f4d1132000000224f4d1132204e00000000000004454f53000000001073656e6420746f206163636f756e7432"
}],
"signatures": []
},
["EOS75hPAeMGgAw9FYLTAHqjHyicjW82bB1q9grXqDtCdMDMpe1eoa"], "0000aa49cc362b1a6eba086a329b14e1c1933c0629a3e72b06b16b7862b8c18a"
]