Merge pull request #6488

99684e3e simplewallet: add show_qr_code command (selsta)
This commit is contained in:
Alexander Blair 2020-07-16 06:14:29 -07:00
commit 5041de8a3b
No known key found for this signature in database
GPG key ID: C64552D877C32479
7 changed files with 1491 additions and 0 deletions

View file

@ -74,10 +74,12 @@
#include "version.h"
#include <stdexcept>
#include "wallet/message_store.h"
#include "QrCode.hpp"
#ifdef WIN32
#include <boost/locale.hpp>
#include <boost/filesystem.hpp>
#include <fcntl.h>
#endif
#ifdef HAVE_READLINE
@ -275,6 +277,7 @@ namespace
const char* USAGE_RPC_PAYMENT_INFO("rpc_payment_info");
const char* USAGE_START_MINING_FOR_RPC("start_mining_for_rpc");
const char* USAGE_STOP_MINING_FOR_RPC("stop_mining_for_rpc");
const char* USAGE_SHOW_QR_CODE("show_qr_code [<subaddress_index>]");
const char* USAGE_VERSION("version");
const char* USAGE_HELP_ADVANCED("help_advanced [<command>]");
const char* USAGE_HELP("help");
@ -2394,6 +2397,62 @@ bool simple_wallet::stop_mining_for_rpc(const std::vector<std::string> &args)
return true;
}
bool simple_wallet::show_qr_code(const std::vector<std::string> &args)
{
uint32_t subaddress_index = 0;
if (args.size() >= 1)
{
if (!string_tools::get_xtype_from_string(subaddress_index, args[0]))
{
fail_msg_writer() << tr("invalid index: must be an unsigned integer");
return true;
}
if (subaddress_index >= m_wallet->get_num_subaddresses(m_current_subaddress_account))
{
fail_msg_writer() << tr("<subaddress_index> is out of bounds");
return true;
}
}
#ifdef _WIN32
#define PRINT_UTF8(pre, x) std::wcout << pre ## x
#define WTEXTON() _setmode(_fileno(stdout), _O_WTEXT)
#define WTEXTOFF() _setmode(_fileno(stdout), _O_TEXT)
#else
#define PRINT_UTF8(pre, x) std::cout << x
#define WTEXTON()
#define WTEXTOFF()
#endif
WTEXTON();
try
{
const std::string address = "monero:" + m_wallet->get_subaddress_as_str({m_current_subaddress_account, subaddress_index});
const qrcodegen::QrCode qr = qrcodegen::QrCode::encodeText(address.c_str(), qrcodegen::QrCode::Ecc::LOW);
for (int y = -2; y < qr.getSize() + 2; y+=2)
{
for (int x = -2; x < qr.getSize() + 2; x++)
{
if (qr.getModule(x, y) && qr.getModule(x, y + 1))
PRINT_UTF8(L, "\u2588");
else if (qr.getModule(x, y) && !qr.getModule(x, y + 1))
PRINT_UTF8(L, "\u2580");
else if (!qr.getModule(x, y) && qr.getModule(x, y + 1))
PRINT_UTF8(L, "\u2584");
else
PRINT_UTF8(L, " ");
}
PRINT_UTF8(, std::endl);
}
}
catch (const std::length_error&)
{
fail_msg_writer() << tr("Failed to generate QR code, input too large");
}
WTEXTOFF();
return true;
}
bool simple_wallet::set_always_confirm_transfers(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
{
const auto pwd_container = get_and_verify_password();
@ -3621,6 +3680,10 @@ simple_wallet::simple_wallet()
boost::bind(&simple_wallet::stop_mining_for_rpc, this, _1),
tr(USAGE_STOP_MINING_FOR_RPC),
tr("Stop mining to pay for RPC access"));
m_cmd_binder.set_handler("show_qr_code",
boost::bind(&simple_wallet::on_command, this, &simple_wallet::show_qr_code, _1),
tr(USAGE_SHOW_QR_CODE),
tr("Show address as QR code"));
m_cmd_binder.set_handler("help_advanced",
boost::bind(&simple_wallet::on_command, this, &simple_wallet::help_advanced, _1),
tr(USAGE_HELP_ADVANCED),