Kagome
Polkadot Runtime Engine in C++17
bip39_provider_impl.cpp
Go to the documentation of this file.
1 
7 
10 
11 namespace kagome::crypto {
12 
14  std::shared_ptr<Pbkdf2Provider> pbkdf2_provider)
15  : pbkdf2_provider_(std::move(pbkdf2_provider)),
16  logger_{log::createLogger("Bip39Provider", "bip39")} {
18  }
19 
20  outcome::result<std::vector<uint8_t>> Bip39ProviderImpl::calculateEntropy(
21  const std::vector<std::string> &word_list) const {
22  // make entropy accumulator
23  OUTCOME_TRY(entropy_accumulator,
24  bip39::EntropyAccumulator::create(word_list.size()));
25 
26  // accumulate entropy
27  for (const auto &w : word_list) {
28  OUTCOME_TRY(entropy_token, dictionary_.findValue(w));
29  OUTCOME_TRY(entropy_accumulator.append(entropy_token));
30  }
31 
32  OUTCOME_TRY(mnemonic_checksum, entropy_accumulator.getChecksum());
33  OUTCOME_TRY(calculated_checksum, entropy_accumulator.calculateChecksum());
34 
35  if (mnemonic_checksum != calculated_checksum) {
37  }
38 
39  // finally get entropy
40  return entropy_accumulator.getEntropy();
41  }
42 
43  outcome::result<bip39::Bip39Seed> Bip39ProviderImpl::makeSeed(
44  gsl::span<const uint8_t> entropy, std::string_view password) const {
45  constexpr size_t iterations_count = 2048u;
46  constexpr auto default_salt = "mnemonic";
47 
48  common::Buffer salt{};
49  salt.put(default_salt);
50  salt.put(password);
51 
52  return pbkdf2_provider_->deriveKey(
53  entropy, salt, iterations_count, bip39::constants::BIP39_SEED_LEN_512);
54  }
55 
56  outcome::result<bip39::Bip39Seed> Bip39ProviderImpl::generateSeed(
57  std::string_view mnemonic_phrase) const {
58  OUTCOME_TRY(mnemonic, bip39::Mnemonic::parse(mnemonic_phrase));
59  OUTCOME_TRY(entropy, calculateEntropy(mnemonic.words));
60  return makeSeed(entropy, mnemonic.password);
61  }
62 } // namespace kagome::crypto
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
outcome::result< EntropyToken > findValue(std::string_view word) const
looks for word in dictionary
Definition: dictionary.cpp:22
Bip39ProviderImpl(std::shared_ptr< Pbkdf2Provider > pbkdf2_provider)
constexpr size_t BIP39_SEED_LEN_512
Definition: bip39_types.hpp:13
STL namespace.
outcome::result< bip39::Bip39Seed > makeSeed(gsl::span< const uint8_t > entropy, std::string_view password) const override
makes seed from entropy
static outcome::result< EntropyAccumulator > create(size_t words_count)
create class instance
outcome::result< bip39::Bip39Seed > generateSeed(std::string_view mnemonic_phrase) const override
SLBuffer & put(std::string_view view)
Put a string into byte buffer.
Definition: buffer.hpp:117
static outcome::result< Mnemonic > parse(std::string_view phrase)
parse mnemonic from phrase
Definition: mnemonic.cpp:34
std::shared_ptr< Pbkdf2Provider > pbkdf2_provider_
void initialize()
initializes dictionary
Definition: dictionary.cpp:14
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
outcome::result< std::vector< uint8_t > > calculateEntropy(const std::vector< std::string > &word_list) const override
calculates entropy from mnemonic