From fd6a779f371b2f0077083f5109b6c2cacf692da8 Mon Sep 17 00:00:00 2001 From: levoncrypto Date: Mon, 11 Nov 2024 09:23:01 +0400 Subject: [PATCH] Add additional checks for memo --- src/qt/sendcoinsentry.cpp | 12 ++++++++++-- src/spark/primitives.h | 10 +++++++++- src/spark/sparkwallet.cpp | 4 ++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/qt/sendcoinsentry.cpp b/src/qt/sendcoinsentry.cpp index 4697423868..26bd96b33c 100644 --- a/src/qt/sendcoinsentry.cpp +++ b/src/qt/sendcoinsentry.cpp @@ -11,6 +11,7 @@ #include "optionsmodel.h" #include "platformstyle.h" #include "walletmodel.h" +#include "../spark/sparkwallet.h" #include "../wallet/wallet.h" #include @@ -70,15 +71,22 @@ SendCoinsEntry::~SendCoinsEntry() void SendCoinsEntry::on_MemoTextChanged(const QString &text) { - int maxLength = 256; + const spark::Params* params = spark::Params::get_default(); + int maxLength = params->get_memo_bytes(); bool isOverLimit = text.length() > maxLength; if (isOverLimit) { - ui->messageWarning->setText("Message exceeds character 256 character limit"); + ui->messageWarning->setText(QString("Message exceeds %1 bytes limit").arg(maxLength)); ui->messageWarning->setVisible(true); ui->messageTextLabel->setStyleSheet("border: 1px solid red;"); ui->iconMessageWarning->setVisible(true); } else { + QString sanitized = text; + sanitized.remove(QRegExp("[\\x00-\\x1F\\x7F]")); + if (sanitized != text) { + ui->messageTextLabel->setText(sanitized); + return; + } ui->messageWarning->clear(); ui->messageWarning->setVisible(false); ui->messageTextLabel->setStyleSheet(""); diff --git a/src/spark/primitives.h b/src/spark/primitives.h index 09de4550eb..2bfa0c72bb 100644 --- a/src/spark/primitives.h +++ b/src/spark/primitives.h @@ -107,7 +107,15 @@ class CSparkOutputTx inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(address); READWRITE(amount); - READWRITE(memo); + if (ser_action.ForRead()) { + if (!s.empty()) { + READWRITE(memo); + } else { + memo = ""; + } + } else { + READWRITE(memo); + } } }; diff --git a/src/spark/sparkwallet.cpp b/src/spark/sparkwallet.cpp index d99a3f4810..9e5e0a49e8 100644 --- a/src/spark/sparkwallet.cpp +++ b/src/spark/sparkwallet.cpp @@ -743,6 +743,10 @@ std::vector CSparkWallet::CreateSparkMintRecipients( unsigned char network = spark::GetNetworkType(); std::string addr = outputs[i].address.encode(network); std::string memo = outputs[i].memo; + const std::size_t max_memo_size = outputs[i].address.get_params()->get_memo_bytes(); + if (memo.length() > max_memo_size) { + throw std::runtime_error(strprintf("Memo exceeds maximum length of %d bytes", max_memo_size)); + } CRecipient recipient = {script, CAmount(outputs[i].v), false, addr, memo}; results.emplace_back(recipient); }