mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
Implementing creating a toxme address
This commit is contained in:
parent
217ca20085
commit
1883b4e25e
13
src/core.cpp
13
src/core.cpp
|
@ -1107,6 +1107,19 @@ QString Core::getIDString() const
|
||||||
// 16^n > 10^10 (which is roughly the planet's population)
|
// 16^n > 10^10 (which is roughly the planet's population)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QPair<QByteArray, QByteArray> Core::getKeypair() const
|
||||||
|
{
|
||||||
|
QPair<QByteArray, QByteArray> keypair;
|
||||||
|
if (!tox)
|
||||||
|
return keypair;
|
||||||
|
|
||||||
|
char buf[2*TOX_PUBLIC_KEY_SIZE];
|
||||||
|
tox_get_keys(tox, (uint8_t*)buf, (uint8_t*)buf+TOX_PUBLIC_KEY_SIZE);
|
||||||
|
keypair.first = QByteArray(buf, TOX_PUBLIC_KEY_SIZE);
|
||||||
|
keypair.second = QByteArray(buf+TOX_PUBLIC_KEY_SIZE, TOX_PUBLIC_KEY_SIZE);
|
||||||
|
return keypair;
|
||||||
|
}
|
||||||
|
|
||||||
QString Core::getStatusMessage() const
|
QString Core::getStatusMessage() const
|
||||||
{
|
{
|
||||||
QString sname;
|
QString sname;
|
||||||
|
|
|
@ -72,6 +72,7 @@ public:
|
||||||
QString getUsername() const; ///< Returns our username, or an empty string on failure
|
QString getUsername() const; ///< Returns our username, or an empty string on failure
|
||||||
QString getStatusMessage() const; ///< Returns our status message, or an empty string on failure
|
QString getStatusMessage() const; ///< Returns our status message, or an empty string on failure
|
||||||
ToxID getSelfId() const; ///< Returns our Tox ID
|
ToxID getSelfId() const; ///< Returns our Tox ID
|
||||||
|
QPair<QByteArray, QByteArray> getKeypair() const; ///< Returns our public and private keys
|
||||||
|
|
||||||
VideoSource* getVideoSourceFromCall(int callNumber); ///< Get a call's video source
|
VideoSource* getVideoSourceFromCall(int callNumber); ///< Get a call's video source
|
||||||
|
|
||||||
|
|
|
@ -185,10 +185,6 @@ int main(int argc, char *argv[])
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Run
|
// Run
|
||||||
Toxme::lookup("tux3@toxme.se");
|
|
||||||
Toxme::createAddress(ToxID::fromString("95B6B3A34C82FC449D1F1CA9A7F621DE6A7516D0AA1183E128A5467590BD8913B8C5D287D924"),
|
|
||||||
"testToxmeAPI@toxme.se",false,"This is a test");
|
|
||||||
|
|
||||||
a.setQuitOnLastWindowClosed(false);
|
a.setQuitOnLastWindowClosed(false);
|
||||||
int errorcode = a.exec();
|
int errorcode = a.exec();
|
||||||
|
|
||||||
|
|
|
@ -1,39 +1,16 @@
|
||||||
#include "toxme.h"
|
#include "toxme.h"
|
||||||
|
#include "core.h"
|
||||||
#include <QNetworkAccessManager>
|
#include <QNetworkAccessManager>
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <sodium/crypto_box.h>
|
#include <sodium/crypto_box.h>
|
||||||
|
#include <sodium/randombytes.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
const QString Toxme::apiUrl{"https://toxme.se/api"};
|
const QString Toxme::apiUrl{"https://toxme.se/api"};
|
||||||
|
|
||||||
void Toxme::incrementNonce(unsigned char nonce[])
|
const unsigned char Toxme::pinnedPk[] = {0x5D, 0x72, 0xC5, 0x17, 0xDF, 0x6A, 0xEC, 0x54, 0xF1, 0xE9, 0x77, 0xA6, 0xB6, 0xF2, 0x59, 0x14,
|
||||||
{
|
0xEA, 0x4C, 0xF7, 0x27, 0x7A, 0x85, 0x02, 0x7C, 0xD9, 0xF5, 0x19, 0x6D, 0xF1, 0x7E, 0x0B, 0x13};
|
||||||
auto nonceSize = crypto_box_NONCEBYTES;
|
|
||||||
for (decltype(nonceSize) i=0; i<nonceSize; ++i)
|
|
||||||
{
|
|
||||||
if (nonce[i]==0xFFU)
|
|
||||||
{
|
|
||||||
if (i==nonceSize-1)
|
|
||||||
{
|
|
||||||
// Shouldn't matter, since we don't reuse keys
|
|
||||||
// But still gets a warning because that's exceedingly unlikely to happen
|
|
||||||
qWarning() << "Toxme::incrementNonce: Nonce overflow!";
|
|
||||||
memset(nonce,0,nonceSize);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
nonce[i]=0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
nonce[i]++;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QByteArray Toxme::makeJsonRequest(QString json)
|
QByteArray Toxme::makeJsonRequest(QString json)
|
||||||
{
|
{
|
||||||
|
@ -50,30 +27,29 @@ QByteArray Toxme::makeJsonRequest(QString json)
|
||||||
|
|
||||||
QByteArray Toxme::prepareEncryptedJson(int action, QString payload)
|
QByteArray Toxme::prepareEncryptedJson(int action, QString payload)
|
||||||
{
|
{
|
||||||
static unsigned char nonce[crypto_box_NONCEBYTES]={0};
|
QPair<QByteArray, QByteArray> keypair = Core::getInstance()->getKeypair();
|
||||||
|
if (keypair.first.isEmpty() || keypair.second.isEmpty())
|
||||||
|
{
|
||||||
|
qWarning() << "Toxme::prepareEncryptedJson: Couldn't get our keypair, aborting";
|
||||||
|
return QByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
unsigned char pk[crypto_box_PUBLICKEYBYTES];
|
QByteArray nonce(crypto_box_NONCEBYTES, 0);
|
||||||
unsigned char sk[crypto_box_SECRETKEYBYTES];
|
randombytes((uint8_t*)nonce.data(), crypto_box_NONCEBYTES);
|
||||||
crypto_box_keypair(pk,sk);
|
|
||||||
|
|
||||||
QByteArray payloadData = payload.toUtf8();
|
QByteArray payloadData = payload.toUtf8();
|
||||||
const size_t mlen = crypto_box_ZEROBYTES+payloadData.size();
|
const size_t cypherlen = crypto_box_MACBYTES+payloadData.size();
|
||||||
unsigned char* payloadMsg = new unsigned char[mlen];
|
unsigned char* payloadEnc = new unsigned char[cypherlen];
|
||||||
unsigned char* payloadEnc = new unsigned char[mlen];
|
|
||||||
memcpy(payloadMsg+crypto_box_ZEROBYTES,payloadData.data(),payloadData.size());
|
|
||||||
|
|
||||||
crypto_box(payloadEnc,payloadMsg,mlen,nonce,pk,sk);
|
crypto_box_easy(payloadEnc,(uint8_t*)payloadData.data(),payloadData.size(),
|
||||||
QByteArray payloadEncData(reinterpret_cast<char*>(payloadEnc), mlen);
|
(uint8_t*)nonce.data(),pinnedPk,(uint8_t*)keypair.second.data());
|
||||||
delete[] payloadMsg;
|
QByteArray payloadEncData(reinterpret_cast<char*>(payloadEnc), cypherlen);
|
||||||
delete[] payloadEnc;
|
delete[] payloadEnc;
|
||||||
|
|
||||||
const QString json{"{\"action\":"+QString().setNum(action)+","
|
const QString json{"{\"action\":"+QString().setNum(action)+","
|
||||||
"\"public_key\":\""+QByteArray(reinterpret_cast<char*>(pk),
|
"\"public_key\":\""+keypair.first.toHex()+"\","
|
||||||
crypto_box_PUBLICKEYBYTES)+"\","
|
"\"encrypted\":\""+payloadEncData.toBase64()+"\","
|
||||||
"\"encrypted\":\""+payloadEncData+"\","
|
"\"nonce\":\""+nonce.toBase64()+"\"}"};
|
||||||
"\"nonce\":\""+QByteArray(reinterpret_cast<char*>(nonce),
|
|
||||||
crypto_box_NONCEBYTES)+"\"}"};
|
|
||||||
incrementNonce(nonce);
|
|
||||||
return json.toUtf8();
|
return json.toUtf8();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,6 +83,28 @@ ToxID Toxme::lookup(QString address)
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Toxme::extractError(QString json)
|
||||||
|
{
|
||||||
|
static const QByteArray pattern{"c\":"};
|
||||||
|
|
||||||
|
json = json.remove(' ');
|
||||||
|
const int index = json.indexOf(pattern);
|
||||||
|
if (index == -1)
|
||||||
|
return INT_MIN;
|
||||||
|
json = json.mid(index+pattern.size());
|
||||||
|
|
||||||
|
const int end = json.indexOf('}');
|
||||||
|
if (end == -1)
|
||||||
|
return INT_MIN;
|
||||||
|
json.truncate(end);
|
||||||
|
|
||||||
|
bool ok;
|
||||||
|
int r = json.toInt(&ok);
|
||||||
|
if (!ok)
|
||||||
|
return INT_MIN;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
bool Toxme::createAddress(ToxID id, QString address,
|
bool Toxme::createAddress(ToxID id, QString address,
|
||||||
bool keepPrivate, QString bio)
|
bool keepPrivate, QString bio)
|
||||||
{
|
{
|
||||||
|
@ -124,8 +122,6 @@ bool Toxme::createAddress(ToxID id, QString address,
|
||||||
"\"timestamp\":"+QString().setNum(time(0))+"}"};
|
"\"timestamp\":"+QString().setNum(time(0))+"}"};
|
||||||
|
|
||||||
QByteArray response = makeJsonRequest(prepareEncryptedJson(1,payload));
|
QByteArray response = makeJsonRequest(prepareEncryptedJson(1,payload));
|
||||||
qDebug() << "payload:"<<payload;
|
|
||||||
qDebug() << "response:"<<response;
|
|
||||||
|
|
||||||
return true;
|
return (extractError(response) == 0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QMutex>
|
#include <QMutex>
|
||||||
|
#include <memory>
|
||||||
#include "corestructs.h"
|
#include "corestructs.h"
|
||||||
|
|
||||||
class QNetworkAccessManager;
|
class QNetworkAccessManager;
|
||||||
|
@ -25,10 +26,11 @@ private:
|
||||||
Toxme()=delete;
|
Toxme()=delete;
|
||||||
static QByteArray makeJsonRequest(QString json);
|
static QByteArray makeJsonRequest(QString json);
|
||||||
static QByteArray prepareEncryptedJson(int action, QString payload);
|
static QByteArray prepareEncryptedJson(int action, QString payload);
|
||||||
static void incrementNonce(unsigned char nonce[]);
|
static int extractError(QString json);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static const QString apiUrl;
|
static const QString apiUrl;
|
||||||
|
static const unsigned char pinnedPk[];
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // TOXME_H
|
#endif // TOXME_H
|
||||||
|
|
Loading…
Reference in New Issue
Block a user