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>
36 Seed const seed({src,
sizeof(src)});
37 BEAST_EXPECT(memcmp(seed.data(), src,
sizeof(src)) == 0);
41 for (
int i = 0; i < 64; i++)
46 BEAST_EXPECT(memcmp(seed.
data(), src.
data(), src.
size()) == 0);
54 auto const seed2 = parseBase58<Seed>(
toBase58(seed1));
56 BEAST_EXPECT(
static_cast<bool>(seed2));
57 BEAST_EXPECT(
equal(seed1, *seed2));
64 testcase(
"generation from passphrase");
67 "snoPBrXtMeMyMHUVTgbuqAfg1SUTb");
70 "snMKnVku798EnBwUfxeSD8953sLYA");
73 "sspUXGrmjQhq6mgc24jiRuevZiwKT");
82 BEAST_EXPECT(parseBase58<Seed>(
"snoPBrXtMeMyMHUVTgbuqAfg1SUTb"));
83 BEAST_EXPECT(parseBase58<Seed>(
"snMKnVku798EnBwUfxeSD8953sLYA"));
84 BEAST_EXPECT(parseBase58<Seed>(
"sspUXGrmjQhq6mgc24jiRuevZiwKT"));
87 BEAST_EXPECT(!parseBase58<Seed>(
""));
88 BEAST_EXPECT(!parseBase58<Seed>(
"sspUXGrmjQhq6mgc24jiRuevZiwK"));
89 BEAST_EXPECT(!parseBase58<Seed>(
"sspUXGrmjQhq6mgc24jiRuevZiwKTT"));
90 BEAST_EXPECT(!parseBase58<Seed>(
"sspOXGrmjQhq6mgc24jiRuevZiwKT"));
91 BEAST_EXPECT(!parseBase58<Seed>(
"ssp/XGrmjQhq6mgc24jiRuevZiwKT"));
99 for (
int i = 0; i < 32; i++)
102 auto const seed2 = parseBase58<Seed>(
toBase58(seed1));
104 BEAST_EXPECT(
static_cast<bool>(seed2));
105 BEAST_EXPECT(
equal(seed1, *seed2));
112 std::string const message1 =
"http://www.ripple.com";
113 std::string const message2 =
"https://www.ripple.com";
116 testcase(
"Node keypair generation & signing (secp256k1)");
120 auto const publicKey =
125 "n94a1u4jAz288pZLtw6yFWVbi89YamiC6JBXPVUj5zmExe5fTVg9");
128 "pnen77YEeUd4fFKG7iycBWcwKpTaeFRkW2WFostaATy1DSupwXe");
131 "7E59C17D50F5959C7B158FEC95C8F815BF653DC8");
134 BEAST_EXPECT(sig.size() != 0);
153 if (
auto ptr = sig.data())
154 ptr[sig.size() / 2]++;
161 testcase(
"Node keypair generation & signing (ed25519)");
169 "nHUeeJCSY2dM71oxM8Cgjouf5ekTuev2mwDpc374aLMxzDLXNmjf");
172 "paKv46LztLqK3GaKz1rG2nQGN6M4JLyRtxFBYFTw4wAVHtGys36");
175 "AA066C988C712815CC37AF71472B7CBBBD4E2A0A");
178 BEAST_EXPECT(sig.size() != 0);
197 if (
auto ptr = sig.data())
198 ptr[sig.size() / 2]++;
205 testcase(
"Account keypair generation & signing (secp256k1)");
212 "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh");
215 "aBQG8RQAzjs1eTKFEAQXr2gS4utcDiEC9wmi7pfUPTi27VCahwgw");
218 "p9JfM6HHi64m6mvB6v5k7G2b1cXzGmYiCNJf6GHPKvFTWdeRVjh");
221 BEAST_EXPECT(sig.size() != 0);
239 if (
auto ptr = sig.data())
240 ptr[sig.size() / 2]++;
247 testcase(
"Account keypair generation & signing (ed25519)");
254 "rGWrZyQqhTp9Xu7G5Pkayo7bXjH4k4QYpf");
257 "aKGheSBjmCsKJVuLNKRAKpZXT6wpk2FCuEZAXJupXgdAxX5THCqR");
260 "pwDQjwEhbUBmPuEjFpEG75bFhv2obkCB7NxQsfFxM7xGHBMVPu9");
263 BEAST_EXPECT(sig.size() != 0);
281 if (
auto ptr = sig.data())
282 ptr[sig.size() / 2]++;
testcase_t testcase
Memberspace for declaring test cases.
void run() override
Runs the suite.
std::string testPassphrase(std::string passphrase)
static bool equal(Seed const &lhs, Seed const &rhs)
void testKeypairGenerationAndSigning()
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::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig, bool mustBeFullyCanonical=true) noexcept
Verify a signature on a message.
Seed randomSeed()
Create a seed using secure random numbers.
AccountID calcAccountID(PublicKey const &pk)
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
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)
NodeID calcNodeID(PublicKey const &)
Calculate the 160-bit node ID from a node public key.
std::string to_string(base_uint< Bits, Tag > const &a)
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
std::optional< Seed > parseGenericSeed(std::string const &str, bool rfc1751=true)
Attempt to parse a string as a seed.
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
beast::xor_shift_engine & default_prng()
Return the default random engine.