From dd5787e16199ea26791f10f7b03d2662f8e88f6a Mon Sep 17 00:00:00 2001 From: gauravlochab Date: Thu, 28 Nov 2024 22:14:24 +0530 Subject: [PATCH] fix:hiding secrets --- run_service.py | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/run_service.py b/run_service.py index 4a92b26..7863d6f 100644 --- a/run_service.py +++ b/run_service.py @@ -19,6 +19,9 @@ """Olas Modius Quickstart script.""" import warnings warnings.filterwarnings("ignore", category=UserWarning) +import sys +import tty +import termios import getpass import json import os @@ -128,6 +131,33 @@ def estimate_priority_fee( return values[len(values) // 2] +def get_masked_input(prompt: str) -> str: + """Get user input while masking it with asterisks.""" + password = "" + sys.stdout.write(prompt) + sys.stdout.flush() + + fd = sys.stdin.fileno() + old_settings = termios.tcgetattr(fd) + try: + tty.setraw(fd) + while True: + char = sys.stdin.read(1) + if char == '\r' or char == '\n': + break + if char == '\x7f': + if password: + password = password[:-1] + sys.stdout.write('\b \b') + else: + password += char + sys.stdout.write('*') + sys.stdout.flush() + finally: + termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) + sys.stdout.write('\n') + return password + @dataclass class OptimusConfig(LocalResource): """Local configuration.""" @@ -213,7 +243,6 @@ def ask_confirm_password() -> str: print("Passwords do not match. Terminating.") sys.exit(1) - def check_rpc(rpc_url: str) -> None: spinner = Halo(text=f"Checking RPC...", spinner="dots") spinner.start() @@ -284,22 +313,22 @@ def configure_local_config() -> OptimusConfig: if optimus_config.tenderly_access_key is None: print_section("Tenderly API Configuration and Price Data Source") - optimus_config.tenderly_access_key = input( + optimus_config.tenderly_access_key = get_masked_input( "Please enter your Tenderly API Key. Get one at https://dashboard.tenderly.co/: " ) if optimus_config.tenderly_account_slug is None: - optimus_config.tenderly_account_slug = input( + optimus_config.tenderly_account_slug = get_masked_input( "Please enter your Tenderly Account Slug: " ) if optimus_config.tenderly_project_slug is None: - optimus_config.tenderly_project_slug = input( + optimus_config.tenderly_project_slug = get_masked_input( "Please enter your Tenderly Project Slug: " ) if optimus_config.coingecko_api_key is None: - optimus_config.coingecko_api_key = input( + optimus_config.coingecko_api_key = get_masked_input( "Please enter your CoinGecko API Key. Get one at https://www.coingecko.com/: " ) print() @@ -358,7 +387,7 @@ def configure_local_config() -> OptimusConfig: if optimus_config.mode_rpc is None: print_section("Chain RPC") - optimus_config.mode_rpc = input("Please enter a Mode RPC URL: ") + optimus_config.mode_rpc = get_masked_input("Please enter a Mode RPC URL: ") print()