| 438 | |
| 439 | |
| 440 | def get_auth_cookie(datadir, chain): |
| 441 | user = None |
| 442 | password = None |
| 443 | if os.path.isfile(os.path.join(datadir, "elements.conf")): |
| 444 | with open(os.path.join(datadir, "elements.conf"), 'r', encoding='utf8') as f: |
| 445 | for line in f: |
| 446 | if line.startswith("rpcuser="): |
| 447 | assert user is None # Ensure that there is only one rpcuser line |
| 448 | user = line.split("=")[1].strip("\n") |
| 449 | if line.startswith("rpcpassword="): |
| 450 | assert password is None # Ensure that there is only one rpcpassword line |
| 451 | password = line.split("=")[1].strip("\n") |
| 452 | try: |
| 453 | with open(os.path.join(datadir, chain, ".cookie"), 'r', encoding="ascii") as f: |
| 454 | userpass = f.read() |
| 455 | split_userpass = userpass.split(':') |
| 456 | user = split_userpass[0] |
| 457 | password = split_userpass[1] |
| 458 | except OSError: |
| 459 | pass |
| 460 | if user is None or password is None: |
| 461 | raise ValueError("No RPC credentials") |
| 462 | return user, password |
| 463 | |
| 464 | |
| 465 | # If a cookie file exists in the given datadir, delete it. |