Skip to content

Commit

Permalink
Gui: getrawtransaction Implementation
Browse files Browse the repository at this point in the history
Add getrawtransaction RPC to GUI to tools > getrawtransactions
  • Loading branch information
BrandonOdiwuor committed Dec 20, 2023
1 parent 29c2c90 commit a17fb06
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build_msvc/libbitcoin_qt/libbitcoin_qt.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<ClCompile Include="..\..\src\qt\transactiontablemodel.cpp" />
<ClCompile Include="..\..\src\qt\transactionview.cpp" />
<ClCompile Include="..\..\src\qt\utilitydialog.cpp" />
<ClCompile Include="..\..\src\qt\toolsdialog.cpp" />
<ClCompile Include="..\..\src\qt\walletcontroller.cpp" />
<ClCompile Include="..\..\src\qt\walletframe.cpp" />
<ClCompile Include="..\..\src\qt\walletmodel.cpp" />
Expand Down Expand Up @@ -114,6 +115,7 @@
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_transactiontablemodel.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_transactionview.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_utilitydialog.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_toolsdialog.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_walletcontroller.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_walletframe.cpp" />
<ClCompile Include="$(GeneratedFilesOutDir)\moc\moc_walletmodel.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions src/Makefile.qt.include
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ QT_FORMS_UI = \
qt/forms/modaloverlay.ui \
qt/forms/openuridialog.ui \
qt/forms/optionsdialog.ui \
qt/forms/toolsdialog.ui \
qt/forms/overviewpage.ui \
qt/forms/psbtoperationsdialog.ui \
qt/forms/receivecoinsdialog.ui \
Expand Down Expand Up @@ -84,6 +85,7 @@ QT_MOC_CPP = \
qt/moc_transactiontablemodel.cpp \
qt/moc_transactionview.cpp \
qt/moc_utilitydialog.cpp \
qt/moc_toolsdialog.cpp \
qt/moc_walletcontroller.cpp \
qt/moc_walletframe.cpp \
qt/moc_walletmodel.cpp \
Expand Down Expand Up @@ -244,6 +246,7 @@ BITCOIN_QT_BASE_CPP = \
qt/rpcconsole.cpp \
qt/splashscreen.cpp \
qt/trafficgraphwidget.cpp \
qt/toolsdialog.cpp \
qt/utilitydialog.cpp

BITCOIN_QT_WINDOWS_CPP = qt/winshutdownmonitor.cpp
Expand Down
15 changes: 15 additions & 0 deletions src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <qt/platformstyle.h>
#include <qt/rpcconsole.h>
#include <qt/utilitydialog.h>
#include <qt/toolsdialog.h>

#ifdef ENABLE_WALLET
#include <qt/walletcontroller.h>
Expand Down Expand Up @@ -368,12 +369,17 @@ void BitcoinGUI::createActions()
m_mask_values_action->setStatusTip(tr("Mask the values in the Overview tab"));
m_mask_values_action->setCheckable(true);

getRawTransactionAction = new QAction(tr("&Verify external txid"), this);
getRawTransactionAction->setMenuRole(QAction::NoRole);
getRawTransactionAction->setStatusTip(tr("getrawtransaction RPC"));

connect(quitAction, &QAction::triggered, this, &BitcoinGUI::quitRequested);
connect(aboutAction, &QAction::triggered, this, &BitcoinGUI::aboutClicked);
connect(aboutQtAction, &QAction::triggered, qApp, QApplication::aboutQt);
connect(optionsAction, &QAction::triggered, this, &BitcoinGUI::optionsClicked);
connect(showHelpMessageAction, &QAction::triggered, this, &BitcoinGUI::showHelpMessageClicked);
connect(openRPCConsoleAction, &QAction::triggered, this, &BitcoinGUI::showDebugWindow);
connect(getRawTransactionAction, &QAction::triggered, this, &BitcoinGUI::getRawTransactionClicked);
// prevents an open debug window from becoming stuck/unusable on client shutdown
connect(quitAction, &QAction::triggered, rpcConsole, &QWidget::hide);

Expand Down Expand Up @@ -555,6 +561,9 @@ void BitcoinGUI::createMenuBar()
});
}

QMenu *tools = appMenuBar->addMenu(tr("&Tools"));
tools->addAction(getRawTransactionAction);

QMenu *help = appMenuBar->addMenu(tr("&Help"));
help->addAction(showHelpMessageAction);
help->addSeparator();
Expand Down Expand Up @@ -932,6 +941,12 @@ void BitcoinGUI::showHelpMessageClicked()
GUIUtil::bringToFront(helpMessageDialog);
}

void BitcoinGUI::getRawTransactionClicked()
{
auto dlg = new ToolsDialog(this, ToolsDialog::GetRawTransactionMode, &m_node);
GUIUtil::ShowModalDialogAsynchronously(dlg);
}

#ifdef ENABLE_WALLET
void BitcoinGUI::openClicked()
{
Expand Down
3 changes: 3 additions & 0 deletions src/qt/bitcoingui.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class BitcoinGUI : public QMainWindow
QAction* m_load_psbt_action = nullptr;
QAction* m_load_psbt_clipboard_action = nullptr;
QAction* aboutAction = nullptr;
QAction* getRawTransactionAction = nullptr;
QAction* receiveCoinsAction = nullptr;
QAction* optionsAction = nullptr;
QAction* encryptWalletAction = nullptr;
Expand Down Expand Up @@ -308,6 +309,8 @@ public Q_SLOTS:
void showDebugWindowActivateConsole();
/** Show help message dialog */
void showHelpMessageClicked();
/** show getrawtransaction dialog*/
void getRawTransactionClicked();

/** Show window if hidden, unminimize when minimized, rise when obscured or show if hidden and fToggleHidden is true */
void showNormalIfMinimized() { showNormalIfMinimized(false); }
Expand Down
151 changes: 151 additions & 0 deletions src/qt/forms/toolsdialog.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ToolsDialog</class>
<widget class="QDialog" name="ToolsDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>780</width>
<height>400</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QFormLayout" name="formLayout">
<property name="bottomMargin">
<number>12</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="labelTxId">
<property name="text">
<string>transaction id: </string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QValidatedLineEdit" name="txidEdit">
<property name="toolTip">
<string>The transaction id</string>
</property>
<property name="placeholderText">
<string notr="true">Enter a transaction id...</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="verboseCheckbox">
<property name="toolTip">
<string>verbose</string>
</property>
<property name="text">
<string>verbose</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="labelBlockHash">
<property name="text">
<string>blockhash (optional): </string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QValidatedLineEdit" name="blockHashEdit">
<property name="toolTip">
<string>The block in which to look for the transaction</string>
</property>
<property name="placeholderText">
<string notr="true">Enter Blockhash...</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QTextEdit" name="helpMessage">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>4</width>
<height>4</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="submitButton">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources>
<include location="../bitcoin.qrc"/>
</resources>
<customwidgets>
<customwidget>
<class>QValidatedLineEdit</class>
<extends>QLineEdit</extends>
<header>qt/qvalidatedlineedit.h</header>
</customwidget>
</customwidgets>
<connections>
<connection>
<sender>submitButton</sender>
<signal>accepted()</signal>
<receiver>ToolsDialog</receiver>
<slot>onSubmitForm()</slot>
<hints>
<hint type="sourcelabel">
<x>395</x>
<y>343</y>
</hint>
<hint type="destinationlabel">
<x>389</x>
<y>199</y>
</hint>
</hints>
</connection>
<connection>
<sender>submitButton</sender>
<signal>rejected()</signal>
<receiver>ToolsDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>395</x>
<y>343</y>
</hint>
<hint type="destinationlabel">
<x>389</x>
<y>199</y>
</hint>
</hints>
</connection>
</connections>
</ui>
100 changes: 100 additions & 0 deletions src/qt/toolsdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) 2023-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <qt/toolsdialog.h>
#include <qt/forms/ui_toolsdialog.h>

#include <qt/guiutil.h>

#include <univalue.h>
#include <rpc/client.h>

#include <QString>
#include <QTextDocument>
#include <QTextCursor>

#include <string>
#include <vector>

/** Tools dialog box */
ToolsDialog::ToolsDialog(QWidget *parent, Mode _mode, interfaces::Node* _node) :
QDialog(parent, GUIUtil::dialog_flags),
ui(new Ui::ToolsDialog),
mode(_mode),
node(_node)
{
ui->setupUi(this);

switch (mode) {
case GetRawTransactionMode:
{
setWindowTitle(tr("getrawtransaction"));
break;
}
}

GUIUtil::handleCloseWindowShortcut(this);
}

ToolsDialog::~ToolsDialog()
{
delete ui;
}

QString ToolsDialog::txid() const
{
return ui->txidEdit->text();
}

bool ToolsDialog::isVerboseChecked() const
{
return ui->verboseCheckbox->isChecked();
}

QString ToolsDialog::blockhash() const
{
return ui->blockHashEdit->text();
}

void ToolsDialog::onSubmitForm()
{
std::string transaction_id = txid().toUtf8().constData();
bool verbose = isVerboseChecked();
std::string block_hash = blockhash().toUtf8().constData();
std::string result;

getrawtransactionRPC(transaction_id, verbose, block_hash, result);

QTextDocument *document = ui->helpMessage->document();
document->clear();
QTextCursor cursor(document);
cursor.movePosition(QTextCursor::Start);
cursor.insertText(QString::fromStdString(result));
}

void ToolsDialog::getrawtransactionRPC(std::string txid, bool verbose, std::string blockhash, std::string& result)
{
std::string command {"getrawtransaction"};
std::vector<std::string> args {txid};
args.emplace_back(verbose ? "true" : "false");
if (!blockhash.empty()) {
args.emplace_back(blockhash);
}
UniValue params {RPCConvertValues(command, args)};
UniValue lastResult;

try {
assert(node);
lastResult = node->executeRpc(command, params, "");

if (lastResult.isStr())
result = lastResult.get_str();
else
result = lastResult.write(2);
} catch (std::exception& e) {
result = "Error: " + std::string(e.what());
} catch (...) {
result = "No such mempool transaction. Use -txindex or provide a block hash to enable blockchain transaction queries. Use gettransaction for wallet transactions";
}
}
Loading

0 comments on commit a17fb06

Please sign in to comment.