rippled
Loading...
Searching...
No Matches
Account.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/amount.h>
3
4#include <xrpl/protocol/UintTypes.h>
5
6namespace ripple {
7namespace test {
8namespace jtx {
9
12
13Account const Account::master(
14 "master",
16 Account::privateCtorTag{});
17
19 std::string name,
22 : name_(std::move(name))
23 , pk_(keys.first)
24 , sk_(keys.second)
25 , id_(calcAccountID(pk_))
26 , human_(toBase58(id_))
27{
28}
29
32{
33 auto p = std::make_pair(name, type); // non-const so it can be moved from
34 auto const iter = cache_.find(p);
35 if (iter != cache_.end())
36 return iter->second;
37
38 auto const keys = [stringType, &name, type]() {
39 // Special handling for base58Seeds.
40 if (stringType == base58Seed)
41 {
42 std::optional<Seed> const seed = parseBase58<Seed>(name);
43 if (!seed.has_value())
44 Throw<std::runtime_error>("Account:: invalid base58 seed");
45
46 return generateKeyPair(type, *seed);
47 }
48 return generateKeyPair(type, generateSeed(name));
49 }();
50 auto r = cache_.emplace(
52 std::forward_as_tuple(std::move(p)),
53 std::forward_as_tuple(std::move(name), keys, privateCtorTag{}));
54 return r.first->second;
55}
56
58 : Account(fromCache(Account::other, std::move(name), type))
59{
60}
61
63 : Account(fromCache(
64 Account::base58Seed,
65 std::move(base58SeedStr),
67{
68}
69
72{
73 // override the randomly generated values
74 id_ = id;
76}
77
78IOU
80{
81 auto const currency = to_currency(s);
82 assert(currency != noCurrency());
83 return IOU(*this, currency);
84}
85
86} // namespace jtx
87} // namespace test
88} // namespace ripple
Immutable cryptographic account descriptor.
Definition Account.h:20
AccountID id() const
Returns the Account ID.
Definition Account.h:92
static Account const master
The master account.
Definition Account.h:29
std::string const & name() const
Return the name.
Definition Account.h:68
IOU operator[](std::string const &s) const
Returns an IOU for the specified gateway currency.
Definition Account.cpp:79
static std::unordered_map< std::pair< std::string, KeyType >, Account, beast::uhash<> > cache_
Definition Account.h:121
static Account fromCache(AcctStringType stringType, std::string name, KeyType type)
Definition Account.cpp:31
Converts to IOU Issue or STAmount.
T forward_as_tuple(T... args)
T make_pair(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
Currency const & noCurrency()
A placeholder for empty currencies.
AccountID calcAccountID(PublicKey const &pk)
KeyType
Definition KeyType.h:9
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition Seed.cpp:57
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:65
STL namespace.
T has_value(T... args)
T piecewise_construct