From b52f9aa4865c2bdfc627ca563a69e9de5c5f0230 Mon Sep 17 00:00:00 2001 From: Andrei Lebedev Date: Tue, 20 Jun 2017 16:55:29 +0300 Subject: [PATCH] iroha-cli account creation (#372) * Add gflags * iroha-cli creates pub/priv keys --- CMakeLists.txt | 1 + cmake/dependencies.cmake | 34 +++++++++++-- iroha-cli/CMakeLists.txt | 5 ++ iroha-cli/main.cpp | 79 +++++++++++++++++++++++++++++++ test/module/vendor/CMakeLists.txt | 1 - 5 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 iroha-cli/CMakeLists.txt create mode 100644 iroha-cli/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 07db9864df..4fc697c9e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ include(cmake/functions.cmake) add_subdirectory(schema) add_subdirectory(libs) add_subdirectory(irohad) +add_subdirectory(iroha-cli) link_directories(${PROJECT_BINARY_DIR}/lib) diff --git a/cmake/dependencies.cmake b/cmake/dependencies.cmake index 6bc93561ef..4a8077d2ce 100644 --- a/cmake/dependencies.cmake +++ b/cmake/dependencies.cmake @@ -308,8 +308,8 @@ file(MAKE_DIRECTORY ${optional_INCLUDE_DIRS}) add_library(optional INTERFACE IMPORTED) set_target_properties(optional PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${optional_INCLUDE_DIRS} - ) + INTERFACE_INCLUDE_DIRECTORIES ${optional_INCLUDE_DIRS} + ) add_dependencies(optional martinmoene_optional) @@ -331,8 +331,8 @@ file(MAKE_DIRECTORY ${any_INCLUDE_DIRS}) add_library(any INTERFACE IMPORTED) set_target_properties(any PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${any_INCLUDE_DIRS} - ) + INTERFACE_INCLUDE_DIRECTORIES ${any_INCLUDE_DIRS} + ) add_dependencies(any martinmoene_any) @@ -449,4 +449,28 @@ if (NOT PQxx_FOUND) ) add_dependencies(pqxx jtv_libpqxx) -endif () \ No newline at end of file +endif () + +################################ +# gflags # +################################ +externalproject_add(gflags_gflags + URL "https://github.com/gflags/gflags/archive/v2.2.0.tar.gz" + INSTALL_COMMAND "" # remove install step + TEST_COMMAND "" # remove test step + UPDATE_COMMAND "" # remove update step + ) +externalproject_get_property(gflags_gflags binary_dir) +set(gflags_INCLUDE_DIR ${binary_dir}/include) +set(gflags_LIBRARY ${binary_dir}/lib/libgflags.a) + +add_library(gflags STATIC IMPORTED) +file(MAKE_DIRECTORY ${gflags_INCLUDE_DIR}) + +set_target_properties(gflags PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${gflags_INCLUDE_DIR} + IMPORTED_LOCATION ${gflags_LIBRARY} + IMPORTED_LINK_INTERFACE_LIBRARIES "pthread" + ) +add_dependencies(gflags gflags_gflags) + diff --git a/iroha-cli/CMakeLists.txt b/iroha-cli/CMakeLists.txt new file mode 100644 index 0000000000..7c3be67f54 --- /dev/null +++ b/iroha-cli/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.0) +PROJECT(iroha C CXX) + +add_executable(cli-main main.cpp) +target_link_libraries(cli-main gflags ed25519) \ No newline at end of file diff --git a/iroha-cli/main.cpp b/iroha-cli/main.cpp new file mode 100644 index 0000000000..dd30bf2bc7 --- /dev/null +++ b/iroha-cli/main.cpp @@ -0,0 +1,79 @@ +/** + * Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved. + * http://soramitsu.co.jp + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +DEFINE_bool(new_account, false, "Choose if account does not exist"); +DEFINE_string(name, "", "Name of the account"); + +static bool not_empty_name(const char* flagname, const std::string& value) { + return value[0] != '\0'; +} +DEFINE_validator(name, ¬_empty_name); + +void create_account(std::string name); + +int main(int argc, char* argv[]) { + gflags::ParseCommandLineFlags(&argc, &argv, true); + + gflags::ShutDownCommandLineFlags(); + + if (FLAGS_new_account) { + if (std::ifstream(FLAGS_name + ".pub")) { + std::cout << "File already exists" << std::endl; + return -1; + } + create_account(FLAGS_name); + } +} + +std::string hex_str(unsigned char* data, int len) { + constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + std::string s((unsigned long)(len * 2), ' '); + for (int i = 0; i < len; ++i) { + s[2 * i] = hexmap[(data[i] & 0xF0) >> 4]; + s[2 * i + 1] = hexmap[data[i] & 0x0F]; + } + return s; +} + +void create_account(std::string name) { + unsigned char public_key[32], private_key[64], seed[32]; + + ed25519_create_keypair(public_key, private_key, seed); + auto pub_hex = hex_str(public_key, 32); + + auto priv_hex = hex_str(private_key, 64); + + // Save pubkey to file + std::ofstream pub_file(name + ".pub"); + pub_file << pub_hex; + pub_file.close(); + + // Save privkey to file + std::ofstream priv_file(name + ".priv"); + priv_file << priv_hex; + priv_file.close(); + + std::cout << "Public and private key has been generated in current directory" + << std::endl; +} \ No newline at end of file diff --git a/test/module/vendor/CMakeLists.txt b/test/module/vendor/CMakeLists.txt index efb1b45469..b68d20299a 100644 --- a/test/module/vendor/CMakeLists.txt +++ b/test/module/vendor/CMakeLists.txt @@ -29,6 +29,5 @@ AddTest(uvw_test uvw_test.cpp) target_link_libraries(uvw_test - uv uvw ) \ No newline at end of file