EOS合约开发第五章-钱包和账户手册

钱包和账户手册

一、创建和启动单个节点

使用以下命令来启动私有的单节点EOSIO:

cd build/programs/nodeos

./nodeos -e -p eosio --plugin eosio::wallet_api_plugin --plugin eosio::chain_api_plugin --plugin eosio::account_history_api_plugin 

运行时,nodeos会输出以下的日志消息,这意味着EOSIO已经成功生产EOS区块。

2192031ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/acco                                                                                                                                              unt_history/get_transaction
2192031ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/acco                                                                                                                                              unt_history/get_transactions
2192031ms thread-0   net_plugin.cpp:2760           plugin_startup       ] starting listener, ma                                                                                                                                              x clients is 25
2192031ms thread-0   producer_plugin.cpp:161       plugin_startup       ] producer plugin:  plu                                                                                                                                              gin_startup() begin
2192031ms thread-0   producer_plugin.cpp:166       plugin_startup       ] Launching block produ                                                                                                                                              ction for 1 producers.


*******************************
*                             *
*   ------ NEW CHAIN ------   *
*   -  Welcome to EOSIO!  -   *
*   -----------------------   *
*                             *
*******************************


Your genesis seems to have an old timestamp
Please consider using the --genesis-timestamp option to give your genesis a recent timestamp


2192031ms thread-0   producer_plugin.cpp:176       plugin_startup       ] producer plugin:  plu                                                                                                                                              gin_startup() end
eosio generated block 0e03ce32... #1 @ 2018-04-23T00:36:32.500 with 0 trxs, lib: 0
eosio generated block 68b72661... #2 @ 2018-04-23T00:36:33.000 with 0 trxs, lib: 1
eosio generated block 65485d5d... #3 @ 2018-04-23T00:36:33.500 with 0 trxs, lib: 2
eosio generated block 4444c6a5... #4 @ 2018-04-23T00:36:34.000 with 0 trxs, lib: 3

eosio generated block 1b43b858... #5 @ 2018-04-23T00:36:34.500 with 0 trxs, lib: 4

下图描述了以上刚创建的单节点EOSIO,在上面的配置中,使用nodeos加载钱包插件来管理钱包,nodeos命令行参数中指定的eosio::wallet_api_plugin就是用来启动钱包管理的。无论是nodeos启动钱包管理还是keosd,cleos都是可用来管理钱包、管理账户和调用EOS.IO的功能。

二、配置

默认的配置文件夹路径如下:

mac: ~/Library/Application Support/eosio/nodeos/config
Linux: ~/.local/share/eosio/nodeos/config

也可以指定配置文件夹,在启动nodeos使用的命令行参数中添加--config-dir来指定nodeos使用的配置文件夹。

配置文件夹下需要有两个文件:genesis.json和config.ini。

启动nodeos后,nodeos在配置的文件夹中查找config.ini,如果找不到,则会创建一个默认的config.ini。

如果需要和上面的命令行启动一样的nodeos,则需要修改已有的默认配置文件:

   # Load the testnet genesis state, which creates some initial block producers with the default key
   genesis-json = /path/to/eos/source/genesis.json
   # Enable production on a stale chain, since a single-node test chain is pretty much always stale
   enable-stale-production = true
   # Enable block production with the testnet producers
   producer-name = eosio
   # Load the block producer plugin, so you can produce blocks
   plugin = eosio::producer_plugin
   # Wallet plugin
   plugin = eosio::wallet_api_plugin
   # As well as API and HTTP plugins
   plugin = eosio::chain_api_plugin
   plugin = eosio::http_plugin
   # This will be used by the validation step below, to view account history

   plugin = eosio::account_history_api_plugin

现在可以使用以下命令运行nodeos:

build/programs/nodeos/nodeos

三、数据和日志

数据和日志默认的文件路径如下:

mac: ~/Library/Application Support/eosio/nodeos/data
Linux: ~/.local/share/eosio/nodeos/data
也可以指定数据文件路劲,在启动nodeos使用的命令行参数中添加--data-dir来指定nodeos使用的数据文件路径。

nodeos将运行时数据(共享内存、日志等)存储在自定义的数据文件路径下。

四、账户和钱包概念

下图是EOSIO账户和钱包的概念视图:

钱包可以被认为是公私钥对的存储库,发送到区块链上执行的操作需要私钥进行签名,通过将密钥本地存储在被锁定的加密存储中,钱包可以以安全的方式获得密钥来进行签名。keosd则完成了这些功能,而cleos则用来访问钱包。

账户则被认为是区块链上的一个带有访问权限控制的安全主体,nodeos则管理了区块链上账户的发布以及账户相关的action。而cleos则用来访问nodeos的账户管理功能。

账户和钱包之间没有固定的关系,nodeos和keosd之间也没有内在联系,他们的基本功能是根本不同的。Cleos有效的充当了keosd密钥检索操作和nodeos账户需要使用这些密钥进行签名的区块链操作之间的中间。

五、创建和管理钱包

进入cleos程序目录

cd /path_to_eos/build/programs/cleos
使用cleos的wallet create命令创建一个钱包
[kingnet@bctestnode3 cleos]$ ./cleos wallet create
Creating wallet: default
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.

"PW5KfoH51MEQ4Yqg1zaz22AdcGKAvaZipL5gtFtgggdJpcbLykLAg"

这里keosd创建了一个名为default的钱包,并且返回了这个钱包的主密码,这个密码用于解锁该钱包文件。

这个钱包文件被命名为default.wallet。默认情况下,keosd将钱包存储在data-dir/eosio-wallet文件目录下,钱包数据文件目录是在启动钱包时的命令行参数--data-dir中指定的。

六、管理多个钱包

Cleos可以管理多个钱包,每个个人钱包受不同的钱包主密码保护,下面创建了另一个钱包并使用参数-n来命名它。
[kingnet@bctestnode3 cleos]$ ./cleos wallet create -n gaoyuan
Creating wallet: gaoyuan
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5Js5S7YFPicSrVW6wC35xeDAwaCL63s8VPXhWkVVU3kZQNvL9u4"
列出所有的钱包:
[kingnet@bctestnode3 cleos]$ ./cleos wallet list
Wallets:
[
  "default *",
  "gaoyuan *"
]
每个列出的钱包后面的星号(*)意味着相应的钱包是解锁状态,创建的账户默认是解锁的。
锁定一个钱包:
[kingnet@bctestnode3 cleos]$ ./cleos wallet lock -n gaoyuan
Locked: gaoyuan
[kingnet@bctestnode3 cleos]$ ./cleos wallet list
Wallets:
[
  "default *",
  "gaoyuan"
]
使用wallet unlock来解锁钱包
[kingnet@bctestnode3 cleos]$ ./cleos wallet unlock -n gaoyuan
password: Unlocked: gaoyuan
[kingnet@bctestnode3 cleos]$ ./cleos wallet list
Wallets:
[
  "default *",
  "gaoyuan *"
]
如果重启了keosd,执行以下命令:
[kingnet@bctestnode3 cleos]$ ./cleos wallet list
Wallets:
[]
这里没有任何钱包,首先需要打开一个钱包才能进行钱包操作,包括列出钱包。关闭keosd时钱包被锁定且keosd重新启动后,钱包并未打开。
打开钱包:
[kingnet@bctestnode3 cleos]$ ./cleos wallet open
Opened: default
[kingnet@bctestnode3 cleos]$ ./cleos wallet list
Wallets:
[
  "default"
]
如果wallet命令中没有使用-n指定钱包名称,则使用‘default’钱包。
[kingnet@bctestnode3 cleos]$ ./cleos wallet open -n gaoyuan
Opened: gaoyuan
[kingnet@bctestnode3 cleos]$ ./cleos wallet list
Wallets:
[
  "default",
  "gaoyuan"
]

打开的钱包默认是锁定的。

七、生成和导入密钥

一个空的钱包没有什么用,需要导入密钥到钱包才能使用。
生成密钥对有很多种方法,在这里介绍cleos的create key命令方法。
生成两对公私钥:
[kingnet@bctestnode3 cleos]$ ./cleos create key
Private key: 5Hpc9t34wXN3uE5Yi8ofMR7jNRJVVLQqpSYHxZ2QUq1purCP1UK
Public key: EOS6rfkVfqWXVdY71JHMj3bquv6sri5gd5aqgjiUU4EPnha96yWU8
[kingnet@bctestnode3 cleos]$ ./cleos create key
Private key: 5JVNBoNPMwePkinqh6ABBrhamqfVtopFaVZJBjQCCqsnL5MqkBi
Public key: EOS75r6BFqZ6gwgsjQgJfdyGGVYqdNfaiUtjJtmRXXL4h4KhxvqkX
打开并解锁默认钱包
将以上生成的两个私钥导入钱包:
[kingnet@bctestnode3 cleos]$ ./cleos wallet import 5Hpc9t34wXN3uE5Yi8ofMR7jNRJVVLQqpSYHxZ2QUq1purCP1UK
imported private key for: EOS6rfkVfqWXVdY71JHMj3bquv6sri5gd5aqgjiUU4EPnha96yWU8
[kingnet@bctestnode3 cleos]$ ./cleos wallet import 5JVNBoNPMwePkinqh6ABBrhamqfVtopFaVZJBjQCCqsnL5MqkBi
imported private key for: EOS75r6BFqZ6gwgsjQgJfdyGGVYqdNfaiUtjJtmRXXL4h4KhxvqkX
检查导入的密钥对:
[kingnet@bctestnode3 cleos]$ ./cleos wallet keys
[[
    "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
    "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
  ],[
    "EOS6rfkVfqWXVdY71JHMj3bquv6sri5gd5aqgjiUU4EPnha96yWU8",
    "5Hpc9t34wXN3uE5Yi8ofMR7jNRJVVLQqpSYHxZ2QUq1purCP1UK"
  ],[
    "EOS75r6BFqZ6gwgsjQgJfdyGGVYqdNfaiUtjJtmRXXL4h4KhxvqkX",
    "5JVNBoNPMwePkinqh6ABBrhamqfVtopFaVZJBjQCCqsnL5MqkBi"
  ]
]

钱包文件本身被加密,因此钱包在锁定时将保护这些密钥,访问锁定的钱包中的密钥需要在创建钱包期间提供的主密码。

八、备份钱包

既然钱包包含密钥,最好经常备份钱包,钱包文件在启动keosd时命令行参数data-dir配置的文件目录下。

[kingnet@bctestnode3 cleos]$ ls -l ~/.local/share/eosio/nodeos/data/
total 16
drwxr-xr-x 2 kingnet users 4096 Apr 23 08:36 blocks
-rw------- 1 kingnet users  567 Apr 23 10:37 default.wallet
-rw------- 1 kingnet users  311 Apr 23 10:09 gaoyuan.wallet

drwxr-xr-x 2 kingnet users 4096 Apr 23 08:36 shared_mem

九、创建账户

在区块链上执行操作需要账户,cleos通过请求nodeos在区块链上创建账户,以下命令均需要启动nodeos,同时某些命令还需要启动keosd,所以最好将nodeos和keosd同时运行起来。
目前keosd和nodeos的默认端口都为8888,我们为了更好了解nodeos和keosd的功能,我们将把keosd的端口改为8899,以下两种方法都可以做到:
1. 编辑keosd配置文件
编辑/data-dir/config/config.ini文件,将http-server-address属性修改为:
http-server-address = 127.0.0.1:8899
2. 使用命令行参数
启动keosd是增加命令行参数 --http-server-address = localhost:8899
使用命令行参数启动keosd:
[kingnet@bctestnode3 keosd]$ ./keosd --http-server-address=localhost:8899
136660ms thread-0   wallet_plugin.cpp:41          plugin_initialize    ] initializing wallet plugin
136660ms thread-0   http_plugin.cpp:141           plugin_initialize    ] host: localhost port: 8899
136661ms thread-0   http_plugin.cpp:144           plugin_initialize    ] configured http to listen on localhost:8899
136661ms thread-0   http_plugin.cpp:213           plugin_startup       ] start listening for http requests
136661ms thread-0   wallet_api_plugin.cpp:70      plugin_startup       ] starting wallet_api_plugin
136661ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/create
136661ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/get_public_keys
136661ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/import_key
136661ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/list_keys
136661ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/list_wallets
136661ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/lock
136661ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/lock_all
136661ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/open
136661ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/set_timeout
136661ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/sign_transaction
136661ms thread-0   http_plugin.cpp:242           add_handler          ] add api url: /v1/wallet/unlock
keosd重新启动,钱包被锁定且没有打开,由于keosd在端口8899开始监听,因此需要使用--wallet-port命令行参数。
打开,解锁默认钱包
[kingnet@bctestnode3 cleos]$ ./cleos --wallet-port=8899 wallet open
Opened: default
[kingnet@bctestnode3 cleos]$ ./cleos --wallet-port=8899 wallet list
Wallets:
[
  "default",
  "gaoyuan"
]
[kingnet@bctestnode3 cleos]$ ./cleos --wallet-port=8899 wallet unlock
password: Unlocked: default
[kingnet@bctestnode3 cleos]$ ./cleos --wallet-port=8899 wallet list
Wallets:
[
  "default *",
  "gaoyuan"
]
现在可以创建账户了,创建账户的命令是cleos create account,以下是命令参数结构:
$ cleos create account ${authorizing_account} ${new_account} ${owner_key} ${active_key}
1.authorizing_account : 为账户创建提供资金的账户名称
2.new_account : 想要创建的新账户名称
3.owner_key : 账户owner权限的公钥
4.active_key : 账户active权限的公钥
EOSIO授权账户:
在这里创建账户时,使用EOSIO账户作为授权账户,EOSIO账户是用于引导EOSIO节点的特殊账户,而使用该EOSIO账户在区块链上执行操作必须使用与该账户关联的密钥进行签名,EOSIO账户的密钥可以在config.ini配置文件中找到。
新账户名称:
新账户名称必须符合以下规则:
必须少于13个字符
只能包含以下符号.12345abcdefghijklmnopqrstuvwxyz
账户公钥:
使用在上面生成并导入到钱包的公钥,这些公钥将被分配到某个权限,分配到owner权限的公钥对应的私钥等同于账户的完全控制权,分配到active权限的公钥对应的私钥等同于账户中资金存取权。
[kingnet@bctestnode3 cleos]$ ./cleos --wallet-port=8899 wallet keys                   [[
    "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
    "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
  ],[
    "EOS6rfkVfqWXVdY71JHMj3bquv6sri5gd5aqgjiUU4EPnha96yWU8",
    "5Hpc9t34wXN3uE5Yi8ofMR7jNRJVVLQqpSYHxZ2QUq1purCP1UK"
  ],[
    "EOS75r6BFqZ6gwgsjQgJfdyGGVYqdNfaiUtjJtmRXXL4h4KhxvqkX",
    "5JVNBoNPMwePkinqh6ABBrhamqfVtopFaVZJBjQCCqsnL5MqkBi"
  ]
]
[kingnet@bctestnode3 cleos]$ ./cleos --wallet-port=8899 create account eosio myaccount2 EOS6rfkVfqWXVdY71JHMj3bquv6sri5gd5aqgjiUU4EPnha96yWU8 EOS75r6BFqZ6gwgsjQgJfdyGGVYqdNfaiUtjJtmRXXL4h4KhxvqkX
executed transaction: 49515e71f977ae273716dfdf58eb9bcd0688622f177a27b3edd0ee5fb114d630  352 bytes  102400 cycles
#         eosio <= eosio::newaccount            {"creator":"eosio","name":"myaccount2","owner":{"threshold":1,"keys":[{"key":"EOS6rfkVfqWXVdY71JHMj3...
如果在创建账户时出现以下错误:
2639714ms thread-0   main.cpp:1158                 main                 ] Failed with error: Assert Exception (10)
itr != structs.end(): Unknown struct 
Failed to serialize type
请先执行:

cleos set contract eosio build/contracts/eosio.bios -p eosio

十、账户相关操作命令

cleos commang

description

create account

创建一个新的账户

get account

获取一个账户

get code

获取一个账户的code和ABI

get accounts

获取和一个公钥关联的账户

get servants

获取一个账户的仆人账户

get transactions

获取一个特定账户名称的所有交易

set contract

在账户上创建或者更新智能合约

set account

更新账户状态

transfer

在一个账户上进行转账

猜你喜欢

转载自blog.csdn.net/bedrock_stable/article/details/80425504