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 
78  Account(AcctStringType stringType, std::string base58SeedStr);
79 
81  std::string const&
82  name() const
83  {
84  return name_;
85  }
86 
88  PublicKey const&
89  pk() const
90  {
91  return pk_;
92  }
93 
95  SecretKey const&
96  sk() const
97  {
98  return sk_;
99  }
100 
105  AccountID
106  id() const
107  {
108  return id_;
109  }
110 
112  std::string const&
113  human() const
114  {
115  return human_;
116  }
117 
123  operator AccountID() const
124  {
125  return id_;
126  }
127 
129  IOU
130  operator[](std::string const& s) const;
131 
132 private:
133  static std::
134  unordered_map<std::pair<std::string, KeyType>, Account, beast::uhash<>>
136 
137  // Return the account from the cache & add it to the cache if needed
138  static Account
139  fromCache(AcctStringType stringType, std::string name, KeyType type);
140 
145  std::string human_; // base58 public key string
146 };
147 
148 inline bool
149 operator==(Account const& lhs, Account const& rhs) noexcept
150 {
151  return lhs.id() == rhs.id();
152 }
153 
154 template <class Hasher>
155 void
156 hash_append(Hasher& h, Account const& v) noexcept
157 {
158  hash_append(h, v.id());
159 }
160 
161 inline bool
162 operator<(Account const& lhs, Account const& rhs) noexcept
163 {
164  return lhs.id() < rhs.id();
165 }
166 
167 } // namespace jtx
168 } // namespace test
169 } // namespace ripple
170 
171 #endif
ripple::test::jtx::Account::name
std::string const & name() const
Return the name.
Definition: Account.h:82
ripple::test::jtx::Account::id_
AccountID id_
Definition: Account.h:144
ripple::test::jtx::hash_append
void hash_append(Hasher &h, Account const &v) noexcept
Definition: Account.h:156
std::string
STL class.
std::pair
ripple::test::jtx::Account::human
std::string const & human() const
Returns the human readable public key.
Definition: Account.h:113
ripple::test::jtx::Account::other
@ other
Definition: Account.h:76
ripple::test::jtx::Account::id
AccountID id() const
Returns the Account ID.
Definition: Account.h:106
ripple::test::jtx::Account::human_
std::string human_
Definition: Account.h:145
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:142
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:89
ripple::KeyType
KeyType
Definition: KeyType.h:28
ripple::KeyType::secp256k1
@ secp256k1
ripple::test::jtx::Account::name_
std::string name_
Definition: Account.h:141
ripple::test::jtx::operator<
bool operator<(Account const &lhs, Account const &rhs) noexcept
Definition: Account.h:162
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
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:143
beast::uhash<>
ripple::test::jtx::Account::sk
SecretKey const & sk() const
Return the secret key.
Definition: Account.h:96
ripple::test::jtx::Account::fromCache
static Account fromCache(AcctStringType stringType, std::string name, KeyType type)
Definition: Account.cpp:49
ripple::test::jtx::operator==
bool operator==(Account const &lhs, Account const &rhs) noexcept
Definition: Account.h:149
unordered_map
ripple::test::jtx::Account::pk
PublicKey const & pk() const
Return the public key.
Definition: Account.h:89
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