| 133 | directly.""" |
| 134 | |
| 135 | def __init__(self, |
| 136 | service_url=None, |
| 137 | service_port=None, |
| 138 | btc_conf_file=None, |
| 139 | timeout=DEFAULT_HTTP_TIMEOUT, |
| 140 | connection=None): |
| 141 | |
| 142 | # Create a dummy connection early on so if __init__() fails prior to |
| 143 | # __conn being created __del__() can detect the condition and handle it |
| 144 | # correctly. |
| 145 | self.__conn = None |
| 146 | authpair = None |
| 147 | |
| 148 | if service_url is None: |
| 149 | # Figure out the path to the bitcoin.conf file |
| 150 | if btc_conf_file is None: |
| 151 | if platform.system() == 'Darwin': |
| 152 | btc_conf_file = os.path.expanduser('~/Library/Application Support/Bitcoin/') |
| 153 | elif platform.system() == 'Windows': |
| 154 | btc_conf_file = os.path.join(os.environ['APPDATA'], 'Bitcoin') |
| 155 | else: |
| 156 | btc_conf_file = os.path.expanduser('~/.bitcoin') |
| 157 | btc_conf_file = os.path.join(btc_conf_file, 'bitcoin.conf') |
| 158 | |
| 159 | # Bitcoin Core accepts empty rpcuser, not specified in btc_conf_file |
| 160 | conf = {'rpcuser': ""} |
| 161 | |
| 162 | # Extract contents of bitcoin.conf to build service_url |
| 163 | try: |
| 164 | with open(btc_conf_file, 'r') as fd: |
| 165 | for line in fd.readlines(): |
| 166 | if '#' in line: |
| 167 | line = line[:line.index('#')] |
| 168 | if '=' not in line: |
| 169 | continue |
| 170 | k, v = line.split('=', 1) |
| 171 | conf[k.strip()] = v.strip() |
| 172 | |
| 173 | # Treat a missing bitcoin.conf as though it were empty |
| 174 | except FileNotFoundError: |
| 175 | pass |
| 176 | |
| 177 | if service_port is None: |
| 178 | service_port = bitcoin.params.RPC_PORT |
| 179 | conf['rpcport'] = int(conf.get('rpcport', service_port)) |
| 180 | conf['rpchost'] = conf.get('rpcconnect', 'localhost') |
| 181 | |
| 182 | service_url = ('%s://%s:%d' % |
| 183 | ('http', conf['rpchost'], conf['rpcport'])) |
| 184 | |
| 185 | cookie_dir = conf.get('datadir', os.path.dirname(btc_conf_file)) |
| 186 | if bitcoin.params.NAME == 'testnet': |
| 187 | cookie_dir = os.path.join(cookie_dir, 'testnet3') |
| 188 | elif bitcoin.params.NAME == 'regtest': |
| 189 | cookie_dir = os.path.join(cookie_dir, bitcoin.params.NAME) |
| 190 | cookie_file = os.path.join(cookie_dir, ".cookie") |
| 191 | try: |
| 192 | with open(cookie_file, 'r') as fd: |