bitshares研究系列【初识cli_wallet】

in bitshares •  7 years ago 

bitshares研究系列【mac下安装bitshares】这篇文章里,安装并运行bitshares节点时, 看到有个cli_wallet的东西,大概知道通过这个执行一些命令比较简单,但一直没用它,今天来看看这个cli_wallet。

安装并运行

编译安装bitshares后,cli_wallet也同时准备好了,执行文件在./programs/cli_wallet/cli_wallet,也可以直接到文件所在目录运行:

Chaim:cli_wallet Chaim$ ./cli_wallet -s ws://127.0.0.1:8090

因为我已经在本地跑了节点,可以直接连接127.0.0.1,如果本地没有节点,也可以找一个线上的节点,例如:

Chaim:cli_wallet Chaim$ ./cli_wallet -s ws://bitshares.dacplay.org:8090

如果自己建的链,chainid改变了,也可以通过参数 --chain-id 指定。更多参数 --help

常用命令

刚进去会提示设置密码,如下:

Logging RPC to file: logs/rpc/rpc.log
504831ms th_a       main.cpp:136                  main                 ] key_to_wif( committee_private_key ): 5KCBDTcyDqzsqehcb52tW5nU6pXife6V2rX9Yf7c3saYSzbDZ5W 
504834ms th_a       main.cpp:140                  main                 ] nathan_pub_key: BTS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV 
504838ms th_a       main.cpp:141                  main                 ] key_to_wif( nathan_private_key ): 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3 
Starting a new wallet with chain ID 4018d7844c78f6a6c41c6a552b898022310fc5dec06da467ee7905a8dad512c8 (from egenesis)
504843ms th_a       main.cpp:188                  main                 ] wdata.ws_server: ws://127.0.0.1:8090 
504869ms th_a       main.cpp:193                  main                 ] wdata.ws_user:  wdata.ws_password:  
Please use the set_password method to initialize a new wallet before continuing

调用set_password设置一个钱包密码:

new >>> set_password 123456
set_password 123456
null

设置密码后,提示locked,在一些操作时需要先解锁:

locked >>> unlock 123456
unlock 123456
null
unlocked >>> suggest_brain_key

这些命令提示比较明显,还有哪些命令呢?习惯性的help一下:

unlocked >>> help
help
                          variant_object about()
                                    void add_operation_to_builder_transaction(transaction_handle_type transaction_handle, const operation & op)
                      signed_transaction approve_proposal(const string & fee_paying_account, const string & proposal_id, const approval_delta & delta, bool broadcast)
                 transaction_handle_type begin_builder_transaction()
                      signed_transaction bid_collateral(string bidder_name, string debt_amount, string debt_symbol, string additional_collateral, bool broadcast)
                   vector<blind_receipt> blind_history(string key_or_account)
                      blind_confirmation blind_transfer(string from_key_or_label, string to_key_or_label, string amount, string symbol, bool broadcast)
                      signed_transaction borrow_asset(string borrower_name, string amount_to_borrow, string asset_symbol, string amount_of_collateral, bool broadcast)
pair<transaction_id_type, signed_transaction> broadcast_transaction(signed_transaction tx)
                      signed_transaction cancel_order(object_id_type order_id, bool broadcast)
                      signed_transaction create_account_with_brain_key(string brain_key, string account_name, string registrar_account, string referrer_account, bool broadcast)
                      signed_transaction create_asset(string issuer, string symbol, uint8_t precision, asset_options common, fc::optional<bitasset_options> bitasset_opts, bool broadcast)
                      signed_transaction create_committee_member(string owner_account, string url, bool broadcast)
                      signed_transaction create_witness(string owner_account, string url, bool broadcast)
                      signed_transaction create_worker(string owner_account, time_point_sec work_begin_date, time_point_sec work_end_date, share_type daily_pay, string name, string url, variant worker_settings, bool broadcast)
                                    void dbg_generate_blocks(std::string debug_wif_key, uint32_t count)
                                    void dbg_make_mia(string creator, string symbol)
                                    void dbg_make_uia(string creator, string symbol)
                                    void dbg_push_blocks(std::string src_filename, uint32_t count)
                                    void dbg_stream_json_objects(const std::string & filename)
                                    void dbg_update_object(fc::variant_object update)
                  vector<brain_key_info> derive_owner_keys_from_brain_key(string brain_key, int number_of_desired_keys)
            map<public_key_type, string> dump_private_keys()
                                    void flood_network(string prefix, uint32_t number_of_transactions)
                      signed_transaction fund_asset_fee_pool(string from, string symbol, string amount, bool broadcast)
                          account_object get_account(string account_name_or_id)
                                uint64_t get_account_count()
                vector<operation_detail> get_account_history(string name, int limit)
        account_history_operation_detail get_account_history_by_operations(string name, vector<uint16_t> operation_types, uint32_t start, int limit)
                         account_id_type get_account_id(string account_name_or_id)
                            asset_object get_asset(string asset_name_or_id)
              asset_bitasset_data_object get_bitasset_data(string asset_name_or_id)
        optional<signed_block_with_info> get_block(uint32_t num)
               vector<call_order_object> get_call_orders(string a, uint32_t limit)
           vector<collateral_bid_object> get_collateral_bids(string asset, uint32_t limit, uint32_t start)
                 committee_member_object get_committee_member(string owner_account)
          dynamic_global_property_object get_dynamic_global_properties()
                  global_property_object get_global_properties()
              vector<limit_order_object> get_limit_orders(string a, string b, uint32_t limit)
                   vector<bucket_object> get_market_history(string symbol, string symbol2, uint32_t bucket, fc::time_point_sec start, fc::time_point_sec end)
                                 variant get_object(object_id_type id)
                              order_book get_order_book(const string & base, const string & quote, unsigned limit)
                                  string get_private_key(public_key_type pubkey)
                               operation get_prototype_operation(string operation_type)
                vector<operation_detail> get_relative_account_history(string name, uint32_t stop, int limit, uint32_t start)
         vector<force_settlement_object> get_settle_orders(string a, uint32_t limit)
                     transaction_id_type get_transaction_id(const signed_transaction & trx)
vector<vesting_balance_object_with_info> get_vesting_balances(string account_name)
                          witness_object get_witness(string owner_account)
                                  string gethelp(const string & method)
                      signed_transaction global_settle_asset(string symbol, price settle_price, bool broadcast)
                                  string help()
                                    bool import_account_keys(string filename, string password, string src_account_name, string dest_account_name)
                       map<string, bool> import_accounts(string filename, string password)
              vector<signed_transaction> import_balance(string account_name_or_id, const vector<string> & wif_keys, bool broadcast)
                                    bool import_key(string account_name_or_id, string wif_key)
                                 variant info()
                                    bool is_locked()
                                    bool is_new()
                                    bool is_public_key_registered(string public_key)
                      signed_transaction issue_asset(string to_account, string amount, string symbol, string memo, bool broadcast)
                           vector<asset> list_account_balances(const string & id)
            map<string, account_id_type> list_accounts(const string & lowerbound, uint32_t limit)
                    vector<asset_object> list_assets(const string & lowerbound, uint32_t limit)
   map<string, committee_member_id_type> list_committee_members(const string & lowerbound, uint32_t limit)
                  vector<account_object> list_my_accounts()
            map<string, witness_id_type> list_witnesses(const string & lowerbound, uint32_t limit)
                                    bool load_wallet_file(string wallet_filename)
                                    void lock()
                                    void network_add_nodes(const vector<string> & nodes)
                         vector<variant> network_get_connected_peers()
                                  string normalize_brain_key(string s)
                             transaction preview_builder_transaction(transaction_handle_type handle)
                      signed_transaction propose_builder_transaction(transaction_handle_type handle, time_point_sec expiration, uint32_t review_period_seconds, bool broadcast)
                      signed_transaction propose_builder_transaction2(transaction_handle_type handle, string account_name_or_id, time_point_sec expiration, uint32_t review_period_seconds, bool broadcast)
                      signed_transaction propose_fee_change(const string & proposing_account, fc::time_point_sec expiration_time, const variant_object & changed_values, bool broadcast)
                      signed_transaction propose_parameter_change(const string & proposing_account, fc::time_point_sec expiration_time, const variant_object & changed_values, bool broadcast)
                      signed_transaction publish_asset_feed(string publishing_account, string symbol, price_feed feed, bool broadcast)
                                  string read_memo(const memo_data & memo)
                           blind_receipt receive_blind_transfer(string confirmation_receipt, string opt_from, string opt_memo)
                      signed_transaction register_account(string name, public_key_type owner, public_key_type active, string registrar_account, string referrer_account, uint32_t referrer_percent, bool broadcast)
                                    void remove_builder_transaction(transaction_handle_type handle)
                                    void replace_operation_in_builder_transaction(transaction_handle_type handle, unsigned operation_index, const operation & new_op)
                      signed_transaction reserve_asset(string from, string amount, string symbol, bool broadcast)
                                    void save_wallet_file(string wallet_filename)
                      signed_transaction sell_asset(string seller_account, string amount_to_sell, string symbol_to_sell, string min_to_receive, string symbol_to_receive, uint32_t timeout_sec, bool fill_or_kill, bool broadcast)
                                  string serialize_transaction(signed_transaction tx)
                      signed_transaction set_desired_witness_and_committee_member_count(string account_to_modify, uint16_t desired_number_of_witnesses, uint16_t desired_number_of_committee_members, bool broadcast)
                                   asset set_fees_on_builder_transaction(transaction_handle_type handle, string fee_asset)
                                    void set_password(string password)
                      signed_transaction set_voting_proxy(string account_to_modify, optional<string> voting_account, bool broadcast)
                      signed_transaction settle_asset(string account_to_settle, string amount_to_settle, string symbol, bool broadcast)
                      signed_transaction sign_builder_transaction(transaction_handle_type transaction_handle, bool broadcast)
                               memo_data sign_memo(string from, string to, string memo)
                      signed_transaction sign_transaction(signed_transaction tx, bool broadcast)
                          brain_key_info suggest_brain_key()
                      signed_transaction transfer(string from, string to, string amount, string asset_symbol, string memo, bool broadcast)
pair<transaction_id_type, signed_transaction> transfer2(string from, string to, string amount, string asset_symbol, string memo)
                      blind_confirmation transfer_from_blind(string from_blind_account_key_or_label, string to_account_id_or_name, string amount, string asset_symbol, bool broadcast)
                      blind_confirmation transfer_to_blind(string from_account_id_or_name, string asset_symbol, vector<pair<string, string>> to_amounts, bool broadcast)
                                    void unlock(string password)
                      signed_transaction update_asset(string symbol, optional<string> new_issuer, asset_options new_options, bool broadcast)
                      signed_transaction update_asset_feed_producers(string symbol, flat_set<string> new_feed_producers, bool broadcast)
                      signed_transaction update_bitasset(string symbol, bitasset_options new_options, bool broadcast)
                      signed_transaction update_witness(string witness_name, string url, string block_signing_key, bool broadcast)
                      signed_transaction update_worker_votes(string account, worker_vote_delta delta, bool broadcast)
                      signed_transaction upgrade_account(string name, bool broadcast)
                      signed_transaction vote_for_committee_member(string voting_account, string committee_member, bool approve, bool broadcast)
                      signed_transaction vote_for_witness(string voting_account, string witness, bool approve, bool broadcast)
                      signed_transaction whitelist_account(string authorizing_account, string account_to_list, account_whitelist_operation::account_listing new_listing_status, bool broadcast)
                      signed_transaction withdraw_vesting(string witness_name, string amount, string asset_symbol, bool broadcast)

看起来一些取帐号、取资产数据、转帐功能都有,以前都是通过websocket命令发送的,看来用cli_wallet可以简单一些了。

帐户命令

链上有预先存在的帐户,在启动时可以看到nathan这个帐户和私钥,直接导入:

unlocked >>> import_key "nathan" 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
import_key "nathan" 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
461790ms th_a       wallet.cpp:470                copy_wallet_file     ] backing up wallet wallet.json to before-import-key-9407629b-1.wallet
false
unlocked >>> list_my_accounts
list_my_accounts
[{
    "id": "1.2.298",
    ...
  },{
    "id": "1.2.861586",
    ...
  }
]

以上有两个帐户,"1.2.298"就是"nathan","1.2.861586"是我导入了"barnard18"这个帐户。

如果已导入的帐户,可以通过公钥取私钥:

unlocked >>> get_private_key BTS5dd18jA7tNkDV4EErxYaEd9upxvzuAna4hzdvgH3tspnMUMC74

取帐户余额:

unlocked >>> list_account_balances barnard18
list_account_balances barnard18
7.97012 BTS

看起来我很穷,有人给转点吗?

看看"nathan"这个帐户:

unlocked >>> list_account_balances nathan
list_account_balances nathan
24979.32030 BTS
0.00000868 BTC
0.0001 CNY
0 USD
178.0223 SHAREBIT
44.64000000 DNXTO
10000 BADCOIN
1 DECENTRALIZED

转帐

因为要终身会员才能注册帐号,只能先去官网上注册一个"barnard20",试下转帐功能

注意bitshares官网会在浏览器中记录已注册帐户信息,如果要注册新帐户需要清除浏览器缓存,要不就得自己帐户支付注册费了
我能和你们说,我把barnard19的owner key删掉了吗?然后还设成了多重签名支付,已经无法转帐了

unlocked >>> transfer barnard18 barnard20 1 BTS "don't lose your key!" true
transfer barnard18 barnard20 1 BTS "don't lose your key!" true
10 assert_exception: Assert Exception
it != _keys.end(): 
    {}
    th_a  wallet.cpp:656 get_private_key

    {"from":"barnard18","to":"barnard20","amount":"1","asset_symbol":"BTS","memo":"don't lose your key!","broadcast":true}
    th_a  wallet.cpp:2030 transfer

这个好像是barnard18的active key没有引入,先用import_key引入active private key,再执行:

unlocked >>> transfer barnard18 barnard20 1 BTS "don't lose your key!" true
transfer barnard18 barnard20 1 BTS "don't lose your key!" true
{
  "ref_block_num": 26129,
  "ref_block_prefix": 956066014,
  "expiration": "2018-04-21T08:38:57",
  "operations": [[
      0,{
        "fee": {
          "amount": 11030,
          "asset_id": "1.3.0"
        },
        "from": "1.2.861586",
        "to": "1.2.879822",
        "amount": {
          "amount": 100000,
          "asset_id": "1.3.0"
        },
        "memo": {
          "from": "BTS5snEyiDkP6dReWafV4jqoHZYqcg2D8L4n2yUmBipyysLmZsRna",
          "to": "BTS8EdfKXbmNrzcngwZBZpUrTNotsFGFWTRKpNaBpcGRrx4Qrqcz6",
          "nonce": "7207283703702597608",
          "message": "7e1ed9398b911f0be661d099f52dab0df99a7045922697c9d45ceec7c9c863c8"
        },
        "extensions": []
      }
    ]
  ],
  "extensions": [],
  "signatures": [
    "1f3ce7ba5bcac69e0808ea11f0b82401c4dc3e6caf40a6aeacc9be269dbc80c628187ae74df65c5a1fad7ccd69a4783575c89962e08f002a7c81581d094014b43f"
  ]
}

取下这两个帐户的余额:

unlocked >>> list_account_balances barnard18
list_account_balances barnard18
6.85982 BTS

unlocked >>> list_account_balances barnard20
list_account_balances barnard20
1 BTS

取配置和数据

取一些全局属性

unlocked >>> get_global_properties
get_global_properties
{
  "id": "2.0.0",
...
}

取帐户数据

unlocked >>> get_account barnard18
get_account barnard18
{
  "id": "1.2.861586",
  "membership_expiration_date": "1970-01-01T00:00:00",
  "registrar": "1.2.450921",
  "referrer": "1.2.450921",
  "lifetime_referrer": "1.2.450921",
  "network_fee_percentage": 2000,
  "lifetime_referrer_fee_percentage": 3000,
  "referrer_rewards_percentage": 7000,
  "name": "barnard18",
  "owner": {
    "weight_threshold": 1,
    "account_auths": [],
    "key_auths": [[
        "BTS5dd18jA7tNkDV4EErxYaEd9upxvzuAna4hzdvgH3tspnMUMC74",
        1
      ]
    ],
    "address_auths": []
  },
  "active": {
    "weight_threshold": 1,
    "account_auths": [],
    "key_auths": [[
        "BTS5snEyiDkP6dReWafV4jqoHZYqcg2D8L4n2yUmBipyysLmZsRna",
        1
      ]
    ],
    "address_auths": []
  },
  "options": {
    "memo_key": "BTS5snEyiDkP6dReWafV4jqoHZYqcg2D8L4n2yUmBipyysLmZsRna",
    "voting_account": "1.2.5",
    "num_witness": 0,
    "num_committee": 0,
    "votes": [],
    "extensions": []
  },
  "statistics": "2.6.861586",
  "whitelisting_accounts": [],
  "blacklisting_accounts": [],
  "whitelisted_accounts": [],
  "blacklisted_accounts": [],
  "owner_special_authority": [
    0,{}
  ],
  "active_special_authority": [
    0,{}
  ],
  "top_n_control_flags": 0
}

bitshares研究系列【 bitshares资产数据】这个文章中的一些数据就可以直接通过cli_wallet来取了,如:

unlocked >>> get_asset 1.3.121
get_asset 1.3.121
{
  "id": "1.3.121",
  "symbol": "USD",
  "precision": 4,
  "issuer": "1.2.0",
  "options": {
    "max_supply": "1000000000000000",
    "market_fee_percent": 10,
    "max_market_fee": "1000000000000000",
    "issuer_permissions": 511,
    "flags": 129,
    "core_exchange_rate": {
      "base": {
        "amount": 197,
        "asset_id": "1.3.121"
      },
      "quote": {
        "amount": 6011,
        "asset_id": "1.3.0"
      }
    },
    "whitelist_authorities": [],
    "blacklist_authorities": [],
    "whitelist_markets": [],
    "blacklist_markets": [],
    "description": "{\"main\":\"1 United States dollar\",\"market\":\"\"}",
    "extensions": []
  },
  "dynamic_asset_data_id": "2.3.121",
  "bitasset_data_id": "2.4.21"
}
unlocked >>> get_object 2.4.21
get_object 2.4.21

好了,就这些了,更多的使用方法后续有需要再用!

参考

http://docs.bitshares.org/integration/apps/cliwallet.html

https://github.com/cryptonomex/graphene/wiki/CLI-Wallet-Cookbook

https://github.com/bitshares/bitshares-core/wiki/private-testnet


感谢您阅读 @chaimyu 的帖子,期待您能留言交流!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
http://docs.bitshares.org/development/namespaces/wallet.html

Loading...

主网的 nathan 账号私钥不一样。两个账号同时都在钱包文件了,感觉是有些 bug 。

import_key "nathan" 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
461790ms th_a wallet.cpp:470 copy_wallet_file ] backing up wallet wallet.json to before-import-key-9407629b-1.wallet
false

这个import_key是返回false的,只是在list_my_accounts中能看到了,等看看这实现

_wallet.update_account(account);

不管import成功还是失败都会把帐号数据导入钱包,还在注释里说明了
// @returns true if the key matches a current active/owner/memo key for the named
// account, false otherwise (but it is stored either way)

方便执行下network_get_connected_peers看下返回结构吗?我连接的私网,这个命令异常,看不到返回信息。谢谢~