1. Connect signals to make status of swap reflected in AtomicSwap dialog
     2. Add informational tabs to AtomicSwap dialog
     4. Add recovery to atomic widget
     5. Refactor AtomicWidget so AtomicSwap handles parsing of swap binary output.
TODO:
     3. Add cancel and refund functionality to AtomicSwap when things go wrong
This commit is contained in:
twiddle 2024-08-04 22:49:31 -04:00
parent 06c43a707e
commit c67fedf9bc
3 changed files with 63 additions and 47 deletions

View file

@ -7,12 +7,13 @@
#include "AtomicSwap.h"
#include <utility>
#include <QJsonParseError>
#include "ui_AtomicSwap.h"
#include "AtomicWidget.h"
AtomicSwap::AtomicSwap(QWidget *parent) :
WindowModalDialog(parent), ui(new Ui::AtomicSwap), fundDialog( new AtomicFundDialog(this)) {
WindowModalDialog(parent), ui(new Ui::AtomicSwap), fundDialog( new AtomicFundDialog(this)), procList(new QList<QSharedPointer<QProcess>>()) {
ui->setupUi(this);
//ui->debug_log->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
ui->label_status->setTextInteractionFlags(Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse);
@ -26,10 +27,61 @@ AtomicSwap::AtomicSwap(QWidget *parent) :
connect(ui->btn_cancel, &QPushButton::clicked, this, &AtomicSwap::cancel);
}
void AtomicSwap::runSwap(QStringList arguments){
auto *swap = new QProcess();
procList->append(QSharedPointer<QProcess>(swap));
swap->setProcessChannelMode(QProcess::MergedChannels);
swap->setReadChannel(QProcess::StandardOutput);
connect(swap, &QProcess::readyRead,this, [this, swap] {
//Refactor and move this to a slot in atomicswap, move fund dialog to be part of atomic swap
while(swap->canReadLine()){
QJsonParseError err;
const QByteArray& rawline = swap->readLine();
QJsonDocument line = QJsonDocument::fromJson(rawline, &err);
qDebug() << rawline;
bool check;
if (line["fields"]["message"].toString().contains("Connected to Alice")){
qDebug() << "Successfully connected";
this->logLine(line["fields"].toString());
} else if (!line["fields"]["deposit_address"].toString().isEmpty()){
qDebug() << "Deposit to btc to segwit address";
QString address = line["fields"]["deposit_address"].toString();
fundDialog = new AtomicFundDialog(this, "Deposit BTC to this address", address);
//dialog->setModal(true);
fundDialog->show();
} else if (line["fields"]["message"].toString().startsWith("Received Bitcoin")){
this->updateStatus(line["fields"]["new_balance"].toString().split(" ")[0] + " BTC received, starting swap");
this->setSwap(line["span"]["swap_id"].toString());
fundDialog->close();
qDebug() << "Spawn atomic swap progress dialog";
this->show();
} else if ( QString confs = line["fields"]["seen_confirmations"].toString(); !confs.isEmpty()){
qDebug() << "Updating xmrconfs " + confs;
this->updateXMRConf(confs.toInt());
} else if (QString message = line["fields"]["message"].toString(); !QString::compare(message, "Bitcoin transaction status changed")){
qDebug() << "Updating btconfs " + line["fields"]["new_status"].toString().split(" ")[2];
this->updateBTCConf(line["fields"]["new_status"].toString().split(" ")[2].toInt());
}
//Insert line conditionals here
}
});
swap->start(Config::instance()->get(Config::swapPath).toString(),arguments);
qDebug() << "process started";
}
AtomicSwap::~AtomicSwap() {
delete ui;
emit cleanProcs();
for (const auto& proc : *procList){
proc->kill();
}
if(QString::compare("WINDOWS",Config::instance()->get(Config::operatingSystem).toString()) != 0) {
qDebug() << "Closing monero-wallet-rpc";
(new QProcess)->start("kill", QStringList{"-f", Config::defaultConfigDir().absolutePath() +
"/mainnet/monero/monero-wallet-rpc"});
(new QProcess)->start("kill", QStringList{"-f", Config::defaultConfigDir().absolutePath() +
"/testnet/monero/monero-wallet-rpc"});
}
}
void AtomicSwap::logLine(QString line){
//ui->debug_log->setText(ui->debug_log->toPlainText().append(QTime::currentTime().toString() + ":" + line));

View file

@ -7,6 +7,7 @@
#include <QDialog>
#include <QTime>
#include <QProcess>
#include "components.h"
#include "AtomicFundDialog.h"
@ -27,12 +28,15 @@ public:
void updateXMRConf(int confs);
void setTitle(QString title);
void setSwap(QString swapId);
public slots:
void runSwap(QStringList swap);
signals:
void cleanProcs();
private:
Ui::AtomicSwap *ui;
QString id;
AtomicFundDialog fundDialog;
AtomicFundDialog* fundDialog;
QList<QSharedPointer<QProcess>>* procList;
int btc_confs;
void cancel();

View file

@ -88,7 +88,7 @@ AtomicWidget::AtomicWidget(QWidget *parent)
m_instance->set(Config::rendezVous,copy);
}
});
connect(swapDialog,&AtomicSwap::cleanProcs, this, [this]{clean();});
//Remove after testing
//QVariant var;
@ -136,7 +136,7 @@ void AtomicWidget::runSwap(const QString& seller, const QString& btcChange, cons
arguments << "78YnzFTp3UUMgtKuAJCP2STcbxRZPDPveJ5YGgfg5doiPahS9suWF1r3JhKqjM1McYBJvu8nhkXExGfXVkU6n5S6AXrg4KP";
//arguments << xmrReceive;
arguments << "--seller";
arguments << "/ip4/127.0.0.1/tcp/9939/p2p/12D3KooWQA4fXDYLNXgxPsVZmnR8kh2wwHUQnkH9e1Wjc8KyJ7p8";
arguments << "/ip4/127.0.0.1/tcp/9939/p2p/12D3KooW9yDFYojXnZRdqS9UXcfP2amgwoYdSjujwWdRw4LTSdWw";
// Remove after testing
arguments << "--electrum-rpc";
arguments << "tcp://127.0.0.1:50001";
@ -148,48 +148,7 @@ void AtomicWidget::runSwap(const QString& seller, const QString& btcChange, cons
//arguments << seller;
arguments << "--tor-socks5-port";
arguments << m_instance->get(Config::socks5Port).toString();
auto *swap = new QProcess();
procList->append(QSharedPointer<QProcess>(swap));
swap->setProcessChannelMode(QProcess::MergedChannels);
swap->setReadChannel(QProcess::StandardOutput);
connect(swap, &QProcess::readyRead,this, [this, swap] {
//Refactor and move this to a slot in atomicswap, move fund dialog to be part of atomic swap
while(swap->canReadLine()){
QJsonParseError err;
const QByteArray& rawline = swap->readLine();
QJsonDocument line = QJsonDocument::fromJson(rawline, &err);
qDebug() << rawline;
bool check;
if (line["fields"]["message"].toString().contains("Connected to Alice")){
qDebug() << "Successfully connected";
swapDialog->logLine(line["fields"].toString());
} else if (!line["fields"]["deposit_address"].toString().isEmpty()){
qDebug() << "Deposit to btc to segwit address";
QString address = line["fields"]["deposit_address"].toString();
fundDialog = new AtomicFundDialog(this, "Deposit BTC to this address", address);
//dialog->setModal(true);
fundDialog->show();
} else if (line["fields"]["message"].toString().startsWith("Received Bitcoin")){
swapDialog->updateStatus(line["fields"]["new_balance"].toString().split(" ")[0] + " BTC received, starting swap");
swapDialog->setSwap(line["span"]["swap_id"].toString());
fundDialog->close();
qDebug() << "Spawn atomic swap progress dialog";
showAtomicSwapDialog();
} else if ( QString confs = line["fields"]["seen_confirmations"].toString(); !confs.isEmpty()){
qDebug() << "Updating xmrconfs " + confs;
swapDialog->updateXMRConf(confs.toInt());
} else if (QString message = line["fields"]["message"].toString(); !QString::compare(message, "Bitcoin transaction status changed")){
qDebug() << "Updating btconfs " + line["fields"]["new_status"].toString().split(" ")[2];
swapDialog->updateBTCConf(line["fields"]["new_status"].toString().split(" ")[2].toInt());
}
//Insert line conditionals here
}
});
swap->start(m_instance->get(Config::swapPath).toString(),arguments);
qDebug() << "process started";
swapDialog->runSwap(arguments);
}
@ -253,6 +212,7 @@ void AtomicWidget::list(const QString& rendezvous) {
AtomicWidget::~AtomicWidget() {
qDebug()<< "Exiting widget!!";
delete swapDialog;
delete o_model;
delete offerList;
clean();