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");
63 BEAST_EXPECT(
testPassphrase(
"cookies excitement hand public") ==
"sspUXGrmjQhq6mgc24jiRuevZiwKT");
72 BEAST_EXPECT(parseBase58<Seed>(
"snoPBrXtMeMyMHUVTgbuqAfg1SUTb"));
73 BEAST_EXPECT(parseBase58<Seed>(
"snMKnVku798EnBwUfxeSD8953sLYA"));
74 BEAST_EXPECT(parseBase58<Seed>(
"sspUXGrmjQhq6mgc24jiRuevZiwKT"));
77 BEAST_EXPECT(!parseBase58<Seed>(
""));
78 BEAST_EXPECT(!parseBase58<Seed>(
"sspUXGrmjQhq6mgc24jiRuevZiwK"));
79 BEAST_EXPECT(!parseBase58<Seed>(
"sspUXGrmjQhq6mgc24jiRuevZiwKTT"));
80 BEAST_EXPECT(!parseBase58<Seed>(
"sspOXGrmjQhq6mgc24jiRuevZiwKT"));
81 BEAST_EXPECT(!parseBase58<Seed>(
"ssp/XGrmjQhq6mgc24jiRuevZiwKT"));
89 for (
int i = 0; i < 32; i++)
92 auto const seed2 = parseBase58<Seed>(
toBase58(seed1));
94 BEAST_EXPECT(
static_cast<bool>(seed2));
95 BEAST_EXPECT(
equal(seed1, *seed2));
102 std::string const message1 =
"http://www.ripple.com";
103 std::string const message2 =
"https://www.ripple.com";
106 testcase(
"Node keypair generation & signing (secp256k1)");
115 BEAST_EXPECT(
to_string(
calcNodeID(publicKey)) ==
"7E59C17D50F5959C7B158FEC95C8F815BF653DC8");
118 BEAST_EXPECT(sig.size() != 0);
135 if (
auto ptr = sig.data())
136 ptr[sig.size() / 2]++;
143 testcase(
"Node keypair generation & signing (ed25519)");
152 BEAST_EXPECT(
to_string(
calcNodeID(publicKey)) ==
"AA066C988C712815CC37AF71472B7CBBBD4E2A0A");
155 BEAST_EXPECT(sig.size() != 0);
172 if (
auto ptr = sig.data())
173 ptr[sig.size() / 2]++;
180 testcase(
"Account keypair generation & signing (secp256k1)");
191 BEAST_EXPECT(sig.size() != 0);
207 if (
auto ptr = sig.data())
208 ptr[sig.size() / 2]++;
215 testcase(
"Account keypair generation & signing (ed25519)");
226 BEAST_EXPECT(sig.size() != 0);
242 if (
auto ptr = sig.data())
243 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.