mirror of
https://github.com/Xahau/xahaud.git
synced 2026-09-28 07:58:05 +00:00
- Fix unlikely edge case in setCurrentThreadName
setCurrentThreadName accepts a std::string_view parameter, which is
not guaranteed to include a null terminator, which it then forwards
to OS-specific APIs that expect null-terminated C strings.
- Fix several compilation warnings
- Fix header includes
- Fix unnecessarily verbose logging
- Simplify code using inline lambdas instead of std::bind
- Modernize the Beast "lexical cast" framework
- Use boost::asio types instead of legacy Beast type wrappers
- Clean up LogicError, custom exception throwing and termination handling
- Clean up and modernize PublicKey, SecretKey and Seed
Simplify construction, buffer iteration and comparisons by leveraging
modern C++ features.
- Clean up and modernize RFC1751 code
Turn static-member-only class into namespace, thin out the public API
and make code noexcept and constexpr. Verify internal data at compile
time.
- Clean up CSPRNG engine
- Clean up rngfill function, eliminating GCC false-positive warning.
- Improve Slice comparison using C++ operator<=> and operator synthesis.
135 lines
3.9 KiB
C++
135 lines
3.9 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
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.
|
|
*/
|
|
//==============================================================================
|
|
|
|
#ifndef RIPPLE_PROTOCOL_SECRETKEY_H_INCLUDED
|
|
#define RIPPLE_PROTOCOL_SECRETKEY_H_INCLUDED
|
|
|
|
#include <xrpl/basics/Buffer.h>
|
|
#include <xrpl/basics/Slice.h>
|
|
#include <xrpl/protocol/KeyType.h>
|
|
#include <xrpl/protocol/PublicKey.h>
|
|
#include <xrpl/protocol/Seed.h>
|
|
#include <xrpl/protocol/detail/KeyBase.h>
|
|
#include <xrpl/protocol/tokens.h>
|
|
#include <array>
|
|
#include <string>
|
|
|
|
namespace ripple {
|
|
|
|
/** A secret key. */
|
|
class SecretKey : public detail::KeyBase<SecretKey, 32>
|
|
{
|
|
public:
|
|
SecretKey() = delete;
|
|
|
|
SecretKey(SecretKey const&) = default;
|
|
SecretKey&
|
|
operator=(SecretKey const&) = default;
|
|
|
|
/** Destroy the secret key. This will try to securely erase the buffer. */
|
|
~SecretKey();
|
|
|
|
SecretKey(Slice const& slice)
|
|
{
|
|
if (slice.size() != buf_.size())
|
|
LogicError("SecretKey::SecretKey: invalid size");
|
|
|
|
std::copy_n(slice.data(), buf_.size(), buf_.data());
|
|
}
|
|
|
|
explicit SecretKey(span_t const& data) noexcept : KeyBase(data)
|
|
{
|
|
}
|
|
};
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
/** Parse a secret key */
|
|
template <>
|
|
std::optional<SecretKey>
|
|
parseBase58(TokenType type, std::string const& s);
|
|
|
|
inline std::string
|
|
toBase58(TokenType type, SecretKey const& sk)
|
|
{
|
|
return encodeBase58Token(type, sk.data(), sk.size());
|
|
}
|
|
|
|
/** Create a secret key using secure random numbers. */
|
|
SecretKey
|
|
randomSecretKey();
|
|
|
|
/** Generate a new secret key deterministically. */
|
|
SecretKey
|
|
generateSecretKey(KeyType type, Seed const& seed);
|
|
|
|
/** Derive the public key from a secret key. */
|
|
PublicKey
|
|
derivePublicKey(KeyType type, SecretKey const& sk);
|
|
|
|
/** Generate a key pair deterministically.
|
|
|
|
This algorithm is specific to Ripple:
|
|
|
|
For secp256k1 key pairs, the seed is converted
|
|
to a Generator and used to compute the key pair
|
|
corresponding to ordinal 0 for the generator.
|
|
*/
|
|
std::pair<PublicKey, SecretKey>
|
|
generateKeyPair(KeyType type, Seed const& seed);
|
|
|
|
/** Create a key pair using secure random numbers. */
|
|
std::pair<PublicKey, SecretKey>
|
|
randomKeyPair(KeyType type);
|
|
|
|
/** Generate a signature for a message digest.
|
|
This can only be used with secp256k1 since Ed25519's
|
|
security properties come, in part, from how the message
|
|
is hashed.
|
|
*/
|
|
/** @{ */
|
|
Buffer
|
|
signDigest(PublicKey const& pk, SecretKey const& sk, uint256 const& digest);
|
|
|
|
inline Buffer
|
|
signDigest(KeyType type, SecretKey const& sk, uint256 const& digest)
|
|
{
|
|
return signDigest(derivePublicKey(type, sk), sk, digest);
|
|
}
|
|
/** @} */
|
|
|
|
/** Generate a signature for a message.
|
|
With secp256k1 signatures, the data is first hashed with
|
|
SHA512-Half, and the resulting digest is signed.
|
|
*/
|
|
/** @{ */
|
|
Buffer
|
|
sign(PublicKey const& pk, SecretKey const& sk, Slice const& message);
|
|
|
|
inline Buffer
|
|
sign(KeyType type, SecretKey const& sk, Slice const& message)
|
|
{
|
|
return sign(derivePublicKey(type, sk), sk, message);
|
|
}
|
|
/** @} */
|
|
|
|
} // namespace ripple
|
|
|
|
#endif
|