-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacks_preferences.py
More file actions
159 lines (114 loc) · 4.97 KB
/
stacks_preferences.py
File metadata and controls
159 lines (114 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
Stacks Preferences Dialog
Global settings for the Stacks plugin.
"""
from typing import TYPE_CHECKING
from PyQt6.QtWidgets import (
QDialog, QVBoxLayout, QHBoxLayout, QLabel, QComboBox,
QPushButton, QGroupBox, QFormLayout, QLineEdit
)
from electrum.i18n import _
from .stacks_wire import StacksNetwork
if TYPE_CHECKING:
from electrum.gui.qt.main_window import ElectrumWindow
from .qt import Plugin
# Config keys
CONFIG_KEY_NETWORK = 'stacks_network'
CONFIG_KEY_NODE_URL = 'stacks_node_url'
# Default node URL
DEFAULT_NODE_URL = 'https://api.hiro.so'
def get_stacks_network(config) -> StacksNetwork:
"""Get the configured Stacks network from Electrum config."""
network_str = config.get(CONFIG_KEY_NETWORK, 'mainnet')
if network_str == 'testnet':
return StacksNetwork.TESTNET
return StacksNetwork.MAINNET
def set_stacks_network(config, network: StacksNetwork):
"""Save the Stacks network preference to Electrum config."""
network_str = 'testnet' if network == StacksNetwork.TESTNET else 'mainnet'
config.set_key(CONFIG_KEY_NETWORK, network_str)
def get_stacks_node_url(config) -> str:
"""Get the configured Stacks node URL from Electrum config."""
return config.get(CONFIG_KEY_NODE_URL, DEFAULT_NODE_URL)
def set_stacks_node_url(config, url: str):
"""Save the Stacks node URL to Electrum config."""
# Strip trailing slash if present
url = url.rstrip('/')
config.set_key(CONFIG_KEY_NODE_URL, url)
def get_stacks_api_url(config, endpoint: str) -> str:
"""Get full API URL for an endpoint, respecting network setting."""
base_url = get_stacks_node_url(config)
network = get_stacks_network(config)
# If using default Hiro API and on testnet, use testnet subdomain
if base_url == DEFAULT_NODE_URL and network == StacksNetwork.TESTNET:
base_url = 'https://api.testnet.hiro.so'
return f"{base_url}{endpoint}"
class StacksPreferencesDialog(QDialog):
"""Dialog for configuring Stacks plugin preferences."""
def __init__(self, window: 'ElectrumWindow', plugin: 'Plugin'):
super().__init__(window)
self.window = window
self.config = window.config
self.plugin = plugin
self.setWindowTitle(_("Stacks Preferences"))
self.setMinimumWidth(400)
self.setup_ui()
self.load_preferences()
def setup_ui(self):
"""Set up the dialog UI."""
layout = QVBoxLayout(self)
# Network settings
network_group = QGroupBox(_("Network"))
network_layout = QFormLayout()
self.network_combo = QComboBox()
self.network_combo.addItem(_("Mainnet"), StacksNetwork.MAINNET)
self.network_combo.addItem(_("Testnet"), StacksNetwork.TESTNET)
network_layout.addRow(_("Stacks Network:"), self.network_combo)
network_info = QLabel(_("Select which Stacks network to use for transactions and address lookups."))
network_info.setWordWrap(True)
network_info.setStyleSheet("color: #666; font-style: italic;")
network_layout.addRow(network_info)
network_group.setLayout(network_layout)
layout.addWidget(network_group)
# Node settings
node_group = QGroupBox(_("Stacks Node"))
node_layout = QFormLayout()
self.node_url_input = QLineEdit()
self.node_url_input.setPlaceholderText(DEFAULT_NODE_URL)
node_layout.addRow(_("Node URL:"), self.node_url_input)
node_info = QLabel(_("Base URL for Stacks API requests. Leave empty to use the default Hiro API."))
node_info.setWordWrap(True)
node_info.setStyleSheet("color: #666; font-style: italic;")
node_layout.addRow(node_info)
node_group.setLayout(node_layout)
layout.addWidget(node_group)
layout.addStretch()
# Buttons
btn_layout = QHBoxLayout()
btn_layout.addStretch()
self.save_btn = QPushButton(_("Save"))
self.save_btn.clicked.connect(self.save_preferences)
self.save_btn.setDefault(True)
btn_layout.addWidget(self.save_btn)
self.cancel_btn = QPushButton(_("Cancel"))
self.cancel_btn.clicked.connect(self.reject)
btn_layout.addWidget(self.cancel_btn)
layout.addLayout(btn_layout)
def load_preferences(self):
"""Load current preferences into the UI."""
network = get_stacks_network(self.config)
index = self.network_combo.findData(network)
if index >= 0:
self.network_combo.setCurrentIndex(index)
node_url = get_stacks_node_url(self.config)
if node_url != DEFAULT_NODE_URL:
self.node_url_input.setText(node_url)
def save_preferences(self):
"""Save preferences and close dialog."""
network = self.network_combo.currentData()
set_stacks_network(self.config, network)
node_url = self.node_url_input.text().strip()
if not node_url:
node_url = DEFAULT_NODE_URL
set_stacks_node_url(self.config, node_url)
self.accept()