rippled
Loading...
Searching...
No Matches
Account.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/jtx/Account.h>
21#include <test/jtx/amount.h>
22#include <xrpl/protocol/UintTypes.h>
23
24namespace ripple {
25namespace test {
26namespace jtx {
27
30
31Account const Account::master(
32 "master",
34 Account::privateCtorTag{});
35
37 std::string name,
40 : name_(std::move(name))
41 , pk_(keys.first)
42 , sk_(keys.second)
43 , id_(calcAccountID(pk_))
44 , human_(toBase58(id_))
45{
46}
47
50{
51 auto p = std::make_pair(name, type); // non-const so it can be moved from
52 auto const iter = cache_.find(p);
53 if (iter != cache_.end())
54 return iter->second;
55
56 auto const keys = [stringType, &name, type]() {
57 // Special handling for base58Seeds.
58 if (stringType == base58Seed)
59 {
60 std::optional<Seed> const seed = parseBase58<Seed>(name);
61 if (!seed.has_value())
62 Throw<std::runtime_error>("Account:: invalid base58 seed");
63
64 return generateKeyPair(type, *seed);
65 }
66 return generateKeyPair(type, generateSeed(name));
67 }();
68 auto r = cache_.emplace(
69 std::piecewise_construct,
70 std::forward_as_tuple(std::move(p)),
71 std::forward_as_tuple(std::move(name), keys, privateCtorTag{}));
72 return r.first->second;
73}
74
76 : Account(fromCache(Account::other, std::move(name), type))
77{
78}
79
81 : Account(fromCache(
82 Account::base58Seed,
83 std::move(base58SeedStr),
85{
86}
87
88IOU
90{
91 auto const currency = to_currency(s);
92 assert(currency != noCurrency());
93 return IOU(*this, currency);
94}
95
96} // namespace jtx
97} // namespace test
98} // namespace ripple
Immutable cryptographic account descriptor.
Definition: Account.h:38
static Account const master
The master account.
Definition: Account.h:47
std::string const & name() const
Return the name.
Definition: Account.h:82
IOU operator[](std::string const &s) const
Returns an IOU for the specified gateway currency.
Definition: Account.cpp:89
static std::unordered_map< std::pair< std::string, KeyType >, Account, beast::uhash<> > cache_
Definition: Account.h:135
static Account fromCache(AcctStringType stringType, std::string name, KeyType type)
Definition: Account.cpp:49
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:26
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:106
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
Definition: SecretKey.cpp:351
Currency const & noCurrency()
A placeholder for empty currencies.
Definition: UintTypes.cpp:122
AccountID calcAccountID(PublicKey const &pk)
Definition: AccountID.cpp:160
KeyType
Definition: KeyType.h:28
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition: Seed.cpp:69
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition: UintTypes.cpp:80
STL namespace.
T has_value(T... args)