diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/libwalletqt/Wallet.cpp | 194 | ||||
| -rw-r--r-- | src/libwalletqt/Wallet.h | 44 | ||||
| -rw-r--r-- | src/libwalletqt/WalletManager.cpp | 116 | ||||
| -rw-r--r-- | src/libwalletqt/WalletManager.h | 48 |
4 files changed, 402 insertions, 0 deletions
diff --git a/src/libwalletqt/Wallet.cpp b/src/libwalletqt/Wallet.cpp new file mode 100644 index 00000000..61998ab5 --- /dev/null +++ b/src/libwalletqt/Wallet.cpp @@ -0,0 +1,194 @@ +#include "Wallet.h" +#include <QFile> +#include <QDir> +#include <QDebug> +#include <QUrl> + +namespace { + QString TEST_SEED = "bound class paint gasp task soul forgot past pleasure physical circle " + " appear shore bathroom glove women crap busy beauty bliss idea give needle burden"; + + namespace { + bool createFileWrapper(const QString &filename) + { + QFile file(filename); + // qDebug("%s: about to create file: %s", __FUNCTION__, qPrintable(filename)); + bool result = file.open(QIODevice::WriteOnly); + if (!result ){ + qWarning("%s: error creating file '%s' : '%s'", + __FUNCTION__, + qPrintable(filename), + qPrintable(file.errorString())); + } + return result; + } + } + +} + +struct WalletImpl +{ + + QString basename() const; + void setBasename(const QString &name); + + QString keysName() const; + QString addressName() const; + +// Bitmonero::Wallet * m_walletImpl; + QString m_basename; + QString m_seed; + QString m_password; + QString m_language; + + static QString keysName(const QString &basename); + static QString addressName(const QString &basename); + +}; + + +QString WalletImpl::basename() const +{ + return m_basename; +} + +void WalletImpl::setBasename(const QString &name) +{ + m_basename = name; +} + +QString WalletImpl::keysName() const +{ + return keysName(m_basename); +} + +QString WalletImpl::addressName() const +{ + return addressName(m_basename); +} + +QString WalletImpl::keysName(const QString &basename) +{ + return basename + ".keys"; +} + +QString WalletImpl::addressName(const QString &basename) +{ + return basename + ".address.txt"; +} + + +Wallet::Wallet(QObject *parent) + : QObject(parent) +{ + +} + +QString Wallet::getSeed() const +{ + return m_pimpl->m_seed; +} + +QString Wallet::getSeedLanguage() const +{ + return "English"; +} + +//void Wallet::setSeedLaguage(const QString &lang) +//{ +// // TODO: call libwallet's appropriate method +//} + +bool Wallet::setPassword(const QString &password) +{ + // set/change password implies: + // recovery wallet with existing path, seed and lang + qDebug("%s: recovering wallet with path=%s, seed=%s, lang=%s and new password=%s", + __FUNCTION__, + qPrintable(this->getBasename()), + qPrintable(this->getSeed()), + qPrintable(this->getSeedLanguage()), + qPrintable(password)); + return true; +} + +QString Wallet::getPassword() const +{ + return m_pimpl->m_password; +} + +bool Wallet::rename(const QString &name) +{ + + QString dst = QUrl(name).toLocalFile(); + + if (dst.isEmpty()) + dst = name; + + qDebug("%s: renaming '%s' to '%s'", + __FUNCTION__, + qPrintable(m_pimpl->basename()), + qPrintable(dst)); + + QString walletKeysFile = m_pimpl->keysName(); + QString walletAddressFile = m_pimpl->addressName(); + + QString dstWalletKeysFile = WalletImpl::keysName(dst); + QString dstWalletAddressFile = WalletImpl::addressName(dst); + + QFile walletFile(this->getBasename()); + + if (!walletFile.rename(dst)) { + qWarning("Error renaming file: '%s' to '%s' : (%s)", + qPrintable(m_pimpl->basename()), + qPrintable(dst), + qPrintable(walletFile.errorString())); + return false; + } + QFile::rename(walletKeysFile, dstWalletKeysFile); + QFile::rename(walletAddressFile, dstWalletAddressFile); + + bool result = QFile::exists(dst) && QFile::exists(dstWalletKeysFile) + && QFile::exists(dstWalletAddressFile); + + if (result) { + m_pimpl->m_basename = dst; + } + + return result; +} + +QString Wallet::getBasename() const +{ + return m_pimpl->basename(); +} + +int Wallet::error() const +{ + return 0; +} + +QString Wallet::errorString() const +{ + return m_pimpl->m_seed; +} + +Wallet::Wallet(const QString &path, const QString &password, const QString &language) +{ + m_pimpl = new WalletImpl; + m_pimpl->m_basename = path; + m_pimpl->m_password = password; + m_pimpl->m_language = language; + m_pimpl->m_seed = TEST_SEED; + + // Create dummy files for testing + QFileInfo fi(path); + QDir tempDir; + tempDir.mkpath(fi.absolutePath()); + createFileWrapper(m_pimpl->basename()); + createFileWrapper(m_pimpl->keysName()); + createFileWrapper(m_pimpl->addressName()); +} + + + diff --git a/src/libwalletqt/Wallet.h b/src/libwalletqt/Wallet.h new file mode 100644 index 00000000..221d3d43 --- /dev/null +++ b/src/libwalletqt/Wallet.h @@ -0,0 +1,44 @@ +#ifndef WALLET_H +#define WALLET_H + +#include <QObject> + +struct WalletImpl; +class Wallet : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString seed READ getSeed) +public: + explicit Wallet(QObject *parent = 0); + + //! returns mnemonic seed + Q_INVOKABLE QString getSeed() const; + + //! returns seed language + Q_INVOKABLE QString getSeedLanguage() const; + + + //! changes the password using existing parameters (path, seed, seed lang) + Q_INVOKABLE bool setPassword(const QString &password); + //! returns curret wallet password + Q_INVOKABLE QString getPassword() const; + + //! renames/moves wallet files + Q_INVOKABLE bool rename(const QString &name); + + //! returns current wallet name (basename, as wallet consists of several files) + Q_INVOKABLE QString getBasename() const; + + Q_INVOKABLE int error() const; + Q_INVOKABLE QString errorString() const; + +private: + Wallet(const QString &path, const QString &password, const QString &language); + +private: + friend class WalletManager; + //! pimpl wrapper for libwallet; + WalletImpl * m_pimpl; +}; + +#endif // WALLET_H diff --git a/src/libwalletqt/WalletManager.cpp b/src/libwalletqt/WalletManager.cpp new file mode 100644 index 00000000..cb0ffbac --- /dev/null +++ b/src/libwalletqt/WalletManager.cpp @@ -0,0 +1,116 @@ +#include "WalletManager.h" +#include "Wallet.h" +#include "wallet/wallet2_api.h" +#include <QFile> +#include <QFileInfo> +#include <QDir> +#include <QDebug> +#include <QUrl> + +WalletManager * WalletManager::m_instance = nullptr; + + + + + +WalletManager *WalletManager::instance() +{ + if (!m_instance) { + m_instance = new WalletManager; + } + // Checking linkage (doesn't work, TODO: have every dependencies linked statically into libwallet) + Bitmonero::WalletManager * wallet_manager_impl = Bitmonero::WalletManagerFactory::getWalletManager(); + + return m_instance; +} + +Wallet *WalletManager::createWallet(const QString &path, const QString &password, + const QString &language) +{ + QFileInfo fi(path); + if (fi.exists()) { + qCritical("%s: already exists", __FUNCTION__); + // TODO: set error and error string + // return nullptr; + } + Wallet * wallet = new Wallet(path, password, language); + return wallet; +} + +Wallet *WalletManager::openWallet(const QString &path, const QString &language, const QString &password) +{ + QFileInfo fi(path); + if (fi.exists()) { + qCritical("%s: not exists", __FUNCTION__); + // TODO: set error and error string + // return nullptr; + } + // TODO: call the libwallet api here; + Wallet * wallet = new Wallet(path, password, language); + + return wallet; +} + +Wallet *WalletManager::recoveryWallet(const QString &path, const QString &memo, const QString &language) +{ + // TODO: call the libwallet api here; + + return nullptr; +} + +bool WalletManager::moveWallet(const QString &src, const QString &dst_) +{ + // TODO: move this to libwallet; + QFile walletFile(src); + if (!walletFile.exists()) { + qWarning("%s: source file [%s] doesn't exits", __FUNCTION__, + qPrintable(src)); + return false; + } + QString dst = QUrl(dst_).toLocalFile(); + QString walletKeysFile = src + ".keys"; + QString walletAddressFile = src + ".address.txt"; + + QString dstWalletKeysFile = dst + ".keys"; + QString dstWalletAddressFile = dst + ".address.txt"; + + if (!walletFile.rename(dst)) { + qWarning("Error renaming file: '%s' to '%s' : (%s)", + qPrintable(src), + qPrintable(dst), + qPrintable(walletFile.errorString())); + return false; + } + QFile::rename(walletKeysFile, dstWalletKeysFile); + QFile::rename(walletAddressFile, dstWalletAddressFile); + + return QFile::exists(dst) && QFile::exists(dstWalletKeysFile) + && QFile::exists(dstWalletAddressFile); +} + +void WalletManager::closeWallet(Wallet *wallet) +{ + delete wallet; +} + +QString WalletManager::walletLanguage(const QString &locale) +{ + return "English"; +} + +int WalletManager::error() const +{ + return 0; +} + +QString WalletManager::errorString() const +{ + return tr("Unknown error"); +} + +WalletManager::WalletManager(QObject *parent) : QObject(parent) +{ + +} + + diff --git a/src/libwalletqt/WalletManager.h b/src/libwalletqt/WalletManager.h new file mode 100644 index 00000000..231f104c --- /dev/null +++ b/src/libwalletqt/WalletManager.h @@ -0,0 +1,48 @@ +#ifndef WALLETMANAGER_H +#define WALLETMANAGER_H + +#include <QObject> + +class Wallet; + +class WalletManager : public QObject +{ + Q_OBJECT +public: + static WalletManager * instance(); + // wizard: createWallet path; + Q_INVOKABLE Wallet * createWallet(const QString &path, const QString &password, + const QString &language); + // just for future use + Q_INVOKABLE Wallet * openWallet(const QString &path, const QString &language, + const QString &password); + + // wizard: recoveryWallet path; hint: internally it recorvers wallet and set password = "" + Q_INVOKABLE Wallet * recoveryWallet(const QString &path, const QString &memo, + const QString &language); + + // wizard: both "create" and "recovery" paths. + // TODO: probably move it to "Wallet" interface + Q_INVOKABLE bool moveWallet(const QString &src, const QString &dst); + + //! utils: close wallet to free memory + Q_INVOKABLE void closeWallet(Wallet * wallet); + + //! returns libwallet language name for given locale + Q_INVOKABLE QString walletLanguage(const QString &locale); + + //! returns last error happened in WalletManager + Q_INVOKABLE int error() const; + + //! returns error description in human language + Q_INVOKABLE QString errorString() const; +signals: + +public slots: + +private: + explicit WalletManager(QObject *parent = 0); + static WalletManager * m_instance; +}; + +#endif // WALLETMANAGER_H |
