1
0

swaps: factor out pubkey_to_rgb_color into core lib

This commit is contained in:
SomberNight
2025-07-14 21:08:19 +00:00
parent 78a7c85f49
commit f2f1dddcc8
3 changed files with 14 additions and 21 deletions

View File

@@ -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']):

View File

@@ -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)

View File

@@ -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