• EOS区块链钱包开发教程


    在币圈流行着这样一句话:钱包是区块链的入口,犹如浏览器是互联网的入口。区块链钱包是什么呢?一些人会脱口而出类似于微信支付宝钱包呗,其实区块链钱包并不存储加密货币,加密货币其实都放在链上。钱包只是个公私钥的管理工具而已,同时它也具有货币可进行交易的特性,也就是收款和付款。区块链钱包的应用价值,不仅体现在资产管理上,更体现于支付、交易以及DApp市场上。

    号称能实现百万TPS的EOS拥有着庞大的用户群体,自从2018年6月10日众筹结束主网上线以来,截止笔者发稿时,它的注册帐户已经超过188万,再加上那些没有EOS账户却在加密货币交易平台上持有EOS代币的用户数,可以想象下这个总用户数。

     

    区块链钱包担当加密货币资产的存储和DApp的超级流量入口,它的市场需求也随之增加,怎么进行EOS区块链钱包开发呢?

    1、创建钱包

    描述:创建新钱包

    请求示例:

    $ curl http://localhost:8888/v1/wallet/create -X POST -d '"default"'

    参数:

    返回数据:

    PW5KFWYKqvt63d4iNvedfDEPVZL227D3RQ1zpVFzuUwhMAJmRAYyX

     

    数据说明:

    新钱包的密码,用于解锁该钱包

     

     

    2、打开钱包

    描述:钱包的默认状态为关闭,只有打开钱包的状态下才能进行后面的操作

    请求示例:

    $ curl http://localhost:8888/v1/wallet/open-X POST -d '"default"'

    参数:

    返回数据得到:

    {}

     

    3、锁定钱包

    描述:对给定的钱包进行锁定

    请求示例:

    $ curlhttp://localhost:8888/v1/wallet/lock -X POST -d '"default"'

    参数:

    返回数据:

    {}

     

    4、锁定所有钱包

    描述:对本地所有钱包进行锁定

    请求示例:

    $ curlhttp://localhost:8888/v1/wallet/lock_all

    返回数据:

    {}

     

    5、解锁钱包

    描述:对给定的钱包进行解锁操作

    请求示例:

    $ curl http://localhost:8888/v1/wallet/unlock-X POST -d '["default", "PW5KFWYKqvt63d4iNvedfDEPVZL227D3RQ1zpVFzuUwhMAJmRAYyX"]'

    参数:

     

     

    返回数据:

    {}

    6、钱包导入私钥

    描述:在给定的钱包里导入一个私钥

    请求示例:

    $ curlhttp://localhost:8888/v1/wallet/import_key -X POST -d '["default","5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"]'

    参数:

     

    返回数据:

    {}

    7、列出钱包

    描述:列出本地打开的所有钱包

    请求示例:

    $ curl http://localhost:8888/v1/wallet/list_wallets

    参数:

    返回数据:

    ["default *"]

    数据说明:

    *意味着解锁的钱包

    8、获取公钥列表

    描述:将本地所有钱包中所有账号的公钥列出

    请求示例:

    curl http://localhost:8888/v1/wallet/get_public_keys

    参数:

    返回数据:

    ["EOS5Dz1qfz9H5cmoofUC5h2ticeNgPUAjKsWK7EWySsEDs7m8iNTw","EOS5o9u5hPF3yWceYDwvJvXdqBvEAzUETW4jzL8oJpRk7XaYRbZTZ","EOS6ERAj2QTzExko3qe3hbReQAPPhsQmqHE9nszC172kXFceoJnu7"]

    数据说明:

     

    9、设置钱包自动上锁

    描述:在设定的时间内(以秒为单位),钱包自动锁定

    请求示例:

    curl http://localhost:8888/v1/wallet/set_timeout -X POST -d '10'

    参数:

     

     

    返回数据:

    {}

    10、交易签名

    curl http://localhost:8888/v1/wallet/sign_transaction-X POST -d '[{

                     "ref_block_num":21453,

                     "ref_block_prefix":3165644999,

                     "expiration":"2017-12-08T10:28:49",

                     "scope":["initb", "initc"],

                     "read_scope":[],

                     "messages":[{

                             "code":"currency",

                             "type":"transfer",

                             "authorization":[{

                                      "account":"initb",

                                      "permission":"active"

                             }],

                             "data":"000000008093dd74000000000094dd74e803000000000000"

                    }],

                     "signatures":[]

            },

            ["EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"],""

    ]’

    对交易信息和智能合约进行查看,找到与此数据相关数据

     

    11、获取最新区块信息

    描述:获取最新区块信息

    请求示例:

    curlhttp://127.0.0.1:8888/v1/chain/get_info

    参数:

    返回数据:

    {

    "server_version":"b2eb1667",

    "head_block_num":259590,

    "last_irreversible_block_num":259573,

    "head_block_id":"0003f60677f3707f0704f16177bf5f007ebd45eb6efbb749fb1c468747f72046",

    "head_block_time":"2017-12-10T17:05:36",

    "head_block_producer":"initp",

    "recent_slots":"1111111111111111111111111111111111111111111111111111111111111111",

    "participation_rate":"1.00000000000000000"

    }

    数据说明:

    head_block_num:意味着区块高度,第一个区块为1,依次累加

    last_irreversible_block_num:代表上一个不可逆转的区块高度(一般和当前区块间隔14以上)

    head_block_id:区块id

    head_block_producer:打包此区块的节点

     

    12、查询区块

     

    描述:通过给定的区块高度或区块id得到指定区块信息

    请求示例:

    $ curl http://127.0.0.1:8888/v1/chain/get_block -X POST -d'{"block_num_or_id":5}'

    $ curlhttp://127.0.0.1:8888/v1/chain/get_block -X POST -d'{"block_num_or_id":0000000445a9f27898383fd7de32835d5d6a978cc14ce40d9f327b5329de796b}'

     

    参数:

    区块高度/区块id

    返回数据:

    {

        "previous":"0000000674814e01a617ea58921f1e347b9904bd8b61c16de5d4efc73b5bd674",

        "timestamp":"2018-05-05T00:05:22",

        "transaction_merkle_root":"0000000000000000000000000000000000000000000000000000000000000000",

        "producer": "inith",

        "producer_changes":[],

        "producer_signature":"20121bd1010967fdc4839806250855652282106efbb3b16dfa768b9369f4fd5e2b011d742c8bfd61321b90c9282f1ecb33b6e9a6969a30ee8330a589065e05d910",

        "cycles": [],

        "id":"00000007103c0be2b7fa5710a3f92f4e740ccc1e5b5e201e49b4b776d599168d",

        "block_num": 7,

        "ref_block_prefix":274201271

    }

    接下来对数据进行说明:

    previous:父区块id

    Timestamp:产生时间

    transaction_merkle_root:默克尔根节点

    Producer:生产节点

    producer_signature:生产节点签名

    producer_changes:生产者列表的变化

    Id:区块id

    block_num:区块高度

    ref_block_prefix:引用的区块前缀

     

    13、获取账号信息

    描述:通过账号得到账号详情

    请求示例:

    $ curlhttp://127.0.0.1:8888/v1/chain/get_account -X POST -d'{"account_name":"inita"}'

    参数:

    账号名

    返回数据:

    {

        "account_name": "inita",

        "eos_balance": "999999.9992 EOS",

        "staked_balance": "0.0000EOS",

        "unstaking_balance":"0.0000 EOS",

        "last_unstaking_time": "1969-12-31T23:59:59",

        "permissions": [{

            "perm_name": "active",

            "parent": "owner",

            "required_auth": {

               "threshold": 1,

               "keys": [{

                   "key":"EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",

                   "weight": 1

               }],

               "accounts": []

            }

        }, {

            "perm_name": "owner",

            "parent": "",

            "required_auth": {

               "threshold": 1,

               "keys": [{

                   "key":"EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",

                   "weight": 1

               }],

               "accounts": []

            }

        }]

    }

    数据说明:

    eos_balance:账户的eos余额

    Permissions:指权限设定,inita账号有2个权限,一个是active另一个是owner,owner为拥有者,同样是active的父权限,一般操作由active权限进行执行,owner能够用来恢复active

    Threshold:阈值

    Weight:权重,假如权重达到阈值时,才有权限操作

    Key:公钥

     

    14、获取智能合约

    描述:获取智能合约

    请求示例:

    curlhttp://127.0.0.1:8888/v1/chain/get_code -X POST -d '{"account_name":"currency"}'

    参数:

    智能合约名(也就是部署此智能合约的名字,他们都用currency这个名字)

    返回数据:

    {

           "name":"currency",

           "code_hash":"a1c8c84b4700c09c8edb83522237439e33cf011a4d7ace51075998bd002e04c9",

           "wast":"(module  (type $0 (func (parami64 i64 i32) (result i32))) ...truncated",

           "abi":{

                   "types":[{

                            "new_type_name":"account_name",

                            "type":"name"

                   }],

                   "structs":[{

                            "name":"transfer",

                            "base":"",

                            "fields":[{

                                            "name":"from",

                                            "type":"account_name"

                                    },

                                    {

                                            "name":"to",

                                            "type":"account_name"

                                    },

                                    {

                                            "name":"quantity",

                                            "type":"uint64"

                                    }

                            ]

                   },{

                            "name":"account",

                            "base":"",

                            "fields":[{

                                            "name":"key",

                                            "type":"name"

                                    },

                                    {

                                            "name":"balance",

                                            "type":"uint64"

                                    }

                            ]

                   }],

                   "actions":[{

                            "name":"transfer",

                            "type":"transfer"

                   }],

                   "tables":[{

                            "name":"account",

                            "type":"account",

                            "index_type":"i64",

                            "key_names":["key"],

                            "key_types":["name"]

                   }]

           }

    数据说明:

    这个智能合约currency是部署在currency账号上面,主要功能是交易”transfer”

     

     

    15、get_table_rows

    描述:获得指定账号在智能合约上的数据

    请求示例:

    $ curl http://127.0.0.1:8888/v1/chain/get_table_rows -XPOST -d '{"scope":"inita","code":"currency", "table":"account","json": true}'

    $ curl http://127.0.0.1:8888/v1/chain/get_table_rows -XPOST -d '{"scope":"inita","code":"currency", "table":"account","json": true, "lower_bound":0, "upper_bound":-1,"limit":10}'

    参数:

    Scope:代表帐户名

    Code:智能合约名

    Table:代表表名

    Json:是否以json格式显示

     

    返回数据:

    {

    "rows": [{

    "key": "account",

    "balance": 1000

    }],

    "more": false

    }

    数据说明:

    Key:表名

    Balance:所查询账户余额

     

    16、json转16进制

    描述:通过把json序列化,产出结果常用为交易中的参数

    请求示例:

    $ curlhttp://127.0.0.1:8888/v1/chain/abi_json_to_bin -X POST -d'{"code":"currency","action":"transfer","args":{"from":"currency","to":"inita", "quantity":100}}'

    参数:

    Code:智能合约名

    action:智能合约中的规定动作,transfer为转账动作

    initb账户向initc账户转账1000

    返回数据:

    {"binargs":"000000008093dd74000000000094dd74e803000000000000","required_scope":[],"required_auth":[]}

    数据说明:

     

    17、16进制转json

    描述:

    将上面得到的结果反序列化

    请求示例:

    $ curl http://127.0.0.1:8888/v1/chain/abi_bin_to_json-X POST -d '{"code":"currency","action":"transfer","binargs":"000000008093dd74000000000094dd74e803000000000000"}'

    参数:

    Code:智能合约名

    action:智能合约中的规定动作,transfer是转账动作

    Json:是否以json格式显示

     

    返回数据:

    {

    "args": {

    "from": "initb",

    "to": "initc",

    "quantity": 1000

    },

    "required_scope": [],

    "required_auth": []

    }

    数据说明:

     

    18、发送交易

    这种方法需要一条json格式的交易信息,接着会尝试将交易在链上执行。

    响应:

    成功即会返回 HTTP200和交易id

    {

    'transaction_id' : "..."

    }

    交易实际上是在本地发送的,并不代表交易已经上链。

    假如发生错误会返回http400(无效的参数)或500(内部服务器错误)

    HTTP/1.1 500 Internal Server Error

    Content-Length: 1466

    ...error message...

    请求示例:

    接下来的示例假定了一条转账操作,此时的ref_block_num和ref_block_prefix是 “/v1/chain/get_block +上一个不可逆的区块”提供的结果. 你还要使用 /v1/wallet/sign_transaction获得正确的签名

    curl http://localhost:8888/v1/chain/push_transaction -X POST -d'{"ref_block_num":10975,"ref_block_prefix":2385262967,"expiration":"2018-05-10T17:26:21","scope":["currency","inita"],"messages":[{"code":"currency","type":"transfer","recipients":["currency","inita"],"authorization":[{"account":"currency","permission":"active"}],"data":"0000001e4d75af46000000000093dd746400000000000000"}],"signatures":["1f7088ee47102461c2e1e25f7e17034802c3d067e82c5e02b81951199a00e7dbe74018f7c3a3d5eac9a89e30cde3b7705add8ebccf7b50f3675d60789b3a2ca7f5"],"authorizations":[]}'

     

    19、发送多条交易

    请求示例:

    curl http://localhost:8888/v1/chain/push_transaction -XPOST -d '[{"ref_block_num":10975,"ref_block_prefix":2385262967,"expiration":"2018-05-10T17:26:21","scope":["currency","inita"],"messages":[{"code":"currency","type":"transfer","recipients":["currency","inita"],"authorization":[{"account":"currency","permission":"active"}],"data":"0000001e4d75af46000000000093dd746400000000000000"}],"signatures":["1f7088ee47102461c2e1e25f7e17034802c3d067e82c5e02b81951199a00e7dbe74018f7c3a3d5eac9a89e30cde3b7705add8ebccf7b50f3675d60789b3a2ca7f5"],"authorizations":[]}, {"ref_block_num":10975,"ref_block_prefix":2385262967,"expiration":"2018-05-10T17:26:21","scope":["currency","initb"],"messages":[{"code":"currency","type":"transfer","recipients":["currency","initb"],"authorization":[{"account":"currency","permission":"active"}],"data":"0000001e4d75af46000000008093dd746400000000000000"}],"signatures":["1f7088ee47102461c2e1e25f7e17034802c3d067e82c5e02b81951199a00e7dbe74018f7c3a3d5eac9a89e30cde3b7705add8ebccf7b50f3675d60789b3a2ca7f5"],"authorizations":[]}]'

     

    20、获取所需key

    Getrequired keys to sign a transaction from list of your keys.

    在列表里获获得签署一条交易所需的key

    请求示例:

    curl http://localhost:8888/v1/chain/get_required_keys-X POST -d '{"transaction":{"ref_block_num":"100","ref_block_prefix":"137469861","expiration":"2017-09-25T06:28:49","scope":["initb","initc"],"actions":[{"code":"currency","type":"transfer","recipients":["initb","initc"],"authorization":[{"account":"initb","permission":"active"}],"data":"000000000041934b000000008041934be803000000000000"}],"signatures":[],"authorizations":[]},"available_keys":["EOS4toFS3YXEQCkuuw1aqDLrtHim86Gz9u3hBdcBw5KNPZcursVHq","EOS7d9A3uLe6As66jzN8j44TXJUqJSK3bFjjEEqR4oTvNAB3iM9SA","EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"]}'

    返回数据:

    {

    "required_keys": [

    "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"

    ]

    }

    现在一般的交易所都不会自己去单独开发一套钱包系统了。 不仅因为比特币以太坊等区块节点数据庞大,同步传输慢,而且在服务器和带宽的花费成本比较高。最重要的是养不起这样的一个技术团队!原生钱包这种私钥放在服务器非常不安全,被偷就凉凉了。

  • 相关阅读:
    再不迁移到Material Design Components 就out啦
    Material Design Compoents 1.1.0
    Android低功耗蓝牙总结
    JAVA8 对象排序
    记录UIButton 自适应的一个坑
    MySQL 索引大全
    MySQL 与 MongoDB 的区别
    Flutter https://flutter.cn/docs学习之添加资源和图片
    Flutter https://flutter.cn/docs学习之加入交互体验
    Flutter https://flutter.cn/docs学习之向 Android 应用中添加闪屏页和启动页
  • 原文地址:https://www.cnblogs.com/fly-l/p/12881885.html
Copyright © 2020-2023  润新知