I was trying to generate the private and public key of an Ethereum account keyfile(one which gets generated at new account creation) and the password of account.
After I found 1st method, I tried to run keccak on public key to get hash and checked if its giving the account address or not. It printed some different address. So then I tried for another method, this time also I got same keypair.
Now I am confused about
- Is my code to get keypair is false(both the methods)?
- OR I am doing keccack part wrong?
- OR I am having completely wrong understanding of ethereum keypairs and address relation?
from web3.auto import w3 import binascii import ecdsa import codecs password = "Password string goes here" #Getting privatekey with open(".//UTC--2022-06-16T17-01-58.671279300Z--af043ebbcc38ed68595ecd752040499afc4a69a0") as keyfile: encrypted = keyfile.read() pvt_key = w3.eth.account.decrypt(encrypted, password) print("Private key method 1: ") print(binascii.b2a_hex(pvt_key)) print() #Getting public key private_key_bytes = bytearray.fromhex((binascii.b2a_hex(pvt_key)).decode("utf-8")) key = ecdsa.SigningKey.from_string(private_key_bytes, curve=ecdsa.SECP256k1).verifying_key key_bytes = key.to_string() key_hex = codecs.encode(key_bytes, 'hex') print("Public key method 1:") print(key_hex) print() #Another method import eth_keys from eth_keys import keys from eth_utils import decode_hex from eth_account import account with open(".//UTC--2022-06-16T17-01-58.671279300Z--af043ebbcc38ed68595ecd752040499afc4a69a0") as keyfile: encrypted = keyfile.read() private_key = eth_keys.keys.PrivateKey(account.Account.decrypt(encrypted, password)) print("Private key method 2: ") print(private_key) print() priv_key_bytes = decode_hex(pvt_key.hex()) priv_key = keys.PrivateKey(priv_key_bytes) pub_key = priv_key.public_key print("Public key method 2: ") print(pub_key) print() #Method to get adress after running keccak on public key #Not sure if its correct from Crypto.Hash import keccak public_key_bytes = bytearray.fromhex((binascii.b2a_hex(key_hex)).decode("utf-8")) keccak_hash = keccak.new(digest_bits=256) keccak_hash.update(public_key_bytes) keccak_digest = keccak_hash.hexdigest() wallet = "0x" + keccak_digest[-40:] print(wallet)