Skip to content

Commit

Permalink
FIXUP: Refactor hex/ascii conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyRand authored and Jeremy Rand committed Jun 13, 2022
1 parent aa90ebd commit d03f7bc
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 25 deletions.
41 changes: 33 additions & 8 deletions src/qt/buynamespage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <logging.h>
#include <qt/configurenamedialog.h>
#include <qt/guiutil.h>
#include <qt/nametablemodel.h>
#include <qt/platformstyle.h>
#include <qt/walletmodel.h>
#include <rpc/protocol.h>
Expand Down Expand Up @@ -113,7 +114,20 @@ QString BuyNamesPage::name_available(const QString &name) const
LogPrint(BCLog::QT, "wallet attempting name_show: name=%s\n", strName);

UniValue params(UniValue::VOBJ);
params.pushKV ("name", strName);

try
{
const QString hexName = NameTableModel::asciiToHex(name);
params.pushKV ("name", hexName.toStdString());
}
catch (const InvalidNameString& exc)
{
return tr ("Name was invalid ASCII.");
}

UniValue options(UniValue::VOBJ);
options.pushKV ("nameEncoding", "hex");
params.pushKV ("options", options);

const std::string walletURI = "/wallet/" + walletModel->getWalletName().toStdString();

Expand Down Expand Up @@ -147,20 +161,31 @@ QString BuyNamesPage::firstupdate(const QString &name, const std::optional<QStri

UniValue params(UniValue::VOBJ);

valtype vtName = valtype (strName.begin (), strName.end ());
std::string hexName = EncodeName (vtName, NameEncoding::HEX);
params.pushKV ("name", hexName);
try
{
const QString hexName = NameTableModel::asciiToHex(name);
params.pushKV ("name", hexName.toStdString());
}
catch (const InvalidNameString& exc)
{
return tr ("Name was invalid ASCII.");
}

UniValue options(UniValue::VOBJ);
options.pushKV ("nameEncoding", "hex");

if (value)
{
std::string strValue = value.value().toStdString();
valtype vtValue = valtype (strValue.begin (), strValue.end ());
std::string hexValue = EncodeName (vtValue, NameEncoding::HEX);
try
{
const QString hexValue = NameTableModel::asciiToHex(value.value());
params.pushKV ("value", hexValue.toStdString());
}
catch (const InvalidNameString& exc)
{
return tr ("Value was invalid ASCII.");
}

params.pushKV ("value", hexValue);
options.pushKV ("valueEncoding", "hex");
}

Expand Down
94 changes: 77 additions & 17 deletions src/qt/nametablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,26 @@ class NameTablePriv
}

const std::string hexName = maybeName.get_str();
const valtype vtName = DecodeName(hexName, NameEncoding::HEX);
const std::string name = std::string (vtName.begin (), vtName.end ());
std::string name;
try
{
name = NameTableModel::hexToAscii(QString::fromStdString(hexName)).toStdString();
}
catch (const InvalidNameString& exc)
{
continue;
}

const std::string hexData = maybeData.get_str();
const valtype vtData = DecodeName(hexData, NameEncoding::HEX);
const std::string data = std::string (vtData.begin (), vtData.end ());
std::string data;
try
{
data = NameTableModel::hexToAscii(QString::fromStdString(hexData)).toStdString();
}
catch (const InvalidNameString& exc)
{
continue;
}

const int height = find_value ( v, "height").getInt<int>();
const int expiresIn = find_value ( v, "expires_in").getInt<int>();
Expand Down Expand Up @@ -165,12 +179,26 @@ class NameTablePriv
}

const std::string hexName = maybeName.get_str();
const valtype vtName = DecodeName(hexName, NameEncoding::HEX);
const std::string name = std::string (vtName.begin (), vtName.end ());
std::string name;
try
{
name = NameTableModel::hexToAscii(QString::fromStdString(hexName)).toStdString();
}
catch (const InvalidNameString& exc)
{
continue;
}

const std::string hexData = maybeData.get_str();
const valtype vtData = DecodeName(hexData, NameEncoding::HEX);
const std::string data = std::string (vtData.begin (), vtData.end ());
std::string data;
try
{
data = NameTableModel::hexToAscii(QString::fromStdString(hexData)).toStdString();
}
catch (const InvalidNameString& exc)
{
continue;
}

const bool isMine = find_value ( v, "ismine").get_bool();
const std::string op = find_value ( v, "op").get_str();
Expand Down Expand Up @@ -506,27 +534,59 @@ NameTableModel::emitDataChanged(int idx)
dataChanged(index(idx, 0), index(idx, columns.length()-1));
}

QString NameTableModel::asciiToHex(const QString &ascii)
{
const std::string strAscii = ascii.toStdString();
const valtype vt = DecodeName (strAscii, NameEncoding::ASCII);

const std::string strHex = EncodeName (vt, NameEncoding::HEX);
const QString hex = QString::fromStdString(strHex);

return hex;
}

QString NameTableModel::hexToAscii(const QString &hex)
{
const std::string strHex = hex.toStdString();
const valtype vt = DecodeName (strHex, NameEncoding::HEX);

const std::string strAscii = EncodeName (vt, NameEncoding::ASCII);
const QString ascii = QString::fromStdString(strAscii);

return ascii;
}

QString NameTableModel::update(const QString &name, const std::optional<QString> &value, const std::optional<QString> &transferTo) const
{
const std::string strName = name.toStdString();
LogPrint(BCLog::QT, "wallet attempting name_update: name=%s\n", strName);
LogPrint(BCLog::QT, "wallet attempting name_update: name=%s\n", name.toStdString());

UniValue params(UniValue::VOBJ);

valtype vtName = valtype (strName.begin (), strName.end ());
std::string hexName = EncodeName (vtName, NameEncoding::HEX);
params.pushKV ("name", hexName);
try
{
const QString hexName = NameTableModel::asciiToHex(name);
params.pushKV ("name", hexName.toStdString());
}
catch (const InvalidNameString& exc)
{
return tr ("Name was invalid ASCII.");
}

UniValue options(UniValue::VOBJ);
options.pushKV ("nameEncoding", "hex");

if (value)
{
std::string strValue = value.value().toStdString();
valtype vtValue = valtype (strValue.begin (), strValue.end ());
std::string hexValue = EncodeName (vtValue, NameEncoding::HEX);
try
{
const QString hexValue = NameTableModel::asciiToHex(value.value());
params.pushKV ("value", hexValue.toStdString());
}
catch (const InvalidNameString& exc)
{
return tr ("Value was invalid ASCII.");
}

params.pushKV ("value", hexValue);
options.pushKV ("valueEncoding", "hex");
}

Expand Down
3 changes: 3 additions & 0 deletions src/qt/nametablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class NameTableModel : public QAbstractTableModel
Qt::ItemFlags flags(const QModelIndex &index) const;
/*@}*/

static QString asciiToHex(const QString &ascii);
static QString hexToAscii(const QString &hex);

QString update(const QString &name, const std::optional<QString> &value, const std::optional<QString> &transferTo) const;
QString renew(const QString &name) const;

Expand Down

0 comments on commit d03f7bc

Please sign in to comment.