Skip to content

Commit

Permalink
iroha-cli account creation (hyperledger-iroha#372)
Browse files Browse the repository at this point in the history
* Add gflags

* iroha-cli creates pub/priv keys
  • Loading branch information
lebdron authored Jun 20, 2017
1 parent c1da013 commit b52f9aa
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 6 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
34 changes: 29 additions & 5 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -449,4 +449,28 @@ if (NOT PQxx_FOUND)
)

add_dependencies(pqxx jtv_libpqxx)
endif ()
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)

5 changes: 5 additions & 0 deletions iroha-cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
79 changes: 79 additions & 0 deletions iroha-cli/main.cpp
Original file line number Diff line number Diff line change
@@ -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 <ed25519.h>
#include <gflags/gflags.h>
#include <cstring>
#include <fstream>
#include <iostream>

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, &not_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;
}
1 change: 0 additions & 1 deletion test/module/vendor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@

AddTest(uvw_test uvw_test.cpp)
target_link_libraries(uvw_test
uv
uvw
)

0 comments on commit b52f9aa

Please sign in to comment.