EOS调用Json-RPC接口

本文作者:陈进坚
博客地址: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
}

得到idref_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"
]

参考文章

https://www.jianshu.com/p/3c9cb9bc6fd6

https://eos.readthedocs.io/zh_CN/latest/API/EOSIO-RPC/

你可能感兴趣的:(区块链)