account list fix. existing configs fix

This commit is contained in:
Sergio Chouhy 2026-06-16 21:02:54 -03:00
parent bd36ed9002
commit a724a8ece5
6 changed files with 54 additions and 30 deletions

View File

@ -1,5 +1,5 @@
#include "LEZWalletAccountModel.h"
#include <QJsonObject>
#include <QVariantMap>
LEZWalletAccountModel::LEZWalletAccountModel(QObject* parent)
: QAbstractListModel(parent)
@ -38,23 +38,23 @@ QHash<int, QByteArray> LEZWalletAccountModel::roleNames() const
};
}
void LEZWalletAccountModel::replaceFromJsonArray(const QJsonArray& arr)
void LEZWalletAccountModel::replaceFromVariantList(const QVariantList& list)
{
beginResetModel();
int oldCount = m_entries.size();
m_entries.clear();
for (const QJsonValue& v : arr) {
for (const QVariant& v : list) {
LEZWalletAccountEntry e;
e.balance = QString();
if (v.isObject()) {
const QJsonObject obj = v.toObject();
e.accountId = obj.value(QStringLiteral("account_id")).toString();
e.isPublic = obj.value(QStringLiteral("is_public")).toBool(true);
e.name = QString();
if (v.type() == QVariant::Map) {
const QVariantMap map = v.toMap();
e.accountId = map.value(QStringLiteral("account_id")).toString();
e.isPublic = map.value(QStringLiteral("is_public"), true).toBool();
} else {
e.accountId = v.toString();
e.isPublic = true;
}
e.name = QString();
m_entries.append(e);
}
endResetModel();

View File

@ -1,7 +1,7 @@
#pragma once
#include <QAbstractListModel>
#include <QJsonArray>
#include <QVariant>
#include <QString>
struct LEZWalletAccountEntry {
@ -29,7 +29,7 @@ public:
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override;
void replaceFromJsonArray(const QJsonArray& arr);
void replaceFromVariantList(const QVariantList& list);
void setBalanceByAccountId(const QString& accountId, const QString& balance);
int count() const { return m_entries.size(); }

View File

@ -8,7 +8,6 @@
#include <QFile>
#include <QFileInfo>
#include <QGuiApplication>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QSettings>
@ -127,22 +126,27 @@ void LEZWalletBackend::openIfPathsConfigured()
int err = m_logos->logos_execution_zone.open(configPath(), storagePath());
if (err == WALLET_FFI_SUCCESS) {
qDebug() << "LEZWalletBackend: wallet opened successfully";
setIsWalletOpen(true);
QJsonArray arr = m_logos->logos_execution_zone.list_accounts();
m_accountModel->replaceFromJsonArray(arr);
fetchAndUpdateBlockHeights();
startChunkedSync();
refreshSequencerAddr();
finishOpeningWallet();
} else {
qWarning() << "LEZWalletBackend: failed to open wallet, error" << err
<< "config:" << configPath() << "storage:" << storagePath();
}
}
void LEZWalletBackend::finishOpeningWallet()
{
setIsWalletOpen(true);
QVariantList arr = m_logos->logos_execution_zone.list_accounts();
m_accountModel->replaceFromVariantList(arr);
fetchAndUpdateBlockHeights();
startChunkedSync();
refreshSequencerAddr();
}
void LEZWalletBackend::refreshAccounts()
{
QJsonArray arr = m_logos->logos_execution_zone.list_accounts();
m_accountModel->replaceFromJsonArray(arr);
QVariantList arr = m_logos->logos_execution_zone.list_accounts();
m_accountModel->replaceFromVariantList(arr);
fetchAndUpdateBlockHeights();
if (!m_syncing)
startChunkedSync();
@ -356,23 +360,39 @@ void LEZWalletBackend::applySequencerAddrToConfig(const QString& configPath, con
file.write(QJsonDocument(obj).toJson(QJsonDocument::Indented));
}
bool LEZWalletBackend::createNew(QString configPath, QString storagePath, QString password, QString sequencerAddr)
QString LEZWalletBackend::createNew(QString configPath, QString storagePath, QString password, QString sequencerAddr)
{
const QString localConfigPath = toLocalPath(configPath);
const QString localStoragePath = toLocalPath(storagePath);
// Both files already on disk: this is most likely an existing wallet the
// user pointed us at (e.g. from the setup screen), not a request to
// overwrite it. Try to load it instead of blindly creating a new one.
if (QFile::exists(localConfigPath) && QFile::exists(localStoragePath)) {
int err = m_logos->logos_execution_zone.open(localConfigPath, localStoragePath);
if (err != WALLET_FFI_SUCCESS) {
return QStringLiteral(
"Could not load the wallet at the selected paths. Pick "
"different existing config/storage files, or choose paths "
"that don't exist yet to create a new wallet there.");
}
persistConfigPath(localConfigPath);
persistStoragePath(localStoragePath);
finishOpeningWallet();
return QString();
}
if (!sequencerAddr.isEmpty())
applySequencerAddrToConfig(localConfigPath, sequencerAddr);
int err = m_logos->logos_execution_zone.create_new(localConfigPath, localStoragePath, password);
if (err != WALLET_FFI_SUCCESS) return false;
if (err != WALLET_FFI_SUCCESS)
return QStringLiteral("Failed to create wallet. Check paths and try again.");
persistConfigPath(localConfigPath);
persistStoragePath(localStoragePath);
setIsWalletOpen(true);
refreshAccounts();
refreshSequencerAddr();
return true;
finishOpeningWallet();
return QString();
}
void LEZWalletBackend::copyToClipboard(QString text)

View File

@ -45,7 +45,7 @@ public slots:
QString transferShielded(QString fromHex, QString toKeysJson, QString amountStr) override;
QString transferShieldedOwned(QString fromHex, QString toHex, QString amountStr) override;
QString transferDeshielded(QString fromHex, QString toHex, QString amountStr) override;
bool createNew(QString configPath, QString storagePath, QString password, QString sequencerAddr) override;
QString createNew(QString configPath, QString storagePath, QString password, QString sequencerAddr) override;
void copyToClipboard(QString text) override;
private slots:
@ -62,6 +62,7 @@ private:
void refreshSequencerAddr();
void saveWallet();
void openIfPathsConfigured();
void finishOpeningWallet();
bool m_syncing = false;
quint64 m_syncTarget = 0;

View File

@ -23,7 +23,7 @@ class LEZWalletBackend
SLOT(QString transferShieldedOwned(QString fromHex, QString toHex, QString amountStr))
SLOT(QString transferDeshielded(QString fromHex, QString toHex, QString amountStr))
SLOT(bool createNew(QString configPath, QString storagePath, QString password, QString sequencerAddr))
SLOT(QString createNew(QString configPath, QString storagePath, QString password, QString sequencerAddr))
SLOT(void copyToClipboard(QString text))
}

View File

@ -118,10 +118,13 @@ Rectangle {
configPath: backend ? backend.configPath : ""
onCreateWallet: function(configPath, storagePath, password, sequencerUrl) {
if (!backend) return
// createNew() returns an empty string on success, or a
// human-readable error message (e.g. existing files at
// the chosen paths failed to load) otherwise.
logos.watch(backend.createNew(configPath, storagePath, password, sequencerUrl),
function(ok) {
if (!ok)
createError = qsTr("Failed to create wallet. Check paths and try again.")
function(errorMessage) {
if (errorMessage)
createError = errorMessage
},
function(error) {
createError = qsTr("Error creating wallet: %1").arg(error)