From 99f98a91c31dcf9dca874a502077d9f7e14d6bdf Mon Sep 17 00:00:00 2001 From: June Egbert Date: Tue, 26 Mar 2024 00:09:16 +0100 Subject: [PATCH] qt: added verification of minimal json in the configure name dialog box --- src/names/applications.cpp | 33 ++++++++++++++++++++++++++ src/names/applications.h | 4 ++++ src/qt/configurenamedialog.cpp | 35 +++++++++++++++++++++++++--- src/test/name_applications_tests.cpp | 13 +++++++++++ 4 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/names/applications.cpp b/src/names/applications.cpp index 9e06243c29..b3aa94cfa5 100644 --- a/src/names/applications.cpp +++ b/src/names/applications.cpp @@ -10,6 +10,8 @@ #include +#include + namespace { @@ -183,4 +185,35 @@ IsValidJSONOrEmptyString (const std::string& text){ UniValue v; return text.empty() || v.read(text); +} + +bool +IsMinimalJSONOrEmptyString (const std::string& text){ + UniValue v; + if(text.empty()){ + return true; + } + + if(!v.read(text)){ + return false; + } + + const std::string minimalJSON = GetMinimalJSON(text); + + bool isMinimal = (text == minimalJSON); + + if(!isMinimal){ + LogDebug(BCLog::NAMES, "Minimalised JSON string is: %s \n", minimalJSON); + } + + return isMinimal; +} + +std::string +GetMinimalJSON (const std::string& text){ + UniValue v; + + v.read(text); + + return v.write(0,0); } diff --git a/src/names/applications.h b/src/names/applications.h index d50f5b4c0f..525d90f182 100644 --- a/src/names/applications.h +++ b/src/names/applications.h @@ -25,4 +25,8 @@ std::string DescFromName (const valtype& name, NameNamespace ns); bool IsValidJSONOrEmptyString (const std::string& text); +bool IsMinimalJSONOrEmptyString (const std::string& text); + +std::string GetMinimalJSON (const std::string& text); + #endif // H_BITCOIN_NAMES_APPLICATIONS diff --git a/src/qt/configurenamedialog.cpp b/src/qt/configurenamedialog.cpp index 0a166d0b6b..9931200770 100644 --- a/src/qt/configurenamedialog.cpp +++ b/src/qt/configurenamedialog.cpp @@ -72,6 +72,33 @@ void ConfigureNameDialog::accept() if(MessageBoxInvalidJSON == QMessageBox::Ok){ QDialog::accept(); } + + } else if (!IsMinimalJSONOrEmptyString(data)) { + QMessageBox MessageBoxNonMinimalJSON; + MessageBoxNonMinimalJSON.setIcon(QMessageBox::Warning); + MessageBoxNonMinimalJSON.setWindowTitle(tr("Non-minimal JSON")); + MessageBoxNonMinimalJSON.setText(tr("Are you sure you want to continue anyway? The inputted JSON data is non-minimal, and therefore will waste space as well as incurring added transaction costs when written on the blockchain.")); + MessageBoxNonMinimalJSON.addButton(QMessageBox::Ok); + MessageBoxNonMinimalJSON.addButton(QMessageBox::Cancel); + MessageBoxNonMinimalJSON.addButton(tr("Minimalise JSON"), QMessageBox::ActionRole); + + MessageBoxNonMinimalJSON.exec(); + + QMessageBox::ButtonRole reply = MessageBoxNonMinimalJSON.buttonRole(MessageBoxNonMinimalJSON.clickedButton()); + + if(reply == QMessageBox::AcceptRole){ + QDialog::accept(); + } else if(reply == QMessageBox::ActionRole){ + + std::string minimalJSONData = GetMinimalJSON(data); + ui->dataEdit->setText(QString::fromStdString(minimalJSONData)); + + returnData = QString::fromStdString(minimalJSONData); + data = minimalJSONData; + + QDialog::accept(); + } + } else { QDialog::accept(); } @@ -114,9 +141,11 @@ void ConfigureNameDialog::onDataEdited(const QString &name) ui->dataSize->resize(ui->dataSize->fontMetrics().horizontalAdvance(ui->dataSize->text()), ui->dataSize->height()); std::string data = ui->dataEdit->text().toStdString(); - - if(IsValidJSONOrEmptyString(data)){ - ui->labelValidJSON->setText(tr("Valid JSON text.")); + + if(IsMinimalJSONOrEmptyString(data)){ + ui->labelValidJSON->setText(tr("Valid and minimal JSON data.")); + } else if(IsValidJSONOrEmptyString(data)){ + ui->labelValidJSON->setText(tr("JSON data is not minimal.")); } else { ui->labelValidJSON->setText(tr("JSON data inputted is invalid.")); } diff --git a/src/test/name_applications_tests.cpp b/src/test/name_applications_tests.cpp index 1cb2fbd081..72cb7bc7d7 100644 --- a/src/test/name_applications_tests.cpp +++ b/src/test/name_applications_tests.cpp @@ -128,4 +128,17 @@ BOOST_AUTO_TEST_CASE( valid_json ) BOOST_CHECK_EQUAL(IsValidJSONOrEmptyString(""), true); } +BOOST_AUTO_TEST_CASE( minimal_json ) +{ + BOOST_CHECK_EQUAL(IsMinimalJSONOrEmptyString("{\"bar\":[1,2,3]}"), true); + + BOOST_CHECK_EQUAL(IsMinimalJSONOrEmptyString("{\"bar\": [1,2,3]}"), false); + + BOOST_CHECK_EQUAL(IsMinimalJSONOrEmptyString("{\foo:"), false); + + BOOST_CHECK_EQUAL(IsMinimalJSONOrEmptyString(""), true); + + BOOST_CHECK_EQUAL(IsMinimalJSONOrEmptyString("{\"bar\":[1, 2, 3]}"), false); +} + BOOST_AUTO_TEST_SUITE_END()