LCOV - code coverage report
Current view: top level - irohad/consensus/yac/impl - yac_crypto_provider_impl.cpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 22 25 88.0 %
Date: 2018-12-05 17:11:35 Functions: 10 11 90.9 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : 
       6             : #include "consensus/yac/impl/yac_crypto_provider_impl.hpp"
       7             : 
       8             : #include "consensus/yac/transport/yac_pb_converters.hpp"
       9             : #include "cryptography/crypto_provider/crypto_signer.hpp"
      10             : #include "cryptography/crypto_provider/crypto_verifier.hpp"
      11             : 
      12             : namespace iroha {
      13             :   namespace consensus {
      14             :     namespace yac {
      15             :       CryptoProviderImpl::CryptoProviderImpl(
      16             :           const shared_model::crypto::Keypair &keypair,
      17             :           std::shared_ptr<shared_model::interface::CommonObjectsFactory>
      18             :               factory)
      19         248 :           : keypair_(keypair), factory_(std::move(factory)) {}
      20             : 
      21             :       bool CryptoProviderImpl::verify(const std::vector<VoteMessage> &msg) {
      22        1420 :         return std::all_of(
      23             :             std::begin(msg), std::end(msg), [](const auto &vote) {
      24             :               auto serialized =
      25        1422 :                   PbConverters::serializeVote(vote).hash().SerializeAsString();
      26        1422 :               auto blob = shared_model::crypto::Blob(serialized);
      27             : 
      28        1422 :               return shared_model::crypto::CryptoVerifier<>::verify(
      29        1422 :                   vote.signature->signedData(),
      30             :                   blob,
      31        1422 :                   vote.signature->publicKey());
      32        1422 :             });
      33             :       }
      34             : 
      35             :       VoteMessage CryptoProviderImpl::getVote(YacHash hash) {
      36         712 :         VoteMessage vote;
      37         712 :         vote.hash = hash;
      38             :         auto serialized =
      39         712 :             PbConverters::serializeVotePayload(vote).hash().SerializeAsString();
      40         712 :         auto blob = shared_model::crypto::Blob(serialized);
      41         712 :         const auto &pubkey = keypair_.publicKey();
      42         712 :         const auto &privkey = keypair_.privateKey();
      43         712 :         auto signature = shared_model::crypto::CryptoSigner<>::sign(
      44         712 :             blob, shared_model::crypto::Keypair(pubkey, privkey));
      45             : 
      46             :         // TODO 30.08.2018 andrei: IR-1670 Remove optional from YAC
      47             :         // CryptoProviderImpl::getVote
      48         712 :         factory_->createSignature(pubkey, signature)
      49         712 :             .match(
      50             :                 [&](iroha::expected::Value<
      51             :                     std::unique_ptr<shared_model::interface::Signature>> &sig) {
      52         712 :                   vote.signature = std::move(sig.value);
      53         712 :                 },
      54             :                 [](iroha::expected::Error<std::string> &reason) {
      55           0 :                   logger::log("YacCryptoProvider::getVote")
      56           0 :                       ->error("Cannot build vote signature: {}", reason.error);
      57           0 :                 });
      58             : 
      59         712 :         return vote;
      60         712 :       }
      61             : 
      62             :     }  // namespace yac
      63             :   }    // namespace consensus
      64             : }  // namespace iroha

Generated by: LCOV version 1.13