From c320d4376e7c042a3b8404f865a3875c77cbe650 Mon Sep 17 00:00:00 2001 From: Eric Lombrozo Date: Tue, 9 May 2017 23:26:12 -0700 Subject: [PATCH] Added support for old p2sh address types if the coin is transitioning. --- build-all.sh | 8 ++++++++ mSIGNA-ltc.pro | 2 +- src/accountmodel.cpp | 16 +++++++++++++--- src/accountmodel.h | 3 +++ src/coinparams.cpp | 14 ++++++++++++++ src/coinparams.h | 5 +++++ src/createtxdialog.cpp | 4 ++++ src/mainwindow.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- src/mainwindow.h | 17 +++++++++++++++++ src/scriptmodel.cpp | 15 ++++++++++++++- src/scriptmodel.h | 4 ++++ src/txmodel.cpp | 20 ++++++++++++++------ src/txmodel.h | 2 ++ 13 files changed, 140 insertions(+), 12 deletions(-) diff --git a/build-all.sh b/build-all.sh index 1f9521ac..5e595a79 100755 --- a/build-all.sh +++ b/build-all.sh @@ -161,6 +161,7 @@ fi cd $CURRENT_DIR +#TODO: Automatically detect whether the following units need recompilation # Always recompile coinparams if [[ -e build/$BUILD_TYPE/obj/coinparams.o ]] then @@ -179,6 +180,13 @@ then rm build/$BUILD_TYPE/obj/main.o fi +# The following units also need to be recompiled +# accountmodel.o +# createtxdialog.o +# mainwindow.o +# scriptmodel.o +# txmodel.o + # For OS X, remove any existing instance of the app bundle if [[ "$OS" == "osx" ]] then diff --git a/mSIGNA-ltc.pro b/mSIGNA-ltc.pro index cec5c783..69613c9d 100644 --- a/mSIGNA-ltc.pro +++ b/mSIGNA-ltc.pro @@ -12,7 +12,7 @@ RC_FILE = res/mSIGNA_ltc.rc ICON = res/icons/app_icons/osx-ltc.icns DEFINES += QT_GUI BOOST_THREAD_USE_LIB BOOST_SPIRIT_THREADSAFE DATABASE_SQLITE -DEFINES += DEFAULT_NETWORK_LITECOIN +DEFINES += DEFAULT_NETWORK_LITECOIN SUPPORT_OLD_ADDRESS_VERSIONS CONFIG += c++11 rtti thread QT += widgets network diff --git a/src/accountmodel.cpp b/src/accountmodel.cpp index 9a15d4b6..36dff1d5 100644 --- a/src/accountmodel.cpp +++ b/src/accountmodel.cpp @@ -37,14 +37,22 @@ using namespace std; AccountModel::AccountModel(CoinDB::SynchedVault& synchedVault) : m_synchedVault(synchedVault), numAccounts(0) +{ + setBase58Versions(); + currencySymbol = getCurrencySymbol(); + setColumns(); +} + +void AccountModel::setBase58Versions() { base58_versions[0] = getCoinParams().pay_to_pubkey_hash_version(); +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS + base58_versions[1] = getUseOldAddressVersions() ? getCoinParams().old_pay_to_script_hash_version() : getCoinParams().pay_to_script_hash_version(); +#else base58_versions[1] = getCoinParams().pay_to_script_hash_version(); +#endif base58_versions[2] = getCoinParams().pay_to_witness_pubkey_hash_version(); base58_versions[3] = getCoinParams().pay_to_witness_script_hash_version(); - - currencySymbol = getCurrencySymbol(); - setColumns(); } void AccountModel::setColumns() @@ -63,6 +71,8 @@ void AccountModel::setVault(CoinDB::Vault* vault) */ void AccountModel::update() { + setBase58Versions(); + QString newCurrencySymbol = getCurrencySymbol(); if (newCurrencySymbol != currencySymbol) { diff --git a/src/accountmodel.h b/src/accountmodel.h index f4b65f87..b1a5ae6c 100644 --- a/src/accountmodel.h +++ b/src/accountmodel.h @@ -52,6 +52,9 @@ class AccountModel : public QStandardItemModel static const unsigned int DEFAULT_LOOKAHEAD = 25; + // UI operations + void setBase58Versions(); + // Account operations void newAccount(const QString& name, unsigned int minsigs, const QList& keychainNames, qint64 msecsSinceEpoch = QDateTime::currentDateTime().toMSecsSinceEpoch(), unsigned int unusedPoolSize = DEFAULT_LOOKAHEAD); void newAccount(bool enableSegwit, bool useSegwitP2SH, const QString& name, unsigned int minsigs, const QList& keychainNames, qint64 msecsSinceEpoch = QDateTime::currentDateTime().toMSecsSinceEpoch(), unsigned int unusedPoolSize = DEFAULT_LOOKAHEAD); diff --git a/src/coinparams.cpp b/src/coinparams.cpp index 4e738692..1993869a 100644 --- a/src/coinparams.cpp +++ b/src/coinparams.cpp @@ -129,3 +129,17 @@ uint64_t getDefaultFee() { return getCoinParams().default_fee(); } + +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS +static bool useOldAddressVersions = false; + +void setAddressVersions(bool useOld) +{ + useOldAddressVersions = useOld; +} + +bool getUseOldAddressVersions() +{ + return useOldAddressVersions; +} +#endif diff --git a/src/coinparams.h b/src/coinparams.h index bdb4ba6d..101b9e73 100644 --- a/src/coinparams.h +++ b/src/coinparams.h @@ -36,3 +36,8 @@ const QString& getCurrencySymbol(); int getCurrencyDecimals(); uint64_t getCurrencyMax(); uint64_t getDefaultFee(); + +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS +void setAddressVersions(bool useOld = false); +bool getUseOldAddressVersions(); +#endif diff --git a/src/createtxdialog.cpp b/src/createtxdialog.cpp index 4fa907e2..73f3e26c 100644 --- a/src/createtxdialog.cpp +++ b/src/createtxdialog.cpp @@ -44,7 +44,11 @@ TxOutLayout::TxOutLayout(uint64_t currencyDivisor, const QString& currencySymbol { // Base58 version bytes base58_versions[0] = getCoinParams().pay_to_pubkey_hash_version(); +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS + base58_versions[1] = getUseOldAddressVersions() ? getCoinParams().old_pay_to_script_hash_version() : getCoinParams().pay_to_script_hash_version(); +#else base58_versions[1] = getCoinParams().pay_to_script_hash_version(); +#endif // Coin parameters this->currencyDivisor = currencyDivisor; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 5f53bea8..60a23103 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -266,6 +266,12 @@ MainWindow::MainWindow() : refreshAccounts(); }); +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS + connect(this, &MainWindow::signal_addressVersionsChanged, [this]() { + refreshAccounts(); + }); +#endif + setAcceptDrops(true); updateFonts(fontSize); @@ -488,6 +494,19 @@ void MainWindow::selectTrailingDecimals(bool newShowTrailingDecimals) } } +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS +void MainWindow::selectAddressVersions(bool newUseOldAddressVersions) +{ + if (newUseOldAddressVersions != useOldAddressVersions) + { + useOldAddressVersions = newUseOldAddressVersions; + saveSettings(); + setAddressVersions(useOldAddressVersions); + emit signal_addressVersionsChanged(); + } +} +#endif + void MainWindow::newVault(QString fileName) { if (fileName.isEmpty()) { @@ -2432,6 +2451,15 @@ void MainWindow::createActions() connect(showTrailingDecimalsAction, &QAction::toggled, [this]() { selectTrailingDecimals(showTrailingDecimalsAction->isChecked()); }); showTrailingDecimalsAction->setChecked(showTrailingDecimals); +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS + // address version actions + useOldAddressVersionsAction = new QAction(tr("Use Old Address Versions"), this); + useOldAddressVersionsAction->setCheckable(true); + useOldAddressVersionsAction->setStatusTip(tr("Use old address versions")); + connect(useOldAddressVersionsAction, &QAction::toggled, [this]() { selectAddressVersions(useOldAddressVersionsAction->isChecked()); }); + useOldAddressVersionsAction->setChecked(useOldAddressVersions); +#endif + // about/help actions aboutAction = new QAction(tr("About..."), this); aboutAction->setStatusTip(tr("About ") + getDefaultSettings().getAppName()); @@ -2543,9 +2571,14 @@ void MainWindow::createMenus() currencyUnitMenu->addSeparator(); currencyUnitMenu->addAction(showTrailingDecimalsAction); +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS + addressVersionsMenu = menuBar()->addMenu(tr("Address Versions")); + addressVersionsMenu->addSeparator()->setText("Addresses Versions"); + addressVersionsMenu->addAction(useOldAddressVersionsAction); +#endif + menuBar()->addSeparator(); - helpMenu = menuBar()->addMenu(tr("&Help")); helpMenu->addAction(aboutAction); } @@ -2618,6 +2651,10 @@ void MainWindow::loadSettings() host = settings.value("host", "localhost").toString(); port = settings.value("port", getCoinParams().default_port()).toInt(); autoConnect = settings.value("autoconnect", false).toBool(); +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS + useOldAddressVersions = settings.value("useoldaddressversions", false).toBool(); + setAddressVersions(useOldAddressVersions); +#endif setDocDir(settings.value("lastvaultdir", getDefaultSettings().getDocumentDir()).toString()); } @@ -2640,6 +2677,9 @@ void MainWindow::saveSettings() settings.setValue("host", host); settings.setValue("port", port); settings.setValue("autoconnect", autoConnect); +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS + settings.setValue("useoldaddressversions", useOldAddressVersions); +#endif settings.setValue("lastvaultdir", getDocDir()); } } diff --git a/src/mainwindow.h b/src/mainwindow.h index dc75489c..2f6ce304 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -101,6 +101,11 @@ class MainWindow : public QMainWindow void signal_currencyUnitChanged(); +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS + void signal_addressVersionsChanged(); +#endif + + void unsignedTx(); public slots: @@ -129,6 +134,9 @@ private slots: void selectCurrencyUnit(); void selectCurrencyUnit(const QString& newCurrencyUnitPrefix); void selectTrailingDecimals(bool newShowTrailingDecimals); +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS + void selectAddressVersions(bool useOld); +#endif /////////////////// // VAULT OPERATIONS @@ -268,6 +276,9 @@ private slots: QMenu* networkMenu; QMenu* fontsMenu; QMenu* currencyUnitMenu; +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS + QMenu* addressVersionsMenu; +#endif QMenu* helpMenu; // toolbars @@ -365,6 +376,12 @@ private slots: QList currencyUnitActions; QAction* showTrailingDecimalsAction; +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS + // address version actions + bool useOldAddressVersions; + QAction* useOldAddressVersionsAction; +#endif + // about/help actions QAction* aboutAction; diff --git a/src/scriptmodel.cpp b/src/scriptmodel.cpp index e102ac25..76e3eb93 100644 --- a/src/scriptmodel.cpp +++ b/src/scriptmodel.cpp @@ -14,6 +14,7 @@ #include "scriptmodel.h" #include "settings.h" +#include "coinparams.h" #include @@ -27,16 +28,28 @@ ScriptModel::ScriptModel(QObject* parent) : QStandardItemModel(parent) { initColumns(); + setBase58Versions(); } ScriptModel::ScriptModel(CoinDB::Vault* vault, const QString& accountName, QObject* parent) : QStandardItemModel(parent) { initColumns(); + setBase58Versions(); setVault(vault); setAccount(accountName); } +void ScriptModel::setBase58Versions() +{ + base58_versions[0] = getCoinParams().pay_to_pubkey_hash_version(); +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS + base58_versions[1] = getUseOldAddressVersions() ? getCoinParams().old_pay_to_script_hash_version() : getCoinParams().pay_to_script_hash_version(); +#else + base58_versions[1] = getCoinParams().pay_to_script_hash_version(); +#endif +} + void ScriptModel::initColumns() { QStringList columns; @@ -72,7 +85,7 @@ void ScriptModel::update() std::vector scripts = vault->getSigningScriptViews(accountName.toStdString(), "", SigningScript::CHANGE | SigningScript::ISSUED | SigningScript::USED); for (auto& script: scripts) { QList row; - QString address = QString::fromStdString(getAddressForTxOutScript(script.txoutscript, getDefaultSettings().getBase58Versions())); + QString address = QString::fromStdString(getAddressForTxOutScript(script.txoutscript, base58_versions)); QString type; switch (script.status) { diff --git a/src/scriptmodel.h b/src/scriptmodel.h index 6359eb59..03fa7a04 100644 --- a/src/scriptmodel.h +++ b/src/scriptmodel.h @@ -25,6 +25,8 @@ class ScriptModel : public QStandardItemModel ScriptModel(QObject* parent = NULL); ScriptModel(CoinDB::Vault* vault, const QString& accountName, QObject* parent = NULL); + void setBase58Versions(); + void setVault(CoinDB::Vault* vault); void setAccount(const QString& accountName); void update(); @@ -38,5 +40,7 @@ class ScriptModel : public QStandardItemModel CoinDB::Vault* vault; QString accountName; // empty when not loaded + + unsigned char base58_versions[2]; }; diff --git a/src/txmodel.cpp b/src/txmodel.cpp index 81496bb9..c7d41bb5 100644 --- a/src/txmodel.cpp +++ b/src/txmodel.cpp @@ -35,9 +35,7 @@ using namespace std; TxModel::TxModel(QObject* parent) : QStandardItemModel(parent) { - base58_versions[0] = getCoinParams().pay_to_pubkey_hash_version(); - base58_versions[1] = getCoinParams().pay_to_script_hash_version(); - + setBase58Versions(); currencySymbol = getCurrencySymbol(); setColumns(); @@ -46,9 +44,7 @@ TxModel::TxModel(QObject* parent) TxModel::TxModel(CoinDB::Vault* vault, const QString& accountName, QObject* parent) : QStandardItemModel(parent) { - base58_versions[0] = getCoinParams().pay_to_pubkey_hash_version(); - base58_versions[1] = getCoinParams().pay_to_script_hash_version(); - + setBase58Versions(); currencySymbol = getCurrencySymbol(); setColumns(); @@ -56,6 +52,16 @@ TxModel::TxModel(CoinDB::Vault* vault, const QString& accountName, QObject* pare setAccount(accountName); } +void TxModel::setBase58Versions() +{ + base58_versions[0] = getCoinParams().pay_to_pubkey_hash_version(); +#ifdef SUPPORT_OLD_ADDRESS_VERSIONS + base58_versions[1] = getUseOldAddressVersions() ? getCoinParams().old_pay_to_script_hash_version() : getCoinParams().pay_to_script_hash_version(); +#else + base58_versions[1] = getCoinParams().pay_to_script_hash_version(); +#endif +} + void TxModel::setColumns() { QStringList columns; @@ -116,6 +122,8 @@ class SortableRow void TxModel::update() { + setBase58Versions(); + QString newCurrencySymbol = getCurrencySymbol(); if (newCurrencySymbol != currencySymbol) { diff --git a/src/txmodel.h b/src/txmodel.h index 1b69ef38..80148597 100644 --- a/src/txmodel.h +++ b/src/txmodel.h @@ -32,6 +32,8 @@ class TxModel : public QStandardItemModel TxModel(QObject* parent = nullptr); TxModel(CoinDB::Vault* vault, const QString& accountName, QObject* parent = nullptr); + void setBase58Versions(); + void setVault(CoinDB::Vault* vault); CoinDB::Vault* getVault() const { return vault; } void setAccount(const QString& accountName);