Skip to content

Commit 5f77336

Browse files
committed
Support cookie file on testnet and regtest
Previously only worked correctly on mainnet, as the file actually has a different location for each chain.
1 parent 1927f3a commit 5f77336

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

bitcoin/rpc.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,19 @@ def __init__(self,
161161
service_url = ('%s://%s:%d' %
162162
('http', conf['rpchost'], conf['rpcport']))
163163

164-
cookie_file = os.path.dirname(btc_conf_file)
165-
cookie_file = os.path.join(cookie_file, ".cookie")
166-
with open(cookie_file, 'r') as fd:
167-
authpair = fd.read()
168-
169-
if 'rpcpassword' in conf:
170-
authpair = "%s:%s" % (conf['rpcuser'], conf['rpcpassword'])
171-
172-
if authpair is None:
173-
raise ValueError('The value of rpcpassword not specified in the configuration file: %s' % btc_conf_file)
164+
cookie_dir = os.path.dirname(btc_conf_file)
165+
if bitcoin.params.NAME != "mainnet":
166+
cookie_dir = os.path.join(cookie_dir, bitcoin.params.NAME)
167+
cookie_file = os.path.join(cookie_dir, ".cookie")
168+
try:
169+
with open(cookie_file, 'r') as fd:
170+
authpair = fd.read()
171+
except IOError as err:
172+
if 'rpcpassword' in conf:
173+
authpair = "%s:%s" % (conf['rpcuser'], conf['rpcpassword'])
174+
175+
else:
176+
raise ValueError('Cookie file unusable (%s) and rpcpassword not specified in the configuration file: %r' % (err, btc_conf_file))
174177

175178
self.__service_url = service_url
176179
self.__url = urlparse.urlparse(service_url)

0 commit comments

Comments
 (0)