Skip to content

Commit

Permalink
Generate key pair from hex string seed in bindings (hyperledger-iroha…
Browse files Browse the repository at this point in the history
…#1043)

Signed-off-by: Moonraker <[email protected]>
  • Loading branch information
Solonets authored and x3medima17 committed Mar 30, 2018
1 parent 977c205 commit a85ee73
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
7 changes: 6 additions & 1 deletion shared_model/bindings/model_crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ namespace shared_model {
}

crypto::Keypair ModelCrypto::generateKeypair(const std::string &seed) {
auto byte_string = iroha::hexstringToBytestring(seed);
if (not byte_string.has_value())
{
throw std::runtime_error("invalid seed");
}
return crypto::CryptoProviderEd25519Sha3::generateKeypair(
crypto::Seed(seed));
crypto::Seed(*byte_string));
}

crypto::Keypair ModelCrypto::convertFromExisting(
Expand Down
1 change: 1 addition & 0 deletions shared_model/bindings/model_crypto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace shared_model {

/**
* Generates new keypair (ed25519) based on user-provided seed
* the seed should be 32 byte hex-encoded string
* @return generated keypair
*/
crypto::Keypair generateKeypair(const std::string &seed);
Expand Down
9 changes: 9 additions & 0 deletions test/module/shared_model/bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@
addtest(model_query_builder_test
model_query_builder_test.cpp
)

target_link_libraries(model_query_builder_test
bindings
)

addtest(model_crypto_test
model_crypto_test.cpp
)

target_link_libraries(model_crypto_test
bindings
)

if (SWIG_PYTHON OR SWIG_JAVA)
get_property(SWIG_BUILD_DIR GLOBAL PROPERTY SWIG_BUILD_DIR)
endif()
Expand Down
47 changes: 47 additions & 0 deletions test/module/shared_model/bindings/model_crypto_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright Soramitsu Co., Ltd. 2018 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 "bindings/model_crypto.hpp"

#include <gtest/gtest.h>

/**
* @given ModelCrypto module
* @when Receive 32 byte hex string
* @then assertion is not thrown on keypair generation
*/
TEST(ModelCryptoTest, GenerateKeypair) {
ASSERT_NO_THROW(shared_model::bindings::ModelCrypto().generateKeypair(
std::string(64, 'a')););
ASSERT_NO_THROW(shared_model::bindings::ModelCrypto().generateKeypair(
std::string(64, 'A')););
}
/**
* @given ModelCrypto module
* @when Receive invalid hex byte string
* @then assertion is thrown
*/
TEST(ModelCryptoTest, GenerateKeypairInvalidSeed) {
ASSERT_THROW(shared_model::bindings::ModelCrypto().generateKeypair(
std::string(64, 'g'));, std::runtime_error);
ASSERT_THROW(shared_model::bindings::ModelCrypto().generateKeypair(
std::string(63, 'a'));, std::runtime_error);
ASSERT_THROW(shared_model::bindings::ModelCrypto().generateKeypair(
std::string(65, 'a'));, std::runtime_error);
ASSERT_THROW(shared_model::bindings::ModelCrypto().generateKeypair(
std::string(32, 'a'));, std::invalid_argument);
}

0 comments on commit a85ee73

Please sign in to comment.