jmcore.network
jmcore.network
Network primitives and connection management.
Attributes
ONION_HOSTID = 'onion-network'
module-attribute
Classes
Connection
Bases: ABC
Source code in jmcore/src/jmcore/network.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
Functions
close() -> None
abstractmethod
async
Source code in jmcore/src/jmcore/network.py
38 39 40 | |
is_connected() -> bool
abstractmethod
Source code in jmcore/src/jmcore/network.py
42 43 44 | |
receive() -> bytes
abstractmethod
async
Source code in jmcore/src/jmcore/network.py
34 35 36 | |
send(data: bytes) -> None
abstractmethod
async
Source code in jmcore/src/jmcore/network.py
30 31 32 | |
ConnectionError
Bases: Exception
Source code in jmcore/src/jmcore/network.py
25 26 | |
ConnectionPool
Source code in jmcore/src/jmcore/network.py
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 | |
Attributes
connections: dict[str, Connection] = {}
instance-attribute
max_connections = max_connections
instance-attribute
Functions
__init__(max_connections: int = 1000)
Source code in jmcore/src/jmcore/network.py
117 118 119 | |
__len__() -> int
Source code in jmcore/src/jmcore/network.py
139 140 | |
add(peer_id: str, connection: Connection) -> None
Source code in jmcore/src/jmcore/network.py
121 122 123 124 | |
close_all() -> None
async
Source code in jmcore/src/jmcore/network.py
133 134 135 136 137 | |
get(peer_id: str) -> Connection | None
Source code in jmcore/src/jmcore/network.py
126 127 | |
remove(peer_id: str) -> None
Source code in jmcore/src/jmcore/network.py
129 130 131 | |
HiddenServiceListener
TCP listener for accepting direct peer connections via Tor hidden service.
This is used by makers to accept direct connections from takers, bypassing the directory server for lower latency.
Source code in jmcore/src/jmcore/network.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | |
Attributes
bound_port: int
property
Get the actual port the server is bound to.
host = host
instance-attribute
max_message_size = max_message_size
instance-attribute
on_connection = on_connection
instance-attribute
port = port
instance-attribute
running = False
instance-attribute
server: asyncio.Server | None = None
instance-attribute
Functions
__init__(host: str = '127.0.0.1', port: int = 0, max_message_size: int = 2097152, on_connection: Callable[[TCPConnection, str], Coroutine[Any, Any, None]] | None = None)
Initialize hidden service listener.
Args: host: Local address to bind to (typically 127.0.0.1 for Tor) port: Local port to bind to (0 for auto-assign) max_message_size: Maximum message size in bytes on_connection: Callback when new connection is accepted
Source code in jmcore/src/jmcore/network.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
serve_forever() -> None
async
Run the server until stopped.
Source code in jmcore/src/jmcore/network.py
307 308 309 310 | |
start() -> int
async
Start listening for connections.
Returns: The port number the server is bound to
Source code in jmcore/src/jmcore/network.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
stop() -> None
async
Stop the listener.
Source code in jmcore/src/jmcore/network.py
298 299 300 301 302 303 304 305 | |
OnionPeer
Represents a direct peer connection over Tor.
Used by takers to establish direct connections to makers, bypassing the directory server for private message exchange. This improves privacy by preventing directories from seeing who is communicating with whom.
Connection Flow: 1. Taker gets maker's onion address from peerlist 2. Taker creates OnionPeer and calls try_to_connect() 3. Connection is established via Tor SOCKS proxy 4. Handshake is performed (same protocol as directory) 5. Messages can be sent/received directly
State Machine: UNCONNECTED -> CONNECTING -> CONNECTED -> HANDSHAKED -> DISCONNECTED | | | v v v (failure) (failure) (disconnect) | | | v v v UNCONNECTED DISCONNECTED DISCONNECTED
Source code in jmcore/src/jmcore/network.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 | |
Attributes
hostname: str | None
property
Get peer's hostname.
location = location
instance-attribute
max_message_size = max_message_size
instance-attribute
nick = nick
instance-attribute
nick_identity = nick_identity
instance-attribute
on_disconnect = on_disconnect
instance-attribute
on_handshake_complete = on_handshake_complete
instance-attribute
on_message = on_message
instance-attribute
port: int | None
property
Get peer's port.
socks_host = socks_host
instance-attribute
socks_password = socks_password
instance-attribute
socks_port = socks_port
instance-attribute
socks_username = socks_username
instance-attribute
timeout = timeout
instance-attribute
Functions
__init__(nick: str, location: str, socks_host: str = '127.0.0.1', socks_port: int = 9050, timeout: float = 120.0, max_message_size: int = 2097152, on_message: Callable[[str, bytes], Coroutine[Any, Any, None]] | None = None, on_disconnect: Callable[[str], Coroutine[Any, Any, None]] | None = None, on_handshake_complete: Callable[[str], Coroutine[Any, Any, None]] | None = None, nick_identity: NickIdentity | None = None, socks_username: str | None = None, socks_password: str | None = None)
Initialize OnionPeer.
Args: nick: Peer's JoinMarket nick location: Peer's onion address (host:port) socks_host: SOCKS proxy host for Tor socks_port: SOCKS proxy port for Tor timeout: Connection timeout in seconds (covers SOCKS + Tor circuit + PoW) max_message_size: Maximum message size in bytes on_message: Callback when message received (nick, data) on_disconnect: Callback when peer disconnects (nick) on_handshake_complete: Callback when handshake completes (nick) nick_identity: Our nick identity for signing messages (required for compatibility with reference implementation) socks_username: SOCKS5 username for Tor stream isolation (optional) socks_password: SOCKS5 password for Tor stream isolation (optional)
Source code in jmcore/src/jmcore/network.py
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | |
__repr__() -> str
Source code in jmcore/src/jmcore/network.py
756 757 | |
can_connect() -> bool
Check if we can attempt to connect to this peer.
Source code in jmcore/src/jmcore/network.py
458 459 460 461 462 463 464 465 466 | |
connect(our_nick: str, our_location: str, network: str) -> bool
async
Connect to the peer and perform handshake.
Args: our_nick: Our JoinMarket nick our_location: Our onion address or NOT-SERVING-ONION network: Bitcoin network (mainnet, testnet, signet, regtest)
Returns: True if connection and handshake succeeded
Source code in jmcore/src/jmcore/network.py
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 | |
disconnect() -> None
async
Disconnect from peer.
Source code in jmcore/src/jmcore/network.py
700 701 702 703 704 705 706 707 708 | |
is_connected() -> bool
Check if peer is connected and ready to send messages.
Source code in jmcore/src/jmcore/network.py
450 451 452 | |
is_connecting() -> bool
Check if connection is in progress.
Source code in jmcore/src/jmcore/network.py
454 455 456 | |
send(data: bytes) -> bool
async
Send data to peer.
Args: data: Raw message bytes to send
Returns: True if send succeeded
Source code in jmcore/src/jmcore/network.py
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | |
send_privmsg(our_nick: str, command: str, message: str) -> bool
async
Send a private message to peer.
Messages are signed with our nick identity for authentication. The reference implementation verifies all private messages, whether received via directory relay or direct peer connection.
Args: our_nick: Our JoinMarket nick command: Command name (e.g., "fill", "pubkey") message: Message content (will be signed if nick_identity is set)
Returns: True if send succeeded
Source code in jmcore/src/jmcore/network.py
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 | |
status() -> int
Get current connection status.
Source code in jmcore/src/jmcore/network.py
446 447 448 | |
try_to_connect(our_nick: str, our_location: str, network: str) -> asyncio.Task | None
Try to connect to peer asynchronously (non-blocking).
This method is called opportunistically when we want to send a message but don't have a direct connection yet. The message is sent via directory relay, but we start a background connection for future messages.
Args: our_nick: Our JoinMarket nick our_location: Our onion address network: Bitcoin network
Returns: Task if connection attempt started, None if skipped
Source code in jmcore/src/jmcore/network.py
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 | |
OnionPeerConnectionError
Bases: OnionPeerError
Error during connection to peer.
Source code in jmcore/src/jmcore/network.py
327 328 | |
OnionPeerError
Bases: Exception
Base exception for OnionPeer errors.
Source code in jmcore/src/jmcore/network.py
323 324 | |
PeerStatus
Connection status for OnionPeer.
Source code in jmcore/src/jmcore/network.py
313 314 315 316 317 318 319 320 | |
Attributes
CONNECTED = 2
class-attribute
instance-attribute
CONNECTING = 1
class-attribute
instance-attribute
DISCONNECTED = 4
class-attribute
instance-attribute
HANDSHAKED = 3
class-attribute
instance-attribute
UNCONNECTED = 0
class-attribute
instance-attribute
TCPConnection
Bases: Connection
Source code in jmcore/src/jmcore/network.py
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 | |
Attributes
max_message_size = max_message_size
instance-attribute
reader = reader
instance-attribute
writer = writer
instance-attribute
Functions
__init__(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, max_message_size: int = 2097152)
Source code in jmcore/src/jmcore/network.py
48 49 50 51 52 53 54 55 56 57 58 59 | |
close() -> None
async
Source code in jmcore/src/jmcore/network.py
105 106 107 108 109 110 | |
is_connected() -> bool
Source code in jmcore/src/jmcore/network.py
112 113 | |
receive() -> bytes
async
Source code in jmcore/src/jmcore/network.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
send(data: bytes) -> None
async
Source code in jmcore/src/jmcore/network.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
Functions
connect_direct(host: str, port: int, max_message_size: int = 2097152, timeout: float = 30.0) -> TCPConnection
async
Connect directly via TCP without Tor (for local development/testing).
Source code in jmcore/src/jmcore/network.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
connect_via_tor(onion_address: str, port: int, socks_host: str = '127.0.0.1', socks_port: int = 9050, max_message_size: int = 2097152, timeout: float = 120.0, socks_username: str | None = None, socks_password: str | None = None) -> TCPConnection
async
Connect to an onion address via Tor SOCKS5 proxy.
The timeout covers the entire SOCKS5 connection lifecycle including TCP handshake to the proxy, SOCKS5 negotiation, Tor circuit building, and PoW solving (if the destination has Tor PoW defense enabled).
Under normal conditions, Tor circuit establishment takes 5-15 seconds. With PoW defense active (during DoS attacks), the PoW solving can add significant time. Tor's internal circuit timeout is ~120 seconds, so the default matches that.
When socks_username and socks_password are provided, they are
sent during SOCKS5 authentication. Tor's IsolateSOCKSAuth (enabled
by default) uses these to assign the connection to a distinct circuit,
enabling stream isolation between different connection categories.
Source code in jmcore/src/jmcore/network.py
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 215 216 | |