xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
jtx
impl
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
22
namespace
xrpl::test::jtx
{
23
24
std::unordered_map<std::pair<std::string, KeyType>,
Account
, beast::Uhash<>>
Account::cache
;
25
26
Account
const
Account::kMaster
(
27
"master"
,
28
generateKeyPair
(
KeyType::Secp256k1
,
generateSeed
(
"masterpassphrase"
)),
29
Account::PrivateCtorTag
{});
30
31
Account::Account
(
32
std::string
name
,
33
std::pair<PublicKey, SecretKey>
const
& keys,
34
Account::PrivateCtorTag
)
35
:
name_
(
std
::move(
name
))
36
,
pk_
(keys.first)
37
,
sk_
(keys.second)
38
,
id_
(
calcAccountID
(
pk_
))
39
,
human_
(
toBase58
(
id_
))
40
{
41
}
42
43
Account
44
Account::fromCache
(
AcctStringType
stringType,
std::string
name
,
KeyType
type)
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
{
55
std::optional<Seed>
const
seed =
parseBase58<Seed>
(
name
);
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(
64
std::piecewise_construct
,
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
70
Account::Account
(
std::string
name
,
KeyType
type)
71
:
Account
(
fromCache
(
Account
::
AcctStringType
::Other,
std
::move(
name
), type))
72
{
73
}
74
75
Account::Account
(
AcctStringType
stringType,
std::string
base58SeedStr)
76
:
Account
(
fromCache
(
77
Account
::
AcctStringType
::Base58Seed,
78
std
::move(base58SeedStr),
79
KeyType
::
Secp256k1
))
80
{
81
}
82
83
Account::Account
(
std::string
name
,
AccountID
const
&
id
)
84
:
Account
(
name
,
randomKeyPair
(
KeyType
::
Secp256k1
),
PrivateCtorTag
{})
85
{
86
// override the randomly generated values
87
id_
=
id
;
88
human_
=
toBase58
(
id_
);
89
}
90
91
IOU
92
Account::operator[]
(
std::string
const
& s)
const
93
{
94
auto
const
currency =
toCurrency
(s);
95
assert(currency !=
noCurrency
());
96
return
IOU
(*
this
, currency);
97
}
98
99
}
// namespace xrpl::test::jtx
std::string
cassert
xrpl::test::jtx::Account
Immutable cryptographic account descriptor.
Definition
jtx/Account.h:21
xrpl::test::jtx::Account::human_
std::string human_
Definition
jtx/Account.h:146
xrpl::test::jtx::Account::fromCache
static Account fromCache(AcctStringType stringType, std::string name, KeyType type)
Definition
jtx/impl/Account.cpp:44
xrpl::test::jtx::Account::sk_
SecretKey sk_
Definition
jtx/Account.h:144
xrpl::test::jtx::Account::cache
static std::unordered_map< std::pair< std::string, KeyType >, Account, beast::Uhash<> > cache
Definition
jtx/Account.h:136
xrpl::test::jtx::Account::pk_
PublicKey pk_
Definition
jtx/Account.h:143
xrpl::test::jtx::Account::id_
AccountID id_
Definition
jtx/Account.h:145
xrpl::test::jtx::Account::name
std::string const & name() const
Return the name.
Definition
jtx/Account.h:75
xrpl::test::jtx::Account::Account
Account()=delete
xrpl::test::jtx::Account::kMaster
static Account const kMaster
The master account.
Definition
jtx/Account.h:32
xrpl::test::jtx::Account::name_
std::string name_
Definition
jtx/Account.h:142
xrpl::test::jtx::Account::AcctStringType
AcctStringType
Definition
jtx/Account.h:65
xrpl::test::jtx::Account::AcctStringType::Base58Seed
@ Base58Seed
Definition
jtx/Account.h:65
xrpl::test::jtx::Account::operator[]
IOU operator[](std::string const &s) const
Returns an IOU for the specified gateway currency.
Definition
jtx/impl/Account.cpp:92
xrpl::test::jtx::Account::id
AccountID id() const
Returns the Account ID.
Definition
jtx/Account.h:104
xrpl::test::jtx::IOU
Converts to IOU Issue or STAmount.
Definition
src/test/jtx/amount.h:404
std::forward_as_tuple
T forward_as_tuple(T... args)
std::make_pair
T make_pair(T... args)
std
STL namespace.
xrpl::test::jtx
Definition
Oracle_test.cpp:34
xrpl::KeyType
KeyType
Definition
KeyType.h:8
xrpl::KeyType::Secp256k1
@ Secp256k1
Definition
KeyType.h:9
xrpl::randomKeyPair
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
Definition
SecretKey.cpp:355
xrpl::parseBase58
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Definition
AccountID.cpp:105
xrpl::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition
AccountID.cpp:95
xrpl::toCurrency
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition
UintTypes.cpp:65
xrpl::generateSeed
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition
Seed.cpp:58
xrpl::generateKeyPair
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
Definition
SecretKey.cpp:338
xrpl::calcAccountID
AccountID calcAccountID(PublicKey const &pk)
Definition
AccountID.cpp:149
xrpl::AccountID
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition
AccountID.h:34
xrpl::noCurrency
Currency const & noCurrency()
A placeholder for empty currencies.
Definition
UintTypes.cpp:106
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
std::optional::has_value
T has_value(T... args)
optional
std::pair
std::piecewise_construct
T piecewise_construct
stdexcept
string
xrpl::test::jtx::Account::PrivateCtorTag
Definition
jtx/Account.h:25
tuple
unordered_map
utility
Generated by
1.17.0