rippled
Account.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2015 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 #ifndef RIPPLE_TEST_JTX_ACCOUNT_H_INCLUDED
21 #define RIPPLE_TEST_JTX_ACCOUNT_H_INCLUDED
22 
23 #include <ripple/beast/hash/uhash.h>
24 #include <ripple/protocol/KeyType.h>
25 #include <ripple/protocol/SecretKey.h>
26 #include <ripple/protocol/UintTypes.h>
27 #include <string>
28 #include <unordered_map>
29 
30 namespace ripple {
31 namespace test {
32 namespace jtx {
33 
34 class IOU;
35 
37 class Account
38 {
39 private:
40  // Tag for access to private contr
42  {
43  };
44 
45 public:
47  static Account const master;
48 
49  Account() = default;
50  Account(Account&&) = default;
51  Account(Account const&) = default;
52  Account&
53  operator=(Account const&) = default;
54  Account&
55  operator=(Account&&) = default;
56 
60 
61  Account(char const* name, KeyType type = KeyType::secp256k1)
62  : Account(std::string(name), type)
63  {
64  }
65 
66  // This constructor needs to be public so `std::pair` can use it when
67  // emplacing into the cache. However, it is logically `private`. This is
68  // enforced with the `privateTag` parameter.
69  Account(
73 
77  std::string const&
78  name() const
79  {
80  return name_;
81  }
82 
84  PublicKey const&
85  pk() const
86  {
87  return pk_;
88  }
89 
91  SecretKey const&
92  sk() const
93  {
94  return sk_;
95  }
96 
101  AccountID
102  id() const
103  {
104  return id_;
105  }
106 
108  std::string const&
109  human() const
110  {
111  return human_;
112  }
113 
119  operator AccountID() const
120  {
121  return id_;
122  }
123 
125  IOU
126  operator[](std::string const& s) const;
127 
128 private:
129  static std::
130  unordered_map<std::pair<std::string, KeyType>, Account, beast::uhash<>>
132 
133  // Return the account from the cache & add it to the cache if needed
134  static Account
136 
141  std::string human_; // base58 public key string
142 };
143 
144 inline bool
145 operator==(Account const& lhs, Account const& rhs) noexcept
146 {
147  return lhs.id() == rhs.id();
148 }
149 
150 template <class Hasher>
151 void
152 hash_append(Hasher& h, Account const& v) noexcept
153 {
154  hash_append(h, v.id());
155 }
156 
157 inline bool
158 operator<(Account const& lhs, Account const& rhs) noexcept
159 {
160  return lhs.id() < rhs.id();
161 }
162 
163 } // namespace jtx
164 } // namespace test
165 } // namespace ripple
166 
167 #endif
ripple::test::jtx::Account::name
std::string const & name() const
Return the name.
Definition: Account.h:78
ripple::test::jtx::Account::id_
AccountID id_
Definition: Account.h:140
ripple::test::jtx::hash_append
void hash_append(Hasher &h, Account const &v) noexcept
Definition: Account.h:152
std::string
STL class.
std::pair
ripple::test::jtx::Account::fromCache
static Account fromCache(std::string name, KeyType type)
Definition: Account.cpp:49
ripple::test::jtx::Account::human
std::string const & human() const
Returns the human readable public key.
Definition: Account.h:109
ripple::test::jtx::Account::id
AccountID id() const
Returns the Account ID.
Definition: Account.h:102
ripple::test::jtx::Account::human_
std::string human_
Definition: Account.h:141
ripple::base_uint< 160, detail::AccountIDTag >
ripple::test::jtx::Account::privateCtorTag
Definition: Account.h:41
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::test::jtx::Account::pk_
PublicKey pk_
Definition: Account.h:138
ripple::SecretKey
A secret key.
Definition: SecretKey.h:36
ripple::test::jtx::Account::master
static const Account master
The master account.
Definition: Account.h:47
ripple::test::jtx::Account::operator[]
IOU operator[](std::string const &s) const
Returns an IOU for the specified gateway currency.
Definition: Account.cpp:70
ripple::KeyType
KeyType
Definition: KeyType.h:28
ripple::KeyType::secp256k1
@ secp256k1
ripple::test::jtx::Account::name_
std::string name_
Definition: Account.h:137
ripple::test::jtx::operator<
bool operator<(Account const &lhs, Account const &rhs) noexcept
Definition: Account.h:158
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:131
ripple::test::jtx::Account::operator=
Account & operator=(Account const &)=default
ripple::test::jtx::Account
Immutable cryptographic account descriptor.
Definition: Account.h:37
ripple::test::jtx::Account::sk_
SecretKey sk_
Definition: Account.h:139
beast::uhash<>
ripple::test::jtx::Account::sk
SecretKey const & sk() const
Return the secret key.
Definition: Account.h:92
ripple::test::jtx::operator==
bool operator==(Account const &lhs, Account const &rhs) noexcept
Definition: Account.h:145
unordered_map
ripple::test::jtx::Account::pk
PublicKey const & pk() const
Return the public key.
Definition: Account.h:85
ripple::test::jtx::Account::Account
Account()=default
ripple::test::jtx::Account::Account
Account(char const *name, KeyType type=KeyType::secp256k1)
Definition: Account.h:61
string