From f2f1dddcc837a537b21f0b1a0e3098aaea70f61f Mon Sep 17 00:00:00 2001 From: SomberNight Date: Mon, 14 Jul 2025 21:08:19 +0000 Subject: [PATCH] swaps: factor out pubkey_to_rgb_color into core lib --- electrum/gui/qml/qeswaphelper.py | 13 ++----------- electrum/gui/qt/swap_dialog.py | 12 ++---------- electrum/submarine_swaps.py | 10 ++++++++++ 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/electrum/gui/qml/qeswaphelper.py b/electrum/gui/qml/qeswaphelper.py index 8a9c0c194..bcc1a8e65 100644 --- a/electrum/gui/qml/qeswaphelper.py +++ b/electrum/gui/qml/qeswaphelper.py @@ -13,9 +13,8 @@ from electrum.logging import get_logger from electrum.transaction import PartialTxOutput, PartialTransaction from electrum.util import (NotEnoughFunds, NoDynamicFeeEstimates, profiler, get_asyncio_loop, age, wait_for2) -from electrum.submarine_swaps import NostrTransport, SwapServerTransport +from electrum.submarine_swaps import NostrTransport, SwapServerTransport, pubkey_to_rgb_color from electrum.fee_policy import FeePolicy -from electrum.crypto import sha256 from electrum.gui import messages @@ -70,14 +69,6 @@ class QESwapServerNPubListModel(QAbstractListModel): self._services = [] self.endResetModel() - @staticmethod - def str_to_color(color_input: str) -> QColor: - input_hash = int.from_bytes(sha256(color_input), byteorder="big") - r = (input_hash & 0xFF0000) >> 16 - g = (input_hash & 0x00FF00) >> 8 - b = input_hash & 0x0000FF - return QColor(r, g, b) - def offer_to_model(self, x: 'SwapOffer'): return { 'npub': x.server_npub, @@ -89,7 +80,7 @@ class QESwapServerNPubListModel(QAbstractListModel): 'max_reverse_amount': x.pairs.max_reverse, 'timestamp': age(x.timestamp), 'pow_bits': x.pow_bits, - 'color': self.str_to_color(x.server_pubkey), + 'color': QColor(*pubkey_to_rgb_color(x.server_pubkey)), } def updateModel(self, items: Sequence['SwapOffer']): diff --git a/electrum/gui/qt/swap_dialog.py b/electrum/gui/qt/swap_dialog.py index e4753d5e0..8c5447b27 100644 --- a/electrum/gui/qt/swap_dialog.py +++ b/electrum/gui/qt/swap_dialog.py @@ -13,8 +13,7 @@ from electrum.util import NotEnoughFunds, NoDynamicFeeEstimates, UserCancelled from electrum.bitcoin import DummyAddress from electrum.transaction import PartialTxOutput, PartialTransaction from electrum.fee_policy import FeePolicy -from electrum.crypto import sha256 -from electrum.submarine_swaps import NostrTransport +from electrum.submarine_swaps import NostrTransport, pubkey_to_rgb_color from electrum.gui import messages from . import util @@ -519,14 +518,7 @@ class SwapServerDialog(WindowModalDialog, QtEventListener): @staticmethod def _pubkey_to_q_icon(server_pubkey: str) -> QIcon: - def str_to_rgb(color_input: str) -> int: - input_hash = int.from_bytes(sha256(color_input), byteorder="big") - r = (input_hash & 0xFF0000) >> 16 - g = (input_hash & 0x00FF00) >> 8 - b = input_hash & 0x0000FF - return (r << 16) | (g << 8) | b - - color = QColor(str_to_rgb(server_pubkey)) + color = QColor(*pubkey_to_rgb_color(server_pubkey)) color_pixmap = QPixmap(100, 100) color_pixmap.fill(color) return QIcon(color_pixmap) diff --git a/electrum/submarine_swaps.py b/electrum/submarine_swaps.py index 274c88063..e4f223642 100644 --- a/electrum/submarine_swaps.py +++ b/electrum/submarine_swaps.py @@ -186,6 +186,16 @@ class SwapData(StoredObject): return self._payment_pending or bool(self.funding_txid) +def pubkey_to_rgb_color(swapserver_pubkey: str) -> Tuple[int, int, int]: + assert isinstance(swapserver_pubkey, str), type(swapserver_pubkey) + assert len(swapserver_pubkey) == 64, len(swapserver_pubkey) + input_hash = int.from_bytes(sha256(swapserver_pubkey), byteorder="big") + r = (input_hash & 0xFF0000) >> 16 + g = (input_hash & 0x00FF00) >> 8 + b = input_hash & 0x0000FF + return r, g, b + + class SwapManager(Logger): network: Optional['Network'] = None