Skip to content

Commit 191663c

Browse files
committed
RPC: Treat a missing bitcoin.conf as if it's empty
Particularly useful for cookie authentication.
1 parent fd5b96f commit 191663c

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

bitcoin/rpc.py

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -142,39 +142,46 @@ def __init__(self,
142142
btc_conf_file = os.path.expanduser('~/.bitcoin')
143143
btc_conf_file = os.path.join(btc_conf_file, 'bitcoin.conf')
144144

145+
# Bitcoin Core accepts empty rpcuser, not specified in btc_conf_file
146+
conf = {'rpcuser': ""}
147+
145148
# Extract contents of bitcoin.conf to build service_url
146-
with open(btc_conf_file, 'r') as fd:
147-
# Bitcoin Core accepts empty rpcuser, not specified in btc_conf_file
148-
conf = {'rpcuser': ""}
149-
for line in fd.readlines():
150-
if '#' in line:
151-
line = line[:line.index('#')]
152-
if '=' not in line:
153-
continue
154-
k, v = line.split('=', 1)
155-
conf[k.strip()] = v.strip()
156-
157-
if service_port is None:
158-
service_port = bitcoin.params.RPC_PORT
159-
conf['rpcport'] = int(conf.get('rpcport', service_port))
160-
conf['rpchost'] = conf.get('rpcconnect', 'localhost')
161-
162-
service_url = ('%s://%s:%d' %
163-
('http', conf['rpchost'], conf['rpcport']))
164-
165-
cookie_dir = conf.get('datadir', os.path.dirname(btc_conf_file))
166-
if bitcoin.params.NAME != "mainnet":
167-
cookie_dir = os.path.join(cookie_dir, bitcoin.params.NAME)
168-
cookie_file = os.path.join(cookie_dir, ".cookie")
169-
try:
170-
with open(cookie_file, 'r') as fd:
171-
authpair = fd.read()
172-
except IOError as err:
173-
if 'rpcpassword' in conf:
174-
authpair = "%s:%s" % (conf['rpcuser'], conf['rpcpassword'])
175-
176-
else:
177-
raise ValueError('Cookie file unusable (%s) and rpcpassword not specified in the configuration file: %r' % (err, btc_conf_file))
149+
try:
150+
with open(btc_conf_file, 'r') as fd:
151+
for line in fd.readlines():
152+
if '#' in line:
153+
line = line[:line.index('#')]
154+
if '=' not in line:
155+
continue
156+
k, v = line.split('=', 1)
157+
conf[k.strip()] = v.strip()
158+
159+
# Treat a missing bitcoin.conf as though it were empty
160+
except FileNotFoundError:
161+
pass
162+
163+
if service_port is None:
164+
service_port = bitcoin.params.RPC_PORT
165+
conf['rpcport'] = int(conf.get('rpcport', service_port))
166+
conf['rpchost'] = conf.get('rpcconnect', 'localhost')
167+
168+
service_url = ('%s://%s:%d' %
169+
('http', conf['rpchost'], conf['rpcport']))
170+
171+
cookie_dir = conf.get('datadir', os.path.dirname(btc_conf_file))
172+
if bitcoin.params.NAME != "mainnet":
173+
cookie_dir = os.path.join(cookie_dir, bitcoin.params.NAME)
174+
cookie_file = os.path.join(cookie_dir, ".cookie")
175+
try:
176+
with open(cookie_file, 'r') as fd:
177+
authpair = fd.read()
178+
except IOError as err:
179+
if 'rpcpassword' in conf:
180+
authpair = "%s:%s" % (conf['rpcuser'], conf['rpcpassword'])
181+
182+
else:
183+
raise ValueError('Cookie file unusable (%s) and rpcpassword not specified in the configuration file: %r' % (err, btc_conf_file))
184+
178185
else:
179186
url = urlparse.urlparse(service_url)
180187
authpair = "%s:%s" % (url.username, url.password)

0 commit comments

Comments
 (0)