Scorum
authority.cpp
Go to the documentation of this file.
3 
4 #include <cctype>
5 
6 namespace scorum {
7 namespace protocol {
8 
9 // authority methods
11 {
12  key_auths[k] = w;
13 }
14 
16 {
17  account_auths[k] = w;
18 }
19 
20 std::vector<public_key_type> authority::get_keys() const
21 {
22  std::vector<public_key_type> result;
23  result.reserve(key_auths.size());
24  for (const auto& k : key_auths)
25  result.push_back(k.first);
26  return result;
27 }
28 
30 {
31  uint64_t auth_weights = 0;
32  for (const auto& item : account_auths)
33  auth_weights += item.second;
34  for (const auto& item : key_auths)
35  auth_weights += item.second;
36  return auth_weights < weight_threshold;
37 }
38 
39 uint32_t authority::num_auths() const
40 {
41  return account_auths.size() + key_auths.size();
42 }
43 
45 {
46  account_auths.clear();
47  key_auths.clear();
48 }
49 
50 void authority::validate() const
51 {
52  for (const auto& item : account_auths)
53  {
54  FC_ASSERT(is_valid_account_name(item.first));
55  }
56 }
57 
58 bool is_valid_account_name(const std::string& name)
59 {
60 #if SCORUM_MIN_ACCOUNT_NAME_LENGTH < 3
61 #error This is_valid_account_name implementation implicitly enforces minimum name length of 3.
62 #endif
63 
64  const size_t len = name.size();
65  if (len < SCORUM_MIN_ACCOUNT_NAME_LENGTH)
66  return false;
67 
68  if (len > SCORUM_MAX_ACCOUNT_NAME_LENGTH)
69  return false;
70 
71  size_t begin = 0;
72  while (true)
73  {
74  size_t end = name.find_first_of('.', begin);
75  if (end == std::string::npos)
76  end = len;
77  if (end - begin < 3)
78  return false;
79 
80  if (!std::islower(name[begin]))
81  return false;
82 
83  if (!std::islower(name[end - 1]) && !std::isdigit(name[end - 1]))
84  return false;
85 
86  for (size_t i = begin + 1; i < end - 1; ++i)
87  {
88  if (!std::islower(name[i]) && !std::isdigit(name[i]) && !(name[i] == '-'))
89  return false;
90  }
91  if (end == len)
92  break;
93  begin = end + 1;
94  }
95  return true;
96 }
97 
98 bool operator==(const authority& a, const authority& b)
99 {
101  && (a.key_auths == b.key_auths);
102 }
103 
104 } // namespace protocol
105 } // namespace scorum
bool operator==(const authority &a, const authority &b)
Definition: authority.cpp:98
fc::fixed_string_16 account_name_type
Definition: types.hpp:62
bool is_valid_account_name(const std::string &name)
Definition: authority.cpp:58
uint16_t authority_weight_type
Definition: types.hpp:68
Definition: asset.cpp:15
account_authority_map account_auths
Definition: authority.hpp:55
uint32_t num_auths() const
Definition: authority.cpp:39
key_authority_map key_auths
Definition: authority.hpp:56
void add_authority(const public_key_type &k, authority_weight_type w)
Definition: authority.cpp:10
std::vector< public_key_type > get_keys() const
Definition: authority.cpp:20