1#include <xrpl/basics/random.h>
2#include <xrpl/beast/unit_test.h>
3#include <xrpl/beast/utility/rngfill.h>
4#include <xrpl/protocol/PublicKey.h>
5#include <xrpl/protocol/SecretKey.h>
6#include <xrpl/protocol/Seed.h>
32 Seed const seed({src,
sizeof(src)});
33 BEAST_EXPECT(memcmp(seed.data(), src,
sizeof(src)) == 0);
37 for (
int i = 0; i < 64; i++)
42 BEAST_EXPECT(memcmp(seed.
data(), src.
data(), src.
size()) == 0);
50 auto const seed2 = parseBase58<Seed>(
toBase58(seed1));
52 BEAST_EXPECT(
static_cast<bool>(seed2));
53 BEAST_EXPECT(
equal(seed1, *seed2));
60 testcase(
"generation from passphrase");
61 BEAST_EXPECT(
testPassphrase(
"masterpassphrase") ==
"snoPBrXtMeMyMHUVTgbuqAfg1SUTb");
62 BEAST_EXPECT(
testPassphrase(
"Non-Random Passphrase") ==
"snMKnVku798EnBwUfxeSD8953sLYA");
64 testPassphrase(
"cookies excitement hand public") ==
"sspUXGrmjQhq6mgc24jiRuevZiwKT");
73 BEAST_EXPECT(parseBase58<Seed>(
"snoPBrXtMeMyMHUVTgbuqAfg1SUTb"));
74 BEAST_EXPECT(parseBase58<Seed>(
"snMKnVku798EnBwUfxeSD8953sLYA"));
75 BEAST_EXPECT(parseBase58<Seed>(
"sspUXGrmjQhq6mgc24jiRuevZiwKT"));
78 BEAST_EXPECT(!parseBase58<Seed>(
""));
79 BEAST_EXPECT(!parseBase58<Seed>(
"sspUXGrmjQhq6mgc24jiRuevZiwK"));
80 BEAST_EXPECT(!parseBase58<Seed>(
"sspUXGrmjQhq6mgc24jiRuevZiwKTT"));
81 BEAST_EXPECT(!parseBase58<Seed>(
"sspOXGrmjQhq6mgc24jiRuevZiwKT"));
82 BEAST_EXPECT(!parseBase58<Seed>(
"ssp/XGrmjQhq6mgc24jiRuevZiwKT"));
90 for (
int i = 0; i < 32; i++)
93 auto const seed2 = parseBase58<Seed>(
toBase58(seed1));
95 BEAST_EXPECT(
static_cast<bool>(seed2));
96 BEAST_EXPECT(
equal(seed1, *seed2));
103 std::string const message1 =
"http://www.ripple.com";
104 std::string const message2 =
"https://www.ripple.com";
107 testcase(
"Node keypair generation & signing (secp256k1)");
109 auto const secretKey =
115 "n94a1u4jAz288pZLtw6yFWVbi89YamiC6JBXPVUj5zmExe5fTVg9");
118 "pnen77YEeUd4fFKG7iycBWcwKpTaeFRkW2WFostaATy1DSupwXe");
123 BEAST_EXPECT(!sig.empty());
141 if (
auto ptr = sig.data())
142 ptr[sig.size() / 2]++;
149 testcase(
"Node keypair generation & signing (ed25519)");
151 auto const secretKey =
157 "nHUeeJCSY2dM71oxM8Cgjouf5ekTuev2mwDpc374aLMxzDLXNmjf");
160 "paKv46LztLqK3GaKz1rG2nQGN6M4JLyRtxFBYFTw4wAVHtGys36");
165 BEAST_EXPECT(!sig.empty());
183 if (
auto ptr = sig.data())
184 ptr[sig.size() / 2]++;
191 testcase(
"Account keypair generation & signing (secp256k1)");
193 auto const [pk, sk] =
199 "aBQG8RQAzjs1eTKFEAQXr2gS4utcDiEC9wmi7pfUPTi27VCahwgw");
202 "p9JfM6HHi64m6mvB6v5k7G2b1cXzGmYiCNJf6GHPKvFTWdeRVjh");
205 BEAST_EXPECT(!sig.empty());
213 auto const otherKeyPair =
222 if (
auto ptr = sig.data())
223 ptr[sig.size() / 2]++;
230 testcase(
"Account keypair generation & signing (ed25519)");
232 auto const [pk, sk] =
238 "aKGheSBjmCsKJVuLNKRAKpZXT6wpk2FCuEZAXJupXgdAxX5THCqR");
241 "pwDQjwEhbUBmPuEjFpEG75bFhv2obkCB7NxQsfFxM7xGHBMVPu9");
244 BEAST_EXPECT(!sig.empty());
252 auto const otherKeyPair =
261 if (
auto ptr = sig.data())
262 ptr[sig.size() / 2]++;
testcase_t testcase
Memberspace for declaring test cases.
void testKeypairGenerationAndSigning()
std::string testPassphrase(std::string passphrase)
static bool equal(Seed const &lhs, Seed const &rhs)
void run() override
Runs the suite.
Seeds are used to generate deterministic secret keys.
std::uint8_t const * data() const
Integers of any length that is a multiple of 32-bits.
static constexpr std::size_t size()
void rngfill(void *const buffer, std::size_t const bytes, Generator &g)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
Seed randomSeed()
Create a seed using secure random numbers.
std::string to_string(base_uint< Bits, Tag > const &a)
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig) noexcept
Verify a signature on a message.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
beast::xor_shift_engine & default_prng()
Return the default random engine.
NodeID calcNodeID(PublicKey const &)
Calculate the 160-bit node ID from a node public key.
AccountID calcAccountID(PublicKey const &pk)
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
std::optional< Seed > parseGenericSeed(std::string const &str, bool rfc1751=true)
Attempt to parse a string as a seed.