Skip to content

Commit

Permalink
server selection now migrated from API to wallet, wallets communicate…
Browse files Browse the repository at this point in the history
… directly with servers
  • Loading branch information
proletesseract committed Dec 30, 2015
1 parent 6c56829 commit 122f529
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 104 deletions.
2 changes: 1 addition & 1 deletion NavajoCoin-qt.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.4.1, 2015-12-19T18:39:08. -->
<!-- Written by QtCreator 3.4.1, 2015-12-29T18:49:36. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
4 changes: 2 additions & 2 deletions src/qt/overviewpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ void OverviewPage::getMarketData()


//bter
/*

QUrl bterUrl;
bterUrl.setUrl("http://data.bter.com/api/1/ticker/NAV_BTC");
static QNetworkRequest bterReq(bterUrl);
bterReq.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
*/


// connect only first time
if (bPrepare1st)
Expand Down
Binary file modified src/qt/res/images/splash.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
141 changes: 114 additions & 27 deletions src/qt/sendcoinsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#include <QtGui>
#include <QDebug>

#include <algorithm>
#include <iterator>

#include <openssl/aes.h>
#include <QSslSocket>

Expand Down Expand Up @@ -140,40 +143,86 @@ SendCoinsDialog::~SendCoinsDialog()
delete ui;
}

void SendCoinsDialog::apiRequest(QNetworkReply *reply){
bool SendCoinsDialog::chooseServer(QJsonArray anonServers, QString localHash)
{
int max = anonServers.size();

if(serversTried.size() >= max) {
QMessageBox::warning(this, tr("Anonymous Transaction"),
tr("Unable to contact a verified server, please try again later."),
QMessageBox::Ok, QMessageBox::Ok);
return false;
}

QString rawReply = reply->readAll();
//@TODO: refactor the random selection to be more efficient, eg. making array of possibilites then removing them once tried.

QJsonDocument jsonDoc = QJsonDocument::fromJson(rawReply.toUtf8());
int randomNumber = qrand() % max;

QJsonObject jsonObject = jsonDoc.object();
bool tried = false;
if(serversTried.size() > 0){
for (int i = 0; i < serversTried.size(); i++){
if(serversTried.at(i) == randomNumber) {
tried = true;
}
}
}

QString type = jsonObject["type"].toString();
if(tried == false) {
QJsonObject randomAnon = anonServers.at(randomNumber).toObject();
bool success = this->testServer(randomAnon["server"].toString(), localHash);

if(type == "SUCCESS"){
if(success == false) {
serversTried.push_back(randomNumber);
return this->chooseServer(anonServers, localHash);
}else{
return true;
}

QString address = jsonObject["address"].toString();
QString publicKey = jsonObject["public_key"].toString();
QString minAmount = jsonObject["min_amount"].toString();
QString maxAmount = jsonObject["max_amount"].toString();
QString txFee = jsonObject["transaction_fee"].toString();
}else{
return this->chooseServer(anonServers, localHash);
}
}

bool SendCoinsDialog::testServer(QString serverAddress, QString localHash)
{

QSslSocket *socket = new QSslSocket(this);
socket->setPeerVerifyMode(socket->VerifyNone);

this->sendCoins(address);
socket->connectToHostEncrypted(serverAddress, 443);

}else{
QMessageBox::warning(this, tr("Anonymous Transaction"),
tr("We were unable to locate an active Anonymous node, please try again later."),
QMessageBox::Ok, QMessageBox::Ok);
if(!socket->waitForEncrypted()){
qDebug() << socket->errorString();
return false;
}

}
QString reqString = QString("POST /api/check-node HTTP/1.1\r\n" \
"Host: %1\r\n" \
"Content-Type: application/x-www-form-urlencoded\r\n" \
"Connection: Close\r\n\r\n").arg(serverAddress);

void SendCoinsDialog::sslRequest()
{
qDebug() << "sslRequest";
socket->write(reqString.toUtf8());

while (socket->waitForReadyRead()){

while(socket->canReadLine()){
QString line = socket->readLine();
}

QString rawReply = socket->readAll();
QJsonDocument jsonDoc = QJsonDocument::fromJson(rawReply.toUtf8());
QJsonObject jsonObject = jsonDoc.object();
QString type = jsonObject["type"].toString();
QJsonObject jsonData = jsonObject["data"].toObject();
QString serverHash = jsonData["hash"].toString();

if(type == "SUCCESS" && serverHash == localHash) {
selectedServer = jsonData;
return true;
} else {
return false;
}
}
}

void SendCoinsDialog::on_sendButton_clicked()
Expand Down Expand Up @@ -308,9 +357,40 @@ void SendCoinsDialog::on_sendButton_clicked()
this->sendCoins(node);
}else{


QString anonFileContents;
QString anonFilePath = QString("%1%2%3").arg(GetDefaultDataDir().string().c_str()).arg(QDir::separator()).arg("anon.dat");
QFile anonFile(anonFilePath);
anonFile.open(QIODevice::ReadOnly | QIODevice::Text);
anonFileContents = anonFile.readAll();
anonFile.close();

QJsonDocument anonJsonDoc = QJsonDocument::fromJson(anonFileContents.toUtf8());
QJsonObject anonJsonObject = anonJsonDoc.object();
QJsonArray anonServers = anonJsonObject["servers"].toArray();
QString localHash = anonJsonObject["hash"].toString();
int max = anonServers.size();

if(max <= 0) {
QMessageBox::warning(this, tr("Anonymous Transaction"),
tr("Your server list appears to be empty, please wait for peer network sync."),
QMessageBox::Ok, QMessageBox::Ok);
return;
}

serversTried.clear();

bool serverReady = this->chooseServer(anonServers, localHash);

qDebug() << serverReady;
qDebug() << selectedServer;

/*
* @TODO: wire this back together once sever is selected
*
QSslSocket *socket = new QSslSocket(this);
socket->setPeerVerifyMode(socket->VerifyNone);
//connect(socket, SIGNAL(encrypted()), this, SLOT(sslRequest()));
socket->connectToHostEncrypted("api.navajocoin.org", 443);
Expand Down Expand Up @@ -344,12 +424,17 @@ void SendCoinsDialog::on_sendButton_clicked()
int contentLength = qAddress.length() + 8;
QString reqString = QString("POST /api/select-incoming-node HTTP/1.1\r\n" \
"Host: api.navajocoin.org\r\n" \
qDebug() << randomAnon["server"].toString();
QString reqString = QString("POST /api/check-node HTTP/1.1\r\n" \
"Host: %1\r\n" \
"Content-Type: application/x-www-form-urlencoded\r\n" \
"Content-Length: %1\r\n" \
"Content-Length: %2\r\n" \
"Connection: Close\r\n\r\n" \
"address=%2\r\n").arg(contentLength).arg(qAddress);
"address=%3\r\n").arg(randomAnon["server"].toString()).arg(contentLength).arg(qAddress);
socket->write(reqString.toUtf8());
Expand All @@ -365,7 +450,7 @@ void SendCoinsDialog::on_sendButton_clicked()
QJsonObject jsonObject = jsonDoc.object();
QString type = jsonObject["type"].toString();
//qDebug() << rawReply;
qDebug() << rawReply;
if(type == "SUCCESS"){
Expand All @@ -391,6 +476,8 @@ void SendCoinsDialog::on_sendButton_clicked()
}else{
qDebug() << "NOT SUCCESS";
qDebug() << jsonObject;
QMessageBox::warning(this, tr("Anonymous Transaction"),
tr("We were unable to locate an active Anonymous node, please try again later."),
QMessageBox::Ok, QMessageBox::Ok);
Expand All @@ -400,7 +487,7 @@ void SendCoinsDialog::on_sendButton_clicked()
}//wait for ready read
}//no socket error

*/

}//else

Expand Down
11 changes: 8 additions & 3 deletions src/qt/sendcoinsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

#include <QDialog>
#include <QString>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QNetworkReply>
#include <vector>

namespace Ui {
class SendCoinsDialog;
Expand Down Expand Up @@ -48,6 +52,8 @@ public slots:
bool fNewRecipientAllowed;
double minAmount;
double maxAmount;
std::vector<int> serversTried;
QJsonObject selectedServer;

protected:
void sendCoins(QString anonNode);
Expand All @@ -69,9 +75,8 @@ private slots:
void coinControlClipboardPriority();
void coinControlClipboardLowOutput();
void coinControlClipboardChange();
void apiRequest(QNetworkReply *reply);
void sslRequest();

bool chooseServer(QJsonArray anonServers, QString localHash);
bool testServer(QString serverAddress, QString localHash);
};

#endif // SENDCOINSDIALOG_H
71 changes: 0 additions & 71 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,79 +192,8 @@ WalletModel::SendCoinsReturn WalletModel::sendCoins(const QString &txcomment, co
return MaxAmount;
}

//TODO encrypt the tx-comment

//string to ecrypt: rcp.address.toStdString();

/*
char pubKey[] = "-----BEGIN PUBLIC KEY-----\n"\
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDS6KKqBKKCNxclmY/la1P8gGMc\n"\
"o4hr5KKD/IeXGQmLiKeUhF0lX35S/jbG7AsS5LkS4cw3CHqvA+s6jUkQ7zv936yB\n"\
"HxLCuflg+E4T5I9lnyIfbk/fw3LAh1NBSiXefddiABYgzibJKRoeCB+BG+bn+ixE\n"\
"cR4HuyaCCKyPoft6WQIDAQAB\n"\
"-----END PUBLIC KEY-----\n";
//QByteArray pubKeyBA = publicKey.toUtf8();
qDebug() << publicKey;
qDebug() << rcp.address;
char pubKey[2048];
memcpy(pubKey, publicKey.toStdString().c_str(), publicKey.size());
char plainText[2048/8];
memcpy(plainText, rcp.address.toStdString().c_str(), rcp.address.size());
//char plainText[2048/8] = "This is some test text";
unsigned char encrypted[1024] = {};
int padding = RSA_PKCS1_PADDING;
RSA *rsa = NULL;
BIO *keybio;
keybio = BIO_new_mem_buf(pubKey, -1);
BUF_MEM *bptr;
BIO_get_mem_ptr(keybio, &bptr);
if(keybio == NULL){
qDebug() << "Failed to create key BIO";
}
rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL);
if(rsa == NULL){
qDebug() << "rsa is null";
}
int result = RSA_public_encrypt(strlen(plainText), plainText, encrypted, rsa, padding);
qDebug() << result;
qDebug() << encrypted;
QString qEnc = QString(encrypted);
QString q64;
QByteArray ba;
ba.append(qEnc);
q64 = ba.toBase64();
qDebug() << q64;
strTxComment = q64.toStdString();
//qDebug() << strTxComment;
*/

strTxComment = txComment.toStdString();

//qDebug() << txComment;

//return OK;

}
}

Expand Down

0 comments on commit 122f529

Please sign in to comment.