EOS环境搭建入门(私链节点-钱包-密钥-账号)
EOS环境搭建
## 1. 摘要 **【本文目标】** 通过本文实践,能在已编译的EOS V1.0.5版本环境上,完成私链节点启动,钱包创建,密钥导入和账号创建等内容。 **【前置条件】** 你已完成了EOS编译,编译测试成功。未完成的可参考 [《第4课 如何在UBUNTU虚拟机上编译EOS完成环境搭建?》](https://learnblockchain.cn/2018/06/29/55cd194eff7a) 完成相关配置。 **【技术收获】** 1)EOS的节点,钱包,密钥,账号的概念和理解 2)EOS钱包/账号的建立和遇到的问题分析及解决方法 **【说明】** EOS版本还没有稳定下来,即使完成了V1.0.2版本环境搭建的人,到V1.0.5时还是摔在了坑里。辉哥通过踩坑分析给大家提供尽可能多的知识和解决思路,大家在V1.0.5以后的版本部署可参考文章和以错误关键字搜索官网的 [issue](https://github.com/EOSIO/eos/issues) 网址获取更多知识。 ## 2. EOSIO总体框架 EOSIO有多个程序模块组成,经常会用到的有以下三个模块: * `nodeos` - EOSIO核心模块,用于启动eosio服务,在后台运行,启动时可以添加多种插件plugin。 * `cleos` - 命令行界面钱包工具,见 [《EOS命令行界面钱包》](https://github.com/eoshackathon/eos_dapp_development_cn/blob/master/docs/eos_command_line_wallet.md) ,位于 `eos/build/programs/cleos/cleos` * `keosd` - 管理钱包的各种组件,默认情况下, `keosd` 将随 `cleos` 一起启动。位于 `eos/build/programs/keosd` 下图是上面三个工具的关系:[](https://camo.githubusercontent.com/97d6aaae9a7b3e7cb3464710c927cf6ebf18d946/68747470733a2f2f66696c65732e726561646d652e696f2f386633316366642d42617369632d454f53494f2d53797374656d2d4172636869746563747572652e706e67) ![](https://img.learnblockchain.cn/2020/02/01_/993857894.jpeg) [](https://camo.githubusercontent.com/97d6aaae9a7b3e7cb3464710c927cf6ebf18d946/68747470733a2f2f66696c65732e726561646d652e696f2f386633316366642d42617369632d454f53494f2d53797374656d2d4172636869746563747572652e706e67) 另外,还有智能合约的编译工具 `eosiocpp` 。 ## 3. EOS系统中钱包,密钥对,账号,智能合约的关系 相对其他区块链公链,EOS系统的组合关系比较自由,也带来了理解上的困难。辉哥结合自己的理解,以中国大地上人们最关系的房子为例,做比喻介绍。 **1) 钱包是土豪房东** 土豪可以有很多的房子和各种开门的钥匙。上海这个城市有很多个土豪房东,所以一个节点可以创建多个钱包。 > cleos wallet create ${参数} ![](https://img.learnblockchain.cn/2020/02/01_/34783661.jpg) **2)钥匙用来打开房东门的。** 钥匙分为私钥和公钥。公钥是别人可以看到的,例如下面图片是土豪家的橙色钥匙包,是专门用来存放仁恒滨江的大平层房子钥匙的,私钥是钥匙包里面实际开门的钥匙。 你拿到钥匙包没有用的,你得拿到钥匙盒里面的私钥才可以打开土豪家房子大门。 土豪房东可以把很多的房子配成一样的锁,用一对公钥/私钥来开门,也可以不同的房子不同的钥匙。 > cleos wallet import ${参数} 私钥 ![](https://img.learnblockchain.cn/2020/02/01_/362203144.jpg) **3) 账户是房子** 房东可以给多个房子配一对钥匙(钥匙包和钥匙),也可以不同房子配不同钥匙。 另外,一个房子可以用2把钥匙打开,一把是owner钥匙对,一把是active钥匙对。 房子的owner钥匙地址表示为房东的公钥,表示主人产权归属,用它对应的私钥可以打开房子,这个钥匙包是房东连小姨子也不会给的; 房子的active钥匙对表示为房客的公钥,表示该用户的私钥可以打开房子,房子出租后,房东就把这个钥匙包给租客了; > cleos create account 节点 账户名 Owner的公钥 Active的公钥 ![](https://img.learnblockchain.cn/2020/02/01_/628216396.jpg) ## 4. 搭建实操 搭建实操的流程图如下。 ![](https://img.learnblockchain.cn/2020/02/01_/102005109.png) ### 4.1 启动私链 **1) 启动keosd** > keosd --http-server-address=127.0.0.1:8900 命令行界面钱包程序为 keosd,位于 eos/build/programs/keosd 路径下,用于存储交易签名的私钥。keosd在本地节点上运行,并将私钥保存在本地节点上。 默认情况下,keosd会在目录 ~/eosio-wallet 中生成一个基础的配置文件 config.ini。该配置文件中的wallet-dir指定了钱包文件存放目录。 另外,在运行命令行钱包时,可通过配置命令行参数 --config-dir指定config.ini配置文件的目录。该配置文件中保存用于接入http链接的服务器配置http-server-address参数,以及其他用于资源共享的配置参数。 默认情况下,keosd将钱包文件保存在 ~/eosio-wallet 目录下,钱包文件名为:.wallet。例如,默认钱包文件名为 default.wallet。当建立了其他钱包后,在该目录下会分别建立每个钱包文件,例如当建立了一个名称为"duncanwang"的钱包,会生成一个钱包文件duncanwang.wallet。钱包文件可以通过命令行参数--data-dir存放在指定的目录中。 **【预警】** 从V1.0.5开始,发现要先运行keosd后在运行nodeos才可以创建钱包。 成功输出结果如下: ``` duncanwang@duncanwang:~$ keosd --http-server-address=127.0.0.1:8900 3861ms thread-0 wallet_plugin.cpp:39 plugin_initialize ] initializing wallet plugin 3862ms thread-0 http_plugin.cpp:290 plugin_initialize ] configured http to listen on 127.0.0.1:8900 3863ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/keosd/stop 3863ms thread-0 http_plugin.cpp:331 plugin_startup ] start listening for http requests 3865ms thread-0 wallet_api_plugin.cpp:73 plugin_startup ] starting wallet_api_plugin 3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/create 3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/create_key 3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/get_public_keys 3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/import_key 3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/list_keys 3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/list_wallets 3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/lock 3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/lock_all 3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/open 3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/remove_key 3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/set_timeout 3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/sign_digest 3866ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/sign_transaction 3866ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/unlock 75115ms thread-0 wallet.cpp:223 save_wallet_file ] saving wallet to file /home/duncanwang/eosio-wallet/./duncanwang.wallet ``` **【问题】** 关机后重新启动NODEOS,会有以下错误提示。 ``` duncanwang@duncanwang:~/eos/build/programs/nodeos$ ./nodeos -e -p eosio --plugin eosio::wallet_plugin --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --replay-blockchain 1983994ms thread-0 wallet_plugin.cpp:39 plugin_initialize ] initializing wallet plugin 1983994ms thread-0 chain_plugin.cpp:208 plugin_initialize ] initializing chain plugin 1983996ms thread-0 chain_plugin.cpp:337 plugin_initialize ] Replay requested: deleting state database CHAINBASE: Failed to pin chainbase shared memory (of size 1024 MB) in RAM. Performance degradation is possible. 1984049ms thread-0 main.cpp:123 main ] database dirty flag set (likely due to unclean shutdown): replay required ``` **【解决方法】** 进入'~/.local/share/eosio/nodeos' 目录,删除data文件夹。 重新运行即可成功。 ``` duncanwang@duncanwang:~/eos/build/programs/nodeos$ cd ~/.local/share/eosio/nodeos duncanwang@duncanwang:~/.local/share/eosio/nodeos$ ls config data duncanwang@duncanwang:~/.local/share/eosio/nodeos$ rm -r -f data ``` **2) 启动私链** 在新的命令行窗口输入以下命令。 > cd ~/eos/build/programs/nodeos > ./nodeos -e -p eosio --plugin eosio::wallet_plugin --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --replay-blockchain **参数说明:** 1) -e enable-stale-production,开启后,即使链过时了,也能产生区块 2) -p eosio producer-name,生产者的名字,这里指定为eosio 3) --plugin eosio::wallet_plugin 在启动 `nodeos` 时,需要添加参数 `eosio::wallet_plugin` ,否则的话,每次节点重启,之前创建的钱包,账号都不会加载进来。 4)--plugin eosio::chain_api_plugin 5) --plugin eosio::history_api_plugin 记录执行过程。 7) --replay-blockchain `--replay-blockchain` 表示清除数据库内链的状态,重新运行,它会导致重新启动时先读取之前的区块进行加载。 **【注意】** 节点关闭后,钱包将会被加锁。重新启动 `nodeos` 后,需要使用 `open` 命令打开钱包,然后使用 `unlock` 命令解锁钱包。 例如: ``` duncanwang@duncanwang:~/eos$ cleos wallet unlock -n duncanwang password: Unlocked: duncanwang ``` 输入nodes -help可以看到所有参数的帮助说明。 ``` duncanwang@duncanwang:~/eos$ nodeos -help Application Options: Config Options for eosio::bnet_plugin: --bnet-endpoint arg (=0.0.0.0:4321) the endpoint upon which to listen for incoming connections --bnet-follow-irreversible arg (=0) this peer will request only irreversible blocks from other nodes --bnet-threads arg the number of threads to use to process network messages --bnet-connect arg remote endpoint of other node to connect to; Use multiple bnet-connect options as needed to compose a network --bnet-no-trx this peer will request no pending transactions from other nodes --bnet-peer-log-format arg (=["${_name}" ${_ip}:${_port}]) The string used to format peers when logging messages about them. Variables are escaped with ${<variable name>}. Available Variables: _name self-reported name _id self-reported ID (Public Key) _ip remote IP address of peer _port remote port number of peer _lip local IP address connected to peer _lport local port number connected to peer Config Options for eosio::chain_plugin: --blocks-dir arg (="blocks") the location of the blocks directory (absolute path or relative to application data dir) --checkpoint arg Pairs of [BLOCK_NUM,BLOCK_ID] that should be enforced as checkpoints. --wasm-runtime wavm/binaryen Override default WASM runtime --chain-state-db-size-mb arg (=1024) Maximum size (in MB) of the chain state database --reversible-blocks-db-size-mb arg (=340) Maximum size (in MB) of the reversible blocks database --contracts-console print contract's output to console --actor-whitelist arg Account added to actor whitelist (may specify multiple times) --actor-blacklist arg Account added to actor blacklist (may specify multiple times) --contract-whitelist arg Contract account added to contract whitelist (may specify multiple times) --contract-blacklist arg Contract account added to contract blacklist (may specify multiple times) --action-blacklist arg Action (in the form code::action) added to action blacklist (may specify multiple times) --key-blacklist arg Public key added to blacklist of keys that should not be included in authorities (may specify multiple times) Command Line Options for eosio::chain_plugin: --genesis-json arg File to read Genesis State from --genesis-timestamp arg override the initial timestamp in the Genesis State file --print-genesis-json extract genesis_state from blocks.log as JSON, print to console, and exit --extract-genesis-json arg extract genesis_state from blocks.log as JSON, write into specified file, and exit --fix-reversible-blocks recovers reversible block database if that database is in a bad state --force-all-checks do not skip any checks that can be skipped while replaying irreversible blocks --replay-blockchain clear chain state database and replay all blocks --hard-replay-blockchain clear chain state database, recover as many blocks as possible from the block log, and then replay those blocks --delete-all-blocks clear chain state database and block log --truncate-at-block arg (=0) stop hard replay / block log recovery at this block number (if set to non-zero number) Config Options for eosio::history_plugin: -f [ --filter-on ] arg Track actions which match receiver:action:actor. Actor may be blank to include all. Receiver and Action may not be blank. Config Options for eosio::http_client_plugin: --https-client-root-cert arg PEM encoded trusted root certificate (or path to file containing one) used to validate any TLS connections made. (may specify multiple times) --https-client-validate-peers arg (=1) true: validate that the peer certificates are valid and trusted, false: ignore cert errors Config Options for eosio::http_plugin: --http-server-address arg (=127.0.0.1:8888) The local IP and port to listen for incoming http connections; set blank to disable. --https-server-address arg The local IP and port to listen for incoming https connections; leave blank to disable. --https-certificate-chain-file arg Filename with the certificate chain to present on https connections. PEM format. Required for https. --https-private-key-file arg Filename with https private key in PEM format. Required for https --access-control-allow-origin arg Specify the Access-Control-Allow-Origin to be returned on each request. --access-control-allow-headers arg Specify the Access-Control-Allow-Header s to be returned on each request. --access-control-max-age arg Specify the Access-Control-Max-Age to be returned on each request. --access-control-allow-credentials Specify if Access-Control-Allow-Credent ials: true should be returned on each request. --max-body-size arg (=1048576) The maximum body size in bytes allowed for incoming RPC requests --verbose-http-errors Append the error log to HTTP responses Config Options for eosio::net_plugin: --p2p-listen-endpoint arg (=0.0.0.0:9876) The actual host:port used to listen for incoming p2p connections. --p2p-server-address arg An externally accessible host:port for identifying this node. Defaults to p2p-listen-endpoint. --p2p-peer-address arg The public endpoint of a peer node to connect to. Use multiple p2p-peer-address options as needed to compose a network. --p2p-max-nodes-per-host arg (=1) Maximum number of client0nodes from any single IP address --agent-name arg (="EOS Test Agent") The name supplied to identify this node amongst the peers. --allowed-connection arg (=any) Can be 'any' or 'producers' or 'specified' or 'none'. If 'specified', peer-key must be specified at least once. If only 'producers', peer-key is not required. 'producers' and 'specified' may be combined. --peer-key arg Optional public key of peer allowed to connect. May be used multiple times. --peer-private-key arg Tuple of [PublicKey, WIF private key] (may specify multiple times) --max-clients arg (=25) Maximum number of clients from which connections are accepted, use 0 for no limit --connection-cleanup-period arg (=30) number of seconds to wait before cleaning up dead connections --network-version-match arg (=0) True to require exact match of peer network version. --sync-fetch-span arg (=100) number of blocks to retrieve in a chunk from any individual peer during synchronization --max-implicit-request arg (=1500) maximum sizes of transaction or block messages that are sent without first sending a notice --use-socket-read-watermark arg (=0) Enable expirimental socket read watermark optimization --peer-log-format arg (=["${_name}" ${_ip}:${_port}]) The string used to format peers when logging messages about them. Variables are escaped with ${<variable name>}. Available Variables: _name self-reported name _id self-reported ID (64 hex characters) _sid first 8 characters of _peer.id _ip remote IP address of peer _port remote port number of peer _lip local IP address connected to peer _lport local port number connected to peer Config Options for eosio::producer_plugin: -e [ --enable-stale-production ] Enable block production, even if the chain is stale. -x [ --pause-on-startup ] Start this node in a state where production is paused --max-transaction-time arg (=30) Limits the maximum time (in milliseconds) that is allowed a pushed transaction's code to execute before being considered invalid --max-irreversible-block-age arg (=-1) Limits the maximum age (in seconds) of the DPOS Irreversible Block for a chain this node will produce blocks on (use negative value to indicate unlimited) -p [ --producer-name ] arg ID of producer controlled by this node (e.g. inita; may specify multiple times) --private-key arg (DEPRECATED - Use signature-provider instead) Tuple of [public key, WIF private key] (may specify multiple times) --signature-provider arg (=EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV=KEY:5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3) Key=Value pairs in the form <public-key>=<provider-spec> Where: <public-key> is a string form of a vaild EOSIO public key <provider-spec> is a string in the form <provider-type> :<data> <provider-type> is KEY, or KEOSD KEY:<data> is a string form of a valid EOSIO private key which maps to the provided public key KEOSD:<data> is the URL where keosd is available and the approptiate wallet(s) are unlocked --keosd-provider-timeout arg (=5) Limits the maximum time (in milliseconds) that is allowd for sending blocks to a keosd provider for signing Config Options for eosio::txn_test_gen_plugin: --txn-reference-block-lag arg (=0) Lag in number of blocks from the head block when selecting the reference block for transactions (-1 means Last Irreversible Block) Config Options for eosio::wallet_plugin: --wallet-dir arg (=".") The path of the wallet files (absolute path or relative to application data dir) --unlock-timeout arg (=900) Timeout for unlocked wallet in seconds (default 900 (15 minutes)). Wallets will automatically lock after specified number of seconds of inactivity. Activity is defined as any wallet command e.g. list-wallets. Application Config Options: --plugin arg Plugin(s) to enable, may be specified multiple times Application Command Line Options: -h [ --help ] Print this help message and exit. -v [ --version ] Print version information. --print-default-config Print default configuration template -d [ --data-dir ] arg Directory containing program runtime data --config-dir arg Directory containing configuration files such as config.ini -c [ --config ] arg (=config.ini) Configuration file name relative to config-dir -l [ --logconf ] arg (=logging.json) Logging configuration file name/path for library users ``` ### 4.2 创建钱包 > cleos wallet create -n duncanwang 创建 `duncanwang` 钱包成功,输出结果如下: ``` duncanwang@duncanwang:~/eos$ cleos wallet create -n duncanwang Creating wallet: duncanwang Save password to use in the future to unlock this wallet. Without password imported keys will not be retrievable. "PW5JMZdES2Cds5LsPRUBRo2THEXpbFSM17Xmcd2XWG7XBd49wveTo" ``` **【结果确认】** ``` duncanwang@duncanwang:~$ cleos wallet list Wallets: [ "duncanwang *" ] ``` `duanwang` 钱包已存在了,*表示该账号已解锁。 **【问题1】 创建钱包重名,但是cleos wallet list看不到** ``` duncanwang@duncanwang:~$ cleos wallet create -n duncanwang Error 3120001: Wallet already exists Try to use different wallet name. ``` **【解决方法】** 这个主要是运行的命令中没有导入钱包 `--plugin eosio::wallet_plugin` 这个参数,导致已创建钱包未加载,但是目录下存在已创建的钱包文件。 > ./nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --replay-blockchain ~/eosio-wallet 目录下删除文件duncanwang.wallet即可重新创建。 **【问题2】重启节点后,cleos wallet list发现已创建的钱包等不存在。** ``` duncanwang@duncanwang:~$ cleos wallet list Wallets: [ ] ``` **【解决方法】** 运行./nodeos命令时要带参数--plugin eosio::wallet_plugin **【解决方法2】** 后来发现这种方法有时也是不可行,cleos wallet list没有看到钱包。 采用命令'cleos wallet open -n duncanwang'打开钱包后,就正常加载进来了。 **【问题3】创建钱包时提示keosd已运行,并且无法连接** ``` duncanwang@duncanwang:~$ cleos wallet create -n duncanwang "/usr/local/bin/keosd" launched Unable to connect to keosd, if keosd is running please kill the process and try again. ``` **【解决方法】** 需要输入以下命令把keosd重启下。 > ps -ef | grep keosd > kill -9 pid > keosd --http-server-address=127.0.0.1:8900 操作实例如下: ``` duncanwang@duncanwang:~$ ps -ef | grep keosd duncanw+ 2439 1 0 07:14 pts/1 00:00:00 /usr/local/bin/keosd --http-server-address=::1:8900 duncanw+ 2441 2389 0 07:16 pts/1 00:00:00 grep --color=auto keosd duncanwang@duncanwang:~$ kill -9 2439 ``` ### 4.3 钱包导入系统账号私钥 **1) 找到eosio的系统账号的默认公钥/私钥对** 找到配置文件,例如以下地址,~/.local/share/eosio/nodeosconfig 的 config.ini,默认的私钥/公钥是一样的。 ``` #signature-provider = EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV=KEY:5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3 ``` **2),钱包导入系统账号私钥** > cleos wallet import 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3 -n duncanwang ``` duncanwang@duncanwang:~/eos$ cleos wallet import 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3 -n duncanwang imported private key for: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV ``` **3),查看系统账号信息** 以下命令可以查看eosio系统账号的密钥和资源占用情况。 > cleos get account eosio ``` duncanwang@duncanwang:~/eos/build$ cleos get account eosio privileged: true permissions: owner 1: 1 EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV active 1: 1 EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV memory: quota: unlimited used: 60.75 KiB net bandwidth: used: unlimited available: unlimited limit: unlimited cpu bandwidth: used: unlimited available: unlimited limit: unlimited ``` ### 4.4 加载Bios合约 现在我们有一个钱包,并且加载了eosio帐户的密钥,我们可以设置一个默认的系统合约。为了开发的目的,可以使用默认的eosio.bios合约。通过此合约,您可以直接控制其他帐户的资源分配,并调用其他特权API。在公开区块链中,这个系统合约将管理其他账户的 token 抵押和解抵押操作,以为合约执行预留CPU、网络活动带宽,以及预留内存。 eosio.bios合约可以在你的EOSIO源代码文件夹中找到:contracts/eosio.bios。下面的命令序列,都假定是在EOSIO源代码的根目录执行。但是您可以通过指定完整路径,从任意位置执行这个命令:${EOSIO_SOURCE}/build/contracts/eosio.bios。 这个命令序列的结果是,cleos发起一个包含两个操作(actions)的交易(transaction):eosio::setcode和eosio::setabi。 代码定义了合约如何运行,abi描述了参数如何在二进制和json表示之间进行转换。虽然abi在技术上是可选的,但为了便于使用,所有的EOSIO工具都依赖于它。 输入命令: > cleos set contract eosio build/contracts/eosio.bios -p eosio 输出结果,表示本地执行成功。 ``` duncanwang@duncanwang:~/eos$ cleos set contract eosio build/contracts/eosio.bios -p eosio Reading WAST/WASM from build/contracts/eosio.bios/eosio.bios.wasm... Using already assembled WASM... Publishing contract... executed transaction: f4c1cc4e953710645a4849eb41cf92d9d3881c756b227323a3ade221e3807fbb 3720 bytes 12685 us ## eosio <= eosio::setcode {"account":"eosio","vmtype":0,"vmversion":0,"code":"0061736d0100000001621260037f7e7f0060057f7e7e7e7e... ## eosio <= eosio::setabi {"account":"eosio","abi":"0e656f73696f3a3a6162692f312e30050c6163636f756e745f6e616d65046e616d650f7065... warning: transaction executed locally, but may not be confirmed by the network yet ``` ### 4.5 创建并导入新的密钥对 **1)创建新的密钥对** > cleos create key 创建密钥对的输出结果。 ``` duncanwang@duncanwang:~/eos$ cleos create key Private key: 5JZQmnt6ZtEzUADswgKgBwMp9qAwTSNM9JFHPRFu1FjrLjj49g7 Public key: EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1 ``` **2) 导入新的私钥** > cleos wallet import 5JZQmnt6ZtEzUADswgKgBwMp9qAwTSNM9JFHPRFu1FjrLjj49g7 -n duncanwang 输出结果如下: ``` duncanwang@duncanwang:~/eos$ cleos wallet import 5JZQmnt6ZtEzUADswgKgBwMp9qAwTSNM9JFHPRFu1FjrLjj49g7 -n duncanwang imported private key for: EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1 ``` **3)查看确认密钥对是否导入成功 ** > cleos wallet keys 用于查看钱包的密钥对情况。 【输出结果】 ``` duncanwang@duncanwang:~/eos$ cleos wallet keys [ "EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1", "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV" ] ``` ### 4.6 创建新账号 创建账号的命令: cleos create account eosio {new_account} ownerkey {active_key} 其中eosio是超级用户,需要靠超级用户来创建其它的新用户,eosio后面就是你的新用户的用户名。 除了新的账号之外,命令后面还有两个key: 1、Owner key 2、Active key Owner key是什么意思呢?Owner key表示分配给新账号的一个Owner认证的公钥。Active key是分配给新账号一个Active认证的一个公钥。 至于这两个认证,我后面会给详细介绍,这是两个主要的权限。我创建一个账号,如果这个账号要有Owner的权限和Active的权限,就必须要用这两个key才能实现。 我们来总结一下刚才的操作,我们刚才操作是调用cleos create account创建了一个账号,这个账号的命名规则遵守下边两个规则: 1、小于13个字符; 2、仅包含这些字符:.12345abcdefghijklmnopqrstuvwxyz 另外,刚才给大家说到Owner key和Active key的概念。Owner key的概念就是你账号的所有控制权限,你只要有了Owner key,你可以对这个账号的任何东西做任何的事儿,这是它的所有控制权。 而Active key只掌握了你的账号资金的访问权限,也就是你如果有了Active这个权限的话,你可以对这个账号的资金进行转移,但是你不能转移这个账号的所有权,或者不能做超过这个Active权限其它的权利。 如果简单的理解,Owner key就是对这个账号的最高权限,Active只是用来转移资金而已。这也是与以太坊智能合约开发的一个区别,以太坊账号的权限其实没有这么细分,它就只有一个账号,我只要有这个账号的公钥和私钥,我就可以做任何事情。 具体操作如下。 > cleos create account eosio wangdenghui1 EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1 EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1 【错误现象】 ``` duncanwang@duncanwang:~/eos$ cleos create account eosio wangdenghui1 EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1 EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1 Error 3050000: action exception ``` 【解决方案-查询账号】 查询发现该账号已存在,之前辉哥创建过,忘记了。 ``` duncanwang@duncanwang:~/eos$ cleos get accounts EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1 { "account_names": [ "wangdenghui1" ] } ``` > cleos create account eosio boss EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1 EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1 创建成功输出结果: ``` duncanwang@duncanwang:~/eos$ cleos create account eosio boss EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1 EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1 executed transaction: cb6801fe82816f94b447cbfb903ae8e9477f5c99920322d679a9c8c04347e536 200 bytes 367 us ## eosio <= eosio::newaccount {"creator":"eosio","name":"boss","owner":{"threshold":1,"keys":[{"key":"EOS6EHAzvrpQ4wo1BPcAk86X6aGD... warning: transaction executed locally, but may not be confirmed by the network yet ``` ### 4.6 总结 至此,基于私链环境搭建和准备工作已经完毕,接下来就可以在上面运行"Hello World"智能合约了。 ## 5. 参考 1) [eoshackathon/eos_dapp_development_cn 古千峰Github](https://github.com/eoshackathon/eos_dapp_development_cn) 2) [github官网](https://github.com/EOSIO/eos/wiki) 3) [本地环境](https://github.com/EOSIO/eos/wiki/Local-Environment) *EOS圈古千峰大侠担任本篇的武术指导,深表感谢。*
1. 摘要
【本文目标】 通过本文实践,能在已编译的EOS V1.0.5版本环境上,完成私链节点启动,钱包创建,密钥导入和账号创建等内容。 【前置条件】 你已完成了EOS编译,编译测试成功。未完成的可参考 《第4课 如何在UBUNTU虚拟机上编译EOS完成环境搭建?》 完成相关配置。 【技术收获】 1)EOS的节点,钱包,密钥,账号的概念和理解 2)EOS钱包/账号的建立和遇到的问题分析及解决方法 【说明】 EOS版本还没有稳定下来,即使完成了V1.0.2版本环境搭建的人,到V1.0.5时还是摔在了坑里。辉哥通过踩坑分析给大家提供尽可能多的知识和解决思路,大家在V1.0.5以后的版本部署可参考文章和以错误关键字搜索官网的 issue 网址获取更多知识。
2. EOSIO总体框架
EOSIO有多个程序模块组成,经常会用到的有以下三个模块:
nodeos
- EOSIO核心模块,用于启动eosio服务,在后台运行,启动时可以添加多种插件plugin。cleos
- 命令行界面钱包工具,见 《EOS命令行界面钱包》 ,位于eos/build/programs/cleos/cleos
keosd
- 管理钱包的各种组件,默认情况下,keosd
将随cleos
一起启动。位于eos/build/programs/keosd
下图是上面三个工具的关系:
另外,还有智能合约的编译工具 eosiocpp
。
3. EOS系统中钱包,密钥对,账号,智能合约的关系
相对其他区块链公链,EOS系统的组合关系比较自由,也带来了理解上的困难。辉哥结合自己的理解,以中国大地上人们最关系的房子为例,做比喻介绍。 1) 钱包是土豪房东 土豪可以有很多的房子和各种开门的钥匙。上海这个城市有很多个土豪房东,所以一个节点可以创建多个钱包。
cleos wallet create ${参数}
2)钥匙用来打开房东门的。 钥匙分为私钥和公钥。公钥是别人可以看到的,例如下面图片是土豪家的橙色钥匙包,是专门用来存放仁恒滨江的大平层房子钥匙的,私钥是钥匙包里面实际开门的钥匙。 你拿到钥匙包没有用的,你得拿到钥匙盒里面的私钥才可以打开土豪家房子大门。 土豪房东可以把很多的房子配成一样的锁,用一对公钥/私钥来开门,也可以不同的房子不同的钥匙。
cleos wallet import ${参数} 私钥
3) 账户是房子 房东可以给多个房子配一对钥匙(钥匙包和钥匙),也可以不同房子配不同钥匙。 另外,一个房子可以用2把钥匙打开,一把是owner钥匙对,一把是active钥匙对。 房子的owner钥匙地址表示为房东的公钥,表示主人产权归属,用它对应的私钥可以打开房子,这个钥匙包是房东连小姨子也不会给的; 房子的active钥匙对表示为房客的公钥,表示该用户的私钥可以打开房子,房子出租后,房东就把这个钥匙包给租客了;
cleos create account 节点 账户名 Owner的公钥 Active的公钥
4. 搭建实操
搭建实操的流程图如下。
4.1 启动私链
1) 启动keosd
keosd --http-server-address=127.0.0.1:8900
命令行界面钱包程序为 keosd,位于 eos/build/programs/keosd 路径下,用于存储交易签名的私钥。keosd在本地节点上运行,并将私钥保存在本地节点上。
默认情况下,keosd会在目录 ~/eosio-wallet 中生成一个基础的配置文件 config.ini。该配置文件中的wallet-dir指定了钱包文件存放目录。
另外,在运行命令行钱包时,可通过配置命令行参数 --config-dir指定config.ini配置文件的目录。该配置文件中保存用于接入http链接的服务器配置http-server-address参数,以及其他用于资源共享的配置参数。
默认情况下,keosd将钱包文件保存在 ~/eosio-wallet 目录下,钱包文件名为:.wallet。例如,默认钱包文件名为 default.wallet。当建立了其他钱包后,在该目录下会分别建立每个钱包文件,例如当建立了一个名称为"duncanwang"的钱包,会生成一个钱包文件duncanwang.wallet。钱包文件可以通过命令行参数--data-dir存放在指定的目录中。
【预警】 从V1.0.5开始,发现要先运行keosd后在运行nodeos才可以创建钱包。
成功输出结果如下:
duncanwang@duncanwang:~$ keosd --http-server-address=127.0.0.1:8900
3861ms thread-0 wallet_plugin.cpp:39 plugin_initialize ] initializing wallet plugin
3862ms thread-0 http_plugin.cpp:290 plugin_initialize ] configured http to listen on 127.0.0.1:8900
3863ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/keosd/stop
3863ms thread-0 http_plugin.cpp:331 plugin_startup ] start listening for http requests
3865ms thread-0 wallet_api_plugin.cpp:73 plugin_startup ] starting wallet_api_plugin
3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/create
3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/create_key
3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/get_public_keys
3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/import_key
3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/list_keys
3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/list_wallets
3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/lock
3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/lock_all
3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/open
3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/remove_key
3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/set_timeout
3865ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/sign_digest
3866ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/sign_transaction
3866ms thread-0 http_plugin.cpp:377 add_handler ] add api url: /v1/wallet/unlock
75115ms thread-0 wallet.cpp:223 save_wallet_file ] saving wallet to file /home/duncanwang/eosio-wallet/./duncanwang.wallet
【问题】 关机后重新启动NODEOS,会有以下错误提示。
duncanwang@duncanwang:~/eos/build/programs/nodeos$ ./nodeos -e -p eosio --plugin eosio::wallet_plugin --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --replay-blockchain
1983994ms thread-0 wallet_plugin.cpp:39 plugin_initialize ] initializing wallet plugin
1983994ms thread-0 chain_plugin.cpp:208 plugin_initialize ] initializing chain plugin
1983996ms thread-0 chain_plugin.cpp:337 plugin_initialize ] Replay requested: deleting state database
CHAINBASE: Failed to pin chainbase shared memory (of size 1024 MB) in RAM. Performance degradation is possible.
1984049ms thread-0 main.cpp:123 main ] database dirty flag set (likely due to unclean shutdown): replay required
【解决方法】 进入'~/.local/share/eosio/nodeos' 目录,删除data文件夹。 重新运行即可成功。
duncanwang@duncanwang:~/eos/build/programs/nodeos$ cd ~/.local/share/eosio/nodeos
duncanwang@duncanwang:~/.local/share/eosio/nodeos$ ls
config data
duncanwang@duncanwang:~/.local/share/eosio/nodeos$ rm -r -f data
2) 启动私链 在新的命令行窗口输入以下命令。
cd ~/eos/build/programs/nodeos ./nodeos -e -p eosio --plugin eosio::wallet_plugin --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --replay-blockchain
参数说明: 1) -e enable-stale-production,开启后,即使链过时了,也能产生区块 2) -p eosio producer-name,生产者的名字,这里指定为eosio 3) --plugin eosio::wallet_plugin 在启动 nodeos
时,需要添加参数 eosio::wallet_plugin
,否则的话,每次节点重启,之前创建的钱包,账号都不会加载进来。 4)--plugin eosio::chain_api_plugin
5) --plugin eosio::history_api_plugin 记录执行过程。 7) --replay-blockchain --replay-blockchain
表示清除数据库内链的状态,重新运行,它会导致重新启动时先读取之前的区块进行加载。 【注意】 节点关闭后,钱包将会被加锁。重新启动 nodeos
后,需要使用 open
命令打开钱包,然后使用 unlock
命令解锁钱包。 例如:
duncanwang@duncanwang:~/eos$ cleos wallet unlock -n duncanwang
password: Unlocked: duncanwang
输入nodes -help可以看到所有参数的帮助说明。
duncanwang@duncanwang:~/eos$ nodeos -help
Application Options:
Config Options for eosio::bnet_plugin:
--bnet-endpoint arg (=0.0.0.0:4321) the endpoint upon which to listen for
incoming connections
--bnet-follow-irreversible arg (=0) this peer will request only
irreversible blocks from other nodes
--bnet-threads arg the number of threads to use to process
network messages
--bnet-connect arg remote endpoint of other node to
connect to; Use multiple bnet-connect
options as needed to compose a network
--bnet-no-trx this peer will request no pending
transactions from other nodes
--bnet-peer-log-format arg (=["${_name}" ${_ip}:${_port}])
The string used to format peers when
logging messages about them. Variables
are escaped with ${<variable name>}.
Available Variables:
_name self-reported name
_id self-reported ID (Public Key)
_ip remote IP address of peer
_port remote port number of peer
_lip local IP address connected to
peer
_lport local port number connected
to peer
Config Options for eosio::chain_plugin:
--blocks-dir arg (="blocks") the location of the blocks directory
(absolute path or relative to
application data dir)
--checkpoint arg Pairs of [BLOCK_NUM,BLOCK_ID] that
should be enforced as checkpoints.
--wasm-runtime wavm/binaryen Override default WASM runtime
--chain-state-db-size-mb arg (=1024) Maximum size (in MB) of the chain state
database
--reversible-blocks-db-size-mb arg (=340)
Maximum size (in MB) of the reversible
blocks database
--contracts-console print contract's output to console
--actor-whitelist arg Account added to actor whitelist (may
specify multiple times)
--actor-blacklist arg Account added to actor blacklist (may
specify multiple times)
--contract-whitelist arg Contract account added to contract
whitelist (may specify multiple times)
--contract-blacklist arg Contract account added to contract
blacklist (may specify multiple times)
--action-blacklist arg Action (in the form code::action) added
to action blacklist (may specify
multiple times)
--key-blacklist arg Public key added to blacklist of keys
that should not be included in
authorities (may specify multiple
times)
Command Line Options for eosio::chain_plugin:
--genesis-json arg File to read Genesis State from
--genesis-timestamp arg override the initial timestamp in the
Genesis State file
--print-genesis-json extract genesis_state from blocks.log
as JSON, print to console, and exit
--extract-genesis-json arg extract genesis_state from blocks.log
as JSON, write into specified file, and
exit
--fix-reversible-blocks recovers reversible block database if
that database is in a bad state
--force-all-checks do not skip any checks that can be
skipped while replaying irreversible
blocks
--replay-blockchain clear chain state database and replay
all blocks
--hard-replay-blockchain clear chain state database, recover as
many blocks as possible from the block
log, and then replay those blocks
--delete-all-blocks clear chain state database and block
log
--truncate-at-block arg (=0) stop hard replay / block log recovery
at this block number (if set to
non-zero number)
Config Options for eosio::history_plugin:
-f [ --filter-on ] arg Track actions which match
receiver:action:actor. Actor may be
blank to include all. Receiver and
Action may not be blank.
Config Options for eosio::http_client_plugin:
--https-client-root-cert arg PEM encoded trusted root certificate
(or path to file containing one) used
to validate any TLS connections made.
(may specify multiple times)
--https-client-validate-peers arg (=1)
true: validate that the peer
certificates are valid and trusted,
false: ignore cert errors
Config Options for eosio::http_plugin:
--http-server-address arg (=127.0.0.1:8888)
The local IP and port to listen for
incoming http connections; set blank to
disable.
--https-server-address arg The local IP and port to listen for
incoming https connections; leave blank
to disable.
--https-certificate-chain-file arg Filename with the certificate chain to
present on https connections. PEM
format. Required for https.
--https-private-key-file arg Filename with https private key in PEM
format. Required for https
--access-control-allow-origin arg Specify the Access-Control-Allow-Origin
to be returned on each request.
--access-control-allow-headers arg Specify the Access-Control-Allow-Header
s to be returned on each request.
--access-control-max-age arg Specify the Access-Control-Max-Age to
be returned on each request.
--access-control-allow-credentials Specify if Access-Control-Allow-Credent
ials: true should be returned on each
request.
--max-body-size arg (=1048576) The maximum body size in bytes allowed
for incoming RPC requests
--verbose-http-errors Append the error log to HTTP responses
Config Options for eosio::net_plugin:
--p2p-listen-endpoint arg (=0.0.0.0:9876)
The actual host:port used to listen for
incoming p2p connections.
--p2p-server-address arg An externally accessible host:port for
identifying this node. Defaults to
p2p-listen-endpoint.
--p2p-peer-address arg The public endpoint of a peer node to
connect to. Use multiple
p2p-peer-address options as needed to
compose a network.
--p2p-max-nodes-per-host arg (=1) Maximum number of client0nodes from any
single IP address
--agent-name arg (="EOS Test Agent") The name supplied to identify this node
amongst the peers.
--allowed-connection arg (=any) Can be 'any' or 'producers' or
'specified' or 'none'. If 'specified',
peer-key must be specified at least
once. If only 'producers', peer-key is
not required. 'producers' and
'specified' may be combined.
--peer-key arg Optional public key of peer allowed to
connect. May be used multiple times.
--peer-private-key arg Tuple of [PublicKey, WIF private key]
(may specify multiple times)
--max-clients arg (=25) Maximum number of clients from which
connections are accepted, use 0 for no
limit
--connection-cleanup-period arg (=30) number of seconds to wait before
cleaning up dead connections
--network-version-match arg (=0) True to require exact match of peer
network version.
--sync-fetch-span arg (=100) number of blocks to retrieve in a chunk
from any individual peer during
synchronization
--max-implicit-request arg (=1500) maximum sizes of transaction or block
messages that are sent without first
sending a notice
--use-socket-read-watermark arg (=0) Enable expirimental socket read
watermark optimization
--peer-log-format arg (=["${_name}" ${_ip}:${_port}])
The string used to format peers when
logging messages about them. Variables
are escaped with ${<variable name>}.
Available Variables:
_name self-reported name
_id self-reported ID (64 hex
characters)
_sid first 8 characters of
_peer.id
_ip remote IP address of peer
_port remote port number of peer
_lip local IP address connected to
peer
_lport local port number connected
to peer
Config Options for eosio::producer_plugin:
-e [ --enable-stale-production ] Enable block production, even if the
chain is stale.
-x [ --pause-on-startup ] Start this node in a state where
production is paused
--max-transaction-time arg (=30) Limits the maximum time (in
milliseconds) that is allowed a pushed
transaction's code to execute before
being considered invalid
--max-irreversible-block-age arg (=-1)
Limits the maximum age (in seconds) of
the DPOS Irreversible Block for a chain
this node will produce blocks on (use
negative value to indicate unlimited)
-p [ --producer-name ] arg ID of producer controlled by this node
(e.g. inita; may specify multiple
times)
--private-key arg (DEPRECATED - Use signature-provider
instead) Tuple of [public key, WIF
private key] (may specify multiple
times)
--signature-provider arg (=EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV=KEY:5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3)
Key=Value pairs in the form
<public-key>=<provider-spec>
Where:
<public-key> is a string form of
a vaild EOSIO public
key
<provider-spec> is a string in the
form <provider-type>
:<data>
<provider-type> is KEY, or KEOSD
KEY:<data> is a string form of
a valid EOSIO
private key which
maps to the provided
public key
KEOSD:<data> is the URL where
keosd is available
and the approptiate
wallet(s) are
unlocked
--keosd-provider-timeout arg (=5) Limits the maximum time (in
milliseconds) that is allowd for
sending blocks to a keosd provider for
signing
Config Options for eosio::txn_test_gen_plugin:
--txn-reference-block-lag arg (=0) Lag in number of blocks from the head
block when selecting the reference
block for transactions (-1 means Last
Irreversible Block)
Config Options for eosio::wallet_plugin:
--wallet-dir arg (=".") The path of the wallet files (absolute
path or relative to application data
dir)
--unlock-timeout arg (=900) Timeout for unlocked wallet in seconds
(default 900 (15 minutes)). Wallets
will automatically lock after specified
number of seconds of inactivity.
Activity is defined as any wallet
command e.g. list-wallets.
Application Config Options:
--plugin arg Plugin(s) to enable, may be specified
multiple times
Application Command Line Options:
-h [ --help ] Print this help message and exit.
-v [ --version ] Print version information.
--print-default-config Print default configuration template
-d [ --data-dir ] arg Directory containing program runtime
data
--config-dir arg Directory containing configuration
files such as config.ini
-c [ --config ] arg (=config.ini) Configuration file name relative to
config-dir
-l [ --logconf ] arg (=logging.json) Logging configuration file name/path
for library users
4.2 创建钱包
cleos wallet create -n duncanwang
创建 duncanwang
钱包成功,输出结果如下:
duncanwang@duncanwang:~/eos$ cleos wallet create -n duncanwang
Creating wallet: duncanwang
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5JMZdES2Cds5LsPRUBRo2THEXpbFSM17Xmcd2XWG7XBd49wveTo"
【结果确认】
duncanwang@duncanwang:~$ cleos wallet list
Wallets:
[
"duncanwang *"
]
duanwang
钱包已存在了,*表示该账号已解锁。
【问题1】 创建钱包重名,但是cleos wallet list看不到
duncanwang@duncanwang:~$ cleos wallet create -n duncanwang
Error 3120001: Wallet already exists
Try to use different wallet name.
【解决方法】 这个主要是运行的命令中没有导入钱包 --plugin eosio::wallet_plugin
这个参数,导致已创建钱包未加载,但是目录下存在已创建的钱包文件。
./nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --replay-blockchain
~/eosio-wallet 目录下删除文件duncanwang.wallet即可重新创建。
【问题2】重启节点后,cleos wallet list发现已创建的钱包等不存在。
duncanwang@duncanwang:~$ cleos wallet list
Wallets:
[
]
【解决方法】 运行./nodeos命令时要带参数--plugin eosio::wallet_plugin 【解决方法2】 后来发现这种方法有时也是不可行,cleos wallet list没有看到钱包。 采用命令'cleos wallet open -n duncanwang'打开钱包后,就正常加载进来了。
【问题3】创建钱包时提示keosd已运行,并且无法连接
duncanwang@duncanwang:~$ cleos wallet create -n duncanwang
"/usr/local/bin/keosd" launched
Unable to connect to keosd, if keosd is running please kill the process and try again.
【解决方法】 需要输入以下命令把keosd重启下。
ps -ef | grep keosd kill -9 pid keosd --http-server-address=127.0.0.1:8900
操作实例如下:
duncanwang@duncanwang:~$ ps -ef | grep keosd
duncanw+ 2439 1 0 07:14 pts/1 00:00:00 /usr/local/bin/keosd --http-server-address=::1:8900
duncanw+ 2441 2389 0 07:16 pts/1 00:00:00 grep --color=auto keosd
duncanwang@duncanwang:~$ kill -9 2439
4.3 钱包导入系统账号私钥
1) 找到eosio的系统账号的默认公钥/私钥对 找到配置文件,例如以下地址,~/.local/share/eosio/nodeosconfig 的 config.ini,默认的私钥/公钥是一样的。
#signature-provider = EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV=KEY:5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
2),钱包导入系统账号私钥
cleos wallet import 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3 -n duncanwang
duncanwang@duncanwang:~/eos$ cleos wallet import 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3 -n duncanwang
imported private key for: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
3),查看系统账号信息 以下命令可以查看eosio系统账号的密钥和资源占用情况。
cleos get account eosio
duncanwang@duncanwang:~/eos/build$ cleos get account eosio
privileged: true
permissions:
owner 1: 1 EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
active 1: 1 EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
memory:
quota: unlimited used: 60.75 KiB
net bandwidth:
used: unlimited
available: unlimited
limit: unlimited
cpu bandwidth:
used: unlimited
available: unlimited
limit: unlimited
4.4 加载Bios合约
现在我们有一个钱包,并且加载了eosio帐户的密钥,我们可以设置一个默认的系统合约。为了开发的目的,可以使用默认的eosio.bios合约。通过此合约,您可以直接控制其他帐户的资源分配,并调用其他特权API。在公开区块链中,这个系统合约将管理其他账户的 token 抵押和解抵押操作,以为合约执行预留CPU、网络活动带宽,以及预留内存。 eosio.bios合约可以在你的EOSIO源代码文件夹中找到:contracts/eosio.bios。下面的命令序列,都假定是在EOSIO源代码的根目录执行。但是您可以通过指定完整路径,从任意位置执行这个命令:${EOSIO_SOURCE}/build/contracts/eosio.bios。
这个命令序列的结果是,cleos发起一个包含两个操作(actions)的交易(transaction):eosio::setcode和eosio::setabi。
代码定义了合约如何运行,abi描述了参数如何在二进制和json表示之间进行转换。虽然abi在技术上是可选的,但为了便于使用,所有的EOSIO工具都依赖于它。
输入命令:
cleos set contract eosio build/contracts/eosio.bios -p eosio
输出结果,表示本地执行成功。
duncanwang@duncanwang:~/eos$ cleos set contract eosio build/contracts/eosio.bios -p eosio
Reading WAST/WASM from build/contracts/eosio.bios/eosio.bios.wasm...
Using already assembled WASM...
Publishing contract...
executed transaction: f4c1cc4e953710645a4849eb41cf92d9d3881c756b227323a3ade221e3807fbb 3720 bytes 12685 us
## eosio <= eosio::setcode {"account":"eosio","vmtype":0,"vmversion":0,"code":"0061736d0100000001621260037f7e7f0060057f7e7e7e7e...
## eosio <= eosio::setabi {"account":"eosio","abi":"0e656f73696f3a3a6162692f312e30050c6163636f756e745f6e616d65046e616d650f7065...
warning: transaction executed locally, but may not be confirmed by the network yet
4.5 创建并导入新的密钥对
1)创建新的密钥对
cleos create key
创建密钥对的输出结果。
duncanwang@duncanwang:~/eos$ cleos create key
Private key: 5JZQmnt6ZtEzUADswgKgBwMp9qAwTSNM9JFHPRFu1FjrLjj49g7
Public key: EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1
2) 导入新的私钥
cleos wallet import 5JZQmnt6ZtEzUADswgKgBwMp9qAwTSNM9JFHPRFu1FjrLjj49g7 -n duncanwang
输出结果如下:
duncanwang@duncanwang:~/eos$ cleos wallet import 5JZQmnt6ZtEzUADswgKgBwMp9qAwTSNM9JFHPRFu1FjrLjj49g7 -n duncanwang
imported private key for: EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1
3)查看确认密钥对是否导入成功
cleos wallet keys
用于查看钱包的密钥对情况。 【输出结果】
duncanwang@duncanwang:~/eos$ cleos wallet keys
[
"EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1",
"EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
]
4.6 创建新账号
创建账号的命令: cleos create account eosio {new_account} ownerkey {active_key}
其中eosio是超级用户,需要靠超级用户来创建其它的新用户,eosio后面就是你的新用户的用户名。
除了新的账号之外,命令后面还有两个key: 1、Owner key 2、Active key
Owner key是什么意思呢?Owner key表示分配给新账号的一个Owner认证的公钥。Active key是分配给新账号一个Active认证的一个公钥。
至于这两个认证,我后面会给详细介绍,这是两个主要的权限。我创建一个账号,如果这个账号要有Owner的权限和Active的权限,就必须要用这两个key才能实现。 我们来总结一下刚才的操作,我们刚才操作是调用cleos create account创建了一个账号,这个账号的命名规则遵守下边两个规则: 1、小于13个字符; 2、仅包含这些字符:.12345abcdefghijklmnopqrstuvwxyz 另外,刚才给大家说到Owner key和Active key的概念。Owner key的概念就是你账号的所有控制权限,你只要有了Owner key,你可以对这个账号的任何东西做任何的事儿,这是它的所有控制权。 而Active key只掌握了你的账号资金的访问权限,也就是你如果有了Active这个权限的话,你可以对这个账号的资金进行转移,但是你不能转移这个账号的所有权,或者不能做超过这个Active权限其它的权利。 如果简单的理解,Owner key就是对这个账号的最高权限,Active只是用来转移资金而已。这也是与以太坊智能合约开发的一个区别,以太坊账号的权限其实没有这么细分,它就只有一个账号,我只要有这个账号的公钥和私钥,我就可以做任何事情。 具体操作如下。
cleos create account eosio wangdenghui1 EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1 EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1
【错误现象】
duncanwang@duncanwang:~/eos$ cleos create account eosio wangdenghui1 EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1 EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1
Error 3050000: action exception
【解决方案-查询账号】 查询发现该账号已存在,之前辉哥创建过,忘记了。
duncanwang@duncanwang:~/eos$ cleos get accounts EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1
{
"account_names": [
"wangdenghui1"
]
}
cleos create account eosio boss EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1 EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1
创建成功输出结果:
duncanwang@duncanwang:~/eos$ cleos create account eosio boss EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1 EOS6EHAzvrpQ4wo1BPcAk86X6aGDARZgqTcAq1mJRF1SxEYgNGWN1
executed transaction: cb6801fe82816f94b447cbfb903ae8e9477f5c99920322d679a9c8c04347e536 200 bytes 367 us
## eosio <= eosio::newaccount {"creator":"eosio","name":"boss","owner":{"threshold":1,"keys":[{"key":"EOS6EHAzvrpQ4wo1BPcAk86X6aGD...
warning: transaction executed locally, but may not be confirmed by the network yet
4.6 总结
至此,基于私链环境搭建和准备工作已经完毕,接下来就可以在上面运行"Hello World"智能合约了。
5. 参考
1) eoshackathon/eos_dapp_development_cn 古千峰Github 2) github官网 3) 本地环境 EOS圈古千峰大侠担任本篇的武术指导,深表感谢。
- 发表于 2018-07-05 14:18
- 阅读 ( 2769 )
- 学分 ( 5 )
- 分类:EOS
评论