rippled
ValidationCreate.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2014 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/basics/Log.h>
21 #include <ripple/net/RPCErr.h>
22 #include <ripple/protocol/ErrorCodes.h>
23 #include <ripple/protocol/Seed.h>
24 #include <ripple/protocol/jss.h>
25 #include <ripple/rpc/Context.h>
26 
27 namespace ripple {
28 
31 {
32  if (!params.isMember(jss::secret))
33  return randomSeed();
34 
35  return parseGenericSeed(params[jss::secret].asString());
36 }
37 
38 // {
39 // secret: <string> // optional
40 // }
41 //
42 // This command requires Role::ADMIN access because it makes
43 // no sense to ask an untrusted server for this.
46 {
48 
49  auto seed = validationSeed(context.params);
50 
51  if (!seed)
52  return rpcError(rpcBAD_SEED);
53 
54  auto const private_key = generateSecretKey(KeyType::secp256k1, *seed);
55 
56  obj[jss::validation_public_key] = toBase58(
58  derivePublicKey(KeyType::secp256k1, private_key));
59 
60  obj[jss::validation_private_key] =
61  toBase58(TokenType::NodePrivate, private_key);
62 
63  obj[jss::validation_seed] = toBase58(*seed);
64  obj[jss::validation_key] = seedAs1751(*seed);
65 
66  return obj;
67 }
68 
69 } // namespace ripple
ripple::RPC::JsonContext
Definition: Context.h:53
ripple::doValidationCreate
Json::Value doValidationCreate(RPC::JsonContext &)
Definition: ValidationCreate.cpp:45
ripple::rpcError
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:29
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:104
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::derivePublicKey
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
Definition: SecretKey.cpp:313
ripple::rpcBAD_SEED
@ rpcBAD_SEED
Definition: ErrorCodes.h:99
ripple::seedAs1751
std::string seedAs1751(Seed const &seed)
Encode a Seed in RFC1751 format.
Definition: Seed.cpp:128
ripple::randomSeed
Seed randomSeed()
Create a seed using secure random numbers.
Definition: Seed.cpp:59
ripple::validationSeed
static std::optional< Seed > validationSeed(Json::Value const &params)
Definition: ValidationCreate.cpp:30
ripple::generateSecretKey
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
Definition: SecretKey.cpp:291
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:932
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::TokenType::NodePublic
@ NodePublic
std::optional
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
ripple::TokenType::NodePrivate
@ NodePrivate
Json::Value
Represents a JSON value.
Definition: json_value.h:145