xrpld
Loading...
Searching...
No Matches
jtx/impl/Account.cpp
1#include <test/jtx/Account.h>
2
3#include <test/jtx/amount.h>
4
5#include <xrpl/basics/contract.h>
6#include <xrpl/beast/hash/uhash.h>
7#include <xrpl/protocol/AccountID.h>
8#include <xrpl/protocol/KeyType.h>
9#include <xrpl/protocol/PublicKey.h>
10#include <xrpl/protocol/SecretKey.h>
11#include <xrpl/protocol/Seed.h>
12#include <xrpl/protocol/UintTypes.h>
13
14#include <cassert>
15#include <optional>
16#include <stdexcept>
17#include <string>
18#include <tuple>
19#include <unordered_map>
20#include <utility>
21
22namespace xrpl::test::jtx {
23
24std::unordered_map<std::pair<std::string, KeyType>, Account, beast::Uhash<>> Account::cache;
25
27 "master",
30
35 : name_(std::move(name))
36 , pk_(keys.first)
37 , sk_(keys.second)
40{
41}
42
45{
46 auto p = std::make_pair(name, type); // non-const so it can be moved from
47 auto const iter = cache.find(p);
48 if (iter != cache.end())
49 return iter->second;
50
51 auto const keys = [stringType, &name, type]() {
52 // Special handling for base58Seeds.
53 if (stringType == AcctStringType::Base58Seed)
54 {
56 if (!seed.has_value())
57 Throw<std::runtime_error>("Account:: invalid base58 seed");
58
59 return generateKeyPair(type, *seed);
60 }
61 return generateKeyPair(type, generateSeed(name));
62 }();
63 auto r = cache.emplace(
65 std::forward_as_tuple(std::move(p)),
66 std::forward_as_tuple(std::move(name), keys, PrivateCtorTag{}));
67 return r.first->second;
68}
69
74
77 Account::AcctStringType::Base58Seed,
78 std::move(base58SeedStr),
80{
81}
82
85{
86 // override the randomly generated values
87 id_ = id;
89}
90
91IOU
93{
94 auto const currency = toCurrency(s);
95 assert(currency != noCurrency());
96 return IOU(*this, currency);
97}
98
99} // namespace xrpl::test::jtx
Immutable cryptographic account descriptor.
Definition jtx/Account.h:17
static Account fromCache(AcctStringType stringType, std::string name, KeyType type)
static std::unordered_map< std::pair< std::string, KeyType >, Account, beast::Uhash<> > cache
std::string const & name() const
Return the name.
Definition jtx/Account.h:61
static Account const kMaster
The master account.
Definition jtx/Account.h:26
IOU operator[](std::string const &s) const
Returns an IOU for the specified gateway currency.
AccountID id() const
Returns the Account ID.
Definition jtx/Account.h:85
Converts to IOU Issue or STAmount.
T forward_as_tuple(T... args)
T make_pair(T... args)
STL namespace.
KeyType
Definition KeyType.h:8
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:93
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:65
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition Seed.cpp:58
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
AccountID calcAccountID(PublicKey const &pk)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
Currency const & noCurrency()
A placeholder for empty currencies.
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
T has_value(T... args)
T piecewise_construct