rippled
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 <ripple/protocol/UintTypes.h>
21 #include <test/jtx/Account.h>
22 #include <test/jtx/amount.h>
23 
24 namespace ripple {
25 namespace test {
26 namespace jtx {
27 
30 
31 Account const Account::master(
32  "master",
33  generateKeyPair(KeyType::secp256k1, generateSeed("masterpassphrase")),
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 
48 Account
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 
80 Account::Account(AcctStringType stringType, std::string base58SeedStr)
81  : Account(fromCache(
82  Account::base58Seed,
83  std::move(base58SeedStr),
85 {
86 }
87 
88 IOU
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
ripple::test::jtx::Account::name
std::string const & name() const
Return the name.
Definition: Account.h:82
ripple::to_currency
bool to_currency(Currency &currency, std::string const &code)
Tries to convert a string to a Currency, returns true on success.
Definition: UintTypes.cpp:80
std::optional::has_value
T has_value(T... args)
std::string
STL class.
ripple::noCurrency
Currency const & noCurrency()
A placeholder for empty currencies.
Definition: UintTypes.cpp:128
std::pair
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:104
ripple::generateKeyPair
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
Definition: SecretKey.cpp:351
ripple::test::jtx::Account::privateCtorTag
Definition: Account.h:41
ripple::calcAccountID
AccountID calcAccountID(PublicKey const &pk)
Definition: AccountID.cpp:158
ripple::test::jtx::Account::master
static const Account master
The master account.
Definition: Account.h:47
std::forward_as_tuple
T forward_as_tuple(T... args)
ripple::test::jtx::Account::operator[]
IOU operator[](std::string const &s) const
Returns an IOU for the specified gateway currency.
Definition: Account.cpp:89
ripple::KeyType
KeyType
Definition: KeyType.h:28
ripple::KeyType::secp256k1
@ secp256k1
ripple::generateSeed
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition: Seed.cpp:69
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::jtx::IOU
Converts to IOU Issue or STAmount.
Definition: amount.h:291
std
STL namespace.
ripple::test::jtx::Account::cache_
static std::unordered_map< std::pair< std::string, KeyType >, Account, beast::uhash<> > cache_
Definition: Account.h:135
ripple::test::jtx::Account::base58Seed
@ base58Seed
Definition: Account.h:76
ripple::test::jtx::Account::AcctStringType
AcctStringType
Definition: Account.h:76
std::optional
ripple::test::jtx::Account
Immutable cryptographic account descriptor.
Definition: Account.h:37
std::make_pair
T make_pair(T... args)
beast::uhash<>
ripple::test::jtx::Account::fromCache
static Account fromCache(AcctStringType stringType, std::string name, KeyType type)
Definition: Account.cpp:49
std::unordered_map
STL class.
ripple::test::jtx::Account::Account
Account()=default