multiple times password entering
This commit is contained in:
125
wallet.py
125
wallet.py
@@ -48,7 +48,7 @@ def connect_to_network():
|
|||||||
if web3.is_connected():
|
if web3.is_connected():
|
||||||
print("[green1]Подключено![/]")
|
print("[green1]Подключено![/]")
|
||||||
else:
|
else:
|
||||||
print("[red]Ошибка подключения к сети![/]")
|
print("[red1]Ошибка подключения к сети![/]")
|
||||||
web3 = None
|
web3 = None
|
||||||
|
|
||||||
|
|
||||||
@@ -57,6 +57,10 @@ def create_wallet():
|
|||||||
acct = Account.create()
|
acct = Account.create()
|
||||||
save_wallet(acct)
|
save_wallet(acct)
|
||||||
|
|
||||||
|
show_private_key = Prompt.ask("[bold cyan]Показать приватный ключ?", choices=["y", "n"], default="y")
|
||||||
|
if show_private_key == 'y':
|
||||||
|
print(f"[orange1]{acct.key.hex()}")
|
||||||
|
|
||||||
|
|
||||||
def import_wallet():
|
def import_wallet():
|
||||||
print("[bold cyan]Введите приватный ключ: [/]", end='')
|
print("[bold cyan]Введите приватный ключ: [/]", end='')
|
||||||
@@ -65,13 +69,35 @@ def import_wallet():
|
|||||||
save_wallet(account)
|
save_wallet(account)
|
||||||
|
|
||||||
|
|
||||||
|
def check_password():
|
||||||
|
return get_private_key() is not None
|
||||||
|
|
||||||
|
|
||||||
|
def get_private_key():
|
||||||
|
print("[bold cyan]Введите пароль от кошелька: [/]", end='')
|
||||||
|
password = getpass.getpass(prompt="")
|
||||||
|
|
||||||
|
private_key = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
private_key = decrypt_wallet(selected_wallet['data'], password)
|
||||||
|
del password
|
||||||
|
except:
|
||||||
|
print("[red1]Неверный пароль![/]")
|
||||||
|
|
||||||
|
return private_key
|
||||||
|
|
||||||
|
|
||||||
def delete_wallet():
|
def delete_wallet():
|
||||||
global selected_wallet
|
global selected_wallet
|
||||||
|
|
||||||
|
while not check_password():
|
||||||
|
pass
|
||||||
|
|
||||||
address = Prompt.ask("[red1]При удалении кошелька доступ к средствам будет потерян, введите номер кошелька для подтверждения операции")
|
address = Prompt.ask("[red1]При удалении кошелька доступ к средствам будет потерян, введите номер кошелька для подтверждения операции")
|
||||||
|
|
||||||
if address != selected_wallet['address']:
|
if address != selected_wallet['address']:
|
||||||
print("[red]Операция отменена")
|
print("[red1]Операция отменена")
|
||||||
return
|
return
|
||||||
|
|
||||||
file_path = os.path.join(WALLETS_DIR, selected_wallet['address']+'.json')
|
file_path = os.path.join(WALLETS_DIR, selected_wallet['address']+'.json')
|
||||||
@@ -99,7 +125,7 @@ def select_wallet():
|
|||||||
global selected_wallet
|
global selected_wallet
|
||||||
files = os.listdir(WALLETS_DIR)
|
files = os.listdir(WALLETS_DIR)
|
||||||
if not files:
|
if not files:
|
||||||
print("[red]Нет доступных кошельков.[/]")
|
print("[red1]Нет доступных кошельков.[/]")
|
||||||
return
|
return
|
||||||
|
|
||||||
for i, fname in enumerate(files):
|
for i, fname in enumerate(files):
|
||||||
@@ -108,7 +134,7 @@ def select_wallet():
|
|||||||
try:
|
try:
|
||||||
choice = int(Prompt.ask("[bold cyan]Выберите номер кошелька[/]"))
|
choice = int(Prompt.ask("[bold cyan]Выберите номер кошелька[/]"))
|
||||||
except:
|
except:
|
||||||
print("[red]Неверный номер.[/]")
|
print("[red1]Неверный номер.[/]")
|
||||||
return
|
return
|
||||||
|
|
||||||
with open(os.path.join(WALLETS_DIR, files[choice-1])) as f:
|
with open(os.path.join(WALLETS_DIR, files[choice-1])) as f:
|
||||||
@@ -117,7 +143,7 @@ def select_wallet():
|
|||||||
|
|
||||||
def get_balance():
|
def get_balance():
|
||||||
if not selected_wallet:
|
if not selected_wallet:
|
||||||
print("[red]Кошелек не выбран.[/]")
|
print("[red1]Кошелек не выбран.[/]")
|
||||||
return
|
return
|
||||||
balance = web3.eth.get_balance(selected_wallet['address'])
|
balance = web3.eth.get_balance(selected_wallet['address'])
|
||||||
print(f"[orange1]Баланс: {web3.from_wei(balance, 'ether')} ETH")
|
print(f"[orange1]Баланс: {web3.from_wei(balance, 'ether')} ETH")
|
||||||
@@ -148,15 +174,9 @@ def send_transaction():
|
|||||||
'maxPriorityFeePerGas': max_priority_fee
|
'maxPriorityFeePerGas': max_priority_fee
|
||||||
}
|
}
|
||||||
|
|
||||||
print("[bold cyan]Введите пароль от кошелька: [/]", end='')
|
private_key = None
|
||||||
password = getpass.getpass(prompt="")
|
while not private_key:
|
||||||
|
private_key = get_private_key()
|
||||||
try:
|
|
||||||
private_key = decrypt_wallet(selected_wallet['data'], password)
|
|
||||||
del password
|
|
||||||
except:
|
|
||||||
print("[red1]Неверный пароль![/]")
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
signed = web3.eth.account.sign_transaction(tx, private_key)
|
signed = web3.eth.account.sign_transaction(tx, private_key)
|
||||||
@@ -170,7 +190,7 @@ def send_transaction():
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print("""
|
menu = """
|
||||||
[bold blue]Меню:[/]
|
[bold blue]Меню:[/]
|
||||||
a. Подключение к сети Ethereum
|
a. Подключение к сети Ethereum
|
||||||
c. Создание кошелька
|
c. Создание кошелька
|
||||||
@@ -180,39 +200,54 @@ def main():
|
|||||||
i. Импорт существующего кошелька
|
i. Импорт существующего кошелька
|
||||||
d. Удаление кошелька
|
d. Удаление кошелька
|
||||||
q. Выход
|
q. Выход
|
||||||
""")
|
"""
|
||||||
|
|
||||||
|
print(menu)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
choice = Prompt.ask("[slate_blue1]Выберите действие[/]").lower()
|
try:
|
||||||
if choice == 'a':
|
choice = Prompt.ask("[slate_blue1]Выберите действие[/]").lower()
|
||||||
connect_to_network()
|
|
||||||
elif choice == 'c':
|
|
||||||
create_wallet()
|
|
||||||
elif choice == 'w':
|
|
||||||
select_wallet()
|
|
||||||
elif choice == 'b':
|
|
||||||
if web3 and selected_wallet:
|
|
||||||
get_balance()
|
|
||||||
else:
|
|
||||||
print("[red1]Необходимо подключиться к сети и выбрать кошелек[/]")
|
|
||||||
elif choice == 's':
|
|
||||||
if web3 and selected_wallet:
|
|
||||||
send_transaction()
|
|
||||||
else:
|
|
||||||
print("[red1]Необходимо подключиться к сети и выбрать кошелек[/]")
|
|
||||||
elif choice == 'i':
|
|
||||||
import_wallet()
|
|
||||||
elif choice == 'd':
|
|
||||||
if not selected_wallet:
|
|
||||||
select_wallet()
|
|
||||||
|
|
||||||
if selected_wallet:
|
if choice == 'a':
|
||||||
delete_wallet()
|
connect_to_network()
|
||||||
else:
|
elif choice == 'c':
|
||||||
print("[red1]Необходимо выбрать кошелек[/]")
|
create_wallet()
|
||||||
elif choice == 'q':
|
elif choice == 'w':
|
||||||
break
|
select_wallet()
|
||||||
|
elif choice == 'b':
|
||||||
|
if web3 and selected_wallet:
|
||||||
|
get_balance()
|
||||||
|
else:
|
||||||
|
print("[red1]Необходимо подключиться к сети и выбрать кошелек[/]")
|
||||||
|
elif choice == 's':
|
||||||
|
if web3 and selected_wallet:
|
||||||
|
send_transaction()
|
||||||
|
else:
|
||||||
|
print("[red1]Необходимо подключиться к сети и выбрать кошелек[/]")
|
||||||
|
elif choice == 'i':
|
||||||
|
import_wallet()
|
||||||
|
elif choice == 'd':
|
||||||
|
if not selected_wallet:
|
||||||
|
select_wallet()
|
||||||
|
|
||||||
|
if selected_wallet:
|
||||||
|
delete_wallet()
|
||||||
|
else:
|
||||||
|
print("[red1]Необходимо выбрать кошелек[/]")
|
||||||
|
elif choice == 'q':
|
||||||
|
break
|
||||||
|
# system commands
|
||||||
|
elif choice == "clear":
|
||||||
|
if os.name == 'nt':
|
||||||
|
_ = os.system('cls')
|
||||||
|
else:
|
||||||
|
_ = os.system('clear')
|
||||||
|
print(menu)
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
continue
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user