Skip to content

Commit

Permalink
sign converted statement
Browse files Browse the repository at this point in the history
Signed-off-by: turuslan <turuslan.devbox@gmail.com>
  • Loading branch information
turuslan committed Sep 29, 2022
1 parent 610d964 commit 5bb5b35
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 12 deletions.
20 changes: 20 additions & 0 deletions core/crypto/blake2/blake2b_256.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef KAGOME_CRYPTO_BLAKE2_BLAKE2B_256_HPP
#define KAGOME_CRYPTO_BLAKE2_BLAKE2B_256_HPP

#include "common/blob.hpp"
#include "crypto/blake2/blake2b.h"

namespace kagome::crypto {
inline common::Hash256 blake2b_256(gsl::span<const uint8_t> input) {
common::Hash256 out;
blake2b(out.data(), out.size(), nullptr, 0, input.data(), input.size());
return out;
}
} // namespace kagome::crypto

#endif // KAGOME_CRYPTO_BLAKE2_BLAKE2B_256_HPP
5 changes: 2 additions & 3 deletions core/crypto/hasher/hasher_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <gsl/span>

#include "crypto/blake2/blake2b.h"
#include "crypto/blake2/blake2b_256.hpp"
#include "crypto/blake2/blake2s.h"
#include "crypto/keccak/keccak.h"
#include "crypto/sha/sha256.hpp"
Expand Down Expand Up @@ -44,9 +45,7 @@ namespace kagome::crypto {
}

Hash256 HasherImpl::blake2b_256(gsl::span<const uint8_t> buffer) const {
Hash256 out;
blake2b(out.data(), 32, nullptr, 0, buffer.data(), buffer.size());
return out;
return crypto::blake2b_256(buffer);
}

Hash512 HasherImpl::blake2b_512(gsl::span<const uint8_t> buffer) const {
Expand Down
18 changes: 18 additions & 0 deletions core/network/types/collator_messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "common/blob.hpp"
#include "consensus/grandpa/common.hpp"
#include "crypto/blake2/blake2b_256.hpp"
#include "primitives/common.hpp"
#include "primitives/compact_integer.hpp"
#include "primitives/digest.hpp"
Expand Down Expand Up @@ -246,6 +247,23 @@ namespace kagome::network {
ViewUpdate /// view update message
>;

inline CandidateHash candidateHash(const CandidateReceipt &receipt) {
return crypto::blake2b_256(scale::encode(receipt).value());
}

inline CandidateHash candidateHash(const CommittedCandidateReceipt &receipt) {
return candidateHash(CandidateReceipt{
receipt.descriptor,
crypto::blake2b_256(scale::encode(receipt.commitments).value()),
});
}

inline CandidateHash candidateHash(const CandidateState &statement) {
if (auto receipt = boost::get<CommittedCandidateReceipt>(&statement)) {
return candidateHash(*receipt);
}
return boost::get<CandidateHash>(statement);
}
} // namespace kagome::network

#endif // KAGOME_COLLATOR_DECLARE_HPP
1 change: 1 addition & 0 deletions core/parachain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_library(validator_parachain
)

target_link_libraries(validator_parachain
blake2
crypto_store
req_collation_protocol
collation_protocol
Expand Down
2 changes: 1 addition & 1 deletion core/parachain/availability/bitfield/signer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace kagome::parachain {
}

outcome::result<void> BitfieldSigner::sign(const ValidatorSigner &signer) {
auto &relay_parent = signer.context().relay_parent;
auto &relay_parent = signer.relayParent();
scale::BitVec bitfield;
OUTCOME_TRY(cores, parachain_api_->availability_cores(relay_parent));
bitfield.bits.reserve(cores.size());
Expand Down
4 changes: 2 additions & 2 deletions core/parachain/validator/signer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ namespace kagome::parachain {
return validator_index_;
}

const SigningContext &ValidatorSigner::context() const {
return context_;
const primitives::BlockHash &ValidatorSigner::relayParent() const {
return context_.relay_parent;
}

ValidatorSignerFactory::ValidatorSignerFactory(
Expand Down
23 changes: 17 additions & 6 deletions core/parachain/validator/signer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@

namespace kagome::parachain {
/// A type returned by runtime with current session index and a parent hash.
struct SigningContext {
class SigningContext {
private:
static auto &toSignable(const scale::BitVec &v) {
return v;
}
static auto toSignable(const network::CandidateState &v) {
constexpr std::array<uint8_t, 4> kMagic{'B', 'K', 'N', 'G'};
return std::make_tuple(
kMagic, static_cast<uint8_t>(v.which()), candidateHash(v));
}

public:
SCALE_TIE(2);

/// Make signing context for given block.
Expand All @@ -26,8 +37,8 @@ namespace kagome::parachain {
/// Make signable message for payload.
template <typename T>
auto signable(const T &payload) const {
// TODO(turuslan): Statement requires conversion
return scale::encode(std::tie(payload, *this));
auto &&signable = toSignable(payload);
return scale::encode(std::tie(signable, *this)).value();
}

/// Current session index.
Expand All @@ -49,7 +60,7 @@ namespace kagome::parachain {
/// Sign payload.
template <typename T>
outcome::result<network::Signed<T>> sign(T payload) const {
OUTCOME_TRY(data, context_.signable(payload));
auto data = context_.signable(payload);
OUTCOME_TRY(signature, sr25519_provider_->sign(*keypair_, data));
return network::Signed<T>{
std::move(payload),
Expand All @@ -61,8 +72,8 @@ namespace kagome::parachain {
/// Get validator index.
ValidatorIndex validatorIndex() const;

/// Get signing context.
const SigningContext &context() const;
/// Get relay parent hash.
const primitives::BlockHash &relayParent() const;

private:
ValidatorIndex validator_index_;
Expand Down

0 comments on commit 5bb5b35

Please sign in to comment.