0

I made a simple SHA3 Python script to generate Ethereum addresses and priv/pub keys. After that I sent some ETH to those addresses but then found out that priv keys do not correspond to the addresses I have. I think I misunderstood behavior of update method in Python keccak_256 implementation. Do I still have any chance to get priv keys for the addresses I got using this script?

from ecdsa import SigningKey, SECP256k1
import sha3, sys

n = 5
full_file = "addresses.txt"
keccak = sha3.keccak_256()
with open(full_file, "r") as f:
    for i in range(n):
        priv = SigningKey.generate(curve=SECP256k1)
        pub = priv.get_verifying_key().to_string()
        keccak.update(pub)
        address = keccak.hexdigest()[24:]
        print address
        pr_key = str(priv.to_string().hex())
        pub_key = str(pub.hex())
        address_str = "0x" + address
        f.write(address_str + " " + pr_key + " " + pub_key + "\n")

Thank you

0

It seems that what you did nearly works. I just modified your code snippet a bit:

from ecdsa import SigningKey, SECP256k1
import sha3, sys

n = 5
full_file = "addresses.txt"
keccak = sha3.keccak_256()
with open(full_file, "w") as f:
    f.write("address                                    | private key\n")
    f.write("---------------------------------------------------------------------------------------------------------------\n")
    for i in range(n):
        priv = SigningKey.generate(curve=SECP256k1)
        pub = priv.get_verifying_key().to_string()
        keccak.update(pub)
        address = keccak.hexdigest()[24:]
        pr_key = str(priv.to_string().hex())
        pub_key = str(pub.hex())
        f.write("0x" + address + " | " + "0x" + pr_key + "\n")

When I generate the public an private keys with the script above and check the output of the Web3js-Function web3.eth.accounts.privateKeyToAccount(privateKey) with the according private key as input I get the public key as expected. There were only two things I needed to correct:

  1. The file full_file needs to be opened as writable ("w") not readable ("r").
  2. The private key also needs the 0x-Prefix just as the address (which is the public key...)

Hope it helps.

  • thank you for the attention to this problem And did you check if the addresses that you got really correspond to the pubkeys you have got? (this part was actually the problem) – Galileo Feb 13 '18 at 17:35
  • Yes indeed. As written above I checked the result by means of the privateKeyToAccount function. And the public key I received matched the right public key. So first private key of the outputted file gives first public key of the file. – joffi Feb 14 '18 at 13:42
  • correct. and do you have idea why first public key of the outputted file DOES NOT give first public address of the file? – Galileo Feb 14 '18 at 16:10
  • What do you mean with the "first public address"? According to this post the address is only a part of the public key. So if the public keys are okay the address should be fine as well. Am I missing something? – joffi Feb 15 '18 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.