fiatjaf / bitcoin-requests

Simplest Bitcoin Core RPC interface.
Other
2 stars 3 forks source link

Cookie file etc #2

Open xloem opened 3 years ago

xloem commented 3 years ago

Hi, if you are interested, I am using the library with an interface like this that automatically finds credentials:

    def __init__(self, datadir='~/.bitcoin', rpcurl='127.0.0.1', rpcuser=None, rpcpassword=None, rpcport=None):
        if '://' in rpcurl:
            dummy, rpcurl = rpcurl.split('://', 1)
        if '/' in rpcurl:
            rpcurl, dummy = rpcurl.split('/', 1)
        if '@' in rpcurl:
            if rpcuser is not None:
                raise Exception('user specified in rpcurl and parameter list')
            rpcuser, rpcurl = rpcurl.split('@', 1)
            if ':' in rpcuser:
                if rpcpassword is not None:
                    raise Exception('password specified in rpcurl and parameter list')
                rpcuser, rpcpassword = rpcuser.split(':', 1)
        if ':' in rpcurl:
            if rpcport is not None:
                raise DatacoinException('port specified in rpcurl and parameter list')
            rpcurl, rpcport = rpcurl.split(':', 1)
        rpcurl = 'http://{}'.format(rpcurl)
        if rpcport is not None:
            rpcurl += ':{}'.format(rpcport)
        if rpcpassword is None and rpcuser is None:
            datadir = os.path.expanduser(datadir)
            cookiefn = os.path.join(datadir, '.cookie')
            if os.path.exists(cookiefn):
                with open(cookiefn) as cookiefile:
                    rpcuser, rpcpassword = cookiefile.read().split(':', 1)
fiatjaf commented 3 years ago

Interesting, thank you. My bitcoind doesn't seem to have that .cookie file. I'll look into that later.

xloem commented 3 years ago

It is generated if you don't specify rpcuser/rpcpassword to bitcoind, and contains them.