rippled
Loading...
Searching...
No Matches
ValidatorKeys.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 <xrpld/app/misc/ValidatorKeys.h>
21
22#include <xrpld/app/misc/Manifest.h>
23#include <xrpld/core/Config.h>
24#include <xrpld/core/ConfigSections.h>
25#include <xrpl/basics/Log.h>
26#include <xrpl/basics/base64.h>
27
28namespace ripple {
30{
31 if (config.exists(SECTION_VALIDATOR_TOKEN) &&
32 config.exists(SECTION_VALIDATION_SEED))
33 {
34 configInvalid_ = true;
35 JLOG(j.fatal()) << "Cannot specify both [" SECTION_VALIDATION_SEED
36 "] and [" SECTION_VALIDATOR_TOKEN "]";
37 return;
38 }
39
40 if (config.exists(SECTION_VALIDATOR_TOKEN))
41 {
42 // token is non-const so it can be moved from
43 if (auto token = loadValidatorToken(
44 config.section(SECTION_VALIDATOR_TOKEN).lines()))
45 {
46 auto const pk =
47 derivePublicKey(KeyType::secp256k1, token->validationSecret);
48 auto const m = deserializeManifest(base64_decode(token->manifest));
49
50 if (!m || pk != m->signingKey)
51 {
52 configInvalid_ = true;
53 JLOG(j.fatal())
54 << "Invalid token specified in [" SECTION_VALIDATOR_TOKEN
55 "]";
56 }
57 else
58 {
59 keys.emplace(m->masterKey, pk, token->validationSecret);
60 nodeID = calcNodeID(m->masterKey);
61 sequence = m->sequence;
62 manifest = std::move(token->manifest);
63 }
64 }
65 else
66 {
67 configInvalid_ = true;
68 JLOG(j.fatal())
69 << "Invalid token specified in [" SECTION_VALIDATOR_TOKEN "]";
70 }
71 }
72 else if (config.exists(SECTION_VALIDATION_SEED))
73 {
74 auto const seed = parseBase58<Seed>(
75 config.section(SECTION_VALIDATION_SEED).lines().front());
76 if (!seed)
77 {
78 configInvalid_ = true;
79 JLOG(j.fatal())
80 << "Invalid seed specified in [" SECTION_VALIDATION_SEED "]";
81 }
82 else
83 {
86 keys.emplace(pk, pk, sk);
87 nodeID = calcNodeID(pk);
88 sequence = 0;
89 }
90 }
91}
92} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:59
Stream fatal() const
Definition: Journal.h:351
bool exists(std::string const &name) const
Returns true if a section with the given name exists.
Section & section(std::string const &name)
Returns the section with the given name.
A public key.
Definition: PublicKey.h:62
A secret key.
Definition: SecretKey.h:37
std::vector< std::string > const & lines() const
Returns all the lines in the section.
Definition: BasicConfig.h:71
std::optional< Keys > keys
Definition: ValidatorKeys.h:62
std::uint32_t sequence
Definition: ValidatorKeys.h:65
T front(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::optional< Manifest > deserializeManifest(Slice s, beast::Journal journal)
Constructs Manifest from serialized string.
std::string base64_decode(std::string_view data)
Definition: base64.cpp:245
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
Definition: SecretKey.cpp:313
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
Definition: SecretKey.cpp:291
NodeID calcNodeID(PublicKey const &)
Calculate the 160-bit node ID from a node public key.
Definition: PublicKey.cpp:303
std::optional< ValidatorToken > loadValidatorToken(std::vector< std::string > const &blob, beast::Journal journal)