From 407d02d12ab2c0f237b20c924f146d6b010d7a57 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Tue, 21 Oct 2025 20:28:35 -0400 Subject: [PATCH] Fix nip46 handshake UI and guard QR stubs --- js/ui/loginModalController.js | 24 ++++++++- js/utils/qrcode.js | 99 +++++++++++++++++++++++++---------- 2 files changed, 95 insertions(+), 28 deletions(-) diff --git a/js/ui/loginModalController.js b/js/ui/loginModalController.js index af8e8dac..97f6c634 100644 --- a/js/ui/loginModalController.js +++ b/js/ui/loginModalController.js @@ -931,11 +931,33 @@ export default class LoginModalController { if (qrContainer instanceof HTMLElement) { qrContainer.innerHTML = ""; if (uri) { - qrInstance = renderQrCode(qrContainer, uri, { width: 224, height: 224 }); + try { + qrInstance = renderQrCode(qrContainer, uri, { + width: 224, + height: 224, + }); + } catch (error) { + qrInstance = null; + devLogger.warn( + "[LoginModalController] Failed to render NIP-46 QR code:", + error, + ); + const fallback = this.document?.createElement("div"); + if (fallback) { + fallback.className = "text-center text-xs text-text-muted"; + fallback.textContent = + "Unable to render QR code. Use the connect link below."; + qrContainer.appendChild(fallback); + } + } } else { qrInstance = null; } } + setPendingState(false); + if (submitButton instanceof HTMLButtonElement) { + submitButton.disabled = true; + } resetStatus(); clearAuthChallenge(); setError(""); diff --git a/js/utils/qrcode.js b/js/utils/qrcode.js index f86daa2a..1fc7fc72 100644 --- a/js/utils/qrcode.js +++ b/js/utils/qrcode.js @@ -1,16 +1,27 @@ -/** - * @fileoverview - * - Using the 'QRCode for Javascript library' - * - Fixed dataset of 'QRCode for Javascript library' for support full-spec. - * - this library has no dependencies. - * - * @author davidshimjs - * @see http://www.d-project.com/ - * @see http://jeromeetienne.github.com/jquery-qrcode/ - */ -var QRCode; +const hasDocument = + typeof document !== "undefined" && + typeof document.createElement === "function" && + typeof document.createElementNS === "function"; -(function () { +let QRCodeExport = null; +let createQrCodeExport = null; +let renderQrCodeExport = null; +let updateQrCodeExport = null; + +if (hasDocument) { + /** + * @fileoverview + * - Using the 'QRCode for Javascript library' + * - Fixed dataset of 'QRCode for Javascript library' for support full-spec. + * - this library has no dependencies. + * + * @author davidshimjs + * @see http://www.d-project.com/ + * @see http://jeromeetienne.github.com/jquery-qrcode/ + */ + var QRCode; + + (function () { //--------------------------------------------------------------------- // QRCode for JavaScript // @@ -631,24 +642,58 @@ var QRCode; * @name QRCode.CorrectLevel */ QRCode.CorrectLevel = QRErrorCorrectLevel; -})(); + })(); -export { QRCode }; + QRCodeExport = QRCode; -export function createQrCode(element, options = {}) { - return new QRCode(element, options); -} + createQrCodeExport = function createQrCode(element, options = {}) { + return new QRCode(element, options); + }; -export function renderQrCode(element, text, options = {}) { - const normalizedOptions = Object.assign({}, options, { text }); - const instance = new QRCode(element, normalizedOptions); - return instance; -} + renderQrCodeExport = function renderQrCode(element, text, options = {}) { + const normalizedOptions = Object.assign({}, options, { text }); + const instance = new QRCode(element, normalizedOptions); + return instance; + }; -export function updateQrCode(instance, text) { - if (!instance || typeof instance.makeCode !== "function") { - throw new Error("Invalid QRCode instance"); + updateQrCodeExport = function updateQrCode(instance, text) { + if (!instance || typeof instance.makeCode !== "function") { + throw new Error("Invalid QRCode instance"); + } + instance.makeCode(text); + return instance; + }; +} else { + class QRCodeStub { + constructor() { + throw new Error("QR code rendering requires a DOM environment."); + } + + makeCode() { + throw new Error("QR code rendering requires a DOM environment."); + } + + clear() {} } - instance.makeCode(text); - return instance; + + QRCodeExport = QRCodeStub; + + createQrCodeExport = function createQrCodeStub() { + throw new Error("QR code rendering requires a DOM environment."); + }; + + renderQrCodeExport = function renderQrCodeStub() { + throw new Error("QR code rendering requires a DOM environment."); + }; + + updateQrCodeExport = function updateQrCodeStub() { + throw new Error("QR code rendering requires a DOM environment."); + }; } + +export { QRCodeExport as QRCode }; +export { + createQrCodeExport as createQrCode, + renderQrCodeExport as renderQrCode, + updateQrCodeExport as updateQrCode, +};