Eos源码阅读(cleos子命令初始化和create key命令
2019-02-14 本文已影响0人
坠叶飘香
cleos命令.png
eos/programs/cleos/main.cpp
1.main()里面,先初始化一个顶层的App
CLI::App app{"Command Line Interface to EOSIO Client"};
2.然后依次构建子命令App,然后添加到总App的subcommands_(vector)成员中.
auto version = app.add_subcommand("version", localized("Retrieve version information"), false);
auto create = app.add_subcommand("create", localized("Create various items, on and off the blockchain"), false);
……
auto system = app.add_subcommand("system", localized("Send eosio.system contract action to the blockchain."), false);
3.针对子命令的子命令又做同样的操作
这里列举create key
auto create_key = create->add_subcommand("key", localized("Create a new keypair and print the public and private keys"))->set_callback( [&r1, &key_file, &print_console](){
if (key_file.empty() && !print_console) { //如果没有选择写入到文件或者控制台,会执行失败
std::cerr << "ERROR: Either indicate a file using \"--file\" or pass \"--to-console\"" << std::endl;
return;
}
auto pk = r1 ? private_key_type::generate_r1() : private_key_type::generate();
auto privs = string(pk); //私钥
auto pubs = string(pk.get_public_key()); //公钥
if (print_console) { //打印到控制台
std::cout << localized("Private key: ${key}", ("key", privs) ) << std::endl;
std::cout << localized("Public key: ${key}", ("key", pubs ) ) << std::endl;
} else {
std::cerr << localized("saving keys to ${filename}", ("filename", key_file)) << std::endl;
std::ofstream out( key_file.c_str() ); //写入到文件
out << localized("Private key: ${key}", ("key", privs) ) << std::endl;
out << localized("Public key: ${key}", ("key", pubs ) ) << std::endl;
}
});
create_key->add_flag( "--r1", r1, "Generate a key using the R1 curve (iPhone), instead of the K1 curve (Bitcoin)" );
create_key->add_option("-f,--file", key_file, localized("Name of file to write private/public key output to. (Must be set, unless \"--to-console\" is passed"));
create_key->add_flag( "--to-console", print_console, localized("Print private/public keys to console."));