-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathutils.cpp
222 lines (194 loc) · 5.65 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "utils.hpp"
#include "public_key_address.hpp"
#include <bts/application.hpp>
#include <bts/profile.hpp>
#include <bts/addressbook/addressbook.hpp>
#include <bts/addressbook/contact.hpp>
#include <QStringList>
namespace Utils
{
QString toString(const fc::ecc::public_key& pk, TContactTextFormatting contactFormatting,
bts::addressbook::contact* matchingContact /*= nullptr*/, bool* isKnownContact /*= nullptr*/)
{
assert(pk.valid());
auto address_book = bts::get_profile()->get_addressbook();
auto c = address_book->get_contact_by_public_key(pk);
if (c)
{
if(matchingContact != nullptr)
*matchingContact = *c;
if(isKnownContact != nullptr)
*isKnownContact = true;
switch(contactFormatting)
{
case KEYHOTEE_IDENTIFIER:
return QString(c->dac_id_string.c_str());
case CONTACT_ALIAS_FULL_NAME:
return QString(std::string(c->first_name + " " + c->last_name).c_str());
case FULL_CONTACT_DETAILS:
return QString(c->get_display_name().c_str());
default:
assert(false);
return QString();
}
}
else
{
auto profile = bts::get_profile();
/// If no contact found try one of registered identities.
std::vector<bts::addressbook::wallet_identity> identities = profile->identities();
for(const auto& identity : identities)
{
assert(identity.public_key.valid());
if(identity.public_key == pk)
{
if(matchingContact != nullptr)
*matchingContact = identity;
if(isKnownContact != nullptr)
*isKnownContact = true;
switch(contactFormatting)
{
case KEYHOTEE_IDENTIFIER:
return QString::fromStdString(identity.dac_id_string);
case CONTACT_ALIAS_FULL_NAME:
return QString::fromStdString(std::string(identity.first_name + " " + identity.last_name));
case FULL_CONTACT_DETAILS:
return QString::fromStdString(identity.get_display_name());
default:
assert(false);
return QString();
}
break;
}
}
if(matchingContact != nullptr)
{
*matchingContact = bts::addressbook::wallet_contact();
matchingContact->public_key = pk;
}
if(isKnownContact != nullptr)
*isKnownContact = false;
/// If code reached this point the publick key is unknown - lets display it as base58
return QString::fromStdString(pk.to_base58());
}
}
bool matchContact(const fc::ecc::public_key& pk, bts::addressbook::wallet_contact* matchedContact)
{
assert(pk.valid());
auto address_book = bts::get_profile()->get_addressbook();
try
{
auto c = address_book->get_contact_by_public_key(pk);
if(c)
{
*matchedContact = *c;
return true;
}
else
{
*matchedContact = bts::addressbook::wallet_contact();
matchedContact->public_key = pk;
return false;
}
}
catch (const fc::exception&)
{
*matchedContact = bts::addressbook::wallet_contact();
matchedContact->public_key = pk;
return false;
}
}
bool matchIdentity(const fc::ecc::public_key& pk, bts::addressbook::wallet_identity0* matched_identity)
{
try
{
auto identities = bts::get_profile()->identities();
for(auto &it : identities)
{
if (pk == it.public_key)
{
*matched_identity = it;
return true;
}
}
*matched_identity = bts::addressbook::wallet_identity();
return false;
}
catch (const fc::exception&)
{
*matched_identity = bts::addressbook::wallet_identity();
return false;
}
}
QString
makeContactListString(const std::vector<fc::ecc::public_key>& key_list, char separator /*= ','*/,
TContactTextFormatting contactFormatting /* = KEYHOTEE_IDENTIFIER*/)
{
QStringList to_list;
for(const auto& public_key : key_list)
to_list.append(toString(public_key, contactFormatting));
return to_list.join(separator);
}
bool isOwnedPublicKey(const fc::ecc::public_key& public_key)
{
bts::application_ptr app = bts::application::instance();
bts::profile_ptr currentProfile = app->get_profile();
bts::keychain keyChain = currentProfile->get_keychain();
typedef std::set<fc::ecc::public_key_data> TPublicKeyIndex;
try
{
//put all public keys owned by profile into a set
TPublicKeyIndex myPublicKeys;
for (const auto& id : currentProfile->identities())
{
auto myPublicKey = keyChain.get_identity_key(id.dac_id_string).get_public_key();
fc::ecc::public_key_data keyData = myPublicKey;
myPublicKeys.insert(keyData);
}
//check if we have a public key in our set matching the contact's public key
return myPublicKeys.find(public_key) != myPublicKeys.end();
}
catch (const fc::exception&)
{
return false;
}
}
QString lTrim(QString const& str)
{
if (str.isEmpty())
return str;
const QChar *s = (const QChar*)str.data();
if (!s->isSpace() )
return str;
int start = 0;
int end = str.size() - 1;
while (start<=end && s[start].isSpace()) // skip white space from start
start++;
int l = end - start + 1;
if (l <= 0) {
QStringDataPtr empty = { QString::Data::allocate(0) };
return QString(empty);
}
return QString(s + start, l);
}
void convertToASCII(const std::string& input, std::string* buffer)
{
assert(buffer != nullptr);
buffer->reserve(input.size());
for(const auto& c : input)
{
unsigned int cCode = c;
if(cCode > 0x7F)
{
/// Non ASCII character
char numBuffer[64];
sprintf(numBuffer, "_0x%X_", cCode);
buffer->append(numBuffer);
}
else
{
*buffer += c;
}
}
}
} ///namespace Utils