From ea67a8bf85edd7fb6b5c5113d6485eaa42bf0129 Mon Sep 17 00:00:00 2001 From: mecampbellsoup Date: Tue, 27 Mar 2018 10:50:33 -0400 Subject: [PATCH] Optionally read RPC credentials from bitcoind cookie auth file --- jmclient/jmclient/configure.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/jmclient/jmclient/configure.py b/jmclient/jmclient/configure.py index f068a1f..516a8a9 100644 --- a/jmclient/jmclient/configure.py +++ b/jmclient/jmclient/configure.py @@ -362,6 +362,30 @@ def load_program_config(config_path=None, bs=None): global_singleton.commit_file_location)) +########################################################## +## Returns a tuple (rpc_user: String, rpc_pass: String) ## +########################################################## +def get_bitcoin_rpc_credentials(_config): + filepath = None + + try: + filepath = _config.get("BLOCKCHAIN", "rpc_cookie_file") + except NoOptionError: + pass + + if filepath: + if os.path.isfile(filepath): + rpc_credentials_string = open(filepath, 'r').read() + return rpc_credentials_string.split(":") + else: + raise ValueError("Invalid cookie auth credentials file location") + else: + rpc_user = _config.get("BLOCKCHAIN", "rpc_user") + rpc_password = _config.get("BLOCKCHAIN", "rpc_password") + if not (rpc_user and rpc_password): + raise ValueError("Invalid RPC auth credentials `rpc_user` and `rpc_password`") + return rpc_user, rpc_password + def get_blockchain_interface_instance(_config): # todo: refactor joinmarket module to get rid of loops # importing here is necessary to avoid import loops @@ -375,15 +399,13 @@ def get_blockchain_interface_instance(_config): #This cannot be tested without mainnet or testnet blockchain (not regtest) rpc_host = _config.get("BLOCKCHAIN", "rpc_host") rpc_port = _config.get("BLOCKCHAIN", "rpc_port") - rpc_user = _config.get("BLOCKCHAIN", "rpc_user") - rpc_password = _config.get("BLOCKCHAIN", "rpc_password") + rpc_user, rpc_password = get_bitcoin_rpc_credentials(_config) rpc = JsonRpc(rpc_host, rpc_port, rpc_user, rpc_password) bc_interface = BitcoinCoreInterface(rpc, network) elif source == 'regtest': rpc_host = _config.get("BLOCKCHAIN", "rpc_host") rpc_port = _config.get("BLOCKCHAIN", "rpc_port") - rpc_user = _config.get("BLOCKCHAIN", "rpc_user") - rpc_password = _config.get("BLOCKCHAIN", "rpc_password") + rpc_user, rpc_password = get_bitcoin_rpc_credentials(_config) rpc = JsonRpc(rpc_host, rpc_port, rpc_user, rpc_password) bc_interface = RegtestBitcoinCoreInterface(rpc) elif source == 'electrum':