rippled
NodeIdentity.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/main/Application.h>
21 #include <ripple/app/main/NodeIdentity.h>
22 #include <ripple/basics/Log.h>
23 #include <ripple/core/Config.h>
24 #include <ripple/core/ConfigSections.h>
25 #include <ripple/core/DatabaseCon.h>
26 #include <boost/format.hpp>
27 #include <boost/optional.hpp>
28 
29 namespace ripple {
30 
33 {
34  // If a seed is specified in the configuration file use that directly.
35  if (app.config().exists(SECTION_NODE_SEED))
36  {
37  auto const seed = parseBase58<Seed>(
38  app.config().section(SECTION_NODE_SEED).lines().front());
39 
40  if (!seed)
41  Throw<std::runtime_error>("NodeIdentity: Bad [" SECTION_NODE_SEED
42  "] specified");
43 
44  auto secretKey = generateSecretKey(KeyType::secp256k1, *seed);
45  auto publicKey = derivePublicKey(KeyType::secp256k1, secretKey);
46 
47  return {publicKey, secretKey};
48  }
49 
50  // Try to load a node identity from the database:
51  boost::optional<PublicKey> publicKey;
52  boost::optional<SecretKey> secretKey;
53 
54  auto db = app.getWalletDB().checkoutDb();
55 
56  {
57  boost::optional<std::string> pubKO, priKO;
58  soci::statement st =
59  (db->prepare << "SELECT PublicKey, PrivateKey FROM NodeIdentity;",
60  soci::into(pubKO),
61  soci::into(priKO));
62  st.execute();
63  while (st.fetch())
64  {
65  auto const sk = parseBase58<SecretKey>(
66  TokenType::NodePrivate, priKO.value_or(""));
67  auto const pk = parseBase58<PublicKey>(
68  TokenType::NodePublic, pubKO.value_or(""));
69 
70  // Only use if the public and secret keys are a pair
71  if (sk && pk && (*pk == derivePublicKey(KeyType::secp256k1, *sk)))
72  {
73  secretKey = sk;
74  publicKey = pk;
75  }
76  }
77  }
78 
79  // If a valid identity wasn't found, we randomly generate a new one:
80  if (!publicKey || !secretKey)
81  {
82  std::tie(publicKey, secretKey) = randomKeyPair(KeyType::secp256k1);
83 
84  *db << str(
85  boost::format("INSERT INTO NodeIdentity (PublicKey,PrivateKey) "
86  "VALUES ('%s','%s');") %
87  toBase58(TokenType::NodePublic, *publicKey) %
88  toBase58(TokenType::NodePrivate, *secretKey));
89  }
90 
91  return {*publicKey, *secretKey};
92 }
93 
94 } // namespace ripple
ripple::Application
Definition: Application.h:101
ripple::loadNodeIdentity
std::pair< PublicKey, SecretKey > loadNodeIdentity(Application &app)
The cryptographic credentials identifying this server instance.
Definition: NodeIdentity.cpp:32
std::pair
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:29
std::vector::front
T front(T... args)
ripple::Application::getWalletDB
virtual DatabaseCon & getWalletDB()=0
Retrieve the "wallet database".
std::tie
T tie(T... args)
ripple::DatabaseCon::checkoutDb
LockedSociSession checkoutDb()
Definition: DatabaseCon.h:178
ripple::derivePublicKey
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
Definition: SecretKey.cpp:205
ripple::Application::config
virtual Config & config()=0
ripple::Section::lines
std::vector< std::string > const & lines() const
Returns all the lines in the section.
Definition: BasicConfig.h:67
ripple::generateSecretKey
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
Definition: SecretKey.cpp:179
ripple::KeyType::secp256k1
@ secp256k1
ripple::randomKeyPair
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
Definition: SecretKey.cpp:260
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::TokenType::NodePublic
@ NodePublic
ripple::TokenType::NodePrivate
@ NodePrivate
ripple::BasicConfig::exists
bool exists(std::string const &name) const
Returns true if a section with the given name exists.
Definition: BasicConfig.cpp:132
ripple::BasicConfig::section
Section & section(std::string const &name)
Returns the section with the given name.
Definition: BasicConfig.cpp:138