Created
December 12, 2024 12:54
-
-
Save royki/e3a0888af618dd3eea386219e1e01853 to your computer and use it in GitHub Desktop.
Create Eth Signer Address, private key with encrypted Keystore file with password
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from eth_account import Account | |
import json | |
import subprocess | |
import os | |
from datetime import datetime | |
# Generate a random password using OpenSSL's rand -hex 32 command | |
def generate_random_password(): | |
# Generate a random hex string of 32 bytes (64 characters) | |
result = subprocess.run(['openssl', 'rand', '-hex', '32'], capture_output=True, text=True) | |
return result.stdout.strip() | |
# Generate a new Ethereum account | |
account = Account.create() | |
private_key = account.key.hex() | |
ethereum_address = account.address | |
# Generate a random password for the keystore file | |
password = generate_random_password() | |
# Encrypt the private key to create a keystore file | |
keystore = Account.encrypt(private_key, password) | |
# Get the current timestamp in the required format | |
timestamp = datetime.utcnow().strftime('%Y-%m-%dT%H-%M-%S.%fZ') | |
# Ethereum address without the 0x prefix | |
eth_address_without_prefix = ethereum_address[2:] | |
# Construct the keystore file name | |
keystore_file_name = f'UTC--{timestamp}--{eth_address_without_prefix}' | |
keystore_file_path = f'{keystore_file_name}' | |
# Save the keystore file to a JSON file | |
with open(keystore_file_path, 'w') as file: | |
json.dump(keystore, file, indent=4) | |
# Save the password to a .secret file | |
secret_file_path = f'.secret_{ethereum_address}' | |
with open(secret_file_path, 'w') as file: | |
file.write(password) | |
# Save the private key to a .privatekey file | |
privatekey_file_path = f'.privatekey_{ethereum_address}' | |
with open(privatekey_file_path, 'w') as file: | |
file.write(private_key) | |
# Save all results to a eth_account_0xxxxx.txt file | |
results_file_path = f'eth_account_{ethereum_address}.txt' | |
with open(results_file_path, 'w') as file: | |
file.write(f'Ethereum Address: {ethereum_address}\n') | |
file.write(f'Private Key: {private_key}\n') | |
file.write(f'Keystore File Path: {os.path.abspath(keystore_file_path)}\n') | |
file.write(f'Secret File Path: {os.path.abspath(secret_file_path)}\n') | |
file.write(f'Private Key File Path: {os.path.abspath(privatekey_file_path)}\n') | |
# Output the results | |
print(f'Ethereum Address: {ethereum_address}') | |
print(f'Private Key: {private_key}') | |
print(f'Keystore File Path: {os.path.abspath(keystore_file_path)}') | |
print(f'Secret File Path: {os.path.abspath(secret_file_path)}') | |
print(f'Private Key File Path: {os.path.abspath(privatekey_file_path)}') | |
print(f'Results File Path: {os.path.abspath(results_file_path)}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment