//------------------------------------------------------------------------------ /* This file is part of rippled: https://github.com/ripple/rippled Copyright (c) 2012, 2013 Ripple Labs Inc. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #include #include #include #include #include #include #include #include namespace ripple { std::pair loadNodeIdentity (Application& app) { // If a seed is specified in the configuration file use that directly. if (!app.config().NODE_SEED.empty ()) { auto const seed = parseBase58( app.config().NODE_SEED); if (!seed) Throw( "NodeIdentity: Bad [node_seed] specified"); auto secretKey = generateSecretKey (KeyType::secp256k1, *seed); auto publicKey = derivePublicKey (KeyType::secp256k1, secretKey); return { publicKey, secretKey }; } // Try to load a node identity from the database: boost::optional publicKey; boost::optional secretKey; auto db = app.getWalletDB ().checkoutDb (); { boost::optional pubKO, priKO; soci::statement st = (db->prepare << "SELECT PublicKey, PrivateKey FROM NodeIdentity;", soci::into(pubKO), soci::into(priKO)); st.execute (); while (st.fetch ()) { auto const sk = parseBase58( TOKEN_NODE_PRIVATE, priKO.value_or("")); auto const pk = parseBase58( TOKEN_NODE_PUBLIC, pubKO.value_or("")); // Only use if the public and secret keys are a pair if (sk && pk && (*pk == derivePublicKey (KeyType::secp256k1, *sk))) { secretKey = sk; publicKey = pk; } } } // If a valid identity wasn't found, we randomly generate a new one: if (!publicKey || !secretKey) { std::tie(publicKey, secretKey) = randomKeyPair(KeyType::secp256k1); *db << str (boost::format ( "INSERT INTO NodeIdentity (PublicKey,PrivateKey) VALUES ('%s','%s');") % toBase58 (TokenType::TOKEN_NODE_PUBLIC, *publicKey) % toBase58 (TokenType::TOKEN_NODE_PRIVATE, *secretKey)); } return { *publicKey, *secretKey }; } } // ripple