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/protocol/KeyType.h>
24 #include <ripple/protocol/SecretKey.h>
25 #include <ripple/protocol/UintTypes.h>
26 #include <ripple/beast/hash/uhash.h>
27 #include <unordered_map>
28 #include <string>
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
41  struct privateCtorTag{};
42 public:
44  static Account const master;
45 
46  Account() = default;
47  Account (Account&&) = default;
48  Account (Account const&) = default;
49  Account& operator= (Account const&) = default;
50  Account& operator= (Account&&) = default;
51 
56 
57  Account (char const* name,
59  : Account(std::string(name), type)
60  {
61  }
62 
63  // This constructor needs to be public so `std::pair` can use it when emplacing
64  // into the cache. However, it is logically `private`. This is enforced with the
65  // `privateTag` parameter.
68 
72  std::string const&
73  name() const
74  {
75  return name_;
76  }
77 
79  PublicKey const&
80  pk() const
81  {
82  return pk_;
83  }
84 
86  SecretKey const&
87  sk() const
88  {
89  return sk_;
90  }
91 
96  AccountID
97  id() const
98  {
99  return id_;
100  }
101 
103  std::string const&
104  human() const
105  {
106  return human_;
107  }
108 
114  operator AccountID() const
115  {
116  return id_;
117  }
118 
120  IOU
121  operator[](std::string const& s) const;
122 
123 private:
124  static std::unordered_map<
127 
128 
129  // Return the account from the cache & add it to the cache if needed
130  static Account fromCache(std::string name, KeyType type);
131 
136  std::string human_; // base58 public key string
137 };
138 
139 inline
140 bool
141 operator== (Account const& lhs,
142  Account const& rhs) noexcept
143 {
144  return lhs.id() == rhs.id();
145 }
146 
147 template <class Hasher>
148 void
149 hash_append (Hasher& h,
150  Account const& v) noexcept
151 {
152  hash_append(h, v.id());
153 }
154 
155 inline
156 bool
157 operator< (Account const& lhs,
158  Account const& rhs) noexcept
159 {
160  return lhs.id() < rhs.id();
161 }
162 
163 } // jtx
164 } // test
165 } // ripple
166 
167 #endif
ripple::test::jtx::Account::name
std::string const & name() const
Return the name.
Definition: Account.h:73
ripple::test::jtx::Account::id_
AccountID id_
Definition: Account.h:135
ripple::test::jtx::hash_append
void hash_append(Hasher &h, Account const &v) noexcept
Definition: Account.h:149
std::string
STL class.
std::pair
ripple::test::jtx::Account::fromCache
static Account fromCache(std::string name, KeyType type)
Definition: Account.cpp:46
ripple::test::jtx::Account::human
std::string const & human() const
Returns the human readable public key.
Definition: Account.h:104
ripple::test::jtx::Account::id
AccountID id() const
Returns the Account ID.
Definition: Account.h:97
ripple::test::jtx::Account::human_
std::string human_
Definition: Account.h:136
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:133
ripple::SecretKey
A secret key.
Definition: SecretKey.h:36
ripple::test::jtx::Account::master
static const Account master
The master account.
Definition: Account.h:44
ripple::test::jtx::Account::operator[]
IOU operator[](std::string const &s) const
Returns an IOU for the specified gateway currency.
Definition: Account.cpp:66
ripple::KeyType
KeyType
Definition: KeyType.h:28
ripple::KeyType::secp256k1
@ secp256k1
ripple::test::jtx::Account::name_
std::string name_
Definition: Account.h:132
ripple::test::jtx::operator<
bool operator<(Account const &lhs, Account const &rhs) noexcept
Definition: Account.h:157
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:312
std
STL namespace.
ripple::test::jtx::Account::cache_
static std::unordered_map< std::pair< std::string, KeyType >, Account, beast::uhash<> > cache_
Definition: Account.h:126
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:134
beast::uhash<>
ripple::test::jtx::Account::sk
SecretKey const & sk() const
Return the secret key.
Definition: Account.h:87
ripple::test::jtx::operator==
bool operator==(Account const &lhs, Account const &rhs) noexcept
Definition: Account.h:141
unordered_map
ripple::test::jtx::Account::pk
PublicKey const & pk() const
Return the public key.
Definition: Account.h:80
ripple::test::jtx::Account::Account
Account()=default
ripple::test::jtx::Account::Account
Account(char const *name, KeyType type=KeyType::secp256k1)
Definition: Account.h:57
string