diff --git a/libraries/networking/src/AccountManager.cpp b/libraries/networking/src/AccountManager.cpp index 7b49aebfe9..a8b88133d6 100644 --- a/libraries/networking/src/AccountManager.cpp +++ b/libraries/networking/src/AccountManager.cpp @@ -19,9 +19,11 @@ #include #include #include +#include #include "NodeList.h" #include "PacketHeaders.h" +#include "RSAKeypairGenerator.h" #include "AccountManager.h" @@ -417,6 +419,7 @@ void AccountManager::requestAccessTokenFinished() { } requestProfile(); + generateNewKeypair(); } } else { // TODO: error handling @@ -478,3 +481,39 @@ void AccountManager::requestProfileError(QNetworkReply::NetworkError error) { // TODO: error handling qDebug() << "AccountManager requestProfileError - " << error; } + +void AccountManager::generateNewKeypair() { + // setup a new QThread to generate the keypair on, in case it takes a while + QThread* generateThread = new QThread(this); + + // setup a keypair generator + RSAKeypairGenerator* keypairGenerator = new RSAKeypairGenerator(); + + connect(generateThread, &QThread::started, keypairGenerator, &RSAKeypairGenerator::generateKeypair); + connect(keypairGenerator, &RSAKeypairGenerator::generatedKeypair, this, &AccountManager::processGeneratedKeypair); + connect(keypairGenerator, &RSAKeypairGenerator::errorGeneratingKeypair, + this, &AccountManager::handleKeypairGenerationError); + connect(keypairGenerator, &QObject::destroyed, generateThread, &QThread::quit); + connect(generateThread, &QThread::finished, generateThread, &QThread::deleteLater); + + keypairGenerator->moveToThread(generateThread); + + qDebug() << "Starting worker thread to generate 2048-bit RSA key-pair."; + generateThread->start(); +} + +void AccountManager::processGeneratedKeypair(const QByteArray& publicKey, const QByteArray& privateKey) { + + qDebug() << "Generated 2048-bit RSA key-pair. Storing private key and uploading public key."; + + // set the private key on our data-server account info + _accountInfo.setPrivateKey(privateKey); + + // get rid of the keypair generator now that we don't need it anymore + sender()->deleteLater(); +} + +void AccountManager::handleKeypairGenerationError() { + // for now there isn't anything we do with this except get the worker thread to clean up + sender()->deleteLater(); +} diff --git a/libraries/networking/src/AccountManager.h b/libraries/networking/src/AccountManager.h index 3628b5a865..1dc0506338 100644 --- a/libraries/networking/src/AccountManager.h +++ b/libraries/networking/src/AccountManager.h @@ -91,6 +91,8 @@ signals: void balanceChanged(qint64 newBalance); private slots: void processReply(); + void handleKeypairGenerationError(); + void processGeneratedKeypair(const QByteArray& publicKey, const QByteArray& privateKey); private: AccountManager(); AccountManager(AccountManager const& other); // not implemented @@ -98,6 +100,8 @@ private: void passSuccessToCallback(QNetworkReply* reply); void passErrorToCallback(QNetworkReply* reply); + + void generateNewKeypair(); Q_INVOKABLE void invokedRequest(const QString& path, bool requiresAuthentication, diff --git a/libraries/networking/src/DataServerAccountInfo.h b/libraries/networking/src/DataServerAccountInfo.h index ae7699584b..8dc321bb02 100644 --- a/libraries/networking/src/DataServerAccountInfo.h +++ b/libraries/networking/src/DataServerAccountInfo.h @@ -41,6 +41,8 @@ public: const QUuid& getWalletID() const { return _walletID; } void setWalletID(const QUuid& walletID); + + void setPrivateKey(const QByteArray& privateKey) { _privateKey = privateKey; } qint64 getBalance() const { return _balance; } float getBalanceInSatoshis() const { return _balance / SATOSHIS_PER_CREDIT; } diff --git a/libraries/networking/src/RSAKeypairGenerator.cpp b/libraries/networking/src/RSAKeypairGenerator.cpp index ae61ac3eda..91a7fe8df6 100644 --- a/libraries/networking/src/RSAKeypairGenerator.cpp +++ b/libraries/networking/src/RSAKeypairGenerator.cpp @@ -17,6 +17,12 @@ #include "RSAKeypairGenerator.h" +RSAKeypairGenerator::RSAKeypairGenerator(QObject* parent) : + QObject(parent) +{ + +} + void RSAKeypairGenerator::generateKeypair() { RSA* keyPair = RSA_new(); diff --git a/libraries/networking/src/RSAKeypairGenerator.h b/libraries/networking/src/RSAKeypairGenerator.h index f98daddb1e..dd90313625 100644 --- a/libraries/networking/src/RSAKeypairGenerator.h +++ b/libraries/networking/src/RSAKeypairGenerator.h @@ -16,6 +16,8 @@ class RSAKeypairGenerator : public QObject { Q_OBJECT +public: + RSAKeypairGenerator(QObject* parent = 0); public slots: void generateKeypair(); signals: