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
23#include <xrpl/protocol/UintTypes.h>
24
25namespace ripple {
26namespace test {
27namespace jtx {
28
31
32Account const Account::master(
33 "master",
35 Account::privateCtorTag{});
36
38 std::string name,
41 : name_(std::move(name))
42 , pk_(keys.first)
43 , sk_(keys.second)
44 , id_(calcAccountID(pk_))
45 , human_(toBase58(id_))
46{
47}
48
51{
52 auto p = std::make_pair(name, type); // non-const so it can be moved from
53 auto const iter = cache_.find(p);
54 if (iter != cache_.end())
55 return iter->second;
56
57 auto const keys = [stringType, &name, type]() {
58 // Special handling for base58Seeds.
59 if (stringType == base58Seed)
60 {
61 std::optional<Seed> const seed = parseBase58<Seed>(name);
62 if (!seed.has_value())
63 Throw<std::runtime_error>("Account:: invalid base58 seed");
64
65 return generateKeyPair(type, *seed);
66 }
67 return generateKeyPair(type, generateSeed(name));
68 }();
69 auto r = cache_.emplace(
71 std::forward_as_tuple(std::move(p)),
72 std::forward_as_tuple(std::move(name), keys, privateCtorTag{}));
73 return r.first->second;
74}
75
77 : Account(fromCache(Account::other, std::move(name), type))
78{
79}
80
82 : Account(fromCache(
83 Account::base58Seed,
84 std::move(base58SeedStr),
86{
87}
88
89IOU
91{
92 auto const currency = to_currency(s);
93 assert(currency != noCurrency());
94 return IOU(*this, currency);
95}
96
97} // namespace jtx
98} // namespace test
99} // namespace ripple
Immutable cryptographic account descriptor.
Definition: Account.h:39
static Account const master
The master account.
Definition: Account.h:48
std::string const & name() const
Return the name.
Definition: Account.h:83
IOU operator[](std::string const &s) const
Returns an IOU for the specified gateway currency.
Definition: Account.cpp:90
static std::unordered_map< std::pair< std::string, KeyType >, Account, beast::uhash<> > cache_
Definition: Account.h:136
static Account fromCache(AcctStringType stringType, std::string name, KeyType type)
Definition: Account.cpp:50
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:25
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:114
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
Definition: SecretKey.cpp:369
Currency const & noCurrency()
A placeholder for empty currencies.
Definition: UintTypes.cpp:126
AccountID calcAccountID(PublicKey const &pk)
Definition: AccountID.cpp:168
KeyType
Definition: KeyType.h:28
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition: Seed.cpp:76
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition: UintTypes.cpp:84
STL namespace.
T has_value(T... args)
T piecewise_construct