feat(lite): network-tab polish, reworked key-export/QR dialogs

Network tab: glow only the active node, drop the left accent bar. Key-export
dialog: fix the lite-wallet "Not connected" failure by exporting the key
locally via the SDXL backend when there's no daemon; rework the layout to
wrapping click-to-copy fields with a side QR (empty placeholder when hidden),
85% modal width, HRP-preserving key chunking, and a centered, emphasized
warning. QR popup matched to the same sizing and click-to-copy address. Shared
field rendering extracted to widgets/copy_field.h so both dialogs stay in sync.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-27 21:27:25 -05:00
parent 4473e7e00a
commit b20e7efb16
4 changed files with 289 additions and 122 deletions

View File

@@ -4,6 +4,8 @@
#include "key_export_dialog.h"
#include "../../app.h"
#include "../../wallet/lite_wallet_controller.h"
#include <nlohmann/json.hpp>
#include "../../rpc/rpc_client.h"
#include "../../rpc/rpc_worker.h"
#include "../../util/i18n.h"
@@ -11,7 +13,10 @@
#include "../material/draw_helpers.h"
#include "../material/type.h"
#include "../material/colors.h"
#include "../layout.h"
#include "../widgets/qr_code.h"
#include "../widgets/copy_field.h"
#include "../../embedded/IconsMaterialDesign.h"
#include "imgui.h"
namespace dragonx {
@@ -57,19 +62,24 @@ void KeyExportDialog::render(App* app)
{
if (!s_open) return;
const char* title = (s_key_type == KeyType::Private) ?
const char* title = (s_key_type == KeyType::Private) ?
TR("export_private_key") : TR("export_viewing_key");
auto& S = schema::UI();
auto win = S.window("dialogs.key-export");
auto warningBox = S.drawElement("dialogs.key-export", "warning-box");
auto addrInput = S.input("dialogs.key-export", "address-input");
auto revealBtn = S.button("dialogs.key-export", "reveal-button");
auto keyDisplay = S.drawElement("dialogs.key-export", "key-display");
auto copyBtn = S.button("dialogs.key-export", "copy-button");
auto closeBtn = S.button("dialogs.key-export", "close-button");
(void)addrInput;
(void)keyDisplay;
(void)copyBtn;
if (material::BeginOverlayDialog(title, &s_open, win.width, 0.94f)) {
// Modal scales to 85% of the window width. BeginOverlayDialog multiplies the width by
// Layout::dpiScale(); divide it out here so the final card is exactly 85% at any font scale.
const float cardW = (0.85f * ImGui::GetMainViewport()->Size.x) / Layout::dpiScale();
if (material::BeginOverlayDialog(title, &s_open, cardW, 0.94f)) {
ImGui::Spacing();
// Warning section with colored background
@@ -78,16 +88,40 @@ void KeyExportDialog::render(App* app)
(void)warningBox;
ImGui::BeginChild("WarningBox", ImVec2(-1, 0),
ImGuiChildFlags_AutoResizeY | ImGuiChildFlags_Borders);
ImGui::TextColored(ImVec4(1.0f, 0.4f, 0.4f, 1.0f), " %s", TR("warning_upper"));
ImGui::Spacing();
if (s_key_type == KeyType::Private) {
ImGui::TextWrapped(" %s", TR("key_export_private_warning"));
} else {
ImGui::TextWrapped(" %s", TR("key_export_viewing_warning"));
// Heading — warning icon (drawn with the icon font, not the text font, so it isn't tofu) +
// larger h6 text, centered, for emphasis.
{
ImFont* iconF = material::Type().iconMed();
ImFont* textF = material::Type().h6();
const char* icon = ICON_MD_WARNING;
const char* txt = TR("warning_upper");
ImGui::PushFont(iconF); const float iw = ImGui::CalcTextSize(icon).x; ImGui::PopFont();
ImGui::PushFont(textF); const float tw = ImGui::CalcTextSize(txt).x; ImGui::PopFont();
const float sp = ImGui::GetStyle().ItemSpacing.x;
const float wa = ImGui::GetContentRegionAvail().x;
const float total = iw + sp + tw;
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + (wa > total ? (wa - total) * 0.5f : 0.0f));
const ImVec4 red(1.0f, 0.42f, 0.42f, 1.0f);
ImGui::PushFont(iconF); ImGui::TextColored(red, "%s", icon); ImGui::PopFont();
ImGui::SameLine(0, sp);
ImGui::PushFont(textF); ImGui::TextColored(red, "%s", txt); ImGui::PopFont();
}
ImGui::Spacing();
// Body — centered wrapped text.
{
const char* body = (s_key_type == KeyType::Private)
? TR("key_export_private_warning") : TR("key_export_viewing_warning");
ImDrawList* wdl = ImGui::GetWindowDrawList();
ImFont* wf = ImGui::GetFont();
const float wfs = ImGui::GetFontSize();
const float wa = ImGui::GetContentRegionAvail().x;
const ImVec2 wp = ImGui::GetCursorScreenPos();
const float bh = widgets::DrawCenteredWrapped(wdl, wf, wfs, wp, wa, material::OnSurface(), body);
ImGui::Dummy(ImVec2(wa, bh));
}
ImGui::EndChild();
ImGui::PopStyleColor();
@@ -95,32 +129,9 @@ void KeyExportDialog::render(App* app)
ImGui::Separator();
ImGui::Spacing();
// Address display
// Address — click-to-copy field (chunked, width-clamped, centered).
ImGui::Text("%s", TR("address_label"));
// Determine if it's a z-address (longer) or t-address
bool is_zaddr = s_address.length() > 50;
if (is_zaddr) {
// Use multiline for z-addresses
char addr_buf[512];
strncpy(addr_buf, s_address.c_str(), sizeof(addr_buf) - 1);
addr_buf[sizeof(addr_buf) - 1] = '\0';
// Fit the field to the wrapped address (no excess empty space below it).
(void)addrInput;
const float addrFieldH =
ImGui::CalcTextSize(addr_buf, nullptr, false,
ImGui::GetContentRegionAvail().x - ImGui::GetStyle().FramePadding.x * 2.0f).y
+ ImGui::GetStyle().FramePadding.y * 2.0f + 4.0f;
ImGui::InputTextMultiline("##Address", addr_buf, sizeof(addr_buf),
ImVec2(-1, addrFieldH), ImGuiInputTextFlags_ReadOnly);
} else {
char addr_buf[128];
strncpy(addr_buf, s_address.c_str(), sizeof(addr_buf) - 1);
addr_buf[sizeof(addr_buf) - 1] = '\0';
ImGui::SetNextItemWidth(-1);
ImGui::InputText("##Address", addr_buf, sizeof(addr_buf), ImGuiInputTextFlags_ReadOnly);
}
widgets::AddressCopyField("##addrcopy", s_address);
ImGui::Spacing();
@@ -136,11 +147,48 @@ void KeyExportDialog::render(App* app)
// Show button to fetch key
if (material::StyledButton(TR("key_export_reveal"), ImVec2(revealBtn.width, 0), S.resolveFont(revealBtn.font))) {
s_fetching = true;
// Check if z-address or t-address
bool is_zaddress = (s_address.length() > 50 || s_address[0] == 'z');
if (s_key_type == KeyType::Private) {
if (auto* lite = app->liteWallet()) {
// Lite wallet: there is no daemon RPC — export the key locally via the SDXL
// backend (a local, network-free op) instead of the z_exportkey/dumpprivkey
// RPCs, which fail with "Not connected" when no full node is present.
const bool wantViewing = (s_key_type != KeyType::Private);
if (wantViewing && !is_zaddress) {
s_error = TR("key_export_viewing_keys_zonly");
s_fetching = false;
} else {
auto r = lite->exportPrivateKeys(s_address);
std::string found;
if (r.ok) {
try {
auto j = nlohmann::json::parse(r.privateKeysJson);
const nlohmann::json* entry = nullptr;
if (j.is_array()) {
for (auto& e : j)
if (e.contains("address") && e["address"] == s_address) { entry = &e; break; }
if (!entry && !j.empty()) entry = &j.front();
} else if (j.is_object()) {
entry = &j;
}
const char* field = wantViewing ? "viewing_key" : "private_key";
if (entry && entry->contains(field) && (*entry)[field].is_string())
found = (*entry)[field].get<std::string>();
} catch (...) {}
wallet::secureWipeLiteSecret(r.privateKeysJson);
}
if (!found.empty()) {
s_key = found;
s_show_key = wantViewing; // viewing keys are less sensitive
} else {
s_error = r.ok ? std::string("Key not available for this address") : r.error;
}
wallet::secureWipeLiteSecret(found);
s_fetching = false;
}
} else if (s_key_type == KeyType::Private) {
// Export private key
std::string addr = s_address;
std::string method = is_zaddress ? "z_exportkey" : "dumpprivkey";
@@ -201,51 +249,52 @@ void KeyExportDialog::render(App* app)
ImGui::SameLine();
ImGui::TextDisabled("%s", TR("key_export_click_retrieve"));
} else {
// Key has been fetched - display it
// Fit the field to the wrapped key (same length whether shown or masked).
(void)keyDisplay;
char key_buf[1024];
if (s_show_key) {
strncpy(key_buf, s_key.c_str(), sizeof(key_buf) - 1);
} else {
std::string masked(s_key.length(), '*');
strncpy(key_buf, masked.c_str(), sizeof(key_buf) - 1);
}
key_buf[sizeof(key_buf) - 1] = '\0';
const float keyFieldH =
ImGui::CalcTextSize(key_buf, nullptr, false,
ImGui::GetContentRegionAvail().x - ImGui::GetStyle().FramePadding.x * 2.0f).y
+ ImGui::GetStyle().FramePadding.y * 2.0f + 4.0f;
ImGui::InputTextMultiline("##Key", key_buf, sizeof(key_buf),
ImVec2(-1, keyFieldH), ImGuiInputTextFlags_ReadOnly);
// Key fetched. Layout: [ key text (click-to-copy) + Show/Hide below ] [ QR | square ].
const float gap = 12.0f;
const float avail = ImGui::GetContentRegionAvail().x;
// Larger, responsive QR: ~30% of the content width, clamped to a comfortable range.
float qrSize = avail * 0.30f;
if (qrSize < 200.0f) qrSize = 200.0f;
if (qrSize > 340.0f) qrSize = 340.0f;
float keyW = avail - qrSize - gap;
const bool sideBySide = keyW >= 200.0f; // too narrow -> stack the QR under the key
if (!sideBySide) keyW = avail;
// Action row: Show/Hide · Copy · QR. Auto-width buttons (size 0) so the label text never
// clips at the user's font scale, and ONE shared font so they all match.
ImGui::Spacing();
ImFont* actionFont = S.resolveFont(copyBtn.font);
if (material::StyledButton(s_show_key ? TR("hide") : TR("show"), ImVec2(0, 0), actionFont)) {
s_show_key = !s_show_key;
if (!s_show_key) s_show_qr = false; // hiding the key also hides its QR
}
ImGui::SameLine();
if (material::StyledButton(TR("copy_to_clipboard"), ImVec2(0, 0), actionFont)) {
// Auto-clearing clipboard: the key (as sensitive as the seed) is wiped after ~45s.
app->copySecretToClipboard(s_key);
}
// QR (only once revealed) — for scanning the key into another wallet.
if (s_show_key) {
ImGui::SameLine();
if (material::StyledButton(s_show_qr ? TR("hide_qr") : TR("show_qr"), ImVec2(0, 0), actionFont)) {
s_show_qr = !s_show_qr;
// Chunk for readability, but keep a Bech32 HRP (e.g. "secret-extended-key-main") intact
// rather than slicing it into 4s — only the part from the '1' separator on is chunked.
auto chunkKey = [](const std::string& s) -> std::string {
const size_t one = s.find('1');
if (one != std::string::npos && one > 0) {
bool isHrp = true;
for (size_t i = 0; i < one; ++i) {
const char c = s[i];
if (!((c >= 'a' && c <= 'z') || c == '-')) { isHrp = false; break; }
}
if (isHrp) return s.substr(0, one) + " " + widgets::ChunkString(s.substr(one), 4);
}
}
return widgets::ChunkString(s, 4);
};
const std::string shown = s_show_key ? s_key : std::string(s_key.size(), '*');
const std::string keyText = chunkKey(shown);
if (s_show_qr && s_show_key && !s_key.empty()) {
// Left column: key text (click-to-copy), with the Show/Hide button just below it.
ImGui::BeginGroup();
if (widgets::CopyField("##keycopy", keyText, keyW, 0.0f, /*center=*/true)) {
app->copySecretToClipboard(s_key); // auto-clearing clipboard
}
ImGui::Spacing();
if (material::StyledButton(s_show_key ? TR("hide") : TR("show"), ImVec2(0, 0),
material::Type().subtitle1())) {
s_show_key = !s_show_key;
}
ImGui::EndGroup();
if (sideBySide) ImGui::SameLine(0, gap);
else ImGui::Spacing();
// QR to the right of the key when revealed; an empty placeholder square while hidden.
const ImVec2 sq0 = ImGui::GetCursorScreenPos();
if (s_show_key && !s_key.empty()) {
if (s_qr_cached != s_key) { // (re)generate the QR texture when the key changes
if (s_qr_tex) { FreeQRTexture(s_qr_tex); s_qr_tex = 0; }
int qw = 0, qh = 0;
@@ -253,11 +302,20 @@ void KeyExportDialog::render(App* app)
s_qr_cached = s_key;
}
if (s_qr_tex) {
ImGui::Spacing();
const float qrSize = 180.0f;
ImGui::SetCursorPosX((ImGui::GetWindowWidth() - qrSize) * 0.5f);
// White quiet-zone backing so the code stays scannable on dark themes.
ImGui::GetWindowDrawList()->AddRectFilled(
sq0, ImVec2(sq0.x + qrSize, sq0.y + qrSize), IM_COL32_WHITE, 4.0f);
RenderQRCode(s_qr_tex, qrSize);
} else {
ImGui::Dummy(ImVec2(qrSize, qrSize));
}
} else {
// Empty placeholder square — same footprint the QR will occupy once revealed.
ImGui::Dummy(ImVec2(qrSize, qrSize));
ImDrawList* dl = ImGui::GetWindowDrawList();
const ImVec2 sq1(sq0.x + qrSize, sq0.y + qrSize);
dl->AddRectFilled(sq0, sq1, material::WithAlpha(material::OnSurface(), 8), 6.0f);
dl->AddRect(sq0, sq1, material::WithAlpha(material::OnSurface(), 45), 6.0f, 0, 1.0f);
}
}
@@ -269,7 +327,7 @@ void KeyExportDialog::render(App* app)
float button_width = closeBtn.width;
float avail_width = ImGui::GetContentRegionAvail().x;
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + (avail_width - button_width) / 2.0f);
if (material::StyledButton(TR("close"), ImVec2(button_width, 0), S.resolveFont(closeBtn.font))) {
s_open = false;
// Clear sensitive data