jmcore.cli_common
jmcore.cli_common
Common CLI components for JoinMarket NG.
This module provides reusable CLI helper functions to reduce duplication across jmwallet, maker, and taker CLIs.
Architecture: - Resolver functions: Take CLI args + settings and return resolved values - Setup functions: Common initialization (logging, settings, etc.) - Mnemonic loading: Unified mnemonic resolution from multiple sources
The CLI parameter definitions remain in each CLI module for now, but the resolution logic is centralized here. This approach: - Avoids typer dependency in jmcore - Allows each CLI to customize parameter names/help text if needed - Centralizes the complex resolution logic that was duplicated
Usage: from jmcore.cli_common import ( resolve_backend_settings, resolve_mnemonic, resolve_tor_settings, setup_cli, )
@app.command()
def my_command(
network: Annotated[str | None, typer.Option("--network")] = None,
rpc_url: Annotated[str | None, typer.Option("--rpc-url")] = None,
...
):
settings = setup_cli(log_level, data_dir=data_dir)
backend = resolve_backend_settings(settings, network=network, rpc_url=rpc_url, ...)
Classes
ResolvedBackendSettings
dataclass
Resolved backend settings ready for use.
Source code in jmcore/src/jmcore/cli_common.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
Attributes
backend_type: str
instance-attribute
bitcoin_network: str
instance-attribute
data_dir: Path
instance-attribute
fee_estimate_proxy: str | None = None
class-attribute
instance-attribute
fee_estimate_url: str | None = None
class-attribute
instance-attribute
network: str
instance-attribute
neutrino_add_peers: list[str]
instance-attribute
neutrino_auth_token: str | None = None
class-attribute
instance-attribute
neutrino_tls_cert: str | None = None
class-attribute
instance-attribute
neutrino_url: str
instance-attribute
rpc_password: str
instance-attribute
rpc_url: str
instance-attribute
rpc_user: str
instance-attribute
scan_start_height: int | None = None
class-attribute
instance-attribute
ResolvedMnemonic
dataclass
Resolved mnemonic and BIP39 passphrase.
Note: bip39_passphrase is the optional BIP39 passphrase (13th/25th word), NOT the password used to decrypt an encrypted mnemonic file.
Source code in jmcore/src/jmcore/cli_common.py
87 88 89 90 91 92 93 94 95 96 97 98 | |
Attributes
bip39_passphrase: str
instance-attribute
creation_height: int | None = None
class-attribute
instance-attribute
mnemonic: str
instance-attribute
source: str
instance-attribute
ResolvedTorSettings
dataclass
Resolved Tor settings ready for use.
Source code in jmcore/src/jmcore/cli_common.py
75 76 77 78 79 80 81 82 83 84 | |
Attributes
control_enabled: bool
instance-attribute
control_host: str
instance-attribute
control_port: int
instance-attribute
cookie_path: Path | None
instance-attribute
socks_host: str
instance-attribute
socks_port: int
instance-attribute
Functions:
create_backend(backend_settings: ResolvedBackendSettings, *, wallet_name: str | None = None, creation_height: int | None = None) -> Any
Create a backend instance based on resolved settings.
Args: backend_settings: Resolved backend settings wallet_name: Wallet name for descriptor_wallet backend creation_height: Block height at wallet creation time (used as scan start hint)
Returns: Backend instance (DescriptorWalletBackend or NeutrinoBackend)
Raises: ValueError: If backend type is invalid ImportError: If backend module not available
Source code in jmcore/src/jmcore/cli_common.py
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 | |
generate_descriptor_wallet_name(mnemonic: str, network: str, passphrase: str = '') -> str
Generate a deterministic wallet name from mnemonic fingerprint.
Args: mnemonic: BIP39 mnemonic network: Network name (mainnet, testnet, etc.) passphrase: BIP39 passphrase
Returns: Wallet name in format "jm-{fingerprint}-{network}"
Source code in jmcore/src/jmcore/cli_common.py
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 | |
load_mnemonic_from_file(path: Path, password: str | None = None, auto_prompt: bool = True, max_prompt_attempts: int = 3) -> str
Load mnemonic from a file (plain text or Fernet encrypted).
Args:
path: Path to mnemonic file
password: Password for decrypting the file (NOT BIP39 passphrase)
auto_prompt: If True, prompt for password when encrypted file is detected
max_prompt_attempts: When interactively prompting for a password, how
many times to retry on wrong-password errors before giving up.
A value of 1 disables retry. Only applies to the interactive
prompt path: explicit password arguments and passwords coming
from the MNEMONIC_PASSWORD env var still fail fast on mismatch.
Returns: The mnemonic phrase
Raises: FileNotFoundError: If file doesn't exist ValueError: If file format is invalid or decryption fails
Source code in jmcore/src/jmcore/cli_common.py
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 | |
log_resolved_settings(backend: ResolvedBackendSettings, tor: ResolvedTorSettings | None = None, directory_servers: list[str] | None = None, mnemonic_source: str | None = None) -> None
Log resolved settings for debugging/transparency.
Args: backend: Resolved backend settings tor: Resolved Tor settings (optional) directory_servers: Resolved directory servers (optional) mnemonic_source: Source of mnemonic (optional)
Source code in jmcore/src/jmcore/cli_common.py
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 | |
resolve_backend_settings(settings: JoinMarketSettings, *, network: NetworkType | str | None = None, bitcoin_network: NetworkType | str | None = None, backend_type: str | None = None, rpc_url: str | None = None, rpc_user: str | None = None, rpc_password: str | None = None, neutrino_url: str | None = None, neutrino_tls_cert: str | None = None, neutrino_auth_token: str | None = None, data_dir: Path | None = None) -> ResolvedBackendSettings
Resolve backend settings with priority: CLI > Settings (env + config) > Defaults.
Args: settings: JoinMarketSettings instance network: CLI override for network bitcoin_network: CLI override for bitcoin network backend_type: CLI override for backend type rpc_url: CLI override for RPC URL rpc_user: CLI override for RPC user rpc_password: CLI override for RPC password neutrino_url: CLI override for Neutrino URL neutrino_tls_cert: CLI override for Neutrino TLS certificate path neutrino_auth_token: CLI override for Neutrino API auth token data_dir: CLI override for data directory
Returns: ResolvedBackendSettings with all values resolved
Source code in jmcore/src/jmcore/cli_common.py
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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 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 | |
resolve_bip39_passphrase(bip39_passphrase: str | None = None, prompt: bool = False) -> str
Resolve BIP39 passphrase from argument or prompt.
Args: bip39_passphrase: Direct passphrase value prompt: Whether to prompt interactively
Returns: Resolved passphrase (empty string if none)
Source code in jmcore/src/jmcore/cli_common.py
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 | |
resolve_configured_mnemonic_file(settings: JoinMarketSettings) -> Path | None
Resolve the active wallet's mnemonic file path without decrypting it.
Mirrors the file-based portion of :func:resolve_mnemonic's priority
chain so per-wallet read commands can locate the configured wallet and
read its companion .meta fingerprint without prompting for a
password:
MNEMONIC_FILEenvironment variable- Config file
wallet.mnemonic_filesetting - Default wallet path (
<data_dir>/wallets/default.mnemonic)
Raw-mnemonic sources (--mnemonic/MNEMONIC env) have no backing
file and are intentionally ignored here. Returns None when no
file-based wallet is configured or the default does not exist.
Source code in jmcore/src/jmcore/cli_common.py
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 | |
resolve_directory_servers(settings: JoinMarketSettings, *, directory_servers: str | None = None, network: str | None = None) -> list[str]
Resolve directory servers with priority: CLI > Settings > Network defaults.
Args: settings: JoinMarketSettings instance directory_servers: CLI override (comma-separated) network: Network to use for defaults (if not in settings)
Returns: List of directory server addresses
Source code in jmcore/src/jmcore/cli_common.py
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 | |
resolve_mnemonic(settings: JoinMarketSettings, *, mnemonic: str | None = None, mnemonic_file: Path | None = None, password: str | None = None, bip39_passphrase: str | None = None, prompt_bip39_passphrase: bool = False, required: bool = True) -> ResolvedMnemonic | None
Resolve mnemonic from various sources with priority.
Mnemonic priority: 1. --mnemonic argument 2. --mnemonic-file argument 3. MNEMONIC_FILE environment variable 4. MNEMONIC environment variable 5. Config file wallet.mnemonic_file setting 6. Default wallet path (~/.joinmarket-ng/wallets/default.mnemonic)
BIP39 passphrase priority: 1. --bip39-passphrase argument 2. BIP39_PASSPHRASE environment variable 3. Config file wallet.bip39_passphrase setting 4. Interactive prompt (if --prompt-bip39-passphrase is set) 5. Empty string (default - no passphrase)
For encrypted mnemonic files, the password is resolved as: 1. Config file wallet.mnemonic_password setting (or password param) 2. MNEMONIC_PASSWORD environment variable 3. Interactive prompt (if auto_prompt is enabled)
Args: settings: JoinMarketSettings instance mnemonic: CLI mnemonic string mnemonic_file: CLI mnemonic file path password: Password for encrypted mnemonic file (NOT BIP39 passphrase) bip39_passphrase: BIP39 passphrase (13th/25th word, NOT file encryption password) prompt_bip39_passphrase: Whether to prompt for BIP39 passphrase interactively required: Whether mnemonic is required (raises error if not found)
Returns: ResolvedMnemonic or None if not required and not found
Raises: ValueError: If required but not found, or if loading fails
Source code in jmcore/src/jmcore/cli_common.py
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 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 | |
resolve_tor_settings(settings: JoinMarketSettings, *, socks_host: str | None = None, socks_port: int | None = None, control_host: str | None = None, control_port: int | None = None, cookie_path: Path | None = None, disable_control: bool = False) -> ResolvedTorSettings
Resolve Tor settings with priority: CLI > Settings > Defaults.
Args: settings: JoinMarketSettings instance socks_host: CLI override for SOCKS host socks_port: CLI override for SOCKS port control_host: CLI override for control host control_port: CLI override for control port cookie_path: CLI override for cookie path disable_control: Whether to disable Tor control
Returns: ResolvedTorSettings with all values resolved
Source code in jmcore/src/jmcore/cli_common.py
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 | |
setup_cli(log_level: str | None = None, data_dir: Path | None = None, config_file: Path | None = None) -> JoinMarketSettings
Common CLI setup: reset settings cache, configure logging, return settings.
Log level priority: CLI argument > settings (env/config) > default "INFO"
When data_dir is provided (typically from a CLI --data-dir flag),
the JOINMARKET_DATA_DIR environment variable is set to that path
BEFORE settings are loaded. This ensures that <data_dir>/config.toml
is read, that wallet.mnemonic_file defaults resolve to that data
directory, and that any other env-driven defaults (paths, etc.) point at
the user-specified data directory.
When config_file is provided (typically from a CLI --config-file
flag), the JOINMARKET_CONFIG_FILE environment variable is set so that
the config is read from (and created at) that explicit path instead of
<data_dir>/config.toml. This decouples the configuration file location
from the data directory for FHS-style deployments (e.g. /etc/joinmarket
config with /var/lib/joinmarket data); see issue #537.
Args:
log_level: Log level override from CLI (None means use settings)
data_dir: Optional CLI override for data directory. When provided,
JOINMARKET_DATA_DIR is set in os.environ so that all
subsequent settings-loading code paths agree on the same data
directory.
config_file: Optional CLI override for the config file path. When
provided, JOINMARKET_CONFIG_FILE is set in os.environ so
that settings are read from and created at that explicit path.
Returns: JoinMarketSettings instance with all sources loaded
Source code in jmcore/src/jmcore/cli_common.py
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 | |
setup_logging(level: str = 'INFO') -> None
Configure loguru logging with consistent format.
Args: level: Log level (TRACE, DEBUG, INFO, WARNING, ERROR)
Source code in jmcore/src/jmcore/cli_common.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |