Skip to content
This repository has been archived by the owner on Jan 27, 2022. It is now read-only.

Commit

Permalink
Environment file setup script
Browse files Browse the repository at this point in the history
  • Loading branch information
kornpow committed Aug 12, 2022
1 parent 9e1b31b commit 70dffd4
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 164 deletions.
98 changes: 98 additions & 0 deletions lndgrpc/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import os
from pathlib import Path
import code
import subprocess

import click
from yachalk import chalk

from lndgrpc.client import LNDClient

@click.group()
def cli():
pass

@click.command(help="Enter a Python REPL with access to your node")
def shell():
# LNDClient gets all configuration parameters from environment variables!
lnd = LNDClient()

# Enter a shell for interacting with LND
code.interact(local=dict(globals(), **locals()))

@click.command(help="Input and save your credentials to disk")
@click.option('--input_format', default="base64", type=click.Choice(["hex", "base64"]), help="Input format")
@click.option('--credential_type', default="macaroon", type=click.Choice(["macaroon", "tls"]), help="Input Type")
def credentials(input_format, credential_type):
credential_path = Path(os.getenv("LND_CRED_PATH", None))
subprocess.check_call(["stty","-icanon"])
print(f"Saving credentials to: {credential_path}")
input_data = click.prompt(chalk.yellow.bold(f"Enter your node's {credential_type}"), type=str, default="")
if input_format == "hex":
data = bytes.fromhex(input_data)
elif input_format == "base64":
data = base64.b64decode(input_data)

output_file = None
if credential_type == "tls":
output_file = credential_path.joinpath("tls.cert")
with open(output_file, "wb") as f:
f.write(data)

if credential_type == "macaroon":
macaroon_name = click.prompt(chalk.yellow.bold(f"Enter your macaroon name:"), type=str, default="admin")
output_file = credential_path.joinpath(f"{macaroon_name}.macaroon")
with open(output_file, "wb") as f:
f.write(data)

print(f"Enable this macaroon by running:\n", chalk.red.bg_yellow(f"export LND_MACAROON={output_file.name}"))

subprocess.check_call(["stty","icanon"])
print(f"Wrote file: {output_file}")

@click.command(help="Create an environment file")
def environment():
print("Saving credentials!")
node_uri = click.prompt(chalk.yellow.bold("Enter your node's IP Address"), type=str, default="127.0.0.1")
print(chalk.white(node_uri))

node_port = click.prompt(chalk.yellow.bold("Enter your node's Port"), type=str, default="10009")
print(chalk.white(node_port))

node_nickname = click.prompt(chalk.yellow.bold("Enter your node's Alias"), type=str, default="default-node-alias")
print(chalk.white(node_nickname))

default_root_path = Path.expanduser(Path("~")).joinpath("Documents").joinpath("lnd-creds").joinpath(node_nickname)
default_path = default_root_path.joinpath("lnd")
credential_path = Path(click.prompt(chalk.yellow.bold("Where do you want keep your node credentials? Macaroons and tls.cert?"), type=str, default=default_root_path))

macaroon_filename = click.prompt(chalk.yellow.bold("Enter your macaroon filename"), type=str, default="admin.macaroon")

save_env_file = click.prompt(chalk.yellow.bold(f"Build directory structure and save `node-env` file at location: {default_root_path}"), type=bool, default=True)
env_file = f"""
export LND_CRED_PATH={credential_path}
export LND_NODE_IP={node_uri}
export LND_NODE_PORT={node_port}
export LND_MACAROON={macaroon_filename}"""
print(chalk.red.bold("This environment file must be loaded to access your node!"))
print(chalk.red.bg_yellow(env_file))
if save_env_file:
print("Writing file....")
credential_path.joinpath("lnd").mkdir( parents=True, exist_ok=True)
env_file_path = credential_path.joinpath("node-env")
with open(env_file_path, "w") as f:
f.write(env_file)

print(f"Wrote environment file to location: {env_file_path}")
print(f"Enable it by running:", chalk.red.bg_yellow(f"source {env_file_path}"))
else:
print("Not saving file...")


cli.add_command(shell)
cli.add_command(credentials)
cli.add_command(environment)


if __name__ == '__main__':
cli()
12 changes: 6 additions & 6 deletions lndgrpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class LNDClient(
pass


def cli():
import code
# LNDClient gets all configuration parameters from environment variables!
lnd = LNDClient()
# def cli():
# import code
# # LNDClient gets all configuration parameters from environment variables!
# lnd = LNDClient()

# Enter a shell for interacting with LND
code.interact(local=dict(globals(), **locals()))
# # Enter a shell for interacting with LND
# code.interact(local=dict(globals(), **locals()))
3 changes: 2 additions & 1 deletion lndgrpc/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def __init__(
network = os.getenv("LND_NETWORK", None)
node_ip = os.getenv("LND_NODE_IP")
node_port = os.getenv("LND_NODE_PORT")
lnd_macaroon = os.getenv("LND_MACAROON", "admin.macaroon")
# Handle either passing in credentials_paths, or environment variable paths

# IF credential_path
Expand All @@ -149,7 +150,7 @@ def __init__(

if credential_path:
credential_path = Path(credential_path)
macaroon_filepath = str(credential_path.joinpath("admin.macaroon").absolute())
macaroon_filepath = str(credential_path.joinpath(lnd_macaroon).absolute())
cert_filepath = str(credential_path.joinpath("tls.cert").absolute())

elif root_dir and network:
Expand Down
Loading

0 comments on commit 70dffd4

Please sign in to comment.