refactoring

This commit is contained in:
2025-07-14 12:54:52 +03:00
parent cd50a3b92a
commit 6b0937930c

View File

@@ -1,7 +1,10 @@
import gc
import os import os
import json import json
import getpass import getpass
from pathlib import Path from pathlib import Path
import rich
from web3 import Web3 from web3 import Web3
from Crypto.Cipher import AES from Crypto.Cipher import AES
from Crypto.Protocol.KDF import PBKDF2 from Crypto.Protocol.KDF import PBKDF2
@@ -14,7 +17,6 @@ WALLETS_DIR = "wallets"
web3 = None web3 = None
selected_wallet = None selected_wallet = None
wallet_data = None
if not os.path.exists(WALLETS_DIR): if not os.path.exists(WALLETS_DIR):
os.makedirs(WALLETS_DIR) os.makedirs(WALLETS_DIR)
@@ -79,33 +81,32 @@ def save_wallet(account):
def select_wallet(): def select_wallet():
global selected_wallet, wallet_data global selected_wallet
files = os.listdir(WALLETS_DIR) files = os.listdir(WALLETS_DIR)
if not files: if not files:
print("[red]Нет доступных кошельков.[/]") print("[red]Нет доступных кошельков.[/]")
return return
for i, fname in enumerate(files): for i, fname in enumerate(files):
print(f"[{i}] {Path(fname).stem}") print(f"[orange1][{i}] {Path(fname).stem}[/]")
try: try:
choice = int(Prompt.ask("Выберите номер кошелька")) choice = int(Prompt.ask("[bold cyan]Выберите номер кошелька[/]"))
except: except:
print("[red]Неверный номер.[/]") print("[red]Неверный номер.[/]")
return return
with open(os.path.join(WALLETS_DIR, files[choice])) as f: with open(os.path.join(WALLETS_DIR, files[choice])) as f:
wallet_data = json.load(f) selected_wallet = json.load(f)
selected_wallet = wallet_data['address'] print(f"[green]Кошелек выбран: {selected_wallet['address']}[/]")
print(f"[green]Кошелек выбран: {selected_wallet}[/]")
def get_balance(): def get_balance():
if not selected_wallet: if not selected_wallet:
print("[red]Кошелек не выбран.[/]") print("[red]Кошелек не выбран.[/]")
return return
balance = web3.eth.get_balance(selected_wallet) balance = web3.eth.get_balance(selected_wallet['address'])
print(f"Баланс: {web3.from_wei(balance, 'ether')} ETH") print(f"[orange1]Баланс: {web3.from_wei(balance, 'ether')} ETH")
def send_transaction(): def send_transaction():
@@ -113,15 +114,6 @@ def send_transaction():
print("[red]Кошелек не выбран.[/]") print("[red]Кошелек не выбран.[/]")
return return
print("[bold cyan]Введите пароль от кошелька: [/]", end='')
password = getpass.getpass(prompt="")
try:
private_key = decrypt_wallet(wallet_data['data'], password)
except:
print("[red]Неверный пароль![/]")
return
to = Prompt.ask("[bold cyan]Введите адрес получателя[/]") to = Prompt.ask("[bold cyan]Введите адрес получателя[/]")
value = float(Prompt.ask("[bold cyan]Введите сумму в ETH[/]")) value = float(Prompt.ask("[bold cyan]Введите сумму в ETH[/]"))
@@ -134,7 +126,7 @@ def send_transaction():
tx = { tx = {
'chainId': web3.eth.chain_id, 'chainId': web3.eth.chain_id,
'nonce': web3.eth.get_transaction_count(selected_wallet), 'nonce': web3.eth.get_transaction_count(selected_wallet['address']),
'to': to, 'to': to,
'value': web3.to_wei(value, 'ether'), 'value': web3.to_wei(value, 'ether'),
'gas': 21000, 'gas': 21000,
@@ -142,13 +134,25 @@ def send_transaction():
'maxPriorityFeePerGas': max_priority_fee 'maxPriorityFeePerGas': max_priority_fee
} }
print("[bold cyan]Введите пароль от кошелька: [/]", end='')
password = getpass.getpass(prompt="")
try:
private_key = decrypt_wallet(selected_wallet['data'], password)
del password
except:
print("[red]Неверный пароль![/]")
return
try: try:
signed = web3.eth.account.sign_transaction(tx, private_key) signed = web3.eth.account.sign_transaction(tx, private_key)
del private_key
tx_hash = web3.eth.send_raw_transaction(signed.raw_transaction) tx_hash = web3.eth.send_raw_transaction(signed.raw_transaction)
print(f"[green]Транзакция отправлена! TX Hash: {tx_hash.hex()}[/]") print(f"[green]Транзакция отправлена! TX Hash: {tx_hash.hex()}[/]")
except Exception as error: except Exception as error:
print(f"[red]{error.message}[/]") print(f"[red]{error.message}[/]")
finally:
gc.collect()
def main(): def main():
@@ -164,7 +168,7 @@ def main():
""") """)
while True: while True:
choice = Prompt.ask("Выберите действие").lower() choice = Prompt.ask("[slate_blue1]Выберите действие[/]").lower()
if choice == 'a': if choice == 'a':
connect_to_network() connect_to_network()
elif choice == 'c': elif choice == 'c':