Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
3 : * http://soramitsu.co.jp
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #include "bindings/model_crypto.hpp"
19 : #include "common/byteutils.hpp"
20 : #include "cryptography/crypto_provider/crypto_defaults.hpp"
21 : #include "generator/generator.hpp"
22 :
23 : namespace shared_model {
24 : namespace bindings {
25 : crypto::Keypair ModelCrypto::generateKeypair() {
26 0 : return crypto::CryptoProviderEd25519Sha3::generateKeypair();
27 : }
28 :
29 : crypto::Keypair ModelCrypto::fromPrivateKey(
30 : const std::string &private_key) {
31 6 : if (private_key.size()
32 6 : != crypto::DefaultCryptoAlgorithmType::kPrivateKeyLength) {
33 4 : throw std::invalid_argument("input string has incorrect length "
34 3 : + std::to_string(private_key.length()));
35 : }
36 3 : auto byte_string = iroha::hexstringToBytestring(private_key);
37 3 : if (not byte_string) {
38 1 : throw std::invalid_argument("invalid seed");
39 : }
40 2 : return crypto::CryptoProviderEd25519Sha3::generateKeypair(
41 2 : crypto::Seed(*byte_string));
42 3 : }
43 :
44 : crypto::Keypair ModelCrypto::convertFromExisting(
45 : const std::string &public_key, const std::string &private_key) {
46 0 : crypto::Keypair keypair((crypto::Keypair::PublicKeyType(
47 0 : crypto::Blob::fromHexString(public_key))),
48 0 : crypto::Keypair::PrivateKeyType(
49 0 : crypto::Blob::fromHexString(private_key)));
50 :
51 0 : auto rand_str = generator::randomString(32);
52 0 : if (not crypto::CryptoProviderEd25519Sha3::verify(
53 0 : crypto::CryptoProviderEd25519Sha3::sign(crypto::Blob(rand_str),
54 : keypair),
55 0 : crypto::Blob(rand_str),
56 0 : keypair.publicKey())) {
57 0 : throw std::invalid_argument("Provided keypair is not correct");
58 : }
59 :
60 0 : return keypair;
61 0 : }
62 : } // namespace bindings
63 : } // namespace shared_model
|