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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214 | class MempoolAPI:
def __init__(
self,
base_url: str,
timeout: float = 30.0,
socks_proxy: str | None = None,
trust_env: bool = True,
) -> None:
self.base_url = base_url.rstrip("/") if base_url else ""
self.timeout = timeout
self.socks_proxy = socks_proxy
self.trust_env = trust_env
client_kwargs: dict[str, Any] = {"trust_env": trust_env}
if socks_proxy:
try:
from httpx_socks import AsyncProxyTransport
from jmcore.tor_isolation import normalize_proxy_url
# python-socks does not support the socks5h:// scheme directly.
# normalize_proxy_url converts socks5h:// -> socks5:// + rdns=True
# so that .onion addresses are resolved by Tor.
normalized = normalize_proxy_url(socks_proxy)
transport = AsyncProxyTransport.from_url(normalized.url, rdns=normalized.rdns)
client_kwargs["transport"] = transport
except ImportError as e:
raise MempoolAPIError(
"Tor-routed mempool access requires httpx-socks; install httpx-socks and retry"
) from e
except Exception as e:
raise MempoolAPIError("Failed to configure Tor transport for mempool access") from e
self.client = httpx.AsyncClient(timeout=timeout, follow_redirects=True, **client_kwargs)
async def __aenter__(self) -> MempoolAPI:
return self
async def __aexit__(self, *args: Any) -> None:
await self.close()
async def close(self) -> None:
await self.client.aclose()
async def test_connection(self) -> bool:
"""Test if the API connection works by making a simple request."""
if not self.base_url:
logger.debug("Mempool API connection test skipped (no base_url configured)")
return False
try:
# Test with a lightweight endpoint - get current block tip height
url = f"{self.base_url}/blocks/tip/height"
logger.debug(f"Testing connection to: {url}")
response = await self.client.get(url)
response.raise_for_status()
height = int(response.text)
logger.info(f"Connection test successful - current block height: {height}")
return True
except Exception as e:
logger.error(f"MempoolAPI connection test failed: {e}")
return False
async def _get(self, endpoint: str) -> dict[str, Any]:
if not self.base_url:
raise MempoolAPIError("Mempool API URL is not configured")
url = f"{self.base_url}/{endpoint}"
try:
logger.debug(f"MempoolAPI request: GET {url}")
transport = "Tor" if self.socks_proxy else "direct"
logger.debug(f"MempoolAPI request transport configured: {transport}")
response = await self.client.get(url)
response.raise_for_status()
return response.json()
except httpx.HTTPError as e:
logger.error(f"MempoolAPI error: {e}")
logger.debug(
f"MempoolAPI client transport: {getattr(self.client, '_transport', 'None')}"
)
raise MempoolAPIError(f"API request failed: {e}") from e
async def _get_list(self, endpoint: str) -> list[dict[str, Any]]:
if not self.base_url:
raise MempoolAPIError("Mempool API URL is not configured")
url = f"{self.base_url}/{endpoint}"
try:
response = await self.client.get(url)
response.raise_for_status()
data = response.json()
except httpx.HTTPError as e:
logger.error(f"MempoolAPI error: {e}")
raise MempoolAPIError(f"API request failed: {e}") from e
if not isinstance(data, list) or not all(isinstance(item, dict) for item in data):
raise MempoolAPIError(f"API returned invalid list data for {endpoint}")
return data
async def get_address_info(self, address: str) -> AddressInfo:
data = await self._get(f"address/{address}")
return AddressInfo(**data)
async def get_transaction(self, txid: str) -> Transaction:
data = await self._get(f"tx/{txid}")
return Transaction(**data)
async def get_outspend(self, txid: str, vout: int) -> Outspend:
outspends = await self._get_list(f"tx/{txid}/outspends")
if vout < 0 or vout >= len(outspends):
raise MempoolAPIError(f"Output index {vout} is missing from outspend response")
return Outspend(**outspends[vout])
async def get_block_height(self) -> int:
response = await self.client.get(f"{self.base_url}/blocks/tip/height")
response.raise_for_status()
return int(response.text)
async def get_block_hash(self, height: int) -> str:
response = await self.client.get(f"{self.base_url}/block-height/{height}")
response.raise_for_status()
return response.text
async def get_utxo_confirmations(self, txid: str, vout: int) -> int | None:
try:
tx = await self.get_transaction(txid)
if not tx.status.confirmed or tx.status.block_height is None:
return None
current_height = await self.get_block_height()
confirmations = current_height - tx.status.block_height + 1
return max(0, confirmations)
except MempoolAPIError:
return None
async def get_utxo_value(self, txid: str, vout: int) -> int | None:
try:
tx = await self.get_transaction(txid)
if vout >= len(tx.vout):
return None
return tx.vout[vout].value
except MempoolAPIError:
return None
|