Skip to content

Commit

Permalink
Merge branch 'feature/iroha-cli-tests' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
muratovv committed Aug 2, 2017
2 parents 9b2d68e + d7b4f7c commit 17337fc
Show file tree
Hide file tree
Showing 14 changed files with 427 additions and 188 deletions.
64 changes: 40 additions & 24 deletions iroha-cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,55 @@ set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
# GenesisBlockClient
add_library(genesis_block_client genesis_block_client_impl.cpp)
target_link_libraries(genesis_block_client
endpoint
optional
model
)
endpoint
optional
model
)

# BootstrapNetwork
add_library(bootstrap_network bootstrap_network.cpp)
target_link_libraries(bootstrap_network
genesis_block_client
rapidjson
model
ametsuchi
ip_tools
)
genesis_block_client
rapidjson
model
ametsuchi
ip_tools
)

# Gflags config validators
add_library(cli-flags_validators validators.cpp)
target_link_libraries(cli-flags_validators gflags)

add_library(keys_manager impl/keys_manager_impl.cpp)
target_link_libraries(keys_manager
optional
crypto
)

add_library(client
client.cpp
)
target_link_libraries(client
model
crypto
optional
rapidjson
command_client
ametsuchi
)

# IrohaCli
add_executable(iroha-cli
main.cpp
validators.cpp
client.cpp
)
main.cpp
validators.cpp
)
target_link_libraries(iroha-cli
crypto
cli-flags_validators
bootstrap_network
rapidjson
logger
ametsuchi
model
optional
command_client
)
client
cli-flags_validators
bootstrap_network
rapidjson
logger
ametsuchi
model
keys_manager
)
65 changes: 15 additions & 50 deletions iroha-cli/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,69 +15,34 @@
* limitations under the License.
*/

// In iroha-cli only, " is used.
#include <utility>
#include "client.hpp"

#include <grpc++/channel.h>
#include <grpc++/client_context.h>
#include <grpc++/create_channel.h>
#include <grpc++/security/credentials.h>
#include <grpc/grpc.h>

#include <endpoint.grpc.pb.h>
#include <endpoint.pb.h>

#include "ametsuchi/block_serializer.hpp"
#include "model/converters/pb_transaction_factory.hpp"
#include "model/model_hash_provider_impl.hpp"

namespace iroha_cli {

CliClient::CliClient(std::string target_ip, int port, std::string name)
: client_(target_ip, port) {
std::ifstream priv_file(name + ".priv");
priv_file >> client_priv_key_;
priv_file.close();
CliClient::CliClient(std::string target_ip, int port)
: client_(std::move(target_ip), port) {}

std::ifstream pub_file(name + ".pub");
pub_file >> client_pub_key_;
pub_file.close();
}

std::string CliClient::sendTx(std::string json_tx) {
CliClient::Status CliClient::sendTx(std::string json_tx) {
iroha::ametsuchi::BlockSerializer serializer;
auto tx_opt = serializer.deserialize(json_tx);
auto tx_opt = serializer.deserialize(std::move(json_tx));
if (not tx_opt.has_value()) {
return "Wrong transaction format";
return WRONG_FORMAT;
}
iroha::model::converters::PbTransactionFactory factory;
auto model_tx = tx_opt.value();
iroha::model::HashProviderImpl hashProvider;
auto tx_hash = hashProvider.get_hash(model_tx);
auto pubkey = iroha::hex2bytes(client_pub_key_);
auto privkey = iroha::hex2bytes(client_priv_key_);

iroha::ed25519::pubkey_t ed_pubkey;
std::copy(pubkey.begin(), pubkey.end(), ed_pubkey.begin());

iroha::ed25519::privkey_t ed_privkey;
std::copy(privkey.begin(), privkey.end(), ed_privkey.begin());
iroha::model::Signature sign;
sign.pubkey = ed_pubkey;
sign.signature = iroha::sign(tx_hash.data(), tx_hash.size(),
ed_pubkey,
ed_privkey);
model_tx.signatures = {sign};

// Convert to protobuf
iroha::model::converters::PbTransactionFactory factory;
auto pb_tx = factory.serialize(model_tx);
// Send to iroha:
iroha::protocol::ToriiResponse response;
auto stat = client_.Torii(pb_tx, response);
if (response.validation() == iroha::protocol::STATELESS_VALIDATION_SUCCESS){
std::cout << "Stateless validation success" << std::endl;
} else{
std::cout << "Stateless validation error" << std::endl;
}
return "";

return response.validation() ==
iroha::protocol::STATELESS_VALIDATION_SUCCESS
? OK
: NOT_VALID;
}

};
}; // namespace iroha_cli
14 changes: 5 additions & 9 deletions iroha-cli/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,26 @@
#ifndef IROHA_CLIENT_HPP
#define IROHA_CLIENT_HPP

#include <model/transaction.hpp>
#include <string>
#include "ametsuchi/block_serializer.hpp"
#include "torii/command_client.hpp"
#include <fstream>
#include "common/types.hpp"

namespace iroha_cli {

class CliClient {
public:
explicit CliClient(std::string target_ip, int port, std::string account_name);
enum Status { WRONG_FORMAT, NOT_VALID, OK };

CliClient(std::string target_ip, int port);
/**
* Send transaction to Iroha-Network
* @param json_tx
* @return
*/
std::string sendTx(std::string json_tx);
Status sendTx(std::string json_tx);

private:
torii::CommandSyncClient client_;
std::string client_priv_key_;
std::string client_pub_key_;
};
};
} // namespace iroha_cli

#endif // IROHA_CLIENT_CPP_HPP
71 changes: 71 additions & 0 deletions iroha-cli/impl/keys_manager_impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* 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 <utility>
#include "keys_manager_impl.hpp"
#include <fstream>
#include "common/types.hpp"

namespace iroha_cli {

KeysManagerImpl::KeysManagerImpl(std::string account_name)
: account_name_(std::move(account_name)) {}

nonstd::optional<iroha::ed25519::keypair_t> KeysManagerImpl::loadKeys() {
// Try to load from local file
std::ifstream priv_file(account_name_ + ".priv");
std::ifstream pub_file(account_name_ + ".pub");
if (not priv_file || not pub_file) {
return nonstd::nullopt;
}
std::string client_pub_key_;
std::string client_priv_key_;
priv_file >> client_priv_key_;
pub_file >> client_pub_key_;

iroha::ed25519::keypair_t keypair;
auto privkey = iroha::hex2bytes(client_priv_key_);
std::copy(privkey.begin(), privkey.end(), keypair.privkey.begin());
auto pubkey = iroha::hex2bytes(client_pub_key_);
std::copy(pubkey.begin(), pubkey.end(), keypair.pubkey.begin());
return keypair;
}

bool KeysManagerImpl::createKeys(std::string pass_phrase) {
auto seed = iroha::create_seed(pass_phrase);
auto key_pairs = iroha::create_keypair(seed);
std::ifstream pb_file(account_name_ + ".pub");
std::ifstream pr_file(account_name_ + ".priv");
if (pb_file && pr_file) {
return false;
}
// Save pubkey to file
std::ofstream pub_file(account_name_ + ".pub");
if (not pub_file) {
return false;
}
pub_file << key_pairs.pubkey.to_hexstring();
// Save privkey to file
std::ofstream priv_file(account_name_ + ".priv");
if (not priv_file) {
return false;
}
priv_file << key_pairs.privkey.to_hexstring();
return true;
}

} // namespace iroha_cli
35 changes: 35 additions & 0 deletions iroha-cli/impl/keys_manager_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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.
*/

#ifndef IROHA_CLI_KEYS_MANAGER_IMPL_HPP
#define IROHA_CLI_KEYS_MANAGER_IMPL_HPP
#include "../keys_manager.hpp"

namespace iroha_cli {
class KeysManagerImpl : public KeysManager {
public:
explicit KeysManagerImpl(std::string account_name);

nonstd::optional<iroha::ed25519::keypair_t> loadKeys() override;

bool createKeys(std::string pass_phrase) override;

private:
std::string account_name_;
};
} // namespace iroha_cli
#endif // IROHA_CLI_KEYS_MANAGER_IMPL_HPP
46 changes: 46 additions & 0 deletions iroha-cli/keys_manager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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.
*/

#ifndef IROHA_CLI_KEYS_MANAGER_HPP
#define IROHA_CLI_KEYS_MANAGER_HPP

#include <nonstd/optional.hpp>
#include "crypto/crypto.hpp"

namespace iroha_cli {

class KeysManager {
public:
/**
* Load keys associated with account
* @param account_name
* @return nullopt if no keypair found locally
*/
virtual nonstd::optional<iroha::ed25519::keypair_t> loadKeys() = 0;

/**
* Create keys and associate with account
* @param account_name
* @param pass_phrase
* @return false if create account failed
*/
virtual bool createKeys(std::string pass_phrase) = 0;

};

} // namepsace iroha_cli
#endif // IROHA_CLI_KEYS_MANAGER_HPP
Loading

0 comments on commit 17337fc

Please sign in to comment.