Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
feat(toxid): Improve the ToxId class
Browse files Browse the repository at this point in the history
  • Loading branch information
sudden6 committed Dec 29, 2016
1 parent b9c1d59 commit 94ec561
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 67 deletions.
4 changes: 2 additions & 2 deletions src/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ void Core::requestFriendship(const QString& friendAddress, const QString& messag

Profile* profile = Nexus::getProfile();
if (profile->isHistoryEnabled())
profile->getHistory()->addNewMessage(userId, inviteStr, getSelfId().publicKey, QDateTime::currentDateTime(), true, QString());
profile->getHistory()->addNewMessage(userId, inviteStr, getSelfId().getPublicKeyString(), QDateTime::currentDateTime(), true, QString());
emit friendAdded(friendId, userId);
emit friendshipChanged(friendId);
}
Expand Down Expand Up @@ -839,7 +839,7 @@ void Core::setAvatar(const QByteArray& data)
{
QPixmap pic;
pic.loadFromData(data);
profile.saveAvatar(data, getSelfId().publicKey);
profile.saveAvatar(data, getSelfId().getPublicKeyString());
emit selfAvatarChanged(pic);
}
else
Expand Down
102 changes: 82 additions & 20 deletions src/core/toxid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@
#include <tox/tox.h>
#include <qregularexpression.h>

#define TOX_ID_PUBLIC_KEY_LENGTH 64
#define TOX_ID_NO_SPAM_LENGTH 8
#define TOX_ID_CHECKSUM_LENGTH 4
#define TOX_HEX_ID_LENGTH 2*TOX_ADDRESS_SIZE
// Tox doesn't publicly define these
#define NOSPAM_BYTES 4
#define CHECKSUM_BYTES 2

#define PUBLIC_KEY_HEX_CHARS (2*TOX_PUBLIC_KEY_SIZE)
#define NOSPAM_HEX_CHARS (2*NOSPAM_BYTES)
#define CHECKSUM_HEX_CHARS (2*CHECKSUM_BYTES)
#define TOXID_HEX_CHARS (2*TOX_ADDRESS_SIZE)

/**
* @class ToxId
Expand All @@ -51,19 +55,19 @@
* @brief The default constructor. Creates an empty Tox ID.
*/
ToxId::ToxId()
: publicKey(), noSpam(), checkSum()
: toxId()
{}

/**
* @brief The copy constructor.
* @param other ToxId to copy
*/
ToxId::ToxId(const ToxId &other)
: publicKey(other.publicKey), noSpam(other.noSpam), checkSum(other.checkSum)
: toxId(other.toxId)
{}

/**
* @brief Create a Tox ID from QString.
* @brief Create a Tox ID from a QString.
*
* If the given id is not a valid Tox ID, then:
* publicKey == id and noSpam == "" == checkSum.
Expand All @@ -74,13 +78,41 @@ ToxId::ToxId(const QString &id)
{
if (isToxId(id))
{
publicKey = id.left(TOX_ID_PUBLIC_KEY_LENGTH);
noSpam = id.mid(TOX_ID_PUBLIC_KEY_LENGTH, TOX_ID_NO_SPAM_LENGTH);
checkSum = id.right(TOX_ID_CHECKSUM_LENGTH);
toxId = QByteArray::fromHex(id.toLatin1());
}
else if(id.length() >= PUBLIC_KEY_HEX_CHARS)
{
toxId = QByteArray::fromHex(id.left(PUBLIC_KEY_HEX_CHARS).toLatin1());
}
else
{
publicKey = id;
toxId = QByteArray(TOX_ADDRESS_SIZE, 0x00); // invalid id string
}
}

/**
* @brief Create a Tox ID from a QByteArray.
*
* If the given id is not a valid Tox ID, then:
* publicKey == id and noSpam == "" == checkSum.
*
* @param id Tox ID string to convert to ToxId object
*/
ToxId::ToxId(const QByteArray &rawId)
{
if(rawId.length() == TOX_SECRET_KEY_SIZE)
{
toxId = QByteArray(rawId); // construct from PK only
}
else if (rawId.length() == TOX_ADDRESS_SIZE
&& isToxId(rawId.toHex().toUpper()))
{

toxId = QByteArray(rawId); // construct from full toxid
}
else
{
toxId = QByteArray(TOX_ADDRESS_SIZE, 0x00); // invalid id string
}
}

Expand All @@ -91,7 +123,7 @@ ToxId::ToxId(const QString &id)
*/
bool ToxId::operator==(const ToxId& other) const
{
return publicKey == other.publicKey;
return getPublicKey() == other.getPublicKey();
}

/**
Expand All @@ -101,7 +133,7 @@ bool ToxId::operator==(const ToxId& other) const
*/
bool ToxId::operator!=(const ToxId &other) const
{
return publicKey != other.publicKey;
return getPublicKey() != other.getPublicKey();
}

/**
Expand All @@ -110,17 +142,15 @@ bool ToxId::operator!=(const ToxId &other) const
*/
QString ToxId::toString() const
{
return publicKey + noSpam + checkSum;
return toxId.toHex().toUpper();
}

/**
* @brief Clears all elements of the Tox ID.
*/
void ToxId::clear()
{
publicKey.clear();
noSpam.clear();
checkSum.clear();
toxId.clear();
}

/**
Expand All @@ -132,7 +162,39 @@ void ToxId::clear()
bool ToxId::isValidToxId(const QString& id)
{
const QRegularExpression hexRegExp("^[A-Fa-f0-9]+$");
return id.length() == TOX_HEX_ID_LENGTH && id.contains(hexRegExp);
return id.length() == TOXID_HEX_CHARS && id.contains(hexRegExp);
}

/**
* @brief Gets the Public Key part of the ToxID
* @return Public Key of the ToxID
*/
QByteArray ToxId::getPublicKey() const
{
return toxId.mid(0, TOX_PUBLIC_KEY_SIZE);
}

/**
* @brief Returns the Public Key converted to QString.
* @return The Public Key as QString.
*/
QString ToxId::getPublicKeyString() const
{
return getPublicKey().toHex().toUpper();
}

/**
* @brief Returns the NoSpam value converted to QString.
* @return The NoSpam value as QString or "" if the ToxId was constructed from a Public Key.
*/
QString ToxId::getNoSpamString() const
{
if(toxId.length() == TOX_ADDRESS_SIZE)
{
return toxId.mid(TOX_PUBLIC_KEY_SIZE, NOSPAM_BYTES).toHex().toUpper();
}

return {};
}

/**
Expand All @@ -147,9 +209,9 @@ bool ToxId::isToxId(const QString& id)
return false;
}

uint32_t size = TOX_ID_PUBLIC_KEY_LENGTH + TOX_ID_NO_SPAM_LENGTH;
uint32_t size = PUBLIC_KEY_HEX_CHARS + NOSPAM_HEX_CHARS;
QString publicKeyStr = id.left(size);
QString checksumStr = id.right(TOX_ID_CHECKSUM_LENGTH);
QString checksumStr = id.right(CHECKSUM_HEX_CHARS);

QByteArray publicKey = QByteArray::fromHex(publicKeyStr.toLatin1());
QByteArray checksum = QByteArray::fromHex(checksumStr.toLatin1());
Expand Down
11 changes: 7 additions & 4 deletions src/core/toxid.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ToxId
ToxId();
ToxId(const ToxId& other);
explicit ToxId(const QString& id);
explicit ToxId(const QByteArray& rawId);

bool operator==(const ToxId& other) const;
bool operator!=(const ToxId& other) const;
Expand All @@ -37,11 +38,13 @@ class ToxId

static bool isToxId(const QString& id);
static bool isValidToxId(const QString &id);
QByteArray getToxId() const;
QByteArray getPublicKey() const;
QString getPublicKeyString() const;
QString getNoSpamString() const;

public:
QString publicKey;
QString noSpam;
QString checkSum;
private:
QByteArray toxId;
};

#endif // TOXID_H
4 changes: 2 additions & 2 deletions src/friend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Friend::Friend(uint32_t FriendId, const ToxId &UserId)

{
if (userName.size() == 0)
userName = UserId.publicKey;
userName = UserId.getPublicKeyString();

userAlias = Settings::getInstance().getFriendAlias(UserId);

Expand Down Expand Up @@ -65,7 +65,7 @@ void Friend::loadHistory()
void Friend::setName(QString name)
{
if (name.isEmpty())
name = userID.publicKey;
name = userID.getPublicKeyString();

userName = name;
if (userAlias.size() == 0)
Expand Down
6 changes: 3 additions & 3 deletions src/friendlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <QHash>

QHash<int, Friend*> FriendList::friendList;
QHash<QString, int> FriendList::tox2id;
QHash<QByteArray, int> FriendList::tox2id;

Friend* FriendList::addFriend(int friendId, const ToxId& userId)
{
Expand All @@ -35,7 +35,7 @@ Friend* FriendList::addFriend(int friendId, const ToxId& userId)

Friend* newfriend = new Friend(friendId, userId);
friendList[friendId] = newfriend;
tox2id[userId.publicKey] = friendId;
tox2id[userId.getPublicKey()] = friendId;

return newfriend;
}
Expand Down Expand Up @@ -69,7 +69,7 @@ void FriendList::clear()

Friend* FriendList::findFriend(const ToxId& userId)
{
auto id = tox2id.find(userId.publicKey);
auto id = tox2id.find(userId.getPublicKey());
if (id != tox2id.end())
{
Friend *f = findFriend(*id);
Expand Down
4 changes: 2 additions & 2 deletions src/friendlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
template <class T> class QList;
template <class A, class B> class QHash;
class Friend;
class QString;
class QByteArray;
class ToxId;

class FriendList
Expand All @@ -38,7 +38,7 @@ class FriendList

private:
static QHash<int, Friend*> friendList;
static QHash<QString, int> tox2id;
static QHash<QByteArray, int> tox2id;
};

#endif // FRIENDLIST_H
6 changes: 3 additions & 3 deletions src/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Group::~Group()
void Group::updatePeer(int peerId, QString name)
{
ToxId id = Core::getInstance()->getGroupPeerToxId(groupId, peerId);
QString toxid = id.publicKey;
QString toxid = id.getPublicKey();
peers[peerId] = name;
toxids[toxid] = name;

Expand Down Expand Up @@ -95,7 +95,7 @@ void Group::regeneratePeerList()
if (id == self)
selfPeerNum = i;

QString toxid = id.publicKey;
QString toxid = id.getPublicKey();
toxids[toxid] = peers[i];
if (toxids[toxid].isEmpty())
toxids[toxid] = tr("<Empty>", "Placeholder when someone's name in a group chat is empty");
Expand Down Expand Up @@ -170,7 +170,7 @@ int Group::getMentionedFlag() const

QString Group::resolveToxId(const ToxId &id) const
{
QString key = id.publicKey;
QString key = id.getPublicKeyString();
auto it = toxids.find(key);

if (it != toxids.end())
Expand Down
14 changes: 7 additions & 7 deletions src/persistence/profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ QString Profile::avatarPath(const QString& ownerId, bool forceUnencrypted)
return Settings::getInstance().getSettingsDirPath() + "avatars/" + ownerId + ".png";

QByteArray idData = ownerId.toUtf8();
QByteArray pubkeyData = core->getSelfId().publicKey.toUtf8();
QByteArray pubkeyData = core->getSelfId().getPublicKey();
constexpr int hashSize = TOX_PUBLIC_KEY_SIZE;
static_assert(hashSize >= crypto_generichash_BYTES_MIN
&& hashSize <= crypto_generichash_BYTES_MAX, "Hash size not supported by libsodium");
Expand All @@ -434,7 +434,7 @@ QString Profile::avatarPath(const QString& ownerId, bool forceUnencrypted)
*/
QPixmap Profile::loadAvatar()
{
return loadAvatar(core->getSelfId().publicKey);
return loadAvatar(core->getSelfId().getPublicKeyString());
}

/**
Expand Down Expand Up @@ -503,7 +503,7 @@ void Profile::loadDatabase(const QString& id)
return;
}

QByteArray salt = QByteArray::fromHex(ToxId {id}.publicKey.toUtf8());
QByteArray salt = ToxId {id}.getPublicKey();
if(salt.size() != TOX_PASS_SALT_LENGTH)
{
qWarning() << "Couldn't compute salt from public key" << name;
Expand Down Expand Up @@ -572,7 +572,7 @@ QByteArray Profile::getAvatarHash(const QString& ownerId)
*/
void Profile::removeAvatar()
{
removeAvatar(core->getSelfId().publicKey);
removeAvatar(core->getSelfId().getPublicKeyString());
}

/**
Expand Down Expand Up @@ -600,7 +600,7 @@ History* Profile::getHistory()
void Profile::removeAvatar(const QString& ownerId)
{
QFile::remove(avatarPath(ownerId));
if (ownerId == core->getSelfId().publicKey)
if (ownerId == core->getSelfId().getPublicKeyString())
core->setAvatar({});
}

Expand Down Expand Up @@ -789,7 +789,7 @@ void Profile::restartCore()
*/
void Profile::setPassword(const QString& newPassword)
{
QByteArray avatar = loadAvatarData(core->getSelfId().publicKey);
QByteArray avatar = loadAvatarData(core->getSelfId().getPublicKeyString());
QString oldPassword = password;
password = newPassword;
passkey = core->createPasskey(password);
Expand All @@ -801,7 +801,7 @@ void Profile::setPassword(const QString& newPassword)
}

Nexus::getDesktopGUI()->reloadHistory();
saveAvatar(avatar, core->getSelfId().publicKey);
saveAvatar(avatar, core->getSelfId().getPublicKeyString());

QVector<uint32_t> friendList = core->getFriendList();
QVectorIterator<uint32_t> i(friendList);
Expand Down
Loading

0 comments on commit 94ec561

Please sign in to comment.