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/jss.h>
24 #include <ripple/protocol/Seed.h>
25 #include <ripple/rpc/Context.h>
26 
27 namespace ripple {
28 
29 static
30 boost::optional<Seed>
31 validationSeed (Json::Value const& params)
32 {
33  if (!params.isMember (jss::secret))
34  return randomSeed ();
35 
36  return parseGenericSeed (params[jss::secret].asString ());
37 }
38 
39 // {
40 // secret: <string> // optional
41 // }
42 //
43 // This command requires Role::ADMIN access because it makes
44 // 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] = toBase58 (
61  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 } // ripple
ripple::RPC::JsonContext
Definition: Context.h:52
ripple::doValidationCreate
Json::Value doValidationCreate(RPC::JsonContext &)
Definition: ValidationCreate.cpp:45
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:29
ripple::validationSeed
static boost::optional< Seed > validationSeed(Json::Value const &params)
Definition: ValidationCreate.cpp:31
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:45
ripple::derivePublicKey
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
Definition: SecretKey.cpp:228
ripple::rpcBAD_SEED
@ rpcBAD_SEED
Definition: ErrorCodes.h:100
ripple::seedAs1751
std::string seedAs1751(Seed const &seed)
Encode a Seed in RFC1751 format.
Definition: Seed.cpp:133
ripple::randomSeed
Seed randomSeed()
Create a seed using secure random numbers.
Definition: Seed.cpp:61
ripple::parseGenericSeed
boost::optional< Seed > parseGenericSeed(std::string const &str)
Attempt to parse a string as a seed.
Definition: Seed.cpp:96
ripple::generateSecretKey
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
Definition: SecretKey.cpp:199
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:961
ripple::rpcError
Json::Value rpcError(int iError, Json::Value jvResult)
Definition: RPCErr.cpp:28
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
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:63
ripple::TokenType::NodePrivate
@ NodePrivate
Json::Value
Represents a JSON value.
Definition: json_value.h:141