From a724a8ece572138c0b1124b8ab92ba5814e9b7bd Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Tue, 16 Jun 2026 21:02:54 -0300 Subject: [PATCH] account list fix. existing configs fix --- src/LEZWalletAccountModel.cpp | 16 ++++----- src/LEZWalletAccountModel.h | 4 +-- src/LEZWalletBackend.cpp | 50 ++++++++++++++++++++--------- src/LEZWalletBackend.h | 3 +- src/LEZWalletBackend.rep | 2 +- src/qml/ExecutionZoneWalletView.qml | 9 ++++-- 6 files changed, 54 insertions(+), 30 deletions(-) diff --git a/src/LEZWalletAccountModel.cpp b/src/LEZWalletAccountModel.cpp index b9df171..fd1b584 100644 --- a/src/LEZWalletAccountModel.cpp +++ b/src/LEZWalletAccountModel.cpp @@ -1,5 +1,5 @@ #include "LEZWalletAccountModel.h" -#include +#include LEZWalletAccountModel::LEZWalletAccountModel(QObject* parent) : QAbstractListModel(parent) @@ -38,23 +38,23 @@ QHash 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(); diff --git a/src/LEZWalletAccountModel.h b/src/LEZWalletAccountModel.h index 8efb086..0f59039 100644 --- a/src/LEZWalletAccountModel.h +++ b/src/LEZWalletAccountModel.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include struct LEZWalletAccountEntry { @@ -29,7 +29,7 @@ public: QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; QHash 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(); } diff --git a/src/LEZWalletBackend.cpp b/src/LEZWalletBackend.cpp index 5ed2c17..d10f301 100644 --- a/src/LEZWalletBackend.cpp +++ b/src/LEZWalletBackend.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -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) diff --git a/src/LEZWalletBackend.h b/src/LEZWalletBackend.h index 7706d8d..f886de9 100644 --- a/src/LEZWalletBackend.h +++ b/src/LEZWalletBackend.h @@ -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; diff --git a/src/LEZWalletBackend.rep b/src/LEZWalletBackend.rep index 376f3e4..568ccfa 100644 --- a/src/LEZWalletBackend.rep +++ b/src/LEZWalletBackend.rep @@ -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)) } diff --git a/src/qml/ExecutionZoneWalletView.qml b/src/qml/ExecutionZoneWalletView.qml index 1b46a66..9164d83 100644 --- a/src/qml/ExecutionZoneWalletView.qml +++ b/src/qml/ExecutionZoneWalletView.qml @@ -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)