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/app/rdb/Wallet.h>
23 #include <ripple/core/Config.h>
24 #include <ripple/core/ConfigSections.h>
25 #include <boost/optional.hpp>
26 
27 namespace ripple {
28 
31  Application& app,
32  boost::program_options::variables_map const& cmdline)
33 {
35 
36  if (cmdline.count("nodeid"))
37  {
38  seed = parseGenericSeed(cmdline["nodeid"].as<std::string>(), false);
39 
40  if (!seed)
41  Throw<std::runtime_error>("Invalid 'nodeid' in command line");
42  }
43  else if (app.config().exists(SECTION_NODE_SEED))
44  {
45  seed = parseBase58<Seed>(
46  app.config().section(SECTION_NODE_SEED).lines().front());
47 
48  if (!seed)
49  Throw<std::runtime_error>("Invalid [" SECTION_NODE_SEED
50  "] in configuration file");
51  }
52 
53  if (seed)
54  {
55  auto secretKey = generateSecretKey(KeyType::secp256k1, *seed);
56  auto publicKey = derivePublicKey(KeyType::secp256k1, secretKey);
57 
58  return {publicKey, secretKey};
59  }
60 
61  auto db = app.getWalletDB().checkoutDb();
62 
63  if (cmdline.count("newnodeid") != 0)
64  clearNodeIdentity(*db);
65 
66  return getNodeIdentity(*db);
67 }
68 
69 } // namespace ripple
ripple::Application
Definition: Application.h:115
std::pair
std::vector::front
T front(T... args)
ripple::Application::getWalletDB
virtual DatabaseCon & getWalletDB()=0
Retrieve the "wallet database".
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:313
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:68
ripple::generateSecretKey
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
Definition: SecretKey.cpp:291
ripple::parseGenericSeed
std::optional< Seed > parseGenericSeed(std::string const &str, bool rfc1751)
Attempt to parse a string as a seed.
Definition: Seed.cpp:90
ripple::KeyType::secp256k1
@ secp256k1
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::getNodeIdentity
std::pair< PublicKey, SecretKey > getNodeIdentity(Application &app, boost::program_options::variables_map const &cmdline)
The cryptographic credentials identifying this server instance.
Definition: NodeIdentity.cpp:30
std::optional
ripple::clearNodeIdentity
void clearNodeIdentity(soci::session &session)
Delete any saved public/private key associated with this node.
Definition: Wallet.cpp:123
ripple::BasicConfig::exists
bool exists(std::string const &name) const
Returns true if a section with the given name exists.
Definition: BasicConfig.cpp:121
ripple::BasicConfig::section
Section & section(std::string const &name)
Returns the section with the given name.
Definition: BasicConfig.cpp:127