rippled
Loading...
Searching...
No Matches
AccountID.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 <xrpl/basics/hardened_hash.h>
21#include <xrpl/basics/spinlock.h>
22#include <xrpl/protocol/AccountID.h>
23#include <xrpl/protocol/PublicKey.h>
24#include <xrpl/protocol/digest.h>
25#include <xrpl/protocol/tokens.h>
26#include <array>
27#include <cstring>
28#include <mutex>
29
30namespace ripple {
31
32namespace detail {
33
36{
37private:
39 {
41 char encoding[40] = {0};
42 };
43
44 // The actual cache
46
47 // We use a hash function designed to resist algorithmic complexity attacks
49
50 // 64 spinlocks, packed into a single 64-bit value
52
53public:
55 {
56 // This is non-binding, but we try to avoid wasting memory that
57 // is caused by overallocation.
58 cache_.shrink_to_fit();
59 }
60
63 {
64 auto const index = hasher_(id) % cache_.size();
65
66 packed_spinlock sl(locks_, index % 64);
67
68 {
69 std::lock_guard lock(sl);
70
71 // The check against the first character of the encoding ensures
72 // that we don't mishandle the case of the all-zero account:
73 if (cache_[index].encoding[0] != 0 && cache_[index].id == id)
74 return cache_[index].encoding;
75 }
76
77 auto ret =
78 encodeBase58Token(TokenType::AccountID, id.data(), id.size());
79
80 XRPL_ASSERT(
81 ret.size() <= 38,
82 "ripple::detail::AccountIdCache : maximum result size");
83
84 {
85 std::lock_guard lock(sl);
86 cache_[index].id = id;
87 std::strcpy(cache_[index].encoding, ret.c_str());
88 }
89
90 return ret;
91 }
92};
93
94} // namespace detail
95
97
98void
100{
101 if (!accountIdCache && count != 0)
102 accountIdCache = std::make_unique<detail::AccountIdCache>(count);
103}
104
107{
108 if (accountIdCache)
109 return accountIdCache->toBase58(v);
110
112}
113
114template <>
117{
118 auto const result = decodeBase58Token(s, TokenType::AccountID);
119 if (result.size() != AccountID::bytes)
120 return std::nullopt;
121 return AccountID{result};
122}
123
124//------------------------------------------------------------------------------
125/*
126 Calculation of the Account ID
127
128 The AccountID is a 160-bit identifier that uniquely
129 distinguishes an account. The account may or may not
130 exist in the ledger. Even for accounts that are not in
131 the ledger, cryptographic operations may be performed
132 which affect the ledger. For example, designating an
133 account not in the ledger as a regular key for an
134 account that is in the ledger.
135
136 Why did we use half of SHA512 for most things but then
137 SHA256 followed by RIPEMD160 for account IDs? Why didn't
138 we do SHA512 half then RIPEMD160? Or even SHA512 then RIPEMD160?
139 For that matter why RIPEMD160 at all why not just SHA512 and keep
140 only 160 bits?
141
142 Answer (David Schwartz):
143
144 The short answer is that we kept Bitcoin's behavior.
145 The longer answer was that:
146 1) Using a single hash could leave ripple
147 vulnerable to length extension attacks.
148 2) Only RIPEMD160 is generally considered safe at 160 bits.
149
150 Any of those schemes would have been acceptable. However,
151 the one chosen avoids any need to defend the scheme chosen.
152 (Against any criticism other than unnecessary complexity.)
153
154 "The historical reason was that in the very early days,
155 we wanted to give people as few ways to argue that we were
156 less secure than Bitcoin. So where there was no good reason
157 to change something, it was not changed."
158*/
161{
162 static_assert(AccountID::bytes == sizeof(ripesha_hasher::result_type));
163
164 ripesha_hasher rsh;
165 rsh(pk.data(), pk.size());
166 return AccountID{static_cast<ripesha_hasher::result_type>(rsh)};
167}
168
169AccountID const&
171{
172 static AccountID const account(beast::zero);
173 return account;
174}
175
176AccountID const&
178{
179 static AccountID const account(1);
180 return account;
181}
182
183bool
185{
186 if (issuer.parseHex(s))
187 return true;
188 auto const account = parseBase58<AccountID>(s);
189 if (!account)
190 return false;
191 issuer = *account;
192 return true;
193}
194
195} // namespace ripple
A public key.
Definition: PublicKey.h:62
std::uint8_t const * data() const noexcept
Definition: PublicKey.h:87
std::size_t size() const noexcept
Definition: PublicKey.h:93
static std::size_t constexpr bytes
Definition: base_uint.h:107
pointer data()
Definition: base_uint.h:124
static constexpr std::size_t size()
Definition: base_uint.h:525
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:502
Caches the base58 representations of AccountIDs.
Definition: AccountID.cpp:36
std::string toBase58(AccountID const &id)
Definition: AccountID.cpp:62
AccountIdCache(std::size_t count)
Definition: AccountID.cpp:54
std::vector< CachedAccountID > cache_
Definition: AccountID.cpp:45
std::atomic< std::uint64_t > locks_
Definition: AccountID.cpp:51
Seed functor once per construction.
Definition: hardened_hash.h:97
Classes to handle arrays of spinlocks packed into a single atomic integer:
Definition: spinlock.h:90
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition: AccountID.h:49
AccountID const & noAccount()
A placeholder for empty accounts.
Definition: AccountID.cpp:177
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:106
AccountID const & xrpAccount()
Compute AccountID from public key.
Definition: AccountID.cpp:170
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Definition: AccountID.cpp:116
static std::unique_ptr< detail::AccountIdCache > accountIdCache
Definition: AccountID.cpp:96
bool to_issuer(AccountID &, std::string const &)
Convert hex or base58 string to AccountID.
Definition: AccountID.cpp:184
void initAccountIdCache(std::size_t count)
Initialize the global cache used to map AccountID to base58 conversions.
Definition: AccountID.cpp:99
std::string encodeBase58Token(TokenType type, void const *token, std::size_t size)
Encode data in Base58Check format using XRPL alphabet.
Definition: tokens.cpp:195
AccountID calcAccountID(PublicKey const &pk)
Definition: AccountID.cpp:160
std::string decodeBase58Token(std::string const &s, TokenType type)
Definition: tokens.cpp:205
T strcpy(T... args)
Returns the RIPEMD-160 digest of the SHA256 hash of the message.
Definition: digest.h:135
std::array< std::uint8_t, 20 > result_type
Definition: digest.h:142